Backport Handle read-char-from-minibuffer and y-or-n-p from pre-command-hook

* lisp/subr.el (read-char-from-minibuffer-insert-char)
(read-char-from-minibuffer-insert-other, y-or-n-p-insert-y)
(y-or-n-p-insert-n, y-or-n-p-insert-other):
Check for 'minibufferp' before executing the body.
(read-char-from-minibuffer, y-or-n-p): Let-bind this-command
before calling read-from-minibuffer.  (Bug#45029)

; Do not merge to master.
This commit is contained in:
Juri Linkov 2020-12-06 23:12:13 +02:00
parent 32090a3de4
commit a40be60ec8

View file

@ -2697,20 +2697,22 @@ floating point support."
"Insert the character you type in the minibuffer and exit. "Insert the character you type in the minibuffer and exit.
Discard all previous input before inserting and exiting the minibuffer." Discard all previous input before inserting and exiting the minibuffer."
(interactive) (interactive)
(delete-minibuffer-contents) (when (minibufferp)
(insert last-command-event) (delete-minibuffer-contents)
(exit-minibuffer)) (insert last-command-event)
(exit-minibuffer)))
(defun read-char-from-minibuffer-insert-other () (defun read-char-from-minibuffer-insert-other ()
"Handle inserting of a character other than allowed. "Handle inserting of a character other than allowed.
Display an error on trying to insert a disallowed character. Display an error on trying to insert a disallowed character.
Also discard all previous input in the minibuffer." Also discard all previous input in the minibuffer."
(interactive) (interactive)
(delete-minibuffer-contents) (when (minibufferp)
(ding) (delete-minibuffer-contents)
(discard-input) (ding)
(minibuffer-message "Wrong answer") (discard-input)
(sit-for 2)) (minibuffer-message "Wrong answer")
(sit-for 2)))
(defvar empty-history) (defvar empty-history)
@ -2737,6 +2739,8 @@ history."
map) map)
read-char-from-minibuffer-map-hash)) read-char-from-minibuffer-map-hash))
read-char-from-minibuffer-map)) read-char-from-minibuffer-map))
;; Protect this-command when called from pre-command-hook (bug#45029)
(this-command this-command)
(result (result
(read-from-minibuffer prompt nil map nil (read-from-minibuffer prompt nil map nil
(or history 'empty-history))) (or history 'empty-history)))
@ -2791,28 +2795,31 @@ history."
"Insert the answer \"y\" and exit the minibuffer of `y-or-n-p'. "Insert the answer \"y\" and exit the minibuffer of `y-or-n-p'.
Discard all previous input before inserting and exiting the minibuffer." Discard all previous input before inserting and exiting the minibuffer."
(interactive) (interactive)
(delete-minibuffer-contents) (when (minibufferp)
(insert "y") (delete-minibuffer-contents)
(exit-minibuffer)) (insert "y")
(exit-minibuffer)))
(defun y-or-n-p-insert-n () (defun y-or-n-p-insert-n ()
"Insert the answer \"n\" and exit the minibuffer of `y-or-n-p'. "Insert the answer \"n\" and exit the minibuffer of `y-or-n-p'.
Discard all previous input before inserting and exiting the minibuffer." Discard all previous input before inserting and exiting the minibuffer."
(interactive) (interactive)
(delete-minibuffer-contents) (when (minibufferp)
(insert "n") (delete-minibuffer-contents)
(exit-minibuffer)) (insert "n")
(exit-minibuffer)))
(defun y-or-n-p-insert-other () (defun y-or-n-p-insert-other ()
"Handle inserting of other answers in the minibuffer of `y-or-n-p'. "Handle inserting of other answers in the minibuffer of `y-or-n-p'.
Display an error on trying to insert a disallowed character. Display an error on trying to insert a disallowed character.
Also discard all previous input in the minibuffer." Also discard all previous input in the minibuffer."
(interactive) (interactive)
(delete-minibuffer-contents) (when (minibufferp)
(ding) (delete-minibuffer-contents)
(discard-input) (ding)
(minibuffer-message "Please answer y or n") (discard-input)
(sit-for 2)) (minibuffer-message "Please answer y or n")
(sit-for 2)))
(defvar empty-history) (defvar empty-history)
@ -2865,6 +2872,8 @@ is nil and `use-dialog-box' is non-nil."
(setq prompt (funcall padded prompt)) (setq prompt (funcall padded prompt))
(let* ((empty-history '()) (let* ((empty-history '())
(enable-recursive-minibuffers t) (enable-recursive-minibuffers t)
;; Protect this-command when called from pre-command-hook (bug#45029)
(this-command this-command)
(str (read-from-minibuffer (str (read-from-minibuffer
prompt nil prompt nil
(make-composed-keymap y-or-n-p-map query-replace-map) (make-composed-keymap y-or-n-p-map query-replace-map)