avy.el (avy--process-1): Extract

This commit is contained in:
Oleh Krehel 2018-06-07 18:17:53 +02:00
parent 8606a8984b
commit 70e384aee5

87
avy.el
View File

@ -725,6 +725,29 @@ Set `avy-style' according to COMMMAND as well."
(when (looking-at-p "\\b") (when (looking-at-p "\\b")
(ispell-word))))))) (ispell-word)))))))
(defun avy--process-1 (candidates overlay-fn)
(let ((len (length candidates)))
(cond ((= len 0)
nil)
((= len 1)
(car candidates))
(t
(unwind-protect
(progn
(avy--make-backgrounds
(avy-window-list))
(cond ((eq avy-style 'de-bruijn)
(avy-read-de-bruijn
candidates avy-keys))
((eq avy-style 'words)
(avy-read-words
candidates avy-words))
(t
(avy-read (avy-tree candidates avy-keys)
overlay-fn
#'avy--remove-leading-chars))))
(avy--done))))))
(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'.
Use OVERLAY-FN to visualize the decision overlay." Use OVERLAY-FN to visualize the decision overlay."
@ -733,48 +756,30 @@ Use OVERLAY-FN to visualize the decision overlay."
(setq candidates (setq candidates
(mapcar (lambda (x) (cons x (selected-window))) (mapcar (lambda (x) (cons x (selected-window)))
candidates))) candidates)))
(let ((len (length candidates)) (let ((res (avy--process-1 candidates overlay-fn)))
res) (cond
(if (= len 0) ((null res)
(progn (message "zero candidates")
(message "zero candidates") t)
t) ((eq res 'restart)
(if (= len 1) (avy--process candidates overlay-fn))
(setq res (car candidates)) ;; ignore exit from `avy-handler-function'
(unwind-protect ((eq res 'exit))
(progn (t
(avy--make-backgrounds (avy-push-mark)
(avy-window-list)) (when (and (consp res)
(setq res (cond ((eq avy-style 'de-bruijn) (windowp (cdr res)))
(avy-read-de-bruijn (let* ((window (cdr res))
candidates avy-keys)) (frame (window-frame window)))
((eq avy-style 'words) (unless (equal frame (selected-frame))
(avy-read-words (select-frame-set-input-focus frame))
candidates avy-words)) (select-window window))
(t (setq res (car res)))
(avy-read (avy-tree candidates avy-keys)
overlay-fn
#'avy--remove-leading-chars)))))
(avy--done)))
(cond ((eq res 'restart)
(avy--process candidates overlay-fn))
;; ignore exit from `avy-handler-function'
((eq res 'exit))
(t
(avy-push-mark)
(when (and (consp res)
(windowp (cdr res)))
(let* ((window (cdr res))
(frame (window-frame window)))
(unless (equal frame (selected-frame))
(select-frame-set-input-focus frame))
(select-window window))
(setq res (car res)))
(funcall (or avy-action 'avy-action-goto) (funcall (or avy-action 'avy-action-goto)
(if (consp res) (if (consp res)
(car res) (car res)
res))))))) res))))))
(defvar avy--overlays-back nil (defvar avy--overlays-back nil
"Hold overlays for when `avy-background' is t.") "Hold overlays for when `avy-background' is t.")