unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean Louis <bugs@gnu.support>
To: carlmarcos@tutanota.com
Cc: Help Gnu Emacs <help-gnu-emacs@gnu.org>
Subject: Re: Operating the HIST feature of completing-read
Date: Tue, 12 Jul 2022 01:43:41 +0300	[thread overview]
Message-ID: <YsynnWwOd/93Lz5Q@protected.localdomain> (raw)
In-Reply-To: <N6ecky2--3-2@tutanota.com>

* 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.

(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	other threads:[~2022-07-11 22:43 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-11  0:22 Operating the HIST feature of completing-read carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-11 22:43 ` Jean Louis [this message]
     [not found] ` <YsynnWwOd/93Lz5Q@protected.localdomain-N6jRHVs----2>
2022-07-12  0:13   ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-12  8:52     ` Jean Louis
2022-07-12 14:28       ` [External] : " Drew Adams
2022-07-12 16:20         ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-12 16:40           ` Drew Adams
     [not found]           ` <SJ0PR10MB5488AF0AFC27B5DDA0FD6937F3869@SJ0PR10MB5488.namprd10.prod.outlook.com-N6nHMGc--3-2>
2022-07-12 17:00             ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-13  1:16             ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-13  2:04               ` Jean Louis
2022-07-13  6:52               ` Yuri Khan
     [not found]               ` <Ys4oPraxx7rSD+OQ@protected.localdomain-N6qVVuQ----2>
2022-07-13  8:01                 ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-13 15:11               ` Drew Adams
     [not found]       ` <SJ0PR10MB5488F286DDB15D2F9DC59981F3869@SJ0PR10MB5488.namprd10.prod.outlook.com-N6n2IGP--7-2>
2022-07-13  0:41         ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-13 15:09           ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YsynnWwOd/93Lz5Q@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=carlmarcos@tutanota.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).