unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Functions with multiple optional arguments
Date: Mon, 17 Oct 2022 03:48:00 +0200	[thread overview]
Message-ID: <878rlf9q5r.fsf@dataswamp.org> (raw)
In-Reply-To: FL1ztjjucrxLSBjfWJdw63C4sECwGVJ737A96PJsEO0rdIUoqz5D3NGAH2OAYtZUJ0I_8UJa51pH_9UljPPmJzLKLka-sjclsyfTHOgPuqM=@protonmail.com

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




  reply	other threads:[~2022-10-17  1:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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=878rlf9q5r.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).