]> code.delx.au - gnu-emacs-elpa/blob - swiper.el
Remove 'field text property for twittering-mode
[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.6.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 (define-obsolete-face-alias 'swiper-minibuffer-match-face-1
65 'ivy-minibuffer-match-face-1 "0.6.0")
66
67 (define-obsolete-face-alias 'swiper-minibuffer-match-face-2
68 'ivy-minibuffer-match-face-2 "0.6.0")
69
70 (define-obsolete-face-alias 'swiper-minibuffer-match-face-3
71 'ivy-minibuffer-match-face-3 "0.6.0")
72
73 (define-obsolete-face-alias 'swiper-minibuffer-match-face-4
74 'ivy-minibuffer-match-face-4 "0.6.0")
75
76 (defface swiper-line-face
77 '((t (:inherit highlight)))
78 "Face for current `swiper' line.")
79
80 (defcustom swiper-faces '(swiper-match-face-1
81 swiper-match-face-2
82 swiper-match-face-3
83 swiper-match-face-4)
84 "List of `swiper' faces for group matches.")
85
86 (defcustom swiper-min-highlight 2
87 "Only highlight matches for regexps at least this long."
88 :type 'integer)
89
90 (defvar swiper-map
91 (let ((map (make-sparse-keymap)))
92 (define-key map (kbd "M-q") 'swiper-query-replace)
93 (define-key map (kbd "C-l") 'swiper-recenter-top-bottom)
94 (define-key map (kbd "C-'") 'swiper-avy)
95 (define-key map (kbd "C-7") 'swiper-mc)
96 (define-key map (kbd "C-c C-f") 'swiper-toggle-face-matching)
97 map)
98 "Keymap for swiper.")
99
100 (defun swiper-query-replace ()
101 "Start `query-replace' with string to replace from last search string."
102 (interactive)
103 (if (null (window-minibuffer-p))
104 (user-error "Should only be called in the minibuffer through `swiper-map'")
105 (let* ((enable-recursive-minibuffers t)
106 (from (ivy--regex ivy-text))
107 (to (query-replace-read-to from "Query replace" t)))
108 (swiper--cleanup)
109 (ivy-exit-with-action
110 (lambda (_)
111 (with-ivy-window
112 (move-beginning-of-line 1)
113 (perform-replace from to
114 t t nil)))))))
115
116 (defvar avy-background)
117 (defvar avy-all-windows)
118 (defvar avy-style)
119 (defvar avy-keys)
120 (declare-function avy--regex-candidates "ext:avy")
121 (declare-function avy--process "ext:avy")
122 (declare-function avy--overlay-post "ext:avy")
123 (declare-function avy-action-goto "ext:avy")
124 (declare-function avy--done "ext:avy")
125 (declare-function avy--make-backgrounds "ext:avy")
126 (declare-function avy-window-list "ext:avy")
127 (declare-function avy-read "ext:avy")
128 (declare-function avy-read-de-bruijn "ext:avy")
129 (declare-function avy-tree "ext:avy")
130 (declare-function avy-push-mark "ext:avy")
131 (declare-function avy--remove-leading-chars "ext:avy")
132
133 ;;;###autoload
134 (defun swiper-avy ()
135 "Jump to one of the current swiper candidates."
136 (interactive)
137 (unless (string= ivy-text "")
138 (let* ((avy-all-windows nil)
139 (candidates (append
140 (with-ivy-window
141 (avy--regex-candidates
142 (ivy--regex ivy-text)))
143 (save-excursion
144 (save-restriction
145 (narrow-to-region (window-start) (window-end))
146 (goto-char (point-min))
147 (forward-line)
148 (let ((cands))
149 (while (< (point) (point-max))
150 (push (cons (1+ (point))
151 (selected-window))
152 cands)
153 (forward-line))
154 cands)))))
155 (candidate (unwind-protect
156 (prog2
157 (avy--make-backgrounds
158 (append (avy-window-list)
159 (list (ivy-state-window ivy-last))))
160 (if (eq avy-style 'de-bruijn)
161 (avy-read-de-bruijn
162 candidates avy-keys)
163 (avy-read (avy-tree candidates avy-keys)
164 #'avy--overlay-post
165 #'avy--remove-leading-chars))
166 (avy-push-mark))
167 (avy--done))))
168 (if (window-minibuffer-p (cdr candidate))
169 (progn
170 (ivy-set-index (- (line-number-at-pos (car candidate)) 2))
171 (ivy--exhibit)
172 (ivy-done)
173 (ivy-call))
174 (ivy-quit-and-run
175 (avy-action-goto (caar candidate)))))))
176
177 (declare-function mc/create-fake-cursor-at-point "ext:multiple-cursors-core")
178 (declare-function multiple-cursors-mode "ext:multiple-cursors-core")
179
180 ;;;###autoload
181 (defun swiper-mc ()
182 (interactive)
183 (unless (require 'multiple-cursors nil t)
184 (error "multiple-cursors isn't installed"))
185 (let ((cands (nreverse ivy--old-cands)))
186 (unless (string= ivy-text "")
187 (ivy-exit-with-action
188 (lambda (_)
189 (let (cand)
190 (while (setq cand (pop cands))
191 (swiper--action cand)
192 (when cands
193 (mc/create-fake-cursor-at-point))))
194 (multiple-cursors-mode 1))))))
195
196 (defun swiper-recenter-top-bottom (&optional arg)
197 "Call (`recenter-top-bottom' ARG)."
198 (interactive "P")
199 (with-ivy-window
200 (recenter-top-bottom arg)))
201
202 (defun swiper-font-lock-ensure ()
203 "Ensure the entired buffer is highlighted."
204 (unless (or (derived-mode-p 'magit-mode)
205 (bound-and-true-p magit-blame-mode)
206 (memq major-mode '(package-menu-mode
207 gnus-summary-mode
208 gnus-article-mode
209 gnus-group-mode
210 emms-playlist-mode
211 emms-stream-mode
212 erc-mode
213 org-agenda-mode
214 dired-mode
215 jabber-chat-mode
216 elfeed-search-mode
217 elfeed-show-mode
218 fundamental-mode
219 Man-mode
220 woman-mode
221 mu4e-view-mode
222 mu4e-headers-mode
223 help-mode
224 debbugs-gnu-mode
225 occur-mode
226 occur-edit-mode
227 bongo-mode
228 eww-mode
229 twittering-mode
230 w3m-mode)))
231 (unless (> (buffer-size) 100000)
232 (if (fboundp 'font-lock-ensure)
233 (font-lock-ensure)
234 (with-no-warnings (font-lock-fontify-buffer))))))
235
236 (defvar swiper--format-spec ""
237 "Store the current candidates format spec.")
238
239 (defvar swiper--width nil
240 "Store the amount of digits needed for the longest line nubmer.")
241
242 (defvar swiper-use-visual-line nil
243 "When non-nil, use `line-move' instead of `forward-line'.")
244
245 (declare-function outline-show-all "outline")
246
247 (defun swiper--candidates (&optional numbers-width)
248 "Return a list of this buffer lines.
249
250 NUMBERS-WIDTH, when specified, is used for line numbers width
251 spec, instead of calculating it as the log of the buffer line
252 count."
253 (if (and visual-line-mode
254 ;; super-slow otherwise
255 (< (buffer-size) 20000))
256 (progn
257 (when (eq major-mode 'org-mode)
258 (require 'outline)
259 (outline-show-all))
260 (setq swiper-use-visual-line t))
261 (setq swiper-use-visual-line nil))
262 (let ((n-lines (count-lines (point-min) (point-max))))
263 (unless (zerop n-lines)
264 (setq swiper--width (or numbers-width
265 (1+ (floor (log n-lines 10)))))
266 (setq swiper--format-spec
267 (format "%%-%dd " swiper--width))
268 (let ((line-number 0)
269 (advancer (if swiper-use-visual-line
270 (lambda (arg) (line-move arg t))
271 #'forward-line))
272 candidates)
273 (save-excursion
274 (goto-char (point-min))
275 (swiper-font-lock-ensure)
276 (while (< (point) (point-max))
277 (let ((str (concat
278 " "
279 (replace-regexp-in-string
280 "\t" " "
281 (buffer-substring
282 (point)
283 (if swiper-use-visual-line
284 (save-excursion
285 (end-of-visual-line)
286 (point))
287 (line-end-position)))))))
288 (when (eq major-mode 'twittering-mode)
289 (remove-text-properties 0 (length str) '(field) str))
290 (put-text-property 0 1 'display
291 (format swiper--format-spec
292 (cl-incf line-number))
293 str)
294 (push str candidates))
295 (funcall advancer 1))
296 (nreverse candidates))))))
297
298 (defvar swiper--opoint 1
299 "The point when `swiper' starts.")
300
301 ;;;###autoload
302 (defun swiper (&optional initial-input)
303 "`isearch' with an overview.
304 When non-nil, INITIAL-INPUT is the initial search pattern."
305 (interactive)
306 (swiper--ivy initial-input))
307
308 (declare-function evil-jumper--set-jump "ext:evil-jumper")
309
310 (defun swiper--init ()
311 "Perform initialization common to both completion methods."
312 (setq swiper--opoint (point))
313 (when (bound-and-true-p evil-jumper-mode)
314 (evil-jumper--set-jump)))
315
316 (defun swiper--re-builder (str)
317 "Transform STR into a swiper regex.
318 This is the regex used in the minibuffer, since the candidates
319 there have line numbers. In the buffer, `ivy--regex' should be used."
320 (cond
321 ((equal str "")
322 "")
323 ((equal str "^")
324 (setq ivy--subexps 0)
325 ".")
326 ((string-match "^\\^" str)
327 (setq ivy--old-re "")
328 (let ((re (ivy--regex-plus (substring str 1))))
329 (if (zerop ivy--subexps)
330 (prog1 (format "^ ?\\(%s\\)" re)
331 (setq ivy--subexps 1))
332 (format "^ %s" re))))
333 (t
334 (ivy--regex-plus str))))
335
336 (defvar swiper-history nil
337 "History for `swiper'.")
338
339 (defvar swiper-invocation-face nil
340 "The face at the point of invocation of `swiper'.")
341
342 (defun swiper--ivy (&optional initial-input)
343 "`isearch' with an overview using `ivy'.
344 When non-nil, INITIAL-INPUT is the initial search pattern."
345 (interactive)
346 (swiper--init)
347 (setq swiper-invocation-face
348 (plist-get (text-properties-at (point)) 'face))
349 (let ((candidates (swiper--candidates))
350 (preselect
351 (if swiper-use-visual-line
352 (count-screen-lines
353 (point-min)
354 (save-excursion (beginning-of-visual-line) (point)))
355 (1- (line-number-at-pos))))
356 (minibuffer-allow-text-properties t))
357 (unwind-protect
358 (ivy-read
359 "Swiper: "
360 candidates
361 :initial-input initial-input
362 :keymap swiper-map
363 :preselect preselect
364 :require-match t
365 :update-fn #'swiper--update-input-ivy
366 :unwind #'swiper--cleanup
367 :action #'swiper--action
368 :re-builder #'swiper--re-builder
369 :history 'swiper-history
370 :caller 'swiper)
371 (when (null ivy-exit)
372 (goto-char swiper--opoint)))))
373
374 (defun swiper-toggle-face-matching ()
375 "Toggle matching only the candidates with `swiper-invocation-face'."
376 (interactive)
377 (setf (ivy-state-matcher ivy-last)
378 (if (ivy-state-matcher ivy-last)
379 nil
380 #'swiper--face-matcher))
381 (setq ivy--old-re nil))
382
383 (defun swiper--face-matcher (regexp candidates)
384 "Return REGEXP-matching CANDIDATES.
385 Matched candidates should have `swiper-invocation-face'."
386 (cl-remove-if-not
387 (lambda (x)
388 (and
389 (string-match regexp x)
390 (let ((s (match-string 0 x))
391 (i 0))
392 (while (and (< i (length s))
393 (text-property-any
394 i (1+ i)
395 'face swiper-invocation-face
396 s))
397 (cl-incf i))
398 (eq i (length s)))))
399 candidates))
400
401 (defun swiper--ensure-visible ()
402 "Remove overlays hiding point."
403 (let ((overlays (overlays-at (point)))
404 ov expose)
405 (while (setq ov (pop overlays))
406 (if (and (invisible-p (overlay-get ov 'invisible))
407 (setq expose (overlay-get ov 'isearch-open-invisible)))
408 (funcall expose ov)))))
409
410 (defvar swiper--overlays nil
411 "Store overlays.")
412
413 (defun swiper--cleanup ()
414 "Clean up the overlays."
415 (while swiper--overlays
416 (delete-overlay (pop swiper--overlays)))
417 (save-excursion
418 (goto-char (point-min))
419 (isearch-clean-overlays)))
420
421 (defun swiper--update-input-ivy ()
422 "Called when `ivy' input is updated."
423 (with-ivy-window
424 (swiper--cleanup)
425 (when (> (length ivy--current) 0)
426 (let* ((re (funcall ivy--regex-function ivy-text))
427 (re (if (stringp re) re (caar re)))
428 (str (get-text-property 0 'display ivy--current))
429 (num (if (string-match "^[0-9]+" str)
430 (string-to-number (match-string 0 str))
431 0)))
432 (goto-char (point-min))
433 (when (cl-plusp num)
434 (goto-char (point-min))
435 (if swiper-use-visual-line
436 (line-move (1- num))
437 (forward-line (1- num)))
438 (if (and (equal ivy-text "")
439 (>= swiper--opoint (line-beginning-position))
440 (<= swiper--opoint (line-end-position)))
441 (goto-char swiper--opoint)
442 (re-search-forward re (line-end-position) t))
443 (isearch-range-invisible (line-beginning-position)
444 (line-end-position))
445 (unless (and (>= (point) (window-start))
446 (<= (point) (window-end (ivy-state-window ivy-last) t)))
447 (recenter)))
448 (swiper--add-overlays re)))))
449
450 (defun swiper--add-overlays (re &optional beg end wnd)
451 "Add overlays for RE regexp in visible part of the current buffer.
452 BEG and END, when specified, are the point bounds.
453 WND, when specified is the window."
454 (setq wnd (or wnd (ivy-state-window ivy-last)))
455 (let ((ov (if visual-line-mode
456 (make-overlay
457 (save-excursion
458 (beginning-of-visual-line)
459 (point))
460 (save-excursion
461 (end-of-visual-line)
462 (point)))
463 (make-overlay
464 (line-beginning-position)
465 (1+ (line-end-position))))))
466 (overlay-put ov 'face 'swiper-line-face)
467 (overlay-put ov 'window wnd)
468 (push ov swiper--overlays)
469 (let* ((wh (window-height))
470 (beg (or beg (save-excursion
471 (forward-line (- wh))
472 (point))))
473 (end (or end (save-excursion
474 (forward-line wh)
475 (point)))))
476 (when (>= (length re) swiper-min-highlight)
477 (save-excursion
478 (goto-char beg)
479 ;; RE can become an invalid regexp
480 (while (and (ignore-errors (re-search-forward re end t))
481 (> (- (match-end 0) (match-beginning 0)) 0))
482 (let ((i 0))
483 (while (<= i ivy--subexps)
484 (when (match-beginning i)
485 (let ((overlay (make-overlay (match-beginning i)
486 (match-end i)))
487 (face
488 (cond ((zerop ivy--subexps)
489 (cadr swiper-faces))
490 ((zerop i)
491 (car swiper-faces))
492 (t
493 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
494 swiper-faces)))))
495 (push overlay swiper--overlays)
496 (overlay-put overlay 'face face)
497 (overlay-put overlay 'window wnd)
498 (overlay-put overlay 'priority i)))
499 (cl-incf i)))))))))
500
501 (defun swiper--action (x)
502 "Goto line X."
503 (if (null x)
504 (user-error "No candidates")
505 (with-ivy-window
506 (unless (equal (current-buffer)
507 (ivy-state-buffer ivy-last))
508 (switch-to-buffer (ivy-state-buffer ivy-last)))
509 (goto-char (point-min))
510 (funcall (if swiper-use-visual-line
511 #'line-move
512 #'forward-line)
513 (1- (read (get-text-property 0 'display x))))
514 (re-search-forward
515 (ivy--regex ivy-text) (line-end-position) t)
516 (swiper--ensure-visible)
517 (when (/= (point) swiper--opoint)
518 (unless (and transient-mark-mode mark-active)
519 (when (eq ivy-exit 'done)
520 (push-mark swiper--opoint t)
521 (message "Mark saved where search started")))))))
522
523 ;; (define-key isearch-mode-map (kbd "C-o") 'swiper-from-isearch)
524 (defun swiper-from-isearch ()
525 "Invoke `swiper' from isearch."
526 (interactive)
527 (let ((query (if isearch-regexp
528 isearch-string
529 (regexp-quote isearch-string))))
530 (isearch-exit)
531 (swiper query)))
532
533 (defvar swiper-multi-buffers nil
534 "Store the current list of buffers.")
535
536 (defvar swiper-multi-candidates nil
537 "Store the list of candidates for `swiper-multi'.")
538
539 (defun swiper-multi-prompt ()
540 (format "Buffers (%s): "
541 (mapconcat #'identity swiper-multi-buffers ", ")))
542
543 (defun swiper-multi ()
544 "Select one or more buffers.
545 Run `swiper' for those buffers."
546 (interactive)
547 (setq swiper-multi-buffers nil)
548 (ivy-read (swiper-multi-prompt)
549 'internal-complete-buffer
550 :action 'swiper-multi-action-1)
551 (ivy-read "Swiper: " swiper-multi-candidates
552 :action 'swiper-multi-action-2
553 :unwind #'swiper--cleanup
554 :caller 'swiper-multi))
555
556 (defun swiper-all ()
557 "Run `swiper' for all opened buffers."
558 (interactive)
559 (ivy-read "Swiper: " (swiper--multi-candidates
560 (cl-remove-if-not
561 #'buffer-file-name
562 (buffer-list)))
563 :action 'swiper-multi-action-2
564 :unwind #'swiper--cleanup
565 :caller 'swiper-multi))
566
567 (defun swiper--multi-candidates (buffers)
568 (let* ((ww (window-width))
569 (res nil)
570 (column-2 (apply #'max
571 (mapcar
572 (lambda (b)
573 (length (buffer-name b)))
574 buffers)))
575 (column-1 (- ww 4 column-2 1)))
576 (dolist (buf buffers)
577 (with-current-buffer buf
578 (setq res
579 (append
580 (mapcar
581 (lambda (s)
582 (setq s (concat (ivy--truncate-string s column-1) " "))
583 (let ((len (length s)))
584 (put-text-property
585 (1- len) len 'display
586 (concat
587 (make-string
588 (- ww (string-width s) (length (buffer-name)) 3)
589 ?\ )
590 (buffer-name))
591 s)
592 s))
593 (swiper--candidates 4))
594 res))
595 nil))
596 res))
597
598 (defun swiper-multi-action-1 (x)
599 (if (member x swiper-multi-buffers)
600 (progn
601 (setq swiper-multi-buffers (delete x swiper-multi-buffers)))
602 (unless (equal x "")
603 (setq swiper-multi-buffers (append swiper-multi-buffers (list x)))))
604 (let ((prompt (swiper-multi-prompt)))
605 (setf (ivy-state-prompt ivy-last) prompt)
606 (setq ivy--prompt (concat "%-4d " prompt)))
607 (cond ((memq this-command '(ivy-done
608 ivy-alt-done
609 ivy-immediate-done))
610 (setq swiper-multi-candidates
611 (swiper--multi-candidates
612 (mapcar #'get-buffer swiper-multi-buffers))))
613 ((eq this-command 'ivy-call)
614 (delete-minibuffer-contents))))
615
616 (defun swiper-multi-action-2 (x)
617 (let ((buf-space (get-text-property (1- (length x)) 'display x)))
618 (with-ivy-window
619 (when (string-match "\\` *\\([^ ]+\\)\\'" buf-space)
620 (switch-to-buffer (match-string 1 buf-space))
621 (goto-char (point-min))
622 (forward-line (1- (read (get-text-property 0 'display x))))
623 (re-search-forward
624 (ivy--regex ivy-text)
625 (line-end-position) t)
626 (unless (eq ivy-exit 'done)
627 (swiper--cleanup)
628 (swiper--add-overlays (ivy--regex ivy-text)))))))
629
630 (provide 'swiper)
631
632 ;;; swiper.el ends here