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 05:40:44 +0200 Message-ID: <87wncoclvn.fsf@dataswamp.org> References: <87pmiljgah.fsf@gnu.org> <875yk8ehp8.fsf@dataswamp.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38249"; 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:tZosaC+MGdZUKXAC6aeH0UWhF7s= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 08 05:41:35 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 1o9es2-0009p0-Px for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jul 2022 05:41:34 +0200 Original-Received: from localhost ([::1]:44748 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o9es1-0004Nn-IY for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 07 Jul 2022 23:41:33 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57546) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9erP-0004Nf-KM for help-gnu-emacs@gnu.org; Thu, 07 Jul 2022 23:40:55 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]:60522) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9erN-0001UG-Qd for help-gnu-emacs@gnu.org; Thu, 07 Jul 2022 23:40:55 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1o9erL-000912-CL for help-gnu-emacs@gnu.org; Fri, 08 Jul 2022 05:40:51 +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:138371 Archived-At: Here is an example of a more complicated interactive spec, (interactive `(,(read-string "search: [repeat] ") ,(or (equal current-prefix-arg '( 4)) (equal current-prefix-arg '(64)) ) ,(or (equal current-prefix-arg '(16)) (equal current-prefix-arg '(64)) ) ,@(if (use-region-p) (list (region-beginning) (region-end)) (list (point-min) (point-max)) ))) Again, eval the `interactive'! The signature is (defun wrap-search (str &optional case rev beg end) Especially because 'case' and 'rev' both depends on the `prefix-arg' I wonder if it's impossible to do with just the interactive string, even ... Other than that it isn't so difficult, a string is "s", a number is "n" and the region is "r". Note the use of the backtick just to be able to do ,@ ... that's pretty common. Another situation is this (let*((case-fold-search (not case)) (pos (point)) (data (if rev (list #'search-backward end beg) (list #'search-forward beg end) )) (search-f (car data)) (search-beg (cadr data)) (search-end (caddr data)) ) ...) Here we see that instead of doing (if rev ... ) three times we stash the configuration (here 3 vars) based on 'rev', then use `car' and the "forbidden" `cadr' and `caddr' to get from that. (Forbidden as you should it has been said only use vanilla `car' and `cdr', with 2 or more elements instead of `car' one should do `nth.'. But I think the argument-free cars are interesting I guess .... Full source: https://dataswamp.org/~incal/emacs-init/wrap-search.el -- underground experts united https://dataswamp.org/~incal