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 "Face for `swiper' matches.")
49 (defface swiper-match-face-2
50 '((t (:inherit isearch)))
51 "Face for `swiper' matches.")
53 (defface swiper-match-face-3
54 '((t (:inherit match)))
55 "Face for `swiper' matches.")
57 (defface swiper-match-face-4
58 '((t (:inherit isearch)))
59 "Face for `swiper' matches.")
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
118 (unless (> (buffer-size) 100000)
119 (if (fboundp 'font-lock-ensure)
121 (with-no-warnings (font-lock-fontify-buffer))))))
123 (defvar swiper--format-spec ""
124 "Store the current candidates format spec.")
126 (defun swiper--candidates ()
127 "Return a list of this buffer lines."
128 (let ((n-lines (count-lines (point-min) (point-max))))
129 (unless (zerop n-lines)
130 (setq swiper--format-spec
131 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
132 (let ((line-number 0)
135 (goto-char (point-min))
136 (swiper-font-lock-ensure)
137 (while (< (point) (point-max))
138 (push (format swiper--format-spec
139 (cl-incf line-number)
141 (line-beginning-position)
142 (line-end-position)))
145 (nreverse candidates))))))
147 (defvar swiper--opoint 1
148 "The point when `swiper' starts.")
151 (defun swiper (&optional initial-input)
152 "`isearch' with an overview.
153 When non-nil, INITIAL-INPUT is the initial search pattern."
155 (swiper--ivy initial-input))
157 (defvar swiper--anchor nil
158 "A line number to which the search should be anchored.")
160 (defvar swiper--len 0
161 "The last length of input for which an anchoring was made.")
163 (defun swiper--init ()
164 "Perform initialization common to both completion methods."
166 (setq swiper--opoint (point))
168 (setq swiper--anchor (line-number-at-pos))
169 (setq swiper--window (selected-window)))
171 (defun swiper--ivy (&optional initial-input)
172 "`isearch' with an overview using `ivy'.
173 When non-nil, INITIAL-INPUT is the initial search pattern."
176 (let ((candidates (swiper--candidates))
181 (buffer-substring-no-properties
182 (line-beginning-position)
183 (line-end-position)))))
187 (replace-regexp-in-string
188 "%s" "pattern: " swiper--format-spec)
190 :initial-input initial-input
194 :update-fn #'swiper--update-input-ivy))
197 (goto-char swiper--opoint)
198 (swiper--action res ivy-text)))))
200 (defun swiper--ensure-visible ()
201 "Remove overlays hiding point."
202 (let ((overlays (overlays-at (point)))
204 (while (setq ov (pop overlays))
205 (if (and (invisible-p (overlay-get ov 'invisible))
206 (setq expose (overlay-get ov 'isearch-open-invisible)))
207 (funcall expose ov)))))
209 (defvar swiper--overlays nil
212 (defun swiper--cleanup ()
213 "Clean up the overlays."
214 (while swiper--overlays
215 (delete-overlay (pop swiper--overlays)))
217 (goto-char (point-min))
218 (isearch-clean-overlays)))
220 (defun swiper--update-input-ivy ()
221 "Called when `ivy' input is updated."
223 (let* ((re (ivy--regex ivy-text))
225 (num (if (string-match "^[0-9]+" str)
226 (string-to-number (match-string 0 str))
228 (with-selected-window swiper--window
229 (goto-char (point-min))
231 (goto-char (point-min))
232 (forward-line (1- num))
233 (isearch-range-invisible (line-beginning-position)
235 (unless (and (>= (point) (window-start))
236 (<= (point) (window-end swiper--window t)))
238 (swiper--add-overlays re))))
240 (defun swiper--add-overlays (re &optional beg end)
241 "Add overlays for RE regexp in visible part of the current buffer.
242 BEG and END, when specified, are the point bounds."
243 (let ((ov (make-overlay
244 (line-beginning-position)
245 (1+ (line-end-position)))))
246 (overlay-put ov 'face 'swiper-line-face)
247 (overlay-put ov 'window swiper--window)
248 (push ov swiper--overlays))
249 (let* ((wh (window-height))
250 (beg (or beg (save-excursion
251 (forward-line (- wh))
253 (end (or end (save-excursion
256 (when (>= (length re) swiper-min-highlight)
259 ;; RE can become an invalid regexp
260 (while (and (ignore-errors (re-search-forward re end t))
261 (> (- (match-end 0) (match-beginning 0)) 0))
263 (while (<= i ivy--subexps)
264 (when (match-beginning i)
265 (let ((overlay (make-overlay (match-beginning i)
268 (cond ((zerop ivy--subexps)
269 (cl-caddr swiper-faces))
273 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
275 (push overlay swiper--overlays)
276 (overlay-put overlay 'face face)
277 (overlay-put overlay 'window swiper--window)
278 (overlay-put overlay 'priority i)))
281 (defun swiper--action (x input)
282 "Goto line X and search for INPUT."
284 (user-error "No candidates")
285 (goto-char (point-min))
286 (forward-line (1- (read x)))
288 (ivy--regex input) (line-end-position) t)
289 (swiper--ensure-visible)
290 (when (/= (point) swiper--opoint)
291 (unless (and transient-mark-mode mark-active)
292 (push-mark swiper--opoint t)
293 (message "Mark saved where search started")))))
297 ;;; swiper.el ends here