forked from abo-abo/swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivy.el
4059 lines (3654 loc) · 147 KB
/
ivy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ivy.el --- Incremental Vertical completYon -*- lexical-binding: t -*-
;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
;; Author: Oleh Krehel <[email protected]>
;; URL: https://github.com/abo-abo/swiper
;; Version: 0.9.1
;; Package-Requires: ((emacs "24.1"))
;; Keywords: matching
;; This file is part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This package provides `ivy-read' as an alternative to
;; `completing-read' and similar functions.
;;
;; There's no intricate code to determine the best candidate.
;; Instead, the user can navigate to it with `ivy-next-line' and
;; `ivy-previous-line'.
;;
;; The matching is done by splitting the input text by spaces and
;; re-building it into a regex.
;; So "for example" is transformed into "\\(for\\).*\\(example\\)".
;;; Code:
(require 'cl-lib)
(require 'ffap)
(require 'ivy-overlay)
;;* Customization
(defgroup ivy nil
"Incremental vertical completion."
:group 'convenience)
(defgroup ivy-faces nil
"Font-lock faces for `ivy'."
:group 'ivy
:group 'faces)
(defface ivy-current-match
'((((class color) (background light))
:background "#1a4b77" :foreground "white")
(((class color) (background dark))
:background "#65a7e2" :foreground "black"))
"Face used by Ivy for highlighting the current match.")
(defface ivy-minibuffer-match-face-1
'((((class color) (background light))
:background "#d3d3d3")
(((class color) (background dark))
:background "#555555"))
"The background face for `ivy' minibuffer matches.")
(defface ivy-minibuffer-match-face-2
'((((class color) (background light))
:background "#e99ce8" :weight bold)
(((class color) (background dark))
:background "#777777" :weight bold))
"Face for `ivy' minibuffer matches numbered 1 modulo 3.")
(defface ivy-minibuffer-match-face-3
'((((class color) (background light))
:background "#bbbbff" :weight bold)
(((class color) (background dark))
:background "#7777ff" :weight bold))
"Face for `ivy' minibuffer matches numbered 2 modulo 3.")
(defface ivy-minibuffer-match-face-4
'((((class color) (background light))
:background "#ffbbff" :weight bold)
(((class color) (background dark))
:background "#8a498a" :weight bold))
"Face for `ivy' minibuffer matches numbered 3 modulo 3.")
(defface ivy-confirm-face
'((t :foreground "ForestGreen" :inherit minibuffer-prompt))
"Face used by Ivy for a confirmation prompt.")
(defface ivy-match-required-face
'((t :foreground "red" :inherit minibuffer-prompt))
"Face used by Ivy for a match required prompt.")
(defface ivy-subdir
'((t :inherit dired-directory))
"Face used by Ivy for highlighting subdirs in the alternatives.")
(defface ivy-modified-buffer
'((t :inherit default))
"Face used by Ivy for highlighting modified file visiting buffers.")
(defface ivy-remote
'((((class color) (background light))
:foreground "#110099")
(((class color) (background dark))
:foreground "#7B6BFF"))
"Face used by Ivy for highlighting remotes in the alternatives.")
(defface ivy-virtual
'((t :inherit font-lock-builtin-face))
"Face used by Ivy for matching virtual buffer names.")
(defface ivy-action
'((t :inherit font-lock-builtin-face))
"Face used by Ivy for displaying keys in `ivy-read-action'.")
(defface ivy-highlight-face
'((t :inherit highlight))
"Face used by Ivy to highlight certain candidates.")
(defface ivy-prompt-match
'((t :inherit ivy-current-match))
"Face used by Ivy for highlighting the selected prompt line.")
(setcdr (assoc load-file-name custom-current-group-alist) 'ivy)
(defcustom ivy-height 10
"Number of lines for the minibuffer window."
:type 'integer)
(defcustom ivy-count-format "%-4d "
"The style to use for displaying the current candidate count for `ivy-read'.
Set this to \"\" to suppress the count visibility.
Set this to \"(%d/%d) \" to display both the index and the count."
:type '(choice
(const :tag "Count disabled" "")
(const :tag "Count matches" "%-4d ")
(const :tag "Count matches and show current match" "(%d/%d) ")
string))
(defcustom ivy-add-newline-after-prompt nil
"When non-nil, add a newline after the `ivy-read' prompt."
:type 'boolean)
(defcustom ivy-wrap nil
"When non-nil, wrap around after the first and the last candidate."
:type 'boolean)
(defcustom ivy-display-style (unless (version< emacs-version "24.5") 'fancy)
"The style for formatting the minibuffer.
By default, the matched strings are copied as is.
The fancy display style highlights matching parts of the regexp,
a behavior similar to `swiper'.
This setting depends on `add-face-text-property' - a C function
available as of Emacs 24.5. Fancy style will render poorly in
earlier versions of Emacs."
:type '(choice
(const :tag "Plain" nil)
(const :tag "Fancy" fancy)))
(defcustom ivy-on-del-error-function 'minibuffer-keyboard-quit
"The handler for when `ivy-backward-delete-char' throws.
Usually a quick exit out of the minibuffer."
:type 'function)
(defcustom ivy-extra-directories '("../" "./")
"Add this to the front of the list when completing file names.
Only \"./\" and \"../\" apply here. They appear in reverse order."
:type '(repeat :tag "Dirs"
(choice
(const :tag "Parent Directory" "../")
(const :tag "Current Directory" "./"))))
(defcustom ivy-use-virtual-buffers nil
"When non-nil, add recent files and bookmarks to `ivy-switch-buffer'."
:type 'boolean)
(defcustom ivy-display-function nil
"Decide where to display the candidates.
This function takes a string with the current matching candidates
and has to display it somewhere.
See https://github.com/abo-abo/swiper/wiki/ivy-display-function."
:type '(choice
(const :tag "Minibuffer" nil)
(const :tag "LV" ivy-display-function-lv)
(const :tag "Popup" ivy-display-function-popup)
(const :tag "Overlay" ivy-display-function-overlay)))
(defvar ivy-display-functions-alist
'((ivy-completion-in-region . ivy-display-function-overlay))
"An alist for customizing `ivy-display-function'.")
(defcustom ivy-completing-read-handlers-alist
'((tmm-menubar . completing-read-default)
(tmm-shortcut . completing-read-default)
(bbdb-create . ivy-completing-read-with-empty-string-def)
(auto-insert . ivy-completing-read-with-empty-string-def)
(Info-on-current-buffer . ivy-completing-read-with-empty-string-def)
(Info-follow-reference . ivy-completing-read-with-empty-string-def)
(Info-menu . ivy-completing-read-with-empty-string-def)
(Info-index . ivy-completing-read-with-empty-string-def)
(Info-virtual-index . ivy-completing-read-with-empty-string-def)
(info-display-manual . ivy-completing-read-with-empty-string-def)
(webjump . ivy-completing-read-with-empty-string-def))
"An alist of handlers to replace `completing-read' in `ivy-mode'."
:type '(alist :key-type function :value-type function))
(defvar ivy-completing-read-ignore-handlers-depth -1
"Used to avoid infinite recursion.
If `(minibuffer-depth)' equals this, `ivy-completing-read' will
act as if `ivy-completing-read-handlers-alist' is empty.")
(defvar ivy--actions-list nil
"A list of extra actions per command.")
(defun ivy-set-actions (cmd actions)
"Set CMD extra exit points to ACTIONS."
(setq ivy--actions-list
(plist-put ivy--actions-list cmd actions)))
(defun ivy-add-actions (cmd actions)
"Add extra exit points ACTIONS to CMD.
Existing exit points of CMD are overwritten by those in
ACTIONS that have the same key."
(setq ivy--actions-list
(plist-put ivy--actions-list cmd
(cl-delete-duplicates
(append (plist-get ivy--actions-list cmd) actions)
:key #'car :test #'equal))))
(defvar ivy--prompts-list nil)
(defun ivy-set-prompt (caller prompt-fn)
"Associate CALLER with PROMPT-FN.
PROMPT-FN is a function of no arguments that returns a prompt string."
(setq ivy--prompts-list
(plist-put ivy--prompts-list caller prompt-fn)))
(defvar ivy--display-transformers-list nil
"A list of str->str transformers per command.")
(defun ivy-set-display-transformer (cmd transformer)
"Set CMD a displayed candidate TRANSFORMER.
It's a lambda that takes a string one of the candidates in the
collection and returns a string for display, the same candidate
plus some extra information.
This lambda is called only on the `ivy-height' candidates that
are about to be displayed, not on the whole collection."
(setq ivy--display-transformers-list
(plist-put ivy--display-transformers-list cmd transformer)))
(defvar ivy--sources-list nil
"A list of extra sources per command.")
(defun ivy-set-sources (cmd sources)
"Attach to CMD a list of extra SOURCES.
Each static source is a function that takes no argument and
returns a list of strings.
The (original-source) determines the position of the original
dynamic source.
Extra dynamic sources aren't supported yet.
Example:
(defun small-recentf ()
(cl-subseq recentf-list 0 20))
(ivy-set-sources
'counsel-locate
'((small-recentf)
(original-source)))"
(setq ivy--sources-list
(plist-put ivy--sources-list cmd sources)))
(defvar ivy-current-prefix-arg nil
"Prefix arg to pass to actions.
This is a global variable that is set by ivy functions for use in
action functions.")
;;* Keymap
(require 'delsel)
(defvar ivy-minibuffer-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-m") 'ivy-done)
(define-key map (kbd "C-M-m") 'ivy-call)
(define-key map (kbd "C-j") 'ivy-alt-done)
(define-key map (kbd "C-M-j") 'ivy-immediate-done)
(define-key map (kbd "TAB") 'ivy-partial-or-done)
(define-key map [remap next-line] 'ivy-next-line)
(define-key map [remap previous-line] 'ivy-previous-line)
(define-key map (kbd "C-s") 'ivy-next-line-or-history)
(define-key map (kbd "C-r") 'ivy-reverse-i-search)
(define-key map (kbd "SPC") 'self-insert-command)
(define-key map [remap delete-backward-char] 'ivy-backward-delete-char)
(define-key map [remap backward-delete-char-untabify] 'ivy-backward-delete-char)
(define-key map [remap backward-kill-word] 'ivy-backward-kill-word)
(define-key map [remap delete-char] 'ivy-delete-char)
(define-key map [remap forward-char] 'ivy-forward-char)
(define-key map [remap kill-word] 'ivy-kill-word)
(define-key map [remap beginning-of-buffer] 'ivy-beginning-of-buffer)
(define-key map [remap end-of-buffer] 'ivy-end-of-buffer)
(define-key map (kbd "M-n") 'ivy-next-history-element)
(define-key map (kbd "M-p") 'ivy-previous-history-element)
(define-key map (kbd "C-g") 'minibuffer-keyboard-quit)
(define-key map [remap scroll-up-command] 'ivy-scroll-up-command)
(define-key map [remap scroll-down-command] 'ivy-scroll-down-command)
(define-key map (kbd "<next>") 'ivy-scroll-up-command)
(define-key map (kbd "<prior>") 'ivy-scroll-down-command)
(define-key map (kbd "C-v") 'ivy-scroll-up-command)
(define-key map (kbd "M-v") 'ivy-scroll-down-command)
(define-key map (kbd "C-M-n") 'ivy-next-line-and-call)
(define-key map (kbd "C-M-p") 'ivy-previous-line-and-call)
(define-key map (kbd "M-r") 'ivy-toggle-regexp-quote)
(define-key map (kbd "M-j") 'ivy-yank-word)
(define-key map (kbd "M-i") 'ivy-insert-current)
(define-key map (kbd "C-o") 'hydra-ivy/body)
(define-key map (kbd "M-o") 'ivy-dispatching-done)
(define-key map (kbd "C-M-o") 'ivy-dispatching-call)
(define-key map [remap kill-line] 'ivy-kill-line)
(define-key map (kbd "S-SPC") 'ivy-restrict-to-matches)
(define-key map [remap kill-ring-save] 'ivy-kill-ring-save)
(define-key map (kbd "C-'") 'ivy-avy)
(define-key map (kbd "C-M-a") 'ivy-read-action)
(define-key map (kbd "C-c C-o") 'ivy-occur)
(define-key map (kbd "C-c C-a") 'ivy-toggle-ignore)
(define-key map (kbd "C-c C-s") 'ivy-rotate-sort)
(define-key map [remap describe-mode] 'ivy-help)
map)
"Keymap used in the minibuffer.")
(autoload 'hydra-ivy/body "ivy-hydra" "" t)
(defvar ivy-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap switch-to-buffer]
'ivy-switch-buffer)
(define-key map [remap switch-to-buffer-other-window]
'ivy-switch-buffer-other-window)
map)
"Keymap for `ivy-mode'.")
;;* Globals
(cl-defstruct ivy-state
prompt collection
predicate require-match initial-input
history preselect keymap update-fn sort
;; The frame in which `ivy-read' was called
frame
;; The window in which `ivy-read' was called
window
;; The buffer in which `ivy-read' was called
buffer
;; The value of `ivy-text' to be used by `ivy-occur'
text
action
unwind
re-builder
matcher
;; When this is non-nil, call it for each input change to get new candidates
dynamic-collection
;; A lambda that transforms candidates only for display
display-transformer-fn
directory
caller
current
def)
(defvar ivy-last (make-ivy-state)
"The last parameters passed to `ivy-read'.
This should eventually become a stack so that you could use
`ivy-read' recursively.")
(defvar ivy-recursive-last nil)
(defvar ivy-recursive-restore t
"When non-nil, restore the above state when exiting the minibuffer.
This variable is let-bound to nil by functions that take care of
the restoring themselves.")
(defsubst ivy-set-action (action)
"Set the current `ivy-last' field to ACTION."
(setf (ivy-state-action ivy-last) action))
(defun ivy-thing-at-point ()
"Return a string that corresponds to the current thing at point."
(or
(thing-at-point 'url)
(and (eq (ivy-state-collection ivy-last) 'read-file-name-internal)
(ffap-file-at-point))
(let (s)
(cond ((stringp (setq s (thing-at-point 'symbol)))
(if (string-match "\\`[`']?\\(.*?\\)'?\\'" s)
(match-string 1 s)
s))
((looking-at "(+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
(match-string-no-properties 1))
(t
"")))))
(defvar ivy-history nil
"History list of candidates entered in the minibuffer.
Maximum length of the history list is determined by the value
of `history-length'.")
(defvar ivy--directory nil
"Current directory when completing file names.")
(defvar ivy--length 0
"Store the amount of viable candidates.")
(defvar ivy-text ""
"Store the user's string as it is typed in.")
(defvar ivy--index 0
"Store the index of the current candidate.")
(defvar ivy-exit nil
"Store `done' if the completion was successfully selected.
Otherwise, store nil.")
(defvar ivy--all-candidates nil
"Store the candidates passed to `ivy-read'.")
(defvar ivy--extra-candidates '((original-source))
"Store candidates added by the extra sources.
This is an internal-use alist. Each key is a function name, or
original-source (which represents where the current dynamic
candidates should go).
Each value is an evaluation of the function, in case of static
sources. These values will subsequently be filtered on `ivy-text'.
This variable is set by `ivy-read' and used by `ivy--set-candidates'.")
(defcustom ivy-use-ignore-default t
"The default policy for user-configured candidate filtering."
:type '(choice
(const :tag "Ignore ignored always" always)
(const :tag "Ignore ignored when others exist" t)
(const :tag "Don't ignore" nil)))
(defvar ivy-use-ignore t
"Store policy for user-configured candidate filtering.
This may be changed dynamically by `ivy-toggle-ignore'.
Use `ivy-use-ignore-default' for a permanent configuration.")
(defvar ivy--default nil
"Default initial input.")
(defvar ivy--prompt nil
"Store the format-style prompt.
When non-nil, it should contain at least one %d.")
(defvar ivy--prompt-extra ""
"Temporary modifications to the prompt.")
(defvar ivy--old-re nil
"Store the old regexp.
Either a string or a list for `ivy-re-match'.")
(defvar ivy--old-cands nil
"Store the candidates matched by `ivy--old-re'.")
(defvar ivy--regex-function 'ivy--regex
"Current function for building a regex.")
(defvar ivy--highlight-function 'ivy--highlight-default
"Current function for formatting the candidates.")
(defvar ivy--subexps 0
"Number of groups in the current `ivy--regex'.")
(defvar ivy--full-length nil
"The total amount of candidates when :dynamic-collection is non-nil.")
(defvar ivy--old-text ""
"Store old `ivy-text' for dynamic completion.")
(defcustom ivy-case-fold-search-default 'auto
"The default value for `ivy-case-fold-search'."
:type '(choice
(const :tag "Auto" auto)
(const :tag "Always" always)
(const :tag "Never" nil)))
(defvar ivy-case-fold-search ivy-case-fold-search-default
"Store the current overriding `case-fold-search'.")
(defvar Info-current-file)
(defun ivy-re-to-str (re)
(if (stringp re)
re
(caar re)))
(eval-and-compile
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL."
(declare (debug defvar) (doc-string 3))
(list 'progn (list 'defvar var val docstring)
(list 'make-variable-buffer-local (list 'quote var)))))
(unless (fboundp 'setq-local)
(defmacro setq-local (var val)
"Set variable VAR to value VAL in current buffer."
(list 'set (list 'make-local-variable (list 'quote var)) val))))
(defmacro ivy-quit-and-run (&rest body)
"Quit the minibuffer and run BODY afterwards."
`(progn
(put 'quit 'error-message "")
(run-at-time nil nil
(lambda ()
(put 'quit 'error-message "Quit")
,@body))
(minibuffer-keyboard-quit)))
(defun ivy-exit-with-action (action)
"Quit the minibuffer and call ACTION afterwards."
(ivy-set-action
`(lambda (x)
(funcall ',action x)
(ivy-set-action ',(ivy-state-action ivy-last))))
(setq ivy-exit 'done)
(exit-minibuffer))
(defmacro with-ivy-window (&rest body)
"Execute BODY in the window from which `ivy-read' was called."
(declare (indent 0)
(debug t))
`(with-selected-window (ivy--get-window ivy-last)
,@body))
(defun ivy--done (text)
"Insert TEXT and exit minibuffer."
(insert
(setf (ivy-state-current ivy-last)
(if (and ivy--directory
(not (eq (ivy-state-history ivy-last) 'grep-files-history)))
(expand-file-name text ivy--directory)
text)))
(setq ivy-exit 'done)
(exit-minibuffer))
(defcustom ivy-use-selectable-prompt nil
"When non-nil, make the prompt line selectable like a candidate.
The prompt line can be selected by calling `ivy-previous-line' when the first
regular candidate is selected. Both actions `ivy-done' and `ivy-alt-done',
when called on a selected prompt, are forwarded to `ivy-immediate-done', which
results to the same as calling `ivy-immediate-done' explicitely when a regular
candidate is selected.
Note that if `ivy-wrap' is set to t, calling `ivy-previous-line' when the
prompt is selected wraps around to the last candidate, while calling
`ivy-next-line' on the last candidate wraps around to the first
candidate, not the prompt."
:type 'boolean)
(defun ivy--prompt-selectable-p ()
"Return t if the prompt line is selectable."
(and ivy-use-selectable-prompt
(memq (ivy-state-require-match ivy-last)
'(nil confirm confirm-after-completion))))
(defun ivy--prompt-selected-p ()
"Return t if the prompt line is selected."
(and (ivy--prompt-selectable-p)
(= ivy--index -1)))
;;* Commands
(defun ivy-done ()
"Exit the minibuffer with the selected candidate."
(interactive)
(if (ivy--prompt-selected-p)
(ivy-immediate-done)
(setq ivy-current-prefix-arg current-prefix-arg)
(delete-minibuffer-contents)
(cond ((or (> ivy--length 0)
;; the action from `ivy-dispatching-done' may not need a
;; candidate at all
(eq this-command 'ivy-dispatching-done))
(ivy--done (ivy-state-current ivy-last)))
((memq (ivy-state-collection ivy-last)
'(read-file-name-internal internal-complete-buffer))
(if (or (not (eq confirm-nonexistent-file-or-buffer t))
(equal " (confirm)" ivy--prompt-extra))
(ivy--done ivy-text)
(setq ivy--prompt-extra " (confirm)")
(insert ivy-text)
(ivy--exhibit)))
((memq (ivy-state-require-match ivy-last)
'(nil confirm confirm-after-completion))
(ivy--done ivy-text))
(t
(setq ivy--prompt-extra " (match required)")
(insert ivy-text)
(ivy--exhibit)))))
(defvar ivy-read-action-format-function 'ivy-read-action-format-default
"Function used to transform the actions list into a docstring.")
(defun ivy-read-action-format-default (actions)
"Create a docstring from ACTIONS.
ACTIONS is a list. Each list item is a list of 3 items:
key (a string), cmd and doc (a string)."
(format "%s\n%s\n"
(if (eq this-command 'ivy-read-action)
"Select action: "
(ivy-state-current ivy-last))
(mapconcat
(lambda (x)
(format "%s: %s"
(propertize
(car x)
'face 'ivy-action)
(nth 2 x)))
actions
"\n")))
(defun ivy-read-action ()
"Change the action to one of the available ones.
Return nil for `minibuffer-keyboard-quit' or wrong key during the
selection, non-nil otherwise."
(interactive)
(let ((actions (ivy-state-action ivy-last)))
(if (null (ivy--actionp actions))
t
(let* ((hint (funcall ivy-read-action-format-function (cdr actions)))
(resize-mini-windows t)
(key (string (read-key hint)))
(action-idx (cl-position-if
(lambda (x) (equal (car x) key))
(cdr actions))))
(cond ((member key '("" ""))
nil)
((null action-idx)
(message "%s is not bound" key)
nil)
(t
(message "")
(setcar actions (1+ action-idx))
(ivy-set-action actions)))))))
(defun ivy-shrink-after-dispatching ()
"Shrink the window after dispatching when action list is too large."
(let ((window (selected-window)))
(window-resize window (- ivy-height (window-height window)))))
(defun ivy-dispatching-done ()
"Select one of the available actions and call `ivy-done'."
(interactive)
(when (ivy-read-action)
(ivy-done))
(ivy-shrink-after-dispatching))
(defun ivy-dispatching-call ()
"Select one of the available actions and call `ivy-call'."
(interactive)
(setq ivy-current-prefix-arg current-prefix-arg)
(let ((actions (copy-sequence (ivy-state-action ivy-last))))
(unwind-protect
(when (ivy-read-action)
(ivy-call))
(ivy-set-action actions)))
(ivy-shrink-after-dispatching))
(defun ivy-build-tramp-name (x)
"Reconstruct X into a path.
Is is a cons cell, related to `tramp-get-completion-function'."
(let ((user (car x))
(domain (cadr x)))
(if user
(concat user "@" domain)
domain)))
(declare-function tramp-get-completion-function "tramp")
(declare-function Info-find-node "info")
(defun ivy-alt-done (&optional arg)
"Exit the minibuffer with the selected candidate.
When ARG is t, exit with current text, ignoring the candidates."
(interactive "P")
(setq ivy-current-prefix-arg current-prefix-arg)
(cond ((or arg
(ivy--prompt-selected-p))
(ivy-immediate-done))
(ivy--directory
(ivy--directory-done))
((eq (ivy-state-collection ivy-last) 'Info-read-node-name-1)
(if (member (ivy-state-current ivy-last) '("(./)" "(../)"))
(ivy-quit-and-run
(ivy-read "Go to file: " 'read-file-name-internal
:action (lambda (x)
(Info-find-node
(expand-file-name x ivy--directory)
"Top"))))
(ivy-done)))
(t
(ivy-done))))
(defun ivy--directory-done ()
"Handle exit from the minibuffer when completing file names."
(let (dir)
(cond
((equal ivy-text "/sudo::")
(setq dir (concat ivy-text (expand-file-name ivy--directory)))
(ivy--cd dir)
(ivy--exhibit))
((and
(> ivy--length 0)
(not (string= (ivy-state-current ivy-last) "./"))
(setq dir (ivy-expand-file-if-directory (ivy-state-current ivy-last))))
(ivy--cd dir)
(ivy--exhibit))
((and (not (string= ivy-text ""))
(ignore-errors (file-exists-p ivy-text)))
(if (file-directory-p ivy-text)
(ivy--cd (expand-file-name
(file-name-as-directory ivy-text) ivy--directory))
(ivy-done)))
((or (and (equal ivy--directory "/")
(string-match "\\`[^/]+:.*:.*\\'" ivy-text))
(string-match "\\`/[^/]+:.*:.*\\'" ivy-text))
(ivy-done))
((or (and (equal ivy--directory "/")
(cond ((string-match
"\\`\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
ivy-text))
((string-match
"\\`\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
(ivy-state-current ivy-last))
(setq ivy-text (ivy-state-current ivy-last)))))
(string-match
"\\`/\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
ivy-text))
(let ((method (match-string 1 ivy-text))
(user (match-string 2 ivy-text))
(rest (match-string 3 ivy-text))
res)
(require 'tramp)
(dolist (x (tramp-get-completion-function method))
(setq res (append res (funcall (car x) (cadr x)))))
(setq res (delq nil res))
(when user
(dolist (x res)
(setcar x user)))
(setq res (cl-delete-duplicates res :test #'equal))
(let* ((old-ivy-last ivy-last)
(enable-recursive-minibuffers t)
(host (let ((ivy-auto-select-single-candidate nil))
(ivy-read "user@host: "
(mapcar #'ivy-build-tramp-name res)
:initial-input rest))))
(setq ivy-last old-ivy-last)
(when host
(setq ivy--directory "/")
(ivy--cd (concat "/" method ":" host ":"))))))
(t
(ivy-done)))))
(defun ivy-expand-file-if-directory (file-name)
"Expand FILE-NAME as directory.
When this directory doesn't exist, return nil."
(when (stringp file-name)
(let ((full-name
;; Ignore host name must not match method "ssh"
(ignore-errors
(file-name-as-directory
(expand-file-name file-name ivy--directory)))))
(when (and full-name (file-directory-p full-name))
full-name))))
(defcustom ivy-tab-space nil
"When non-nil, `ivy-partial-or-done' should insert a space."
:type 'boolean)
(defun ivy-partial-or-done ()
"Complete the minibuffer text as much as possible.
If the text hasn't changed as a result, forward to `ivy-alt-done'."
(interactive)
(if (and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
(or (and (equal ivy--directory "/")
(string-match "\\`[^/]+:.*\\'" ivy-text))
(string-match "\\`/" ivy-text)))
(let ((default-directory ivy--directory)
dir)
(minibuffer-complete)
(setq ivy-text (ivy--input))
(when (setq dir (ivy-expand-file-if-directory ivy-text))
(ivy--cd dir)))
(or (ivy-partial)
(when (or (eq this-command last-command)
(eq ivy--length 1))
(ivy-alt-done)))))
(defun ivy-partial ()
"Complete the minibuffer text as much as possible."
(interactive)
(let* ((parts (or (split-string ivy-text " " t) (list "")))
(postfix (car (last parts)))
(case-fold-search (and ivy-case-fold-search
(or (eq ivy-case-fold-search 'always)
(string= ivy-text (downcase ivy-text)))))
(completion-ignore-case case-fold-search)
(startp (string-match "^\\^" postfix))
(new (try-completion (if startp
(substring postfix 1)
postfix)
(if (ivy-state-dynamic-collection ivy-last)
ivy--all-candidates
(mapcar (lambda (str)
(let ((i (string-match postfix str)))
(when i
(substring str i))))
ivy--old-cands)))))
(cond ((eq new t) nil)
((string= new ivy-text) nil)
(new
(delete-region (minibuffer-prompt-end) (point-max))
(setcar (last parts)
(if startp
(concat "^" new)
new))
(insert (mapconcat #'identity parts " ")
(if ivy-tab-space " " ""))
t))))
(defvar ivy-completion-beg nil
"Completion bounds start.")
(defvar ivy-completion-end nil
"Completion bounds end.")
(defun ivy-immediate-done ()
"Exit the minibuffer with current input instead of current candidate."
(interactive)
(delete-minibuffer-contents)
(insert (setf (ivy-state-current ivy-last)
(if (and ivy--directory
(not (eq (ivy-state-history ivy-last)
'grep-files-history)))
(expand-file-name ivy-text ivy--directory)
ivy-text)))
(setq ivy-completion-beg ivy-completion-end)
(setq ivy-exit 'done)
(exit-minibuffer))
;;;###autoload
(defun ivy-resume ()
"Resume the last completion session."
(interactive)
(if (null (ivy-state-action ivy-last))
(user-error "The last session isn't compatible with `ivy-resume'")
(when (eq (ivy-state-caller ivy-last) 'swiper)
(switch-to-buffer (ivy-state-buffer ivy-last)))
(with-current-buffer (ivy-state-buffer ivy-last)
(let ((default-directory (ivy-state-directory ivy-last)))
(ivy-read
(ivy-state-prompt ivy-last)
(ivy-state-collection ivy-last)
:predicate (ivy-state-predicate ivy-last)
:require-match (ivy-state-require-match ivy-last)
:initial-input ivy-text
:history (ivy-state-history ivy-last)
:preselect (unless (eq (ivy-state-collection ivy-last)
'read-file-name-internal)
(ivy-state-current ivy-last))
:keymap (ivy-state-keymap ivy-last)
:update-fn (ivy-state-update-fn ivy-last)
:sort (ivy-state-sort ivy-last)
:action (ivy-state-action ivy-last)
:unwind (ivy-state-unwind ivy-last)
:re-builder (ivy-state-re-builder ivy-last)
:matcher (ivy-state-matcher ivy-last)
:dynamic-collection (ivy-state-dynamic-collection ivy-last)
:caller (ivy-state-caller ivy-last))))))
(defvar-local ivy-calling nil
"When non-nil, call the current action when `ivy--index' changes.")
(defun ivy-set-index (index)
"Set `ivy--index' to INDEX."
(setq ivy--index index)
(when ivy-calling
(ivy--exhibit)
(ivy-call)))
(defun ivy-beginning-of-buffer ()
"Select the first completion candidate."
(interactive)
(ivy-set-index 0))
(defun ivy-end-of-buffer ()
"Select the last completion candidate."
(interactive)
(ivy-set-index (1- ivy--length)))
(defun ivy-scroll-up-command ()
"Scroll the candidates upward by the minibuffer height."
(interactive)
(ivy-set-index (min (1- (+ ivy--index ivy-height))
(1- ivy--length))))
(defun ivy-scroll-down-command ()
"Scroll the candidates downward by the minibuffer height."
(interactive)
(ivy-set-index (max (1+ (- ivy--index ivy-height))
0)))
(defun ivy-minibuffer-grow ()
"Grow the minibuffer window by 1 line."
(interactive)
(setq-local max-mini-window-height
(cl-incf ivy-height)))
(defun ivy-minibuffer-shrink ()
"Shrink the minibuffer window by 1 line."
(interactive)
(unless (<= ivy-height 2)
(setq-local max-mini-window-height
(cl-decf ivy-height))
(window-resize (selected-window) -1)))
(defun ivy-next-line (&optional arg)
"Move cursor vertically down ARG candidates."
(interactive "p")
(setq arg (or arg 1))
(let ((index (+ ivy--index arg)))
(if (> index (1- ivy--length))
(if ivy-wrap
(ivy-beginning-of-buffer)
(ivy-set-index (1- ivy--length)))
(ivy-set-index index))))
(defun ivy-next-line-or-history (&optional arg)
"Move cursor vertically down ARG candidates.
If the input is empty, select the previous history element instead."
(interactive "p")
(if (string= ivy-text "")
(ivy-previous-history-element 1)
(ivy-next-line arg)))
(defun ivy-previous-line (&optional arg)
"Move cursor vertically up ARG candidates."
(interactive "p")
(setq arg (or arg 1))
(let ((index (- ivy--index arg))
(min-index (or (and (ivy--prompt-selectable-p) -1)
0)))
(if (< index min-index)
(if ivy-wrap
(ivy-end-of-buffer)
(ivy-set-index min-index))
(ivy-set-index index))))
(defun ivy-previous-line-or-history (arg)
"Move cursor vertically up ARG candidates.
If the input is empty, select the previous history element instead."
(interactive "p")
(when (and (zerop ivy--index) (string= ivy-text ""))
(ivy-previous-history-element 1))
(ivy-previous-line arg))
(defun ivy-toggle-calling ()
"Flip `ivy-calling'."
(interactive)
(when (setq ivy-calling (not ivy-calling))
(ivy-call)))
(defun ivy-toggle-ignore ()
"Toggle user-configured candidate filtering."
(interactive)
(setq ivy-use-ignore
(if ivy-use-ignore
nil
(or ivy-use-ignore-default t)))
;; invalidate cache
(setq ivy--old-cands nil))
(defun ivy--get-action (state)
"Get the action function from STATE."
(let ((action (ivy-state-action state)))
(when action