Make "C-g" and "ESC" fail silently when reading char

* avy.el (avy-ignored-modes): Add type.
(avy-handler-default): Silence "C-g" and "ESC".
(avy-goto-line): Call `avy-handler-function' instead of
`avy-handler-default'.
(avy-timeout-seconds): Add type.

Fixes #137
This commit is contained in:
Oleh Krehel 2016-03-16 13:48:29 +01:00
parent 5f76c9d16e
commit 3b75d9520d

26
avy.el
View File

@ -165,7 +165,8 @@ When nil, punctuation chars will not be matched.
(defcustom avy-ignored-modes '(image-mode doc-view-mode pdf-view-mode) (defcustom avy-ignored-modes '(image-mode doc-view-mode pdf-view-mode)
"List of modes to ignore when searching for candidates. "List of modes to ignore when searching for candidates.
Typically, these modes don't use the text representation.") Typically, these modes don't use the text representation."
:type 'list)
(defvar avy-ring (make-ring 20) (defvar avy-ring (make-ring 20)
"Hold the window and point history.") "Hold the window and point history.")
@ -352,12 +353,15 @@ KEYS is the path from the root of `avy-tree' to LEAF."
(defun avy-handler-default (char) (defun avy-handler-default (char)
"The default handler for a bad CHAR." "The default handler for a bad CHAR."
(let (dispatch) (let (dispatch)
(if (setq dispatch (assoc char avy-dispatch-alist)) (cond ((setq dispatch (assoc char avy-dispatch-alist))
(progn (setq avy-action (cdr dispatch))
(setq avy-action (cdr dispatch)) (throw 'done 'restart))
(throw 'done 'restart)) ((memq char '(27 ?\C-g))
(signal 'user-error (list "No such candidate" char)) ;; exit silently
(throw 'done nil)))) (throw 'done 'exit))
(t
(signal 'user-error (list "No such candidate" char))
(throw 'done nil)))))
(defvar avy-handler-function 'avy-handler-default (defvar avy-handler-function 'avy-handler-default
"A function to call for a bad `read-key' in `avy-read'.") "A function to call for a bad `read-key' in `avy-read'.")
@ -1090,11 +1094,12 @@ Otherwise, forward to `goto-line' with ARG."
(goto-char (point-min)) (goto-char (point-min))
(forward-line (1- arg))) (forward-line (1- arg)))
(avy-with avy-goto-line (avy-with avy-goto-line
(let* ((avy-handler-function (let* ((avy-handler-old avy-handler-function)
(avy-handler-function
(lambda (char) (lambda (char)
(if (or (< char ?0) (if (or (< char ?0)
(> char ?9)) (> char ?9))
(avy-handler-default char) (funcall avy-handler-old char)
(let ((line (read-from-minibuffer (let ((line (read-from-minibuffer
"Goto line: " (string char)))) "Goto line: " (string char))))
(when line (when line
@ -1223,7 +1228,8 @@ The window scope is determined by `avy-all-windows' or
'(define-key isearch-mode-map (kbd "C-'") 'avy-isearch))) '(define-key isearch-mode-map (kbd "C-'") 'avy-isearch)))
(defcustom avy-timeout-seconds 0.5 (defcustom avy-timeout-seconds 0.5
"How many seconds to wait for the second char.") "How many seconds to wait for the second char."
:type 'float)
(defun avy--read-candidates () (defun avy--read-candidates ()
"Read as many chars as possible and return their occurences. "Read as many chars as possible and return their occurences.