diff options
| author | Juri Linkov | 2019-10-30 00:01:11 +0100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2019-10-30 00:01:11 +0100 |
| commit | bafcef87c33e3e549bd76852aec6e5073c4ad388 (patch) | |
| tree | e9e9039576fd58da2a72ff3a84795fb7ba12dd15 | |
| parent | d7f62ce0cd58f6275bbf88925f3abbbd1db212a8 (diff) | |
| download | emacs-bafcef87c33e3e549bd76852aec6e5073c4ad388.tar.gz emacs-bafcef87c33e3e549bd76852aec6e5073c4ad388.zip | |
Reimplement read-char-with-history based on read-from-minibuffer
* lisp/simple.el (read-char-with-history): Reimplement based on
read-from-minibuffer.
* lisp/simple.el (read-char-with-history--map): New variable
(bug#10477).
| -rw-r--r-- | lisp/simple.el | 90 |
1 files changed, 25 insertions, 65 deletions
diff --git a/lisp/simple.el b/lisp/simple.el index fca90690a5f..5b84c3ea574 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -5167,83 +5167,43 @@ and KILLP is t if a prefix arg was specified." | |||
| 5167 | (with-no-warnings (delete-backward-char n killp)))) | 5167 | (with-no-warnings (delete-backward-char n killp)))) |
| 5168 | 5168 | ||
| 5169 | (defvar read-char-with-history--history nil | 5169 | (defvar read-char-with-history--history nil |
| 5170 | "The default history for `read-char-with-history'.") | 5170 | "The default history for the `read-char-with-history' function.") |
| 5171 | 5171 | ||
| 5172 | (defun read-char-with-history (prompt &optional inherit-input-method seconds | 5172 | (defvar read-char-with-history--map |
| 5173 | history) | 5173 | (let ((map (make-sparse-keymap))) |
| 5174 | (set-keymap-parent map minibuffer-local-map) | ||
| 5175 | (define-key map [remap self-insert-command] | ||
| 5176 | (lambda () | ||
| 5177 | (interactive) | ||
| 5178 | (delete-minibuffer-contents) | ||
| 5179 | (insert (event-basic-type last-command-event)) | ||
| 5180 | (exit-minibuffer))) | ||
| 5181 | map) | ||
| 5182 | "Keymap for the `read-char-with-history' function.") | ||
| 5183 | |||
| 5184 | (defun read-char-with-history (prompt) | ||
| 5174 | "Like `read-char', but allows navigating in a history. | 5185 | "Like `read-char', but allows navigating in a history. |
| 5175 | HISTORY is like HIST in `read-from-minibuffer'. | 5186 | HISTORY is like HIST in `read-from-minibuffer'. |
| 5176 | 5187 | ||
| 5177 | The navigation commands are `M-p' and `M-n', with `RET' to select | 5188 | The navigation commands are `M-p' and `M-n', with `RET' to select |
| 5178 | a character from history." | 5189 | a character from history." |
| 5179 | (let* ((result nil) | 5190 | (let ((result |
| 5180 | (real-prompt prompt) | 5191 | (read-from-minibuffer prompt nil |
| 5181 | (hist-format | 5192 | read-char-with-history--map nil |
| 5182 | (lambda (char) | 5193 | 'read-char-with-history--history))) |
| 5183 | (if (string-match ": *\\'" real-prompt) | 5194 | (if (> (length result) 0) |
| 5184 | (format "%s (default %c): " | 5195 | ;; We have a string (with one character), so return the first one. |
| 5185 | (substring real-prompt 0 (match-beginning 0)) | 5196 | (elt result 0) |
| 5186 | char) | 5197 | ;; The default value is RET. |
| 5187 | (format "%s (default %c) " real-prompt char)))) | 5198 | (push "\r" read-char-with-history--history) |
| 5188 | (index 0) | 5199 | ?\r))) |
| 5189 | histvar) | ||
| 5190 | ;; Use the same history interface as `read-from-minibuffer'. | ||
| 5191 | (cond | ||
| 5192 | ((null history) | ||
| 5193 | (setq histvar 'read-char-with-history--history)) | ||
| 5194 | ((consp history) | ||
| 5195 | (setq histvar (car history) | ||
| 5196 | index (cdr history))) | ||
| 5197 | ((symbolp history) | ||
| 5198 | (setq histvar history)) | ||
| 5199 | (t | ||
| 5200 | (error "Invalid history: %s" history))) | ||
| 5201 | (while (not result) | ||
| 5202 | (setq result (read-event prompt inherit-input-method seconds)) | ||
| 5203 | ;; Go back in history. | ||
| 5204 | (cond | ||
| 5205 | ((memq result '(?\M-p up)) | ||
| 5206 | (if (>= index (length (symbol-value histvar))) | ||
| 5207 | (progn | ||
| 5208 | (message "Beginning of history; no preceding item") | ||
| 5209 | (ding) | ||
| 5210 | (sit-for 2)) | ||
| 5211 | (setq index (1+ index) | ||
| 5212 | prompt (funcall hist-format | ||
| 5213 | (elt (symbol-value histvar) (1- index))))) | ||
| 5214 | (setq result nil)) | ||
| 5215 | ;; Go forward in history. | ||
| 5216 | ((memq result '(?\M-n down)) | ||
| 5217 | (if (zerop index) | ||
| 5218 | (progn | ||
| 5219 | (message "End of history; no next item") | ||
| 5220 | (ding) | ||
| 5221 | (sit-for 2)) | ||
| 5222 | (setq index (1- index) | ||
| 5223 | prompt (if (zerop index) | ||
| 5224 | real-prompt | ||
| 5225 | (funcall hist-format | ||
| 5226 | (elt (symbol-value histvar) (1- index)))))) | ||
| 5227 | (setq result nil)) | ||
| 5228 | ;; The user hits RET to either select a history item or to | ||
| 5229 | ;; return RET. | ||
| 5230 | ((eq result 'return) | ||
| 5231 | (if (zerop index) | ||
| 5232 | (setq result ?\r) | ||
| 5233 | (setq result (elt (symbol-value histvar) (1- index))))) | ||
| 5234 | ;; The user has entered some non-character event. | ||
| 5235 | ((not (characterp result)) | ||
| 5236 | (user-error "Non-character input event")))) | ||
| 5237 | ;; Record the chosen key. | ||
| 5238 | (set histvar (cons result (symbol-value histvar))) | ||
| 5239 | result)) | ||
| 5240 | 5200 | ||
| 5241 | (defun zap-to-char (arg char) | 5201 | (defun zap-to-char (arg char) |
| 5242 | "Kill up to and including ARGth occurrence of CHAR. | 5202 | "Kill up to and including ARGth occurrence of CHAR. |
| 5243 | Case is ignored if `case-fold-search' is non-nil in the current buffer. | 5203 | Case is ignored if `case-fold-search' is non-nil in the current buffer. |
| 5244 | Goes backward if ARG is negative; error if CHAR not found." | 5204 | Goes backward if ARG is negative; error if CHAR not found." |
| 5245 | (interactive (list (prefix-numeric-value current-prefix-arg) | 5205 | (interactive (list (prefix-numeric-value current-prefix-arg) |
| 5246 | (read-char-with-history "Zap to char: " t))) | 5206 | (read-char-with-history "Zap to char: "))) |
| 5247 | ;; Avoid "obsolete" warnings for translation-table-for-input. | 5207 | ;; Avoid "obsolete" warnings for translation-table-for-input. |
| 5248 | (with-no-warnings | 5208 | (with-no-warnings |
| 5249 | (if (char-table-p translation-table-for-input) | 5209 | (if (char-table-p translation-table-for-input) |