]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/swiper.el
Merge commit '3b78e0e503f4763f8a2d77eeacfc91213ec5532e' from swiper
[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.2.1
8 ;; Package-Requires: ((emacs "24.1") (ivy "0.2.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 (perform-replace from to
94 t t t)))
95 (swiper--cleanup)
96 (exit-minibuffer))))
97
98 (defun swiper-recenter-top-bottom (&optional arg)
99 "Call (`recenter-top-bottom' ARG) in `swiper--window'."
100 (interactive "P")
101 (with-selected-window swiper--window
102 (recenter-top-bottom arg)))
103
104 (defvar swiper--window nil
105 "Store the current window.")
106
107 (defun swiper-font-lock-ensure ()
108 "Ensure the entired buffer is highlighted."
109 (unless (or (derived-mode-p 'magit-mode)
110 (memq major-mode '(package-menu-mode
111 gnus-summary-mode
112 gnus-article-mode
113 gnus-group-mode
114 emms-playlist-mode erc-mode
115 org-agenda-mode)))
116 (if (fboundp 'font-lock-ensure)
117 (font-lock-ensure)
118 (font-lock-fontify-buffer))))
119
120 (defvar swiper--format-spec ""
121 "Store the current candidates format spec.")
122
123 (defun swiper--candidates ()
124 "Return a list of this buffer lines."
125 (let ((n-lines (count-lines (point-min) (point-max))))
126 (unless (zerop n-lines)
127 (setq swiper--format-spec
128 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
129 (let ((line-number 0)
130 candidates)
131 (save-excursion
132 (goto-char (point-min))
133 (swiper-font-lock-ensure)
134 (while (< (point) (point-max))
135 (push (format swiper--format-spec
136 (cl-incf line-number)
137 (buffer-substring
138 (line-beginning-position)
139 (line-end-position)))
140 candidates)
141 (forward-line 1))
142 (nreverse candidates))))))
143
144 (defvar swiper--opoint 1
145 "The point when `swiper' starts.")
146
147 ;;;###autoload
148 (defun swiper (&optional initial-input)
149 "`isearch' with an overview.
150 When non-nil, INITIAL-INPUT is the initial search pattern."
151 (interactive)
152 (swiper--ivy initial-input))
153
154 (defun swiper--init ()
155 "Perform initialization common to both completion methods."
156 (deactivate-mark)
157 (setq swiper--opoint (point))
158 (setq swiper--len 0)
159 (setq swiper--anchor (line-number-at-pos))
160 (setq swiper--window (selected-window)))
161
162 (defun swiper--ivy (&optional initial-input)
163 "`isearch' with an overview using `ivy'.
164 When non-nil, INITIAL-INPUT is the initial search pattern."
165 (interactive)
166 (ido-mode -1)
167 (swiper--init)
168 (let ((candidates (swiper--candidates))
169 (preselect (format
170 swiper--format-spec
171 (line-number-at-pos)
172 (regexp-quote
173 (buffer-substring-no-properties
174 (line-beginning-position)
175 (line-end-position)))))
176 res)
177 (unwind-protect
178 (setq res (ivy-read
179 (replace-regexp-in-string
180 "%s" "pattern: " swiper--format-spec)
181 candidates
182 initial-input
183 swiper-map
184 preselect
185 #'swiper--update-input-ivy))
186 (ido-mode 1)
187 (swiper--cleanup)
188 (if (null ivy-exit)
189 (goto-char swiper--opoint)
190 (swiper--action res ivy-text)))))
191
192 (defun swiper--ensure-visible ()
193 "Remove overlays hiding point."
194 (let ((overlays (overlays-at (point)))
195 ov expose)
196 (while (setq ov (pop overlays))
197 (if (and (invisible-p (overlay-get ov 'invisible))
198 (setq expose (overlay-get ov 'isearch-open-invisible)))
199 (funcall expose ov)))))
200
201 (defun swiper--cleanup ()
202 "Clean up the overlays."
203 (while swiper--overlays
204 (delete-overlay (pop swiper--overlays)))
205 (save-excursion
206 (goto-char (point-min))
207 (isearch-clean-overlays)))
208
209 (defvar swiper--overlays nil
210 "Store overlays.")
211
212 (defvar swiper--anchor nil
213 "A line number to which the search should be anchored.")
214
215 (defvar swiper--len 0
216 "The last length of input for which an anchoring was made.")
217
218 (defun swiper--update-input-ivy ()
219 "Called when `ivy' input is updated."
220 (swiper--cleanup)
221 (let* ((re (ivy--regex ivy-text))
222 (str ivy--current)
223 (num (if (string-match "^[0-9]+" str)
224 (string-to-number (match-string 0 str))
225 0)))
226 (with-selected-window swiper--window
227 (goto-char (point-min))
228 (when (cl-plusp num)
229 (goto-char (point-min))
230 (forward-line (1- num))
231 (isearch-range-invisible (line-beginning-position)
232 (line-end-position))
233 (unless (and (>= (point) (window-start))
234 (<= (point) (window-end swiper--window t)))
235 (recenter)))
236 (let ((ov (make-overlay
237 (line-beginning-position)
238 (1+ (line-end-position)))))
239 (overlay-put ov 'face 'swiper-line-face)
240 (overlay-put ov 'window swiper--window)
241 (push ov swiper--overlays))
242 (swiper--add-overlays
243 re
244 (window-start swiper--window)
245 (window-end swiper--window t)))))
246
247 (defun swiper--add-overlays (re beg end)
248 "Add overlays for RE regexp in current buffer between BEG and END."
249 (when (>= (length re) swiper-min-highlight)
250 (save-excursion
251 (goto-char beg)
252 ;; RE can become an invalid regexp
253 (while (and (ignore-errors (re-search-forward re end t))
254 (> (- (match-end 0) (match-beginning 0)) 0))
255 (let ((i 0))
256 (while (<= i ivy--subexps)
257 (when (match-beginning i)
258 (let ((overlay (make-overlay (match-beginning i)
259 (match-end i)))
260 (face
261 (cond ((zerop ivy--subexps)
262 (cl-caddr swiper-faces))
263 ((zerop i)
264 (car swiper-faces))
265 (t
266 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
267 swiper-faces)))))
268 (push overlay swiper--overlays)
269 (overlay-put overlay 'face face)
270 (overlay-put overlay 'window swiper--window)
271 (overlay-put overlay 'priority i)))
272 (cl-incf i)))))))
273
274 (defun swiper--action (x input)
275 "Goto line X and search for INPUT."
276 (if (null x)
277 (user-error "No candidates")
278 (goto-char (point-min))
279 (forward-line (1- (read x)))
280 (re-search-forward
281 (ivy--regex input) (line-end-position) t)
282 (swiper--ensure-visible)
283 (when (/= (point) swiper--opoint)
284 (unless (and transient-mark-mode mark-active)
285 (push-mark swiper--opoint t)
286 (message "Mark saved where search started")))))
287
288 (provide 'swiper)
289
290 ;;; swiper.el ends here