1 ;;; avy-jump.el --- jump to things tree-style
3 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
7 ;; This file is not part of GNU Emacs
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; For a full copy of the GNU General Public License
20 ;; see <http://www.gnu.org/licenses/>.
24 ;; This package offers various commands for navigating to things using `avy'.
25 ;; They are in the "Commands" outline.
33 (defgroup avy-jump nil
34 "Jump to things tree-style."
38 (defcustom avi-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
41 (defcustom avi-background nil
42 "When non-nil, a gray background will be added during the selection."
45 (defface avi-lead-face
46 '((t (:foreground "white" :background "#e52b50")))
47 "Face used for the leading chars.")
53 POS is either a position or (BEG . END)."
55 (message "zero candidates")
56 (select-window (cdr x))
62 (defun avi--process (candidates overlay-fn)
63 "Select one of CANDIDATES using `avy-read'."
65 (let ((aw-background avi-background))
66 (cl-case (length candidates)
72 (aw--make-backgrounds (list (selected-window)))
73 (avy-read (avy-tree candidates avi-keys)
75 #'aw--remove-leading-chars))))
78 (defun avi--regex-candidates (regex &optional wnd beg end)
79 "Return all elements that match REGEX in WND.
80 Each element of the list is ((BEG . END) . WND)."
81 (setq wnd (or wnd (selected-window)))
82 (let ((we (or end (window-end (selected-window) t)))
84 (save-window-excursion
87 (goto-char (or beg (window-start)))
88 (while (re-search-forward regex we t)
89 (push (cons (cons (match-beginning 0)
92 (nreverse candidates))))
94 (defun avi--overlay (str pt wnd)
95 "Create an overlay with STR at PT in WND."
96 (let ((ol (make-overlay pt (1+ pt) (window-buffer wnd)))
97 (old-str (with-selected-window wnd
98 (buffer-substring pt (1+ pt)))))
100 (setq old-str (propertize
101 old-str 'face 'aw-background-face)))
102 (overlay-put ol 'window wnd)
103 (overlay-put ol 'display (concat str old-str))
104 (push ol aw-overlays-lead)))
106 (defun avi--overlay-pre (path leaf)
107 "Create an overlay with STR at LEAF.
108 PATH is a list of keys from tree root to LEAF.
109 LEAF is ((BEG . END) . WND)."
111 (propertize (apply #'string (reverse path))
112 'face 'avi-lead-face)
113 (if (consp (car leaf))
118 (defun avi--overlay-post (path leaf)
119 "Create an overlay with STR at LEAF.
120 PATH is a list of keys from tree root to LEAF.
121 LEAF is ((BEG . END) . WND)."
123 (propertize (apply #'string (reverse path))
124 'face 'avi-lead-face)
125 (if (consp (car leaf))
132 (defun avi-goto-char ()
133 "Read one char and jump to it in current window."
137 (avi--regex-candidates
138 (string (read-char "char: "))
140 #'avi--overlay-post)))
143 (defun avi-goto-char-2 ()
144 "Read two chars and jump to them in current window."
148 (avi--regex-candidates
150 (read-char "char 1: ")
151 (read-char "char 2: "))
153 #'avi--overlay-post)))
156 (defun avi-isearch ()
157 "Jump to one of the current isearch candidates."
160 (avi--regex-candidates isearch-string))
163 (avi--process candidates #'avi--overlay-post)))
165 (avi--goto candidate)))
168 (defun avi-goto-word-0 ()
169 "Jump to a word start in current window."
171 (let* ((avi-keys (number-sequence ?a ?z))
172 (candidates (avi--regex-candidates "\\b\\sw")))
174 (avi--process candidates #'avi--overlay-pre))))
177 (defun avi-goto-word-1 ()
178 "Jump to a word start in current window.
179 Read one char with which the word should start."
181 (let ((candidates (avi--regex-candidates
184 (string (read-char "char: "))))))
186 (avi--process candidates #'avi--overlay-pre))))
189 "Select line in current window."
190 (let ((avi-background nil)
194 (narrow-to-region (window-start) (window-end (selected-window) t))
195 (goto-char (point-min))
196 (while (< (point) (point-max))
197 (push (cons (point) (selected-window))
200 (avi--process (nreverse candidates) #'avi--overlay-pre)))
203 (defun avi-goto-line ()
204 "Jump to a line start in current buffer."
206 (avi--goto (avi--line)))
209 (defun avi-copy-line (arg)
210 "Copy a selected line above the current line.
211 ARG lines can be used."
213 (let ((start (car (avi--line))))
214 (move-beginning-of-line nil)
217 (buffer-substring-no-properties
221 (move-end-of-line arg)
226 (defun avi-move-line (arg)
227 "Move a selected line above the current line.
228 ARG lines can be used."
230 (let ((start (car (avi--line))))
231 (move-beginning-of-line nil)
235 (move-end-of-line arg)
236 (kill-region start (point)))
241 (defun avi-copy-region ()
242 "Select two lines and copy the text between them here."
244 (let ((beg (car (avi--line)))
245 (end (car (avi--line)))
246 (pad (if (bolp) "" "\n")))
247 (move-beginning-of-line nil)
250 (buffer-substring-no-properties
254 (line-end-position)))
259 ;;; avy-jump.el ends here