From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: carlmarcos--- via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help Subject: RE: [External] : Re: Making a function than can only be used interactively Date: Sat, 9 Jul 2022 00:34:09 +0200 (CEST) Message-ID: References: <875yk8ehp8.fsf@dataswamp.org> <87bku0ce1f.fsf@dataswamp.org> Reply-To: carlmarcos@tutanota.com 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="14446"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Christopher Dimech , Yuri Khan , help-gnu-emacs To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jul 09 00:35:17 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 1o9wZB-0003Xq-Bz for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 09 Jul 2022 00:35:17 +0200 Original-Received: from localhost ([::1]:41398 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o9wZ9-0000z8-Rq for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jul 2022 18:35:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40514) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9wYA-0000yz-0h for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 18:34:14 -0400 Original-Received: from w1.tutanota.de ([81.3.6.162]:59130) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9wY7-0000KE-F9 for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 18:34:13 -0400 Original-Received: from w3.tutanota.de (unknown [192.168.1.164]) by w1.tutanota.de (Postfix) with ESMTP id 68D39FA0EA0; Fri, 8 Jul 2022 22:34:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1657319649; s=s1; d=tutanota.com; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=BFYoFLwFTb+iCcJlHR6BEHgZCyA8Rvp22WXhXdX9KHM=; b=Qsi6fSJT332AmwJJIBxK4H8hzk1XyoGTtqTJW78jTTtuPlwT8ySD0Trpp9yEo0mP hStbhXQKB1T11XRztW30J5bF7q9agxcGUnGGeKKciQF3ZqnaT9eKB7IvKakxk+eCX/8 Aivngy1zg0gpjjx/ToA63NgPKbpDlJEyI23WRRD32Z/ljCGitvqNQv0qv6pd5aroOJp 5osUy6Vsk0apjBuRLHVmKIzmRvXjia/NwzqgYe0EvouQplymYZCq67pU3KwOvRGvJin scTzR5XXdXMWCm9Ejelph9cM5/RnkztzcXB+xp5Q2SUam7LPcmSAS0/DPMuN8jD6faG sFfA1YaEYg== In-Reply-To: Received-SPF: pass client-ip=81.3.6.162; envelope-from=carlmarcos@tutanota.com; helo=w1.tutanota.de 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, HTML_MESSAGE=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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:138392 Archived-At: --=20 Sent with Tutanota, enjoy secure & ad-free emails.=20 Jul 8, 2022, 22:17 by drew.adams@oracle.com: >>> Dunno whether it's a coincidence (I'm guessing no), >>> but in the past couple of weeks there've been a >>> boatload of similar questions on emacs.SE. Here >>> are some of them: >>> >> >> I saw them and got the same kind of problem of how >> to write interactive function. >> > > I can tell - by your just now writing this, even after > reading my message: > >> I thought that because the prefix argument i[s] used >> first (using C-u N myfunc) then it also has to be >> the first argument in the function declaration. >> > > So you _saw_ my comments on emacs.SE, but it's not > clear that you _read_ them. > > Do yourself a favor. Read this s l o w l y . . . > > A "prefix argument" is NOT an argument > to the function (command). It's NOT. > > That is, it _need not be_. It's _not automatically_ > used as any of the function's args. It _could_ be > passed as one or more of the function's args. There's > NO necessary or logical connection between the "prefix > argument" and the function's arguments. > > If this isn't yet clear, please read that again. And > again ... till it's clear. > > A prefix argument results from a _user action_. It > makes a value available when the function's code is > evaluated. If that value isn't passed as one of the > function's arguments then the function body (or code > it invokes...) can obtain it using the global variable > `current-prefix-arg' - see `C-h v current-prefix-arg'. > > And you say: > >> because the prefix argument i[s] used first (using >> C-u N myfunc) >> > > No, it's not "used first". It's _not used at all_ > ... unless the function's code actually, explicitly > uses it somehow. A user can hit `C-u' all day long > with zero effect, if the current command doesn't make > any use of the prefix arg. > > How can a command (function) explicitly use the prefix > arg? (1) in an `interactive' spec, (2) in the body, > or (3) by passing its value as an argument. > > To use the prefix arg in an `interactive' spec you can > (1) use `P' or `p' in a string argument (any number of > times), or (2) use `current-prefix-arg' in a list arg > (any number of times). > > To use it in the body explicitly, use variable > `current-prefix-arg'. > > How else can a function use the prefix arg? (3) Pass > its value as one or more of the function's args. > >> Quite complicated thing. >> > > No, not really. I think you've just been misled by > the name "prefix ARGUMENT". > > Nothing to be ashamed of. But now you know - it is > NOT AN ARGUMENT to the command - not unless (1) the > command's `interactive' spec provides its value as > one of the command's arguments OR (2) code that > invokes the command (function) passes it as one > of the arguments. > > I don't think any of us have been able to help much > by replying to vague requests for guidance about > when to use this or that (optional arg, prefix arg, > `interactive', ...). > > And the reason, I think, is because the request is > misguided (an X-Y question). I'm guessing it's the > name "prefix argument" that's got you (and perhaps > others) twisted in a knot by mis-suggesting that it > represents something that has something to do with > an argument to the command (function). IT DOESN'T. > > Now go outside and have fun. ;-) > ___ > > Rereading the Emacs manual about "prefix argument" > now, I can see where someone might get the wrong > idea. It tries to present a command to users as > something more abstract than the Lisp (or C) function > that implements it. And so it talks about a "prefix > argument" being provided to the command first, and > "minibuffer args" possibly being provided afterward. > Yes, that was exactly why I had the impression that the list should have arg storing the prefix argument as the first argument to the function.=C2= =A0 And that it has to be optional because a user can decide not to call the interactive= function with C-u. > IOW, from a user, non-Lisp point of view, you can > think that you first provide an "argument" to a > command using `C-u' etc. And then the command might > prompt you for other info ("arguments") in the > minibuffer. > Yes, I thought that way. > I think that's what that language is about. But it > can (apparently) create a disconnect when someone > then tries to move to a Lisp understanding, with the > notion of a command as a function (an interactive > function), which gets passed arguments. > Correct.=C2=A0 Found it very hard to go from reading to an actual implement= ation. I also found no way to get the equivalent of Code Character "P" if the func= tion includes an arg that stores the prefix within a list=20 For instance (defun guling (&optional prefix a b) =C2=A0 "Docstring" =C2=A0 (interactive =C2=A0=C2=A0 (cond =C2=A0=C2=A0=C2=A0 ((equal current-prefix-arg 2) =C2=A0=C2=A0=C2=A0=C2=A0 (list =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 () ;=C2=A0 What should one put here???=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (read-from-minibuffer "a: ") =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (read-from-minibuffer "b: ")))))=20 (processing)) =C2=A0=20 > ___ > > > [ > NOTE: If some code in a command (in the `interactive' > spec or in the body), or in any code invoked by the > command, invokes another command interactively, or lets > a user invoke another command interactively, then the > use of that other command invocation redefines the > `current-prefix-arg' value. > > And yes, _that_ can get a bit complicated. If you're > interested in this, and you feel up to it, then dig > into the Elisp manual, node `Command Loop': > > https://www.gnu.org/software/emacs/manual/html_node/elisp/Command-Loop.ht= ml > > ] >