From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: Functions with multiple optional arguments Date: Mon, 17 Oct 2022 03:48:00 +0200 Message-ID: <878rlf9q5r.fsf@dataswamp.org> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39361"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:7zFLfsiuw1kq2zzY6oeTnkMu4Ho= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Oct 17 20:59:49 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1okVL3-000A2f-Fd for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 17 Oct 2022 20:59:49 +0200 Original-Received: from localhost ([::1]:42020 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1okVL2-0004Of-JK for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 17 Oct 2022 14:59:48 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59824) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okFEu-0002IM-35 for help-gnu-emacs@gnu.org; Sun, 16 Oct 2022 21:48:24 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]:53850) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okFEe-0003Go-Rh for help-gnu-emacs@gnu.org; Sun, 16 Oct 2022 21:48:23 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1okFEd-0000n8-GW for help-gnu-emacs@gnu.org; Mon, 17 Oct 2022 03:48:07 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Mail-Copies-To: never Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 17 Oct 2022 14:58:34 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:140088 Archived-At: 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