-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
helm-mode.el
2921 lines (2677 loc) · 134 KB
/
helm-mode.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
;;; helm-mode.el --- Enable helm completion everywhere. -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2023 Thierry Volpiatto
;; This program 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 of the License, 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-lib)
(require 'helm)
(require 'helm-lib)
(require 'helm-files)
(require 'helm-misc)
(defvar crm-separator)
(defvar ido-everywhere)
(defvar completion-flex-nospace)
(defvar helm-completion--sorting-done)
(defvar helm-mode)
(defvar password-cache)
(defvar package--builtins)
(defvar helm--locate-library-doc-cache)
(defvar helm--locate-library-cache)
(defvar completion-lazy-hilit) ; Emacs-30 only.
(defvar eww-bookmarks)
;; No warnings in Emacs built --without-x
(declare-function x-file-dialog "xfns.c")
(declare-function ido-mode "ido.el")
(declare-function helm-apropos-init "helm-elisp")
(declare-function helm-lisp-completion-persistent-action "helm-elisp")
(declare-function helm-lisp-completion-persistent-help "helm-elisp")
(declare-function help--symbol-class "help-fns.el")
(declare-function helm-get-first-line-documentation "helm-elisp")
(declare-function package-desc-summary "package")
(declare-function package-built-in-p "package")
(declare-function package-desc-status "package")
(declare-function package-get-descriptor "package")
(declare-function print-coding-system-briefly "mul-diag.el")
(declare-function color-rgb-to-hex "color.el")
(declare-function find-library-name "find-func.el")
(defgroup helm-mode nil
"Enable helm completion."
:group 'helm)
(defcustom helm-completing-read-handlers-alist
'((find-tag . helm-completing-read-default-find-tag)
(ggtags-find-tag-dwim . helm-completing-read-default-find-tag)
(tmm-menubar . nil)
(find-file . nil)
(execute-extended-command . nil)
(dired-do-rename . helm-read-file-name-handler-1)
(dired-do-copy . helm-read-file-name-handler-1)
(dired-do-symlink . helm-read-file-name-handler-1)
(dired-do-relsymlink . helm-read-file-name-handler-1)
(dired-do-hardlink . helm-read-file-name-handler-1)
;; Next two are using completing-read where not needed.
(read-multiple-choice--long-answers . nil)
(dired-do-touch . nil)
(basic-save-buffer . helm-read-file-name-handler-1)
(write-file . (default helm-read-file-name-handler-1))
(write-region . (default helm-read-file-name-handler-1))
(all-the-icons-insert . helm-mode-all-the-icons-handler))
"Completing read functions for specific Emacs commands.
By default `helm-mode' use `helm-completing-read-default-handler' to
provide helm completion in each `completing-read' or `read-file-name'
found, but other functions can be specified here for specific
commands. This also allows disabling helm completion for some commands
when needed.
Each entry is a cons cell like (EMACS_COMMAND . COMPLETING-READ_HANDLER)
where key and value are symbols.
However if a command is using in its definition both a `completing-read' AND
a `read-file-name' we may want to specify a handler for both of them,
this can be done by specifying value as a list of two symbols instead of
a single symbol where the 1st element of the list specify the handler for the
`completing-read' and the second the handler for the `read-file-name'.
Special symbol \\='default' means use the default helm handler for either
`completing-read' or `read-file-name'.
e.g. (write-region . (default helm-read-file-name-handler-1))
means helm will use `helm-completing-read-default-handler' when
`write-region' calls `completing-read' and
`helm-read-file-name-handler-1' when it calls `read-file-name'.
Each key is an Emacs command that use originaly `completing-read'
or/and `read-file-name'.
Each value maybe a helm function that takes same arguments as
`completing-read' plus NAME and BUFFER, where NAME is the name of the new
helm source and BUFFER the name of the buffer we will use, but it can
be also a function not using helm, in this case the function should
take the same args as `completing-read' and not be prefixed by \"helm-\".
`helm' will use the name of the command calling `completing-read' as
NAME and BUFFER will be computed as well with NAME but prefixed with
\"*helm-mode-\".
This function prefix name must start by \"helm-\" when it uses helm,
otherwise `helm' assumes the function is not a helm function and
expects the same args as `completing-read', this allows you to define a
handler not using helm completion.
Example:
(defun foo/test ()
(interactive)
(message \"%S\" (completing-read \"test: \" \\='(a b c d e))))
(defun helm-foo/test-completing-read-handler (prompt collection
predicate require-match
initial-input hist def
inherit-input-method
name buffer)
(helm-comp-read prompt collection :marked-candidates t
:name name
:buffer buffer))
(add-to-list \\='helm-completing-read-handlers-alist
\\='(foo/test . helm-foo/test-completing-read-handler))
We want here to make the regular `completing-read' in `foo/test'
return a list of candidate(s) instead of a single candidate.
Note that this function will be reused for ALL the `completing-read'
of this command, so it should handle all cases. E.g.,
if first `completing-read' completes against symbols and
second `completing-read' should handle only buffer,
your specialized function should handle both.
If the value of an entry is nil completion will fall back to
Emacs vanilla behaviour.
Example:
If you want to disable helm completion for `describe-function', use:
(describe-function . nil)
Ido is also supported, you can use `ido-completing-read' and
`ido-read-file-name' as value of an entry or just \\='ido.
Example:
Enable ido completion for `find-file':
(find-file . ido)
same as
(find-file . ido-read-file-name)
Note that you don't need to enable `ido-mode' for this to work, see
`helm-mode' documentation."
:group 'helm-mode
:type '(alist
:key-type symbol
:value-type (choice
function
(list :tag "Specify the completing-read and read-file-name handlers"
(choice
(const :tag "Use default helm completing-read handler" default)
(function :tag "Use this helm completing-read function"))
(function :tag "Use this helm read file name function"))
(other :tag "Disabled" nil))))
(defcustom helm-comp-read-case-fold-search helm-case-fold-search
"Default Local setting of `helm-case-fold-search' for `helm-comp-read'.
See `helm-case-fold-search' for more info."
:group 'helm-mode
:type 'symbol)
(defcustom helm-mode-handle-completion-in-region t
"Whether to replace or not `completion-in-region-function'.
This enables support for `completing-read-multiple' and `completion-at-point'
when non--nil."
:group 'helm-mode
:type 'boolean)
(defcustom helm-mode-no-completion-in-region-in-modes nil
"A list of modes that do not want helm for `completion-in-region'."
:group 'helm-mode
:type 'boolean)
(defcustom helm-mode-reverse-history t
"Display history source after current source when non nil.
Apply only in `helm-mode' handled commands."
:group 'helm-mode
:type 'boolean)
(defcustom helm-completion-in-region-default-sort-fn
'helm-completion-in-region-sort-fn
"The default sort function to sort candidates in completion-in-region.
When nil no sorting is done.
The function is a `filtered-candidate-transformer' function which takes
two args CANDIDATES and SOURCE.
The function must use the flag `helm-completion--sorting-done' and
return CANDIDATES unchanged when the flag is nil.
See default function `helm-completion-in-region-sort-fn' as example.
It will be used only when `helm-completion-style' is either Emacs or
helm, otherwise when helm-fuzzy style is used, the fuzzy sort function
will be used."
:group 'helm-mode
:type 'function)
(defcustom helm-mode-ignore-diacritics nil
"Ignore diacritics in completing-read."
:group 'helm-mode
:type 'boolean)
(defcustom helm-completion-mark-suffix t
"Push mark at end of suffix when non nil."
:group 'helm-mode
:type 'boolean)
(defcustom helm-read-file-name-use-default-arg-behavior nil
"Use emacs vanilla `read-file-name' behavior for default arg.
The behavior of default arg in `read-file-name' and friends is using
the default arg as default value when initial input is not modified,
even if this initial input is a valid value i.e. an existing file.
We expect generally a default arg to be used if nothing is specified
in the prompt or if what is specified is invalid, but the emacs behavior
here is really weird, so we use this variable to disable this
behavior, letting user specify default if needed with `M-n'.
However we keep the emacs default for `read-file-name' and derived
fns, this variable affecting only `helm-read-file-name'."
:type 'boolean
:group 'helm-mode)
(defvar helm-mode-minibuffer-setup-hook-black-list '(minibuffer-completion-help)
"Incompatible `minibuffer-setup-hook' functions go here.
A list of symbols. `helm-mode' is rejecting all lambda's, byte-code fns
and all functions belonging in this list from `minibuffer-setup-hook'.
This is mainly needed to prevent \"*Completions*\" buffers to popup.")
(defvar helm-comp-read-require-match-overrides '((describe-function . t)
(describe-command . t)
(describe-minor-mode . t)
(load-theme . t)
(describe-theme . t))
"Allow overriding REQUIRE-MATCH completing-read arg for a specific function.")
(defcustom helm-completions-detailed (and (boundp 'completions-detailed)
completions-detailed)
"Allow providing `completions-detailed' for Emacs < 28.
Not guaranteed to work with Emacs < 27."
:type 'boolean
:group 'helm-mode)
(defvar helm-mode-find-file-target-alist
'(("switch-to-buffer" . helm-buffers-quit-and-find-file-fn))
"An alist composed of (SOURCE_NAME . FUNCTION) elements.
Where FUNCTION is a function suitable for `helm-quit-and-find-file'.")
(defface helm-mode-prefix
`((t ,@(and (>= emacs-major-version 27) '(:extend t))
(:background "red" :foreground "black")))
"Face used for prefix completion."
:group 'helm-mode)
(defface helm-completion-invalid
'((t :inherit font-lock-property-name-face))
"Face used to highlight invalid functions."
:group 'helm-mode)
(defface helm-completions-detailed
'((t :inherit font-lock-warning-face))
"Face used to highlight completion-detailed informations."
:group 'helm-mode)
(defface helm-completions-annotations
'((t :inherit font-lock-property-name-face))
"Face used to highlight annotations in completion."
:group 'helm-mode)
(defvar helm-comp-read-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "<C-return>") 'helm-cr-empty-string)
(define-key map (kbd "M-RET") 'helm-cr-empty-string)
map)
"Keymap for `helm-comp-read'.")
(defvar helm-comp-in-region-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-comp-read-map)
map)
"Keymap for completion-at-point and friends.")
(defun helm-mode-delete-char-backward-1 ()
(interactive)
(condition-case err
(call-interactively 'delete-backward-char)
(text-read-only
(if (with-selected-window (minibuffer-window)
(not (string= (minibuffer-contents) "")))
(message "Trying to delete prefix completion, next hit will quit")
(user-error "%s" (car err))))))
(put 'helm-mode-delete-char-backward-1 'helm-only t)
(defun helm-mode-delete-char-backward-2 ()
(interactive)
(condition-case _err
(call-interactively 'delete-backward-char)
(text-read-only
(unless (with-selected-window (minibuffer-window)
(string= (minibuffer-contents) ""))
(with-helm-current-buffer
(run-with-timer 0.1 nil (lambda ()
(call-interactively 'delete-backward-char))))
(helm-keyboard-quit)))))
(put 'helm-mode-delete-char-backward-2 'helm-only t)
(helm-multi-key-defun helm-mode-delete-char-backward-maybe
"Delete char backward when text is not the prefix helm is completing against.
First call warns user about deleting prefix completion.
Second call deletes backward char in current-buffer and quits helm completion,
letting the user start a new completion with a new prefix."
'(helm-mode-delete-char-backward-1 helm-mode-delete-char-backward-2) 1)
(defcustom helm-completion-style 'helm
"Style of completion to use in `completion-in-region'.
This affects only `completion-at-point' and friends, and
the `completing-read' using the default handler
i.e. `helm-completing-read-default-handler'.
NB: This has nothing to do with `completion-styles', it is independent from
helm, but when using \\='emacs as helm-completion-style helm
will use the `completion-styles' for its completions.
Up to the user to configure `completion-styles'.
There are three possible values to use:
- helm, use multi match regular helm completion.
- helm-fuzzy, use fuzzy matching. Note that as usual when
entering a space helm switches to multi matching mode.
- emacs, use regular Emacs completion according to
`completion-styles'. Note that even in this style, helm allows using
multi match. Emacs-27 provides a style called `flex' that can be used
aside `helm' style (see `completion-styles-alist'). When `flex' style
is not available (Emacs<27) helm provides `helm-flex' style which is
similar to `flex' and helm fuzzy matching.
NOTE: The flex style may become unstable after some time in same session due to
its buggy adjust metadata function which make the metadata object huge and
circular, this may crash emacs. You can force the usage of the helm-flex style
which is safer by removing flex style from `completion-styles-alist' BEFORE
loading helm.
For a better experience with \\='emacs style, if you don't know what to use, set
`completion-styles' to \\='(flex) if you are using emacs-27 or to
\\='(helm-flex) if you are using emacs-26 or you want to force using helm-flex
instead of flex (see note above about flex).
See also `helm-completion-styles-alist' to override
`helm-completion-style' for specific modes and commands.
Of course when using `helm' or `helm-fuzzy' as `helm-completion-style'
emacs `completion-styles' have no effect.
Please use custom interface or `customize-set-variable' to set this,
NOT `setq'."
:group 'helm-mode
:type '(choice (const :tag "Emacs" emacs)
(const :tag "Helm" helm)
(const :tag "Helm-fuzzy" helm-fuzzy))
:set (lambda (var val)
(set var val)
(if (memq val '(helm helm-fuzzy))
(define-key helm-comp-in-region-map (kbd "DEL") 'helm-mode-delete-char-backward-maybe)
(define-key helm-comp-in-region-map (kbd "DEL") 'delete-backward-char))))
(defconst helm-completion--all-styles
(let ((flex (if (assq 'flex completion-styles-alist)
'flex 'helm-flex)))
(helm-fast-remove-dups
(append (list 'helm flex)
(mapcar 'car completion-styles-alist)))))
(defconst helm-completion--styles-type
`(repeat :tag "with other completion styles"
(choice ,@(mapcar (lambda (x) (list 'const x))
helm-completion--all-styles))))
(defcustom helm-completion-styles-alist `((gud-mode . helm)
;; See https://github.com/djcb/mu/issues/2181.
(mu4e-compose-mode . emacs)
(wfnames-mode . (emacs helm ,(if (assq 'flex completion-styles-alist)
'flex 'helm-flex))))
"Allow configuring `helm-completion-style' per mode or command.
NOTE: Commands involving `completing-read' specified in
`helm-completing-read-handlers-alist' take precedence on commands
you put here. Specifying a mode instead of a command affect only
completion-in-region and not the completing-read's called in this mode, use
`helm-completing-read-handlers-alist' for this.
Each entry is a cons cell like (mode_or_command . style) where
style must be a suitable value for `helm-completion-style'. When
specifying emacs as style for a mode or a command,
`completion-styles' can be specified by using a cons cell
specifying completion-styles to use with helm emacs style,
e.g. (foo-mode . (emacs helm flex)) will set `completion-styles'
to \\='(helm flex) for foo-mode."
:group 'helm-mode
:type
`(alist :key-type (symbol :tag "Major Mode")
:value-type
(choice :tag "Use helm style or completion styles"
(radio :tag "Helm Style"
(const helm)
(const helm-fuzzy)
(const emacs))
(cons :tag "Completion Styles"
(const :tag "Using Helm `emacs' style" emacs)
,helm-completion--styles-type))))
;;; helm-comp-read
;;
;;
(defvar helm-comp-read-use-marked nil
"[INTERNAL] When non nil `helm-comp-read' will return marked candidates.
Use this ONLY in `let', NOT globally, this allows third party packages
to use a list as return value when `helm-mode' is enabled, e.g.
(let ((helm-comp-read-use-marked t))
(completing-read \"test: \" \\='(a b c d e f g)))
")
(defun helm-cr-empty-string ()
"Return empty string."
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action
(lambda (_candidate)
(identity "")))))
(put 'helm-cr-empty-string 'helm-only t)
(defun helm-mode--keyboard-quit ()
;; Use this instead of `keyboard-quit'
;; to avoid deactivating mark in current-buffer.
(let ((debug-on-quit nil))
(signal 'quit nil)))
(cl-defun helm-comp-read-get-candidates (collection &optional
test sort-fn alistp
(input helm-pattern))
"Convert COLLECTION to list removing elements that don't match TEST.
See `helm-comp-read' about supported COLLECTION arguments.
SORT-FN is a predicate to sort COLLECTION.
ALISTP when non--nil will not use `all-completions' to collect
candidates because it doesn't handle alists correctly for helm.
i.e In `all-completions' the car of each pair is used as value.
In helm we want to use the cdr instead like (display . real),
so we return the alist as it is with no transformation by
`all-completions'.
e.g
\(setq A \\='((a . 1) (b . 2) (c . 3)))
==>((a . 1) (b . 2) (c . 3))
\(helm-comp-read \"test: \" A :alistp nil
:exec-when-only-one t
:initial-input \"a\")
==>\"a\" Which is not what we expect.
\(helm-comp-read \"test: \" A :alistp t
:exec-when-only-one t
:initial-input \"1\")
==>\"1\"
See docstring of `all-completions' for more info.
INPUT is the string you want to complete against, defaulting to
`helm-pattern' which is the value of what you enter in minibuffer.
Note that when using a function as COLLECTION this value will be
available with the input argument of the function only when using a
sync source from `helm-comp-read', i.e. not using
`:candidates-in-buffer', otherwise the function is called only once
with an empty string as value for `helm-pattern' because
`helm-pattern' is not yet computed, which is what we want otherwise
data would not be fully collected at init time.
If COLLECTION is an `obarray', a TEST should be needed. See `obarray'."
;; Ensure COLLECTION is computed from `helm-current-buffer'
;; because some functions used as COLLECTION work
;; only in the context of current-buffer (Bug#1030) .
(with-helm-current-buffer
(let ((cands
(cond ((and alistp (hash-table-p collection))
(cl-loop for k being the hash-keys of collection
using (hash-values v)
collect (cons k v)))
((vectorp collection)
(all-completions input collection test))
((and (symbolp collection) (boundp collection)
;; Bug#324 history is let-bounded and given
;; quoted as hist argument of completing-read.
;; See example in `rcirc-browse-url'.
(symbolp (symbol-value collection)))
nil)
;; When collection is a symbol, most of the time
;; it should be a symbol used as a minibuffer-history.
;; The value of this symbol in this case return a list
;; of string which maybe are converted later as symbol
;; in special cases.
;; we treat here commandp as a special case as it return t
;; also with a string unless its last arg is provided.
;; Also, the history collections generally collect their
;; elements as string, so intern them to call predicate.
((and (symbolp collection) (boundp collection) test)
(let ((predicate (lambda (elm)
(condition-case _err
(if (eq test 'commandp)
(funcall test (intern elm))
(funcall test elm))
(wrong-type-argument
(funcall test (intern elm)))))))
(all-completions input (symbol-value collection) predicate)))
((and (symbolp collection) (boundp collection))
(all-completions input (symbol-value collection)))
;; Normally file completion should not be handled here,
;; but special cases like `find-file-at-point' do it.
;; Handle here specially such cases.
((and (functionp collection) (not (string= input ""))
(or minibuffer-completing-file-name
(eq (completion-metadata-get
(completion-metadata input collection test)
'category)
'file)))
(cl-loop for f in (funcall collection input test t)
unless (member f '("./" "../"))
if (string-match helm--url-regexp input)
collect f
else
collect (concat (file-name-as-directory
(helm-basedir input))
f)))
((functionp collection)
(funcall collection input test t))
((and alistp (null test)) collection)
;; Next test ensure circular objects are removed
;; with `all-completions' (Bug#1530).
(t (all-completions input collection test)))))
(if sort-fn (sort cands sort-fn) cands))))
(cl-defun helm-cr--pattern-in-candidates-p (candidates &optional (pattern helm-pattern))
(or (assoc pattern candidates)
(assoc (concat " " pattern) candidates)
(assq (intern pattern) candidates)
(member pattern candidates)
(member (downcase pattern) candidates)
(member (upcase pattern) candidates)))
(defun helm-cr-default-transformer (candidates _source)
"Default filter candidate function for `helm-comp-read'."
;; Annotation and affixation are already handled in completion-in-region and
;; in helm-completing-read-default-2 when emacs style is in use.
;; For helm-completing-read-default-1 we handle them in an extra FCT; This
;; allows extracting annotation and affixation from metadata which is not
;; accessible from here.
(cl-loop for c in candidates
for cand = (let ((elm (if (stringp c)
(replace-regexp-in-string "\\s\\" "" c)
c)))
(cond ((and (stringp elm)
(string-match "\n" elm))
(cons (replace-regexp-in-string "\n" "->" elm) c))
(t c)))
collect cand))
(defun helm-cr-default (default cands)
(delq nil
(cond ((and (consp default) (string= helm-pattern ""))
(append (cl-loop for d in default
;; Don't convert
;; nil to "nil" (i.e the string)
;; it will be delq'ed on top.
for str = (if (null d) d (helm-stringify d))
when (member str cands)
do (setq cands (delete d cands))
when str collect str)
cands))
;; Some functions like debug-on-entry use (symbol-name sym)
;; without checking if sym is non nil, so the return value become
;; "nil".
((and (not (member default '("" "nil")))
(string= helm-pattern ""))
(cons default (delete (helm-stringify default)
cands)))
(t cands))))
;;;###autoload
(cl-defun helm-comp-read (prompt collection
&key
test
initial-input
default
preselect
(buffer "*Helm Completions*")
must-match
fuzzy
reverse-history
(requires-pattern 0)
(history nil shistory)
raw-history
input-history
(case-fold helm-comp-read-case-fold-search)
(persistent-action nil)
(persistent-help "DoNothing")
(mode-line helm-comp-read-mode-line)
help-message
(keymap helm-comp-read-map)
(name "Helm Completions")
header-name
candidates-in-buffer
(get-line #'buffer-substring)
diacritics
match-part
match-dynamic
exec-when-only-one
quit-when-no-cand
(volatile t)
sort
fc-transformer
hist-fc-transformer
(marked-candidates helm-comp-read-use-marked)
nomark
(alistp t)
(candidate-number-limit helm-candidate-number-limit)
multiline
allow-nest
coerce
raw-candidate
(group 'helm)
popup-info)
"Read a string in the minibuffer, with helm completion.
It is helm `completing-read' equivalent.
- PROMPT is the prompt name to use.
- COLLECTION can be a list, alist, vector, obarray or hash-table.
For alists and hash-tables their car are use as real value of
candidate unless ALISTP is non-nil.
It can be also a function that receives three arguments:
the values string, predicate and t. See `all-completions' for more details.
Keys description:
- TEST: A predicate called with one arg i.e candidate.
- INITIAL-INPUT: Same as input arg in `helm'.
- PRESELECT: See preselect arg of `helm'.
- DEFAULT: This option is used only for compatibility with regular
Emacs `completing-read' (Same as DEFAULT arg of `completing-read').
- BUFFER: Name of helm-buffer.
- MUST-MATCH: Candidate selected must be one of COLLECTION.
- FUZZY: Enable fuzzy matching.
- REVERSE-HISTORY: When non--nil display history source after current
source completion.
- REQUIRES-PATTERN: Same as helm attribute, default is 0.
- HISTORY: A symbol where each result will be saved.
If not specified as a symbol an error will popup.
When specified, all elements of HISTORY are displayed in
a special source before or after COLLECTION according to REVERSE-HISTORY.
The main difference with INPUT-HISTORY is that the result of the
completion is saved whereas in INPUT-HISTORY it is the minibuffer
contents which is saved when you exit.
Don't use the same symbol for INPUT-HISTORY and HISTORY.
NOTE: As mentionned above this has nothing to do with
`minibuffer-history-variable', therefore if you want to save this
history persistently, you will have to add this variable to the
relevant variable of your favorite tool for persistent emacs session
i.e. psession, desktop etc...
- RAW-HISTORY: When non-nil do not remove backslashs if some in
HISTORY candidates.
- INPUT-HISTORY: A symbol. The minibuffer input history will be
stored there, if nil or not provided, `minibuffer-history'
will be used instead. You can navigate in this history with
`M-p' and `M-n'.
Don't use the same symbol for INPUT-HISTORY and HISTORY.
- CASE-FOLD: Same as `helm-case-fold-search'.
- PERSISTENT-ACTION: A function called with one arg i.e candidate.
- PERSISTENT-HELP: A string to document PERSISTENT-ACTION.
- MODE-LINE: A string or list to display in mode line.
Default is `helm-comp-read-mode-line'.
- KEYMAP: A keymap to use in this `helm-comp-read'.
(the keymap will be shared with history source)
- NAME: The name related to this local source.
- HEADER-NAME: A function to alter NAME, see `helm'.
- EXEC-WHEN-ONLY-ONE: Bound `helm-execute-action-at-once-if-one'
to non--nil. (possibles values are t or nil).
- VOLATILE: Use volatile attribute.
- SORT: A predicate to give to `sort' e.g `string-lessp'
Use this only on small data as it is inefficient.
If you want to sort faster add a sort function to
FC-TRANSFORMER.
Note that FUZZY when enabled is already providing a sort function.
- FC-TRANSFORMER: A `filtered-candidate-transformer' function
or a list of functions.
- HIST-FC-TRANSFORMER: A `filtered-candidate-transformer'
function for the history source.
- MARKED-CANDIDATES: If non-nil return candidate or marked candidates as a list.
- NOMARK: When non--nil don't allow marking candidates.
- ALISTP:
When non-nil (default) pass the value of (DISPLAY . REAL)
candidate in COLLECTION to action when COLLECTION is an alist or a
hash-table, otherwise DISPLAY is always returned as result on exit,
which is the default when using `completing-read'.
See `helm-comp-read-get-candidates'.
- CANDIDATES-IN-BUFFER: when non--nil use a source build with
`helm-source-in-buffer' which is much faster.
Argument VOLATILE have no effect when CANDIDATES-IN-BUFFER is non--nil.
- GET-LINE: Specify the :get-line slot of `helm-source-in-buffer', has no effect
when CANDIDATES-IN-BUFFER is nil.
- MATCH-PART: Allow matching only one part of candidate.
See match-part documentation in `helm-source'.
- MATCH-DYNAMIC: See match-dynamic in `helm-source-sync'
It has no effect when used with CANDIDATES-IN-BUFFER.
- ALLOW-NEST: Allow nesting this `helm-comp-read' in a helm session.
See `helm'.
- MULTILINE: See multiline in `helm-source'.
- COERCE: See coerce in `helm-source'.
- RAW-CANDIDATE: Do not unquote the unknown candidate coming from helm-pattern
when non nil.
- GROUP: See group in `helm-source'.
Any prefix args passed during `helm-comp-read' invocation will be recorded
in `helm-current-prefix-arg', otherwise if prefix args were given before
`helm-comp-read' invocation, the value of `current-prefix-arg' will be used.
That means you can pass prefix args before or after calling a command
that use `helm-comp-read'. See `helm-M-x' for example."
;; Handle error with HISTORY:
;;
;; Should show helm with one source at first run and save result on
;; exit, should show the history source along candidates source on
;; next run as soon as `test-hist' value is feeded.
;; (setq test-hist nil)
;; (helm-comp-read "test: " '(a b c d e)
;; :history 'test-hist)
;;
;; Should run normally as long as `test-hist' is bound and nil. As
;; soon `test-hist' becomes non-nil throw an error.
;; (helm-comp-read "test: " '(a b c d e)
;; :history test-hist)
;;
;; Should run normally.
;; (completing-read "test: " '(a b c d e))
(cl-assert (if shistory
(or (null history)
(and history (symbolp history)))
t)
nil "Error: History should be specified as a symbol")
(when (get-buffer helm-action-buffer)
(kill-buffer helm-action-buffer))
;; The value of MUST-MATCH is given to
;; `helm--set-minibuffer-completion-confirm' which compute it and propagate it
;; to `minibuffer-completion-confirm' which is then used by
;; `helm-confirm-and-exit-minibuffer'.
(unless (or (memq must-match '(confirm confirm-after-completion t nil))
(functionp must-match))
;; Fix completing-read's using something else than (confirm
;; confirm-after-completion t nil) or a function e.g. 1 or
;; whatever (bug #2527).
(setq must-match t))
(let ((action-fn `(("Sole action (Identity)"
. (lambda (candidate)
(if ,marked-candidates
(helm-marked-candidates)
(identity candidate)))))))
(let* ((minibuffer-completion-predicate test)
(minibuffer-completion-table
(or minibuffer-completion-table collection))
(helm-read-file-name-mode-line-string
(replace-regexp-in-string "helm-maybe-exit-minibuffer"
"helm-confirm-and-exit-minibuffer"
helm-read-file-name-mode-line-string))
(get-candidates
(lambda ()
(let ((cands (helm-comp-read-get-candidates
;; If `helm-pattern' is passed as INPUT
;; and :alistp is nil INPUT is passed to
;; `all-completions' which defeat helm
;; matching functions (multi match, fuzzy
;; etc...) Bug#2134.
collection test sort alistp
(if (and match-dynamic (null candidates-in-buffer))
helm-pattern ""))))
(helm-cr-default default cands))))
(history-get-candidates
(lambda ()
(let ((cands (helm-comp-read-get-candidates
history test nil alistp)))
(when cands
(delete "" (helm-cr-default default cands))))))
(src-hist (helm-build-sync-source (format "%s History" name)
:candidates history-get-candidates
:fuzzy-match fuzzy
:multiline multiline
:match-part match-part
:filtered-candidate-transformer
(append `((lambda (candidates _source)
(if ,raw-history
candidates
(cl-loop for i in candidates
;; Input is added to history in completing-read's
;; and may be regexp-quoted, so unquote it
;; but check if cand is a string (it may be at this stage
;; a symbol or nil) Bug#1553.
when (stringp i)
collect (replace-regexp-in-string "\\s\\" "" i)))))
(and hist-fc-transformer (helm-mklist hist-fc-transformer)))
:persistent-action persistent-action
:persistent-help persistent-help
:keymap keymap
:must-match must-match
:group group
:popup-info popup-info
:coerce coerce
:mode-line mode-line
:help-message help-message
:action action-fn))
(dummy-src (helm-build-dummy-source "Unknown candidate"
:must-match must-match
:keymap keymap
:filtered-candidate-transformer
(lambda (_candidates _source)
(let ((pat (if raw-candidate
helm-pattern
(replace-regexp-in-string "\\s\\" "" helm-pattern))))
(unless (string= pat "")
(list (cons (helm-aand (propertize "[?]" 'face 'helm-ff-prefix)
(propertize " " 'display it 'unknown t)
(concat it pat))
pat)))))
:action action-fn))
(src (helm-build-sync-source name
:candidates get-candidates
:popup-info popup-info
:match-part match-part
:multiline multiline
:header-name header-name
:filtered-candidate-transformer
(let ((transformers (helm-mklist fc-transformer)))
(append transformers
(unless (member 'helm-cr-default-transformer transformers)
'(helm-cr-default-transformer))))
:requires-pattern requires-pattern
:persistent-action persistent-action
:persistent-help persistent-help
:fuzzy-match fuzzy
:diacritics diacritics
:keymap keymap
:must-match must-match
:group group
:coerce coerce
:mode-line mode-line
:match-dynamic match-dynamic
:help-message help-message
:action action-fn
:volatile volatile))
(src-1 (helm-build-in-buffer-source name
:data get-candidates
:match-part match-part
:get-line get-line
:multiline multiline
:header-name header-name
:filtered-candidate-transformer
(let ((transformers (helm-mklist fc-transformer)))
(append transformers
(unless (member 'helm-cr-default-transformer transformers)
'(helm-cr-default-transformer))))
:popup-info popup-info
:requires-pattern requires-pattern
:persistent-action persistent-action
:fuzzy-match fuzzy
:diacritics diacritics
:keymap keymap
:must-match must-match
:group group
:coerce coerce
:persistent-help persistent-help
:mode-line mode-line
:help-message help-message
:action action-fn))
(src-list (list src-hist
(if candidates-in-buffer
src-1 src)))
(helm-execute-action-at-once-if-one exec-when-only-one)
(helm-quit-if-no-candidate quit-when-no-cand)
result)
(when nomark
(setq src-list (cl-loop for src in src-list
collect (cons '(nomark) src))))
(when reverse-history (setq src-list (nreverse src-list)))
(unless (eq must-match t)
(setq src-list (append src-list (list dummy-src))))
(when raw-candidate
(cl-loop for src in src-list
do (helm-set-attr 'raw-candidate t src)))
(setq result (helm
:sources src-list
:input initial-input
:default default
:preselect preselect
:prompt prompt
:resume 'noresume
:keymap keymap ;; Needed with empty collection.
:allow-nest allow-nest
:candidate-number-limit candidate-number-limit
:case-fold-search case-fold
:history (and (symbolp input-history) input-history)
:buffer buffer))
;; If `history' is a symbol save it, except when it is t.
(when (and result history (symbolp history) (not (eq history t)))
(set history
;; RESULT may be a a string or a list of strings bug #2461.
(delete-dups (append (mapcar #'substring-no-properties (helm-mklist result))
(symbol-value history)))))
(or result (helm-mode--keyboard-quit)))))
;; Generic completing-read
;;
;; Support also function as collection.
;; e.g M-x man is supported.
;; Support hash-table and vectors as collection.
;; NOTE:
;; Some crap emacs functions may not be supported
;; like ffap-alternate-file (bad use of completing-read)
;; and maybe others.
;; Provide a mode `helm-mode' which turn on
;; helm in all `completing-read' and `read-file-name' in Emacs.
;;
(defvar helm-completion-mode-string " Helm")
(defvar helm-completion-mode-quit-message
"Helm completion disabled")
(defvar helm-completion-mode-start-message
"Helm completion enabled")
;;; Specialized handlers
;;