Allow to break from avy-goto-line' into goto-line'

* avy.el (avy-handler-default): New defun.
(avy-handler-function): New variable. Bind this temporarily to catch bad chars.
(avy-read): Forward to `avy-handler-default'.

* avy-jump.el (avy--goto): Consider the case of 'exit symbol being
  returned.
(avy-goto-line): Bind `avy-handler-function' to catch digits and call
`goto-line' in that case.

Fixes #29
This commit is contained in:
Oleh Krehel 2015-05-09 18:14:50 +02:00
parent a08b049edd
commit a53ffb7cec
2 changed files with 34 additions and 11 deletions

View File

@ -137,14 +137,19 @@ When nil, punctuation chars will not be matched.
"Goto X. "Goto X.
X is (POS . WND) X is (POS . WND)
POS is either a position or (BEG . END)." POS is either a position or (BEG . END)."
(if (null x) (cond ((null x)
(message "zero candidates") (message "zero candidates"))
(select-window (cdr x))
(let ((pt (car x))) ;; ignore exit from `avy-handler-function'
(when (consp pt) ((eq x 'exit))
(setq pt (car pt)))
(unless (= pt (point)) (push-mark)) (t
(goto-char pt)))) (select-window (cdr x))
(let ((pt (car x)))
(when (consp pt)
(setq pt (car pt)))
(unless (= pt (point)) (push-mark))
(goto-char pt)))))
(defun avy--process (candidates overlay-fn) (defun avy--process (candidates overlay-fn)
"Select one of CANDIDATES using `avy-read'. "Select one of CANDIDATES using `avy-read'.
@ -474,7 +479,18 @@ The window scope is determined by `avy-all-windows' (ARG negates it)."
The window scope is determined by `avy-all-windows' (ARG negates it)." The window scope is determined by `avy-all-windows' (ARG negates it)."
(interactive "P") (interactive "P")
(avy--with-avy-keys avy-goto-line (avy--with-avy-keys avy-goto-line
(avy--goto (avy--line arg)))) (let ((avy-handler-function
(lambda (char)
(if (or (< char ?0)
(> char ?9))
(avy-handler-default char)
(let ((line (read-from-minibuffer
"Goto line: " (string char))))
(when line
(goto-char (point-min))
(forward-line (1- (string-to-number line)))
(throw 'done 'exit)))))))
(avy--goto (avy--line arg)))))
;;;###autoload ;;;###autoload
(defun avy-copy-line (arg) (defun avy-copy-line (arg)

11
avy.el
View File

@ -109,6 +109,14 @@ KEYS is the path from the root of `avy-tree' to LEAF."
(funcall walker key (cddr br)) (funcall walker key (cddr br))
(avy-traverse (cdr br) walker key))))) (avy-traverse (cdr br) walker key)))))
(defun avy-handler-default (char)
"The default hander for a bad CHAR."
(signal 'user-error (list "No such candidate" char))
(throw 'done nil))
(defvar avy-handler-function 'avy-handler-default
"A function to call for a bad `read-char' in `avy-read'.")
(defun avy-read (tree display-fn cleanup-fn) (defun avy-read (tree display-fn cleanup-fn)
"Select a leaf from TREE using consecutive `read-char'. "Select a leaf from TREE using consecutive `read-char'.
@ -127,8 +135,7 @@ multiple DISPLAY-FN invokations."
(if (setq branch (assoc char tree)) (if (setq branch (assoc char tree))
(if (eq (car (setq tree (cdr branch))) 'leaf) (if (eq (car (setq tree (cdr branch))) 'leaf)
(throw 'done (cdr tree))) (throw 'done (cdr tree)))
(signal 'user-error (list "No such candidate" char)) (funcall avy-handler-function char))))))
(throw 'done nil))))))
(provide 'avy) (provide 'avy)