all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* (interactive) and &optional
@ 2023-03-23 19:10 Dr Rainer Woitok
  2023-03-23 19:20 ` Philip Kaludercic
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Dr Rainer Woitok @ 2023-03-23 19:10 UTC (permalink / raw)
  To: Help-Gnu-Emacs

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

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

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.

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?

Any pointers welcome :-)

Sincerely,
  Rainer



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  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-23 20:01 ` Emanuel Berg
  2023-03-24  9:23 ` Jean Louis
  2 siblings, 1 reply; 19+ messages in thread
From: Philip Kaludercic @ 2023-03-23 19:20 UTC (permalink / raw)
  To: Dr Rainer Woitok; +Cc: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-23 19:10 (interactive) and &optional Dr Rainer Woitok
  2023-03-23 19:20 ` Philip Kaludercic
@ 2023-03-23 20:01 ` Emanuel Berg
  2023-03-24  9:23 ` Jean Louis
  2 siblings, 0 replies; 19+ messages in thread
From: Emanuel Berg @ 2023-03-23 20:01 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-23 19:10 (interactive) and &optional Dr Rainer Woitok
  2023-03-23 19:20 ` 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-25 12:05   ` Dr Rainer Woitok
  2 siblings, 2 replies; 19+ messages in thread
From: Jean Louis @ 2023-03-24  9:23 UTC (permalink / raw)
  To: Dr Rainer Woitok; +Cc: Help-Gnu-Emacs

* Dr Rainer Woitok <rainer.woitok@gmail.com> [2023-03-23 22:12]:
> 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?

I do not like involving `interactive' at all, so I do the handling by
providing optional argument and telling function to check for it, or
ask for it.

(defun search-something (&optional query)
  "Search something by using QUERY."
  (interactive)
  (let* ((query (or query (read-from-minibuffer "Query: "))))
    (do-the-search query)))

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-24  9:23 ` Jean Louis
@ 2023-03-24 20:52   ` Philip Kaludercic
  2023-03-24 21:07     ` Emanuel Berg
  2023-03-24 21:42     ` Jean Louis
  2023-03-25 12:05   ` Dr Rainer Woitok
  1 sibling, 2 replies; 19+ messages in thread
From: Philip Kaludercic @ 2023-03-24 20:52 UTC (permalink / raw)
  To: Dr Rainer Woitok; +Cc: Help-Gnu-Emacs

Jean Louis <bugs@gnu.support> writes:

> * Dr Rainer Woitok <rainer.woitok@gmail.com> [2023-03-23 22:12]:
>> 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?
>
> I do not like involving `interactive' at all, so I do the handling by
> providing optional argument and telling function to check for it, or
> ask for it.
>
> (defun search-something (&optional query)
>   "Search something by using QUERY."
>   (interactive)
>   (let* ((query (or query (read-from-minibuffer "Query: "))))
>     (do-the-search query)))

The issue here is that the function is difficult to re-use from other
scripts.  If the interactive stuff is encapsulated within an
(interactive) form then you avoid the issue.

-- 
Philip Kaludercic



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-24 20:52   ` Philip Kaludercic
@ 2023-03-24 21:07     ` Emanuel Berg
  2023-03-27  1:52       ` [External] : " Drew Adams
  2023-03-24 21:42     ` Jean Louis
  1 sibling, 1 reply; 19+ messages in thread
From: Emanuel Berg @ 2023-03-24 21:07 UTC (permalink / raw)
  To: help-gnu-emacs

Philip Kaludercic wrote:

> The issue here is that the function is difficult to re-use
> from other scripts. If the interactive stuff is encapsulated
> within an (interactive) form then you avoid the issue.

`interactive' is the right place for everything interactive,
so unless there is a specific reason why it is impractical to
put it there, that's where one should put the
interactive stuff.

But I do get the feeling one could improve `interactive' ...

Note that you can also use `cl-defun' to put the default
values there, as in

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/dice.el

(require 'cl-lib)

(cl-defun dice (&optional (sides 6) (num 1))
  (cl-loop
    repeat num
    with sum = 0
    do (cl-incf sum (1+ (random sides)))
    finally return sum) )

;; (dice)     ; 1D6, 1-6
;; (dice 3)   ; 1D3, 1-3
;; (dice 3 2) ; 2D3, 2-6

(provide 'dice)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-24 20:52   ` Philip Kaludercic
  2023-03-24 21:07     ` Emanuel Berg
@ 2023-03-24 21:42     ` Jean Louis
  2023-03-26  1:04       ` Emanuel Berg
  1 sibling, 1 reply; 19+ messages in thread
From: Jean Louis @ 2023-03-24 21:42 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: Dr Rainer Woitok, Help-Gnu-Emacs

* Philip Kaludercic <philipk@posteo.net> [2023-03-24 23:54]:
> > I do not like involving `interactive' at all, so I do the handling by
> > providing optional argument and telling function to check for it, or
> > ask for it.
> >
> > (defun search-something (&optional query)
> >   "Search something by using QUERY."
> >   (interactive)
> >   (let* ((query (or query (read-from-minibuffer "Query: "))))
> >     (do-the-search query)))
> 
> The issue here is that the function is difficult to re-use from other
> scripts.  If the interactive stuff is encapsulated within an
> (interactive) form then you avoid the issue.

I understand the meaning in general, though I do not understand what
is the mentioned issue. Could you explain it?

It is interesting as I use those functions from other functions.

This should be the classic Emacs Lisp approach:

(defun search-something (query)
   "Search something by using QUERY."
   (interactive "MQuery: ")
   (message query))

(progn (search-something "Hello")) ➜ "Hello"
(progn (search-something)) ➜ "Ok here"

This should be my approach:

(defun search-something (&optional query)
   "Search something by using QUERY."
   (interactive)
   (let ((query (or query (read-from-minibuffer "Query: "))))
     (message query)))

(progn (search-something "Hello")) ➜ "Hello"
(progn (search-something)) ➜ "Ok here"

- while `interactive' declaration may shorten some code, it may as
  well complicated readability of function

Here is short version where buffer must be existing buffer:

(defun my-message-buffer (buffer)
  "Message name of my BUFFER."
  (interactive "b")   
  (message buffer))

That short function I do not know how to make easy without (interactive "b"). 

For majority of uses in my code I do not use arguments to
`interactive' and there are no issues so far.

I find `interactive' complex and would like to know why it was
invented.

(info "(elisp) Using Interactive")
(info "(elisp) Interactive Codes")

Tell me examples on what issues you encounter and pointers to
implications.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-24  9:23 ` Jean Louis
  2023-03-24 20:52   ` Philip Kaludercic
@ 2023-03-25 12:05   ` Dr Rainer Woitok
  2023-03-25 14:32     ` Jean Louis
  1 sibling, 1 reply; 19+ messages in thread
From: Dr Rainer Woitok @ 2023-03-25 12:05 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help-Gnu-Emacs

Jean,

On Friday, 2023-03-24 12:23:29 +0300, you wrote:

> ...
> I do not like involving `interactive' at all, so I do the handling by
> providing optional argument and telling function to check for it, or
> ask for it.
> 
> (defun search-something (&optional query)
>   "Search something by using QUERY."
>   (interactive)
>   (let* ((query (or query (read-from-minibuffer "Query: "))))
>     (do-the-search query)))

Correct me if I'm wrong,  but doesn't this  unconditionally call  "read-
from-minibuffer"  when non-interactively called  without argument?  Non-
interactively I would like the function to simply use my default value.

Anyway, many thanks to you and the others having taken part in this dis-
cussion -- I've learned a few things :-)

Sincerely,
  Rainer



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-23 19:20 ` Philip Kaludercic
@ 2023-03-25 12:35   ` Dr Rainer Woitok
  2023-03-25 13:12     ` Philip Kaludercic
  0 siblings, 1 reply; 19+ messages in thread
From: Dr Rainer Woitok @ 2023-03-25 12:35 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: help-gnu-emacs

Philip,

On Thursday, 2023-03-23 19:20:29 +0000, you wrote:

> ...
> > 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.

That's a bit flabbergasting: experimenting some more with my function, I
found that "symbolp" really returns t in case of empty responses,  mean-
ing there really is such a thing as an empty symbol.   So how can I test
for an empty symbol?  There doesn't seem to be a function "symbol-empty-
p" while, strictly speaking,  "string-empty-p" shouldn't be applyable to
symbols at all, should it?  Does an empty symbol have an external repre-
sentation like an empty list or string, allowing the use of "eq"?

Sincerely,
  Rainer



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-25 12:35   ` Dr Rainer Woitok
@ 2023-03-25 13:12     ` Philip Kaludercic
  2023-03-25 14:33       ` [External] : " Drew Adams
  0 siblings, 1 reply; 19+ messages in thread
From: Philip Kaludercic @ 2023-03-25 13:12 UTC (permalink / raw)
  To: Dr Rainer Woitok; +Cc: help-gnu-emacs

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

> Philip,
>
> On Thursday, 2023-03-23 19:20:29 +0000, you wrote:
>
>> ...
>> > 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.
>
> That's a bit flabbergasting: experimenting some more with my function, I
> found that "symbolp" really returns t in case of empty responses,  mean-
> ing there really is such a thing as an empty symbol.   So how can I test
> for an empty symbol?  

I am guessing it is not an "empty symbol" (to my knowledge the term is
not defined), but rather the fact that `nil' is a symbol.  Depending on
what you did, it might also be that you are interning the empty string,
which creates a symbol with an empty string as the symbol name -- which
is not unique. 

>                       There doesn't seem to be a function "symbol-empty-
> p" while, strictly speaking,  "string-empty-p" shouldn't be applyable to
> symbols at all, should it?  

No, trivially because a symbols is not a string, and shouldn't be
regarded as similar data types.  Depending on the context, it might be
totally justified to regard the symbol name of a symbol as totally
incidental.

>                             Does an empty symbol have an external repre-
> sentation like an empty list or string, allowing the use of "eq"?

A symbol is eq to all other symbols that were either created by passing
the same (equal) string to `intern' or by the same invocation of
`make-symbol'.  That could be useful if you are trying to handle a
symbol that as an empty string as the symbol name -- but at that point I
think it would be better to just check the string before it is interned.

> Sincerely,
>   Rainer

-- 
Philip Kaludercic



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-25 12:05   ` Dr Rainer Woitok
@ 2023-03-25 14:32     ` Jean Louis
  2023-03-25 15:41       ` Dr Rainer Woitok
  0 siblings, 1 reply; 19+ messages in thread
From: Jean Louis @ 2023-03-25 14:32 UTC (permalink / raw)
  To: Dr Rainer Woitok; +Cc: help-gnu-emacs

* Dr Rainer Woitok <rainer.woitok@gmail.com> [2023-03-25 15:05]:
> > I do not like involving `interactive' at all, so I do the handling by
> > providing optional argument and telling function to check for it, or
> > ask for it.
> > 
> > (defun search-something (&optional query)
> >   "Search something by using QUERY."
> >   (interactive)
> >   (let* ((query (or query (read-from-minibuffer "Query: "))))
> >     (do-the-search query)))
> 
> Correct me if I'm wrong,  but doesn't this  unconditionally call  "read-
> from-minibuffer"  when non-interactively called  without argument?  Non-
> interactively I would like the function to simply use my default value.

Yet, it calls unconditionally if that is the choice of programmer.

If you want default value, then you can do so:

(let* ((query (or query my-default-value (read-from-minibuffer "Query: "))))
  (do-the-search query)))

or like this:

(let* ((query (or query my-default-value)))
  (do-the-search query))

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 19+ messages in thread

* RE: [External] : Re: (interactive) and &optional
  2023-03-25 13:12     ` Philip Kaludercic
@ 2023-03-25 14:33       ` Drew Adams
  2023-03-25 15:36         ` Dr Rainer Woitok
  2023-03-25 16:58         ` Philip Kaludercic
  0 siblings, 2 replies; 19+ messages in thread
From: Drew Adams @ 2023-03-25 14:33 UTC (permalink / raw)
  To: Philip Kaludercic, Dr Rainer Woitok; +Cc: help-gnu-emacs@gnu.org

> it might also be that you are interning the empty string,
> which creates a symbol with an empty string as the symbol name -- which
> is not unique.

What do you mean by "is not unique"?

> > Does an empty symbol have an external representation like an empty list or string, allowing the use of "eq"?

Yes: ##

(setq foo (intern ""))

C-h v foo

  foo's value is ##

(symbolp '##) ; => t
(symbolp foo) ; => t

(symbol-name '##) ; => ""
(symbol-name foo) ; => ""

(setq ## 42) ; or (set foo 42)

(symbol-value '##) ; => 42
##                 ; => 42

(But note that `C-h v ##' doesn't "work".)



^ permalink raw reply	[flat|nested] 19+ messages in thread

* RE: [External] : Re: (interactive) and &optional
  2023-03-25 14:33       ` [External] : " Drew Adams
@ 2023-03-25 15:36         ` Dr Rainer Woitok
  2023-03-25 16:58         ` Philip Kaludercic
  1 sibling, 0 replies; 19+ messages in thread
From: Dr Rainer Woitok @ 2023-03-25 15:36 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs@gnu.org

Drew,

On Saturday, 2023-03-25 14:33:46 +0000, you wrote:

> ...
> > > Does an empty symbol have an external representation like an empty list or string, allowing the use of "eq"?
> 
> Yes: ##

Thanks for that piece of information!   This lets me get rid of the awk-
ward (in my opinion) call "(string-empty-p arg)", where "arg" contains a
possibly empty symbol.

Sincerely,
  Rainer



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-25 14:32     ` Jean Louis
@ 2023-03-25 15:41       ` Dr Rainer Woitok
  0 siblings, 0 replies; 19+ messages in thread
From: Dr Rainer Woitok @ 2023-03-25 15:41 UTC (permalink / raw)
  To: Jean Louis; +Cc: help-gnu-emacs

Jean,

On Saturday, 2023-03-25 17:32:06 +0300, Jean Louis wrote:

> ...
> If you want default value, then you can do so:
> 
> (let* ((query (or query my-default-value (read-from-minibuffer "Query: "))))
>   (do-the-search query)))

But I wan BOTH, default value AND interactive input!  In the above code
function "read-from-minibuffer" will never be called, so it's totally
equivalent to your next piece of code:

> ...
> (let* ((query (or query my-default-value)))
>   (do-the-search query))

Sincerely,
  Rainer



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [External] : Re: (interactive) and &optional
  2023-03-25 14:33       ` [External] : " Drew Adams
  2023-03-25 15:36         ` Dr Rainer Woitok
@ 2023-03-25 16:58         ` Philip Kaludercic
  1 sibling, 0 replies; 19+ messages in thread
From: Philip Kaludercic @ 2023-03-25 16:58 UTC (permalink / raw)
  To: Drew Adams; +Cc: Dr Rainer Woitok, help-gnu-emacs@gnu.org

Drew Adams <drew.adams@oracle.com> writes:

>> it might also be that you are interning the empty string,
>> which creates a symbol with an empty string as the symbol name -- which
>> is not unique.
>
> What do you mean by "is not unique"?

Two symbols with the same symbol name (i.r.t. `equal') are not
necessarily the same symbol (i.r.t `eq').  So

(eq '## (make-symbol "")) ;=> nil

even though they both have the same name, 

(equal (symbol-name '##) (symbol-name (make-symbol ""))) ;=> t

which is why I don't think that the name "the empty symbol" makes sense.

-- 
Philip Kaludercic



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-24 21:42     ` Jean Louis
@ 2023-03-26  1:04       ` Emanuel Berg
  2023-03-30 19:04         ` Jean Louis
  0 siblings, 1 reply; 19+ messages in thread
From: Emanuel Berg @ 2023-03-26  1:04 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> (defun search-something (&optional query)
>    "Search something by using QUERY."
>    (interactive)
>    (let ((query (or query (read-from-minibuffer "Query: "))))
>      (message query)))

This has the same problem as before, it should look something
like this:

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/geh.el

(defun search-something (&optional query)
  "Search something by using QUERY."
  (interactive "sQuery: ")
  (unless (and (stringp query)
               (not (string= "" query)) )
    (setq query "default search") )
  (message query) )

;; (search-something "love")         ; love
;; (search-something)                ; default search
;; M-x search-something RET RET      ; default search
;; M-x search-something RET love RET ; love

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 19+ messages in thread

* RE: [External] : Re: (interactive) and &optional
  2023-03-24 21:07     ` Emanuel Berg
@ 2023-03-27  1:52       ` Drew Adams
  2023-03-27  2:40         ` John Yates
  0 siblings, 1 reply; 19+ messages in thread
From: Drew Adams @ 2023-03-27  1:52 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs@gnu.org

> `interactive' is the right place for everything interactive,
> so unless there is a specific reason why it is impractical to
> put it there, that's where one should put the
> interactive stuff.

At the risk of being pedantic...

`interactive' is the right place for code that
should _only_ affect interactive use, and that
should/can do so _only_ at the outset of the
function.

1. Putting that first "only" differently: if you
want some code to be run _both_ interactively
and non-interactively, then don't put it in the
`interactive' form.  (This one is obvious, but
it does conflict with the prescription to put
"everything" interactive in `interactive'.)

2. Putting that second "only" differently: if
something interactive needs to be done _after_
some code is run that isn't only for #1 (i.e.
isn't interactive-only at the outset), then
don't put it in `interactive'.  Instead do it
in the body conditionally, e.g., using a test
such as `called-interactively-p'.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [External] : Re: (interactive) and &optional
  2023-03-27  1:52       ` [External] : " Drew Adams
@ 2023-03-27  2:40         ` John Yates
  0 siblings, 0 replies; 19+ messages in thread
From: John Yates @ 2023-03-27  2:40 UTC (permalink / raw)
  To: Drew Adams; +Cc: Emanuel Berg, help-gnu-emacs@gnu.org

Thanks Drew.  That was nicely explained.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: (interactive) and &optional
  2023-03-26  1:04       ` Emanuel Berg
@ 2023-03-30 19:04         ` Jean Louis
  0 siblings, 0 replies; 19+ messages in thread
From: Jean Louis @ 2023-03-30 19:04 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2023-03-27 08:05]:
> > (defun search-something (&optional query)
> >    "Search something by using QUERY."
> >    (interactive)
> >    (let ((query (or query (read-from-minibuffer "Query: "))))
> >      (message query)))
> 
> This has the same problem as before, it should look something
> like this:
> 
> ;;; -*- lexical-binding: t -*-
> ;;
> ;; this file:
> ;;   https://dataswamp.org/~incal/emacs-init/geh.el
> 
> (defun search-something (&optional query)
>   "Search something by using QUERY."
>   (interactive "sQuery: ")
>   (unless (and (stringp query)
>                (not (string= "" query)) )
>     (setq query "default search") )
>   (message query) )
> 
> ;; (search-something "love")         ; love
> ;; (search-something)                ; default search
> ;; M-x search-something RET RET      ; default search
> ;; M-x search-something RET love RET ; love

That is your design opinion that it should look like that as related to
`interactive', and my design is that I avoid putting questions within
`interactive' declaration. I find it simpler, more visible, easier to
understand, readable, and freeer. For example I can remove
(interactive) from function for function still to work, without
restructuring, sometimes I find I do not want function any more to be
a command.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2023-03-30 19:04 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.