1 ;;; swiper.el --- Isearch with an overview. Oh, man! -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((emacs "24.1"))
11 ;; This file is part of GNU Emacs.
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)
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.
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
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.
32 ;; It can double as a quick `regex-builder', although only single
33 ;; lines will be matched.
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).
44 "`isearch' with an overview."
48 (defface swiper-match-face-1
49 '((t (:inherit isearch-lazy-highlight-face)))
50 "The background face for `swiper' matches.")
52 (defface swiper-match-face-2
53 '((t (:inherit isearch)))
54 "Face for `swiper' matches modulo 1.")
56 (defface swiper-match-face-3
57 '((t (:inherit match)))
58 "Face for `swiper' matches modulo 2.")
60 (defface swiper-match-face-4
61 '((t (:inherit isearch-fail)))
62 "Face for `swiper' matches modulo 3.")
64 (defface swiper-line-face
65 '((t (:inherit highlight)))
66 "Face for current `swiper' line.")
68 (defcustom swiper-faces '(swiper-match-face-1
72 "List of `swiper' faces for group matches.")
74 (defcustom swiper-min-highlight 2
75 "Only highlight matches for regexps at least this long."
79 (let ((map (make-sparse-keymap)))
80 (define-key map (kbd "M-q") 'swiper-query-replace)
81 (define-key map (kbd "C-l") 'swiper-recenter-top-bottom)
82 (define-key map (kbd "C-'") 'swiper-avy)
83 (define-key map (kbd "C-7") 'swiper-mc)
84 (define-key map (kbd "C-c C-f") 'swiper-toggle-face-matching)
88 (defun swiper-query-replace ()
89 "Start `query-replace' with string to replace from last search string."
91 (if (null (window-minibuffer-p))
92 (user-error "Should only be called in the minibuffer through `swiper-map'")
93 (let* ((enable-recursive-minibuffers t)
94 (from (ivy--regex ivy-text))
95 (to (query-replace-read-to from "Query replace" t)))
100 (move-beginning-of-line 1)
101 (perform-replace from to
104 (defvar avy-background)
105 (defvar avy-all-windows)
108 (declare-function avy--regex-candidates "ext:avy")
109 (declare-function avy--process "ext:avy")
110 (declare-function avy--overlay-post "ext:avy")
111 (declare-function avy-action-goto "ext:avy")
112 (declare-function avy--done "ext:avy")
113 (declare-function avy--make-backgrounds "ext:avy")
114 (declare-function avy-window-list "ext:avy")
115 (declare-function avy-read "ext:avy")
116 (declare-function avy-read-de-bruijn "ext:avy")
117 (declare-function avy-tree "ext:avy")
118 (declare-function avy-push-mark "ext:avy")
119 (declare-function avy--remove-leading-chars "ext:avy")
123 "Jump to one of the current swiper candidates."
125 (unless (string= ivy-text "")
126 (let* ((avy-all-windows nil)
129 (avy--regex-candidates
130 (ivy--regex ivy-text)))
133 (narrow-to-region (window-start) (window-end))
134 (goto-char (point-min))
137 (while (< (point) (point-max))
138 (push (cons (1+ (point))
143 (candidate (unwind-protect
145 (avy--make-backgrounds
146 (append (avy-window-list)
147 (list (ivy-state-window ivy-last))))
148 (if (eq avy-style 'de-bruijn)
151 (avy-read (avy-tree candidates avy-keys)
153 #'avy--remove-leading-chars))
156 (if (window-minibuffer-p (cdr candidate))
158 (ivy-set-index (- (line-number-at-pos (car candidate)) 2))
163 (avy-action-goto (caar candidate)))))))
165 (declare-function mc/create-fake-cursor-at-point "ext:multiple-cursors-core")
166 (declare-function multiple-cursors-mode "ext:multiple-cursors-core")
171 (unless (require 'multiple-cursors nil t)
172 (error "multiple-cursors isn't installed"))
173 (let ((cands (nreverse ivy--old-cands)))
174 (unless (string= ivy-text "")
175 (ivy-exit-with-action
178 (while (setq cand (pop cands))
179 (swiper--action cand)
181 (mc/create-fake-cursor-at-point))))
182 (multiple-cursors-mode 1))))))
184 (defun swiper-recenter-top-bottom (&optional arg)
185 "Call (`recenter-top-bottom' ARG)."
188 (recenter-top-bottom arg)))
190 (defvar swiper-font-lock-exclude
221 "List of major-modes that are incompatible with font-lock-ensure.")
223 (defun swiper-font-lock-ensure-p ()
224 "Return non-nil if we should font-lock-ensure."
225 (or (derived-mode-p 'magit-mode)
226 (bound-and-true-p magit-blame-mode)
227 (memq major-mode swiper-font-lock-exclude)))
229 (defun swiper-font-lock-ensure ()
230 "Ensure the entired buffer is highlighted."
231 (unless (swiper-font-lock-ensure-p)
232 (unless (> (buffer-size) 100000)
233 (if (fboundp 'font-lock-ensure)
235 (with-no-warnings (font-lock-fontify-buffer))))))
237 (defvar swiper--format-spec ""
238 "Store the current candidates format spec.")
240 (defvar swiper--width nil
241 "Store the number of digits needed for the longest line nubmer.")
243 (defvar swiper-use-visual-line nil
244 "When non-nil, use `line-move' instead of `forward-line'.")
246 (declare-function outline-show-all "outline")
248 (defun swiper--candidates (&optional numbers-width)
249 "Return a list of this buffer lines.
251 NUMBERS-WIDTH, when specified, is used for width spec of line
252 numbers; replaces calculating the width from buffer line count."
253 (if (and visual-line-mode
254 ;; super-slow otherwise
255 (< (buffer-size) 20000))
257 (when (eq major-mode 'org-mode)
259 (if (fboundp 'outline-show-all)
263 (setq swiper-use-visual-line t))
264 (setq swiper-use-visual-line nil))
265 (let ((n-lines (count-lines (point-min) (point-max))))
266 (unless (zerop n-lines)
267 (setq swiper--width (or numbers-width
268 (1+ (floor (log n-lines 10)))))
269 (setq swiper--format-spec
270 (format "%%-%dd " swiper--width))
271 (let ((line-number 0)
272 (advancer (if swiper-use-visual-line
273 (lambda (arg) (line-move arg t))
277 (goto-char (point-min))
278 (swiper-font-lock-ensure)
279 (while (< (point) (point-max))
282 (replace-regexp-in-string
284 (if swiper-use-visual-line
287 (beginning-of-visual-line)
294 (line-end-position)))))))
295 (when (eq major-mode 'twittering-mode)
296 (remove-text-properties 0 (length str) '(field) str))
297 (put-text-property 0 1 'display
298 (format swiper--format-spec
299 (cl-incf line-number))
301 (push str candidates))
302 (funcall advancer 1))
303 (nreverse candidates))))))
305 (defvar swiper--opoint 1
306 "The point when `swiper' starts.")
309 (defun swiper (&optional initial-input)
310 "`isearch' with an overview.
311 When non-nil, INITIAL-INPUT is the initial search pattern."
313 (swiper--ivy initial-input))
315 (defun swiper-occur ()
316 "Generate a custom occur buffer for `swiper'."
317 (ivy-occur-grep-mode)
319 (let* ((fname (propertize
321 (file-name-nondirectory
331 (get-text-property 0 'display s))
332 'face 'compilation-line-number)
335 (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
337 (insert (format "%d candidates:\n" (length cands)))
338 (ivy--occur-insert-lines
340 (lambda (cand) (concat "./" cand))
343 (ivy-set-occur 'swiper 'swiper-occur)
345 (declare-function evil-jumper--set-jump "ext:evil-jumper")
347 (defvar swiper--current-line nil)
348 (defvar swiper--current-match-start nil)
350 (defun swiper--init ()
351 "Perform initialization common to both completion methods."
352 (setq swiper--current-line nil)
353 (setq swiper--current-match-start nil)
354 (setq swiper--opoint (point))
355 (when (bound-and-true-p evil-jumper-mode)
356 (evil-jumper--set-jump)))
358 (defun swiper--re-builder (str)
359 "Transform STR into a swiper regex.
360 This is the regex used in the minibuffer where candidates have
361 line numbers. For the buffer, use `ivy--regex' instead."
362 (replace-regexp-in-string
368 (setq ivy--subexps 0)
370 ((string-match "^\\^" str)
371 (setq ivy--old-re "")
372 (let ((re (ivy--regex-plus (substring str 1))))
373 (if (zerop ivy--subexps)
374 (prog1 (format "^ ?\\(%s\\)" re)
375 (setq ivy--subexps 1))
376 (format "^ %s" re))))
378 (ivy--regex-plus str)))))
380 (defvar swiper-history nil
381 "History for `swiper'.")
383 (defvar swiper-invocation-face nil
384 "The face at the point of invocation of `swiper'.")
386 (defun swiper--ivy (&optional initial-input)
387 "`isearch' with an overview using `ivy'.
388 When non-nil, INITIAL-INPUT is the initial search pattern."
391 (setq swiper-invocation-face
392 (plist-get (text-properties-at (point)) 'face))
393 (let ((candidates (swiper--candidates))
395 (if swiper-use-visual-line
398 (save-excursion (beginning-of-visual-line) (point)))
399 (1- (line-number-at-pos))))
400 (minibuffer-allow-text-properties t)
408 :initial-input initial-input
412 :update-fn #'swiper--update-input-ivy
413 :unwind #'swiper--cleanup
414 :action #'swiper--action
415 :re-builder #'swiper--re-builder
416 :history 'swiper-history
420 (goto-char swiper--opoint)))))
422 (defun swiper-toggle-face-matching ()
423 "Toggle matching only the candidates with `swiper-invocation-face'."
425 (setf (ivy-state-matcher ivy-last)
426 (if (ivy-state-matcher ivy-last)
428 #'swiper--face-matcher))
429 (setq ivy--old-re nil))
431 (defun swiper--face-matcher (regexp candidates)
432 "Return REGEXP-matching CANDIDATES.
433 Matched candidates should have `swiper-invocation-face'."
437 (string-match regexp x)
438 (let ((s (match-string 0 x))
440 (while (and (< i (length s))
443 'face swiper-invocation-face
449 (defun swiper--ensure-visible ()
450 "Remove overlays hiding point."
451 (let ((overlays (overlays-at (point)))
453 (while (setq ov (pop overlays))
454 (if (and (invisible-p (overlay-get ov 'invisible))
455 (setq expose (overlay-get ov 'isearch-open-invisible)))
456 (funcall expose ov)))))
458 (defvar swiper--overlays nil
461 (defun swiper--cleanup ()
462 "Clean up the overlays."
463 (while swiper--overlays
464 (delete-overlay (pop swiper--overlays)))
466 (goto-char (point-min))
467 (isearch-clean-overlays)))
469 (defun swiper--update-input-ivy ()
470 "Called when `ivy' input is updated."
473 (when (> (length ivy--current) 0)
474 (let* ((re (funcall ivy--regex-function ivy-text))
475 (re (if (stringp re) re (caar re)))
476 (str (get-text-property 0 'display ivy--current))
477 (num (if (string-match "^[0-9]+" str)
478 (string-to-number (match-string 0 str))
480 (unless (eq this-command 'ivy-yank-word)
482 (unless (if swiper--current-line
483 (eq swiper--current-line num)
484 (eq (line-number-at-pos) num))
485 (goto-char (point-min))
486 (if swiper-use-visual-line
488 (forward-line (1- num))))
489 (if (and (equal ivy-text "")
490 (>= swiper--opoint (line-beginning-position))
491 (<= swiper--opoint (line-end-position)))
492 (goto-char swiper--opoint)
493 (if (eq swiper--current-line num)
494 (when swiper--current-match-start
495 (goto-char swiper--current-match-start))
496 (setq swiper--current-line num))
497 (when (re-search-forward re (line-end-position) t)
498 (setq swiper--current-match-start (match-beginning 0))))
499 (isearch-range-invisible (line-beginning-position)
501 (unless (and (>= (point) (window-start))
502 (<= (point) (window-end (ivy-state-window ivy-last) t)))
504 (swiper--add-overlays re)))))
506 (defun swiper--add-overlays (re &optional beg end wnd)
507 "Add overlays for RE regexp in visible part of the current buffer.
508 BEG and END, when specified, are the point bounds.
509 WND, when specified is the window."
510 (setq wnd (or wnd (ivy-state-window ivy-last)))
511 (let ((ov (if visual-line-mode
514 (beginning-of-visual-line)
520 (line-beginning-position)
521 (1+ (line-end-position))))))
522 (overlay-put ov 'face 'swiper-line-face)
523 (overlay-put ov 'window wnd)
524 (push ov swiper--overlays)
525 (let* ((wh (window-height))
526 (beg (or beg (save-excursion
527 (forward-line (- wh))
529 (end (or end (save-excursion
532 (when (>= (length re) swiper-min-highlight)
535 ;; RE can become an invalid regexp
536 (while (and (ignore-errors (re-search-forward re end t))
537 (> (- (match-end 0) (match-beginning 0)) 0))
539 (while (<= i ivy--subexps)
540 (when (match-beginning i)
541 (let ((overlay (make-overlay (match-beginning i)
544 (cond ((zerop ivy--subexps)
549 (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
551 (push overlay swiper--overlays)
552 (overlay-put overlay 'face face)
553 (overlay-put overlay 'window wnd)
554 (overlay-put overlay 'priority i)))
557 (defun swiper--action (x)
559 (let ((ln (1- (read (if (memq this-command '(ivy-occur-press))
560 (when (string-match ":\\([0-9]+\\):.*\\'" x)
561 (match-string-no-properties 1 x))
562 (get-text-property 0 'display x)))))
563 (re (ivy--regex ivy-text)))
565 (user-error "No candidates")
567 (unless (equal (current-buffer)
568 (ivy-state-buffer ivy-last))
569 (switch-to-buffer (ivy-state-buffer ivy-last)))
570 (goto-char (point-min))
571 (funcall (if swiper-use-visual-line
575 (re-search-forward re (line-end-position) t)
576 (swiper--ensure-visible)
577 (when (/= (point) swiper--opoint)
578 (unless (and transient-mark-mode mark-active)
579 (when (eq ivy-exit 'done)
580 (push-mark swiper--opoint t)
581 (message "Mark saved where search started"))))
585 regexp-search-ring-max)))))
587 ;; (define-key isearch-mode-map (kbd "C-o") 'swiper-from-isearch)
588 (defun swiper-from-isearch ()
589 "Invoke `swiper' from isearch."
591 (let ((query (if isearch-regexp
593 (regexp-quote isearch-string))))
597 (defvar swiper-multi-buffers nil
598 "Store the current list of buffers.")
600 (defvar swiper-multi-candidates nil
601 "Store the list of candidates for `swiper-multi'.")
603 (defun swiper-multi-prompt ()
604 (format "Buffers (%s): "
605 (mapconcat #'identity swiper-multi-buffers ", ")))
607 (defun swiper-multi ()
608 "Select one or more buffers.
609 Run `swiper' for those buffers."
611 (setq swiper-multi-buffers nil)
612 (ivy-read (swiper-multi-prompt)
613 'internal-complete-buffer
614 :action 'swiper-multi-action-1)
615 (ivy-read "Swiper: " swiper-multi-candidates
616 :action 'swiper-multi-action-2
617 :unwind #'swiper--cleanup
618 :caller 'swiper-multi))
621 "Run `swiper' for all opened buffers."
623 (ivy-read "Swiper: " (swiper--multi-candidates
627 :action 'swiper-multi-action-2
628 :unwind #'swiper--cleanup
629 :caller 'swiper-multi))
631 (defun swiper--multi-candidates (buffers)
632 (let* ((ww (window-width))
634 (column-2 (apply #'max
637 (length (buffer-name b)))
639 (column-1 (- ww 4 column-2 1)))
640 (dolist (buf buffers)
641 (with-current-buffer buf
646 (setq s (concat (ivy--truncate-string s column-1) " "))
647 (let ((len (length s)))
649 (1- len) len 'display
652 (- ww (string-width s) (length (buffer-name)) 3)
657 (swiper--candidates 4))
662 (defun swiper-multi-action-1 (x)
663 (if (member x swiper-multi-buffers)
665 (setq swiper-multi-buffers (delete x swiper-multi-buffers)))
667 (setq swiper-multi-buffers (append swiper-multi-buffers (list x)))))
668 (let ((prompt (swiper-multi-prompt)))
669 (setf (ivy-state-prompt ivy-last) prompt)
670 (setq ivy--prompt (concat "%-4d " prompt)))
671 (cond ((memq this-command '(ivy-done
674 (setq swiper-multi-candidates
675 (swiper--multi-candidates
676 (mapcar #'get-buffer swiper-multi-buffers))))
677 ((eq this-command 'ivy-call)
678 (delete-minibuffer-contents))))
680 (defun swiper-multi-action-2 (x)
681 (let ((buf-space (get-text-property (1- (length x)) 'display x)))
683 (when (string-match "\\` *\\([^ ]+\\)\\'" buf-space)
684 (switch-to-buffer (match-string 1 buf-space))
685 (goto-char (point-min))
686 (forward-line (1- (read (get-text-property 0 'display x))))
688 (ivy--regex ivy-text)
689 (line-end-position) t)
690 (unless (eq ivy-exit 'done)
692 (swiper--add-overlays (ivy--regex ivy-text)))))))
696 ;;; swiper.el ends here