1 ;;; avy-jump.el --- jump to things tree-style
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
7 ;; This file is 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 pred)
79 "Return all elements that match REGEX in WND.
80 Each element of the list is ((BEG . END) . WND)
81 When PRED is non-nil, it's a filter for matching point positions."
82 (setq wnd (or wnd (selected-window)))
83 (let ((we (or end (window-end (selected-window) t)))
85 (save-window-excursion
88 (goto-char (or beg (window-start)))
89 (while (re-search-forward regex we t)
92 (push (cons (cons (match-beginning 0)
95 (nreverse candidates))))
97 (defvar avi--overlay-offset 0
98 "The offset to apply in `avi--overlay'.")
100 (defun avi--overlay (str pt wnd)
101 "Create an overlay with STR at PT in WND."
102 (let* ((pt (+ pt avi--overlay-offset))
103 (ol (make-overlay pt (1+ pt) (window-buffer wnd)))
104 (old-str (with-selected-window wnd
105 (buffer-substring pt (1+ pt)))))
107 (setq old-str (propertize
108 old-str 'face 'aw-background-face)))
109 (overlay-put ol 'window wnd)
110 (overlay-put ol 'display (concat str old-str))
111 (push ol aw-overlays-lead)))
113 (defun avi--overlay-pre (path leaf)
114 "Create an overlay with STR at LEAF.
115 PATH is a list of keys from tree root to LEAF.
116 LEAF is ((BEG . END) . WND)."
118 (propertize (apply #'string (reverse path))
119 'face 'avi-lead-face)
120 (if (consp (car leaf))
125 (defun avi--overlay-at (path leaf)
126 "Create an overlay with STR at LEAF.
127 PATH is a list of keys from tree root to LEAF.
128 LEAF is ((BEG . END) . WND)."
129 (let ((str (propertize
130 (string (car (last path)))
131 'face 'avi-lead-face))
132 (pt (if (consp (car leaf))
136 (let ((ol (make-overlay pt (1+ pt)
137 (window-buffer wnd)))
138 (old-str (with-selected-window wnd
139 (buffer-substring pt (1+ pt)))))
141 (setq old-str (propertize
142 old-str 'face 'aw-background-face)))
143 (overlay-put ol 'window wnd)
144 (overlay-put ol 'display str)
145 (push ol aw-overlays-lead))))
147 (defun avi--overlay-post (path leaf)
148 "Create an overlay with STR at LEAF.
149 PATH is a list of keys from tree root to LEAF.
150 LEAF is ((BEG . END) . WND)."
152 (propertize (apply #'string (reverse path))
153 'face 'avi-lead-face)
154 (if (consp (car leaf))
161 (defun avi-goto-char ()
162 "Read one char and jump to it in current window."
166 (avi--regex-candidates
167 (string (read-char "char: "))
169 #'avi--overlay-post)))
172 (defun avi-goto-char-2 ()
173 "Read two chars and jump to them in current window."
177 (avi--regex-candidates
179 (read-char "char 1: ")
180 (read-char "char 2: "))
182 #'avi--overlay-post)))
185 (defun avi-isearch ()
186 "Jump to one of the current isearch candidates."
189 (avi--regex-candidates isearch-string))
192 (avi--process candidates #'avi--overlay-post)))
194 (avi--goto candidate)))
197 (defun avi-goto-word-0 ()
198 "Jump to a word start in current window."
200 (let* ((avi-keys (number-sequence ?a ?z))
201 (candidates (avi--regex-candidates "\\b\\sw")))
203 (avi--process candidates #'avi--overlay-pre))))
206 (defun avi-goto-word-1 ()
207 "Jump to a word start in current window.
208 Read one char with which the word should start."
210 (let ((candidates (avi--regex-candidates
213 (string (read-char "char: "))))))
215 (avi--process candidates #'avi--overlay-pre))))
218 "Select line in current window."
219 (let ((avi-background nil)
223 (narrow-to-region (window-start) (window-end (selected-window) t))
224 (goto-char (point-min))
225 (while (< (point) (point-max))
226 (push (cons (point) (selected-window))
229 (avi--process (nreverse candidates) #'avi--overlay-pre)))
232 (defun avi-goto-line ()
233 "Jump to a line start in current buffer."
235 (avi--goto (avi--line)))
238 (defun avi-copy-line (arg)
239 "Copy a selected line above the current line.
240 ARG lines can be used."
242 (let ((start (car (avi--line))))
243 (move-beginning-of-line nil)
246 (buffer-substring-no-properties
250 (move-end-of-line arg)
255 (defun avi-move-line (arg)
256 "Move a selected line above the current line.
257 ARG lines can be used."
259 (let ((start (car (avi--line))))
260 (move-beginning-of-line nil)
264 (move-end-of-line arg)
265 (kill-region start (point)))
270 (defun avi-copy-region ()
271 "Select two lines and copy the text between them here."
273 (let ((beg (car (avi--line)))
274 (end (car (avi--line)))
275 (pad (if (bolp) "" "\n")))
276 (move-beginning-of-line nil)
279 (buffer-substring-no-properties
283 (line-end-position)))
288 ;;; avy-jump.el ends here