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