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 (perform-replace from to
98 (defvar swiper--window nil
99 "Store the current window.")
101 (defun swiper-recenter-top-bottom (&optional arg)
102 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
104 (with-selected-window swiper--window
105 (recenter-top-bottom arg)))
107 (defun swiper-font-lock-ensure ()
108 "Ensure the entired buffer is highlighted."
109 (unless (or (derived-mode-p 'magit-mode)
110 (memq major-mode '(package-menu-mode
114 emms-playlist-mode erc-mode
117 (unless (> (buffer-size) 100000)
118 (if (fboundp 'font-lock-ensure)
120 (with-no-warnings (font-lock-fontify-buffer))))))
122 (defvar swiper--format-spec ""
123 "Store the current candidates format spec.")
125 (defun swiper--candidates ()
126 "Return a list of this buffer lines."
127 (let ((n-lines (count-lines (point-min) (point-max))))
128 (unless (zerop n-lines)
129 (setq swiper--format-spec
130 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
131 (let ((line-number 0)
134 (goto-char (point-min))
135 (swiper-font-lock-ensure)
136 (while (< (point) (point-max))
137 (push (format swiper--format-spec
138 (cl-incf line-number)
140 (line-beginning-position)
141 (line-end-position)))
144 (nreverse candidates))))))
146 (defvar swiper--opoint 1
147 "The point when `swiper' starts.")
150 (defun swiper (&optional initial-input)
151 "`isearch' with an overview.
152 When non-nil, INITIAL-INPUT is the initial search pattern."
154 (swiper--ivy initial-input))
156 (defvar swiper--anchor nil
157 "A line number to which the search should be anchored.")
159 (defvar swiper--len 0
160 "The last length of input for which an anchoring was made.")
162 (defun swiper--init ()
163 "Perform initialization common to both completion methods."
165 (setq swiper--opoint (point))
167 (setq swiper--anchor (line-number-at-pos))
168 (setq swiper--window (selected-window)))
170 (defun swiper--ivy (&optional initial-input)
171 "`isearch' with an overview using `ivy'.
172 When non-nil, INITIAL-INPUT is the initial search pattern."
175 (let ((candidates (swiper--candidates))
180 (buffer-substring-no-properties
181 (line-beginning-position)
182 (line-end-position)))))
186 (replace-regexp-in-string
187 "%s" "pattern: " swiper--format-spec)
193 #'swiper--update-input-ivy))
196 (goto-char swiper--opoint)
197 (swiper--action res ivy-text)))))
199 (defun swiper--ensure-visible ()
200 "Remove overlays hiding point."
201 (let ((overlays (overlays-at (point)))
203 (while (setq ov (pop overlays))
204 (if (and (invisible-p (overlay-get ov 'invisible))
205 (setq expose (overlay-get ov 'isearch-open-invisible)))
206 (funcall expose ov)))))
208 (defvar swiper--overlays nil
211 (defun swiper--cleanup ()
212 "Clean up the overlays."
213 (while swiper--overlays
214 (delete-overlay (pop swiper--overlays)))
216 (goto-char (point-min))
217 (isearch-clean-overlays)))
219 (defun swiper--update-input-ivy ()
220 "Called when `ivy' input is updated."
222 (let* ((re (ivy--regex ivy-text))
224 (num (if (string-match "^[0-9]+" str)
225 (string-to-number (match-string 0 str))
227 (with-selected-window swiper--window
228 (goto-char (point-min))
230 (goto-char (point-min))
231 (forward-line (1- num))
232 (isearch-range-invisible (line-beginning-position)
234 (unless (and (>= (point) (window-start))
235 (<= (point) (window-end swiper--window t)))
237 (let ((ov (make-overlay
238 (line-beginning-position)
239 (1+ (line-end-position)))))
240 (overlay-put ov 'face 'swiper-line-face)
241 (overlay-put ov 'window swiper--window)
242 (push ov swiper--overlays))
243 (let ((wh (window-height)))
244 (swiper--add-overlays
247 (forward-line (- wh))
253 (defun swiper--add-overlays (re beg end)
254 "Add overlays for RE regexp in current buffer between BEG and END."
255 (when (>= (length re) swiper-min-highlight)
258 ;; RE can become an invalid regexp
259 (while (and (ignore-errors (re-search-forward re end t))
260 (> (- (match-end 0) (match-beginning 0)) 0))
262 (while (<= i ivy--subexps)
263 (when (match-beginning i)
264 (let ((overlay (make-overlay (match-beginning i)
267 (cond ((zerop ivy--subexps)
268 (cl-caddr swiper-faces))
272 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
274 (push overlay swiper--overlays)
275 (overlay-put overlay 'face face)
276 (overlay-put overlay 'window swiper--window)
277 (overlay-put overlay 'priority i)))
280 (defun swiper--action (x input)
281 "Goto line X and search for INPUT."
283 (user-error "No candidates")
284 (goto-char (point-min))
285 (forward-line (1- (read x)))
287 (ivy--regex input) (line-end-position) t)
288 (swiper--ensure-visible)
289 (when (/= (point) swiper--opoint)
290 (unless (and transient-mark-mode mark-active)
291 (push-mark swiper--opoint t)
292 (message "Mark saved where search started")))))
296 ;;; swiper.el ends here