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: Making a function than can only be used interactively Date: Fri, 08 Jul 2022 08:30:04 +0200 Message-ID: <87bku0ce1f.fsf@dataswamp.org> References: <87pmiljgah.fsf@gnu.org> <875yk8ehp8.fsf@dataswamp.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23562"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:ULyP2SbREAAWXVzMX6tI/rCcsTg= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 08 08:32:48 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 1o9hXk-0005v5-Ll for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jul 2022 08:32:48 +0200 Original-Received: from localhost ([::1]:36452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o9hXj-0001CU-9f for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jul 2022 02:32:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9hVM-000190-Co for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 02:30:21 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]:47730) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9hVK-00012P-6s for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 02:30:19 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1o9hVH-0002wr-Bo for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 08:30:15 +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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action 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:138373 Archived-At: Yuri Khan wrote: > It does not matter for interactive use. [...] ‘&optional’ > comes into play if you use this function non-interactively, > from Lisp No, it matters. One example how it matters is that optional arguments defaults to nil. Check out this file and in particular example 4 which doesn't make sense to me? ;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/dwim.el ;; DWIM example 1, from Lisp ignore region if set (defun test-dwim (&optional beg end) (interactive (when (use-region-p) (list (region-beginning) (region-end)) )) (or beg (setq beg (point-min))) ; or (point) (or end (setq end (point-max))) ;; insert code here ;; now let's just make a list to do something (list beg end) ) ;; example 2, use the region if available from Lisp as well (defun test-dwim-2 (&optional beg end) (interactive (when (use-region-p) (list (region-beginning) (region-end)) )) (or beg (setq beg (if (use-region-p) (region-beginning) (point-min)))) (or end (setq end (if (use-region-p) (region-end) (point-max)))) (list beg end) ) ;; example 3, one call to `use-region-p' is enough (defun test-dwim-3 (re &optional beg end) (interactive `(,(read-regexp "re: ") ,@(when (use-region-p) (list (region-beginning) (region-end)) ))) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (message "%s" (list re beg end)) ) ;; example 4, let's do that with the `interactive' spec ;; string. but without `use-region-p' it doesn't reset after ;; I clear the region, or that's what I thought happened ;; anyway :) so this doesn't work as intended, which ;; `test-dwim-3' does, supposedly the worse one. (defun test-dwim-4 (re &optional beg end) (interactive "sre: \nr") (or beg (setq beg (point-min))) (or end (setq end (point-max))) (message "%s" (list re beg end)) ) ;; test the interface (when nil (save-excursion (set-mark 10) (goto-char 500) (call-interactively #'test-dwim) ) ; (10 500) (call-interactively #'test-dwim) ; ( 1 2867) (test-dwim 30 90) ; (30 90) (test-dwim) ; ( 1 2867) ) ;; example function (defun count-chars (&optional beg end) (interactive (when (use-region-p) (list (region-beginning) (region-end)) )) (let*((bg (or beg (point-min))) (ed (or end (point-max))) (df (- ed bg)) ) (prog1 df (message "%d" df) ))) ;; Test the example function: ;; :; [try these with and without a region] ;; ;; (call-interactively #'count-chars) ;; (count-chars) ;; ;; [this will always be the same tho] ;; ;; (count-chars 10 40) ;; ;; Note: ;; A common mistake in Elisp is that optional formal ;; parameters aren't sent as arguments when called from ;; Lisp, they are then nil but are used as for example an ;; integer, as in: ;; ;; (when (and (<= 0 width) (<= width 648)) ... ) ; DNC ;; ;; The test method is to call the function every way possible: ;; 1. interactively with a region ;; 2. ditto w/o ;; 3. From Lisp with arguments ;; 4. ditto w/o ;; .. .. -- underground experts united https://dataswamp.org/~incal