unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Functions with multiple optional arguments
@ 2022-10-16 21:04 Heime via Users list for the GNU Emacs text editor
  2022-10-17  1:48 ` Emanuel Berg
  2022-10-17  4:05 ` Jean Louis
  0 siblings, 2 replies; 16+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2022-10-16 21:04 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

Have been writing a function that has two optional arguments. It is turning out to be a difficult task in situations
when one in missing an argument. Anybody has experience about this, as I have not seen much code with multiple
optional arguments.

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

* Re: Functions with multiple optional arguments
  2022-10-16 21:04 Functions with multiple optional arguments Heime via Users list for the GNU Emacs text editor
@ 2022-10-17  1:48 ` Emanuel Berg
  2022-10-17  4:05 ` Jean Louis
  1 sibling, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2022-10-17  1:48 UTC (permalink / raw)
  To: help-gnu-emacs

Heime via Users list for the GNU Emacs text editor wrote:

> Have been writing a function that has two optional
> arguments. It is turning out to be a difficult task in
> situations when one in missing an argument. Anybody has
> experience about this, as I have not seen much code with
> multiple optional arguments.

Yes, you have to have them all to the right of &optional.

Optional args default to nil so if you want to use one to the
right of another that is also optional it can be explicitly
ignored by calling the function with nil for the optional
argument (all of them) that are desired to be left unset ...

If you intend to use them in the function, and nil doesn't
make sense to use, you have to check for nil and set them
manually to what will in practice be their default value ...

See these examples

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/dwim.el
;;
;; DWIM code examples.
;;
;; Advantages:
;;
;; * the same defaults interactively and from Lisp
;; * that default is the whole buffer
;; * the default is always set unless other data is set explicitely
;; * the region is never used from Lisp
;; * it works with preceding, non-optional arguments as well
;;
;; re: `use-region', see lines 6873-6879 in simple.el for
;; `use-region-beginning' and `use-region-end'.

(defun use-region ()
  (when (use-region-p)
    (list (region-beginning) (region-end)) ))

(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 "re: ")
                 ,@(use-region) ))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (message "%d %d %s" beg end re) )

;; test

(when nil

  (save-mark-and-excursion
    (set-mark   10)
    (goto-char 500)
    (call-interactively #'test-dwim) ) ; 10  500

  (call-interactively #'test-dwim)     ;  1 1282

  (test-dwim)                          ;  1 1282
  (test-dwim 30)                       ; 30 1282
  (test-dwim nil 90)                   ;  1   90
  (test-dwim 30  90)                   ; 30   90

  (test-dwim-2 "a" 30 90)              ; 30   90 a
)

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




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

* Re: Functions with multiple optional arguments
  2022-10-16 21:04 Functions with multiple optional arguments Heime via Users list for the GNU Emacs text editor
  2022-10-17  1:48 ` Emanuel Berg
@ 2022-10-17  4:05 ` Jean Louis
  2022-10-17  4:18   ` Emanuel Berg
  2022-10-17  5:42   ` Heime
  1 sibling, 2 replies; 16+ messages in thread
From: Jean Louis @ 2022-10-17  4:05 UTC (permalink / raw)
  To: Heime; +Cc: help-gnu-emacs@gnu.org

* Heime via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2022-10-17 00:06]:

> Have been writing a function that has two optional arguments. It is
> turning out to be a difficult task in situations when one in missing
> an argument. Anybody has experience about this, as I have not seen
> much code with multiple optional arguments.

If argument is optional but required by your function, I use this method:

(defun my-fun (&optional title description)
  (let ((title (or title "Best Movie"))
	(description (or description "Description")))
    (message "%s: %s" title description)))

(my-fun)

-- 
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] 16+ messages in thread

* Re: Functions with multiple optional arguments
  2022-10-17  4:05 ` Jean Louis
@ 2022-10-17  4:18   ` Emanuel Berg
  2022-10-17 21:12     ` Jean Louis
  2022-10-17  5:42   ` Heime
  1 sibling, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2022-10-17  4:18 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>> Have been writing a function that has two optional
>> arguments. It is turning out to be a difficult task in
>> situations when one in missing an argument. Anybody has
>> experience about this, as I have not seen much code with
>> multiple optional arguments.
>
> If argument is optional but required by your function, I use
> this method:
>
> (defun my-fun (&optional title description)
>   (let ((title (or title "Best Movie"))
> 	(description (or description "Description")))
>     (message "%s: %s" title description)))
>
> (my-fun)

Yes, and to set 'description' (and not 'title') you'd do

  (my-fun nil "Poor movie IMO")

CL has another way, more practical probably, to assign default
values to optional arguments and that can be used in Elisp as
well with `cl-defun' ...

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




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

* Re: Functions with multiple optional arguments
  2022-10-17  4:05 ` Jean Louis
  2022-10-17  4:18   ` Emanuel Berg
@ 2022-10-17  5:42   ` Heime
  2022-10-17 16:24     ` Heime
  1 sibling, 1 reply; 16+ messages in thread
From: Heime @ 2022-10-17  5:42 UTC (permalink / raw)
  To: Jean Louis; +Cc: help-gnu-emacs@gnu.org

------- Original Message -------
On Monday, October 17th, 2022 at 4:05 AM, Jean Louis <bugs@gnu.support> wrote:


> * Heime via Users list for the GNU Emacs text editor help-gnu-emacs@gnu.org [2022-10-17 00:06]:
> 
> > Have been writing a function that has two optional arguments. It is
> > turning out to be a difficult task in situations when one in missing
> > an argument. Anybody has experience about this, as I have not seen
> > much code with multiple optional arguments.
> 
> 
> If argument is optional but required by your function, I use this method:
> 
> (defun my-fun (&optional title description)
> (let ((title (or title "Best Movie"))
> (description (or description "Description")))
> (message "%s: %s" title description)))
> 
> (my-fun)

I see that the trick is to assign defaults that way.  I like it, very clever.



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

* Re: Functions with multiple optional arguments
  2022-10-17  5:42   ` Heime
@ 2022-10-17 16:24     ` Heime
  2022-10-17 16:52       ` [External] : " Drew Adams
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2022-10-17 16:24 UTC (permalink / raw)
  To: Heime; +Cc: Jean Louis, help-gnu-emacs@gnu.org

------- Original Message -------
On Monday, October 17th, 2022 at 5:42 AM, Heime <heimeborgia@protonmail.com> wrote:


> ------- Original Message -------
> On Monday, October 17th, 2022 at 4:05 AM, Jean Louis bugs@gnu.support wrote:
> 
> 
> 
> > * Heime via Users list for the GNU Emacs text editor help-gnu-emacs@gnu.org [2022-10-17 00:06]:
> > 
> > > Have been writing a function that has two optional arguments. It is
> > > turning out to be a difficult task in situations when one in missing
> > > an argument. Anybody has experience about this, as I have not seen
> > > much code with multiple optional arguments.
> > 
> > If argument is optional but required by your function, I use this method:
> > 
> > (defun my-fun (&optional title description)
> > (let ((title (or title "Best Movie"))
> > (description (or description "Description")))
> > (message "%s: %s" title description)))
> > 
> > (my-fun)
 
I want the last argument to use a symbol.  Would you be so kind to scrutinise 
my implementation please.

(defun mesgb (mesg &optional bufr actm)
  "Display message except on 'nogo symbol."

  (let ((bfr (or bufr "*Gundit*"))
	(acm (or actm 'go)))

    (when (not (eq 'nogo acm))
      (with-current-buffer (get-buffer-create bfr)
	(org-mode)
        (insert mesg))) ))




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

* RE: [External] : Re: Functions with multiple optional arguments
  2022-10-17 16:24     ` Heime
@ 2022-10-17 16:52       ` Drew Adams
  2022-10-17 17:28         ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Drew Adams @ 2022-10-17 16:52 UTC (permalink / raw)
  To: Heime; +Cc: Jean Louis, help-gnu-emacs@gnu.org

> I want the last argument to use a symbol.  Would you be so kind to scrutinise
> my implementation please.
> 
> (defun mesgb (mesg &optional bufr actm)
>   "Display message except on 'nogo symbol."
>   (let ((bfr (or bufr "*Gundit*"))
> 	(acm (or actm 'go)))
> 
>     (when (not (eq 'nogo acm))
>       (with-current-buffer (get-buffer-create bfr)
> 	(org-mode)
>         (insert mesg))) ))

(defun mesgb (message &optional buffer actm)
  "Show MESSAGE, unless ACTM is the symbol `nogo'."
  (setq buffer  (or buffer  "*Gundit*")
        actm    (or actm  'go))
  (unless (eq 'actm 'nogo)
    (with-current-buffer (get-buffer-create buffer))
      (org-mode)
      (insert message)))) 

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

* RE: [External] : Re: Functions with multiple optional arguments
  2022-10-17 16:52       ` [External] : " Drew Adams
@ 2022-10-17 17:28         ` Heime
  2022-10-17 17:38           ` Drew Adams
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2022-10-17 17:28 UTC (permalink / raw)
  To: Drew Adams; +Cc: Jean Louis, help-gnu-emacs@gnu.org

------- Original Message -------
On Monday, October 17th, 2022 at 4:52 PM, Drew Adams <drew.adams@oracle.com> wrote:


> > I want the last argument to use a symbol. Would you be so kind to scrutinise
> > my implementation please.
> > 
> > (defun mesgb (mesg &optional bufr actm)
> > "Display message except on 'nogo symbol."
> > (let ((bfr (or bufr "Gundit"))
> > (acm (or actm 'go)))
> > 
> > (when (not (eq 'nogo acm))
> > (with-current-buffer (get-buffer-create bfr)
> > (org-mode)
> > (insert mesg))) ))
> 
> 
> (defun mesgb (message &optional buffer actm)
> "Show MESSAGE, unless ACTM is the symbol `nogo'."
> (setq buffer (or buffer "Gundit")
> actm (or actm 'go))
> (unless (eq 'actm 'nogo)
> (with-current-buffer (get-buffer-create buffer))
> (org-mode)
> (insert message))))

Your adaptation is much neater.  Did not realise that equality 
on symbols should be (eq 'actm 'nogo) rather than (eq actm 'nogo).
Do you have time to describe what does wrong with (eq actm 'nogo)?
 




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

* RE: [External] : Re: Functions with multiple optional arguments
  2022-10-17 17:28         ` Heime
@ 2022-10-17 17:38           ` Drew Adams
  0 siblings, 0 replies; 16+ messages in thread
From: Drew Adams @ 2022-10-17 17:38 UTC (permalink / raw)
  To: Heime; +Cc: Jean Louis, help-gnu-emacs@gnu.org

> Your adaptation is much neater.  Did not realise that equality
> on symbols should be (eq 'actm 'nogo) rather than (eq actm 'nogo).
> Do you have time to describe what does wrong with (eq actm 'nogo)?

No.  In fact, I meant to write (eq actm 'nogo).
Quoting actm was a typo - you want to test its
value.  (eq actm 'nogo) is what it should be.

(eq 'actm 'nogo) will never be non-nil - those
are different values (symbols).

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

* Re: Functions with multiple optional arguments
  2022-10-17  4:18   ` Emanuel Berg
@ 2022-10-17 21:12     ` Jean Louis
  2022-10-17 21:36       ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: Jean Louis @ 2022-10-17 21:12 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2022-10-17 21:57]:
> CL has another way, more practical probably, to assign default
> values to optional arguments and that can be used in Elisp as
> well with `cl-defun' ...

I keep anything like Common Lisp for Common Lisp and strive to keep
Emacs Lisp functions without CL stuff for clarity of my mind.

-- 
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] 16+ messages in thread

* Re: Functions with multiple optional arguments
  2022-10-17 21:12     ` Jean Louis
@ 2022-10-17 21:36       ` Emanuel Berg
  2022-10-20 21:00         ` Jean Louis
  0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2022-10-17 21:36 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>> CL has another way, more practical probably, to assign
>> default values to optional arguments and that can be used
>> in Elisp as well with `cl-defun' ...
>
> I keep anything like Common Lisp for Common Lisp and strive
> to keep Emacs Lisp functions without CL stuff for clarity of
> my mind.

If so, relax, as `cl-defun' is as much Elisp as `defun' ...

C-h f cl-defun RET

It's in cl-macs.el ...

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




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

* Re: Functions with multiple optional arguments
  2022-10-17 21:36       ` Emanuel Berg
@ 2022-10-20 21:00         ` Jean Louis
  2022-10-21  3:29           ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: Jean Louis @ 2022-10-20 21:00 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2022-10-18 12:15]:
> Jean Louis wrote:
> 
> >> CL has another way, more practical probably, to assign
> >> default values to optional arguments and that can be used
> >> in Elisp as well with `cl-defun' ...
> >
> > I keep anything like Common Lisp for Common Lisp and strive
> > to keep Emacs Lisp functions without CL stuff for clarity of
> > my mind.
> 
> If so, relax, as `cl-defun' is as much Elisp as `defun' ...
> 
> C-h f cl-defun RET
> 
> It's in cl-macs.el ...

I do not load what is not needed.

And Emacs Lisp is not Common Lisp, no need to conform to it.

-- 
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] 16+ messages in thread

* Re: Functions with multiple optional arguments
  2022-10-20 21:00         ` Jean Louis
@ 2022-10-21  3:29           ` Emanuel Berg
  2022-10-23  4:47             ` Jean Louis
  0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2022-10-21  3:29 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>>>> CL has another way, more practical probably, to assign
>>>> default values to optional arguments and that can be used
>>>> in Elisp as well with `cl-defun' ...
>>>
>>> I keep anything like Common Lisp for Common Lisp and
>>> strive to keep Emacs Lisp functions without CL stuff for
>>> clarity of my mind.
>> 
>> If so, relax, as `cl-defun' is as much Elisp as `defun' ...
>> 
>> C-h f cl-defun RET
>> 
>> It's in cl-macs.el ...
>
> I do not load what is not needed.

Well, it's not about what's needed or not, there are obviously
several ways to do this as we have seen in this thread
already, and what `cl-defun' offers is another such solution,
one that some people would say is more clean than the
solutions with `or' and `setq'/`let' ...

> And Emacs Lisp is not Common Lisp, no need to conform to it.

Again, there is no need to do any of this but it remains that
`cl-defun' is an Elisp macro ...

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




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

* Re: Functions with multiple optional arguments
  2022-10-21  3:29           ` Emanuel Berg
@ 2022-10-23  4:47             ` Jean Louis
  2022-10-24  3:07               ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: Jean Louis @ 2022-10-23  4:47 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2022-10-22 13:45]:
> > I do not load what is not needed.
> 
> Well, it's not about what's needed or not, there are obviously
> several ways to do this as we have seen in this thread
> already, and what `cl-defun' offers is another such solution,
> one that some people would say is more clean than the
> solutions with `or' and `setq'/`let' ...
> 
> > And Emacs Lisp is not Common Lisp, no need to conform to it.
> 
> Again, there is no need to do any of this but it remains that
> `cl-defun' is an Elisp macro ...

Let us say much of code comes from Common Lisp, then there is no need to
rewrite too much, and then one uses Common Lisp related macros. It
makes sense. 

But when code is much of Emacs Lisp, there is no need for it.

-- 
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] 16+ messages in thread

* Re: Functions with multiple optional arguments
  2022-10-23  4:47             ` Jean Louis
@ 2022-10-24  3:07               ` Emanuel Berg
  2022-10-25  5:25                 ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2022-10-24  3:07 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>>> I do not load what is not needed.
>> 
>> Well, it's not about what's needed or not, there are
>> obviously several ways to do this as we have seen in this
>> thread already, and what `cl-defun' offers is another such
>> solution, one that some people would say is more clean than
>> the solutions with `or' and `setq'/`let' ...
>> 
>>> And Emacs Lisp is not Common Lisp, no need to conform
>>> to it.
>> 
>> Again, there is no need to do any of this but it remains
>> that `cl-defun' is an Elisp macro ...
>
> Let us say much of code comes from Common Lisp, then there
> is no need to rewrite too much, and then one uses Common
> Lisp related macros. It makes sense.

?

> But when code is much of Emacs Lisp, there is no need
> for it.

???

`cl-defun' is Elisp, if you need it or not is up to you ...

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




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

* Re: Functions with multiple optional arguments
  2022-10-24  3:07               ` Emanuel Berg
@ 2022-10-25  5:25                 ` Emanuel Berg
  0 siblings, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2022-10-25  5:25 UTC (permalink / raw)
  To: help-gnu-emacs

> `cl-defun' is Elisp, if you need it or not is up to you ...

Here is an example how it's done in Elisp only.

Hey, never to late to return to the old pen-and-paper RPGs!

Actually I don't want to do that ...

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

(require 'cl-lib)

(cl-defun dice (&optional (sides 6) (num 1))
  (let ((sum 0))
    (dotimes (_ num) (cl-incf sum (1+ (random sides))))
    sum) )

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

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




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

end of thread, other threads:[~2022-10-25  5:25 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-16 21:04 Functions with multiple optional arguments Heime via Users list for the GNU Emacs text editor
2022-10-17  1:48 ` Emanuel Berg
2022-10-17  4:05 ` Jean Louis
2022-10-17  4:18   ` Emanuel Berg
2022-10-17 21:12     ` Jean Louis
2022-10-17 21:36       ` Emanuel Berg
2022-10-20 21:00         ` Jean Louis
2022-10-21  3:29           ` Emanuel Berg
2022-10-23  4:47             ` Jean Louis
2022-10-24  3:07               ` Emanuel Berg
2022-10-25  5:25                 ` Emanuel Berg
2022-10-17  5:42   ` Heime
2022-10-17 16:24     ` Heime
2022-10-17 16:52       ` [External] : " Drew Adams
2022-10-17 17:28         ` Heime
2022-10-17 17:38           ` Drew Adams

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