Commit 04ab6747 authored by Juri Linkov's avatar Juri Linkov

Add CHARS arg to read-char-from-minibuffer compatible with read-char-choice.

* lisp/simple.el (read-char-history):
Rename from read-char-from-minibuffer-history.  (Bug#38076)
(read-char-from-minibuffer-insert-char):
Rename from read-char-from-minibuffer-self-insert.
(read-char-from-minibuffer-map-hash): New defconst.
(read-char-from-minibuffer-insert-other): New command.
(read-char-from-minibuffer): Add optional args CHARS and HISTORY.
(zap-to-char): Use 'read-char-history as HISTORY arg of
read-char-from-minibuffer.

* lisp/emacs-lisp/map-ynp.el (read-answer): Use sit-for instead of sleep-for.
Replace short answer history yes-or-no-p-history with read-char-history.
parent a26a8cc1
Pipeline #3991 failed with stage
in 90 minutes and 2 seconds
...@@ -341,7 +341,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input." ...@@ -341,7 +341,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input."
(delete-minibuffer-contents) (delete-minibuffer-contents)
(beep) (beep)
(message message) (message message)
(sleep-for 2))) (sit-for 2)))
map) map)
read-answer-map--memoize)))) read-answer-map--memoize))))
answer) answer)
...@@ -361,7 +361,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input." ...@@ -361,7 +361,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input."
(short (short
(read-from-minibuffer (read-from-minibuffer
prompt nil short-answer-map nil prompt nil short-answer-map nil
'yes-or-no-p-history)) 'read-char-history))
(t (t
(read-from-minibuffer (read-from-minibuffer
prompt nil nil nil prompt nil nil nil
...@@ -381,7 +381,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input." ...@@ -381,7 +381,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input."
".\n"))) ".\n")))
(beep) (beep)
(message message) (message message)
(sleep-for 2))) (sit-for 2)))
answer)) answer))
;;; map-ynp.el ends here ;;; map-ynp.el ends here
...@@ -5174,46 +5174,83 @@ and KILLP is t if a prefix arg was specified." ...@@ -5174,46 +5174,83 @@ and KILLP is t if a prefix arg was specified."
;; Avoid warning about delete-backward-char ;; Avoid warning about delete-backward-char
(with-no-warnings (delete-backward-char n killp)))) (with-no-warnings (delete-backward-char n killp))))
(defvar read-char-from-minibuffer-history nil (defvar read-char-history nil
"The default history for the `read-char-from-minibuffer' function.") "The default history for the `read-char-from-minibuffer' function.")
(defvar read-char-from-minibuffer-map (defvar read-char-from-minibuffer-map
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map) (set-keymap-parent map minibuffer-local-map)
(define-key map [remap self-insert-command] (define-key map [remap self-insert-command]
'read-char-from-minibuffer-self-insert) 'read-char-from-minibuffer-insert-char)
map) map)
"Keymap for the `read-char-from-minibuffer' function.") "Keymap for the `read-char-from-minibuffer' function.")
(defun read-char-from-minibuffer-self-insert () (defconst read-char-from-minibuffer-map-hash
"Insert the character you type in the minibuffer." (make-hash-table :weakness 'key :test 'equal))
(defun read-char-from-minibuffer-insert-char ()
"Insert the character you type in the minibuffer and exit.
Discard all previous input before inserting and exiting the minibuffer."
(interactive) (interactive)
(delete-minibuffer-contents) (delete-minibuffer-contents)
(insert (event-basic-type last-command-event)) (insert last-command-event)
(exit-minibuffer)) (exit-minibuffer))
(defun read-char-from-minibuffer (prompt) (defun read-char-from-minibuffer-insert-other ()
"Read a character from the minibuffer, prompting with string PROMPT. "Handle inserting of a character other than allowed.
Like `read-char', but allows navigating in a history. The navigation Display an error on trying to insert a disallowed character.
commands are `M-p' and `M-n', with `RET' to select a character from Also discard all previous input in the minibuffer."
history." (interactive)
(let ((result (delete-minibuffer-contents)
(read-from-minibuffer prompt nil (ding)
read-char-from-minibuffer-map nil (minibuffer-message "Wrong answer")
'read-char-from-minibuffer-history))) (sit-for 2))
(if (> (length result) 0)
;; We have a string (with one character), so return the first one. (defvar empty-history)
(elt result 0)
;; The default value is RET. (defun read-char-from-minibuffer (prompt &optional chars history)
(push "\r" read-char-from-minibuffer-history) "Read a character from the minibuffer, prompting for PROMPT.
?\r))) Like `read-char', but uses the minibuffer to read and return a character.
When CHARS is non-nil, any input that is not one of CHARS is ignored.
When HISTORY is a symbol, then allows navigating in a history.
The navigation commands are `M-p' and `M-n', with `RET' to select
a character from history."
(discard-input)
(let* ((empty-history '())
(map (if (consp chars)
(or (gethash chars read-char-from-minibuffer-map-hash)
(puthash chars
(let ((map (make-sparse-keymap)))
(set-keymap-parent map read-char-from-minibuffer-map)
(dolist (char chars)
(define-key map (vector char)
'read-char-from-minibuffer-insert-char))
(define-key map [remap self-insert-command]
'read-char-from-minibuffer-insert-other)
map)
read-char-from-minibuffer-map-hash))
read-char-from-minibuffer-map))
(result
(read-from-minibuffer prompt nil map nil
(or history 'empty-history)))
(char
(if (> (length result) 0)
;; We have a string (with one character), so return the first one.
(elt result 0)
;; The default value is RET.
(when history (push "\r" (symbol-value history)))
?\r)))
;; Display the question with the answer.
(message "%s%s" prompt (char-to-string char))
char))
(defun zap-to-char (arg char) (defun zap-to-char (arg char)
"Kill up to and including ARGth occurrence of CHAR. "Kill up to and including ARGth occurrence of CHAR.
Case is ignored if `case-fold-search' is non-nil in the current buffer. Case is ignored if `case-fold-search' is non-nil in the current buffer.
Goes backward if ARG is negative; error if CHAR not found." Goes backward if ARG is negative; error if CHAR not found."
(interactive (list (prefix-numeric-value current-prefix-arg) (interactive (list (prefix-numeric-value current-prefix-arg)
(read-char-from-minibuffer "Zap to char: "))) (read-char-from-minibuffer "Zap to char: "
nil 'read-char-history)))
;; Avoid "obsolete" warnings for translation-table-for-input. ;; Avoid "obsolete" warnings for translation-table-for-input.
(with-no-warnings (with-no-warnings
(if (char-table-p translation-table-for-input) (if (char-table-p translation-table-for-input)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment