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 (defvar swiper--width nil
131 "Store the amount of digits needed for the longest line nubmer.")
133 (defun swiper--candidates ()
134 "Return a list of this buffer lines."
135 (let ((n-lines (count-lines (point-min) (point-max))))
136 (unless (zerop n-lines)
137 (setq swiper--width (1+ (floor (log n-lines 10))))
138 (setq swiper--format-spec
139 (format "%%-%dd %%s" swiper--width))
140 (let ((line-number 0)
143 (goto-char (point-min))
144 (swiper-font-lock-ensure)
145 (while (< (point) (point-max))
146 (push (format swiper--format-spec
147 (cl-incf line-number)
149 (line-beginning-position)
150 (line-end-position)))
153 (nreverse candidates))))))
155 (defvar swiper--opoint 1
156 "The point when `swiper' starts.")
159 (defun swiper (&optional initial-input)
160 "`isearch' with an overview.
161 When non-nil, INITIAL-INPUT is the initial search pattern."
163 (swiper--ivy initial-input))
165 (defvar swiper--anchor nil
166 "A line number to which the search should be anchored.")
168 (defvar swiper--len 0
169 "The last length of input for which an anchoring was made.")
171 (defun swiper--init ()
172 "Perform initialization common to both completion methods."
174 (setq swiper--opoint (point))
176 (setq swiper--anchor (line-number-at-pos))
177 (setq swiper--window (selected-window))
178 (setq ivy--regex-function
179 (cdr (assoc t ivy-re-builders-alist))))
181 (defun swiper--ivy (&optional initial-input)
182 "`isearch' with an overview using `ivy'.
183 When non-nil, INITIAL-INPUT is the initial search pattern."
185 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
186 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
187 Please remove it and update the \"swiper\" package."))
189 (let ((candidates (swiper--candidates))
194 (buffer-substring-no-properties
195 (line-beginning-position)
196 (line-end-position)))))
200 (replace-regexp-in-string
201 "%s" "pattern: " swiper--format-spec)
203 :initial-input initial-input
207 :update-fn #'swiper--update-input-ivy
208 :unwind #'swiper--cleanup))
210 (goto-char swiper--opoint)
211 (swiper--action res ivy-text)))))
213 (defun swiper--ensure-visible ()
214 "Remove overlays hiding point."
215 (let ((overlays (overlays-at (point)))
217 (while (setq ov (pop overlays))
218 (if (and (invisible-p (overlay-get ov 'invisible))
219 (setq expose (overlay-get ov 'isearch-open-invisible)))
220 (funcall expose ov)))))
222 (defvar swiper--overlays nil
225 (defun swiper--cleanup ()
226 "Clean up the overlays."
227 (while swiper--overlays
228 (delete-overlay (pop swiper--overlays)))
230 (goto-char (point-min))
231 (isearch-clean-overlays)))
233 (defun swiper--update-input-ivy ()
234 "Called when `ivy' input is updated."
236 (let* ((re (funcall ivy--regex-function ivy-text))
238 (num (if (string-match "^[0-9]+" str)
239 (string-to-number (match-string 0 str))
241 (with-selected-window swiper--window
242 (goto-char (point-min))
244 (goto-char (point-min))
245 (forward-line (1- num))
246 (isearch-range-invisible (line-beginning-position)
248 (unless (and (>= (point) (window-start))
249 (<= (point) (window-end swiper--window t)))
251 (swiper--add-overlays re))))
253 (defun swiper--add-overlays (re &optional beg end)
254 "Add overlays for RE regexp in visible part of the current buffer.
255 BEG and END, when specified, are the point bounds."
256 (let ((ov (make-overlay
257 (line-beginning-position)
258 (1+ (line-end-position)))))
259 (overlay-put ov 'face 'swiper-line-face)
260 (overlay-put ov 'window swiper--window)
261 (push ov swiper--overlays))
262 (let* ((wh (window-height))
263 (beg (or beg (save-excursion
264 (forward-line (- wh))
266 (end (or end (save-excursion
269 (when (>= (length re) swiper-min-highlight)
272 ;; RE can become an invalid regexp
273 (while (and (ignore-errors (re-search-forward re end t))
274 (> (- (match-end 0) (match-beginning 0)) 0))
276 (while (<= i ivy--subexps)
277 (when (match-beginning i)
278 (let ((overlay (make-overlay (match-beginning i)
281 (cond ((zerop ivy--subexps)
286 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
288 (push overlay swiper--overlays)
289 (overlay-put overlay 'face face)
290 (overlay-put overlay 'window swiper--window)
291 (overlay-put overlay 'priority i)))
294 (defun swiper--action (x input)
295 "Goto line X and search for INPUT."
297 (user-error "No candidates")
298 (goto-char (point-min))
299 (forward-line (1- (read x)))
301 (funcall ivy--regex-function input) (line-end-position) t)
302 (swiper--ensure-visible)
303 (when (/= (point) swiper--opoint)
304 (unless (and transient-mark-mode mark-active)
305 (push-mark swiper--opoint t)
306 (message "Mark saved where search started")))))
310 ;;; swiper.el ends here