1 ;;; avy.el --- set-based completion -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/avy
8 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
9 ;; Keywords: point, location
11 ;; This file is part of GNU Emacs.
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
28 ;; This package provides a generic completion method based on building
29 ;; a balanced decision tree with each candidate being a leaf. To
30 ;; traverse the tree from the root to a desired leaf, typically a
31 ;; sequence of `read-key' can be used.
33 ;; In order for `read-key' to make sense, the tree needs to be
34 ;; visualized appropriately, with a character at each branch node. So
35 ;; this completion method works only for things that you can see on
36 ;; your screen, all at once:
38 ;; * character positions
39 ;; * word or subword start positions
40 ;; * line beginning positions
44 ;; If you're familiar with the popular `ace-jump-mode' package, this
45 ;; package does all that and more, without the implementation
53 "Jump to things tree-style."
57 (defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
58 "Default keys for jumping.
59 Any key is either a character representing a self-inserting
60 key (a-z, A-Z, 0-9, punctuation, etc.) or a symbol denoting a
61 non-printing key like an arrow key (left, right, up, down). For
62 non-printing keys, a corresponding entry in
63 `avy-key-to-char-alist' must exists in order to visualize the key
65 :type '(repeat :tag "Keys" (choice (character :tag "char")
66 (symbol :tag "non-printing key"))))
68 (defcustom avy-keys-alist nil
69 "Alist of avy-jump commands to `avy-keys' overriding the default `avy-keys'."
71 :key-type (choice :tag "Command"
73 (const avy-goto-char-2)
76 (const avy-goto-subword-0)
77 (const avy-goto-subword-1)
78 (const avy-goto-word-0)
79 (const avy-goto-word-1)
81 (const avy-copy-region)
82 (const avy-move-line))
83 :value-type (repeat :tag "Keys" character)))
85 (defcustom avy-style 'at-full
86 "The default method of displaying the overlays.
87 Use `avy-styles-alist' to customize this per-command."
89 (const :tag "Pre" pre)
91 (const :tag "At Full" at-full)
92 (const :tag "Post" post)
93 (const :tag "De Bruijn" de-bruijn)))
95 (defcustom avy-styles-alist nil
96 "Alist of avy-jump commands to the style for each command.
97 If the commands isn't on the list, `avy-style' is used."
99 :key-type (choice :tag "Command"
100 (const avy-goto-char)
101 (const avy-goto-char-2)
103 (const avy-goto-line)
104 (const avy-goto-subword-0)
105 (const avy-goto-subword-1)
106 (const avy-goto-word-0)
107 (const avy-goto-word-1)
108 (const avy-copy-line)
109 (const avy-copy-region)
110 (const avy-move-line))
112 (const :tag "Pre" pre)
114 (const :tag "At Full" at-full)
115 (const :tag "Post" post)
116 (const :tag "De Bruijn" de-bruijn))))
118 (defcustom avy-background nil
119 "When non-nil, a gray background will be added during the selection."
122 (defcustom avy-all-windows t
123 "Determine the list of windows to consider in search of candidates."
126 (const :tag "All Frames" all-frames)
127 (const :tag "This Frame" t)
128 (const :tag "This Window" nil)))
130 (defcustom avy-case-fold-search t
131 "Non-nil if searches should ignore case."
134 (defcustom avy-word-punc-regexp "[!-/:-@[-`{-~]"
135 "Regexp of punctuation chars that count as word starts for `avy-goto-word-1.
136 When nil, punctuation chars will not be matched.
138 \"[!-/:-@[-`{-~]\" will match all printable punctuation chars."
141 (defcustom avy-ignored-modes '(image-mode doc-view-mode pdf-view-mode)
142 "List of modes to ignore when searching for candidates.
143 Typically, these modes don't use the text representation.")
145 (defvar avy-translate-char-function #'identity
146 "Function to translate user input key into another key.
147 For example, to make SPC do the same as ?a, use
148 \(lambda (c) (if (= c 32) ?a c)).")
150 (defface avy-lead-face-0
151 '((t (:foreground "white" :background "#4f57f9")))
152 "Face used for first non-terminating leading chars.")
154 (defface avy-lead-face-1
155 '((t (:foreground "white" :background "gray")))
156 "Face used for matched leading chars.")
158 (defface avy-lead-face-2
159 '((t (:foreground "white" :background "#f86bf3")))
160 "Face used for leading chars.")
162 (defface avy-lead-face
163 '((t (:foreground "white" :background "#e52b50")))
164 "Face used for the leading chars.")
166 (defface avy-background-face
167 '((t (:foreground "gray40")))
168 "Face for whole window background during selection.")
170 (defconst avy-lead-faces '(avy-lead-face
176 "Face sequence for `avy--overlay-at-full'.")
178 (defvar avy-key-to-char-alist '((left . ?◀)
184 "An alist from non-character keys to chars used to represent
185 them in the avy overlays. This alist must contain all keys used
186 in `avy-keys' which are no self-inserting keys and thus aren't
187 read as characters.")
191 (defmacro avy-multipop (lst n)
192 "Remove LST's first N elements and return them."
193 `(if (<= (length ,lst) ,n)
198 (nthcdr (1- ,n) (prog1 ,lst (setq ,lst (nthcdr ,n ,lst))))
201 (defun avy--de-bruijn (keys n)
202 "De Bruijn sequence for alphabet KEYS and subsequences of length N."
203 (let* ((k (length keys))
204 (a (make-list (* n k) 0))
206 (cl-labels ((db (T p)
211 (cl-subseq a 1 (1+ p)))))
212 (setf (nth T a) (nth (- T p) a))
214 (cl-loop for j from (1+ (nth (- T p) a)) to (1- k) do
222 (defun avy--path-alist-1 (lst seq-len keys)
223 "Build a De Bruin sequence from LST.
224 SEQ-LEN is how many elements of KEYS it takes to identify a match."
225 (let ((db-seq (avy--de-bruijn keys seq-len))
226 prev-pos prev-seq prev-win path-alist)
227 ;; The De Bruijn seq is cyclic, so append the seq-len - 1 first chars to
229 (setq db-seq (nconc db-seq (cl-subseq db-seq 0 (1- seq-len))))
230 (cl-labels ((subseq-and-pop ()
231 (when (nth (1- seq-len) db-seq)
232 (prog1 (cl-subseq db-seq 0 seq-len)
235 (let* ((cur (car lst))
237 ;; ace-window has matches of the form (pos . wnd)
238 ((integerp (car cur)) (car cur))
239 ;; avy-jump have form ((start . end) . wnd)
240 ((consp (car cur)) (caar cur))
241 (t (error "Unexpected match representation: %s" cur))))
244 (let ((diff (if (eq win prev-win)
247 (when (and (> diff 0) (< diff seq-len))
248 (while (and (nth (1- seq-len) db-seq)
251 (cl-subseq prev-seq diff)
252 (cl-subseq db-seq 0 seq-len)))))
259 (push (cons path (car lst)) path-alist)
264 (nreverse path-alist)))
266 (defun avy-tree (lst keys)
267 "Coerce LST into a balanced tree.
268 The degree of the tree is the length of KEYS.
269 KEYS are placed appropriately on internal nodes."
270 (let ((len (length keys)))
273 (let ((ln (length ls)))
276 (mapcar (lambda (x) (cons 'leaf x)) ls))
277 (let ((ks (copy-sequence keys))
279 (dolist (s (avy-subdiv ln len))
282 (cons 'leaf (pop ls))
283 (rd (avy-multipop ls s))))
288 (defun avy-subdiv (n b)
289 "Distribute N in B terms in a balanced way."
290 (let* ((p (1- (floor (+ (log n b) 1e-6))))
294 (n2 (/ delta (- x2 x1)))
299 (- n (* n1 x1) (* n2 x2)))
302 (defun avy-traverse (tree walker &optional recur-key)
303 "Traverse TREE generated by `avy-tree'.
304 WALKER is a function that takes KEYS and LEAF.
306 RECUR-KEY is used in recursion.
308 LEAF is a member of LST argument of `avy-tree'.
310 KEYS is the path from the root of `avy-tree' to LEAF."
312 (let ((key (cons (car br) recur-key)))
313 (if (eq (cadr br) 'leaf)
314 (funcall walker key (cddr br))
315 (avy-traverse (cdr br) walker key)))))
317 (defun avy-handler-default (char)
318 "The default hander for a bad CHAR."
319 (signal 'user-error (list "No such candidate" char))
322 (defvar avy-handler-function 'avy-handler-default
323 "A function to call for a bad `read-key' in `avy-read'.")
325 (defvar avy-current-path ""
326 "Store the current incomplete path during `avy-read'.")
328 (defun avy-read (tree display-fn cleanup-fn)
329 "Select a leaf from TREE using consecutive `read-char'.
331 DISPLAY-FN should take CHAR and LEAF and signify that LEAFs
332 associated with CHAR will be selected if CHAR is pressed. This is
333 commonly done by adding a CHAR overlay at LEAF position.
335 CLEANUP-FN should take no arguments and remove the effects of
336 multiple DISPLAY-FN invokations."
338 (setq avy-current-path "")
340 (let ((avy--leafs nil))
343 (push (cons path leaf) avy--leafs)))
344 (dolist (x avy--leafs)
345 (funcall display-fn (car x) (cdr x))))
346 (let ((char (funcall avy-translate-char-function (read-key)))
349 (if (setq branch (assoc char tree))
350 (if (eq (car (setq tree (cdr branch))) 'leaf)
351 (throw 'done (cdr tree))
352 (setq avy-current-path
353 (concat avy-current-path (string (avy--key-to-char char)))))
354 (funcall avy-handler-function char))))))
356 (defun avy-read-de-bruijn (lst keys)
357 "Select from LST dispatching on KEYS."
358 ;; In theory, the De Bruijn sequence B(k,n) has k^n subsequences of length n
359 ;; (the path length) usable as paths, thus that's the lower bound. Due to
360 ;; partially overlapping matches, not all subsequences may be usable, so it's
361 ;; possible that the path-len must be incremented, e.g., if we're matching
362 ;; for x and a buffer contains xaxbxcx only every second subsequence is
363 ;; usable for the four matches.
364 (let* ((path-len (ceiling (log (length lst) (length keys))))
365 (alist (avy--path-alist-1 lst path-len keys)))
368 (setq alist (avy--path-alist-1 lst path-len keys)))
369 (let* ((len (length (caar alist)))
371 (setq avy-current-path "")
373 (dolist (x (reverse alist))
374 (avy--overlay-at-full (reverse (car x)) (cdr x)))
375 (let ((char (funcall avy-translate-char-function (read-key))))
376 (avy--remove-leading-chars)
380 (when (eq (caar x) char)
381 (cons (cdr (car x)) (cdr x))))
383 (setq avy-current-path
384 (concat avy-current-path (string (avy--key-to-char char))))
387 (funcall avy-handler-function char))))
391 (defun avy-window-list ()
392 "Return a list of windows depending on `avy-all-windows'."
393 (cond ((eq avy-all-windows 'all-frames)
394 (cl-mapcan #'window-list (frame-list)))
396 ((eq avy-all-windows t)
399 ((null avy-all-windows)
400 (list (selected-window)))
403 (error "Unrecognized option: %S" avy-all-windows))))
405 (defmacro avy-dowindows (flip &rest body)
406 "Depending on FLIP and `avy-all-windows' run BODY in each or selected window."
409 `(let ((avy-all-windows (if ,flip
410 (not avy-all-windows)
412 (dolist (wnd (avy-window-list))
413 (with-selected-window wnd
414 (unless (memq major-mode avy-ignored-modes)
417 (defmacro avy--with-avy-keys (command &rest body)
418 "Set `avy-keys' according to COMMAND and execute BODY."
421 `(let ((avy-keys (or (cdr (assq ',command avy-keys-alist))
423 (avy-style (or (cdr (assq ',command avy-styles-alist))
430 POS is either a position or (BEG . END)."
432 (message "zero candidates"))
434 ;; ignore exit from `avy-handler-function'
438 (let* ((window (cdr x))
439 (frame (window-frame window)))
440 (unless (equal frame (selected-frame))
441 (select-frame-set-input-focus frame))
442 (select-window window))
446 (unless (= pt (point)) (push-mark))
449 (defun avy--process (candidates overlay-fn)
450 "Select one of CANDIDATES using `avy-read'.
451 Use OVERLAY-FN to visualize the decision overlay."
453 (cl-case (length candidates)
459 (avy--make-backgrounds
461 (if (eq avy-style 'de-bruijn)
464 (avy-read (avy-tree candidates avy-keys)
466 #'avy--remove-leading-chars))))
469 (defvar avy--overlays-back nil
470 "Hold overlays for when `avy-background' is t.")
472 (defun avy--make-backgrounds (wnd-list)
473 "Create a dim background overlay for each window on WND-LIST."
475 (setq avy--overlays-back
477 (let ((ol (make-overlay
481 (overlay-put ol 'face 'avy-background-face)
482 (overlay-put ol 'window w)
488 (mapc #'delete-overlay avy--overlays-back)
489 (setq avy--overlays-back nil)
490 (avy--remove-leading-chars))
492 (defun avy--regex-candidates (regex &optional beg end pred group)
493 "Return all elements that match REGEX.
494 Each element of the list is ((BEG . END) . WND)
495 When PRED is non-nil, it's a filter for matching point positions.
496 When GROUP is non-nil, (BEG . END) should delimit that regex group."
497 (setq group (or group 0))
498 (let ((case-fold-search avy-case-fold-search)
501 (let ((we (or end (window-end (selected-window) t))))
503 (goto-char (or beg (window-start)))
504 (while (re-search-forward regex we t)
505 (unless (get-char-property (point) 'invisible)
506 (when (or (null pred)
508 (push (cons (cons (match-beginning group)
510 wnd) candidates)))))))
511 (nreverse candidates)))
513 (defvar avy--overlay-offset 0
514 "The offset to apply in `avy--overlay'.")
516 (defvar avy--overlays-lead nil
517 "Hold overlays for leading chars.")
519 (defun avy--remove-leading-chars ()
520 "Remove leading char overlays."
521 (mapc #'delete-overlay avy--overlays-lead)
522 (setq avy--overlays-lead nil))
524 (defun avy--overlay (str pt wnd)
525 "Create an overlay with STR at PT in WND."
526 (when (<= (1+ pt) (with-selected-window wnd (point-max)))
527 (let* ((pt (+ pt avy--overlay-offset))
528 (ol (make-overlay pt (1+ pt) (window-buffer wnd)))
529 (old-str (with-selected-window wnd
530 (buffer-substring pt (1+ pt)))))
532 (setq old-str (propertize
533 old-str 'face 'avy-background-face)))
534 (overlay-put ol 'window wnd)
535 (overlay-put ol 'display (concat str old-str))
536 (push ol avy--overlays-lead))))
538 (defcustom avy-highlight-first nil
539 "When non-nil highlight the first decision char with `avy-lead-face-0'.
540 Do this even when the char is terminating."
543 (defun avy--key-to-char (c)
544 "If C is no character, translate it using `avy-key-to-char-alist'."
547 (or (cdr (assoc c avy-key-to-char-alist))
548 (error "Unknown key %s" c))))
550 (defun avy--overlay-pre (path leaf)
551 "Create an overlay with PATH at LEAF.
552 PATH is a list of keys from tree root to LEAF.
553 LEAF is normally ((BEG . END) . WND)."
554 (let* ((path (mapcar #'avy--key-to-char path))
555 (str (propertize (apply #'string (reverse path))
556 'face 'avy-lead-face)))
557 (when (or avy-highlight-first (> (length str) 1))
558 (set-text-properties 0 1 '(face avy-lead-face-0) str))
560 (propertize avy-current-path
561 'face 'avy-lead-face-1)
565 (cond ((numberp leaf)
573 (selected-window)))))
575 (defun avy--overlay-at (path leaf)
576 "Create an overlay with PATH at LEAF.
577 PATH is a list of keys from tree root to LEAF.
578 LEAF is normally ((BEG . END) . WND)."
579 (let* ((path (mapcar #'avy--key-to-char path))
581 (string (car (last path)))
582 'face 'avy-lead-face))
583 (pt (+ (if (consp (car leaf))
586 avy--overlay-offset))
588 (ol (make-overlay pt (1+ pt)
589 (window-buffer wnd)))
590 (old-str (with-selected-window wnd
591 (buffer-substring pt (1+ pt)))))
593 (setq old-str (propertize
594 old-str 'face 'avy-background-face)))
595 (overlay-put ol 'window wnd)
596 (overlay-put ol 'display (if (string= old-str "\n")
599 (push ol avy--overlays-lead)))
601 (defun avy--overlay-at-full (path leaf)
602 "Create an overlay with PATH at LEAF.
603 PATH is a list of keys from tree root to LEAF.
604 LEAF is normally ((BEG . END) . WND)."
605 (let* ((path (mapcar #'avy--key-to-char path))
607 (apply #'string (reverse path))
608 'face 'avy-lead-face))
610 (beg (if (consp (car leaf))
616 (set-text-properties (- len i 1) (- len i)
617 `(face ,(nth i avy-lead-faces))
619 (when (eq avy-style 'de-bruijn)
621 (propertize avy-current-path
622 'face 'avy-lead-face-1)
624 (setq len (length str)))
625 (with-selected-window wnd
632 (and (eq (overlay-get o 'category) 'avy)
633 (eq (overlay-get o 'window) wnd)
635 (overlays-in (point) (min (+ (point) len)
636 (line-end-position))))))
637 (setq len (- (apply #'min oov) beg))
638 (setq str (substring str 0 len)))
639 (let ((other-ov (cl-find-if
641 (and (eq (overlay-get o 'category) 'avy)
642 (eq (overlay-start o) beg)
643 (not (eq (overlay-get o 'window) wnd))))
644 (overlays-in (point) (min (+ (point) len)
645 (line-end-position))))))
647 (> (overlay-end other-ov)
649 (setq str (concat str (buffer-substring
651 (overlay-end other-ov))))
652 (setq len (- (overlay-end other-ov)
654 (let* ((end (if (= beg (line-end-position))
657 (if (eq (char-after) ?\t)
660 (line-end-position))))
664 (old-str (buffer-substring beg (1+ beg))))
666 (setq old-str (propertize
667 old-str 'face 'avy-background-face)))
668 (overlay-put ol 'window wnd)
669 (overlay-put ol 'category 'avy)
670 (overlay-put ol 'display
671 (cond ((string= old-str "\n")
673 ((string= old-str "\t")
674 (concat str (make-string (- tab-width len) ?\ )))
677 (push ol avy--overlays-lead))))))
679 (defun avy--overlay-post (path leaf)
680 "Create an overlay with PATH at LEAF.
681 PATH is a list of keys from tree root to LEAF.
682 LEAF is normally ((BEG . END) . WND)."
683 (let* ((path (mapcar #'avy--key-to-char path))
684 (str (propertize (apply #'string (reverse path))
685 'face 'avy-lead-face)))
686 (when (or avy-highlight-first (> (length str) 1))
687 (set-text-properties 0 1 '(face avy-lead-face-0) str))
689 (propertize avy-current-path
690 'face 'avy-lead-face-1)
694 (cond ((numberp leaf)
702 (selected-window)))))
704 (defun avy--style-fn (style)
705 "Transform STYLE symbol to a style function."
707 (pre #'avy--overlay-pre)
708 (at #'avy--overlay-at)
709 (at-full 'avy--overlay-at-full)
710 (post #'avy--overlay-post)
711 (de-bruijn #'avy--overlay-at-full)
712 (t (error "Unexpected style %S" style))))
714 (defun avy--generic-jump (regex window-flip style)
716 When WINDOW-FLIP is non-nil, do the opposite of `avy-all-windows'.
717 STYLE determines the leading char overlay style."
718 (let ((avy-all-windows
720 (not avy-all-windows)
724 (avy--regex-candidates regex)
725 (avy--style-fn style)))))
729 (defun avy-goto-char (char &optional arg)
730 "Jump to the currently visible CHAR.
731 The window scope is determined by `avy-all-windows' (ARG negates it)."
732 (interactive (list (read-char "char: " t)
734 (avy--with-avy-keys avy-goto-char
738 (regexp-quote (string char)))
743 (defun avy-goto-char-in-line (char)
744 "Jump to the currently visible CHAR in the current line."
745 (interactive (list (read-char "char: " t)))
746 (let ((avy-all-windows nil))
747 (avy--with-avy-keys avy-goto-char
751 (narrow-to-region (line-beginning-position)
753 (avy--regex-candidates (regexp-quote (string char))))
754 (avy--style-fn avy-style))))))
757 (defun avy-goto-char-2 (char1 char2 &optional arg)
758 "Jump to the currently visible CHAR1 followed by CHAR2.
759 The window scope is determined by `avy-all-windows' (ARG negates it)."
760 (interactive (list (read-char "char 1: " t)
761 (read-char "char 2: " t)
763 (avy--with-avy-keys avy-goto-char-2
765 (regexp-quote (string char1 char2))
770 (defun avy-isearch ()
771 "Jump to one of the current isearch candidates."
773 (avy--with-avy-keys avy-isearch
775 (avy--regex-candidates isearch-string))
778 (avy--process candidates (avy--style-fn avy-style))))
780 (avy--goto candidate))))
783 (defun avy-goto-word-0 (arg)
784 "Jump to a word start.
785 The window scope is determined by `avy-all-windows' (ARG negates it)."
787 (avy--with-avy-keys avy-goto-word-0
788 (avy--generic-jump "\\b\\sw" arg avy-style)))
791 (defun avy-goto-word-1 (char &optional arg)
792 "Jump to the currently visible CHAR at a word start.
793 The window scope is determined by `avy-all-windows' (ARG negates it)."
794 (interactive (list (read-char "char: " t)
796 (avy--with-avy-keys avy-goto-word-1
797 (let* ((str (string char))
798 (regex (cond ((string= str ".")
800 ((and avy-word-punc-regexp
801 (string-match avy-word-punc-regexp str))
807 (avy--generic-jump regex arg avy-style))))
809 (declare-function subword-backward "subword")
812 (defun avy-goto-subword-0 (&optional arg predicate)
813 "Jump to a word or subword start.
815 The window scope is determined by `avy-all-windows' (ARG negates it).
817 When PREDICATE is non-nil it's a function of zero parameters that
821 (avy--with-avy-keys avy-goto-subword-0
822 (let ((case-fold-search nil)
825 (let ((ws (window-start))
828 (goto-char (window-end (selected-window) t))
830 (while (> (point) ws)
831 (when (or (null predicate)
832 (and predicate (funcall predicate)))
833 (push (cons (point) (selected-window)) window-cands))
835 (setq candidates (nconc candidates window-cands))))
837 (avy--process candidates (avy--style-fn avy-style))))))
840 (defun avy-goto-subword-1 (char arg)
841 "Jump to the currently visible CHAR at a subword start.
842 The window scope is determined by `avy-all-windows' (ARG negates it).
843 The case of CHAR is ignored."
844 (interactive (list (read-char "char: " t)
846 (avy--with-avy-keys avy-goto-subword-1
847 (let ((char (downcase char)))
849 arg (lambda () (eq (downcase (char-after)) char))))))
851 (defun avy-goto-word-or-subword-1 ()
852 "Forward to `avy-goto-subword-1' or `avy-goto-word-1'.
853 Which one depends on variable `subword-mode'."
855 (if (bound-and-true-p subword-mode)
856 (call-interactively #'avy-goto-subword-1)
857 (call-interactively #'avy-goto-word-1)))
859 (defun avy--line (&optional arg)
861 The window scope is determined by `avy-all-windows' (ARG negates it)."
862 (let ((avy-background nil)
865 (let ((ws (window-start)))
868 (narrow-to-region ws (window-end (selected-window) t))
869 (goto-char (point-min))
870 (while (< (point) (point-max))
871 (unless (get-char-property
872 (max (1- (point)) ws) 'invisible)
874 (if (eq avy-style 'post)
876 (line-beginning-position))
877 (selected-window)) candidates))
878 (forward-line 1))))))
879 (avy--process (nreverse candidates) (avy--style-fn avy-style))))
882 (defun avy-goto-line (&optional arg)
883 "Jump to a line start in current buffer.
884 The window scope is determined by `avy-all-windows' (ARG negates it)."
886 (avy--with-avy-keys avy-goto-line
887 (let ((avy-handler-function
891 (avy-handler-default char)
892 (let ((line (read-from-minibuffer
893 "Goto line: " (string char))))
896 (goto-char (point-min))
897 (forward-line (1- (string-to-number line)))
898 (throw 'done 'exit)))))))
899 (avy--goto (avy--line arg)))))
902 (defun avy-copy-line (arg)
903 "Copy a selected line above the current line.
904 ARG lines can be used."
906 (avy--with-avy-keys avy-copy-line
907 (let ((start (car (avy--line))))
908 (move-beginning-of-line nil)
911 (buffer-substring-no-properties
915 (move-end-of-line arg)
920 (defun avy-move-line (arg)
921 "Move a selected line above the current line.
922 ARG lines can be used."
924 (avy--with-avy-keys avy-move-line
925 (let ((start (car (avy--line))))
926 (move-beginning-of-line nil)
930 (kill-whole-line arg))
932 (current-kill 0))))))
935 (defun avy-copy-region ()
936 "Select two lines and copy the text between them here."
938 (avy--with-avy-keys avy-copy-region
939 (let ((beg (car (avy--line)))
940 (end (car (avy--line)))
941 (pad (if (bolp) "" "\n")))
942 (move-beginning-of-line nil)
945 (buffer-substring-no-properties
949 (line-end-position)))
953 (defun avy-setup-default ()
954 "Setup the default shortcuts."
955 (eval-after-load "isearch"
956 '(define-key isearch-mode-map (kbd "C-'") 'avy-isearch)))
958 (defcustom avy-timeout-seconds 0.5
959 "How many seconds to wait for the second char.")
962 (defun avy-goto-char-timer (&optional arg)
963 "Read one or two consecutive chars and jump to the first one.
964 The window scope is determined by `avy-all-windows' (ARG negates it)."
966 (let ((c1 (read-char "char 1: " t))
967 (c2 (read-char "char 2: " t avy-timeout-seconds)))
968 (avy--with-avy-keys avy-goto-char-timer
977 (define-obsolete-variable-alias
978 'avy-goto-char-style 'avy-style "0.1.0"
979 "Use `avy-style' and `avy-styles-alist' instead.")
980 (define-obsolete-variable-alias
981 'avy-goto-word-style 'avy-style "0.1.0"
982 "Use `avy-style' and `avy-styles-alist' instead.")
983 (define-obsolete-variable-alias 'avi-keys 'avy-keys "0.1.0")
984 (define-obsolete-variable-alias 'avi-background 'avy-background "0.1.0")
985 (define-obsolete-variable-alias 'avi-word-punc-regexp 'avy-word-punc-regexp "0.1.0")
986 (define-obsolete-face-alias 'avi-lead-face 'avy-lead-face "0.1.0")
987 (define-obsolete-function-alias 'avi--goto 'avy--goto "0.1.0")
988 (define-obsolete-function-alias 'avi--process 'avy--process "0.1.0")
989 (define-obsolete-variable-alias 'avi-all-windows 'avy-all-windows "0.1.0")
990 (define-obsolete-function-alias 'avi--overlay-pre 'avy--overlay-pre "0.1.0")
991 (define-obsolete-function-alias 'avi--overlay-at 'avy--overlay-at "0.1.0")
992 (define-obsolete-function-alias 'avi--overlay-post 'avy--overlay-post "0.1.0")
993 (define-obsolete-function-alias 'avi-goto-char 'avy-goto-char "0.1.0")
994 (define-obsolete-function-alias 'avi-goto-char-2 'avy-goto-char-2 "0.1.0")
995 (define-obsolete-function-alias 'avi-isearch 'avy-isearch "0.1.0")
996 (define-obsolete-function-alias 'avi-goto-word-0 'avy-goto-word-0 "0.1.0")
997 (define-obsolete-function-alias 'avi-goto-subword-0 'avy-goto-subword-0 "0.1.0")
998 (define-obsolete-function-alias 'avi-goto-word-1 'avy-goto-word-1 "0.1.0")
999 (define-obsolete-function-alias 'avi-goto-line 'avy-goto-line "0.1.0")
1000 (define-obsolete-function-alias 'avi-copy-line 'avy-copy-line "0.1.0")
1001 (define-obsolete-function-alias 'avi-move-line 'avy-move-line "0.1.0")
1002 (define-obsolete-function-alias 'avi-copy-region 'avy-copy-region "0.1.0")
1003 (define-obsolete-function-alias 'avi--regex-candidates 'avy--regex-candidates "0.1.0")
1007 ;;; avy.el ends here