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 'ivy
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 (:inherit isearch-lazy-highlight-face)))
53 "Face for `swiper' matches.")
55 (defface swiper-match-face-2
56 '((t (:inherit isearch)))
57 "Face for `swiper' matches.")
59 (defface swiper-match-face-3
60 '((t (:inherit match)))
61 "Face for `swiper' matches.")
63 (defface swiper-match-face-4
64 '((t (:inherit isearch)))
65 "Face for `swiper' matches.")
67 (defface swiper-line-face
68 '((t (:inherit highlight)))
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 (defun swiper-font-lock-ensure ()
81 "Ensure the entired buffer is highlighted."
82 (unless (derived-mode-p 'magit-mode)
83 (if (fboundp 'font-lock-ensure)
85 (font-lock-fontify-buffer))))
87 (defun swiper--candidates ()
88 "Return a list of this buffer lines."
89 (let ((n-lines (count-lines (point-min) (point-max))))
90 (unless (zerop n-lines)
91 (let* ((line-width (1+ (floor (log n-lines 10))))
92 (fspec (format "%%-%dd %%s" line-width))
96 (goto-char (point-min))
97 (swiper-font-lock-ensure)
98 (while (< (point) (point-max))
100 (cl-incf line-number)
102 (line-beginning-position)
103 (line-end-position)))
105 (zerop (forward-line 1)))
106 (nreverse candidates))))))
108 (defvar swiper-helm-keymap
109 (let ((map (make-sparse-keymap)))
110 (define-key map (kbd "C-s") 'helm-next-line)
111 (define-key map (kbd "C-r") 'helm-previous-line)
113 "Allows you to go to next and previous hit isearch-style.")
115 (defvar swiper--opoint 1
116 "The point when `swiper' starts.")
119 (defun swiper (&optional initial-input)
120 "`isearch' with an overview.
121 When non-nil, INITIAL-INPUT is the initial search pattern."
123 (setq swiper--opoint (point))
124 (if (and (eq swiper-completion-method 'helm)
126 (swiper--helm initial-input)
127 (swiper--ivy initial-input)))
129 (defun swiper--init ()
130 "Perform initialization common to both completion methods."
133 (setq swiper--anchor (line-number-at-pos))
134 (setq swiper--window (selected-window)))
136 (defun swiper--ivy (&optional initial-input)
137 "`isearch' with an overview using `ivy'.
138 When non-nil, INITIAL-INPUT is the initial search pattern."
144 (setq res (ivy-read "pattern: "
147 #'swiper--update-input-ivy
148 (1- (line-number-at-pos))))
152 (goto-char swiper--opoint)
153 (swiper--action res ivy-text)))))
155 (defun swiper--helm (&optional initial-input)
156 "`isearch' with an overview using `helm'.
157 When non-nil, INITIAL-INPUT is the initial search pattern."
162 (let ((helm-display-function
165 (split-window-vertically))
167 (switch-to-buffer buf)))
168 helm-candidate-number-limit)
170 `((name . ,(buffer-name))
172 (add-hook 'helm-move-selection-after-hook
173 #'swiper--update-sel)
174 (add-hook 'helm-update-hook
175 #'swiper--update-input-helm)
176 (add-hook 'helm-after-update-hook
177 #'swiper--reanchor)))
178 (match-strict . (lambda (x)
180 (string-match (ivy--regex helm-input) x))))
181 (candidates . ,(swiper--candidates))
182 (filtered-candidate-transformer
183 helm-fuzzy-highlight-matches)
184 (action . swiper--action-helm))
185 :keymap (make-composed-keymap
190 (format "^%d " swiper--anchor)
193 (remove-hook 'helm-move-selection-after-hook #'swiper--update-sel)
194 (remove-hook 'helm-update-hook #'swiper--update-input-helm)
195 (remove-hook 'helm-after-update-hook #'swiper--reanchor)
198 (defun swiper--ensure-visible ()
199 "Remove overlays hiding point."
200 (let ((overlays (overlays-at (point)))
202 (while (setq ov (pop overlays))
203 (if (and (invisible-p (overlay-get ov 'invisible))
204 (setq expose (overlay-get ov 'isearch-open-invisible)))
205 (funcall expose ov)))))
207 (defun swiper--cleanup ()
208 "Clean up the overlays."
209 (while swiper--overlays
210 (delete-overlay (pop swiper--overlays)))
212 (goto-char (point-min))
213 (isearch-clean-overlays)))
215 (defvar swiper--overlays nil
218 (defvar swiper--anchor nil
219 "A line number to which the search should be anchored.")
221 (defvar swiper--len 0
222 "The last length of input for which an anchoring was made.")
224 (defun swiper--update-input-helm ()
227 (with-selected-window swiper--window
228 (swiper--add-overlays
229 (ivy--regex helm-input)
230 (window-start swiper--window)
231 (window-end swiper--window t)))
232 (when (/= (length helm-input) swiper--len)
233 (setq swiper--len (length helm-input))
236 (defun swiper--update-input-ivy ()
237 "Called when `ivy' input is updated."
239 (let* ((re (ivy--regex ivy-text))
241 (num (if (string-match "^[0-9]+" str)
242 (string-to-number (match-string 0 str))
244 (with-selected-window swiper--window
245 (goto-char (point-min))
247 (goto-char (point-min))
248 (forward-line (1- num))
249 (isearch-range-invisible (line-beginning-position)
251 (unless (and (> (point) (window-start))
252 (< (point) (window-end swiper--window t)))
254 (let ((ov (make-overlay
255 (line-beginning-position)
256 (1+ (line-end-position)))))
257 (overlay-put ov 'face 'swiper-line-face)
258 (overlay-put ov 'window swiper--window)
259 (push ov swiper--overlays))
260 (swiper--add-overlays
262 (window-start swiper--window)
263 (window-end swiper--window t)))))
265 (defun swiper--add-overlays (re beg end)
266 "Add overlays for RE regexp in current buffer between BEG and END."
267 (when (> (length re) 1)
270 ;; RE can become an invalid regexp
271 (while (and (ignore-errors (re-search-forward re end t))
272 (> (- (match-end 0) (match-beginning 0)) 0))
274 (while (<= i ivy--subexps)
275 (when (match-beginning i)
276 (let ((overlay (make-overlay (match-beginning i)
279 (cond ((zerop ivy--subexps)
280 (cl-caddr swiper-faces))
284 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
286 (push overlay swiper--overlays)
287 (overlay-put overlay 'face face)
288 (overlay-put overlay 'window swiper--window)
289 (overlay-put overlay 'priority i)))
292 (defun swiper--binary (beg end)
293 "Find anchor between BEG and END."
294 (if (<= (- end beg) 10)
299 (goto-char (point-min))
300 (forward-line (1- beg))
303 (setq n (read (current-buffer)))
304 (when (< (setq d (abs (- n swiper--anchor))) min)
309 (goto-char (point-min))
311 (forward-line (1- ln))))
312 (let ((mid (+ beg (/ (- end beg) 2))))
313 (goto-char (point-min))
316 (let ((n (read (current-buffer))))
317 (if (> n swiper--anchor)
318 (swiper--binary beg mid)
319 (swiper--binary mid end))))))
321 (defun swiper--update-sel ()
323 (let* ((re (ivy--regex helm-input))
324 (str (buffer-substring-no-properties
325 (line-beginning-position)
326 (line-end-position)))
327 (num (if (string-match "^[0-9]+" str)
328 (string-to-number (match-string 0 str))
331 (when (> (length re) 0)
332 (with-selected-window swiper--window
333 (goto-char (point-min))
334 (forward-line (1- num))
335 (when (re-search-forward re (point-max) t)
336 (setq pt (match-beginning 0))))
338 (with-selected-window
339 (helm-persistent-action-display-window)
342 (swiper--update-input-helm))))
343 (with-selected-window swiper--window
344 (let ((ov (make-overlay
345 (line-beginning-position)
346 (1+ (line-end-position)))))
347 (overlay-put ov 'face 'swiper-line-face)
348 (push ov swiper--overlays)))))
350 (defun swiper--reanchor ()
351 "Move to a valid match closest to `swiper--anchor'."
353 (goto-char (point-min))
354 (if (re-search-forward (format "^%d " swiper--anchor) nil t)
357 (swiper--binary 2 (1+ (count-lines (point) (point-max)))))
358 (when (> (count-lines (point-min) (point-max)) 1)
360 (helm-next-line 1))))
362 (defun swiper--action-helm (x)
364 (swiper--action x helm-input))
366 (defun swiper--action (x input)
367 "Goto line X and search for INPUT."
369 (user-error "No candidates")
370 (goto-char (point-min))
371 (forward-line (1- (read x)))
373 (ivy--regex input) (line-end-position) t)
374 (swiper--ensure-visible)
375 (when (/= (point) swiper--opoint)
376 (unless (and transient-mark-mode mark-active)
377 (push-mark swiper--opoint t)
378 (message "Mark saved where search started")))))
382 ;;; swiper.el ends here