From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: (interactive) and &optional
Date: Thu, 23 Mar 2023 21:01:51 +0100 [thread overview]
Message-ID: <87ilerkzio.fsf@dataswamp.org> (raw)
In-Reply-To: 25628.42021.832534.376361@woitok.gmail.com
Rainer Woitok wrote:
> In an attempt to write a function with and optional argument
> which is both, callable from Lisp and via "M-x", I ran into
> some unexpected (by me) problems. Consider the following
> function:
>
> (defun fun (&optional arg)
> (interactive "Sarg: ")
> (message "%s:%s.\n" 'val (symbol-name arg)))
See this file. The lines like this
(or beg (setq beg (point-min)))
are what does it.
If you do it like this, it always work both interactively and
from Lisp and you are able to set a default value that is the
same for both methods.
;;; -*- lexical-binding: t -*-
;;
;; this file:
;; https://dataswamp.org/~incal/emacs-init/dwim.el
;;
;; DWIM code helpers and examples.
;;
;; Advantages to this style:
;;
;; - the same default interactively and from Lisp
;; - the default is the whole buffer
;; - the region is never used from Lisp
;; - the variables are always set, to the default if not explicitely
;; - one can still have preceding, non-optional arguments
(defun use-region (&optional both)
(if (use-region-p)
(list (region-beginning) (region-end))
(when both
(list nil nil) )))
(defun test-dwim (&optional beg end)
(interactive (use-region))
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(message "%d %d" beg end) )
(defun test-dwim-2 (re &optional beg end)
(interactive `(,(read-regexp "regexp: ") ,@(use-region)))
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(message "%s %d %d" re beg end) )
(provide 'dwim)
--
underground experts united
https://dataswamp.org/~incal
next prev parent reply other threads:[~2023-03-23 20:01 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-23 19:10 (interactive) and &optional Dr Rainer Woitok
2023-03-23 19:20 ` Philip Kaludercic
2023-03-25 12:35 ` Dr Rainer Woitok
2023-03-25 13:12 ` Philip Kaludercic
2023-03-25 14:33 ` [External] : " Drew Adams
2023-03-25 15:36 ` Dr Rainer Woitok
2023-03-25 16:58 ` Philip Kaludercic
2023-03-23 20:01 ` Emanuel Berg [this message]
2023-03-24 9:23 ` Jean Louis
2023-03-24 20:52 ` Philip Kaludercic
2023-03-24 21:07 ` Emanuel Berg
2023-03-27 1:52 ` [External] : " Drew Adams
2023-03-27 2:40 ` John Yates
2023-03-24 21:42 ` Jean Louis
2023-03-26 1:04 ` Emanuel Berg
2023-03-30 19:04 ` Jean Louis
2023-03-25 12:05 ` Dr Rainer Woitok
2023-03-25 14:32 ` Jean Louis
2023-03-25 15:41 ` Dr Rainer Woitok
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ilerkzio.fsf@dataswamp.org \
--to=incal@dataswamp.org \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).