diff options
| author | Juri Linkov | 2019-11-10 00:21:26 +0200 |
|---|---|---|
| committer | Juri Linkov | 2019-11-10 00:21:26 +0200 |
| commit | 04ab67470706f1c66bdf08e4078ea3dffd79b41e (patch) | |
| tree | b8468ce0cec9f91339b964931a81026623f3daa8 /lisp | |
| parent | a26a8cc1c85f29fb11209c16d53a8ae4e4ab7ced (diff) | |
| download | emacs-04ab67470706f1c66bdf08e4078ea3dffd79b41e.tar.gz emacs-04ab67470706f1c66bdf08e4078ea3dffd79b41e.zip | |
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.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/emacs-lisp/map-ynp.el | 6 | ||||
| -rw-r--r-- | lisp/simple.el | 79 |
2 files changed, 61 insertions, 24 deletions
diff --git a/lisp/emacs-lisp/map-ynp.el b/lisp/emacs-lisp/map-ynp.el index a688330b74a..5c0e28eac9c 100644 --- a/lisp/emacs-lisp/map-ynp.el +++ b/lisp/emacs-lisp/map-ynp.el | |||
| @@ -341,7 +341,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input." | |||
| 341 | (delete-minibuffer-contents) | 341 | (delete-minibuffer-contents) |
| 342 | (beep) | 342 | (beep) |
| 343 | (message message) | 343 | (message message) |
| 344 | (sleep-for 2))) | 344 | (sit-for 2))) |
| 345 | map) | 345 | map) |
| 346 | read-answer-map--memoize)))) | 346 | read-answer-map--memoize)))) |
| 347 | answer) | 347 | answer) |
| @@ -361,7 +361,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input." | |||
| 361 | (short | 361 | (short |
| 362 | (read-from-minibuffer | 362 | (read-from-minibuffer |
| 363 | prompt nil short-answer-map nil | 363 | prompt nil short-answer-map nil |
| 364 | 'yes-or-no-p-history)) | 364 | 'read-char-history)) |
| 365 | (t | 365 | (t |
| 366 | (read-from-minibuffer | 366 | (read-from-minibuffer |
| 367 | prompt nil nil nil | 367 | prompt nil nil nil |
| @@ -381,7 +381,7 @@ When `use-dialog-box' is t, pop up a dialog window to get user input." | |||
| 381 | ".\n"))) | 381 | ".\n"))) |
| 382 | (beep) | 382 | (beep) |
| 383 | (message message) | 383 | (message message) |
| 384 | (sleep-for 2))) | 384 | (sit-for 2))) |
| 385 | answer)) | 385 | answer)) |
| 386 | 386 | ||
| 387 | ;;; map-ynp.el ends here | 387 | ;;; map-ynp.el ends here |
diff --git a/lisp/simple.el b/lisp/simple.el index 6677291ebab..6dc4e5666da 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -5174,46 +5174,83 @@ and KILLP is t if a prefix arg was specified." | |||
| 5174 | ;; Avoid warning about delete-backward-char | 5174 | ;; Avoid warning about delete-backward-char |
| 5175 | (with-no-warnings (delete-backward-char n killp)))) | 5175 | (with-no-warnings (delete-backward-char n killp)))) |
| 5176 | 5176 | ||
| 5177 | (defvar read-char-from-minibuffer-history nil | 5177 | (defvar read-char-history nil |
| 5178 | "The default history for the `read-char-from-minibuffer' function.") | 5178 | "The default history for the `read-char-from-minibuffer' function.") |
| 5179 | 5179 | ||
| 5180 | (defvar read-char-from-minibuffer-map | 5180 | (defvar read-char-from-minibuffer-map |
| 5181 | (let ((map (make-sparse-keymap))) | 5181 | (let ((map (make-sparse-keymap))) |
| 5182 | (set-keymap-parent map minibuffer-local-map) | 5182 | (set-keymap-parent map minibuffer-local-map) |
| 5183 | (define-key map [remap self-insert-command] | 5183 | (define-key map [remap self-insert-command] |
| 5184 | 'read-char-from-minibuffer-self-insert) | 5184 | 'read-char-from-minibuffer-insert-char) |
| 5185 | map) | 5185 | map) |
| 5186 | "Keymap for the `read-char-from-minibuffer' function.") | 5186 | "Keymap for the `read-char-from-minibuffer' function.") |
| 5187 | 5187 | ||
| 5188 | (defun read-char-from-minibuffer-self-insert () | 5188 | (defconst read-char-from-minibuffer-map-hash |
| 5189 | "Insert the character you type in the minibuffer." | 5189 | (make-hash-table :weakness 'key :test 'equal)) |
| 5190 | |||
| 5191 | (defun read-char-from-minibuffer-insert-char () | ||
| 5192 | "Insert the character you type in the minibuffer and exit. | ||
| 5193 | Discard all previous input before inserting and exiting the minibuffer." | ||
| 5190 | (interactive) | 5194 | (interactive) |
| 5191 | (delete-minibuffer-contents) | 5195 | (delete-minibuffer-contents) |
| 5192 | (insert (event-basic-type last-command-event)) | 5196 | (insert last-command-event) |
| 5193 | (exit-minibuffer)) | 5197 | (exit-minibuffer)) |
| 5194 | 5198 | ||
| 5195 | (defun read-char-from-minibuffer (prompt) | 5199 | (defun read-char-from-minibuffer-insert-other () |
| 5196 | "Read a character from the minibuffer, prompting with string PROMPT. | 5200 | "Handle inserting of a character other than allowed. |
| 5197 | Like `read-char', but allows navigating in a history. The navigation | 5201 | Display an error on trying to insert a disallowed character. |
| 5198 | commands are `M-p' and `M-n', with `RET' to select a character from | 5202 | Also discard all previous input in the minibuffer." |
| 5199 | history." | 5203 | (interactive) |
| 5200 | (let ((result | 5204 | (delete-minibuffer-contents) |
| 5201 | (read-from-minibuffer prompt nil | 5205 | (ding) |
| 5202 | read-char-from-minibuffer-map nil | 5206 | (minibuffer-message "Wrong answer") |
| 5203 | 'read-char-from-minibuffer-history))) | 5207 | (sit-for 2)) |
| 5204 | (if (> (length result) 0) | 5208 | |
| 5205 | ;; We have a string (with one character), so return the first one. | 5209 | (defvar empty-history) |
| 5206 | (elt result 0) | 5210 | |
| 5207 | ;; The default value is RET. | 5211 | (defun read-char-from-minibuffer (prompt &optional chars history) |
| 5208 | (push "\r" read-char-from-minibuffer-history) | 5212 | "Read a character from the minibuffer, prompting for PROMPT. |
| 5209 | ?\r))) | 5213 | Like `read-char', but uses the minibuffer to read and return a character. |
| 5214 | When CHARS is non-nil, any input that is not one of CHARS is ignored. | ||
| 5215 | When HISTORY is a symbol, then allows navigating in a history. | ||
| 5216 | The navigation commands are `M-p' and `M-n', with `RET' to select | ||
| 5217 | a character from history." | ||
| 5218 | (discard-input) | ||
| 5219 | (let* ((empty-history '()) | ||
| 5220 | (map (if (consp chars) | ||
| 5221 | (or (gethash chars read-char-from-minibuffer-map-hash) | ||
| 5222 | (puthash chars | ||
| 5223 | (let ((map (make-sparse-keymap))) | ||
| 5224 | (set-keymap-parent map read-char-from-minibuffer-map) | ||
| 5225 | (dolist (char chars) | ||
| 5226 | (define-key map (vector char) | ||
| 5227 | 'read-char-from-minibuffer-insert-char)) | ||
| 5228 | (define-key map [remap self-insert-command] | ||
| 5229 | 'read-char-from-minibuffer-insert-other) | ||
| 5230 | map) | ||
| 5231 | read-char-from-minibuffer-map-hash)) | ||
| 5232 | read-char-from-minibuffer-map)) | ||
| 5233 | (result | ||
| 5234 | (read-from-minibuffer prompt nil map nil | ||
| 5235 | (or history 'empty-history))) | ||
| 5236 | (char | ||
| 5237 | (if (> (length result) 0) | ||
| 5238 | ;; We have a string (with one character), so return the first one. | ||
| 5239 | (elt result 0) | ||
| 5240 | ;; The default value is RET. | ||
| 5241 | (when history (push "\r" (symbol-value history))) | ||
| 5242 | ?\r))) | ||
| 5243 | ;; Display the question with the answer. | ||
| 5244 | (message "%s%s" prompt (char-to-string char)) | ||
| 5245 | char)) | ||
| 5210 | 5246 | ||
| 5211 | (defun zap-to-char (arg char) | 5247 | (defun zap-to-char (arg char) |
| 5212 | "Kill up to and including ARGth occurrence of CHAR. | 5248 | "Kill up to and including ARGth occurrence of CHAR. |
| 5213 | Case is ignored if `case-fold-search' is non-nil in the current buffer. | 5249 | Case is ignored if `case-fold-search' is non-nil in the current buffer. |
| 5214 | Goes backward if ARG is negative; error if CHAR not found." | 5250 | Goes backward if ARG is negative; error if CHAR not found." |
| 5215 | (interactive (list (prefix-numeric-value current-prefix-arg) | 5251 | (interactive (list (prefix-numeric-value current-prefix-arg) |
| 5216 | (read-char-from-minibuffer "Zap to char: "))) | 5252 | (read-char-from-minibuffer "Zap to char: " |
| 5253 | nil 'read-char-history))) | ||
| 5217 | ;; Avoid "obsolete" warnings for translation-table-for-input. | 5254 | ;; Avoid "obsolete" warnings for translation-table-for-input. |
| 5218 | (with-no-warnings | 5255 | (with-no-warnings |
| 5219 | (if (char-table-p translation-table-for-input) | 5256 | (if (char-table-p translation-table-for-input) |