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