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)
85 (defun swiper-query-replace ()
86 "Start `query-replace' with string to replace from last search string."
88 (if (null (window-minibuffer-p))
89 (user-error "Should only be called in the minibuffer through `swiper-map'")
90 (let* ((enable-recursive-minibuffers t)
91 (from (ivy--regex ivy-text))
92 (to (query-replace-read-to from "Query replace" t)))
93 (delete-minibuffer-contents)
94 (ivy-set-action (lambda ()
95 (with-selected-window swiper--window
96 (perform-replace from to
101 (defvar swiper--window nil
102 "Store the current window.")
104 (defun swiper-recenter-top-bottom (&optional arg)
105 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
107 (with-selected-window swiper--window
108 (recenter-top-bottom arg)))
110 (defun swiper-font-lock-ensure ()
111 "Ensure the entired buffer is highlighted."
112 (unless (or (derived-mode-p 'magit-mode)
113 (memq major-mode '(package-menu-mode
117 emms-playlist-mode erc-mode
121 elfeed-search-mode)))
122 (unless (> (buffer-size) 100000)
123 (if (fboundp 'font-lock-ensure)
125 (with-no-warnings (font-lock-fontify-buffer))))))
127 (defvar swiper--format-spec ""
128 "Store the current candidates format spec.")
130 (defun swiper--candidates ()
131 "Return a list of this buffer lines."
132 (let ((n-lines (count-lines (point-min) (point-max))))
133 (unless (zerop n-lines)
134 (setq swiper--format-spec
135 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
136 (let ((line-number 0)
139 (goto-char (point-min))
140 (swiper-font-lock-ensure)
141 (while (< (point) (point-max))
142 (push (format swiper--format-spec
143 (cl-incf line-number)
145 (line-beginning-position)
146 (line-end-position)))
149 (nreverse candidates))))))
151 (defvar swiper--opoint 1
152 "The point when `swiper' starts.")
155 (defun swiper (&optional initial-input)
156 "`isearch' with an overview.
157 When non-nil, INITIAL-INPUT is the initial search pattern."
159 (swiper--ivy initial-input))
161 (defvar swiper--anchor nil
162 "A line number to which the search should be anchored.")
164 (defvar swiper--len 0
165 "The last length of input for which an anchoring was made.")
167 (defun swiper--init ()
168 "Perform initialization common to both completion methods."
170 (setq swiper--opoint (point))
172 (setq swiper--anchor (line-number-at-pos))
173 (setq swiper--window (selected-window)))
175 (defun swiper--ivy (&optional initial-input)
176 "`isearch' with an overview using `ivy'.
177 When non-nil, INITIAL-INPUT is the initial search pattern."
179 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
180 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
181 Please remove it and update the \"swiper\" package."))
183 (let ((candidates (swiper--candidates))
188 (buffer-substring-no-properties
189 (line-beginning-position)
190 (line-end-position)))))
194 (replace-regexp-in-string
195 "%s" "pattern: " swiper--format-spec)
197 :initial-input initial-input
201 :update-fn #'swiper--update-input-ivy
202 :unwind #'swiper--cleanup))
204 (goto-char swiper--opoint)
205 (swiper--action res ivy-text)))))
207 (defun swiper--ensure-visible ()
208 "Remove overlays hiding point."
209 (let ((overlays (overlays-at (point)))
211 (while (setq ov (pop overlays))
212 (if (and (invisible-p (overlay-get ov 'invisible))
213 (setq expose (overlay-get ov 'isearch-open-invisible)))
214 (funcall expose ov)))))
216 (defvar swiper--overlays nil
219 (defun swiper--cleanup ()
220 "Clean up the overlays."
221 (while swiper--overlays
222 (delete-overlay (pop swiper--overlays)))
224 (goto-char (point-min))
225 (isearch-clean-overlays)))
227 (defun swiper--update-input-ivy ()
228 "Called when `ivy' input is updated."
230 (let* ((re (ivy--regex ivy-text))
232 (num (if (string-match "^[0-9]+" str)
233 (string-to-number (match-string 0 str))
235 (with-selected-window swiper--window
236 (goto-char (point-min))
238 (goto-char (point-min))
239 (forward-line (1- num))
240 (isearch-range-invisible (line-beginning-position)
242 (unless (and (>= (point) (window-start))
243 (<= (point) (window-end swiper--window t)))
245 (swiper--add-overlays re))))
247 (defun swiper--add-overlays (re &optional beg end)
248 "Add overlays for RE regexp in visible part of the current buffer.
249 BEG and END, when specified, are the point bounds."
250 (let ((ov (make-overlay
251 (line-beginning-position)
252 (1+ (line-end-position)))))
253 (overlay-put ov 'face 'swiper-line-face)
254 (overlay-put ov 'window swiper--window)
255 (push ov swiper--overlays))
256 (let* ((wh (window-height))
257 (beg (or beg (save-excursion
258 (forward-line (- wh))
260 (end (or end (save-excursion
263 (when (>= (length re) swiper-min-highlight)
266 ;; RE can become an invalid regexp
267 (while (and (ignore-errors (re-search-forward re end t))
268 (> (- (match-end 0) (match-beginning 0)) 0))
270 (while (<= i ivy--subexps)
271 (when (match-beginning i)
272 (let ((overlay (make-overlay (match-beginning i)
275 (cond ((zerop ivy--subexps)
280 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
282 (push overlay swiper--overlays)
283 (overlay-put overlay 'face face)
284 (overlay-put overlay 'window swiper--window)
285 (overlay-put overlay 'priority i)))
288 (defun swiper--action (x input)
289 "Goto line X and search for INPUT."
291 (user-error "No candidates")
292 (goto-char (point-min))
293 (forward-line (1- (read x)))
295 (ivy--regex input) (line-end-position) t)
296 (swiper--ensure-visible)
297 (when (/= (point) swiper--opoint)
298 (unless (and transient-mark-mode mark-active)
299 (push-mark swiper--opoint t)
300 (message "Mark saved where search started")))))
304 ;;; swiper.el ends here