]> code.delx.au - gnu-emacs-elpa/blob - swiper.el
Account for zero-length regex matches
[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 (and (ignore-errors (re-search-forward re end t))
246 (> (- (match-end 0) (match-beginning 0)) 0))
247 (let ((i 0))
248 (while (<= i ivy--subexps)
249 (when (match-beginning i)
250 (let ((overlay (make-overlay (match-beginning i)
251 (match-end i)))
252 (face
253 (cond ((zerop ivy--subexps)
254 (cl-caddr swiper-faces))
255 ((zerop i)
256 (car swiper-faces))
257 (t
258 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
259 swiper-faces)))))
260 (push overlay swiper--overlays)
261 (overlay-put overlay 'face face)
262 (overlay-put overlay 'window swiper--window)
263 (overlay-put overlay 'priority i)))
264 (cl-incf i)))))))
265
266 (defun swiper--binary (beg end)
267 "Find anchor between BEG and END."
268 (if (<= (- end beg) 10)
269 (let ((min 1000)
270 n
271 ln
272 d)
273 (goto-char (point-min))
274 (forward-line (1- beg))
275 (while (< beg end)
276 (beginning-of-line)
277 (setq n (read (current-buffer)))
278 (when (< (setq d (abs (- n swiper--anchor))) min)
279 (setq min d)
280 (setq ln beg))
281 (cl-incf beg)
282 (forward-line 1))
283 (goto-char (point-min))
284 (when ln
285 (forward-line (1- ln))))
286 (let ((mid (+ beg (/ (- end beg) 2))))
287 (goto-char (point-min))
288 (forward-line mid)
289 (beginning-of-line)
290 (let ((n (read (current-buffer))))
291 (if (> n swiper--anchor)
292 (swiper--binary beg mid)
293 (swiper--binary mid end))))))
294
295 (defun swiper--update-sel ()
296 "Update selection."
297 (let* ((re (ivy--regex helm-input))
298 (str (buffer-substring-no-properties
299 (line-beginning-position)
300 (line-end-position)))
301 (num (if (string-match "^[0-9]+" str)
302 (string-to-number (match-string 0 str))
303 0))
304 pt)
305 (when (> (length re) 0)
306 (with-selected-window swiper--window
307 (goto-char (point-min))
308 (forward-line (1- num))
309 (when (re-search-forward re (point-max) t)
310 (setq pt (match-beginning 0))))
311 (when pt
312 (with-selected-window
313 (helm-persistent-action-display-window)
314 (goto-char pt)
315 (recenter)
316 (swiper--update-input-helm))))
317 (with-selected-window swiper--window
318 (let ((ov (make-overlay
319 (line-beginning-position)
320 (1+ (line-end-position)))))
321 (overlay-put ov 'face 'swiper-line-face)
322 (push ov swiper--overlays)))))
323
324 (defun swiper--reanchor ()
325 "Move to a valid match closest to `swiper--anchor'."
326 (with-helm-window
327 (goto-char (point-min))
328 (if (re-search-forward (format "^%d " swiper--anchor) nil t)
329 nil
330 (forward-line 1)
331 (swiper--binary 2 (1+ (count-lines (point) (point-max)))))
332 (when (> (count-lines (point-min) (point-max)) 1)
333 (forward-line -1)
334 (helm-next-line 1))))
335
336 (defun swiper--action (x)
337 "Goto line X."
338 (goto-char (point-min))
339 (forward-line (1- (read x)))
340 (re-search-forward
341 (ivy--regex helm-input) (line-end-position) t))
342
343 (provide 'swiper)
344
345 ;;; swiper.el ends here