]> code.delx.au - gnu-emacs-elpa/blob - ivy.el
Don't re-anchor to matching old candidate if flx is on
[gnu-emacs-elpa] / ivy.el
1 ;;; ivy.el --- Incremental Vertical completYon -*- 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 ;; Package-Requires: ((emacs "24.1"))
8 ;; Keywords: matching
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
22 ;; For a full copy of the GNU General Public License
23 ;; see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26 ;;
27 ;; This package provides `ivy-read' as an alternative to
28 ;; `completing-read' and similar functions.
29 ;;
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'.
33 ;;
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\\)".
37
38 ;;; Code:
39 (require 'cl-lib)
40
41 ;;* Customization
42 (defgroup ivy nil
43 "Incremental vertical completion."
44 :group 'convenience)
45
46 (defface ivy-current-match
47 '((((class color) (background light))
48 :background "#1a4b77" :foreground "white")
49 (((class color) (background dark))
50 :background "#65a7e2" :foreground "black"))
51 "Face used by Ivy for highlighting first match.")
52
53 (defface ivy-confirm-face
54 '((t :foreground "ForestGreen" :inherit minibuffer-prompt))
55 "Face used by Ivy to issue a confirmation prompt.")
56
57 (defface ivy-match-required-face
58 '((t :foreground "red" :inherit minibuffer-prompt))
59 "Face used by Ivy to issue a match required prompt.")
60
61 (defface ivy-subdir
62 '((t (:inherit 'dired-directory)))
63 "Face used by Ivy for highlighting subdirs in the alternatives.")
64
65 (defface ivy-remote
66 '((t (:foreground "#110099")))
67 "Face used by Ivy for highlighting remotes in the alternatives.")
68
69 (defcustom ivy-height 10
70 "Number of lines for the minibuffer window."
71 :type 'integer)
72
73 (defcustom ivy-count-format "%-4d "
74 "The style of showing the current candidate count for `ivy-read'.
75 Set this to \"\" if you don't want the count. You can also set
76 it to e.g. \"(%d/%d) \" if you want to see both the candidate
77 index and the candidate count."
78 :type '(choice
79 (const :tag "Count disabled" "")
80 (const :tag "Count matches" "%-4d ")
81 (const :tag "Count matches and show current match" "(%d/%d) ")
82 string))
83
84 (defcustom ivy-wrap nil
85 "Whether to wrap around after the first and last candidate."
86 :type 'boolean)
87
88 (defcustom ivy-display-style (unless (version< emacs-version "24.5") 'fancy)
89 "The style for formatting the minibuffer.
90
91 By default, the matched strings will be copied as they are.
92
93 With the fancy method, the matching parts of the regexp will be
94 additionally highlighted, just like `swiper' does it.
95
96 This setting depends on `add-face-text-property' - a C function
97 available as of 24.5. It will behave poorly in earlier Emacs
98 versions."
99 :type '(choice
100 (const :tag "Plain" nil)
101 (const :tag "Fancy" fancy)))
102
103 (defcustom ivy-on-del-error-function 'minibuffer-keyboard-quit
104 "The handler for when `ivy-backward-delete-char' throws.
105 This is usually meant as a quick exit out of the minibuffer."
106 :type 'function)
107
108 (defcustom ivy-extra-directories '("../" "./")
109 "Add this to the front of the list when completing file names.
110 Only \"./\" and \"../\" apply here. They appear in reverse order."
111 :type 'list)
112
113 (defcustom ivy-use-virtual-buffers nil
114 "When non-nil, add `recentf-mode' and bookmarks to `ivy-switch-buffer'."
115 :type 'boolean)
116
117 (defvar ivy--actions-list nil
118 "A list of extra actions per command.")
119
120 (defun ivy-set-actions (cmd actions)
121 "Set CMD extra exit points to ACTIONS."
122 (setq ivy--actions-list
123 (plist-put ivy--actions-list cmd actions)))
124
125 ;;* Keymap
126 (require 'delsel)
127 (defvar ivy-minibuffer-map
128 (let ((map (make-sparse-keymap)))
129 (define-key map (kbd "C-m") 'ivy-done)
130 (define-key map (kbd "C-M-m") 'ivy-call)
131 (define-key map (kbd "C-j") 'ivy-alt-done)
132 (define-key map (kbd "C-M-j") 'ivy-immediate-done)
133 (define-key map (kbd "TAB") 'ivy-partial-or-done)
134 (define-key map (kbd "C-n") 'ivy-next-line)
135 (define-key map (kbd "C-p") 'ivy-previous-line)
136 (define-key map (kbd "<down>") 'ivy-next-line)
137 (define-key map (kbd "<up>") 'ivy-previous-line)
138 (define-key map (kbd "C-s") 'ivy-next-line-or-history)
139 (define-key map (kbd "C-r") 'ivy-reverse-i-search)
140 (define-key map (kbd "SPC") 'self-insert-command)
141 (define-key map (kbd "DEL") 'ivy-backward-delete-char)
142 (define-key map (kbd "M-DEL") 'ivy-backward-kill-word)
143 (define-key map (kbd "C-d") 'ivy-delete-char)
144 (define-key map (kbd "C-f") 'ivy-forward-char)
145 (define-key map (kbd "M-d") 'ivy-kill-word)
146 (define-key map (kbd "M-<") 'ivy-beginning-of-buffer)
147 (define-key map (kbd "M->") 'ivy-end-of-buffer)
148 (define-key map (kbd "M-n") 'ivy-next-history-element)
149 (define-key map (kbd "M-p") 'ivy-previous-history-element)
150 (define-key map (kbd "C-g") 'minibuffer-keyboard-quit)
151 (define-key map (kbd "C-v") 'ivy-scroll-up-command)
152 (define-key map (kbd "M-v") 'ivy-scroll-down-command)
153 (define-key map (kbd "C-M-n") 'ivy-next-line-and-call)
154 (define-key map (kbd "C-M-p") 'ivy-previous-line-and-call)
155 (define-key map (kbd "M-q") 'ivy-toggle-regexp-quote)
156 (define-key map (kbd "M-j") 'ivy-yank-word)
157 (define-key map (kbd "M-i") 'ivy-insert-current)
158 (define-key map (kbd "C-o") 'hydra-ivy/body)
159 (define-key map (kbd "M-o") 'ivy-dispatching-done)
160 (define-key map (kbd "C-M-o") 'ivy-dispatching-call)
161 (define-key map (kbd "C-k") 'ivy-kill-line)
162 (define-key map (kbd "S-SPC") 'ivy-restrict-to-matches)
163 (define-key map (kbd "M-w") 'ivy-kill-ring-save)
164 (define-key map (kbd "C-'") 'ivy-avy)
165 map)
166 "Keymap used in the minibuffer.")
167 (autoload 'hydra-ivy/body "ivy-hydra" "" t)
168
169 (defvar ivy-mode-map
170 (let ((map (make-sparse-keymap)))
171 (define-key map [remap switch-to-buffer] 'ivy-switch-buffer)
172 map)
173 "Keymap for `ivy-mode'.")
174
175 ;;* Globals
176 (cl-defstruct ivy-state
177 prompt collection
178 predicate require-match initial-input
179 history preselect keymap update-fn sort
180 ;; The window in which `ivy-read' was called
181 window
182 action
183 unwind
184 re-builder
185 matcher
186 ;; When this is non-nil, call it for each input change to get new candidates
187 dynamic-collection
188 caller)
189
190 (defvar ivy-last nil
191 "The last parameters passed to `ivy-read'.
192
193 This should eventually become a stack so that you could use
194 `ivy-read' recursively.")
195
196 (defsubst ivy-set-action (action)
197 (setf (ivy-state-action ivy-last) action))
198
199 (defvar ivy-history nil
200 "History list of candidates entered in the minibuffer.
201
202 Maximum length of the history list is determined by the value
203 of `history-length', which see.")
204
205 (defvar ivy--directory nil
206 "Current directory when completing file names.")
207
208 (defvar ivy--length 0
209 "Store the amount of viable candidates.")
210
211 (defvar ivy-text ""
212 "Store the user's string as it is typed in.")
213
214 (defvar ivy--current ""
215 "Current candidate.")
216
217 (defvar ivy--index 0
218 "Store the index of the current candidate.")
219
220 (defvar ivy-exit nil
221 "Store 'done if the completion was successfully selected.
222 Otherwise, store nil.")
223
224 (defvar ivy--all-candidates nil
225 "Store the candidates passed to `ivy-read'.")
226
227 (defvar ivy--default nil
228 "Default initial input.")
229
230 (defvar ivy--prompt nil
231 "Store the format-style prompt.
232 When non-nil, it should contain one %d.")
233
234 (defvar ivy--prompt-extra ""
235 "Temporary modifications to the prompt.")
236
237 (defvar ivy--old-re nil
238 "Store the old regexp.")
239
240 (defvar ivy--old-cands nil
241 "Store the candidates matched by `ivy--old-re'.")
242
243 (defvar ivy--regex-function 'ivy--regex
244 "Current function for building a regex.")
245
246 (defvar ivy--subexps 0
247 "Number of groups in the current `ivy--regex'.")
248
249 (defvar ivy--full-length nil
250 "When :dynamic-collection is non-nil, this can be the total amount of candidates.")
251
252 (defvar ivy--old-text ""
253 "Store old `ivy-text' for dynamic completion.")
254
255 (defvar ivy-case-fold-search 'auto
256 "Store the current overriding `case-fold-search'.")
257
258 (defvar Info-current-file)
259
260 (defmacro ivy-quit-and-run (&rest body)
261 "Quit the minibuffer and run BODY afterwards."
262 `(progn
263 (put 'quit 'error-message "")
264 (run-at-time nil nil
265 (lambda ()
266 (put 'quit 'error-message "Quit")
267 ,@body))
268 (minibuffer-keyboard-quit)))
269
270 (defmacro with-ivy-window (&rest body)
271 "Execute BODY in the window from which `ivy-read' was called."
272 (declare (indent 0)
273 (debug t))
274 `(with-selected-window (ivy-state-window ivy-last)
275 ,@body))
276
277 (defun ivy--done (text)
278 "Insert TEXT and exit minibuffer."
279 (if (and ivy--directory
280 (not (eq (ivy-state-history ivy-last) 'grep-files-history)))
281 (insert (setq ivy--current (expand-file-name
282 text ivy--directory)))
283 (insert (setq ivy--current text)))
284 (setq ivy-exit 'done)
285 (exit-minibuffer))
286
287 ;;* Commands
288 (defun ivy-done ()
289 "Exit the minibuffer with the selected candidate."
290 (interactive)
291 (delete-minibuffer-contents)
292 (cond ((> ivy--length 0)
293 (ivy--done ivy--current))
294 ((memq (ivy-state-collection ivy-last)
295 '(read-file-name-internal internal-complete-buffer))
296 (if (or (not (eq confirm-nonexistent-file-or-buffer t))
297 (equal " (confirm)" ivy--prompt-extra))
298 (ivy--done ivy-text)
299 (setq ivy--prompt-extra " (confirm)")
300 (insert ivy-text)
301 (ivy--exhibit)))
302 ((memq (ivy-state-require-match ivy-last)
303 '(nil confirm confirm-after-completion))
304 (ivy--done ivy-text))
305 (t
306 (setq ivy--prompt-extra " (match required)")
307 (insert ivy-text)
308 (ivy--exhibit))))
309
310 (defun ivy-read-action ()
311 "Change the action to one of the available ones."
312 (interactive)
313 (let ((actions (ivy-state-action ivy-last)))
314 (unless (null (ivy--actionp actions))
315 (let* ((hint (concat ivy--current
316 "\n"
317 (mapconcat
318 (lambda (x)
319 (format "%s: %s"
320 (propertize
321 (car x)
322 'face 'font-lock-builtin-face)
323 (nth 2 x)))
324 (cdr actions)
325 "\n")
326 "\n"))
327 (key (string (read-key hint)))
328 (action-idx (cl-position-if
329 (lambda (x) (equal (car x) key))
330 (cdr actions))))
331 (cond ((string= key "\a"))
332 ((null action-idx)
333 (error "%s is not bound" key))
334 (t
335 (message "")
336 (setcar actions (1+ action-idx))
337 (ivy-set-action actions)))))))
338
339 (defun ivy-dispatching-done ()
340 "Select one of the available actions and call `ivy-done'."
341 (interactive)
342 (ivy-read-action)
343 (ivy-done))
344
345 (defun ivy-dispatching-call ()
346 "Select one of the available actions and call `ivy-call'."
347 (interactive)
348 (let ((actions (copy-sequence (ivy-state-action ivy-last))))
349 (ivy-read-action)
350 (ivy-call)
351 (ivy-set-action actions)))
352
353 (defun ivy-build-tramp-name (x)
354 "Reconstruct X into a path.
355 Is is a cons cell, related to `tramp-get-completion-function'."
356 (let ((user (car x))
357 (domain (cadr x)))
358 (if user
359 (concat user "@" domain)
360 domain)))
361
362 (declare-function tramp-get-completion-function "tramp")
363 (declare-function Info-find-node "info")
364
365 (defun ivy-alt-done (&optional arg)
366 "Exit the minibuffer with the selected candidate.
367 When ARG is t, exit with current text, ignoring the candidates."
368 (interactive "P")
369 (let (dir)
370 (cond (arg
371 (ivy-immediate-done))
372 ((and ivy--directory
373 (or
374 (and
375 (not (string= ivy--current "./"))
376 (cl-plusp ivy--length)
377 (file-directory-p
378 (setq dir (expand-file-name
379 ivy--current ivy--directory))))))
380 (ivy--cd dir)
381 (ivy--exhibit))
382 ((eq (ivy-state-collection ivy-last) 'Info-read-node-name-1)
383 (if (or (equal ivy--current "(./)")
384 (equal ivy--current "(../)"))
385 (ivy-quit-and-run
386 (ivy-read "Go to file: " 'read-file-name-internal
387 :action (lambda (x)
388 (Info-find-node
389 (expand-file-name x ivy--directory)
390 "Top"))))
391 (ivy-done)))
392 ((and ivy--directory
393 (string-match "\\`/[^/]+:.*:.*\\'" ivy-text))
394 (ivy-done))
395 ((and ivy--directory
396 (string-match
397 "\\`/\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
398 ivy-text))
399 (let ((method (match-string 1 ivy-text))
400 (user (match-string 2 ivy-text))
401 (rest (match-string 3 ivy-text))
402 res)
403 (require 'tramp)
404 (dolist (x (tramp-get-completion-function method))
405 (setq res (append res (funcall (car x) (cadr x)))))
406 (setq res (delq nil res))
407 (when user
408 (dolist (x res)
409 (setcar x user)))
410 (setq res (cl-delete-duplicates res :test #'equal))
411 (let* ((old-ivy-last ivy-last)
412 (enable-recursive-minibuffers t)
413 (host (ivy-read "Find File: "
414 (mapcar #'ivy-build-tramp-name res)
415 :initial-input rest)))
416 (setq ivy-last old-ivy-last)
417 (when host
418 (setq ivy--directory "/")
419 (ivy--cd (concat "/" method ":" host ":"))))))
420 (t
421 (ivy-done)))))
422
423 (defcustom ivy-tab-space nil
424 "When non-nil, `ivy-partial-or-done' should insert a space."
425 :type 'boolean)
426
427 (defun ivy-partial-or-done ()
428 "Complete the minibuffer text as much as possible.
429 If the text hasn't changed as a result, forward to `ivy-alt-done'."
430 (interactive)
431 (if (and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
432 (string-match "\\`/" ivy-text))
433 (let ((default-directory ivy--directory))
434 (minibuffer-complete)
435 (setq ivy-text (ivy--input))
436 (when (and (file-directory-p ivy-text)
437 (= ivy--length 1))
438 (ivy--cd (expand-file-name ivy-text))))
439 (or (ivy-partial)
440 (when (or (eq this-command last-command)
441 (eq ivy--length 1))
442 (ivy-alt-done)))))
443
444 (defun ivy-partial ()
445 "Complete the minibuffer text as much as possible."
446 (interactive)
447 (let* ((parts (or (split-string ivy-text " " t) (list "")))
448 (postfix (car (last parts)))
449 (completion-ignore-case t)
450 (startp (string-match "^\\^" postfix))
451 (new (try-completion (if startp
452 (substring postfix 1)
453 postfix)
454 (mapcar (lambda (str) (substring str (string-match postfix str)))
455 ivy--old-cands))))
456 (cond ((eq new t) nil)
457 ((string= new ivy-text) nil)
458 (new
459 (delete-region (minibuffer-prompt-end) (point-max))
460 (setcar (last parts)
461 (if startp
462 (concat "^" new)
463 new))
464 (insert (mapconcat #'identity parts " ")
465 (if ivy-tab-space " " ""))
466 t))))
467
468 (defun ivy-immediate-done ()
469 "Exit the minibuffer with the current input."
470 (interactive)
471 (delete-minibuffer-contents)
472 (insert (setq ivy--current ivy-text))
473 (setq ivy-exit 'done)
474 (exit-minibuffer))
475
476 ;;;###autoload
477 (defun ivy-resume ()
478 "Resume the last completion session."
479 (interactive)
480 (ivy-read
481 (ivy-state-prompt ivy-last)
482 (ivy-state-collection ivy-last)
483 :predicate (ivy-state-predicate ivy-last)
484 :require-match (ivy-state-require-match ivy-last)
485 :initial-input ivy-text
486 :history (ivy-state-history ivy-last)
487 :preselect (unless (eq (ivy-state-collection ivy-last)
488 'read-file-name-internal)
489 ivy--current)
490 :keymap (ivy-state-keymap ivy-last)
491 :update-fn (ivy-state-update-fn ivy-last)
492 :sort (ivy-state-sort ivy-last)
493 :action (ivy-state-action ivy-last)
494 :unwind (ivy-state-unwind ivy-last)
495 :re-builder (ivy-state-re-builder ivy-last)
496 :matcher (ivy-state-matcher ivy-last)
497 :dynamic-collection (ivy-state-dynamic-collection ivy-last)))
498
499 (defvar ivy-calling nil
500 "When non-nil, call the current action when `ivy--index' changes.")
501
502 (defun ivy-set-index (index)
503 "Set `ivy--index' to INDEX."
504 (setq ivy--index index)
505 (when ivy-calling
506 (ivy--exhibit)
507 (ivy-call)))
508
509 (defun ivy-beginning-of-buffer ()
510 "Select the first completion candidate."
511 (interactive)
512 (ivy-set-index 0))
513
514 (defun ivy-end-of-buffer ()
515 "Select the last completion candidate."
516 (interactive)
517 (ivy-set-index (1- ivy--length)))
518
519 (defun ivy-scroll-up-command ()
520 "Scroll the candidates upward by the minibuffer height."
521 (interactive)
522 (ivy-set-index (min (+ ivy--index ivy-height)
523 (1- ivy--length))))
524
525 (defun ivy-scroll-down-command ()
526 "Scroll the candidates downward by the minibuffer height."
527 (interactive)
528 (ivy-set-index (max (- ivy--index ivy-height)
529 0)))
530
531 (defun ivy-minibuffer-grow ()
532 "Grow the minibuffer window by 1 line."
533 (interactive)
534 (setq-local max-mini-window-height
535 (cl-incf ivy-height)))
536
537 (defun ivy-minibuffer-shrink ()
538 "Shrink the minibuffer window by 1 line."
539 (interactive)
540 (unless (<= ivy-height 2)
541 (setq-local max-mini-window-height
542 (cl-decf ivy-height))
543 (window-resize (selected-window) -1)))
544
545 (defun ivy-next-line (&optional arg)
546 "Move cursor vertically down ARG candidates."
547 (interactive "p")
548 (setq arg (or arg 1))
549 (let ((index (+ ivy--index arg)))
550 (if (> index (1- ivy--length))
551 (if ivy-wrap
552 (ivy-beginning-of-buffer)
553 (ivy-set-index (1- ivy--length)))
554 (ivy-set-index index))))
555
556 (defun ivy-next-line-or-history (&optional arg)
557 "Move cursor vertically down ARG candidates.
558 If the input is empty, select the previous history element instead."
559 (interactive "p")
560 (when (string= ivy-text "")
561 (ivy-previous-history-element 1))
562 (ivy-next-line arg))
563
564 (defun ivy-previous-line (&optional arg)
565 "Move cursor vertically up ARG candidates."
566 (interactive "p")
567 (setq arg (or arg 1))
568 (let ((index (- ivy--index arg)))
569 (if (< index 0)
570 (if ivy-wrap
571 (ivy-end-of-buffer)
572 (ivy-set-index 0))
573 (ivy-set-index index))))
574
575 (defun ivy-previous-line-or-history (arg)
576 "Move cursor vertically up ARG candidates.
577 If the input is empty, select the previous history element instead."
578 (interactive "p")
579 (when (string= ivy-text "")
580 (ivy-previous-history-element 1))
581 (ivy-previous-line arg))
582
583 (defun ivy-toggle-calling ()
584 "Flip `ivy-calling'."
585 (interactive)
586 (when (setq ivy-calling (not ivy-calling))
587 (ivy-call)))
588
589 (defun ivy--get-action (state)
590 "Get the action function from STATE."
591 (let ((action (ivy-state-action state)))
592 (when action
593 (if (functionp action)
594 action
595 (cadr (nth (car action) action))))))
596
597 (defun ivy--actionp (x)
598 "Return non-nil when X is a list of actions."
599 (and x (listp x) (not (eq (car x) 'closure))))
600
601 (defun ivy-next-action ()
602 "When the current action is a list, scroll it forwards."
603 (interactive)
604 (let ((action (ivy-state-action ivy-last)))
605 (when (ivy--actionp action)
606 (unless (>= (car action) (1- (length action)))
607 (cl-incf (car action))))))
608
609 (defun ivy-prev-action ()
610 "When the current action is a list, scroll it backwards."
611 (interactive)
612 (let ((action (ivy-state-action ivy-last)))
613 (when (ivy--actionp action)
614 (unless (<= (car action) 1)
615 (cl-decf (car action))))))
616
617 (defun ivy-action-name ()
618 "Return the name associated with the current action."
619 (let ((action (ivy-state-action ivy-last)))
620 (if (ivy--actionp action)
621 (format "[%d/%d] %s"
622 (car action)
623 (1- (length action))
624 (nth 2 (nth (car action) action)))
625 "[1/1] default")))
626
627 (defun ivy-call ()
628 "Call the current action without exiting completion."
629 (interactive)
630 (let ((action (ivy--get-action ivy-last)))
631 (when action
632 (let* ((collection (ivy-state-collection ivy-last))
633 (x (if (and (consp collection)
634 (consp (car collection)))
635 (cdr (assoc ivy--current collection))
636 (if (equal ivy--current "")
637 ivy-text
638 ivy--current))))
639 (prog1 (funcall action x)
640 (unless (or (eq ivy-exit 'done)
641 (equal (selected-window)
642 (active-minibuffer-window))
643 (null (active-minibuffer-window)))
644 (select-window (active-minibuffer-window))))))))
645
646 (defun ivy-next-line-and-call (&optional arg)
647 "Move cursor vertically down ARG candidates.
648 Call the permanent action if possible."
649 (interactive "p")
650 (ivy-next-line arg)
651 (ivy--exhibit)
652 (ivy-call))
653
654 (defun ivy-previous-line-and-call (&optional arg)
655 "Move cursor vertically down ARG candidates.
656 Call the permanent action if possible."
657 (interactive "p")
658 (ivy-previous-line arg)
659 (ivy--exhibit)
660 (ivy-call))
661
662 (defun ivy-previous-history-element (arg)
663 "Forward to `previous-history-element' with ARG."
664 (interactive "p")
665 (previous-history-element arg)
666 (ivy--cd-maybe)
667 (move-end-of-line 1)
668 (ivy--maybe-scroll-history))
669
670 (defun ivy-next-history-element (arg)
671 "Forward to `next-history-element' with ARG."
672 (interactive "p")
673 (next-history-element arg)
674 (ivy--cd-maybe)
675 (move-end-of-line 1)
676 (ivy--maybe-scroll-history))
677
678 (defun ivy--cd-maybe ()
679 "Check if the current input points to a different directory.
680 If so, move to that directory, while keeping only the file name."
681 (when ivy--directory
682 (let* ((input (expand-file-name (ivy--input)))
683 (file (file-name-nondirectory input))
684 (dir (expand-file-name (file-name-directory input))))
685 (if (string= dir ivy--directory)
686 (progn
687 (delete-minibuffer-contents)
688 (insert file))
689 (ivy--cd dir)
690 (insert file)))))
691
692 (defun ivy--maybe-scroll-history ()
693 "If the selected history element has an index, scroll there."
694 (let ((idx (ignore-errors
695 (get-text-property
696 (minibuffer-prompt-end)
697 'ivy-index))))
698 (when idx
699 (ivy--exhibit)
700 (setq ivy--index idx))))
701
702 (defun ivy--cd (dir)
703 "When completing file names, move to directory DIR."
704 (if (null ivy--directory)
705 (error "Unexpected")
706 (setq ivy--old-cands nil)
707 (setq ivy--old-re nil)
708 (setq ivy--index 0)
709 (setq ivy--all-candidates
710 (ivy--sorted-files (setq ivy--directory dir)))
711 (setq ivy-text "")
712 (delete-minibuffer-contents)))
713
714 (defun ivy-backward-delete-char ()
715 "Forward to `backward-delete-char'.
716 On error (read-only), call `ivy-on-del-error-function'."
717 (interactive)
718 (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
719 (progn
720 (ivy--cd (file-name-directory
721 (directory-file-name
722 (expand-file-name
723 ivy--directory))))
724 (ivy--exhibit))
725 (condition-case nil
726 (backward-delete-char 1)
727 (error
728 (when ivy-on-del-error-function
729 (funcall ivy-on-del-error-function))))))
730
731 (defun ivy-delete-char (arg)
732 "Forward to `delete-char' ARG."
733 (interactive "p")
734 (unless (= (point) (line-end-position))
735 (delete-char arg)))
736
737 (defun ivy-forward-char (arg)
738 "Forward to `forward-char' ARG."
739 (interactive "p")
740 (unless (= (point) (line-end-position))
741 (forward-char arg)))
742
743 (defun ivy-kill-word (arg)
744 "Forward to `kill-word' ARG."
745 (interactive "p")
746 (unless (= (point) (line-end-position))
747 (kill-word arg)))
748
749 (defun ivy-kill-line ()
750 "Forward to `kill-line'."
751 (interactive)
752 (if (eolp)
753 (kill-region (minibuffer-prompt-end) (point))
754 (kill-line)))
755
756 (defun ivy-backward-kill-word ()
757 "Forward to `backward-kill-word'."
758 (interactive)
759 (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
760 (progn
761 (ivy--cd (file-name-directory
762 (directory-file-name
763 (expand-file-name
764 ivy--directory))))
765 (ivy--exhibit))
766 (ignore-errors
767 (let ((pt (point)))
768 (forward-word -1)
769 (delete-region (point) pt)))))
770
771 (defvar ivy--regexp-quote 'regexp-quote
772 "Store the regexp quoting state.")
773
774 (defun ivy-toggle-regexp-quote ()
775 "Toggle the regexp quoting."
776 (interactive)
777 (setq ivy--old-re nil)
778 (cl-rotatef ivy--regex-function ivy--regexp-quote))
779
780 (defvar avy-all-windows)
781 (defvar avy-action)
782 (defvar avy-keys)
783 (defvar avy-keys-alist)
784 (defvar avy-style)
785 (defvar avy-styles-alist)
786 (declare-function avy--process "ext:avy")
787 (declare-function avy--style-fn "ext:avy")
788
789 (eval-after-load 'avy
790 '(add-to-list 'avy-styles-alist '(ivy-avy . pre)))
791
792 (defun ivy-avy ()
793 "Jump to one of the current ivy candidates."
794 (interactive)
795 (unless (require 'avy nil 'noerror)
796 (error "Package avy isn't installed"))
797 (let* ((avy-all-windows nil)
798 (avy-keys (or (cdr (assq 'ivy-avy avy-keys-alist))
799 avy-keys))
800 (avy-style (or (cdr (assq 'ivy-avy
801 avy-styles-alist))
802 avy-style))
803 (candidate
804 (let ((candidates))
805 (save-excursion
806 (save-restriction
807 (narrow-to-region
808 (window-start)
809 (window-end))
810 (goto-char (point-min))
811 (forward-line)
812 (while (< (point) (point-max))
813 (push
814 (cons (point)
815 (selected-window))
816 candidates)
817 (forward-line))))
818 (setq avy-action #'identity)
819 (avy--process
820 (nreverse candidates)
821 (avy--style-fn avy-style)))))
822 (ivy-set-index (- (line-number-at-pos candidate) 2))
823 (ivy--exhibit)
824 (ivy-done)))
825
826 (defun ivy-sort-file-function-default (x y)
827 "Compare two files X and Y.
828 Prioritize directories."
829 (if (get-text-property 0 'dirp x)
830 (if (get-text-property 0 'dirp y)
831 (string< x y)
832 t)
833 (if (get-text-property 0 'dirp y)
834 nil
835 (string< x y))))
836
837 (defvar ivy-sort-functions-alist
838 '((read-file-name-internal . ivy-sort-file-function-default)
839 (internal-complete-buffer . nil)
840 (counsel-git-grep-function . nil)
841 (Man-goto-section . nil)
842 (org-refile . nil)
843 (t . string-lessp))
844 "An alist of sorting functions for each collection function.
845 Interactive functions that call completion fit in here as well.
846
847 For each entry, nil means no sorting. It's very useful to turn
848 off the sorting for functions that have candidates in the natural
849 buffer order, like `org-refile' or `Man-goto-section'.
850
851 The entry associated to t is used for all fall-through cases.")
852
853 (defvar ivy-index-functions-alist
854 '((swiper . ivy-recompute-index-swiper)
855 (swiper-multi . ivy-recompute-index-swiper)
856 (counsel-git-grep . ivy-recompute-index-swiper)
857 (t . ivy-recompute-index-zero))
858 "An alist of index recomputing functions for each collection function.
859 When the input changes, calling the appropriate function will
860 return an integer - the index of the matched candidate that
861 should be selected.")
862
863 (defvar ivy-re-builders-alist
864 '((t . ivy--regex-plus))
865 "An alist of regex building functions for each collection function.
866 Each function should take a string and return a valid regex or a
867 regex sequence (see below).
868
869 The entry associated to t is used for all fall-through cases.
870 Possible choices: `ivy--regex', `regexp-quote', `ivy--regex-plus'.
871
872 In case a function returns a list, it should look like this:
873 '((\"matching-regexp\" . t) (\"non-matching-regexp\") ...).
874
875 The matches will be filtered in a sequence, you can mix the
876 regexps that should match and that should not match as you
877 like.")
878
879 (defvar ivy-initial-inputs-alist
880 '((org-refile . "^")
881 (org-agenda-refile . "^")
882 (org-capture-refile . "^")
883 (counsel-M-x . "^")
884 (counsel-describe-function . "^")
885 (counsel-describe-variable . "^")
886 (man . "^")
887 (woman . "^"))
888 "Command to initial input table.")
889
890 (defcustom ivy-sort-max-size 30000
891 "Sorting won't be done for collections larger than this."
892 :type 'integer)
893
894 (defun ivy--sorted-files (dir)
895 "Return the list of files in DIR.
896 Directories come first."
897 (let* ((default-directory dir)
898 (seq (all-completions "" 'read-file-name-internal))
899 sort-fn)
900 (if (equal dir "/")
901 seq
902 (setq seq (delete "./" (delete "../" seq)))
903 (when (eq (setq sort-fn (cdr (assoc 'read-file-name-internal
904 ivy-sort-functions-alist)))
905 #'ivy-sort-file-function-default)
906 (setq seq (mapcar (lambda (x)
907 (propertize x 'dirp (string-match-p "/\\'" x)))
908 seq)))
909 (when sort-fn
910 (setq seq (cl-sort seq sort-fn)))
911 (dolist (dir ivy-extra-directories)
912 (push dir seq))
913 seq)))
914
915 ;;** Entry Point
916 (cl-defun ivy-read (prompt collection
917 &key predicate require-match initial-input
918 history preselect keymap update-fn sort
919 action unwind re-builder matcher dynamic-collection caller)
920 "Read a string in the minibuffer, with completion.
921
922 PROMPT is a string to prompt with; normally it ends in a colon
923 and a space. When PROMPT contains %d, it will be updated with
924 the current number of matching candidates. If % appears elsewhere
925 in the PROMPT it should be quoted as %%.
926 See also `ivy-count-format'.
927
928 COLLECTION is a list of strings, or a function, or an alist, or a
929 hash table.
930
931 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
932
933 KEYMAP is composed together with `ivy-minibuffer-map'.
934
935 If PRESELECT is non-nil select the corresponding candidate out of
936 the ones that match INITIAL-INPUT.
937
938 UPDATE-FN is called each time the current candidate(s) is changed.
939
940 When SORT is t, refer to `ivy-sort-functions-alist' for sorting.
941
942 ACTION is a lambda to call after a result was selected. It should
943 take a single argument, usually a string.
944
945 UNWIND is a lambda to call before exiting.
946
947 RE-BUILDER is a lambda that transforms text into a regex.
948
949 MATCHER can completely override matching.
950
951 DYNAMIC-COLLECTION is a function to call to update the list of
952 candidates with each input.
953
954 CALLER is a symbol to uniquely identify the caller to `ivy-read'.
955 It's used in conjunction with COLLECTION to indentify which
956 customizations should apply to the current completion session."
957 (let ((extra-actions (plist-get ivy--actions-list this-command)))
958 (when extra-actions
959 (setq action
960 (if (functionp action)
961 `(1
962 ("o" ,action "default")
963 ,@extra-actions)
964 (delete-dups (append action extra-actions))))))
965 (setq ivy-last
966 (make-ivy-state
967 :prompt prompt
968 :collection collection
969 :predicate predicate
970 :require-match require-match
971 :initial-input initial-input
972 :history history
973 :preselect preselect
974 :keymap keymap
975 :update-fn update-fn
976 :sort sort
977 :action action
978 :window (selected-window)
979 :unwind unwind
980 :re-builder re-builder
981 :matcher matcher
982 :dynamic-collection dynamic-collection
983 :caller caller))
984 (ivy--reset-state ivy-last)
985 (prog1
986 (unwind-protect
987 (minibuffer-with-setup-hook
988 #'ivy--minibuffer-setup
989 (let* ((hist (or history 'ivy-history))
990 (minibuffer-completion-table collection)
991 (minibuffer-completion-predicate predicate)
992 (resize-mini-windows (cond
993 ((display-graphic-p) nil)
994 ((null resize-mini-windows) 'grow-only)
995 (t resize-mini-windows)))
996 (res (read-from-minibuffer
997 prompt
998 (ivy-state-initial-input ivy-last)
999 (make-composed-keymap keymap ivy-minibuffer-map)
1000 nil
1001 hist)))
1002 (when (eq ivy-exit 'done)
1003 (let ((item (if ivy--directory
1004 ivy--current
1005 ivy-text)))
1006 (unless (equal item "")
1007 (set hist (cons (propertize item 'ivy-index ivy--index)
1008 (delete item
1009 (cdr (symbol-value hist)))))))
1010 res)))
1011 (remove-hook 'post-command-hook #'ivy--exhibit)
1012 (when (setq unwind (ivy-state-unwind ivy-last))
1013 (funcall unwind)))
1014 (ivy-call)))
1015
1016 (defun ivy--reset-state (state)
1017 "Reset the ivy to STATE.
1018 This is useful for recursive `ivy-read'."
1019 (let ((prompt (ivy-state-prompt state))
1020 (collection (ivy-state-collection state))
1021 (predicate (ivy-state-predicate state))
1022 (history (ivy-state-history state))
1023 (preselect (ivy-state-preselect state))
1024 (sort (ivy-state-sort state))
1025 (re-builder (ivy-state-re-builder state))
1026 (dynamic-collection (ivy-state-dynamic-collection state))
1027 (initial-input (ivy-state-initial-input state))
1028 (require-match (ivy-state-require-match state))
1029 (matcher (ivy-state-matcher state)))
1030 (unless initial-input
1031 (setq initial-input (cdr (assoc this-command
1032 ivy-initial-inputs-alist))))
1033 (setq ivy--directory nil)
1034 (setq ivy-case-fold-search 'auto)
1035 (setq ivy--regex-function
1036 (or re-builder
1037 (and (functionp collection)
1038 (cdr (assoc collection ivy-re-builders-alist)))
1039 (cdr (assoc t ivy-re-builders-alist))
1040 'ivy--regex))
1041 (setq ivy--subexps 0)
1042 (setq ivy--regexp-quote 'regexp-quote)
1043 (setq ivy--old-text "")
1044 (setq ivy--full-length nil)
1045 (setq ivy-text "")
1046 (setq ivy-calling nil)
1047 (let (coll sort-fn)
1048 (cond ((eq collection 'Info-read-node-name-1)
1049 (if (equal Info-current-file "dir")
1050 (setq coll
1051 (mapcar (lambda (x) (format "(%s)" x))
1052 (cl-delete-duplicates
1053 (all-completions "(" collection predicate)
1054 :test #'equal)))
1055 (setq coll (all-completions "" collection predicate))))
1056 ((eq collection 'read-file-name-internal)
1057 (setq ivy--directory default-directory)
1058 (require 'dired)
1059 (when preselect
1060 (let ((preselect-directory (file-name-directory preselect)))
1061 (unless (or (null preselect-directory)
1062 (string= preselect-directory
1063 default-directory))
1064 (setq ivy--directory preselect-directory))
1065 (setf
1066 (ivy-state-preselect state)
1067 (setq preselect (file-name-nondirectory preselect)))))
1068 (setq coll (ivy--sorted-files ivy--directory))
1069 (when initial-input
1070 (unless (or require-match
1071 (equal initial-input default-directory)
1072 (equal initial-input ""))
1073 (setq coll (cons initial-input coll)))
1074 (setq initial-input nil)))
1075 ((eq collection 'internal-complete-buffer)
1076 (setq coll (ivy--buffer-list "" ivy-use-virtual-buffers)))
1077 ((or (functionp collection)
1078 (byte-code-function-p collection)
1079 (vectorp collection)
1080 (and (consp collection) (listp (car collection)))
1081 (hash-table-p collection))
1082 (setq coll (all-completions "" collection predicate)))
1083 (t
1084 (setq coll collection)))
1085 (when sort
1086 (if (and (functionp collection)
1087 (setq sort-fn (assoc collection ivy-sort-functions-alist)))
1088 (when (and (setq sort-fn (cdr sort-fn))
1089 (not (eq collection 'read-file-name-internal)))
1090 (setq coll (cl-sort coll sort-fn)))
1091 (unless (eq history 'org-refile-history)
1092 (if (and (setq sort-fn (cdr (assoc t ivy-sort-functions-alist)))
1093 (<= (length coll) ivy-sort-max-size))
1094 (setq coll (cl-sort (copy-sequence coll) sort-fn))))))
1095 (when preselect
1096 (unless (or (and require-match
1097 (not (eq collection 'internal-complete-buffer)))
1098 (let ((re (regexp-quote preselect)))
1099 (cl-find-if (lambda (x) (string-match re x))
1100 coll)))
1101 (setq coll (cons preselect coll))))
1102 (setq ivy--index (or
1103 (and dynamic-collection
1104 ivy--index)
1105 (and preselect
1106 (ivy--preselect-index
1107 coll initial-input preselect matcher))
1108 0))
1109 (setq ivy--old-re nil)
1110 (setq ivy--old-cands nil)
1111 (setq ivy--all-candidates coll))
1112 (setq ivy-exit nil)
1113 (setq ivy--default (or (thing-at-point 'symbol) ""))
1114 (setq ivy--prompt
1115 (cond ((string-match "%.*d" prompt)
1116 prompt)
1117 ((null ivy-count-format)
1118 (error
1119 "`ivy-count-format' can't be nil. Set it to an empty string instead."))
1120 ((string-match "%d.*%d" ivy-count-format)
1121 (let ((w (length (number-to-string
1122 (length ivy--all-candidates))))
1123 (s (copy-sequence ivy-count-format)))
1124 (string-match "%d" s)
1125 (match-end 0)
1126 (string-match "%d" s (match-end 0))
1127 (setq s (replace-match (format "%%-%dd" w) nil nil s))
1128 (string-match "%d" s)
1129 (concat (replace-match (format "%%%dd" w) nil nil s)
1130 prompt)))
1131 ((string-match "%.*d" ivy-count-format)
1132 (concat ivy-count-format prompt))
1133 (ivy--directory
1134 prompt)
1135 (t
1136 nil)))
1137 (setf (ivy-state-initial-input ivy-last) initial-input)))
1138
1139 ;;;###autoload
1140 (defun ivy-completing-read (prompt collection
1141 &optional predicate require-match initial-input
1142 history def _inherit-input-method)
1143 "Read a string in the minibuffer, with completion.
1144
1145 This is an interface that conforms to `completing-read', so that
1146 it can be used for `completing-read-function'.
1147
1148 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1149 COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1150 PREDICATE limits completion to a subset of COLLECTION.
1151 REQUIRE-MATCH is considered boolean. See `completing-read'.
1152 INITIAL-INPUT is a string that can be inserted into the minibuffer initially.
1153 _HISTORY is ignored for now.
1154 DEF is the default value.
1155 _INHERIT-INPUT-METHOD is ignored for now.
1156
1157 The history, defaults and input-method arguments are ignored for now."
1158 (ivy-read (replace-regexp-in-string "%" "%%" prompt)
1159 collection
1160 :predicate predicate
1161 :require-match require-match
1162 :initial-input (if (consp initial-input)
1163 (car initial-input)
1164 (if (and (stringp initial-input)
1165 (string-match "\\+" initial-input))
1166 (replace-regexp-in-string
1167 "\\+" "\\\\+" initial-input)
1168 initial-input))
1169 :preselect (if (listp def) (car def) def)
1170 :history history
1171 :keymap nil
1172 :sort
1173 (let ((sort (assoc this-command ivy-sort-functions-alist)))
1174 (if sort
1175 (cdr sort)
1176 t))))
1177
1178 ;;;###autoload
1179 (define-minor-mode ivy-mode
1180 "Toggle Ivy mode on or off.
1181 With ARG, turn Ivy mode on if arg is positive, off otherwise.
1182 Turning on Ivy mode will set `completing-read-function' to
1183 `ivy-completing-read'.
1184
1185 Global bindings:
1186 \\{ivy-mode-map}
1187
1188 Minibuffer bindings:
1189 \\{ivy-minibuffer-map}"
1190 :group 'ivy
1191 :global t
1192 :keymap ivy-mode-map
1193 :lighter " ivy"
1194 (if ivy-mode
1195 (setq completing-read-function 'ivy-completing-read)
1196 (setq completing-read-function 'completing-read-default)))
1197
1198 (defun ivy--preselect-index (candidates initial-input preselect matcher)
1199 "Return the index in CANDIDATES filtered by INITIAL-INPUT for PRESELECT.
1200 When MATCHER is non-nil it's used instead of `cl-remove-if-not'."
1201 (if initial-input
1202 (progn
1203 (setq initial-input (ivy--regex-plus initial-input))
1204 (setq candidates
1205 (if matcher
1206 (funcall matcher initial-input candidates)
1207 (cl-remove-if-not
1208 (lambda (x)
1209 (string-match initial-input x))
1210 candidates))))
1211 (when matcher
1212 (setq candidates (funcall matcher "" candidates))))
1213 (or (cl-position preselect candidates :test #'equal)
1214 (and (stringp preselect)
1215 (let ((re (regexp-quote preselect)))
1216 (cl-position-if
1217 (lambda (x)
1218 (string-match re x))
1219 candidates)))))
1220
1221 ;;* Implementation
1222 ;;** Regex
1223 (defvar ivy--regex-hash
1224 (make-hash-table :test #'equal)
1225 "Store pre-computed regex.")
1226
1227 (defun ivy--split (str)
1228 "Split STR into a list by single spaces.
1229 The remaining spaces stick to their left.
1230 This allows to \"quote\" N spaces by inputting N+1 spaces."
1231 (let ((len (length str))
1232 start0
1233 (start1 0)
1234 res s
1235 match-len)
1236 (while (and (string-match " +" str start1)
1237 (< start1 len))
1238 (setq match-len (- (match-end 0) (match-beginning 0)))
1239 (if (= match-len 1)
1240 (progn
1241 (when start0
1242 (setq start1 start0)
1243 (setq start0 nil))
1244 (push (substring str start1 (match-beginning 0)) res)
1245 (setq start1 (match-end 0)))
1246 (setq str (replace-match
1247 (make-string (1- match-len) ?\ )
1248 nil nil str))
1249 (setq start0 (or start0 start1))
1250 (setq start1 (1- (match-end 0)))))
1251 (if start0
1252 (push (substring str start0) res)
1253 (setq s (substring str start1))
1254 (unless (= (length s) 0)
1255 (push s res)))
1256 (nreverse res)))
1257
1258 (defun ivy--regex (str &optional greedy)
1259 "Re-build regex from STR in case it has a space.
1260 When GREEDY is non-nil, join words in a greedy way."
1261 (let ((hashed (unless greedy
1262 (gethash str ivy--regex-hash))))
1263 (if hashed
1264 (prog1 (cdr hashed)
1265 (setq ivy--subexps (car hashed)))
1266 (when (string-match "\\([^\\]\\|^\\)\\\\$" str)
1267 (setq str (substring str 0 -1)))
1268 (cdr (puthash str
1269 (let ((subs (ivy--split str)))
1270 (if (= (length subs) 1)
1271 (cons
1272 (setq ivy--subexps 0)
1273 (car subs))
1274 (cons
1275 (setq ivy--subexps (length subs))
1276 (mapconcat
1277 (lambda (x)
1278 (if (string-match "\\`\\\\(.*\\\\)\\'" x)
1279 x
1280 (format "\\(%s\\)" x)))
1281 subs
1282 (if greedy
1283 ".*"
1284 ".*?")))))
1285 ivy--regex-hash)))))
1286
1287 (defun ivy--regex-ignore-order (str)
1288 "Re-build regex from STR by splitting it on spaces.
1289 Ignore the order of each group."
1290 (let* ((subs (split-string str " +" t))
1291 (len (length subs)))
1292 (cl-case len
1293 (1
1294 (setq ivy--subexps 0)
1295 (car subs))
1296 (t
1297 (setq ivy--subexps len)
1298 (let ((all (mapconcat #'identity subs "\\|")))
1299 (mapconcat
1300 (lambda (x)
1301 (if (string-match "\\`\\\\(.*\\\\)\\'" x)
1302 x
1303 (format "\\(%s\\)" x)))
1304 (make-list len all)
1305 ".*?"))))))
1306
1307 (defun ivy--regex-plus (str)
1308 "Build a regex sequence from STR.
1309 Spaces are wild, everything before \"!\" should match.
1310 Everything after \"!\" should not match."
1311 (let ((parts (split-string str "!" t)))
1312 (cl-case (length parts)
1313 (0
1314 "")
1315 (1
1316 (ivy--regex (car parts)))
1317 (2
1318 (let ((res
1319 (mapcar #'list
1320 (split-string (cadr parts) " " t))))
1321 (cons (cons (ivy--regex (car parts)) t)
1322 res)))
1323 (t (error "Unexpected: use only one !")))))
1324
1325 (defun ivy--regex-fuzzy (str)
1326 "Build a regex sequence from STR.
1327 Insert .* between each char."
1328 (if (string-match "\\`\\(\\^?\\)\\(.*?\\)\\(\\$?\\)\\'" str)
1329 (prog1
1330 (concat (match-string 1 str)
1331 (mapconcat
1332 (lambda (x)
1333 (format "\\(%c\\)" x))
1334 (string-to-list (match-string 2 str)) ".*")
1335 (match-string 3 str))
1336 (setq ivy--subexps (length (match-string 2 str))))
1337 str))
1338
1339 ;;** Rest
1340 (defun ivy--minibuffer-setup ()
1341 "Setup ivy completion in the minibuffer."
1342 (set (make-local-variable 'completion-show-inline-help) nil)
1343 (set (make-local-variable 'minibuffer-default-add-function)
1344 (lambda ()
1345 (list ivy--default)))
1346 (when (display-graphic-p)
1347 (setq truncate-lines t))
1348 (setq-local max-mini-window-height ivy-height)
1349 (add-hook 'post-command-hook #'ivy--exhibit nil t)
1350 ;; show completions with empty input
1351 (ivy--exhibit))
1352
1353 (defun ivy--input ()
1354 "Return the current minibuffer input."
1355 ;; assume one-line minibuffer input
1356 (buffer-substring-no-properties
1357 (minibuffer-prompt-end)
1358 (line-end-position)))
1359
1360 (defun ivy--cleanup ()
1361 "Delete the displayed completion candidates."
1362 (save-excursion
1363 (goto-char (minibuffer-prompt-end))
1364 (delete-region (line-end-position) (point-max))))
1365
1366 (defun ivy--insert-prompt ()
1367 "Update the prompt according to `ivy--prompt'."
1368 (when ivy--prompt
1369 (unless (memq this-command '(ivy-done ivy-alt-done ivy-partial-or-done
1370 counsel-find-symbol))
1371 (setq ivy--prompt-extra ""))
1372 (let (head tail)
1373 (if (string-match "\\(.*\\): \\'" ivy--prompt)
1374 (progn
1375 (setq head (match-string 1 ivy--prompt))
1376 (setq tail ": "))
1377 (setq head (substring ivy--prompt 0 -1))
1378 (setq tail " "))
1379 (let ((inhibit-read-only t)
1380 (std-props '(front-sticky t rear-nonsticky t field t read-only t))
1381 (n-str
1382 (concat
1383 (if (and (bound-and-true-p minibuffer-depth-indicate-mode)
1384 (> (minibuffer-depth) 1))
1385 (format "[%d] " (minibuffer-depth))
1386 "")
1387 (concat
1388 (if (string-match "%d.*%d" ivy-count-format)
1389 (format head
1390 (1+ ivy--index)
1391 (or (and (ivy-state-dynamic-collection ivy-last)
1392 ivy--full-length)
1393 ivy--length))
1394 (format head
1395 (or (and (ivy-state-dynamic-collection ivy-last)
1396 ivy--full-length)
1397 ivy--length)))
1398 ivy--prompt-extra
1399 tail)))
1400 (d-str (if ivy--directory
1401 (abbreviate-file-name ivy--directory)
1402 "")))
1403 (save-excursion
1404 (goto-char (point-min))
1405 (delete-region (point-min) (minibuffer-prompt-end))
1406 (if (> (+ (mod (+ (length n-str) (length d-str)) (window-width))
1407 (length ivy-text))
1408 (window-width))
1409 (setq n-str (concat n-str "\n" d-str))
1410 (setq n-str (concat n-str d-str)))
1411 (let ((regex (format "\\([^\n]\\{%d\\}\\)[^\n]" (window-width))))
1412 (while (string-match regex n-str)
1413 (setq n-str (replace-match (concat (match-string 1 n-str) "\n") nil t n-str 1))))
1414 (set-text-properties 0 (length n-str)
1415 `(face minibuffer-prompt ,@std-props)
1416 n-str)
1417 (ivy--set-match-props n-str "confirm"
1418 `(face ivy-confirm-face ,@std-props))
1419 (ivy--set-match-props n-str "match required"
1420 `(face ivy-match-required-face ,@std-props))
1421 (insert n-str))
1422 ;; get out of the prompt area
1423 (constrain-to-field nil (point-max))))))
1424
1425 (defun ivy--set-match-props (str match props)
1426 "Set STR text proprties that match MATCH to PROPS."
1427 (when (string-match match str)
1428 (set-text-properties
1429 (match-beginning 0)
1430 (match-end 0)
1431 props
1432 str)))
1433
1434 (defvar inhibit-message)
1435
1436 (defun ivy--sort-maybe (collection)
1437 "Sort COLLECTION if needed."
1438 (let ((sort (ivy-state-sort ivy-last))
1439 entry)
1440 (if (null sort)
1441 collection
1442 (let ((sort-fn (cond ((functionp sort)
1443 sort)
1444 ((setq entry (assoc (ivy-state-collection ivy-last)
1445 ivy-sort-functions-alist))
1446 (cdr entry))
1447 (t
1448 (cdr (assoc t ivy-sort-functions-alist))))))
1449 (if (functionp sort-fn)
1450 (cl-sort (copy-sequence collection) sort-fn)
1451 collection)))))
1452
1453 (defun ivy--exhibit ()
1454 "Insert Ivy completions display.
1455 Should be run via minibuffer `post-command-hook'."
1456 (when (memq 'ivy--exhibit post-command-hook)
1457 (let ((inhibit-field-text-motion nil))
1458 (constrain-to-field nil (point-max)))
1459 (setq ivy-text (ivy--input))
1460 (if (ivy-state-dynamic-collection ivy-last)
1461 ;; while-no-input would cause annoying
1462 ;; "Waiting for process to die...done" message interruptions
1463 (let ((inhibit-message t))
1464 (unless (equal ivy--old-text ivy-text)
1465 (while-no-input
1466 (setq ivy--all-candidates
1467 (ivy--sort-maybe
1468 (funcall (ivy-state-collection ivy-last) ivy-text)))
1469 (setq ivy--old-text ivy-text)))
1470 (when ivy--all-candidates
1471 (ivy--insert-minibuffer
1472 (ivy--format ivy--all-candidates))))
1473 (cond (ivy--directory
1474 (if (string-match "/\\'" ivy-text)
1475 (if (member ivy-text ivy--all-candidates)
1476 (ivy--cd (expand-file-name ivy-text ivy--directory))
1477 (when (string-match "//\\'" ivy-text)
1478 (if (and default-directory
1479 (string-match "\\`[[:alpha:]]:/" default-directory))
1480 (ivy--cd (match-string 0 default-directory))
1481 (ivy--cd "/")))
1482 (when (string-match "[[:alpha:]]:/" ivy-text)
1483 (let ((drive-root (match-string 0 ivy-text)))
1484 (when (file-exists-p drive-root)
1485 (ivy--cd drive-root)))))
1486 (if (string-match "\\`~\\'" ivy-text)
1487 (ivy--cd (expand-file-name "~/")))))
1488 ((eq (ivy-state-collection ivy-last) 'internal-complete-buffer)
1489 (when (or (and (string-match "\\` " ivy-text)
1490 (not (string-match "\\` " ivy--old-text)))
1491 (and (string-match "\\` " ivy--old-text)
1492 (not (string-match "\\` " ivy-text))))
1493 (setq ivy--all-candidates
1494 (if (and (> (length ivy-text) 0)
1495 (eq (aref ivy-text 0)
1496 ?\ ))
1497 (ivy--buffer-list " ")
1498 (ivy--buffer-list "" ivy-use-virtual-buffers)))
1499 (setq ivy--old-re nil))))
1500 (ivy--insert-minibuffer
1501 (ivy--format
1502 (ivy--filter ivy-text ivy--all-candidates)))
1503 (setq ivy--old-text ivy-text))))
1504
1505 (defun ivy--insert-minibuffer (text)
1506 "Insert TEXT into minibuffer with appropriate cleanup."
1507 (let ((resize-mini-windows nil)
1508 (update-fn (ivy-state-update-fn ivy-last))
1509 deactivate-mark)
1510 (ivy--cleanup)
1511 (when update-fn
1512 (funcall update-fn))
1513 (ivy--insert-prompt)
1514 ;; Do nothing if while-no-input was aborted.
1515 (when (stringp text)
1516 (let ((buffer-undo-list t))
1517 (save-excursion
1518 (forward-line 1)
1519 (insert text))))
1520 (when (display-graphic-p)
1521 (ivy--resize-minibuffer-to-fit))))
1522
1523 (defun ivy--resize-minibuffer-to-fit ()
1524 "Resize the minibuffer window so it has enough space to display
1525 all of the text contained in the minibuffer."
1526 (with-selected-window (minibuffer-window)
1527 (if (fboundp 'window-text-pixel-size)
1528 (let ((text-height (cdr (window-text-pixel-size)))
1529 (body-height (window-body-height nil t)))
1530 (when (> text-height body-height)
1531 (window-resize nil (- text-height body-height) nil t t)))
1532 (let ((text-height (count-screen-lines))
1533 (body-height (window-body-height)))
1534 (when (> text-height body-height)
1535 (window-resize nil (- text-height body-height) nil t))))))
1536
1537 (declare-function colir-blend-face-background "ext:colir")
1538
1539 (defun ivy--add-face (str face)
1540 "Propertize STR with FACE.
1541 `font-lock-append-text-property' is used, since it's better than
1542 `propertize' or `add-face-text-property' in this case."
1543 (require 'colir)
1544 (condition-case nil
1545 (progn
1546 (colir-blend-face-background 0 (length str) face str)
1547 (let ((foreground (face-foreground face)))
1548 (when foreground
1549 (add-face-text-property
1550 0 (length str)
1551 `(:foreground ,foreground)
1552 nil
1553 str))))
1554 (error
1555 (ignore-errors
1556 (font-lock-append-text-property 0 (length str) 'face face str))))
1557 str)
1558
1559 (declare-function flx-make-string-cache "ext:flx")
1560 (declare-function flx-score "ext:flx")
1561
1562 (defvar ivy--flx-cache nil)
1563
1564 (eval-after-load 'flx
1565 '(setq ivy--flx-cache (flx-make-string-cache)))
1566
1567 (defun ivy-toggle-case-fold ()
1568 "Toggle the case folding between nil and auto.
1569 In any completion session, the case folding starts in auto:
1570
1571 - when the input is all lower case, `case-fold-search' is t
1572 - otherwise it's nil.
1573
1574 You can toggle this to make `case-fold-search' nil regardless of input."
1575 (interactive)
1576 (setq ivy-case-fold-search
1577 (if ivy-case-fold-search
1578 nil
1579 'auto))
1580 ;; reset cache so that the candidate list updates
1581 (setq ivy--old-re nil))
1582
1583 (defun ivy--filter (name candidates)
1584 "Return all items that match NAME in CANDIDATES.
1585 CANDIDATES are assumed to be static."
1586 (let ((re (funcall ivy--regex-function name)))
1587 (if (and (equal re ivy--old-re)
1588 ivy--old-cands)
1589 ;; quick caching for "C-n", "C-p" etc.
1590 ivy--old-cands
1591 (let* ((re-str (if (listp re) (caar re) re))
1592 (matcher (ivy-state-matcher ivy-last))
1593 (case-fold-search
1594 (and ivy-case-fold-search
1595 (string= name (downcase name))))
1596 (cands (cond
1597 (matcher
1598 (funcall matcher re candidates))
1599 ((and ivy--old-re
1600 (stringp re)
1601 (stringp ivy--old-re)
1602 (not (string-match "\\\\" ivy--old-re))
1603 (not (equal ivy--old-re ""))
1604 (memq (cl-search
1605 (if (string-match "\\\\)\\'" ivy--old-re)
1606 (substring ivy--old-re 0 -2)
1607 ivy--old-re)
1608 re)
1609 '(0 2)))
1610 (ignore-errors
1611 (cl-remove-if-not
1612 (lambda (x) (string-match re x))
1613 ivy--old-cands)))
1614 (t
1615 (let ((re-list (if (stringp re) (list (cons re t)) re))
1616 (res candidates))
1617 (dolist (re re-list)
1618 (setq res
1619 (ignore-errors
1620 (funcall
1621 (if (cdr re)
1622 #'cl-remove-if-not
1623 #'cl-remove-if)
1624 (let ((re-str (car re)))
1625 (lambda (x) (string-match re-str x)))
1626 res))))
1627 res)))))
1628 (ivy--recompute-index name re-str cands)
1629 (setq ivy--old-re (if cands re-str ""))
1630 (when (and (require 'flx nil 'noerror)
1631 (eq ivy--regex-function 'ivy--regex-fuzzy))
1632 (setq cands (ivy--flx-sort name cands)))
1633 (setq ivy--old-cands cands)))))
1634
1635 (defun ivy--recompute-index (name re-str cands)
1636 (let* ((caller (ivy-state-caller ivy-last))
1637 (func (or (and caller (cdr (assoc caller ivy-index-functions-alist)))
1638 (cdr (assoc t ivy-index-functions-alist))
1639 #'ivy-recompute-index-zero)))
1640 (setq ivy--index
1641 (or (and (not (string= name ""))
1642 (not (and (require 'flx nil 'noerror)
1643 (eq ivy--regex-function 'ivy--regex-fuzzy)
1644 (< (length cands) 200)))
1645 (cl-position (nth ivy--index ivy--old-cands)
1646 cands))
1647 (funcall func re-str cands)))
1648 (when (and (or (string= name "")
1649 (string= name "^"))
1650 (not (equal ivy--old-re "")))
1651 (setq ivy--index
1652 (or (ivy--preselect-index
1653 cands
1654 nil
1655 (ivy-state-preselect ivy-last)
1656 nil)
1657 ivy--index)))))
1658
1659 (defun ivy-recompute-index-swiper (re-str cands)
1660 (let ((tail (nthcdr ivy--index ivy--old-cands))
1661 idx)
1662 (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
1663 (or (and (not (equal re-str ivy--old-re))
1664 (or
1665 (cl-position (if (and (> (length re-str) 0)
1666 (eq ?^ (aref re-str 0)))
1667 (substring re-str 1)
1668 re-str) cands
1669 :test #'equal)
1670 (and ivy--directory
1671 (cl-position
1672 (concat re-str "/") cands
1673 :test #'equal))))
1674 (progn
1675 (while (and tail (null idx))
1676 ;; Compare with eq to handle equal duplicates in cands
1677 (setq idx (cl-position (pop tail) cands)))
1678 (or idx 0)))
1679 ivy--index)))
1680
1681 (defun ivy-recompute-index-zero (_re-str _cands)
1682 0)
1683
1684 (defun ivy--flx-sort (name cands)
1685 "Sort according to closeness to string NAME the string list CANDS."
1686 (condition-case nil
1687 (if (and cands
1688 (< (length cands) 200))
1689 (let* ((flx-name (if (string-match "^\\^" name)
1690 (substring name 1)
1691 name))
1692 (cands-with-score
1693 (delq nil
1694 (mapcar
1695 (lambda (x)
1696 (let ((score (car (flx-score x flx-name ivy--flx-cache))))
1697 (and score
1698 (cons score x))))
1699 cands))))
1700 (if cands-with-score
1701 (mapcar #'cdr
1702 (sort cands-with-score
1703 (lambda (x y)
1704 (> (car x) (car y)))))
1705 cands))
1706 cands)
1707 (error
1708 cands)))
1709
1710 (defvar ivy-format-function 'ivy-format-function-default
1711 "Function to transform the list of candidates into a string.
1712 This string will be inserted into the minibuffer.")
1713
1714 (defun ivy-format-function-default (cands)
1715 "Transform CANDS into a string for minibuffer."
1716 (if (bound-and-true-p truncate-lines)
1717 (mapconcat #'identity cands "\n")
1718 (let ((ww (- (window-width)
1719 (if (and (boundp 'fringe-mode) (eq fringe-mode 0)) 1 0))))
1720 (mapconcat
1721 (if truncate-lines
1722 (lambda (s)
1723 (if (> (length s) ww)
1724 (concat (substring s 0 (- ww 3)) "...")
1725 s))
1726 #'identity)
1727 cands "\n"))))
1728
1729 (defun ivy-format-function-arrow (cands)
1730 "Transform CANDS into a string for minibuffer."
1731 (let ((i -1))
1732 (mapconcat
1733 (lambda (s)
1734 (concat (if (eq (cl-incf i) ivy--index)
1735 "> "
1736 " ")
1737 s))
1738 cands "\n")))
1739
1740 (defcustom swiper-minibuffer-faces
1741 '(swiper-minibuffer-match-face-1
1742 swiper-minibuffer-match-face-2
1743 swiper-minibuffer-match-face-3
1744 swiper-minibuffer-match-face-4)
1745 "List of `swiper' faces for minibuffer group matches.")
1746
1747 (defun ivy--format-minibuffer-line (str)
1748 (let ((start 0)
1749 (str (copy-sequence str)))
1750 (when (eq ivy-display-style 'fancy)
1751 (unless ivy--old-re
1752 (setq ivy--old-re (funcall ivy--regex-function ivy-text)))
1753 (while (and (string-match ivy--old-re str start)
1754 (> (- (match-end 0) (match-beginning 0)) 0))
1755 (setq start (match-end 0))
1756 (let ((i 0))
1757 (while (<= i ivy--subexps)
1758 (let ((face
1759 (cond ((zerop ivy--subexps)
1760 (cadr swiper-minibuffer-faces))
1761 ((zerop i)
1762 (car swiper-minibuffer-faces))
1763 (t
1764 (nth (1+ (mod (+ i 2) (1- (length swiper-minibuffer-faces))))
1765 swiper-minibuffer-faces)))))
1766 (if (fboundp 'add-face-text-property)
1767 (add-face-text-property
1768 (match-beginning i)
1769 (match-end i)
1770 face
1771 nil
1772 str)
1773 (font-lock-append-text-property
1774 (match-beginning i)
1775 (match-end i)
1776 'face
1777 face
1778 str)))
1779 (cl-incf i)))))
1780 str))
1781
1782 (defun ivy--format (cands)
1783 "Return a string for CANDS suitable for display in the minibuffer.
1784 CANDS is a list of strings."
1785 (setq ivy--length (length cands))
1786 (when (>= ivy--index ivy--length)
1787 (setq ivy--index (max (1- ivy--length) 0)))
1788 (if (null cands)
1789 (setq ivy--current "")
1790 (let* ((half-height (/ ivy-height 2))
1791 (start (max 0 (- ivy--index half-height)))
1792 (end (min (+ start (1- ivy-height)) ivy--length))
1793 (start (max 0 (min start (- end (1- ivy-height)))))
1794 (cands (cl-subseq cands start end))
1795 (index (- ivy--index start)))
1796 (when ivy--directory
1797 (setq cands (mapcar (lambda (x)
1798 (if (string-match-p "/\\'" x)
1799 (propertize x 'face 'ivy-subdir)
1800 x))
1801 cands)))
1802 (setq ivy--current (copy-sequence (nth index cands)))
1803 (setq cands (mapcar
1804 #'ivy--format-minibuffer-line
1805 cands))
1806 (setf (nth index cands)
1807 (ivy--add-face (nth index cands) 'ivy-current-match))
1808 (let* ((ivy--index index)
1809 (res (concat "\n" (funcall ivy-format-function cands))))
1810 (put-text-property 0 (length res) 'read-only nil res)
1811 res))))
1812
1813 (defvar ivy--virtual-buffers nil
1814 "Store the virtual buffers alist.")
1815
1816 (defvar recentf-list)
1817
1818 (defface ivy-virtual '((t :inherit font-lock-builtin-face))
1819 "Face used by Ivy for matching virtual buffer names.")
1820
1821 (defcustom ivy-virtual-abbreviate 'name
1822 "The mode of abbreviation for virtual buffer names."
1823 :type '(choice
1824 (const :tag "Only name" 'name)
1825 (const :tag "Full path" 'full)
1826 ;; eventually, uniquify
1827 ))
1828
1829 (defun ivy--virtual-buffers ()
1830 "Adapted from `ido-add-virtual-buffers-to-list'."
1831 (unless recentf-mode
1832 (recentf-mode 1))
1833 (let ((bookmarks (and (boundp 'bookmark-alist)
1834 bookmark-alist))
1835 virtual-buffers name)
1836 (dolist (head (append
1837 recentf-list
1838 (delete " - no file -"
1839 (delq nil (mapcar (lambda (bookmark)
1840 (cdr (assoc 'filename bookmark)))
1841 bookmarks)))))
1842 (setq name
1843 (if (eq ivy-virtual-abbreviate 'name)
1844 (file-name-nondirectory head)
1845 (expand-file-name head)))
1846 (when (equal name "")
1847 (setq name (file-name-nondirectory (directory-file-name head))))
1848 (when (equal name "")
1849 (setq name head))
1850 (and (not (equal name ""))
1851 (null (get-file-buffer head))
1852 (not (assoc name virtual-buffers))
1853 (push (cons name head) virtual-buffers)))
1854 (when virtual-buffers
1855 (dolist (comp virtual-buffers)
1856 (put-text-property 0 (length (car comp))
1857 'face 'ivy-virtual
1858 (car comp)))
1859 (setq ivy--virtual-buffers (nreverse virtual-buffers))
1860 (mapcar #'car ivy--virtual-buffers))))
1861
1862 (defun ivy--buffer-list (str &optional virtual)
1863 "Return the buffers that match STR.
1864 When VIRTUAL is non-nil, add virtual buffers."
1865 (delete-dups
1866 (append
1867 (mapcar
1868 (lambda (x)
1869 (if (with-current-buffer x
1870 (file-remote-p
1871 (abbreviate-file-name default-directory)))
1872 (propertize x 'face 'ivy-remote)
1873 x))
1874 (all-completions str 'internal-complete-buffer))
1875 (and virtual
1876 (ivy--virtual-buffers)))))
1877
1878 (defun ivy--switch-buffer-action (buffer)
1879 "Switch to BUFFER.
1880 BUFFER may be a string or nil."
1881 (with-ivy-window
1882 (if (zerop (length buffer))
1883 (switch-to-buffer
1884 ivy-text nil 'force-same-window)
1885 (let ((virtual (assoc buffer ivy--virtual-buffers)))
1886 (if (and virtual
1887 (not (get-buffer buffer)))
1888 (find-file (cdr virtual))
1889 (switch-to-buffer
1890 buffer nil 'force-same-window))))))
1891
1892 (defun ivy--switch-buffer-other-window-action (buffer)
1893 "Switch to BUFFER in other window.
1894 BUFFER may be a string or nil."
1895 (if (zerop (length buffer))
1896 (switch-to-buffer-other-window ivy-text)
1897 (let ((virtual (assoc buffer ivy--virtual-buffers)))
1898 (if (and virtual
1899 (not (get-buffer buffer)))
1900 (find-file-other-window (cdr virtual))
1901 (switch-to-buffer-other-window buffer)))))
1902
1903 (defun ivy--rename-buffer-action (buffer)
1904 "Rename BUFFER."
1905 (let ((new-name (read-string "Rename buffer (to new name): ")))
1906 (with-current-buffer buffer
1907 (rename-buffer new-name))))
1908
1909 (defvar ivy-switch-buffer-map (make-sparse-keymap))
1910
1911 (ivy-set-actions
1912 'ivy-switch-buffer
1913 '(("k"
1914 (lambda (x)
1915 (kill-buffer x)
1916 (ivy--reset-state ivy-last))
1917 "kill")
1918 ("j"
1919 ivy--switch-buffer-other-window-action
1920 "other")
1921 ("r"
1922 ivy--rename-buffer-action
1923 "rename")))
1924
1925 ;;;###autoload
1926 (defun ivy-switch-buffer ()
1927 "Switch to another buffer."
1928 (interactive)
1929 (if (not ivy-mode)
1930 (call-interactively 'switch-to-buffer)
1931 (let ((this-command 'ivy-switch-buffer))
1932 (ivy-read "Switch to buffer: " 'internal-complete-buffer
1933 :preselect (buffer-name (other-buffer (current-buffer)))
1934 :action #'ivy--switch-buffer-action
1935 :keymap ivy-switch-buffer-map))))
1936
1937 ;;;###autoload
1938 (defun ivy-recentf ()
1939 "Find a file on `recentf-list'."
1940 (interactive)
1941 (ivy-read "Recentf: " recentf-list
1942 :action
1943 (lambda (f)
1944 (with-ivy-window
1945 (find-file f)))))
1946
1947 (defun ivy-yank-word ()
1948 "Pull next word from buffer into search string."
1949 (interactive)
1950 (let (amend)
1951 (with-ivy-window
1952 (let ((pt (point))
1953 (le (line-end-position)))
1954 (forward-word 1)
1955 (if (> (point) le)
1956 (goto-char pt)
1957 (setq amend (buffer-substring-no-properties pt (point))))))
1958 (when amend
1959 (insert (replace-regexp-in-string " +" " " amend)))))
1960
1961 (defun ivy-kill-ring-save ()
1962 "Store the current candidates into the kill ring.
1963 If the region is active, forward to `kill-ring-save' instead."
1964 (interactive)
1965 (if (region-active-p)
1966 (call-interactively 'kill-ring-save)
1967 (kill-new
1968 (mapconcat
1969 #'identity
1970 ivy--old-cands
1971 "\n"))))
1972
1973 (defun ivy-insert-current ()
1974 "Make the current candidate into current input.
1975 Don't finish completion."
1976 (interactive)
1977 (delete-minibuffer-contents)
1978 (if (and ivy--directory
1979 (string-match "/$" ivy--current))
1980 (insert (substring ivy--current 0 -1))
1981 (insert ivy--current)))
1982
1983 (defun ivy-toggle-fuzzy ()
1984 "Toggle the re builder between `ivy--regex-fuzzy' and `ivy--regex-plus'."
1985 (interactive)
1986 (setq ivy--old-re nil)
1987 (if (eq ivy--regex-function 'ivy--regex-fuzzy)
1988 (setq ivy--regex-function 'ivy--regex-plus)
1989 (setq ivy--regex-function 'ivy--regex-fuzzy)))
1990
1991 (defun ivy-reverse-i-search ()
1992 "Enter a recursive `ivy-read' session using the current history.
1993 The selected history element will be inserted into the minibufer."
1994 (interactive)
1995 (let ((enable-recursive-minibuffers t)
1996 (history (symbol-value (ivy-state-history ivy-last)))
1997 (old-last ivy-last))
1998 (ivy-read "Reverse-i-search: "
1999 history
2000 :action (lambda (x)
2001 (ivy--reset-state
2002 (setq ivy-last old-last))
2003 (delete-minibuffer-contents)
2004 (insert (substring-no-properties x))
2005 (ivy--cd-maybe)))))
2006
2007 (defun ivy-restrict-to-matches ()
2008 "Restrict candidates to current matches and erase input."
2009 (interactive)
2010 (delete-minibuffer-contents)
2011 (setq ivy--all-candidates
2012 (ivy--filter ivy-text ivy--all-candidates)))
2013
2014 (provide 'ivy)
2015
2016 ;;; ivy.el ends here