-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
ivy.el
5496 lines (4940 loc) · 204 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-2024 Free Software Foundation, Inc.
;; Author: Oleh Krehel <[email protected]>
;; Maintainer: Basil L. Contovounesios <[email protected]>
;; URL: https://github.com/abo-abo/swiper
;; Version: 0.14.2
;; Package-Requires: ((emacs "24.5"))
;; 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 <https://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 'colir)
(require 'ivy-overlay)
(require 'ivy-faces)
(require 'cl-lib)
(require 'ring)
(eval-when-compile
(require 'subr-x)
(unless (fboundp 'static-if)
(defmacro static-if (condition then-form &rest else-forms)
"Expand to THEN-FORM or ELSE-FORMS based on compile-time CONDITION.
Polyfill for Emacs 30 `static-if'."
(declare (debug (sexp sexp &rest sexp)) (indent 2))
(if (eval condition lexical-binding)
then-form
(macroexp-progn else-forms)))))
;;* Customization
(defgroup ivy nil
"Incremental vertical completion."
:group 'convenience)
(defcustom ivy-height 10
"Number of lines for the minibuffer window.
See also `ivy-height-alist'."
: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-pre-prompt-function nil
"When non-nil, add strings before the `ivy-read' prompt."
:type '(choice
(const :tag "Do nothing" nil)
(function :tag "Custom function")))
(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 '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'."
:type '(choice
(const :tag "Plain" nil)
(const :tag "Fancy" fancy)))
(defcustom ivy-on-del-error-function #'abort-recursive-edit
"Function to call when deletion fails during completion.
The usual reason for `ivy-backward-delete-char' to fail is when
there is no text left to delete, i.e., when it is called at the
beginning of the minibuffer.
The default setting provides a quick exit from completion.
Another common option is `ignore', which does nothing."
:type '(choice
(const :tag "Exit completion" abort-recursive-edit)
(const :tag "Do nothing" ignore)
(function :tag "Custom 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/or bookmarks to `ivy-switch-buffer'.
The value `recentf' includes only recent files to the virtual
buffers list, whereas the value `bookmarks' does the same for
bookmarks. Any other non-nil value includes both."
:type '(choice
(const :tag "Don't use virtual buffers" nil)
(const :tag "Recent files" recentf)
(const :tag "Bookmarks" bookmarks)
(const :tag "All virtual buffers" t)))
(defvar ivy--display-function nil
"The display-function is used in current.")
(defvar ivy-display-functions-props
'((ivy-display-function-overlay :cleanup ivy-overlay-cleanup))
"Map Ivy display functions to their property lists.
Examples of properties include associated `:cleanup' functions.")
(defcustom ivy-display-functions-alist
'((ivy-completion-in-region . ivy-display-function-overlay)
(t . nil))
"An alist for customizing where to display the candidates.
Each key is a caller symbol. When the value is nil (the default),
the candidates are shown in the minibuffer. Otherwise, the value
is a function which takes a string argument comprising the
current matching candidates and displays it somewhere.
See also `https://github.com/abo-abo/swiper/wiki/ivy-display-function'."
:type '(alist
:key-type symbol
:value-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)
(function :tag "Custom function"))))
(defvar ivy-completing-read-dynamic-collection nil
"Run `ivy-completing-read' with `:dynamic-collection t`.")
(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))
"An alist of handlers to replace `completing-read' in `ivy-mode'."
:type '(alist :key-type symbol :value-type function))
(defcustom ivy-height-alist nil
"An alist to customize `ivy-height'.
It is a list of (CALLER . HEIGHT). CALLER is a caller of
`ivy-read' and HEIGHT is the number of lines displayed.
HEIGHT can also be a function that returns the number of lines."
:type '(alist
:key-type function
:value-type (choice integer 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-highlight-grep-commands nil
"List of grep-like commands.")
(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))))
(defun ivy--compute-extra-actions (action caller)
"Add extra actions to ACTION based on CALLER."
(let* ((extra-actions (cl-delete-duplicates
(append (plist-get ivy--actions-list t)
(plist-get ivy--actions-list this-command)
(plist-get ivy--actions-list caller))
:key #'car :test #'equal))
(override-default (assoc "o" extra-actions)))
(cond (override-default
(cons 1 (cons override-default
(cl-delete "o" extra-actions
:key #'car :test #'equal))))
((not extra-actions)
action)
((functionp action)
`(1
("o" ,action "default")
,@extra-actions))
((null action)
`(1
("o" identity "default")
,@extra-actions))
(t
(cons (car action)
(cl-delete-duplicates (cdr (append action extra-actions))
:key #'car :test #'equal :from-end t))))))
(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-alist 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."
(declare (obsolete "use `ivy-configure' :display-transformer-fn instead."
"0.13.2 (2020-05-20)"))
(ivy--alist-set 'ivy--display-transformers-alist 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)))
(defun ivy--compute-extra-candidates (caller)
(let ((extra-sources (or (plist-get ivy--sources-list caller)
'((original-source))))
(result nil))
(dolist (source extra-sources)
(cond ((equal source '(original-source))
(push source result))
((null (cdr source))
(push (list (car source) (funcall (car source))) result))))
result))
(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)
(defun ivy-define-key (keymap key def)
"Forward to (`define-key' KEYMAP KEY DEF).
Remove DEF from `counsel-M-x' list."
(put def 'no-counsel-M-x t)
(define-key keymap key def))
(defvar ivy-minibuffer-map
(let ((map (make-sparse-keymap)))
(ivy-define-key map (kbd "C-m") 'ivy-done)
(define-key map [down-mouse-1] 'ignore)
(ivy-define-key map [mouse-1] 'ivy-mouse-done)
(ivy-define-key map [mouse-3] 'ivy-mouse-dispatching-done)
(ivy-define-key map (kbd "C-M-m") 'ivy-call)
(ivy-define-key map (kbd "C-j") 'ivy-alt-done)
(ivy-define-key map (kbd "C-M-j") 'ivy-immediate-done)
(ivy-define-key map (kbd "TAB") 'ivy-partial-or-done)
(ivy-define-key map [remap next-line] 'ivy-next-line)
(ivy-define-key map [remap previous-line] 'ivy-previous-line)
(ivy-define-key map (kbd "C-r") 'ivy-reverse-i-search)
(define-key map (kbd "SPC") 'self-insert-command)
(ivy-define-key map [remap delete-backward-char] 'ivy-backward-delete-char)
(ivy-define-key map [remap backward-delete-char-untabify] 'ivy-backward-delete-char)
(ivy-define-key map [remap backward-kill-word] 'ivy-backward-kill-word)
(ivy-define-key map [remap delete-char] 'ivy-delete-char)
(ivy-define-key map [remap forward-char] 'ivy-forward-char)
(ivy-define-key map (kbd "<right>") 'ivy-forward-char)
(ivy-define-key map [remap kill-word] 'ivy-kill-word)
(ivy-define-key map [remap beginning-of-buffer] 'ivy-beginning-of-buffer)
(ivy-define-key map [remap end-of-buffer] 'ivy-end-of-buffer)
(ivy-define-key map (kbd "M-n") 'ivy-next-history-element)
(ivy-define-key map (kbd "M-p") 'ivy-previous-history-element)
(define-key map (kbd "C-g") 'minibuffer-keyboard-quit)
(ivy-define-key map [remap scroll-up-command] 'ivy-scroll-up-command)
(ivy-define-key map [remap scroll-down-command] 'ivy-scroll-down-command)
(ivy-define-key map (kbd "<next>") 'ivy-scroll-up-command)
(ivy-define-key map (kbd "<prior>") 'ivy-scroll-down-command)
(ivy-define-key map (kbd "C-v") 'ivy-scroll-up-command)
(ivy-define-key map (kbd "M-v") 'ivy-scroll-down-command)
(ivy-define-key map (kbd "C-M-n") 'ivy-next-line-and-call)
(ivy-define-key map (kbd "C-M-p") 'ivy-previous-line-and-call)
(ivy-define-key map (kbd "M-a") 'ivy-toggle-marks)
(ivy-define-key map (kbd "M-r") 'ivy-toggle-regexp-quote)
(ivy-define-key map (kbd "M-j") 'ivy-yank-word)
(ivy-define-key map (kbd "M-i") 'ivy-insert-current)
(ivy-define-key map (kbd "C-M-y") 'ivy-insert-current-full)
(ivy-define-key map (kbd "C-o") 'hydra-ivy/body)
(ivy-define-key map (kbd "M-o") 'ivy-dispatching-done)
(ivy-define-key map (kbd "C-M-o") 'ivy-dispatching-call)
(ivy-define-key map [remap kill-line] 'ivy-kill-line)
(ivy-define-key map [remap kill-whole-line] 'ivy-kill-whole-line)
(ivy-define-key map (kbd "S-SPC") 'ivy-restrict-to-matches)
(ivy-define-key map [remap kill-ring-save] 'ivy-kill-ring-save)
(ivy-define-key map (kbd "C-M-a") 'ivy-read-action)
(ivy-define-key map (kbd "C-c C-o") 'ivy-occur)
(ivy-define-key map (kbd "C-c C-a") 'ivy-toggle-ignore)
(ivy-define-key map (kbd "C-c C-s") 'ivy-rotate-sort)
(ivy-define-key map [remap describe-mode] 'ivy-help)
(ivy-define-key map "$" 'ivy-magic-read-file-env)
map)
"Keymap used in the minibuffer.")
(autoload 'hydra-ivy/body "ivy-hydra" "" t)
(autoload 'ivy-hydra-read-action "ivy-hydra" "" t)
(defvar ivy-mode-map
(let ((map (make-sparse-keymap)))
(ivy-define-key map [remap switch-to-buffer] 'ivy-switch-buffer)
(ivy-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
ignore
multi-action
extra-props)
(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--sessions nil
"Alist mapping session symbols to `ivy-state' objects.")
(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))
(defvar inhibit-message)
(defvar ffap-machine-p-known)
(defun ivy-thing-at-point ()
"Return a string that corresponds to the current thing at point."
(substring-no-properties
(cond
((use-region-p)
(let* ((beg (region-beginning))
(end (region-end))
(eol (save-excursion (goto-char beg) (line-end-position))))
(buffer-substring-no-properties beg (min end eol))))
((let ((url (thing-at-point 'url)))
;; Work around `https://bugs.gnu.org/58091'.
(and (stringp url) url)))
((and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
(let ((inhibit-message t)
(ffap-machine-p-known 'reject))
(run-hook-with-args-until-success 'file-name-at-point-functions))))
((let ((s (thing-at-point 'symbol)))
(and (stringp s)
(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--directory-hist nil
"Store the history of directories.
This allows RET to reverse consecutive DEL.")
(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-regex ""
"Store the regex value that corresponds to `ivy-text'.")
(defvar ivy--regex-function 'ivy--regex
"Current function for building a regex.")
(defun ivy-set-text (str)
"Set `ivy-text' to STR."
(setq ivy-text str)
(setq ivy-regex (funcall ivy--regex-function ivy-text)))
(defvar ivy--index 0
"Store the index of the current candidate.")
(defvar ivy--window-index 0
"Store the index of the current candidate in the minibuffer window.
This means it's between 0 and `ivy-height'.")
(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--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.")
(defvar ivy--trying-to-resume-dynamic-collection nil
"Non-nil if resuming from a dynamic collection.
When non-nil, ivy will wait until the first chunk of asynchronous
candidates has been received before selecting the last
preselected candidate.")
(defun ivy--set-index-dynamic-collection ()
(when ivy--trying-to-resume-dynamic-collection
(let ((preselect-index
(ivy--preselect-index (ivy-state-preselect ivy-last) ivy--all-candidates)))
(when preselect-index
(ivy-set-index preselect-index)))
(setq ivy--trying-to-resume-dynamic-collection nil)))
(defcustom ivy-case-fold-search-default
(if search-upper-case
'auto
case-fold-search)
"The default value for `case-fold-search' in Ivy operations.
The special value `auto' means case folding is performed so long
as the entire input string comprises lower-case characters. This
corresponds to the default behaviour of most Emacs search
functionality, e.g. as seen in `isearch'."
:link '(info-link "(emacs)Lax Search")
:type '(choice
(const :tag "Auto" auto)
(const :tag "Always" t)
(const :tag "Never" nil)))
(defvar ivy-case-fold-search ivy-case-fold-search-default
"Store the current overriding `case-fold-search'.")
(defcustom ivy-more-chars-alist
'((t . 3))
"Map commands to their minimum required input length.
That is the number of characters prompted for before fetching
candidates. The special key t is used as a fallback."
:type '(alist :key-type symbol :value-type integer))
(defun ivy-more-chars ()
"Return two fake candidates prompting for at least N input.
N is obtained from `ivy-more-chars-alist'."
(let ((diff (- (ivy-alist-setting ivy-more-chars-alist)
(length ivy-text))))
(when (> diff 0)
(list "" (format "%d chars more" diff)))))
(defun ivy--case-fold-p (string)
"Return nil if STRING should be matched case-sensitively."
(if (eq ivy-case-fold-search 'auto)
(string= string (downcase string))
ivy-case-fold-search))
(defun ivy--case-fold-string= (s1 s2)
"Like `string=', but obeys `case-fold-search'."
(eq t (compare-strings s1 nil nil s2 nil nil case-fold-search)))
(defmacro ivy-quit-and-run (&rest body)
"Quit the minibuffer and run BODY afterwards."
(declare (indent 0))
`(progn
(put 'quit 'error-message "")
(run-at-time nil nil
(lambda ()
(put 'quit 'error-message "Quit")
(with-demoted-errors "Error: %S"
,@body)))
(abort-recursive-edit)))
(defun ivy-exit-with-action (action &optional exit-code)
"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 (or exit-code '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--expand-file-name (text)
(cond
((eq (ivy-state-history ivy-last) 'grep-files-history)
text)
(ivy--directory
(if (and (string-match-p "^/" text) (file-remote-p ivy--directory))
(let ((parts (split-string ivy--directory ":")))
(concat (nth 0 parts) ":" (nth 1 parts) ":" text))
(expand-file-name text ivy--directory)))
(t
text)))
(defun ivy--done (text)
"Insert TEXT and exit minibuffer."
(if (member (ivy-state-prompt ivy-last) '("Create directory: " "Make directory: "))
(ivy-immediate-done)
(when (stringp text)
(insert
(setf (ivy-state-current ivy-last)
(ivy--expand-file-name 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' explicitly 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)
(defvar ivy--use-selectable-prompt nil
"Store the effective `ivy-use-selectable-prompt' for current session.")
(defun ivy--prompt-selectable-p ()
"Return t if the prompt line is selectable."
(and ivy-use-selectable-prompt
(or (memq (ivy-state-require-match ivy-last)
'(nil confirm confirm-after-completion))
;; :require-match is t, but "" is in the collection
(let ((coll (ivy-state-collection ivy-last)))
(and (listp coll)
(if (consp (car coll))
(member '("") coll)
(member "" coll)))))))
(defun ivy--prompt-selected-p ()
"Return t if the prompt line is selected."
(and ivy--use-selectable-prompt
(= 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)
(let ((require-match (ivy-state-require-match ivy-last))
(input (ivy--input)))
(delete-minibuffer-contents)
(cond ((and (= ivy--length 0)
(eq this-command 'ivy-dispatching-done))
(ivy--done ivy-text))
((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)))
((string= " (confirm)" ivy--prompt-extra)
(ivy--done ivy-text))
((or (and (memq (ivy-state-collection ivy-last)
'(read-file-name-internal internal-complete-buffer))
(eq confirm-nonexistent-file-or-buffer t))
(and (functionp require-match)
(setq require-match (funcall require-match))))
(setq ivy--prompt-extra " (confirm)")
(insert input)
(ivy--exhibit))
((memq require-match '(nil confirm confirm-after-completion))
(ivy--done ivy-text))
(t
(setq ivy--prompt-extra " (match required)")
(insert ivy-text)
(ivy--exhibit))))))
(defvar ivy-mouse-1-tooltip
"Exit the minibuffer with the selected candidate."
"The doc visible in the tooltip for mouse-1 binding in the minibuffer.")
(defvar ivy-mouse-3-tooltip
"Display alternative actions."
"The doc visible in the tooltip for mouse-3 binding in the minibuffer.")
(make-obsolete-variable 'ivy-mouse-1-tooltip 'ivy-mouse-1-help
"0.15.0 (2024-01-14)")
(make-obsolete-variable 'ivy-mouse-3-tooltip 'ivy-mouse-3-help
"0.15.0 (2024-01-14)")
(defvar ivy-mouse-1-help
(format (if (> emacs-major-version 28) "\\`%s': %s" "%s: %s")
"mouse-1" "Exit the minibuffer with the selected candidate")
"Tooltip doc for \\`mouse-1' binding in the minibuffer.")
(defvar ivy-mouse-3-help
(format (if (> emacs-major-version 28) "\\`%s': %s" "%s: %s")
"mouse-3" "Display alternative actions")
"Tooltip doc for \\`mouse-3' binding in the minibuffer.")
(defun ivy--help-echo (_win _obj _pos)
"Return a `help-echo' string for mouse bindings on minibuffer candidates."
(concat ivy-mouse-1-help (if tooltip-mode "\n" " ") ivy-mouse-3-help))
(defun ivy-mouse-offset (event)
"Compute the offset between the candidate at point and the selected one."
(if event
(let* ((line-number-at-point
(max 2
(line-number-at-pos (posn-point (event-start event)))))
(line-number-candidate ;; convert to 0 based index
(- line-number-at-point 2))
(offset
(- line-number-candidate
ivy--window-index)))
offset)
nil))
(defun ivy-mouse-done (event)
(interactive "@e")
(let ((offset (ivy-mouse-offset event)))
(when offset
(ivy-next-line offset)
(ivy--exhibit)
(ivy-alt-done))))
(defun ivy-mouse-dispatching-done (event)
(interactive "@e")
(let ((offset (ivy-mouse-offset event)))
(when offset
(ivy-next-line offset)
(ivy--exhibit)
(ivy-dispatching-done))))
(defcustom ivy-read-action-format-function 'ivy-read-action-format-default
"Function used to transform the actions list into a docstring."
:type '(radio
(function-item ivy-read-action-format-default)
(function-item ivy-read-action-format-columns)))
(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-format-columns (actions)
"Create a potentially multi-column docstring from ACTIONS.
Several columns are used as needed to preserve `ivy-height'.
ACTIONS is a list with elements of the form (KEY COMMAND DOC),
where KEY and DOC are strings."
(let ((length (length actions))
(i 0)
(max-rows (- ivy-height 1))
rows cols col lwidth rwidth)
(while (< i length)
(setq col (cl-subseq actions i (min length (cl-incf i max-rows))))
(setq lwidth (apply 'max (mapcar (lambda (x)
(length (nth 0 x)))
col)))
(setq rwidth (apply 'max (mapcar (lambda (x)
(length (nth 2 x)))
col)))
(setq col (mapcar (lambda (x)
(format (format "%%%ds: %%-%ds" lwidth rwidth)
(propertize (car x) 'face 'ivy-action)
(nth 2 x)))
col))
(cond
((null rows)
(setq rows (length col)))
((< (length col) rows)
(setq col (append col (make-list (- rows (length col)) "")))))
(push col cols))
(format "%s\n%s\n"
(if (eq this-command 'ivy-read-action)
"Select action: "
(ivy-state-current ivy-last))
(mapconcat 'identity
(apply 'cl-mapcar
(lambda (&rest args)
(mapconcat 'identity args " | "))
(nreverse cols))
"\n"))))
(defcustom ivy-read-action-function #'ivy-read-action-by-key
"Function used to read an action."
:type '(radio
(function-item ivy-read-action-by-key)
(function-item ivy-read-action-ivy)
(function-item ivy-hydra-read-action)))
(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 (not (ivy--actionp actions))
t
(let ((ivy--directory ivy--directory))
(funcall ivy-read-action-function actions)))))
(defvar set-message-function)
(defun ivy-read-action-by-key (actions)
(let* ((set-message-function nil)
(hint (funcall ivy-read-action-format-function (cdr actions)))
(resize-mini-windows t)
(key "")
action-idx)
(while (and (setq action-idx (cl-position-if
(lambda (x)
(string-prefix-p key (car x)))
(cdr actions)))
(not (string= key (car (nth action-idx (cdr actions))))))
(setq key (concat key (key-description (vector (read-key hint))))))
;; Ignore resize errors with minibuffer-only frames (#2726).
(ignore-errors (ivy-shrink-after-dispatching))
(cond ((member key '("ESC" "C-g" "M-o"))
nil)
((null action-idx)
(message "%s is not bound" key)
nil)
(t
(message "")
(setcar actions (1+ action-idx))
(ivy-set-action actions)))))
(defvar ivy-marked-candidates nil
"List of marked candidates.
Use `ivy-mark' to populate this.
When this list is non-nil at the end of the session, the action
will be called for each element of this list.")
(defun ivy-read-action-ivy (actions)
"Select an action from ACTIONS using Ivy."
(let ((enable-recursive-minibuffers t))
(if (and (> (minibuffer-depth) 1)
(eq (ivy-state-caller ivy-last) 'ivy-read-action-ivy))
(minibuffer-keyboard-quit)
(let ((ivy-marked-candidates ivy-marked-candidates))
(ivy-read (format "action (%s): " (ivy-state-current ivy-last))
(cl-mapcar
(lambda (a i) (cons (format "[%s] %s" (nth 0 a) (nth 2 a)) i))
(cdr actions) (number-sequence 1 (length (cdr actions))))
:action (lambda (a)
(setcar actions (cdr a))
(ivy-set-action actions))
:caller 'ivy-read-action-ivy)))))
(defun ivy-shrink-after-dispatching ()
"Shrink the window after dispatching when action list is too large."
(when (window-minibuffer-p)
(window-resize nil (- ivy-height (window-height)))))
(defun ivy-dispatching-done ()
"Select one of the available actions and call `ivy-done'."
(interactive)
(let ((ivy-exit 'ivy-dispatching-done))
(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)))
(old-ivy-text ivy-text))
(unwind-protect
(when (ivy-read-action)
(ivy-set-text old-ivy-text)
(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 Info-find-node "info")
(declare-function Info-read-node-name-1 "info")
(declare-function tramp-get-completion-function "tramp")
(defcustom ivy-alt-done-functions-alist nil
"Customize what `ivy-alt-done' does per-collection."
:type '(alist :key-type symbol :value-type function))
(defun ivy--completing-fname-p ()
(let ((meta (ignore-errors
(funcall (ivy-state-collection ivy-last) ivy-text nil 'metadata))))
(and (consp meta)
(eq 'file (cdr (assoc 'category meta))))))
(defun ivy-alt-done (&optional arg)
"Exit the minibuffer with the selected candidate.
When ARG is t, exit with current text, ignoring the candidates.
When the current candidate during file name completion is a
directory, continue completion from within that directory instead
of exiting. This function is otherwise like `ivy-done'."
(interactive "P")
(setq ivy-current-prefix-arg current-prefix-arg)
(let (alt-done-fn)
(cond ((or arg (ivy--prompt-selected-p))
(ivy-immediate-done))
((setq alt-done-fn (ivy-alist-setting ivy-alt-done-functions-alist))
(funcall alt-done-fn))
((ivy--completing-fname-p)
(ivy--directory-done))
(t
(ivy-done)))))
(defun ivy--info-alt-done ()
(if (member (ivy-state-current ivy-last) '("(./)" "(../)"))
(ivy-quit-and-run
(ivy-read "Go to file: " #'read-file-name-internal