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