1 ;;; swiper.el --- Isearch with an overview. Oh, man! -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((emacs "24.1"))
11 ;; This file is 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 ;; It can double as a quick `regex-builder', although only single
33 ;; lines will be matched.
35 ;; It also provides `ivy-mode': a global minor mode that uses the
36 ;; matching back end of `swiper' for all matching on your system,
37 ;; including file matching. You can use it in place of `ido-mode'
38 ;; (can't have both on at once).
44 "`isearch' with an overview."
48 (defface swiper-match-face-1
49 '((t (:inherit isearch-lazy-highlight-face)))
50 "The background face for `swiper' matches.")
52 (defface swiper-match-face-2
53 '((t (:inherit isearch)))
54 "Face for `swiper' matches modulo 1.")
56 (defface swiper-match-face-3
57 '((t (:inherit match)))
58 "Face for `swiper' matches modulo 2.")
60 (defface swiper-match-face-4
61 '((t (:inherit isearch-fail)))
62 "Face for `swiper' matches modulo 3.")
64 (defface swiper-line-face
65 '((t (:inherit highlight)))
66 "Face for current `swiper' line.")
68 (defcustom swiper-faces '(swiper-match-face-1
72 "List of `swiper' faces for group matches.")
74 (defcustom swiper-min-highlight 2
75 "Only highlight matches for regexps at least this long."
79 (let ((map (make-sparse-keymap)))
80 (define-key map (kbd "M-q") 'swiper-query-replace)
81 (define-key map (kbd "C-l") 'swiper-recenter-top-bottom)
82 (define-key map (kbd "C-'") 'swiper-avy)
86 (defvar swiper--window nil
87 "Store the current window.")
89 (defun swiper-query-replace ()
90 "Start `query-replace' with string to replace from last search string."
92 (if (null (window-minibuffer-p))
93 (user-error "Should only be called in the minibuffer through `swiper-map'")
94 (let* ((enable-recursive-minibuffers t)
95 (from (ivy--regex ivy-text))
96 (to (query-replace-read-to from "Query replace" t)))
97 (delete-minibuffer-contents)
98 (ivy-set-action (lambda (_)
99 (with-selected-window swiper--window
100 (move-beginning-of-line 1)
101 (perform-replace from to
106 (defvar avy-background)
107 (defvar avy-all-windows)
108 (declare-function avy--regex-candidates "ext:avy")
109 (declare-function avy--process "ext:avy")
110 (declare-function avy--overlay-post "ext:avy")
111 (declare-function avy--goto "ext:avy")
115 "Jump to one of the current swiper candidates."
117 (unless (string= ivy-text "")
118 (with-selected-window (ivy-state-window ivy-last)
119 (let* ((avy-all-windows nil)
121 (avy--regex-candidates
122 (ivy--regex ivy-text)))
125 (avy--process candidates #'avy--overlay-post)))
127 (avy--goto candidate))))))
129 (defun swiper-recenter-top-bottom (&optional arg)
130 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
132 (with-selected-window swiper--window
133 (recenter-top-bottom arg)))
135 (defun swiper-font-lock-ensure ()
136 "Ensure the entired buffer is highlighted."
137 (unless (or (derived-mode-p 'magit-mode)
138 (bound-and-true-p magit-blame-mode)
139 (memq major-mode '(package-menu-mode
143 emms-playlist-mode erc-mode
151 (unless (> (buffer-size) 100000)
152 (if (fboundp 'font-lock-ensure)
154 (with-no-warnings (font-lock-fontify-buffer))))))
156 (defvar swiper--format-spec ""
157 "Store the current candidates format spec.")
159 (defvar swiper--width nil
160 "Store the amount of digits needed for the longest line nubmer.")
162 (defun swiper--candidates ()
163 "Return a list of this buffer lines."
164 (let ((n-lines (count-lines (point-min) (point-max))))
165 (unless (zerop n-lines)
166 (setq swiper--width (1+ (floor (log n-lines 10))))
167 (setq swiper--format-spec
168 (format "%%-%dd %%s" swiper--width))
169 (let ((line-number 0)
172 (goto-char (point-min))
173 (swiper-font-lock-ensure)
174 (while (< (point) (point-max))
175 (push (format swiper--format-spec
176 (cl-incf line-number)
178 (line-beginning-position)
179 (line-end-position)))
182 (nreverse candidates))))))
184 (defvar swiper--opoint 1
185 "The point when `swiper' starts.")
188 (defun swiper (&optional initial-input)
189 "`isearch' with an overview.
190 When non-nil, INITIAL-INPUT is the initial search pattern."
192 (swiper--ivy initial-input))
194 (defvar swiper--anchor nil
195 "A line number to which the search should be anchored.")
197 (defvar swiper--len 0
198 "The last length of input for which an anchoring was made.")
200 (defun swiper--init ()
201 "Perform initialization common to both completion methods."
202 (setq swiper--opoint (point))
204 (setq swiper--anchor (line-number-at-pos))
205 (setq swiper--window (selected-window)))
207 (defun swiper--re-builder (str)
208 "Transform STR into a swiper regex.
209 This is the regex used in the minibuffer, since the candidates
210 there have line numbers. In the buffer, `ivy--regex' should be used."
216 ((string-match "^\\^" str)
217 (setq ivy--old-re "")
218 (let ((re (ivy--regex-plus (substring str 1))))
219 (format "^[0-9][0-9 ]\\{%d\\}%s"
221 (if (zerop ivy--subexps)
222 (prog1 (format "\\(%s\\)" re)
223 (setq ivy--subexps 1))
226 (ivy--regex-plus str))))
228 (defvar swiper-history nil
229 "History for `swiper'.")
231 (defun swiper--ivy (&optional initial-input)
232 "`isearch' with an overview using `ivy'.
233 When non-nil, INITIAL-INPUT is the initial search pattern."
235 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
236 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
237 Please remove it and update the \"swiper\" package."))
239 (let ((candidates (swiper--candidates))
243 (buffer-substring-no-properties
244 (line-beginning-position)
245 (line-end-position))))
251 :initial-input initial-input
255 :update-fn #'swiper--update-input-ivy
256 :unwind #'swiper--cleanup
257 :re-builder #'swiper--re-builder
258 :history 'swiper-history))
260 (goto-char swiper--opoint)
261 (swiper--action res ivy-text)))))
263 (defun swiper--ensure-visible ()
264 "Remove overlays hiding point."
265 (let ((overlays (overlays-at (point)))
267 (while (setq ov (pop overlays))
268 (if (and (invisible-p (overlay-get ov 'invisible))
269 (setq expose (overlay-get ov 'isearch-open-invisible)))
270 (funcall expose ov)))))
272 (defvar swiper--overlays nil
275 (defun swiper--cleanup ()
276 "Clean up the overlays."
277 (while swiper--overlays
278 (delete-overlay (pop swiper--overlays)))
280 (goto-char (point-min))
281 (isearch-clean-overlays)))
283 (defun swiper--update-input-ivy ()
284 "Called when `ivy' input is updated."
286 (let* ((re (ivy--regex ivy-text))
288 (num (if (string-match "^[0-9]+" str)
289 (string-to-number (match-string 0 str))
291 (with-selected-window swiper--window
292 (goto-char (point-min))
294 (goto-char (point-min))
295 (forward-line (1- num))
296 (if (and (equal ivy-text "")
297 (>= swiper--opoint (line-beginning-position))
298 (<= swiper--opoint (line-end-position)))
299 (goto-char swiper--opoint)
300 (re-search-forward re (line-end-position) t))
301 (isearch-range-invisible (line-beginning-position)
303 (unless (and (>= (point) (window-start))
304 (<= (point) (window-end swiper--window t)))
306 (swiper--add-overlays re))))
308 (defun swiper--add-overlays (re &optional beg end)
309 "Add overlays for RE regexp in visible part of the current buffer.
310 BEG and END, when specified, are the point bounds."
311 (let ((ov (make-overlay
312 (line-beginning-position)
313 (1+ (line-end-position)))))
314 (overlay-put ov 'face 'swiper-line-face)
315 (overlay-put ov 'window swiper--window)
316 (push ov swiper--overlays))
317 (let* ((wh (window-height))
318 (beg (or beg (save-excursion
319 (forward-line (- wh))
321 (end (or end (save-excursion
324 (when (>= (length re) swiper-min-highlight)
327 ;; RE can become an invalid regexp
328 (while (and (ignore-errors (re-search-forward re end t))
329 (> (- (match-end 0) (match-beginning 0)) 0))
331 (while (<= i ivy--subexps)
332 (when (match-beginning i)
333 (let ((overlay (make-overlay (match-beginning i)
336 (cond ((zerop ivy--subexps)
341 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
343 (push overlay swiper--overlays)
344 (overlay-put overlay 'face face)
345 (overlay-put overlay 'window swiper--window)
346 (overlay-put overlay 'priority i)))
349 (defun swiper--action (x input)
350 "Goto line X and search for INPUT."
352 (user-error "No candidates")
353 (goto-char (point-min))
354 (forward-line (1- (read x)))
356 (ivy--regex input) (line-end-position) t)
357 (swiper--ensure-visible)
358 (when (/= (point) swiper--opoint)
359 (unless (and transient-mark-mode mark-active)
360 (push-mark swiper--opoint t)
361 (message "Mark saved where search started")))))
365 ;;; swiper.el ends here