]> code.delx.au - gnu-emacs-elpa/blob - swiper.el
e88d658be877b2a091cf98cd30f5008b5c7b8449
[gnu-emacs-elpa] / swiper.el
1 ;;; swiper.el --- Isearch with an overview. Oh, man! -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Oleh Krehel
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
7 ;; Version: 0.1.0
8 ;; Package-Requires: ((emacs "24.1") (ivy "0.1.0"))
9 ;; Keywords: matching
10
11 ;; This file is not part of GNU Emacs
12
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)
16 ;; any later version.
17
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.
22
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
28 ;; This package gives an overview of the current regex search
29 ;; candidates. The search regex can be split into groups with a
30 ;; space. Each group is highlighted with a different face.
31 ;;
32 ;; The overview back end can be either `helm' or `ivy'.
33 ;;
34 ;; It can double as a quick `regex-builder', although only single
35 ;; lines will be matched.
36
37 ;;; Code:
38 (require 'ivy)
39
40 (defgroup swiper nil
41 "`isearch' with an overview."
42 :group 'matching
43 :prefix "swiper-")
44
45 (defcustom swiper-completion-method 'helm
46 "Method to select a candidate from a list of strings."
47 :type '(choice
48 (const :tag "Helm" helm)
49 (const :tag "Ivy" ivy)))
50
51 (defface swiper-match-face-1
52 '((t (:background "#FEEA89")))
53 "Face for `swiper' matches.")
54
55 (defface swiper-match-face-2
56 '((t (:background "#F9A35A")))
57 "Face for `swiper' matches.")
58
59 (defface swiper-match-face-3
60 '((t (:background "#fb7905")))
61 "Face for `swiper' matches.")
62
63 (defface swiper-match-face-4
64 '((t (:background "#F15C79")))
65 "Face for `swiper' matches.")
66
67 (defface swiper-line-face
68 '((t (:background "#f3d3d3")))
69 "Face for current `swiper' line.")
70
71 (defcustom swiper-faces '(swiper-match-face-1
72 swiper-match-face-2
73 swiper-match-face-3
74 swiper-match-face-4)
75 "List of `swiper' faces for group matches.")
76
77 (defvar swiper--window nil
78 "Store the current window.")
79
80 (defalias 'swiper-font-lock-ensure
81 (if (fboundp 'font-lock-ensure)
82 'font-lock-ensure
83 'font-lock-fontify-buffer))
84
85 (defun swiper--candidates ()
86 "Return a list of this buffer lines."
87 (let* ((line-width (1+ (floor (log (count-lines
88 (point-min) (point-max))
89 10))))
90 (fspec (format "%%-%dd %%s" line-width))
91 (line-number 0)
92 candidates)
93 (save-excursion
94 (goto-char (point-min))
95 (swiper-font-lock-ensure)
96 (while (< (point) (point-max))
97 (push (format fspec
98 (cl-incf line-number)
99 (buffer-substring
100 (line-beginning-position)
101 (line-end-position)))
102 candidates)
103 (zerop (forward-line 1)))
104 (nreverse candidates))))
105
106 (defvar swiper-helm-keymap
107 (let ((map (make-sparse-keymap)))
108 (define-key map (kbd "C-s") 'helm-next-line)
109 (define-key map (kbd "C-r") 'helm-previous-line)
110 map)
111 "Allows you to go to next and previous hit isearch-style.")
112
113 ;;;###autoload
114 (defun swiper ()
115 "`isearch' with an overview."
116 (interactive)
117 (if (and (eq 'swiper-completion-method 'helm)
118 (featurep 'helm))
119 (swiper--helm)
120 (swiper--ivy)))
121
122 (defun swiper--init ()
123 "Perform initialization common to both completion methods."
124 (deactivate-mark)
125 (setq swiper--len 0)
126 (setq swiper--anchor (line-number-at-pos))
127 (setq swiper--window (selected-window)))
128
129 (defun swiper--ivy ()
130 "`isearch' with an overview using `ivy'."
131 (interactive)
132 (ido-mode -1)
133 (swiper--init)
134 (unwind-protect
135 (let ((res (ivy-read "pattern: "
136 (swiper--candidates)
137 #'swiper--update-input-ivy)))
138 (goto-char (point-min))
139 (forward-line (1- (read res)))
140 (re-search-forward
141 (ivy--regex ivy-text)
142 (line-end-position)
143 t))
144 (ido-mode 1)
145 (swiper--cleanup)))
146
147 (defun swiper--helm ()
148 "`isearch' with an overview using `helm'."
149 (interactive)
150 (require 'helm)
151 (swiper--init)
152 (unwind-protect
153 (let ((helm-display-function
154 (lambda (buf)
155 (when (one-window-p)
156 (split-window-vertically))
157 (other-window 1)
158 (switch-to-buffer buf)))
159 helm-candidate-number-limit)
160 (helm :sources
161 `((name . ,(buffer-name))
162 (init . (lambda ()
163 (add-hook 'helm-move-selection-after-hook
164 #'swiper--update-sel)
165 (add-hook 'helm-update-hook
166 #'swiper--update-input-helm)
167 (add-hook 'helm-after-update-hook
168 #'swiper--reanchor)))
169 (match-strict . (lambda (x)
170 (ignore-errors
171 (string-match (ivy--regex helm-input) x))))
172 (candidates . ,(swiper--candidates))
173 (filtered-candidate-transformer
174 helm-fuzzy-highlight-matches)
175 (action . swiper--action))
176 :keymap (make-composed-keymap
177 swiper-helm-keymap
178 helm-map)
179 :preselect
180 (format "^%d " swiper--anchor)
181 :buffer "*swiper*"))
182 ;; cleanup
183 (remove-hook 'helm-move-selection-after-hook #'swiper--update-sel)
184 (remove-hook 'helm-update-hook #'swiper--update-input-helm)
185 (remove-hook 'helm-after-update-hook #'swiper--reanchor)
186 (swiper--cleanup)))
187
188 (defun swiper--cleanup ()
189 "Clean up the overlays."
190 (while swiper--overlays
191 (delete-overlay (pop swiper--overlays))))
192
193 (defvar swiper--overlays nil
194 "Store overlays.")
195
196 (defvar swiper--anchor nil
197 "A line number to which the search should be anchored.")
198
199 (defvar swiper--len 0
200 "The last length of input for which an anchoring was made.")
201
202 (defun swiper--update-input-helm ()
203 "Update selection."
204 (swiper--cleanup)
205 (with-selected-window swiper--window
206 (swiper--add-overlays
207 (ivy--regex helm-input)
208 (window-start swiper--window)
209 (window-end swiper--window t)))
210 (when (/= (length helm-input) swiper--len)
211 (setq swiper--len (length helm-input))
212 (swiper--reanchor)))
213
214 (defun swiper--update-input-ivy ()
215 "Called when `ivy' input is updated."
216 (swiper--cleanup)
217 (let* ((re (ivy--regex ivy-text))
218 (str ivy--current)
219 (num (if (string-match "^[0-9]+" str)
220 (string-to-number (match-string 0 str))
221 0)))
222 (with-selected-window swiper--window
223 (goto-char (point-min))
224 (when (plusp num)
225 (goto-char (point-min))
226 (forward-line (1- num))
227 (recenter))
228 (let ((ov (make-overlay
229 (line-beginning-position)
230 (1+ (line-end-position)))))
231 (overlay-put ov 'face 'swiper-line-face)
232 (overlay-put ov 'window swiper--window)
233 (push ov swiper--overlays))
234 (swiper--add-overlays
235 re
236 (window-start swiper--window)
237 (window-end swiper--window t)))))
238
239 (defun swiper--add-overlays (re beg end)
240 "Add overlays for RE regexp in current buffer between BEG and END."
241 (when (> (length re) 1)
242 (save-excursion
243 (goto-char beg)
244 ;; RE can become an invalid regexp
245 (while (ignore-errors (re-search-forward re end t))
246 (let ((i 0))
247 (while (<= i ivy--subexps)
248 (when (match-beginning i)
249 (let ((overlay (make-overlay (match-beginning i)
250 (match-end i)))
251 (face
252 (cond ((zerop ivy--subexps)
253 (cl-caddr swiper-faces))
254 ((zerop i)
255 (car swiper-faces))
256 (t
257 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
258 swiper-faces)))))
259 (push overlay swiper--overlays)
260 (overlay-put overlay 'face face)
261 (overlay-put overlay 'window swiper--window)
262 (overlay-put overlay 'priority i)))
263 (cl-incf i)))))))
264
265 (defun swiper--binary (beg end)
266 "Find anchor between BEG and END."
267 (if (<= (- end beg) 10)
268 (let ((min 1000)
269 n
270 ln
271 d)
272 (goto-char (point-min))
273 (forward-line (1- beg))
274 (while (< beg end)
275 (beginning-of-line)
276 (setq n (read (current-buffer)))
277 (when (< (setq d (abs (- n swiper--anchor))) min)
278 (setq min d)
279 (setq ln beg))
280 (cl-incf beg)
281 (forward-line 1))
282 (goto-char (point-min))
283 (when ln
284 (forward-line (1- ln))))
285 (let ((mid (+ beg (/ (- end beg) 2))))
286 (goto-char (point-min))
287 (forward-line mid)
288 (beginning-of-line)
289 (let ((n (read (current-buffer))))
290 (if (> n swiper--anchor)
291 (swiper--binary beg mid)
292 (swiper--binary mid end))))))
293
294 (defun swiper--update-sel ()
295 "Update selection."
296 (let* ((re (ivy--regex helm-input))
297 (str (buffer-substring-no-properties
298 (line-beginning-position)
299 (line-end-position)))
300 (num (if (string-match "^[0-9]+" str)
301 (string-to-number (match-string 0 str))
302 0))
303 pt)
304 (when (> (length re) 0)
305 (with-selected-window swiper--window
306 (goto-char (point-min))
307 (forward-line (1- num))
308 (when (re-search-forward re (point-max) t)
309 (setq pt (match-beginning 0))))
310 (when pt
311 (with-selected-window
312 (helm-persistent-action-display-window)
313 (goto-char pt)
314 (recenter)
315 (swiper--update-input-helm))))
316 (with-selected-window swiper--window
317 (let ((ov (make-overlay
318 (line-beginning-position)
319 (1+ (line-end-position)))))
320 (overlay-put ov 'face 'swiper-line-face)
321 (push ov swiper--overlays)))))
322
323 (defun swiper--reanchor ()
324 "Move to a valid match closest to `swiper--anchor'."
325 (with-helm-window
326 (goto-char (point-min))
327 (if (re-search-forward (format "^%d " swiper--anchor) nil t)
328 nil
329 (forward-line 1)
330 (swiper--binary 2 (1+ (count-lines (point) (point-max)))))
331 (when (> (count-lines (point-min) (point-max)) 1)
332 (forward-line -1)
333 (helm-next-line 1))))
334
335 (defun swiper--action (x)
336 "Goto line X."
337 (goto-char (point-min))
338 (forward-line (1- (read x)))
339 (re-search-forward
340 (ivy--regex helm-input) (line-end-position) t))
341
342 (provide 'swiper)
343
344 ;;; swiper.el ends here