From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Operating the HIST feature of completing-read Date: Tue, 12 Jul 2022 01:43:41 +0300 Message-ID: References: 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="33437"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/+ () (2022-06-11) Cc: Help Gnu Emacs To: carlmarcos@tutanota.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jul 12 00:47:43 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 1oB2Br-0008Va-B9 for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 12 Jul 2022 00:47:43 +0200 Original-Received: from localhost ([::1]:49118 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oB2Bp-0004zI-Ux for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 11 Jul 2022 18:47:41 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59464) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oB29K-0004yB-Cz for help-gnu-emacs@gnu.org; Mon, 11 Jul 2022 18:45:06 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:52519) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oB29G-0004Ed-W2 for help-gnu-emacs@gnu.org; Mon, 11 Jul 2022 18:45:04 -0400 Original-Received: from localhost ([::ffff:154.226.102.225]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 00000000000A3AE6.0000000062CCA7CC.0000794C; Mon, 11 Jul 2022 15:44:27 -0700 Mail-Followup-To: carlmarcos@tutanota.com, Help Gnu Emacs Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, 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-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:138419 Archived-At: * carlmarcos--- via Users list for the GNU Emacs text editor [2022-07-11 03:23]: > How does HIST work when using completing-read?  Any examples that > would help me with this? I find HIST variables a waste in coding, so I am automating it by automatically assigning HIST variable to every call to the function. (defun rcd-symbol-if-not-exist (variable &optional value description) "Return symbol for VARIABLE. It will generate new VARIABLE if it does not exist." (let* ((variable (replace-regexp-in-string "[^[:alnum:]]" "-" (downcase variable))) (rcd-symbol (intern variable)) (description (or description (format "Generated variable `%s'" variable)))) (if (boundp rcd-symbol) rcd-symbol (eval (list 'defvar rcd-symbol value description))))) (defun rcd-ask-history-variable (prompt) "Generate history variable for PROMPT." (let* ((description (format "History for `%s' prompt." prompt))) (rcd-symbol-if-not-exist (concat "rcd-" prompt "-history") nil description))) Then there is my wrapper function for `completing-read`: (defun rcd-choose (list &optional prompt predicate initial-input def) "Ask user for LIST of choices. If only one element, function `y-or-n-p' will be used. For multiple elements `completing-read' is used. If nothing chosen it will return empty string." (let* ((completion-ignore-case t) (prompt (or prompt "Choose: ")) (description (format "History for `%s' completion prompt." prompt)) (history (rcd-symbol-if-not-exist (concat "rcd-" prompt "-history") nil description)) (input (cond ((length= list 1) (if (y-or-n-p (nth 0 list)) (nth 0 list) "")) (t (rcd-repeat-until-not-empty-string 'completing-read prompt list predicate t initial-input history def t))))) input)) That generates history variables automatically in the line, based on the prompt: (history (rcd-symbol-if-not-exist (concat "rcd-" prompt "-history") nil description)) Then this below would use history variable: rcd-choose---history (rcd-choose '("One" "Two" "Three")) because following evaluates to that symbol: (rcd-symbol-if-not-exist (concat "rcd-" "Choose: " "-history") nil "History for `Choose: ' completion prompt.") ⇒ rcd-choose---history However, any other prompt would yield automatically with a different history variable based on its prompt like "Choose a number: " (rcd-choose '("One" "Two" "Three") "Choose a number: ") ⇒ "One" -- Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns In support of Richard M. Stallman https://stallmansupport.org/