unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Dr Rainer Woitok <rainer.woitok@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: (interactive) and &optional
Date: Thu, 23 Mar 2023 19:20:29 +0000	[thread overview]
Message-ID: <87jzz746ma.fsf@posteo.net> (raw)
In-Reply-To: <25628.42021.832534.376361@woitok.gmail.com> (Rainer Woitok's message of "Thu, 23 Mar 2023 20:10:32 +0100")

Dr Rainer Woitok <rainer.woitok@gmail.com> writes:

> Greetings,
>
> 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)))

This would have done the same thing:

     (message "val: %s.\n" arg)

>
> Calling "M-: (fun 'a)" returns
>
>    "val:a.
>    "
>
> including the double quotes,  while calling "M-x fun" and then typing "a
> RET" at the prompt returns
>
>    val:a.\n

That is because `message' returns the message as a string when
evaluated, and using M-: you first have `message' do it's thing, then
return the string it printed (using the printed representation for a
string, which includes queotes).  You can verify this by looking up the
message log using C-h e.

> without double quotes.  Apart from perhaps the double quotes, this is
> what I had expected.  Likewise, calling "M-: (fun)" returns
>
>   "val:nil.
>   "
>
> as expected,  while calling "M-x fun" and then just typing  "RET" at the
> prompt returns
>
>   val:.\n
>
> that is, an empty symbol or string.

Right.

> Am I really expected in a function that is both,  callable from Lisp and
> via "M-x", to code something along the lines of
>
>    (cond ((or (null arg) (string-empty-p arg)) 'default-val)
>          (arg))
>
> to check whether or not an optional argument has been passed?  Are there
> any more elegant ways to achieve this?

The interactive spec can either take a string as you did here, or an
expression that will be evaluated to generate a list of arguments.  So
the following will read a string (not a symbol), check if the string is
empty (I don't think the nil check is necessary) in which case the
symbol `default-var' is returned, otherwise we use `intern' to request a
symbol with the name of whatever we just queried the user:

--8<---------------cut here---------------start------------->8---
(defun fun (&optional arg)
  (interactive (list (let ((name (read-string "arg: ")))
		       (if (string-empty-p name)
			   'default-val
			 (intern name)))))
  (message "val: %s.\n" arg))
--8<---------------cut here---------------end--------------->8---

> Any pointers welcome :-)
>
> Sincerely,
>   Rainer

-- 
Philip Kaludercic



  reply	other threads:[~2023-03-23 19:20 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 [this message]
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
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=87jzz746ma.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=help-gnu-emacs@gnu.org \
    --cc=rainer.woitok@gmail.com \
    /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).