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)
81 (defun swiper-query-replace ()
82 "Start `query-replace' with string to replace from last search string."
84 (if (null (window-minibuffer-p))
85 (user-error "Should only be called in the minibuffer through `swiper-map'")
86 (let* ((from (ivy--regex ivy-text))
87 (to (query-replace-read-to from "Query replace" t)))
88 (delete-minibuffer-contents)
91 (perform-replace from to
96 (defvar swiper--window nil
97 "Store the current window.")
99 (defun swiper-font-lock-ensure ()
100 "Ensure the entired buffer is highlighted."
101 (unless (or (derived-mode-p 'magit-mode)
102 (memq major-mode '(package-menu-mode
106 emms-playlist-mode erc-mode)))
107 (if (fboundp 'font-lock-ensure)
109 (font-lock-fontify-buffer))))
111 (defvar swiper--format-spec ""
112 "Store the current candidates format spec.")
114 (defun swiper--candidates ()
115 "Return a list of this buffer lines."
116 (let ((n-lines (count-lines (point-min) (point-max))))
117 (unless (zerop n-lines)
118 (setq swiper--format-spec
119 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
120 (let ((line-number 0)
123 (goto-char (point-min))
124 (swiper-font-lock-ensure)
125 (while (< (point) (point-max))
126 (push (format swiper--format-spec
127 (cl-incf line-number)
129 (line-beginning-position)
130 (line-end-position)))
132 (zerop (forward-line 1)))
133 (nreverse candidates))))))
135 (defvar swiper--opoint 1
136 "The point when `swiper' starts.")
139 (defun swiper (&optional initial-input)
140 "`isearch' with an overview.
141 When non-nil, INITIAL-INPUT is the initial search pattern."
143 (swiper--ivy initial-input))
145 (defun swiper--init ()
146 "Perform initialization common to both completion methods."
148 (setq swiper--opoint (point))
150 (setq swiper--anchor (line-number-at-pos))
151 (setq swiper--window (selected-window)))
153 (defun swiper--ivy (&optional initial-input)
154 "`isearch' with an overview using `ivy'.
155 When non-nil, INITIAL-INPUT is the initial search pattern."
159 (let ((candidates (swiper--candidates))
164 (buffer-substring-no-properties
165 (line-beginning-position)
166 (line-end-position)))))
170 (replace-regexp-in-string
171 "%s" "pattern: " swiper--format-spec)
176 #'swiper--update-input-ivy))
180 (goto-char swiper--opoint)
181 (swiper--action res ivy-text)))))
183 (defun swiper--ensure-visible ()
184 "Remove overlays hiding point."
185 (let ((overlays (overlays-at (point)))
187 (while (setq ov (pop overlays))
188 (if (and (invisible-p (overlay-get ov 'invisible))
189 (setq expose (overlay-get ov 'isearch-open-invisible)))
190 (funcall expose ov)))))
192 (defun swiper--cleanup ()
193 "Clean up the overlays."
194 (while swiper--overlays
195 (delete-overlay (pop swiper--overlays)))
197 (goto-char (point-min))
198 (isearch-clean-overlays)))
200 (defvar swiper--overlays nil
203 (defvar swiper--anchor nil
204 "A line number to which the search should be anchored.")
206 (defvar swiper--len 0
207 "The last length of input for which an anchoring was made.")
209 (defun swiper--update-input-ivy ()
210 "Called when `ivy' input is updated."
212 (let* ((re (ivy--regex ivy-text))
214 (num (if (string-match "^[0-9]+" str)
215 (string-to-number (match-string 0 str))
217 (with-selected-window swiper--window
218 (goto-char (point-min))
220 (goto-char (point-min))
221 (forward-line (1- num))
222 (isearch-range-invisible (line-beginning-position)
224 (unless (and (> (point) (window-start))
225 (< (point) (window-end swiper--window t)))
227 (let ((ov (make-overlay
228 (line-beginning-position)
229 (1+ (line-end-position)))))
230 (overlay-put ov 'face 'swiper-line-face)
231 (overlay-put ov 'window swiper--window)
232 (push ov swiper--overlays))
233 (swiper--add-overlays
235 (window-start swiper--window)
236 (window-end swiper--window t)))))
238 (defun swiper--add-overlays (re beg end)
239 "Add overlays for RE regexp in current buffer between BEG and END."
240 (when (>= (length re) swiper-min-highlight)
243 ;; RE can become an invalid regexp
244 (while (and (ignore-errors (re-search-forward re end t))
245 (> (- (match-end 0) (match-beginning 0)) 0))
247 (while (<= i ivy--subexps)
248 (when (match-beginning i)
249 (let ((overlay (make-overlay (match-beginning i)
252 (cond ((zerop ivy--subexps)
253 (cl-caddr swiper-faces))
257 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
259 (push overlay swiper--overlays)
260 (overlay-put overlay 'face face)
261 (overlay-put overlay 'window swiper--window)
262 (overlay-put overlay 'priority i)))
265 (defun swiper--action (x input)
266 "Goto line X and search for INPUT."
268 (user-error "No candidates")
269 (goto-char (point-min))
270 (forward-line (1- (read x)))
272 (ivy--regex input) (line-end-position) t)
273 (swiper--ensure-visible)
274 (when (/= (point) swiper--opoint)
275 (unless (and transient-mark-mode mark-active)
276 (push-mark swiper--opoint t)
277 (message "Mark saved where search started")))))
281 ;;; swiper.el ends here