]> code.delx.au - gnu-emacs-elpa/blob - swiper.el
Rename avy-swiper to swiper-avy
[gnu-emacs-elpa] / 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 (define-key map (kbd "C-'") 'swiper-avy)
83 map)
84 "Keymap for swiper.")
85
86 (defun swiper-query-replace ()
87 "Start `query-replace' with string to replace from last search string."
88 (interactive)
89 (if (null (window-minibuffer-p))
90 (user-error "Should only be called in the minibuffer through `swiper-map'")
91 (let* ((enable-recursive-minibuffers t)
92 (from (ivy--regex ivy-text))
93 (to (query-replace-read-to from "Query replace" t)))
94 (delete-minibuffer-contents)
95 (ivy-set-action (lambda ()
96 (with-selected-window swiper--window
97 (perform-replace from to
98 t t nil))))
99 (swiper--cleanup)
100 (exit-minibuffer))))
101
102 (defvar avy-background)
103 (declare-function avy--regex-candidates "ext:avy")
104 (declare-function avy--process "ext:avy")
105 (declare-function avy--overlay-post "ext:avy")
106 (declare-function avy--goto "ext:avy")
107
108 ;;;###autoload
109 (defun swiper-avy ()
110 "Jump to one of the current swiper candidates."
111 (interactive)
112 (with-selected-window (ivy-state-window ivy-last)
113 (let* ((candidates
114 (avy--regex-candidates
115 (funcall ivy--regex-function ivy-text)))
116 (avy-background nil)
117 (candidate
118 (avy--process candidates #'avy--overlay-post)))
119 (ivy-quit-and-run
120 (avy--goto candidate)))))
121
122 (defvar swiper--window nil
123 "Store the current window.")
124
125 (defun swiper-recenter-top-bottom (&optional arg)
126 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
127 (interactive "P")
128 (with-selected-window swiper--window
129 (recenter-top-bottom arg)))
130
131 (defun swiper-font-lock-ensure ()
132 "Ensure the entired buffer is highlighted."
133 (unless (or (derived-mode-p 'magit-mode)
134 (memq major-mode '(package-menu-mode
135 gnus-summary-mode
136 gnus-article-mode
137 gnus-group-mode
138 emms-playlist-mode erc-mode
139 org-agenda-mode
140 dired-mode
141 jabber-chat-mode
142 elfeed-search-mode)))
143 (unless (> (buffer-size) 100000)
144 (if (fboundp 'font-lock-ensure)
145 (font-lock-ensure)
146 (with-no-warnings (font-lock-fontify-buffer))))))
147
148 (defvar swiper--format-spec ""
149 "Store the current candidates format spec.")
150
151 (defvar swiper--width nil
152 "Store the amount of digits needed for the longest line nubmer.")
153
154 (defun swiper--candidates ()
155 "Return a list of this buffer lines."
156 (let ((n-lines (count-lines (point-min) (point-max))))
157 (unless (zerop n-lines)
158 (setq swiper--width (1+ (floor (log n-lines 10))))
159 (setq swiper--format-spec
160 (format "%%-%dd %%s" swiper--width))
161 (let ((line-number 0)
162 candidates)
163 (save-excursion
164 (goto-char (point-min))
165 (swiper-font-lock-ensure)
166 (while (< (point) (point-max))
167 (push (format swiper--format-spec
168 (cl-incf line-number)
169 (buffer-substring
170 (line-beginning-position)
171 (line-end-position)))
172 candidates)
173 (forward-line 1))
174 (nreverse candidates))))))
175
176 (defvar swiper--opoint 1
177 "The point when `swiper' starts.")
178
179 ;;;###autoload
180 (defun swiper (&optional initial-input)
181 "`isearch' with an overview.
182 When non-nil, INITIAL-INPUT is the initial search pattern."
183 (interactive)
184 (swiper--ivy initial-input))
185
186 (defvar swiper--anchor nil
187 "A line number to which the search should be anchored.")
188
189 (defvar swiper--len 0
190 "The last length of input for which an anchoring was made.")
191
192 (defun swiper--init ()
193 "Perform initialization common to both completion methods."
194 (deactivate-mark)
195 (setq swiper--opoint (point))
196 (setq swiper--len 0)
197 (setq swiper--anchor (line-number-at-pos))
198 (setq swiper--window (selected-window))
199 (setq ivy--regex-function
200 (cdr (assoc t ivy-re-builders-alist))))
201
202 (defun swiper--ivy (&optional initial-input)
203 "`isearch' with an overview using `ivy'.
204 When non-nil, INITIAL-INPUT is the initial search pattern."
205 (interactive)
206 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
207 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
208 Please remove it and update the \"swiper\" package."))
209 (swiper--init)
210 (let ((candidates (swiper--candidates))
211 (preselect (format
212 swiper--format-spec
213 (line-number-at-pos)
214 (regexp-quote
215 (buffer-substring-no-properties
216 (line-beginning-position)
217 (line-end-position)))))
218 res)
219 (unwind-protect
220 (setq res (ivy-read
221 (replace-regexp-in-string
222 "%s" "pattern: " swiper--format-spec)
223 candidates
224 :initial-input initial-input
225 :keymap swiper-map
226 :preselect preselect
227 :require-match t
228 :update-fn #'swiper--update-input-ivy
229 :unwind #'swiper--cleanup))
230 (if (null ivy-exit)
231 (goto-char swiper--opoint)
232 (swiper--action res ivy-text)))))
233
234 (defun swiper--ensure-visible ()
235 "Remove overlays hiding point."
236 (let ((overlays (overlays-at (point)))
237 ov expose)
238 (while (setq ov (pop overlays))
239 (if (and (invisible-p (overlay-get ov 'invisible))
240 (setq expose (overlay-get ov 'isearch-open-invisible)))
241 (funcall expose ov)))))
242
243 (defvar swiper--overlays nil
244 "Store overlays.")
245
246 (defun swiper--cleanup ()
247 "Clean up the overlays."
248 (while swiper--overlays
249 (delete-overlay (pop swiper--overlays)))
250 (save-excursion
251 (goto-char (point-min))
252 (isearch-clean-overlays)))
253
254 (defun swiper--update-input-ivy ()
255 "Called when `ivy' input is updated."
256 (swiper--cleanup)
257 (let* ((re (funcall ivy--regex-function ivy-text))
258 (str ivy--current)
259 (num (if (string-match "^[0-9]+" str)
260 (string-to-number (match-string 0 str))
261 0)))
262 (with-selected-window swiper--window
263 (goto-char (point-min))
264 (when (cl-plusp num)
265 (goto-char (point-min))
266 (forward-line (1- num))
267 (isearch-range-invisible (line-beginning-position)
268 (line-end-position))
269 (unless (and (>= (point) (window-start))
270 (<= (point) (window-end swiper--window t)))
271 (recenter)))
272 (swiper--add-overlays re))))
273
274 (defun swiper--add-overlays (re &optional beg end)
275 "Add overlays for RE regexp in visible part of the current buffer.
276 BEG and END, when specified, are the point bounds."
277 (let ((ov (make-overlay
278 (line-beginning-position)
279 (1+ (line-end-position)))))
280 (overlay-put ov 'face 'swiper-line-face)
281 (overlay-put ov 'window swiper--window)
282 (push ov swiper--overlays))
283 (let* ((wh (window-height))
284 (beg (or beg (save-excursion
285 (forward-line (- wh))
286 (point))))
287 (end (or end (save-excursion
288 (forward-line wh)
289 (point)))))
290 (when (>= (length re) swiper-min-highlight)
291 (save-excursion
292 (goto-char beg)
293 ;; RE can become an invalid regexp
294 (while (and (ignore-errors (re-search-forward re end t))
295 (> (- (match-end 0) (match-beginning 0)) 0))
296 (let ((i 0))
297 (while (<= i ivy--subexps)
298 (when (match-beginning i)
299 (let ((overlay (make-overlay (match-beginning i)
300 (match-end i)))
301 (face
302 (cond ((zerop ivy--subexps)
303 (cadr swiper-faces))
304 ((zerop i)
305 (car swiper-faces))
306 (t
307 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
308 swiper-faces)))))
309 (push overlay swiper--overlays)
310 (overlay-put overlay 'face face)
311 (overlay-put overlay 'window swiper--window)
312 (overlay-put overlay 'priority i)))
313 (cl-incf i))))))))
314
315 (defun swiper--action (x input)
316 "Goto line X and search for INPUT."
317 (if (null x)
318 (user-error "No candidates")
319 (goto-char (point-min))
320 (forward-line (1- (read x)))
321 (re-search-forward
322 (funcall ivy--regex-function input) (line-end-position) t)
323 (swiper--ensure-visible)
324 (when (/= (point) swiper--opoint)
325 (unless (and transient-mark-mode mark-active)
326 (push-mark swiper--opoint t)
327 (message "Mark saved where search started")))))
328
329 (provide 'swiper)
330
331 ;;; swiper.el ends here