]> code.delx.au - gnu-emacs-elpa/blob - swiper.el
Allow to use "^" in swiper
[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 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 (defvar swiper--width nil
131 "Store the amount of digits needed for the longest line nubmer.")
132
133 (defun swiper--candidates ()
134 "Return a list of this buffer lines."
135 (let ((n-lines (count-lines (point-min) (point-max))))
136 (unless (zerop n-lines)
137 (setq swiper--width (1+ (floor (log n-lines 10))))
138 (setq swiper--format-spec
139 (format "%%-%dd %%s" swiper--width))
140 (let ((line-number 0)
141 candidates)
142 (save-excursion
143 (goto-char (point-min))
144 (swiper-font-lock-ensure)
145 (while (< (point) (point-max))
146 (push (format swiper--format-spec
147 (cl-incf line-number)
148 (buffer-substring
149 (line-beginning-position)
150 (line-end-position)))
151 candidates)
152 (forward-line 1))
153 (nreverse candidates))))))
154
155 (defvar swiper--opoint 1
156 "The point when `swiper' starts.")
157
158 ;;;###autoload
159 (defun swiper (&optional initial-input)
160 "`isearch' with an overview.
161 When non-nil, INITIAL-INPUT is the initial search pattern."
162 (interactive)
163 (swiper--ivy initial-input))
164
165 (defvar swiper--anchor nil
166 "A line number to which the search should be anchored.")
167
168 (defvar swiper--len 0
169 "The last length of input for which an anchoring was made.")
170
171 (defun swiper--init ()
172 "Perform initialization common to both completion methods."
173 (deactivate-mark)
174 (setq swiper--opoint (point))
175 (setq swiper--len 0)
176 (setq swiper--anchor (line-number-at-pos))
177 (setq swiper--window (selected-window))
178 (setq ivy--regex-function
179 (cdr (assoc t ivy-re-builders-alist))))
180
181 (defun swiper--ivy (&optional initial-input)
182 "`isearch' with an overview using `ivy'.
183 When non-nil, INITIAL-INPUT is the initial search pattern."
184 (interactive)
185 (unless (eq (length (help-function-arglist 'ivy-read)) 4)
186 (warn "You seem to be using the outdated stand-alone \"ivy\" package.
187 Please remove it and update the \"swiper\" package."))
188 (swiper--init)
189 (let ((candidates (swiper--candidates))
190 (preselect (format
191 swiper--format-spec
192 (line-number-at-pos)
193 (regexp-quote
194 (buffer-substring-no-properties
195 (line-beginning-position)
196 (line-end-position)))))
197 res)
198 (unwind-protect
199 (setq res (ivy-read
200 (replace-regexp-in-string
201 "%s" "pattern: " swiper--format-spec)
202 candidates
203 :initial-input initial-input
204 :keymap swiper-map
205 :preselect preselect
206 :require-match t
207 :update-fn #'swiper--update-input-ivy
208 :unwind #'swiper--cleanup))
209 (if (null ivy-exit)
210 (goto-char swiper--opoint)
211 (swiper--action res ivy-text)))))
212
213 (defun swiper--ensure-visible ()
214 "Remove overlays hiding point."
215 (let ((overlays (overlays-at (point)))
216 ov expose)
217 (while (setq ov (pop overlays))
218 (if (and (invisible-p (overlay-get ov 'invisible))
219 (setq expose (overlay-get ov 'isearch-open-invisible)))
220 (funcall expose ov)))))
221
222 (defvar swiper--overlays nil
223 "Store overlays.")
224
225 (defun swiper--cleanup ()
226 "Clean up the overlays."
227 (while swiper--overlays
228 (delete-overlay (pop swiper--overlays)))
229 (save-excursion
230 (goto-char (point-min))
231 (isearch-clean-overlays)))
232
233 (defun swiper--update-input-ivy ()
234 "Called when `ivy' input is updated."
235 (swiper--cleanup)
236 (let* ((re (funcall ivy--regex-function ivy-text))
237 (str ivy--current)
238 (num (if (string-match "^[0-9]+" str)
239 (string-to-number (match-string 0 str))
240 0)))
241 (with-selected-window swiper--window
242 (goto-char (point-min))
243 (when (cl-plusp num)
244 (goto-char (point-min))
245 (forward-line (1- num))
246 (isearch-range-invisible (line-beginning-position)
247 (line-end-position))
248 (unless (and (>= (point) (window-start))
249 (<= (point) (window-end swiper--window t)))
250 (recenter)))
251 (swiper--add-overlays re))))
252
253 (defun swiper--add-overlays (re &optional beg end)
254 "Add overlays for RE regexp in visible part of the current buffer.
255 BEG and END, when specified, are the point bounds."
256 (let ((ov (make-overlay
257 (line-beginning-position)
258 (1+ (line-end-position)))))
259 (overlay-put ov 'face 'swiper-line-face)
260 (overlay-put ov 'window swiper--window)
261 (push ov swiper--overlays))
262 (let* ((wh (window-height))
263 (beg (or beg (save-excursion
264 (forward-line (- wh))
265 (point))))
266 (end (or end (save-excursion
267 (forward-line wh)
268 (point)))))
269 (when (>= (length re) swiper-min-highlight)
270 (save-excursion
271 (goto-char beg)
272 ;; RE can become an invalid regexp
273 (while (and (ignore-errors (re-search-forward re end t))
274 (> (- (match-end 0) (match-beginning 0)) 0))
275 (let ((i 0))
276 (while (<= i ivy--subexps)
277 (when (match-beginning i)
278 (let ((overlay (make-overlay (match-beginning i)
279 (match-end i)))
280 (face
281 (cond ((zerop ivy--subexps)
282 (cadr swiper-faces))
283 ((zerop i)
284 (car swiper-faces))
285 (t
286 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
287 swiper-faces)))))
288 (push overlay swiper--overlays)
289 (overlay-put overlay 'face face)
290 (overlay-put overlay 'window swiper--window)
291 (overlay-put overlay 'priority i)))
292 (cl-incf i))))))))
293
294 (defun swiper--action (x input)
295 "Goto line X and search for INPUT."
296 (if (null x)
297 (user-error "No candidates")
298 (goto-char (point-min))
299 (forward-line (1- (read x)))
300 (re-search-forward
301 (funcall ivy--regex-function input) (line-end-position) t)
302 (swiper--ensure-visible)
303 (when (/= (point) swiper--opoint)
304 (unless (and transient-mark-mode mark-active)
305 (push-mark swiper--opoint t)
306 (message "Mark saved where search started")))))
307
308 (provide 'swiper)
309
310 ;;; swiper.el ends here