1 ;;; swiper.el --- Isearch with an overview. Oh, man! -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Oleh Krehel
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((emacs "24.1") (ivy "0.1.0"))
11 ;; This file is not 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 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.
32 ;; The overview back end can be either `helm' or `ivy'.
34 ;; It can double as a quick `regex-builder', although only single
35 ;; lines will be matched.
41 "`isearch' with an overview."
45 (defcustom swiper-completion-method 'helm
46 "Method to select a candidate from a list of strings."
48 (const :tag "Helm" helm)
49 (const :tag "Ivy" ivy)))
51 (defface swiper-match-face-1
52 '((t (:background "#FEEA89")))
53 "Face for `swiper' matches.")
55 (defface swiper-match-face-2
56 '((t (:background "#F9A35A")))
57 "Face for `swiper' matches.")
59 (defface swiper-match-face-3
60 '((t (:background "#fb7905")))
61 "Face for `swiper' matches.")
63 (defface swiper-match-face-4
64 '((t (:background "#F15C79")))
65 "Face for `swiper' matches.")
67 (defface swiper-line-face
68 '((t (:background "#f3d3d3")))
69 "Face for current `swiper' line.")
71 (defcustom swiper-faces '(swiper-match-face-1
75 "List of `swiper' faces for group matches.")
77 (defvar swiper--window nil
78 "Store the current window.")
80 (defalias 'swiper-font-lock-ensure
81 (if (fboundp 'font-lock-ensure)
83 'font-lock-fontify-buffer))
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))
90 (fspec (format "%%-%dd %%s" line-width))
94 (goto-char (point-min))
95 (swiper-font-lock-ensure)
96 (while (< (point) (point-max))
100 (line-beginning-position)
101 (line-end-position)))
103 (zerop (forward-line 1)))
104 (nreverse candidates))))
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)
111 "Allows you to go to next and previous hit isearch-style.")
115 "`isearch' with an overview."
117 (if (and (eq 'swiper-completion-method 'helm)
122 (defun swiper--init ()
123 "Perform initialization common to both completion methods."
126 (setq swiper--anchor (line-number-at-pos))
127 (setq swiper--window (selected-window)))
129 (defun swiper--ivy ()
130 "`isearch' with an overview using `ivy'."
135 (let ((res (ivy-read "pattern: "
137 #'swiper--update-input-ivy)))
138 (goto-char (point-min))
139 (forward-line (1- (read res)))
141 (ivy--regex ivy-text)
147 (defun swiper--helm ()
148 "`isearch' with an overview using `helm'."
153 (let ((helm-display-function
156 (split-window-vertically))
158 (switch-to-buffer buf)))
159 helm-candidate-number-limit)
161 `((name . ,(buffer-name))
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)
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
180 (format "^%d " swiper--anchor)
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)
188 (defun swiper--cleanup ()
189 "Clean up the overlays."
190 (while swiper--overlays
191 (delete-overlay (pop swiper--overlays))))
193 (defvar swiper--overlays nil
196 (defvar swiper--anchor nil
197 "A line number to which the search should be anchored.")
199 (defvar swiper--len 0
200 "The last length of input for which an anchoring was made.")
202 (defun swiper--update-input-helm ()
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))
214 (defun swiper--update-input-ivy ()
215 "Called when `ivy' input is updated."
217 (let* ((re (ivy--regex ivy-text))
219 (num (if (string-match "^[0-9]+" str)
220 (string-to-number (match-string 0 str))
222 (with-selected-window swiper--window
223 (goto-char (point-min))
225 (goto-char (point-min))
226 (forward-line (1- num))
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
236 (window-start swiper--window)
237 (window-end swiper--window t)))))
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)
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))
248 (while (<= i ivy--subexps)
249 (when (match-beginning i)
250 (let ((overlay (make-overlay (match-beginning i)
253 (cond ((zerop ivy--subexps)
254 (cl-caddr swiper-faces))
258 (nth (1+ (mod (1- i) (1- (length 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)))
266 (defun swiper--binary (beg end)
267 "Find anchor between BEG and END."
268 (if (<= (- end beg) 10)
273 (goto-char (point-min))
274 (forward-line (1- beg))
277 (setq n (read (current-buffer)))
278 (when (< (setq d (abs (- n swiper--anchor))) min)
283 (goto-char (point-min))
285 (forward-line (1- ln))))
286 (let ((mid (+ beg (/ (- end beg) 2))))
287 (goto-char (point-min))
290 (let ((n (read (current-buffer))))
291 (if (> n swiper--anchor)
292 (swiper--binary beg mid)
293 (swiper--binary mid end))))))
295 (defun swiper--update-sel ()
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))
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))))
312 (with-selected-window
313 (helm-persistent-action-display-window)
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)))))
324 (defun swiper--reanchor ()
325 "Move to a valid match closest to `swiper--anchor'."
327 (goto-char (point-min))
328 (if (re-search-forward (format "^%d " swiper--anchor) nil t)
331 (swiper--binary 2 (1+ (count-lines (point) (point-max)))))
332 (when (> (count-lines (point-min) (point-max)) 1)
334 (helm-next-line 1))))
336 (defun swiper--action (x)
338 (goto-char (point-min))
339 (forward-line (1- (read x)))
341 (ivy--regex helm-input) (line-end-position) t))
345 ;;; swiper.el ends here