From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: Making a function than can only be used interactively Date: Fri, 8 Jul 2022 13:55:11 +0700 Message-ID: References: <87pmiljgah.fsf@gnu.org> <875yk8ehp8.fsf@dataswamp.org> <87bku0ce1f.fsf@dataswamp.org> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21242"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 08 08:56:40 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 1o9huq-0005KX-GL for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jul 2022 08:56:40 +0200 Original-Received: from localhost ([::1]:42530 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o9huo-0007e1-W2 for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jul 2022 02:56:39 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9hte-0007dq-CH for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 02:55:26 -0400 Original-Received: from mail-io1-xd36.google.com ([2607:f8b0:4864:20::d36]:42872) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o9htc-0004Ix-OQ for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 02:55:26 -0400 Original-Received: by mail-io1-xd36.google.com with SMTP id d3so18902547ioi.9 for ; Thu, 07 Jul 2022 23:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=86Owoyxv9xQrkVwfonVTa0stbDqBfOyI9y7hP/wrpEk=; b=VthPFs/cagT/1m48N1FRAdG942uJ/rFXQBynnX/U1fklawr8i1maS7h2JACSheV7ov oA8Mfix3nkiqbmYp88xmim2+JpCovCf+ERDOvSzpuRUmE75wX3WWA+yPRROD690+i0bG WC7vDnQXzaWmpr2C4lYeIZGymPNQdUFO4wzcoAexLpdDLXe4N9up2WCVmxXrWiOjW7mS al3xf0KH/i224BfGl4Af/scQwAsg1/S+spfpeDk5OoC41AoJA5Qo78NvNbc6+4dbTH+0 XwExH4rIQcX4djNeS+IOtE929o/YsWCu3xIlA6AMJPGNIbSxREFkiWZuPkx8IAQbjs3e 2/zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=86Owoyxv9xQrkVwfonVTa0stbDqBfOyI9y7hP/wrpEk=; b=nWzGxOAAhYPQOJqbBLU35cTcXyOuC4hmcpOiOZVpB5N3xqa64LskyagCT4wtQMOFh/ F0Fkq7H7GgrK8+NbFmZse7+RtlgwhjXqa8HUiEiDyjrpNry2tV8hOoykSB0Eo5nhY3Wm +oNI3IzTjMw0o1wJ1KVbxIKWYirjutVVgKzzs9/TlY8fX8BREJYwDbobE3fO1I+HqoLz QB1ZNtAtuemyezEPYlt5nbRwEk378LvLR2KAfWbb3JMG5oB7KuxqXMGpzpbJCCgdh0VG lL3uAR4IFIafIkTy9eaCJWUhDJwcqGDxNBg+OK3ssjhkbDad+7AT9zIOXe4DXnfv+KpO ZIQw== X-Gm-Message-State: AJIora//t9z2ox+bnpKa/h/W1SVMxkaxBOdYuUmOZMeNYMvWUjbuhO7M 6A922A2anKCg/sNC28HDn9HJfTHDBLhrYp5J2AgXknH1SZkJxw== X-Google-Smtp-Source: AGRyM1sfJncpkmW6SFNu8cttV9OPbxv978cdI6HXa8gB0OXfZZBFvjhszqT0tKSieQG4nlZSzXD7Iu2DIA9kQ4Kj74k= X-Received: by 2002:a05:6602:2ace:b0:678:d781:2984 with SMTP id m14-20020a0566022ace00b00678d7812984mr1136139iov.186.1657263323192; Thu, 07 Jul 2022 23:55:23 -0700 (PDT) In-Reply-To: <87bku0ce1f.fsf@dataswamp.org> Received-SPF: pass client-ip=2607:f8b0:4864:20::d36; envelope-from=yurivkhan@gmail.com; helo=mail-io1-xd36.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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:138374 Archived-At: On Fri, 8 Jul 2022 at 13:31, Emanuel Berg wrote: > > It does not matter for interactive use. [...] =E2=80=98&optional=E2=80= =99 > > 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. You=E2=80=99re right, with an interactive specification that evaluates to a list it matters, because the list you return may or may not have as many arguments as the function takes. My point was that in the specific case of a three-argument function and a three-item string-valued interactive specification, &optional does not matter for interactive use. > Check out this file and in particular example 4 which doesn't > make sense to me? > > ;; DWIM example 1, from Lisp ignore region if set > ;; example 2, use the region if available from Lisp as well > > (defun test-dwim (&optional beg end) > (interactive (when (use-region-p) > (list (region-beginning) (region-end)) )) Here you have two cases. If the region is active, you produce a two-element list, otherwise, a 0-element list. The function signature allows 0..2 arguments, so it works in either case. > ;; 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)) ))) Mostly same, except you build a list of 3 or 1 elements, and the function accepts 1..3 arguments. > ;; 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") Here you use a string interactive spec which always produces 3 elements. In non-interactive use, it will work if called as (test-dwim-4 "^foo$"), (test-dwim-4 "^foo$" 42), or (test-dwim-4 "^foo$" 42 69). As to your =E2=80=9Cclearing=E2=80=9D the region, Emacs always maintains th= e point and mark positions, and the =E2=80=98r=E2=80=99 interactive spec code ignores t= he region activation flag and always passes the point and mark. (This could be considered a bug, but I see no good alternative behavior, except maybe passing two nils if the region is not active.)