help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Operating the HIST feature of completing-read


From: carlmarcos
Subject: Re: Operating the HIST feature of completing-read
Date: Tue, 12 Jul 2022 02:13:29 +0200 (CEST)

Jul 11, 2022, 22:43 by bugs@gnu.support:

> * carlmarcos--- via Users list for the GNU Emacs text editor 
> <help-gnu-emacs@gnu.org> [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.
>
I found your elaboration difficult to follow.  I just need a summary on what it 
is, how to use it, and how it works.  Without wrappers and clever stuff.


> (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/
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]