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

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

Re: completing-read depricated initial-input


From: Jean Louis
Subject: Re: completing-read depricated initial-input
Date: Thu, 23 Jun 2022 17:36:00 +0300
User-agent: Mutt/+ () (2022-05-21)

I like my function `rcd-ask' as it saves me generation of history variables 
automatically. 

(defun rcd-symbol-if-not-exist (variable &optional value description)
  "Return symbol for VARIABLE string. 

It will generate new VARIABLE if it does not exist.

VALUE and DESCRIPTION are optional."
  (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 based on PROMPT."
  (let* ((description (format "History for `%s' prompt." prompt)))
    (rcd-symbol-if-not-exist (concat "rcd-" prompt "-history") nil 
description)))

(defun rcd-ask (&optional prompt initial-contents keymap read default-value)
  "Modified function `read-from-minibuffer'.

This is shorter, simpler function that generates the prompt
automatically, generates history variable automatically and
inherits the input method. The input will be returned trimmed."
  (let* ((prompt (or prompt "Input data: "))
         (history (rcd-ask-history-variable prompt))
         (input (read-from-minibuffer prompt initial-contents keymap read 
history default-value t))
         (input (string-trim input)))
    input))

;; (rcd-ask "First name: ") ⇒ "Emmanuel"
;; In the next step "Emmanuel" is automatically history, and one can get him by 
M-n M-p
;; (rcd-ask "First name: ")
;; (rcd-ask) ⇒ "Here is some string"

And I also like this function that asks few TIMES for something:

(defun rcd-ask-times (times &optional prompt initial-contents keymap read 
default-value)
  "Return list of queries by multiples TIMES"
  (let ((list ())
        (count 0))
    (while (< count times)
      (push (rcd-ask (concat (format "[#%s] " (1+ count)) prompt) 
initial-contents keymap read default-value) list)
    (setq count (1+ count)))
    (reverse list)))

;; (rcd-ask-times 3 "Query: ") ⇒ ("Emmanuel" "Berg" "Sweden")


-- 
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]