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