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 ;; The overview back end is `ivy'.
34 ;; It can double as a quick `regex-builder', although only single
35 ;; lines will be matched.
41 "`isearch' with an overview."
45 (defface swiper-match-face-1
46 '((t (:inherit isearch-lazy-highlight-face)))
47 "The background face for `swiper' matches.")
49 (defface swiper-match-face-2
50 '((t (:inherit isearch)))
51 "Face for `swiper' matches modulo 1.")
53 (defface swiper-match-face-3
54 '((t (:inherit match)))
55 "Face for `swiper' matches modulo 2.")
57 (defface swiper-match-face-4
58 '((t (:inherit isearch-fail)))
59 "Face for `swiper' matches modulo 3.")
61 (defface swiper-line-face
62 '((t (:inherit highlight)))
63 "Face for current `swiper' line.")
65 (defcustom swiper-faces '(swiper-match-face-1
69 "List of `swiper' faces for group matches.")
71 (defcustom swiper-min-highlight 2
72 "Only highlight matches for regexps at least this long."
76 (let ((map (make-sparse-keymap)))
77 (define-key map (kbd "M-q") 'swiper-query-replace)
78 (define-key map (kbd "C-l") 'swiper-recenter-top-bottom)
82 (defun swiper-query-replace ()
83 "Start `query-replace' with string to replace from last search string."
85 (if (null (window-minibuffer-p))
86 (user-error "Should only be called in the minibuffer through `swiper-map'")
87 (let* ((enable-recursive-minibuffers t)
88 (from (ivy--regex ivy-text))
89 (to (query-replace-read-to from "Query replace" t)))
90 (delete-minibuffer-contents)
93 (with-selected-window swiper--window
94 (perform-replace from to
99 (defvar swiper--window nil
100 "Store the current window.")
102 (defun swiper-recenter-top-bottom (&optional arg)
103 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
105 (with-selected-window swiper--window
106 (recenter-top-bottom arg)))
108 (defun swiper-font-lock-ensure ()
109 "Ensure the entired buffer is highlighted."
110 (unless (or (derived-mode-p 'magit-mode)
111 (memq major-mode '(package-menu-mode
115 emms-playlist-mode erc-mode
119 elfeed-search-mode)))
120 (unless (> (buffer-size) 100000)
121 (if (fboundp 'font-lock-ensure)
123 (with-no-warnings (font-lock-fontify-buffer))))))
125 (defvar swiper--format-spec ""
126 "Store the current candidates format spec.")
128 (defun swiper--candidates ()
129 "Return a list of this buffer lines."
130 (let ((n-lines (count-lines (point-min) (point-max))))
131 (unless (zerop n-lines)
132 (setq swiper--format-spec
133 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
134 (let ((line-number 0)
137 (goto-char (point-min))
138 (swiper-font-lock-ensure)
139 (while (< (point) (point-max))
140 (push (format swiper--format-spec
141 (cl-incf line-number)
143 (line-beginning-position)
144 (line-end-position)))
147 (nreverse candidates))))))
149 (defvar swiper--opoint 1
150 "The point when `swiper' starts.")
153 (defun swiper (&optional initial-input)
154 "`isearch' with an overview.
155 When non-nil, INITIAL-INPUT is the initial search pattern."
157 (swiper--ivy initial-input))
159 (defvar swiper--anchor nil
160 "A line number to which the search should be anchored.")
162 (defvar swiper--len 0
163 "The last length of input for which an anchoring was made.")
165 (defun swiper--init ()
166 "Perform initialization common to both completion methods."
168 (setq swiper--opoint (point))
170 (setq swiper--anchor (line-number-at-pos))
171 (setq swiper--window (selected-window)))
173 (defun swiper--ivy (&optional initial-input)
174 "`isearch' with an overview using `ivy'.
175 When non-nil, INITIAL-INPUT is the initial search pattern."
177 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
178 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
179 Please remove it and update the \"swiper\" package."))
181 (let ((candidates (swiper--candidates))
186 (buffer-substring-no-properties
187 (line-beginning-position)
188 (line-end-position)))))
192 (replace-regexp-in-string
193 "%s" "pattern: " swiper--format-spec)
195 :initial-input initial-input
199 :update-fn #'swiper--update-input-ivy))
202 (goto-char swiper--opoint)
203 (swiper--action res ivy-text)))))
205 (defun swiper--ensure-visible ()
206 "Remove overlays hiding point."
207 (let ((overlays (overlays-at (point)))
209 (while (setq ov (pop overlays))
210 (if (and (invisible-p (overlay-get ov 'invisible))
211 (setq expose (overlay-get ov 'isearch-open-invisible)))
212 (funcall expose ov)))))
214 (defvar swiper--overlays nil
217 (defun swiper--cleanup ()
218 "Clean up the overlays."
219 (while swiper--overlays
220 (delete-overlay (pop swiper--overlays)))
222 (goto-char (point-min))
223 (isearch-clean-overlays)))
225 (defun swiper--update-input-ivy ()
226 "Called when `ivy' input is updated."
228 (let* ((re (ivy--regex ivy-text))
230 (num (if (string-match "^[0-9]+" str)
231 (string-to-number (match-string 0 str))
233 (with-selected-window swiper--window
234 (goto-char (point-min))
236 (goto-char (point-min))
237 (forward-line (1- num))
238 (isearch-range-invisible (line-beginning-position)
240 (unless (and (>= (point) (window-start))
241 (<= (point) (window-end swiper--window t)))
243 (swiper--add-overlays re))))
245 (defun swiper--add-overlays (re &optional beg end)
246 "Add overlays for RE regexp in visible part of the current buffer.
247 BEG and END, when specified, are the point bounds."
248 (let ((ov (make-overlay
249 (line-beginning-position)
250 (1+ (line-end-position)))))
251 (overlay-put ov 'face 'swiper-line-face)
252 (overlay-put ov 'window swiper--window)
253 (push ov swiper--overlays))
254 (let* ((wh (window-height))
255 (beg (or beg (save-excursion
256 (forward-line (- wh))
258 (end (or end (save-excursion
261 (when (>= (length re) swiper-min-highlight)
264 ;; RE can become an invalid regexp
265 (while (and (ignore-errors (re-search-forward re end t))
266 (> (- (match-end 0) (match-beginning 0)) 0))
268 (while (<= i ivy--subexps)
269 (when (match-beginning i)
270 (let ((overlay (make-overlay (match-beginning i)
273 (cond ((zerop ivy--subexps)
278 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
280 (push overlay swiper--overlays)
281 (overlay-put overlay 'face face)
282 (overlay-put overlay 'window swiper--window)
283 (overlay-put overlay 'priority i)))
286 (defun swiper--action (x input)
287 "Goto line X and search for INPUT."
289 (user-error "No candidates")
290 (goto-char (point-min))
291 (forward-line (1- (read x)))
293 (ivy--regex input) (line-end-position) t)
294 (swiper--ensure-visible)
295 (when (/= (point) swiper--opoint)
296 (unless (and transient-mark-mode mark-active)
297 (push-mark swiper--opoint t)
298 (message "Mark saved where search started")))))
302 ;;; swiper.el ends here