1 ;;; ivy.el --- Incremental Vertical completYon -*- 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
7 ;; Package-Requires: ((emacs "24.1"))
10 ;; This file is part of GNU Emacs.
12 ;; This file is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; For a full copy of the GNU General Public License
23 ;; see <http://www.gnu.org/licenses/>.
27 ;; This package provides `ivy-read' as an alternative to
28 ;; `completing-read' and similar functions.
30 ;; There's no intricate code to determine the best candidate.
31 ;; Instead, the user can navigate to it with `ivy-next-line' and
32 ;; `ivy-previous-line'.
34 ;; The matching is done by splitting the input text by spaces and
35 ;; re-building it into a regex.
36 ;; So "for example" is transformed into "\\(for\\).*\\(example\\)".
44 "Incremental vertical completion."
47 (defgroup ivy-faces nil
48 "Font-lock faces for `ivy'."
51 (defface ivy-current-match
52 '((((class color) (background light))
53 :background "#1a4b77" :foreground "white")
54 (((class color) (background dark))
55 :background "#65a7e2" :foreground "black"))
56 "Face used by Ivy for highlighting the current match.")
58 (defface ivy-minibuffer-match-face-1
59 '((((class color) (background light))
60 :background "#d3d3d3")
61 (((class color) (background dark))
62 :background "#555555"))
63 "The background face for `ivy' minibuffer matches.")
65 (defface ivy-minibuffer-match-face-2
66 '((((class color) (background light))
67 :background "#e99ce8" :weight bold)
68 (((class color) (background dark))
69 :background "#777777" :weight bold))
70 "Face for `ivy' minibuffer matches modulo 1.")
72 (defface ivy-minibuffer-match-face-3
73 '((((class color) (background light))
74 :background "#bbbbff" :weight bold)
75 (((class color) (background dark))
76 :background "#7777ff" :weight bold))
77 "Face for `ivy' minibuffer matches modulo 2.")
79 (defface ivy-minibuffer-match-face-4
80 '((((class color) (background light))
81 :background "#ffbbff" :weight bold)
82 (((class color) (background dark))
83 :background "#8a498a" :weight bold))
84 "Face for `ivy' minibuffer matches modulo 3.")
86 (defface ivy-confirm-face
87 '((t :foreground "ForestGreen" :inherit minibuffer-prompt))
88 "Face used by Ivy for a confirmation prompt.")
90 (defface ivy-match-required-face
91 '((t :foreground "red" :inherit minibuffer-prompt))
92 "Face used by Ivy for a match required prompt.")
94 (setcdr (assoc load-file-name custom-current-group-alist) 'ivy)
97 '((t (:inherit 'dired-directory)))
98 "Face used by Ivy for highlighting subdirs in the alternatives.")
100 (defface ivy-modified-buffer
101 '((t :inherit 'default))
102 "Face used by Ivy for highlighting modified file visiting buffers.")
105 '((t (:foreground "#110099")))
106 "Face used by Ivy for highlighting remotes in the alternatives.")
109 '((t :inherit font-lock-builtin-face))
110 "Face used by Ivy for matching virtual buffer names.")
112 (defcustom ivy-height 10
113 "Number of lines for the minibuffer window."
116 (defcustom ivy-count-format "%-4d "
117 "The style to use for displaying the current candidate count for `ivy-read'.
118 Set this to \"\" to suppress the count visibility.
119 Set this to \"(%d/%d) \" to display both the index and the count."
121 (const :tag "Count disabled" "")
122 (const :tag "Count matches" "%-4d ")
123 (const :tag "Count matches and show current match" "(%d/%d) ")
126 (defcustom ivy-wrap nil
127 "When non-nil, wrap around after the first and the last candidate."
130 (defcustom ivy-display-style (unless (version< emacs-version "24.5") 'fancy)
131 "The style for formatting the minibuffer.
133 By default, the matched strings are copied as is.
135 The fancy display style highlights matching parts of the regexp,
136 a behavior similar to `swiper'.
138 This setting depends on `add-face-text-property' - a C function
139 available as of Emacs 24.5. Fancy style will render poorly in
140 earlier versions of Emacs."
142 (const :tag "Plain" nil)
143 (const :tag "Fancy" fancy)))
145 (defcustom ivy-on-del-error-function 'minibuffer-keyboard-quit
146 "The handler for when `ivy-backward-delete-char' throws.
147 Usually a quick exit out of the minibuffer."
150 (defcustom ivy-extra-directories '("../" "./")
151 "Add this to the front of the list when completing file names.
152 Only \"./\" and \"../\" apply here. They appear in reverse order."
153 :type '(repeat :tag "Dirs"
155 (const :tag "Parent Directory" "../")
156 (const :tag "Current Directory" "./"))))
158 (defcustom ivy-use-virtual-buffers nil
159 "When non-nil, add `recentf-mode' and bookmarks to `ivy-switch-buffer'."
162 (defvar ivy--actions-list nil
163 "A list of extra actions per command.")
165 (defun ivy-set-actions (cmd actions)
166 "Set CMD extra exit points to ACTIONS."
167 (setq ivy--actions-list
168 (plist-put ivy--actions-list cmd actions)))
172 (defvar ivy-minibuffer-map
173 (let ((map (make-sparse-keymap)))
174 (define-key map (kbd "C-m") 'ivy-done)
175 (define-key map (kbd "C-M-m") 'ivy-call)
176 (define-key map (kbd "C-j") 'ivy-alt-done)
177 (define-key map (kbd "C-M-j") 'ivy-immediate-done)
178 (define-key map (kbd "TAB") 'ivy-partial-or-done)
179 (define-key map (kbd "C-n") 'ivy-next-line)
180 (define-key map (kbd "C-p") 'ivy-previous-line)
181 (define-key map (kbd "<down>") 'ivy-next-line)
182 (define-key map (kbd "<up>") 'ivy-previous-line)
183 (define-key map (kbd "C-s") 'ivy-next-line-or-history)
184 (define-key map (kbd "C-r") 'ivy-reverse-i-search)
185 (define-key map (kbd "SPC") 'self-insert-command)
186 (define-key map (kbd "DEL") 'ivy-backward-delete-char)
187 (define-key map (kbd "M-DEL") 'ivy-backward-kill-word)
188 (define-key map (kbd "C-d") 'ivy-delete-char)
189 (define-key map (kbd "C-f") 'ivy-forward-char)
190 (define-key map (kbd "M-d") 'ivy-kill-word)
191 (define-key map (kbd "M-<") 'ivy-beginning-of-buffer)
192 (define-key map (kbd "M->") 'ivy-end-of-buffer)
193 (define-key map (kbd "M-n") 'ivy-next-history-element)
194 (define-key map (kbd "M-p") 'ivy-previous-history-element)
195 (define-key map (kbd "C-g") 'minibuffer-keyboard-quit)
196 (define-key map (kbd "C-v") 'ivy-scroll-up-command)
197 (define-key map (kbd "M-v") 'ivy-scroll-down-command)
198 (define-key map (kbd "C-M-n") 'ivy-next-line-and-call)
199 (define-key map (kbd "C-M-p") 'ivy-previous-line-and-call)
200 (define-key map (kbd "M-q") 'ivy-toggle-regexp-quote)
201 (define-key map (kbd "M-j") 'ivy-yank-word)
202 (define-key map (kbd "M-i") 'ivy-insert-current)
203 (define-key map (kbd "C-o") 'hydra-ivy/body)
204 (define-key map (kbd "M-o") 'ivy-dispatching-done)
205 (define-key map (kbd "C-M-o") 'ivy-dispatching-call)
206 (define-key map (kbd "C-k") 'ivy-kill-line)
207 (define-key map (kbd "S-SPC") 'ivy-restrict-to-matches)
208 (define-key map (kbd "M-w") 'ivy-kill-ring-save)
209 (define-key map (kbd "C-'") 'ivy-avy)
210 (define-key map (kbd "C-M-a") 'ivy-read-action)
211 (define-key map (kbd "C-c C-o") 'ivy-occur)
213 "Keymap used in the minibuffer.")
214 (autoload 'hydra-ivy/body "ivy-hydra" "" t)
217 (let ((map (make-sparse-keymap)))
218 (define-key map [remap switch-to-buffer] 'ivy-switch-buffer)
220 "Keymap for `ivy-mode'.")
223 (cl-defstruct ivy-state
225 predicate require-match initial-input
226 history preselect keymap update-fn sort
227 ;; The window in which `ivy-read' was called
229 ;; The buffer in which `ivy-read' was called
231 ;; The value of `ivy-text' to be used by `ivy-occur'
237 ;; When this is non-nil, call it for each input change to get new candidates
242 "The last parameters passed to `ivy-read'.
244 This should eventually become a stack so that you could use
245 `ivy-read' recursively.")
247 (defsubst ivy-set-action (action)
248 (setf (ivy-state-action ivy-last) action))
250 (defvar ivy-history nil
251 "History list of candidates entered in the minibuffer.
253 Maximum length of the history list is determined by the value
254 of `history-length'.")
256 (defvar ivy--directory nil
257 "Current directory when completing file names.")
259 (defvar ivy--length 0
260 "Store the amount of viable candidates.")
263 "Store the user's string as it is typed in.")
265 (defvar ivy--current ""
266 "Current candidate.")
269 "Store the index of the current candidate.")
272 "Store 'done if the completion was successfully selected.
273 Otherwise, store nil.")
275 (defvar ivy--all-candidates nil
276 "Store the candidates passed to `ivy-read'.")
278 (defvar ivy--default nil
279 "Default initial input.")
281 (defvar ivy--prompt nil
282 "Store the format-style prompt.
283 When non-nil, it should contain at least one %d.")
285 (defvar ivy--prompt-extra ""
286 "Temporary modifications to the prompt.")
288 (defvar ivy--old-re nil
289 "Store the old regexp.")
291 (defvar ivy--old-cands nil
292 "Store the candidates matched by `ivy--old-re'.")
294 (defvar ivy--regex-function 'ivy--regex
295 "Current function for building a regex.")
297 (defvar ivy--subexps 0
298 "Number of groups in the current `ivy--regex'.")
300 (defvar ivy--full-length nil
301 "When :dynamic-collection is non-nil, this can be the total amount of candidates.")
303 (defvar ivy--old-text ""
304 "Store old `ivy-text' for dynamic completion.")
306 (defvar ivy-case-fold-search 'auto
307 "Store the current overriding `case-fold-search'.")
309 (defvar Info-current-file)
311 (defmacro ivy-quit-and-run (&rest body)
312 "Quit the minibuffer and run BODY afterwards."
314 (put 'quit 'error-message "")
317 (put 'quit 'error-message "Quit")
319 (minibuffer-keyboard-quit)))
321 (defun ivy-exit-with-action (action)
322 "Quit the minibuffer and call ACTION afterwards."
326 (ivy-set-action ',(ivy-state-action ivy-last))))
327 (setq ivy-exit 'done)
330 (defmacro with-ivy-window (&rest body)
331 "Execute BODY in the window from which `ivy-read' was called."
334 `(with-selected-window (ivy--get-window ivy-last)
337 (defun ivy--done (text)
338 "Insert TEXT and exit minibuffer."
339 (if (and ivy--directory
340 (not (eq (ivy-state-history ivy-last) 'grep-files-history)))
341 (insert (setq ivy--current (expand-file-name
342 text ivy--directory)))
343 (insert (setq ivy--current text)))
344 (setq ivy-exit 'done)
349 "Exit the minibuffer with the selected candidate."
351 (delete-minibuffer-contents)
352 (cond ((> ivy--length 0)
353 (ivy--done ivy--current))
354 ((memq (ivy-state-collection ivy-last)
355 '(read-file-name-internal internal-complete-buffer))
356 (if (or (not (eq confirm-nonexistent-file-or-buffer t))
357 (equal " (confirm)" ivy--prompt-extra))
359 (setq ivy--prompt-extra " (confirm)")
362 ((memq (ivy-state-require-match ivy-last)
363 '(nil confirm confirm-after-completion))
364 (ivy--done ivy-text))
366 (setq ivy--prompt-extra " (match required)")
370 (defun ivy-read-action ()
371 "Change the action to one of the available ones."
373 (let ((actions (ivy-state-action ivy-last)))
374 (unless (null (ivy--actionp actions))
375 (let* ((hint (concat ivy--current
382 'face 'font-lock-builtin-face)
387 (key (string (read-key hint)))
388 (action-idx (cl-position-if
389 (lambda (x) (equal (car x) key))
391 (cond ((string= key "
\a"))
393 (error "%s is not bound" key))
396 (setcar actions (1+ action-idx))
397 (ivy-set-action actions)))))))
399 (defun ivy-dispatching-done ()
400 "Select one of the available actions and call `ivy-done'."
405 (defun ivy-dispatching-call ()
406 "Select one of the available actions and call `ivy-call'."
408 (let ((actions (copy-sequence (ivy-state-action ivy-last))))
410 (when (ivy-read-action)
412 (ivy-set-action actions))))
414 (defun ivy-build-tramp-name (x)
415 "Reconstruct X into a path.
416 Is is a cons cell, related to `tramp-get-completion-function'."
420 (concat user "@" domain)
423 (declare-function tramp-get-completion-function "tramp")
424 (declare-function Info-find-node "info")
426 (defun ivy-alt-done (&optional arg)
427 "Exit the minibuffer with the selected candidate.
428 When ARG is t, exit with current text, ignoring the candidates."
431 (ivy-immediate-done))
433 (ivy--directory-done))
434 ((eq (ivy-state-collection ivy-last) 'Info-read-node-name-1)
435 (if (or (equal ivy--current "(./)")
436 (equal ivy--current "(../)"))
438 (ivy-read "Go to file: " 'read-file-name-internal
441 (expand-file-name x ivy--directory)
447 (defun ivy--directory-done ()
448 "Handle exit from the minibuffer when completing file names."
451 ((equal ivy-text "/sudo::")
452 (setq dir (concat ivy-text ivy--directory))
457 (not (equal ivy-text ""))
461 (file-name-as-directory
463 ivy-text ivy--directory))))))
465 (not (string= ivy--current "./"))
466 (cl-plusp ivy--length)
469 (setq dir (file-name-as-directory
471 ivy--current ivy--directory)))))))
474 ((or (and (equal ivy--directory "/")
475 (string-match "\\`[^/]+:.*:.*\\'" ivy-text))
476 (string-match "\\`/[^/]+:.*:.*\\'" ivy-text))
478 ((or (and (equal ivy--directory "/")
480 "\\`\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
483 "\\`\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
485 (setq ivy-text ivy--current))))
487 "\\`/\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
489 (let ((method (match-string 1 ivy-text))
490 (user (match-string 2 ivy-text))
491 (rest (match-string 3 ivy-text))
494 (dolist (x (tramp-get-completion-function method))
495 (setq res (append res (funcall (car x) (cadr x)))))
496 (setq res (delq nil res))
500 (setq res (cl-delete-duplicates res :test #'equal))
501 (let* ((old-ivy-last ivy-last)
502 (enable-recursive-minibuffers t)
503 (host (ivy-read "Find File: "
504 (mapcar #'ivy-build-tramp-name res)
505 :initial-input rest)))
506 (setq ivy-last old-ivy-last)
508 (setq ivy--directory "/")
509 (ivy--cd (concat "/" method ":" host ":"))))))
513 (defcustom ivy-tab-space nil
514 "When non-nil, `ivy-partial-or-done' should insert a space."
517 (defun ivy-partial-or-done ()
518 "Complete the minibuffer text as much as possible.
519 If the text hasn't changed as a result, forward to `ivy-alt-done'."
521 (if (and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
522 (or (and (equal ivy--directory "/")
523 (string-match "\\`[^/]+:.*\\'" ivy-text))
524 (string-match "\\`/" ivy-text)))
525 (let ((default-directory ivy--directory))
526 (minibuffer-complete)
527 (setq ivy-text (ivy--input))
528 (when (file-directory-p
529 (expand-file-name ivy-text ivy--directory))
530 (ivy--cd (file-name-as-directory
531 (expand-file-name ivy-text ivy--directory)))))
533 (when (or (eq this-command last-command)
537 (defun ivy-partial ()
538 "Complete the minibuffer text as much as possible."
540 (let* ((parts (or (split-string ivy-text " " t) (list "")))
541 (postfix (car (last parts)))
542 (completion-ignore-case t)
543 (startp (string-match "^\\^" postfix))
544 (new (try-completion (if startp
545 (substring postfix 1)
547 (mapcar (lambda (str)
548 (let ((i (string-match postfix str)))
552 (cond ((eq new t) nil)
553 ((string= new ivy-text) nil)
555 (delete-region (minibuffer-prompt-end) (point-max))
560 (insert (mapconcat #'identity parts " ")
561 (if ivy-tab-space " " ""))
564 (defun ivy-immediate-done ()
565 "Exit the minibuffer with the current input."
567 (delete-minibuffer-contents)
568 (insert (setq ivy--current
570 (expand-file-name ivy-text ivy--directory)
572 (setq ivy-exit 'done)
577 "Resume the last completion session."
579 (when (eq (ivy-state-caller ivy-last) 'swiper)
580 (switch-to-buffer (ivy-state-buffer ivy-last)))
581 (with-current-buffer (ivy-state-buffer ivy-last)
583 (ivy-state-prompt ivy-last)
584 (ivy-state-collection ivy-last)
585 :predicate (ivy-state-predicate ivy-last)
586 :require-match (ivy-state-require-match ivy-last)
587 :initial-input ivy-text
588 :history (ivy-state-history ivy-last)
589 :preselect (unless (eq (ivy-state-collection ivy-last)
590 'read-file-name-internal)
592 :keymap (ivy-state-keymap ivy-last)
593 :update-fn (ivy-state-update-fn ivy-last)
594 :sort (ivy-state-sort ivy-last)
595 :action (ivy-state-action ivy-last)
596 :unwind (ivy-state-unwind ivy-last)
597 :re-builder (ivy-state-re-builder ivy-last)
598 :matcher (ivy-state-matcher ivy-last)
599 :dynamic-collection (ivy-state-dynamic-collection ivy-last)
600 :caller (ivy-state-caller ivy-last))))
602 (defvar ivy-calling nil
603 "When non-nil, call the current action when `ivy--index' changes.")
605 (defun ivy-set-index (index)
606 "Set `ivy--index' to INDEX."
607 (setq ivy--index index)
612 (defun ivy-beginning-of-buffer ()
613 "Select the first completion candidate."
617 (defun ivy-end-of-buffer ()
618 "Select the last completion candidate."
620 (ivy-set-index (1- ivy--length)))
622 (defun ivy-scroll-up-command ()
623 "Scroll the candidates upward by the minibuffer height."
625 (ivy-set-index (min (1- (+ ivy--index ivy-height))
628 (defun ivy-scroll-down-command ()
629 "Scroll the candidates downward by the minibuffer height."
631 (ivy-set-index (max (1+ (- ivy--index ivy-height))
634 (defun ivy-minibuffer-grow ()
635 "Grow the minibuffer window by 1 line."
637 (setq-local max-mini-window-height
638 (cl-incf ivy-height)))
640 (defun ivy-minibuffer-shrink ()
641 "Shrink the minibuffer window by 1 line."
643 (unless (<= ivy-height 2)
644 (setq-local max-mini-window-height
645 (cl-decf ivy-height))
646 (window-resize (selected-window) -1)))
648 (defun ivy-next-line (&optional arg)
649 "Move cursor vertically down ARG candidates."
651 (setq arg (or arg 1))
652 (let ((index (+ ivy--index arg)))
653 (if (> index (1- ivy--length))
655 (ivy-beginning-of-buffer)
656 (ivy-set-index (1- ivy--length)))
657 (ivy-set-index index))))
659 (defun ivy-next-line-or-history (&optional arg)
660 "Move cursor vertically down ARG candidates.
661 If the input is empty, select the previous history element instead."
663 (when (string= ivy-text "")
664 (ivy-previous-history-element 1))
667 (defun ivy-previous-line (&optional arg)
668 "Move cursor vertically up ARG candidates."
670 (setq arg (or arg 1))
671 (let ((index (- ivy--index arg)))
676 (ivy-set-index index))))
678 (defun ivy-previous-line-or-history (arg)
679 "Move cursor vertically up ARG candidates.
680 If the input is empty, select the previous history element instead."
682 (when (string= ivy-text "")
683 (ivy-previous-history-element 1))
684 (ivy-previous-line arg))
686 (defun ivy-toggle-calling ()
687 "Flip `ivy-calling'."
689 (when (setq ivy-calling (not ivy-calling))
692 (defun ivy--get-action (state)
693 "Get the action function from STATE."
694 (let ((action (ivy-state-action state)))
696 (if (functionp action)
698 (cadr (nth (car action) action))))))
700 (defun ivy--get-window (state)
701 "Get the window from STATE."
702 (if (ivy-state-p state)
703 (let ((window (ivy-state-window state)))
704 (if (window-live-p window)
706 (if (= (length (window-list)) 1)
711 (defun ivy--actionp (x)
712 "Return non-nil when X is a list of actions."
713 (and x (listp x) (not (eq (car x) 'closure))))
715 (defun ivy-next-action ()
716 "When the current action is a list, scroll it forwards."
718 (let ((action (ivy-state-action ivy-last)))
719 (when (ivy--actionp action)
720 (unless (>= (car action) (1- (length action)))
721 (cl-incf (car action))))))
723 (defun ivy-prev-action ()
724 "When the current action is a list, scroll it backwards."
726 (let ((action (ivy-state-action ivy-last)))
727 (when (ivy--actionp action)
728 (unless (<= (car action) 1)
729 (cl-decf (car action))))))
731 (defun ivy-action-name ()
732 "Return the name associated with the current action."
733 (let ((action (ivy-state-action ivy-last)))
734 (if (ivy--actionp action)
738 (nth 2 (nth (car action) action)))
742 "Call the current action without exiting completion."
744 (let ((action (ivy--get-action ivy-last)))
746 (let* ((collection (ivy-state-collection ivy-last))
747 (x (if (and (consp collection)
748 (consp (car collection)))
749 (cdr (assoc ivy--current collection))
750 (if (equal ivy--current "")
753 (prog1 (funcall action x)
754 (unless (or (eq ivy-exit 'done)
755 (equal (selected-window)
756 (active-minibuffer-window))
757 (null (active-minibuffer-window)))
758 (select-window (active-minibuffer-window))))))))
760 (defun ivy-next-line-and-call (&optional arg)
761 "Move cursor vertically down ARG candidates.
762 Call the permanent action if possible."
768 (defun ivy-previous-line-and-call (&optional arg)
769 "Move cursor vertically down ARG candidates.
770 Call the permanent action if possible."
772 (ivy-previous-line arg)
776 (defun ivy-previous-history-element (arg)
777 "Forward to `previous-history-element' with ARG."
779 (previous-history-element arg)
782 (ivy--maybe-scroll-history))
784 (defun ivy-next-history-element (arg)
785 "Forward to `next-history-element' with ARG."
787 (next-history-element arg)
790 (ivy--maybe-scroll-history))
792 (defun ivy--cd-maybe ()
793 "Check if the current input points to a different directory.
794 If so, move to that directory, while keeping only the file name."
796 (let ((input (ivy--input))
798 (if (setq url (ffap-url-p input))
799 (ivy-exit-with-action
801 (funcall ffap-url-fetcher url)))
802 (setq input (expand-file-name input))
803 (let ((file (file-name-nondirectory input))
804 (dir (expand-file-name (file-name-directory input))))
805 (if (string= dir ivy--directory)
807 (delete-minibuffer-contents)
812 (defun ivy--maybe-scroll-history ()
813 "If the selected history element has an index, scroll there."
814 (let ((idx (ignore-errors
816 (minibuffer-prompt-end)
820 (setq ivy--index idx))))
823 "When completing file names, move to directory DIR."
824 (if (null ivy--directory)
826 (setq ivy--old-cands nil)
827 (setq ivy--old-re nil)
829 (setq ivy--all-candidates
830 (ivy--sorted-files (setq ivy--directory dir)))
832 (delete-minibuffer-contents)))
834 (defun ivy-backward-delete-char ()
835 "Forward to `backward-delete-char'.
836 On error (read-only), call `ivy-on-del-error-function'."
838 (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
840 (ivy--cd (file-name-directory
846 (backward-delete-char 1)
848 (when ivy-on-del-error-function
849 (funcall ivy-on-del-error-function))))))
851 (defun ivy-delete-char (arg)
852 "Forward to `delete-char' ARG."
854 (unless (= (point) (line-end-position))
857 (defun ivy-forward-char (arg)
858 "Forward to `forward-char' ARG."
860 (unless (= (point) (line-end-position))
863 (defun ivy-kill-word (arg)
864 "Forward to `kill-word' ARG."
866 (unless (= (point) (line-end-position))
869 (defun ivy-kill-line ()
870 "Forward to `kill-line'."
873 (kill-region (minibuffer-prompt-end) (point))
876 (defun ivy-backward-kill-word ()
877 "Forward to `backward-kill-word'."
879 (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
881 (ivy--cd (file-name-directory
889 (delete-region (point) pt)))))
891 (defvar ivy--regexp-quote 'regexp-quote
892 "Store the regexp quoting state.")
894 (defun ivy-toggle-regexp-quote ()
895 "Toggle the regexp quoting."
897 (setq ivy--old-re nil)
898 (cl-rotatef ivy--regex-function ivy--regexp-quote))
900 (defvar avy-all-windows)
903 (defvar avy-keys-alist)
905 (defvar avy-styles-alist)
906 (declare-function avy--process "ext:avy")
907 (declare-function avy--style-fn "ext:avy")
909 (eval-after-load 'avy
910 '(add-to-list 'avy-styles-alist '(ivy-avy . pre)))
913 "Jump to one of the current ivy candidates."
915 (unless (require 'avy nil 'noerror)
916 (error "Package avy isn't installed"))
917 (let* ((avy-all-windows nil)
918 (avy-keys (or (cdr (assq 'ivy-avy avy-keys-alist))
920 (avy-style (or (cdr (assq 'ivy-avy
930 (goto-char (point-min))
932 (while (< (point) (point-max))
938 (setq avy-action #'identity)
940 (nreverse candidates)
941 (avy--style-fn avy-style)))))
942 (ivy-set-index (- (line-number-at-pos candidate) 2))
946 (defun ivy-sort-file-function-default (x y)
947 "Compare two files X and Y.
948 Prioritize directories."
949 (if (get-text-property 0 'dirp x)
950 (if (get-text-property 0 'dirp y)
953 (if (get-text-property 0 'dirp y)
957 (defcustom ivy-sort-functions-alist
958 '((read-file-name-internal . ivy-sort-file-function-default)
959 (internal-complete-buffer . nil)
960 (counsel-git-grep-function . nil)
961 (Man-goto-section . nil)
964 "An alist of sorting functions for each collection function.
965 Interactive functions that call completion fit in here as well.
967 Nil means no sorting, which is useful to turn off the sorting for
968 functions that have candidates in the natural buffer order, like
969 `org-refile' or `Man-goto-section'.
971 The entry associated with t is used for all fall-through cases.
973 See also `ivy-sort-max-size'."
977 (const :tag "All other functions" t)
978 (symbol :tag "Function"))
980 (const :tag "plain sort" string-lessp)
981 (const :tag "file sort" ivy-sort-file-function-default)
982 (const :tag "no sort" nil)))
985 (defvar ivy-index-functions-alist
986 '((swiper . ivy-recompute-index-swiper)
987 (swiper-multi . ivy-recompute-index-swiper)
988 (counsel-git-grep . ivy-recompute-index-swiper)
989 (counsel-grep . ivy-recompute-index-swiper-async)
990 (t . ivy-recompute-index-zero))
991 "An alist of index recomputing functions for each collection function.
992 When the input changes, the appropriate function returns an
993 integer - the index of the matched candidate that should be
996 (defvar ivy-re-builders-alist
997 '((t . ivy--regex-plus))
998 "An alist of regex building functions for each collection function.
999 Each function should take a string and return a valid regex or a
1000 regex sequence (see below).
1002 The entry associated with t is used for all fall-through cases.
1003 Possible choices: `ivy--regex', `regexp-quote', `ivy--regex-plus'.
1005 If a function returns a list, it should format like this:
1006 '((\"matching-regexp\" . t) (\"non-matching-regexp\") ...).
1008 The matches will be filtered in a sequence, you can mix the
1009 regexps that should match and that should not match as you
1012 (defvar ivy-initial-inputs-alist
1013 '((org-refile . "^")
1014 (org-agenda-refile . "^")
1015 (org-capture-refile . "^")
1017 (counsel-describe-function . "^")
1018 (counsel-describe-variable . "^")
1021 "Command to initial input table.")
1023 (defcustom ivy-sort-max-size 30000
1024 "Sorting won't be done for collections larger than this."
1027 (defun ivy--sorted-files (dir)
1028 "Return the list of files in DIR.
1029 Directories come first."
1030 (let* ((default-directory dir)
1031 (seq (all-completions "" 'read-file-name-internal))
1035 (setq seq (delete "./" (delete "../" seq)))
1036 (when (eq (setq sort-fn (cdr (assoc 'read-file-name-internal
1037 ivy-sort-functions-alist)))
1038 #'ivy-sort-file-function-default)
1039 (setq seq (mapcar (lambda (x)
1040 (propertize x 'dirp (string-match-p "/\\'" x)))
1043 (setq seq (cl-sort seq sort-fn)))
1044 (dolist (dir ivy-extra-directories)
1048 (defvar ivy-recursive-restore t
1049 "When non-nil, restore the above state when exiting the minibuffer.
1050 This variable is let-bound to nil by functions that take care of
1051 the restoring themselves.")
1054 (cl-defun ivy-read (prompt collection
1055 &key predicate require-match initial-input
1056 history preselect keymap update-fn sort
1057 action unwind re-builder matcher dynamic-collection caller)
1058 "Read a string in the minibuffer, with completion.
1060 PROMPT is a format string, normally ending in a colon and a
1061 space; %d anywhere in the string is replaced by the current
1062 number of matching candidates. For the literal % character,
1063 escape it with %%. See also `ivy-count-format'.
1065 COLLECTION is either a list of strings, a function, an alist, or
1068 If INITIAL-INPUT is not nil, then insert that input in the
1069 minibuffer initially.
1071 KEYMAP is composed with `ivy-minibuffer-map'.
1073 If PRESELECT is not nil, then select the corresponding candidate
1074 out of the ones that match the INITIAL-INPUT.
1076 UPDATE-FN is called each time the current candidate(s) is changed.
1078 When SORT is t, use `ivy-sort-functions-alist' for sorting.
1080 ACTION is a lambda function to call after selecting a result. It
1081 takes a single string argument.
1083 UNWIND is a lambda function to call before exiting.
1085 RE-BUILDER is a lambda function to call to transform text into a
1088 MATCHER is to override matching.
1090 DYNAMIC-COLLECTION is a boolean to specify if the list of
1091 candidates is updated after each input by calling COLLECTION.
1093 CALLER is a symbol to uniquely identify the caller to `ivy-read'.
1094 It is used, along with COLLECTION, to determine which
1095 customizations apply to the current completion session."
1096 (let ((extra-actions (plist-get ivy--actions-list this-command)))
1099 (if (functionp action)
1101 ("o" ,action "default")
1103 (delete-dups (append action extra-actions))))))
1104 (let ((recursive-ivy-last (and (active-minibuffer-window) ivy-last)))
1108 :collection collection
1109 :predicate predicate
1110 :require-match require-match
1111 :initial-input initial-input
1113 :preselect preselect
1115 :update-fn update-fn
1118 :window (selected-window)
1119 :buffer (current-buffer)
1121 :re-builder re-builder
1123 :dynamic-collection dynamic-collection
1125 (ivy--reset-state ivy-last)
1128 (minibuffer-with-setup-hook
1129 #'ivy--minibuffer-setup
1130 (let* ((hist (or history 'ivy-history))
1131 (minibuffer-completion-table collection)
1132 (minibuffer-completion-predicate predicate)
1133 (resize-mini-windows (cond
1134 ((display-graphic-p) nil)
1135 ((null resize-mini-windows) 'grow-only)
1136 (t resize-mini-windows)))
1137 (res (read-from-minibuffer
1139 (ivy-state-initial-input ivy-last)
1140 (make-composed-keymap keymap ivy-minibuffer-map)
1143 (when (eq ivy-exit 'done)
1144 (let ((item (if ivy--directory
1147 (unless (equal item "")
1148 (set hist (cons (propertize item 'ivy-index ivy--index)
1150 (cdr (symbol-value hist)))))))
1152 (remove-hook 'post-command-hook #'ivy--exhibit)
1153 (when (setq unwind (ivy-state-unwind ivy-last))
1155 (unless (eq ivy-exit 'done)
1156 (when recursive-ivy-last
1157 (ivy--reset-state (setq ivy-last recursive-ivy-last)))))
1159 (when (and recursive-ivy-last
1160 ivy-recursive-restore)
1161 (ivy--reset-state (setq ivy-last recursive-ivy-last))))))
1163 (defun ivy--reset-state (state)
1164 "Reset the ivy to STATE.
1165 This is useful for recursive `ivy-read'."
1166 (let ((prompt (or (ivy-state-prompt state) ""))
1167 (collection (ivy-state-collection state))
1168 (predicate (ivy-state-predicate state))
1169 (history (ivy-state-history state))
1170 (preselect (ivy-state-preselect state))
1171 (sort (ivy-state-sort state))
1172 (re-builder (ivy-state-re-builder state))
1173 (dynamic-collection (ivy-state-dynamic-collection state))
1174 (initial-input (ivy-state-initial-input state))
1175 (require-match (ivy-state-require-match state))
1176 (caller (ivy-state-caller state)))
1177 (unless initial-input
1178 (setq initial-input (cdr (assoc this-command
1179 ivy-initial-inputs-alist))))
1180 (setq ivy--directory nil)
1181 (setq ivy-case-fold-search 'auto)
1182 (setq ivy--regex-function
1184 (and (functionp collection)
1185 (cdr (assoc collection ivy-re-builders-alist)))
1187 (cdr (assoc caller ivy-re-builders-alist)))
1188 (cdr (assoc t ivy-re-builders-alist))
1190 (setq ivy--subexps 0)
1191 (setq ivy--regexp-quote 'regexp-quote)
1192 (setq ivy--old-text "")
1193 (setq ivy--full-length nil)
1195 (setq ivy-calling nil)
1197 (cond ((eq collection 'Info-read-node-name-1)
1198 (if (equal Info-current-file "dir")
1200 (mapcar (lambda (x) (format "(%s)" x))
1201 (cl-delete-duplicates
1202 (all-completions "(" collection predicate)
1204 (setq coll (all-completions "" collection predicate))))
1205 ((eq collection 'read-file-name-internal)
1206 (setq ivy--directory default-directory)
1209 (let ((preselect-directory (file-name-directory preselect)))
1210 (unless (or (null preselect-directory)
1211 (string= preselect-directory
1213 (setq ivy--directory preselect-directory))
1215 (ivy-state-preselect state)
1216 (setq preselect (file-name-nondirectory preselect)))))
1217 (setq coll (ivy--sorted-files ivy--directory))
1219 (unless (or require-match
1220 (equal initial-input default-directory)
1221 (equal initial-input ""))
1222 (setq coll (cons initial-input coll)))
1223 (setq initial-input nil)))
1224 ((eq collection 'internal-complete-buffer)
1225 (setq coll (ivy--buffer-list "" ivy-use-virtual-buffers)))
1226 ((or (functionp collection)
1227 (byte-code-function-p collection)
1228 (vectorp collection)
1229 (and (consp collection) (listp (car collection)))
1230 (hash-table-p collection))
1231 (setq coll (all-completions "" collection predicate)))
1233 (setq coll collection)))
1235 (if (and (functionp collection)
1236 (setq sort-fn (assoc collection ivy-sort-functions-alist)))
1237 (when (and (setq sort-fn (cdr sort-fn))
1238 (not (eq collection 'read-file-name-internal)))
1239 (setq coll (cl-sort coll sort-fn)))
1240 (unless (eq history 'org-refile-history)
1241 (if (and (setq sort-fn (cdr (assoc t ivy-sort-functions-alist)))
1242 (<= (length coll) ivy-sort-max-size))
1243 (setq coll (cl-sort (copy-sequence coll) sort-fn))))))
1245 (unless (or (and require-match
1246 (not (eq collection 'internal-complete-buffer)))
1248 (let ((re (regexp-quote preselect)))
1249 (cl-find-if (lambda (x) (string-match re x))
1251 (setq coll (cons preselect coll))))
1252 (setq ivy--old-re nil)
1253 (setq ivy--old-cands nil)
1254 (when (integerp preselect)
1255 (setq ivy--old-re "")
1256 (setq ivy--index preselect))
1258 ;; Needed for anchor to work
1259 (setq ivy--old-cands coll)
1260 (setq ivy--old-cands (ivy--filter initial-input coll)))
1261 (setq ivy--all-candidates coll)
1262 (unless (integerp preselect)
1263 (setq ivy--index (or
1264 (and dynamic-collection
1267 (ivy--preselect-index
1274 (setq ivy--default (or
1275 (thing-at-point 'url)
1276 (thing-at-point 'symbol)
1279 (cond ((string-match "%.*d" prompt)
1281 ((null ivy-count-format)
1283 "`ivy-count-format' can't be nil. Set it to an empty string instead"))
1284 ((string-match "%d.*%d" ivy-count-format)
1285 (let ((w (length (number-to-string
1286 (length ivy--all-candidates))))
1287 (s (copy-sequence ivy-count-format)))
1288 (string-match "%d" s)
1290 (string-match "%d" s (match-end 0))
1291 (setq s (replace-match (format "%%-%dd" w) nil nil s))
1292 (string-match "%d" s)
1293 (concat (replace-match (format "%%%dd" w) nil nil s)
1295 ((string-match "%.*d" ivy-count-format)
1296 (concat ivy-count-format prompt))
1301 (setf (ivy-state-initial-input ivy-last) initial-input)))
1304 (defun ivy-completing-read (prompt collection
1305 &optional predicate require-match initial-input
1306 history def inherit-input-method)
1307 "Read a string in the minibuffer, with completion.
1309 This interface conforms to `completing-read' and can be used for
1310 `completing-read-function'.
1312 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1313 COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1314 PREDICATE limits completion to a subset of COLLECTION.
1315 REQUIRE-MATCH is specified with a boolean value. See `completing-read'.
1316 INITIAL-INPUT is a string that can be inserted into the minibuffer initially.
1317 HISTORY is a list of previously selected inputs.
1318 DEF is the default value.
1319 INHERIT-INPUT-METHOD is currently ignored."
1320 (if (memq this-command '(tmm-menubar tmm-shortcut))
1321 (completing-read-default prompt collection
1322 predicate require-match
1323 initial-input history
1324 def inherit-input-method)
1325 ;; See the doc of `completing-read'.
1326 (when (consp history)
1327 (when (numberp (cdr history))
1328 (setq initial-input (nth (1- (cdr history))
1329 (symbol-value (car history)))))
1330 (setq history (car history)))
1331 (ivy-read (replace-regexp-in-string "%" "%%" prompt)
1333 :predicate predicate
1334 :require-match require-match
1335 :initial-input (if (consp initial-input)
1337 (if (and (stringp initial-input)
1338 (string-match "\\+" initial-input))
1339 (replace-regexp-in-string
1340 "\\+" "\\\\+" initial-input)
1342 :preselect (if (listp def) (car def) def)
1346 (let ((sort (assoc this-command ivy-sort-functions-alist)))
1352 (define-minor-mode ivy-mode
1353 "Toggle Ivy mode on or off.
1354 Turn Ivy mode on if ARG is positive, off otherwise.
1355 Turning on Ivy mode sets `completing-read-function' to
1356 `ivy-completing-read'.
1361 Minibuffer bindings:
1362 \\{ivy-minibuffer-map}"
1365 :keymap ivy-mode-map
1368 (setq completing-read-function 'ivy-completing-read)
1369 (setq completing-read-function 'completing-read-default)))
1371 (defun ivy--preselect-index (preselect candidates)
1372 "Return the index of PRESELECT in CANDIDATES."
1373 (cond ((integerp preselect)
1375 ((cl-position preselect candidates :test #'equal))
1376 ((stringp preselect)
1377 (let ((re (regexp-quote preselect)))
1380 (string-match re x))
1385 (defvar ivy--regex-hash
1386 (make-hash-table :test #'equal)
1387 "Store pre-computed regex.")
1389 (defun ivy--split (str)
1390 "Split STR into a list by single spaces.
1391 The remaining spaces stick to their left.
1392 This allows to \"quote\" N spaces by inputting N+1 spaces."
1393 (let ((len (length str))
1398 (while (and (string-match " +" str start1)
1400 (setq match-len (- (match-end 0) (match-beginning 0)))
1404 (setq start1 start0)
1406 (push (substring str start1 (match-beginning 0)) res)
1407 (setq start1 (match-end 0)))
1408 (setq str (replace-match
1409 (make-string (1- match-len) ?\ )
1411 (setq start0 (or start0 start1))
1412 (setq start1 (1- (match-end 0)))))
1414 (push (substring str start0) res)
1415 (setq s (substring str start1))
1416 (unless (= (length s) 0)
1420 (defun ivy--regex (str &optional greedy)
1421 "Re-build regex pattern from STR in case it has a space.
1422 When GREEDY is non-nil, join words in a greedy way."
1423 (let ((hashed (unless greedy
1424 (gethash str ivy--regex-hash))))
1427 (setq ivy--subexps (car hashed)))
1428 (when (string-match "\\([^\\]\\|^\\)\\\\$" str)
1429 (setq str (substring str 0 -1)))
1431 (let ((subs (ivy--split str)))
1432 (if (= (length subs) 1)
1434 (setq ivy--subexps 0)
1437 (setq ivy--subexps (length subs))
1440 (if (string-match "\\`\\\\(.*\\\\)\\'" x)
1442 (format "\\(%s\\)" x)))
1447 ivy--regex-hash)))))
1449 (defun ivy--regex-ignore-order (str)
1450 "Re-build regex from STR by splitting at spaces.
1451 Ignore the order of each group.
1453 ATTENTION: This is just a proof of concept and may not work as
1454 expected. Besides ignoring the order of the tokens where 'foo'
1455 and 'bar', 'bar' and 'foo' are matched, it also matches multiple
1456 occurrences of 'foo' and 'bar'. To ignore the sort order and avoid
1457 multiple matches, use `ivy-restrict-to-matches' instead.
1459 (let* ((subs (split-string str " +" t))
1460 (len (length subs)))
1463 (setq ivy--subexps 0)
1466 (setq ivy--subexps len)
1467 (let ((all (mapconcat #'identity subs "\\|")))
1470 (if (string-match "\\`\\\\(.*\\\\)\\'" x)
1472 (format "\\(%s\\)" x)))
1476 (defun ivy--regex-plus (str)
1477 "Build a regex sequence from STR.
1478 Spaces are wild card characters, everything before \"!\" should
1479 match. Everything after \"!\" should not match."
1480 (let ((parts (split-string str "!" t)))
1481 (cl-case (length parts)
1485 (if (string-equal (substring str 0 1) "!")
1488 (list (ivy--regex (car parts))))
1489 (ivy--regex (car parts))))
1493 (split-string (cadr parts) " " t))))
1494 (cons (cons (ivy--regex (car parts)) t)
1496 (t (error "Unexpected: use only one !")))))
1498 (defun ivy--regex-fuzzy (str)
1499 "Build a regex sequence from STR.
1500 Insert .* between each char."
1501 (if (string-match "\\`\\(\\^?\\)\\(.*?\\)\\(\\$?\\)\\'" str)
1503 (concat (match-string 1 str)
1506 (format "\\(%c\\)" x))
1507 (string-to-list (match-string 2 str)) ".*")
1508 (match-string 3 str))
1509 (setq ivy--subexps (length (match-string 2 str))))
1513 (defun ivy--minibuffer-setup ()
1514 "Setup ivy completion in the minibuffer."
1515 (set (make-local-variable 'completion-show-inline-help) nil)
1516 (set (make-local-variable 'minibuffer-default-add-function)
1518 (list ivy--default)))
1519 (when (display-graphic-p)
1520 (setq truncate-lines t))
1521 (setq-local max-mini-window-height ivy-height)
1522 (add-hook 'post-command-hook #'ivy--exhibit nil t)
1523 ;; show completions with empty input
1526 (defun ivy--input ()
1527 "Return the current minibuffer input."
1528 ;; assume one-line minibuffer input
1529 (buffer-substring-no-properties
1530 (minibuffer-prompt-end)
1531 (line-end-position)))
1533 (defun ivy--cleanup ()
1534 "Delete the displayed completion candidates."
1536 (goto-char (minibuffer-prompt-end))
1537 (delete-region (line-end-position) (point-max))))
1539 (defun ivy--insert-prompt ()
1540 "Update the prompt according to `ivy--prompt'."
1542 (unless (memq this-command '(ivy-done ivy-alt-done ivy-partial-or-done
1543 counsel-find-symbol))
1544 (setq ivy--prompt-extra ""))
1546 (if (string-match "\\(.*\\): \\'" ivy--prompt)
1548 (setq head (match-string 1 ivy--prompt))
1550 (setq head (substring ivy--prompt 0 -1))
1552 (let ((inhibit-read-only t)
1553 (std-props '(front-sticky t rear-nonsticky t field t read-only t))
1556 (if (and (bound-and-true-p minibuffer-depth-indicate-mode)
1557 (> (minibuffer-depth) 1))
1558 (format "[%d] " (minibuffer-depth))
1561 (if (string-match "%d.*%d" ivy-count-format)
1564 (or (and (ivy-state-dynamic-collection ivy-last)
1568 (or (and (ivy-state-dynamic-collection ivy-last)
1573 (d-str (if ivy--directory
1574 (abbreviate-file-name ivy--directory)
1577 (goto-char (point-min))
1578 (delete-region (point-min) (minibuffer-prompt-end))
1579 (if (> (+ (mod (+ (length n-str) (length d-str)) (window-width))
1582 (setq n-str (concat n-str "\n" d-str))
1583 (setq n-str (concat n-str d-str)))
1584 (let ((regex (format "\\([^\n]\\{%d\\}\\)[^\n]" (window-width))))
1585 (while (string-match regex n-str)
1586 (setq n-str (replace-match (concat (match-string 1 n-str) "\n") nil t n-str 1))))
1587 (set-text-properties 0 (length n-str)
1588 `(face minibuffer-prompt ,@std-props)
1590 (ivy--set-match-props n-str "confirm"
1591 `(face ivy-confirm-face ,@std-props))
1592 (ivy--set-match-props n-str "match required"
1593 `(face ivy-match-required-face ,@std-props))
1595 ;; get out of the prompt area
1596 (constrain-to-field nil (point-max))))))
1598 (defun ivy--set-match-props (str match props)
1599 "Set STR text properties that match MATCH to PROPS."
1600 (when (string-match match str)
1601 (set-text-properties
1607 (defvar inhibit-message)
1609 (defun ivy--sort-maybe (collection)
1610 "Sort COLLECTION if needed."
1611 (let ((sort (ivy-state-sort ivy-last))
1615 (let ((sort-fn (cond ((functionp sort)
1617 ((setq entry (assoc (ivy-state-collection ivy-last)
1618 ivy-sort-functions-alist))
1621 (cdr (assoc t ivy-sort-functions-alist))))))
1622 (if (functionp sort-fn)
1623 (cl-sort (copy-sequence collection) sort-fn)
1626 (defun ivy--exhibit ()
1627 "Insert Ivy completions display.
1628 Should be run via minibuffer `post-command-hook'."
1629 (when (memq 'ivy--exhibit post-command-hook)
1630 (let ((inhibit-field-text-motion nil))
1631 (constrain-to-field nil (point-max)))
1632 (setq ivy-text (ivy--input))
1633 (if (ivy-state-dynamic-collection ivy-last)
1634 ;; while-no-input would cause annoying
1635 ;; "Waiting for process to die...done" message interruptions
1636 (let ((inhibit-message t))
1637 (unless (equal ivy--old-text ivy-text)
1639 (setq ivy--all-candidates
1641 (funcall (ivy-state-collection ivy-last) ivy-text)))
1642 (setq ivy--old-text ivy-text)))
1643 (when ivy--all-candidates
1644 (ivy--insert-minibuffer
1645 (ivy--format ivy--all-candidates))))
1646 (cond (ivy--directory
1647 (if (string-match "/\\'" ivy-text)
1648 (if (member ivy-text ivy--all-candidates)
1649 (ivy--cd (expand-file-name ivy-text ivy--directory))
1650 (when (string-match "//\\'" ivy-text)
1651 (if (and default-directory
1652 (string-match "\\`[[:alpha:]]:/" default-directory))
1653 (ivy--cd (match-string 0 default-directory))
1655 (when (string-match "[[:alpha:]]:/$" ivy-text)
1656 (let ((drive-root (match-string 0 ivy-text)))
1657 (when (file-exists-p drive-root)
1658 (ivy--cd drive-root)))))
1659 (if (string-match "\\`~\\'" ivy-text)
1660 (ivy--cd (expand-file-name "~/")))))
1661 ((eq (ivy-state-collection ivy-last) 'internal-complete-buffer)
1662 (when (or (and (string-match "\\` " ivy-text)
1663 (not (string-match "\\` " ivy--old-text)))
1664 (and (string-match "\\` " ivy--old-text)
1665 (not (string-match "\\` " ivy-text))))
1666 (setq ivy--all-candidates
1667 (if (and (> (length ivy-text) 0)
1668 (eq (aref ivy-text 0)
1670 (ivy--buffer-list " ")
1671 (ivy--buffer-list "" ivy-use-virtual-buffers)))
1672 (setq ivy--old-re nil))))
1673 (ivy--insert-minibuffer
1674 (with-current-buffer (ivy-state-buffer ivy-last)
1676 (ivy--filter ivy-text ivy--all-candidates))))
1677 (setq ivy--old-text ivy-text))))
1679 (defun ivy--insert-minibuffer (text)
1680 "Insert TEXT into minibuffer with appropriate cleanup."
1681 (let ((resize-mini-windows nil)
1682 (update-fn (ivy-state-update-fn ivy-last))
1686 (funcall update-fn))
1687 (ivy--insert-prompt)
1688 ;; Do nothing if while-no-input was aborted.
1689 (when (stringp text)
1690 (let ((buffer-undo-list t))
1694 (when (display-graphic-p)
1695 (ivy--resize-minibuffer-to-fit))))
1697 (defun ivy--resize-minibuffer-to-fit ()
1698 "Resize the minibuffer window size to fit the text in the minibuffer."
1699 (with-selected-window (minibuffer-window)
1700 (if (fboundp 'window-text-pixel-size)
1701 (let ((text-height (cdr (window-text-pixel-size)))
1702 (body-height (window-body-height nil t)))
1703 (when (> text-height body-height)
1704 (window-resize nil (- text-height body-height) nil t t)))
1705 (let ((text-height (count-screen-lines))
1706 (body-height (window-body-height)))
1707 (when (> text-height body-height)
1708 (window-resize nil (- text-height body-height) nil t))))))
1710 (declare-function colir-blend-face-background "ext:colir")
1712 (defun ivy--add-face (str face)
1713 "Propertize STR with FACE.
1714 `font-lock-append-text-property' is used, since it's better than
1715 `propertize' or `add-face-text-property' in this case."
1719 (colir-blend-face-background 0 (length str) face str)
1720 (let ((foreground (face-foreground face)))
1722 (add-face-text-property
1724 `(:foreground ,foreground)
1729 (font-lock-append-text-property 0 (length str) 'face face str))))
1732 (declare-function flx-make-string-cache "ext:flx")
1733 (declare-function flx-score "ext:flx")
1735 (defvar ivy--flx-cache nil)
1737 (eval-after-load 'flx
1738 '(setq ivy--flx-cache (flx-make-string-cache)))
1740 (defun ivy-toggle-case-fold ()
1741 "Toggle the case folding between nil and auto.
1742 In any completion session, the case folding starts in auto:
1744 - when the input is all lower case, `case-fold-search' is t
1747 You can toggle this to make `case-fold-search' nil regardless of input."
1749 (setq ivy-case-fold-search
1750 (if ivy-case-fold-search
1753 ;; reset cache so that the candidate list updates
1754 (setq ivy--old-re nil))
1756 (defun ivy--filter (name candidates)
1757 "Return all items that match NAME in CANDIDATES.
1758 CANDIDATES are assumed to be static."
1759 (let ((re (funcall ivy--regex-function name)))
1760 (if (and (equal re ivy--old-re)
1762 ;; quick caching for "C-n", "C-p" etc.
1764 (let* ((re-str (if (listp re) (caar re) re))
1765 (matcher (ivy-state-matcher ivy-last))
1767 (and ivy-case-fold-search
1768 (string= name (downcase name))))
1771 (funcall matcher re candidates))
1774 (stringp ivy--old-re)
1775 (not (string-match "\\\\" ivy--old-re))
1776 (not (equal ivy--old-re ""))
1778 (if (string-match "\\\\)\\'" ivy--old-re)
1779 (substring ivy--old-re 0 -2)
1785 (lambda (x) (string-match re x))
1788 (let ((re-list (if (stringp re) (list (cons re t)) re))
1790 (dolist (re re-list)
1797 (let ((re-str (car re)))
1798 (lambda (x) (string-match re-str x)))
1801 (ivy--recompute-index name re-str cands)
1802 (setq ivy--old-re (if cands re-str ""))
1803 (setq ivy--old-cands (ivy--sort name cands))))))
1805 (defcustom ivy-sort-matches-functions-alist '((t . nil))
1806 "An alist of functions used to sort the matching candidates.
1808 This is different from `ivy-sort-functions-alist', which is used
1809 to sort the whole collection only once. The functions taken from
1810 here are instead used on each input change, but they are used
1811 only on already matching candidates, not on all of them.
1813 The alist KEY is a collection function or t to match previously
1814 not matched collection functions.
1816 The alist VAL is a sorting function with the signature of
1817 `ivy--prefix-sort'.")
1819 (defun ivy--sort-files-by-date (_name candidates)
1820 "Re-soft CANDIDATES according to file modification date."
1821 (let ((default-directory ivy--directory))
1822 (cl-sort (copy-sequence candidates)
1825 (nth 5 (file-attributes f2))
1826 (nth 5 (file-attributes f1)))))))
1828 (defun ivy--sort (name candidates)
1829 "Re-sort CANDIDATES by NAME.
1830 All CANDIDATES are assumed to match NAME."
1831 (let ((key (or (ivy-state-caller ivy-last)
1832 (when (functionp (ivy-state-collection ivy-last))
1833 (ivy-state-collection ivy-last))))
1835 (cond ((and (require 'flx nil 'noerror)
1836 (eq ivy--regex-function 'ivy--regex-fuzzy))
1837 (ivy--flx-sort name candidates))
1838 ((setq fun (cdr (or (assoc key ivy-sort-matches-functions-alist)
1839 (assoc t ivy-sort-matches-functions-alist))))
1840 (funcall fun name candidates))
1844 (defun ivy--prefix-sort (name candidates)
1845 "Re-sort CANDIDATES.
1846 Prefix matches to NAME are put ahead of the list."
1847 (if (or (string-match "^\\^" name) (string= name ""))
1849 (let ((re-prefix (concat "^" (funcall ivy--regex-function name)))
1852 (dolist (s candidates)
1853 (if (string-match re-prefix s)
1855 (push s res-noprefix)))
1857 (nreverse res-prefix)
1858 (nreverse res-noprefix)))))
1860 (defun ivy--recompute-index (name re-str cands)
1861 (let* ((caller (ivy-state-caller ivy-last))
1862 (func (or (and caller (cdr (assoc caller ivy-index-functions-alist)))
1863 (cdr (assoc t ivy-index-functions-alist))
1864 #'ivy-recompute-index-zero)))
1865 (unless (eq this-command 'ivy-resume)
1868 (cl-position (if (and (> (length re-str) 0)
1869 (eq ?^ (aref re-str 0)))
1870 (substring re-str 1)
1875 (concat re-str "/") cands
1877 (and (not (string= name ""))
1878 (not (and (require 'flx nil 'noerror)
1879 (eq ivy--regex-function 'ivy--regex-fuzzy)
1880 (< (length cands) 200)))
1882 (cl-position (nth ivy--index ivy--old-cands)
1884 (funcall func re-str cands))))
1885 (when (and (or (string= name "")
1887 (not (equal ivy--old-re "")))
1889 (or (ivy--preselect-index
1890 (ivy-state-preselect ivy-last)
1894 (defun ivy-recompute-index-swiper (_re-str cands)
1895 (let ((tail (nthcdr ivy--index ivy--old-cands))
1897 (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
1899 (while (and tail (null idx))
1900 ;; Compare with eq to handle equal duplicates in cands
1901 (setq idx (cl-position (pop tail) cands)))
1905 ;; already in ivy-state-buffer
1906 (let ((n (line-number-at-pos))
1910 (when (eq n (read (get-text-property 0 'display c)))
1915 (defun ivy-recompute-index-swiper-async (_re-str cands)
1916 (let ((tail (nthcdr ivy--index ivy--old-cands))
1918 (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
1920 (while (and tail (null idx))
1921 ;; Compare with `equal', since the collection is re-created
1922 ;; each time with `split-string'
1923 (setq idx (cl-position (pop tail) cands :test #'equal)))
1927 (defun ivy-recompute-index-zero (_re-str _cands)
1930 (defun ivy--flx-sort (name cands)
1931 "Sort according to closeness to string NAME the string list CANDS."
1934 (< (length cands) 200))
1935 (let* ((flx-name (if (string-match "^\\^" name)
1942 (let ((score (car (flx-score x flx-name ivy--flx-cache))))
1946 (if cands-with-score
1948 (sort cands-with-score
1950 (> (car x) (car y)))))
1956 (defcustom ivy-format-function 'ivy-format-function-default
1957 "Function to transform the list of candidates into a string.
1958 This string is inserted into the minibuffer."
1960 (const :tag "Default" ivy-format-function-default)
1961 (const :tag "Arrow prefix" ivy-format-function-arrow)
1962 (const :tag "Full line" ivy-format-function-line)))
1964 (defun ivy--truncate-string (str width)
1965 "Truncate STR to WIDTH."
1966 (if (> (string-width str) width)
1967 (concat (substring str 0 (min (- width 3)
1968 (- (length str) 3))) "...")
1971 (defun ivy--format-function-generic (selected-fn other-fn cand-pairs separator)
1972 "Transform CAND-PAIRS into a string for minibuffer.
1973 SELECTED-FN and OTHER-FN each take two string arguments.
1974 SEPARATOR is used to join the candidates."
1978 (let ((str (car pair))
1980 (curr (eq (cl-incf i) ivy--index)))
1982 (funcall selected-fn str extra)
1983 (funcall other-fn str extra))))
1987 (defun ivy-format-function-default (cand-pairs)
1988 "Transform CAND-PAIRS into a string for minibuffer."
1989 (ivy--format-function-generic
1991 (concat (ivy--add-face str 'ivy-current-match) extra))
1996 (defun ivy-format-function-arrow (cand-pairs)
1997 "Transform CAND-PAIRS into a string for minibuffer."
1998 (ivy--format-function-generic
2000 (concat "> " (ivy--add-face str 'ivy-current-match) extra))
2002 (concat " " str extra))
2006 (defun ivy-format-function-line (cand-pairs)
2007 "Transform CAND-PAIRS into a string for minibuffer."
2008 (ivy--format-function-generic
2010 (ivy--add-face (concat str extra "\n") 'ivy-current-match))
2012 (concat str extra "\n"))
2016 (defcustom ivy-minibuffer-faces
2017 '(ivy-minibuffer-match-face-1
2018 ivy-minibuffer-match-face-2
2019 ivy-minibuffer-match-face-3
2020 ivy-minibuffer-match-face-4)
2021 "List of `ivy' faces for minibuffer group matches.")
2023 (defun ivy--format-minibuffer-line (str)
2025 (str (copy-sequence str)))
2026 (when (eq ivy-display-style 'fancy)
2028 (setq ivy--old-re (funcall ivy--regex-function ivy-text)))
2029 (while (and (string-match ivy--old-re str start)
2030 (> (- (match-end 0) (match-beginning 0)) 0))
2031 (setq start (match-end 0))
2033 (while (<= i ivy--subexps)
2035 (cond ((zerop ivy--subexps)
2036 (cadr ivy-minibuffer-faces))
2038 (car ivy-minibuffer-faces))
2040 (nth (1+ (mod (+ i 2) (1- (length ivy-minibuffer-faces))))
2041 ivy-minibuffer-faces)))))
2042 (if (fboundp 'add-face-text-property)
2043 (add-face-text-property
2049 (font-lock-append-text-property
2058 (defun ivy--format (cands)
2059 "Return a string for CANDS suitable for display in the minibuffer.
2060 CANDS is a list of strings."
2061 (setq ivy--length (length cands))
2062 (when (>= ivy--index ivy--length)
2063 (setq ivy--index (max (1- ivy--length) 0)))
2065 (setq ivy--current "")
2066 (let* ((half-height (/ ivy-height 2))
2067 (start (max 0 (- ivy--index half-height)))
2068 (end (min (+ start (1- ivy-height)) ivy--length))
2069 (start (max 0 (min start (- end (1- ivy-height)))))
2070 (cands (cl-subseq cands start end))
2071 (index (- ivy--index start)))
2072 (cond (ivy--directory
2073 (setq cands (mapcar (lambda (x)
2074 (if (string-match-p "/\\'" x)
2075 (propertize x 'face 'ivy-subdir)
2078 ((eq (ivy-state-collection ivy-last) 'internal-complete-buffer)
2079 (setq cands (mapcar (lambda (x)
2080 (let ((b (get-buffer x)))
2082 (buffer-file-name b)
2083 (buffer-modified-p b))
2084 (propertize x 'face 'ivy-modified-buffer)
2087 (setq ivy--current (copy-sequence (nth index cands)))
2088 (let* ((ivy--index index)
2091 (cons (ivy--format-minibuffer-line cand) nil)) cands))
2092 (res (concat "\n" (funcall ivy-format-function cand-pairs))))
2093 (put-text-property 0 (length res) 'read-only nil res)
2096 (defvar ivy--virtual-buffers nil
2097 "Store the virtual buffers alist.")
2099 (defvar recentf-list)
2101 (defcustom ivy-virtual-abbreviate 'name
2102 "The mode of abbreviation for virtual buffer names."
2104 (const :tag "Only name" name)
2105 (const :tag "Full path" full)
2106 ;; eventually, uniquify
2109 (defun ivy--virtual-buffers ()
2110 "Adapted from `ido-add-virtual-buffers-to-list'."
2111 (unless recentf-mode
2113 (let ((bookmarks (and (boundp 'bookmark-alist)
2115 virtual-buffers name)
2116 (dolist (head (append
2118 (delete " - no file -"
2119 (delq nil (mapcar (lambda (bookmark)
2120 (cdr (assoc 'filename bookmark)))
2123 (if (eq ivy-virtual-abbreviate 'name)
2124 (file-name-nondirectory head)
2125 (expand-file-name head)))
2126 (when (equal name "")
2127 (setq name (file-name-nondirectory (directory-file-name head))))
2128 (when (equal name "")
2130 (and (not (equal name ""))
2131 (null (get-file-buffer head))
2132 (not (assoc name virtual-buffers))
2133 (push (cons name head) virtual-buffers)))
2134 (when virtual-buffers
2135 (dolist (comp virtual-buffers)
2136 (put-text-property 0 (length (car comp))
2139 (setq ivy--virtual-buffers (nreverse virtual-buffers))
2140 (mapcar #'car ivy--virtual-buffers))))
2142 (defun ivy--buffer-list (str &optional virtual)
2143 "Return the buffers that match STR.
2144 When VIRTUAL is non-nil, add virtual buffers."
2149 (if (with-current-buffer x
2151 (abbreviate-file-name default-directory)))
2152 (propertize x 'face 'ivy-remote)
2154 (all-completions str 'internal-complete-buffer))
2156 (ivy--virtual-buffers)))))
2158 (defun ivy--switch-buffer-action (buffer)
2160 BUFFER may be a string or nil."
2162 (if (zerop (length buffer))
2164 ivy-text nil 'force-same-window)
2165 (let ((virtual (assoc buffer ivy--virtual-buffers)))
2167 (not (get-buffer buffer)))
2168 (find-file (cdr virtual))
2170 buffer nil 'force-same-window))))))
2172 (defun ivy--switch-buffer-other-window-action (buffer)
2173 "Switch to BUFFER in other window.
2174 BUFFER may be a string or nil."
2175 (if (zerop (length buffer))
2176 (switch-to-buffer-other-window ivy-text)
2177 (let ((virtual (assoc buffer ivy--virtual-buffers)))
2179 (not (get-buffer buffer)))
2180 (find-file-other-window (cdr virtual))
2181 (switch-to-buffer-other-window buffer)))))
2183 (defun ivy--rename-buffer-action (buffer)
2185 (let ((new-name (read-string "Rename buffer (to new name): ")))
2186 (with-current-buffer buffer
2187 (rename-buffer new-name))))
2189 (defvar ivy-switch-buffer-map (make-sparse-keymap))
2196 (ivy--reset-state ivy-last))
2199 ivy--switch-buffer-other-window-action
2202 ivy--rename-buffer-action
2206 (defun ivy-switch-buffer ()
2207 "Switch to another buffer."
2210 (call-interactively 'switch-to-buffer)
2211 (let ((this-command 'ivy-switch-buffer))
2212 (ivy-read "Switch to buffer: " 'internal-complete-buffer
2213 :preselect (buffer-name (other-buffer (current-buffer)))
2214 :action #'ivy--switch-buffer-action
2215 :keymap ivy-switch-buffer-map))))
2218 (defun ivy-recentf ()
2219 "Find a file on `recentf-list'."
2221 (ivy-read "Recentf: " recentf-list
2227 (defun ivy-yank-word ()
2228 "Pull next word from buffer into search string."
2233 (le (line-end-position)))
2237 (setq amend (buffer-substring-no-properties pt (point))))))
2239 (insert (replace-regexp-in-string " +" " " amend)))))
2241 (defun ivy-kill-ring-save ()
2242 "Store the current candidates into the kill ring.
2243 If the region is active, forward to `kill-ring-save' instead."
2245 (if (region-active-p)
2246 (call-interactively 'kill-ring-save)
2253 (defun ivy-insert-current ()
2254 "Make the current candidate into current input.
2255 Don't finish completion."
2257 (delete-minibuffer-contents)
2258 (if (and ivy--directory
2259 (string-match "/$" ivy--current))
2260 (insert (substring ivy--current 0 -1))
2261 (insert ivy--current)))
2263 (defun ivy-toggle-fuzzy ()
2264 "Toggle the re builder between `ivy--regex-fuzzy' and `ivy--regex-plus'."
2266 (setq ivy--old-re nil)
2267 (if (eq ivy--regex-function 'ivy--regex-fuzzy)
2268 (setq ivy--regex-function 'ivy--regex-plus)
2269 (setq ivy--regex-function 'ivy--regex-fuzzy)))
2271 (defun ivy-reverse-i-search ()
2272 "Enter a recursive `ivy-read' session using the current history.
2273 The selected history element will be inserted into the minibuffer."
2275 (let ((enable-recursive-minibuffers t)
2276 (history (symbol-value (ivy-state-history ivy-last)))
2278 (ivy-recursive-restore nil))
2279 (ivy-read "Reverse-i-search: "
2283 (setq ivy-last old-last))
2284 (delete-minibuffer-contents)
2285 (insert (substring-no-properties x))
2288 (defun ivy-restrict-to-matches ()
2289 "Restrict candidates to current matches and erase input."
2291 (delete-minibuffer-contents)
2292 (setq ivy--all-candidates
2293 (ivy--filter ivy-text ivy--all-candidates)))
2296 (defvar-local ivy-occur-last nil
2297 "Buffer-local value of `ivy-last'.
2298 Can't re-use `ivy-last' because using e.g. `swiper' in the same
2299 buffer would modify `ivy-last'.")
2301 (defvar ivy-occur-mode-map
2302 (let ((map (make-sparse-keymap)))
2303 (define-key map [mouse-1] 'ivy-occur-click)
2304 (define-key map (kbd "RET") 'ivy-occur-press)
2305 (define-key map (kbd "j") 'next-line)
2306 (define-key map (kbd "k") 'previous-line)
2307 (define-key map (kbd "h") 'backward-char)
2308 (define-key map (kbd "l") 'forward-char)
2309 (define-key map (kbd "g") 'ivy-occur-press)
2310 (define-key map (kbd "a") 'ivy-occur-read-action)
2311 (define-key map (kbd "o") 'ivy-occur-dispatch)
2312 (define-key map (kbd "q") 'quit-window)
2314 "Keymap for Ivy Occur mode.")
2316 (define-derived-mode ivy-occur-mode fundamental-mode "Ivy-Occur"
2317 "Major mode for output from \\[ivy-occur].
2319 \\{ivy-occur-mode-map}")
2321 (defvar ivy-occur-grep-mode-map
2322 (let ((map (copy-keymap ivy-occur-mode-map)))
2323 (define-key map (kbd "C-x C-q") 'ivy-wgrep-change-to-wgrep-mode)
2325 "Keymap for Ivy Occur Grep mode.")
2327 (define-derived-mode ivy-occur-grep-mode grep-mode "Ivy-Occur"
2328 "Major mode for output from \\[ivy-occur].
2330 \\{ivy-occur-grep-mode-map}")
2332 (defvar counsel-git-grep-cmd)
2335 "Stop completion and put the current matches into a new buffer.
2337 The new buffer remembers current action(s).
2339 While in the *ivy-occur* buffer, selecting a candidate with RET or
2340 a mouse click will call the appropriate action for that candidate.
2342 There is no limit on the number of *ivy-occur* buffers."
2345 (generate-new-buffer
2346 (format "*ivy-occur%s \"%s\"*"
2348 (if (setq caller (ivy-state-caller ivy-last))
2349 (concat " " (prin1-to-string caller))
2352 (do-grep (eq (ivy-state-caller ivy-last) 'counsel-git-grep)))
2353 (with-current-buffer buffer
2356 (setq ivy--old-cands
2358 (shell-command-to-string
2359 (format counsel-git-grep-cmd ivy--old-re))
2362 (ivy-occur-grep-mode))
2364 (setf (ivy-state-text ivy-last) ivy-text)
2365 (setq ivy-occur-last ivy-last)
2366 (setq-local ivy--directory ivy--directory)
2367 (let ((inhibit-read-only t))
2370 ;; Need precise number of header lines for `wgrep' to work.
2371 (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
2372 default-directory)))
2373 (insert (format "%d candidates:\n" (length ivy--old-cands)))
2374 (dolist (cand ivy--old-cands)
2375 (let ((str (if do-grep
2377 (concat " " cand))))
2378 (add-text-properties
2382 help-echo "mouse-1: call ivy-action")
2384 (insert str "\n")))))
2385 (ivy-exit-with-action
2386 `(lambda (_) (pop-to-buffer ,buffer)))))
2388 (declare-function wgrep-change-to-wgrep-mode "ext:wgrep")
2390 (defun ivy-wgrep-change-to-wgrep-mode ()
2391 "Forward to `wgrep-change-to-wgrep-mode'."
2393 (if (require 'wgrep nil 'noerror)
2394 (wgrep-change-to-wgrep-mode)
2395 (error "Package wgrep isn't installed")))
2397 (defun ivy-occur-read-action ()
2398 "Select one of the available actions as the current one."
2400 (let ((ivy-last ivy-occur-last))
2403 (defun ivy-occur-dispatch ()
2404 "Call one of the available actions on the current item."
2406 (let* ((state-action (ivy-state-action ivy-occur-last))
2407 (actions (if (symbolp state-action)
2409 (copy-sequence state-action))))
2412 (ivy-occur-read-action)
2414 (setf (ivy-state-action ivy-occur-last) actions))))
2416 (defun ivy-occur-click (event)
2417 "Execute action for the current candidate.
2418 EVENT gives the mouse position."
2420 (let ((window (posn-window (event-end event)))
2421 (pos (posn-point (event-end event))))
2422 (with-current-buffer (window-buffer window)
2424 (ivy-occur-press))))
2426 (defun ivy-occur-press ()
2427 "Execute action for the current candidate."
2430 (when (save-excursion
2432 (looking-at "\\(?:./\\| \\)\\(.*\\)$"))
2433 (let* ((ivy-last ivy-occur-last)
2434 (ivy-text (ivy-state-text ivy-last))
2435 (str (buffer-substring
2438 (coll (ivy-state-collection ivy-last))
2439 (action (ivy--get-action ivy-last))
2443 (if (and (consp coll)
2445 (cdr (assoc str coll))
2447 (if (memq (ivy-state-caller ivy-last)
2448 '(swiper counsel-git-grep))
2449 (with-current-buffer (window-buffer (selected-window))
2451 (swiper--add-overlays
2452 (ivy--regex ivy-text)
2453 (line-beginning-position)
2456 (run-at-time 0.5 nil 'swiper--cleanup))
2457 (pulse-momentary-highlight-one-line (point)))))))
2461 ;;; ivy.el ends here