]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/swiper.el
Merge commit '199c52606dcd614cb856bbcaca13b5fada0772b6' from avy
[gnu-emacs-elpa] / packages / swiper / swiper.el
1 ;;; swiper.el --- Isearch with an overview. Oh, man! -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
7 ;; Version: 0.4.0
8 ;; Package-Requires: ((emacs "24.1"))
9 ;; Keywords: matching
10
11 ;; This file is part of GNU Emacs.
12
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)
16 ;; any later version.
17
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.
22
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
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.
31 ;;
32 ;; It can double as a quick `regex-builder', although only single
33 ;; lines will be matched.
34 ;;
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).
39
40 ;;; Code:
41 (require 'ivy)
42
43 (defgroup swiper nil
44 "`isearch' with an overview."
45 :group 'matching
46 :prefix "swiper-")
47
48 (defface swiper-match-face-1
49 '((t (:inherit isearch-lazy-highlight-face)))
50 "The background face for `swiper' matches.")
51
52 (defface swiper-match-face-2
53 '((t (:inherit isearch)))
54 "Face for `swiper' matches modulo 1.")
55
56 (defface swiper-match-face-3
57 '((t (:inherit match)))
58 "Face for `swiper' matches modulo 2.")
59
60 (defface swiper-match-face-4
61 '((t (:inherit isearch-fail)))
62 "Face for `swiper' matches modulo 3.")
63
64 (defface swiper-line-face
65 '((t (:inherit highlight)))
66 "Face for current `swiper' line.")
67
68 (defcustom swiper-faces '(swiper-match-face-1
69 swiper-match-face-2
70 swiper-match-face-3
71 swiper-match-face-4)
72 "List of `swiper' faces for group matches.")
73
74 (defcustom swiper-min-highlight 2
75 "Only highlight matches for regexps at least this long."
76 :type 'integer)
77
78 (defvar swiper-map
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)
82 map)
83 "Keymap for swiper.")
84
85 (defun swiper-query-replace ()
86 "Start `query-replace' with string to replace from last search string."
87 (interactive)
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
97 t t nil))))
98 (swiper--cleanup)
99 (exit-minibuffer))))
100
101 (defvar swiper--window nil
102 "Store the current window.")
103
104 (defun swiper-recenter-top-bottom (&optional arg)
105 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
106 (interactive "P")
107 (with-selected-window swiper--window
108 (recenter-top-bottom arg)))
109
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
114 gnus-summary-mode
115 gnus-article-mode
116 gnus-group-mode
117 emms-playlist-mode erc-mode
118 org-agenda-mode
119 dired-mode
120 jabber-chat-mode
121 elfeed-search-mode)))
122 (unless (> (buffer-size) 100000)
123 (if (fboundp 'font-lock-ensure)
124 (font-lock-ensure)
125 (with-no-warnings (font-lock-fontify-buffer))))))
126
127 (defvar swiper--format-spec ""
128 "Store the current candidates format spec.")
129
130 (defun swiper--candidates ()
131 "Return a list of this buffer lines."
132 (let ((n-lines (count-lines (point-min) (point-max))))
133 (unless (zerop n-lines)
134 (setq swiper--format-spec
135 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
136 (let ((line-number 0)
137 candidates)
138 (save-excursion
139 (goto-char (point-min))
140 (swiper-font-lock-ensure)
141 (while (< (point) (point-max))
142 (push (format swiper--format-spec
143 (cl-incf line-number)
144 (buffer-substring
145 (line-beginning-position)
146 (line-end-position)))
147 candidates)
148 (forward-line 1))
149 (nreverse candidates))))))
150
151 (defvar swiper--opoint 1
152 "The point when `swiper' starts.")
153
154 ;;;###autoload
155 (defun swiper (&optional initial-input)
156 "`isearch' with an overview.
157 When non-nil, INITIAL-INPUT is the initial search pattern."
158 (interactive)
159 (swiper--ivy initial-input))
160
161 (defvar swiper--anchor nil
162 "A line number to which the search should be anchored.")
163
164 (defvar swiper--len 0
165 "The last length of input for which an anchoring was made.")
166
167 (defun swiper--init ()
168 "Perform initialization common to both completion methods."
169 (deactivate-mark)
170 (setq swiper--opoint (point))
171 (setq swiper--len 0)
172 (setq swiper--anchor (line-number-at-pos))
173 (setq swiper--window (selected-window)))
174
175 (defun swiper--ivy (&optional initial-input)
176 "`isearch' with an overview using `ivy'.
177 When non-nil, INITIAL-INPUT is the initial search pattern."
178 (interactive)
179 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
180 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
181 Please remove it and update the \"swiper\" package."))
182 (swiper--init)
183 (let ((candidates (swiper--candidates))
184 (preselect (format
185 swiper--format-spec
186 (line-number-at-pos)
187 (regexp-quote
188 (buffer-substring-no-properties
189 (line-beginning-position)
190 (line-end-position)))))
191 res)
192 (unwind-protect
193 (setq res (ivy-read
194 (replace-regexp-in-string
195 "%s" "pattern: " swiper--format-spec)
196 candidates
197 :initial-input initial-input
198 :keymap swiper-map
199 :preselect preselect
200 :require-match t
201 :update-fn #'swiper--update-input-ivy
202 :unwind #'swiper--cleanup))
203 (if (null ivy-exit)
204 (goto-char swiper--opoint)
205 (swiper--action res ivy-text)))))
206
207 (defun swiper--ensure-visible ()
208 "Remove overlays hiding point."
209 (let ((overlays (overlays-at (point)))
210 ov expose)
211 (while (setq ov (pop overlays))
212 (if (and (invisible-p (overlay-get ov 'invisible))
213 (setq expose (overlay-get ov 'isearch-open-invisible)))
214 (funcall expose ov)))))
215
216 (defvar swiper--overlays nil
217 "Store overlays.")
218
219 (defun swiper--cleanup ()
220 "Clean up the overlays."
221 (while swiper--overlays
222 (delete-overlay (pop swiper--overlays)))
223 (save-excursion
224 (goto-char (point-min))
225 (isearch-clean-overlays)))
226
227 (defun swiper--update-input-ivy ()
228 "Called when `ivy' input is updated."
229 (swiper--cleanup)
230 (let* ((re (ivy--regex ivy-text))
231 (str ivy--current)
232 (num (if (string-match "^[0-9]+" str)
233 (string-to-number (match-string 0 str))
234 0)))
235 (with-selected-window swiper--window
236 (goto-char (point-min))
237 (when (cl-plusp num)
238 (goto-char (point-min))
239 (forward-line (1- num))
240 (isearch-range-invisible (line-beginning-position)
241 (line-end-position))
242 (unless (and (>= (point) (window-start))
243 (<= (point) (window-end swiper--window t)))
244 (recenter)))
245 (swiper--add-overlays re))))
246
247 (defun swiper--add-overlays (re &optional beg end)
248 "Add overlays for RE regexp in visible part of the current buffer.
249 BEG and END, when specified, are the point bounds."
250 (let ((ov (make-overlay
251 (line-beginning-position)
252 (1+ (line-end-position)))))
253 (overlay-put ov 'face 'swiper-line-face)
254 (overlay-put ov 'window swiper--window)
255 (push ov swiper--overlays))
256 (let* ((wh (window-height))
257 (beg (or beg (save-excursion
258 (forward-line (- wh))
259 (point))))
260 (end (or end (save-excursion
261 (forward-line wh)
262 (point)))))
263 (when (>= (length re) swiper-min-highlight)
264 (save-excursion
265 (goto-char beg)
266 ;; RE can become an invalid regexp
267 (while (and (ignore-errors (re-search-forward re end t))
268 (> (- (match-end 0) (match-beginning 0)) 0))
269 (let ((i 0))
270 (while (<= i ivy--subexps)
271 (when (match-beginning i)
272 (let ((overlay (make-overlay (match-beginning i)
273 (match-end i)))
274 (face
275 (cond ((zerop ivy--subexps)
276 (cadr swiper-faces))
277 ((zerop i)
278 (car swiper-faces))
279 (t
280 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
281 swiper-faces)))))
282 (push overlay swiper--overlays)
283 (overlay-put overlay 'face face)
284 (overlay-put overlay 'window swiper--window)
285 (overlay-put overlay 'priority i)))
286 (cl-incf i))))))))
287
288 (defun swiper--action (x input)
289 "Goto line X and search for INPUT."
290 (if (null x)
291 (user-error "No candidates")
292 (goto-char (point-min))
293 (forward-line (1- (read x)))
294 (re-search-forward
295 (ivy--regex input) (line-end-position) t)
296 (swiper--ensure-visible)
297 (when (/= (point) swiper--opoint)
298 (unless (and transient-mark-mode mark-active)
299 (push-mark swiper--opoint t)
300 (message "Mark saved where search started")))))
301
302 (provide 'swiper)
303
304 ;;; swiper.el ends here