forked from emacs-helm/helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-source.el
1008 lines (866 loc) · 36.5 KB
/
helm-source.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-source.el --- Helm source creation. -*- lexical-binding: t -*-
;; Copyright (C) 2015 Thierry Volpiatto <[email protected]>
;; Author: Thierry Volpiatto <[email protected]>
;; URL: http://github.com/emacs-helm/helm
;; 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/>.
;;; Commentary:
;; Interface to create helm sources easily.
;; Actually the eieo objects are transformed in alist for compatibility.
;; In the future this package should allow creating source as eieo objects
;; without conversion to alist, teaching helm to read such a structure.
;; The compatibility with alists would be kept.
;;; Code:
(require 'cl-lib)
(require 'eieio)
(require 'helm-lib)
(defvar helm-fuzzy-sort-fn)
(defvar helm-fuzzy-match-fn)
(defvar helm-fuzzy-search-fn)
(declare-function helm-init-candidates-in-buffer "helm.el")
(declare-function helm-interpret-value "helm.el")
(declare-function helm-fuzzy-highlight-matches "helm.el")
(defgeneric helm--setup-source (source)
"Prepare slots and handle slot errors before creating a helm source.")
(defgeneric helm-setup-user-source (source)
"Allow users modifying slots in SOURCE just before creation.")
;;; Classes for sources
;;
;;
(defclass helm-source ()
((name
:initarg :name
:initform nil
:custom string
:documentation
" The name of the source.
A string which is also the heading which appears
above the list of matches from the source. Must be unique.")
(header-name
:initarg :header-name
:initform nil
:custom function
:documentation
" A function returning the display string of the header.
Its argument is the name of the source. This attribute is useful to
add an additional information with the source name.
It doesn't modify the name of the source.")
(init
:initarg :init
:initform nil
:custom function
:documentation
" Function called with no parameters when helm is started.
It is useful for collecting current state information which can be
used to create the list of candidates later.
Initialization of `candidates-in-buffer' is done here
with `helm-init-candidates-in-buffer'.")
(candidates
:initarg :candidates
:initform nil
:custom (choice function list)
:documentation
" Specifies how to retrieve candidates from the source.
It can either be a variable name, a function called with no parameters
or the actual list of candidates.
The list must be a list whose members are strings, symbols
or (DISPLAY . REAL) pairs.
In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
in the Helm buffer, but the REAL one is used as action
argument when the candidate is selected. This allows a more
readable presentation for candidates which would otherwise be,
for example, too long or have a common part shared with other
candidates which can be safely replaced with an abbreviated
string for display purposes.
Note that if the (DISPLAY . REAL) form is used then pattern
matching is done on the displayed string, not on the real
value.")
(update
:initarg :update
:initform nil
:custom function
:documentation
" Function called with no parameters at before \"init\" function
when `helm-force-update' is called.")
(cleanup
:initarg :cleanup
:initform nil
:custom function
:documentation
" Function called with no parameters when *helm* buffer is
closed. It is useful for killing unneeded candidates buffer.
Note that the function is executed BEFORE performing action.")
(delayed
:initarg :delayed
:initform nil
:custom (choice null integer)
:documentation
" Candidates from the source are shown only if the user stops
typing and is idle for `helm-idle-delay' seconds.
If a value is given to delayed attr, this value is used instead only
if it is > to `helm-idle-delay'.")
(keymap
:initarg :keymap
:initform nil
:custom sexp
:documentation
" Specific keymap for this source.
It is useful to have a keymap per source when using more than
one source. Otherwise, a keymap can be set per command with
`helm' argument KEYMAP. NOTE: when a source have `helm-map' as
keymap attr, the global value of `helm-map' will override the
actual local one.")
(action
:initarg :action
:initform 'identity
:custom (alist :key-type string
:value-type function)
:documentation
" An alist of (DISPLAY . FUNCTION) pairs, a variable name or a function.
FUNCTION is called with one parameter: the selected candidate.
An action other than the default can be chosen from this list
of actions for the currently selected candidate (by default
with TAB). The DISPLAY string is shown in the completions
buffer and the FUNCTION is invoked when an action is
selected. The first action of the list is the default.
You should use `helm-make-actions' to build this alist easily.")
(persistent-action
:initarg :persistent-action
:initform nil
:custom function
:documentation
" Can be a either a Function called with one parameter (the
selected candidate) or a cons cell where first element is this
same function and second element a symbol (e.g never-split)
that inform `helm-execute-persistent-action'to not split his
window to execute this persistent action.")
(persistent-help
:initarg :persistent-help
:initform nil
:custom string
:documentation
" A string to explain persistent-action of this source. It also
accepts a function or a variable name.
It will be displayed in `header-line'.
Have no effect when `helm-echo-input-in-header-line' is non--nil.")
(help-message
:initarg :help-message
:initform nil
:custom (choice string function)
:documentation
" Help message for this source.
If not present, `helm-help-message' value will be used.")
(multiline
:initarg :multiline
:initform nil
:custom boolean
:documentation
" Enable to selection multiline candidates.")
(requires-pattern
:initarg :requires-pattern
:initform nil
:custom integer
:documentation
" If present matches from the source are shown only if the
pattern is not empty. Optionally, it can have an integer
parameter specifying the required length of input which is
useful in case of sources with lots of candidates.")
(candidate-transformer
:initarg :candidate-transformer
:initform nil
:custom (choice function list)
:documentation
" It's a function or a list of functions called with one argument
when the completion list from the source is built. The argument
is the list of candidates retrieved from the source. The
function should return a transformed list of candidates which
will be used for the actual completion. If it is a list of
functions, it calls each function sequentially.
This can be used to transform or remove items from the list of
candidates.
Note that `candidates' is run already, so the given transformer
function should also be able to handle candidates with (DISPLAY
. REAL) format.")
(filtered-candidate-transformer
:initarg :filtered-candidate-transformer
:initform nil
:custom (choice function list)
:documentation
" It has the same format as `candidate-transformer', except the
function is called with two parameters: the candidate list and
the source.
This transformer is run on the candidate list which is already
filtered by the current pattern. While `candidate-transformer'
is run only once, it is run every time the input pattern is
changed.
It can be used to transform the candidate list dynamically, for
example, based on the current pattern.
In some cases it may also be more efficent to perform candidate
transformation here, instead of with `candidate-transformer'
even if this transformation is done every time the pattern is
changed. For example, if a candidate set is very large then
`candidate-transformer' transforms every candidate while only
some of them will actually be displayed due to the limit
imposed by `helm-candidate-number-limit'.
Note that `candidates' and `candidate-transformer' is run
already, so the given transformer function should also be able
to handle candidates with (DISPLAY . REAL) format.")
(filter-one-by-one
:initarg :filter-one-by-one
:initform nil
:custom (choice function list)
:documentation
" A transformer function that treat candidates one by one.
It is called with one arg the candidate.
It is faster than `filtered-candidate-transformer' or
`candidates-transformer', but should be used only in sources
that recompute constantly their candidates, e.g `helm-source-find-files'.
Filtering happen early and candidates are treated
one by one instead of re-looping on the whole list.
If used with `filtered-candidate-transformer' or `candidates-transformer'
these functions should treat the candidates transformed by the
`filter-one-by-one' function in consequence.")
(display-to-real
:initarg :display-to-real
:initform nil
:custom function
:documentation
" Function called with one parameter; the selected candidate.
The function transforms the selected candidate, and the result
is passed to the action function. The display-to-real
attribute provides another way to pass to action other string than
the one shown in Helm buffer.
Traditionally, it is possible to make candidates,
candidate-transformer or filtered-candidate-transformer
function return a list with (DISPLAY . REAL) pairs. But if REAL
can be generated from DISPLAY, display-to-real is more
convenient and faster.
NOTE: This is deprecated and you have better time using `filter-one-by-one'.")
(real-to-display
:initarg :real-to-display
:initform nil
:custom function
:documentation
" Function called with one parameter; the selected candidate.
The real value of candidates will be shown in display.
See `display-to-real'.")
(action-transformer
:initarg :action-transformer
:initform nil
:custom (choice function list)
:documentation
" It's a function or a list of functions called with two
arguments when the action list from the source is
assembled. The first argument is the list of actions, the
second is the current selection. If it is a list of functions,
it calls each function sequentially.
The function should return a transformed action list.
This can be used to customize the list of actions based on the
currently selected candidate.")
(pattern-transformer
:initarg :pattern-transformer
:initform nil
:custom (choice function list)
:documentation
" It's a function or a list of functions called with one argument
before computing matches. Its argument is `helm-pattern'.
Functions should return transformed `helm-pattern'.
It is useful to change interpretation of `helm-pattern'.")
(candidate-number-limit
:initarg :candidate-number-limit
:initform nil
:custom integer
:documentation
" Override `helm-candidate-number-limit' only for this source.")
(volatile
:initarg :volatile
:initform nil
:custom boolean
:documentation
" Indicates the source assembles the candidate list dynamically,
so it shouldn't be cached within a single Helm
invocation. It is only applicable to synchronous sources,
because asynchronous sources are not cached.")
(match
:initarg :match
:initform nil
:custom (choice function list)
:documentation
" List of functions called with one parameter: a candidate. The
function should return non-nil if the candidate matches the
current pattern (see variable `helm-pattern').
When using `candidates-in-buffer' its default value is `identity' and
don't have to be changed, use the `search' slot instead.
This attribute allows the source to override the default
pattern matching based on `string-match'. It can be used, for
example, to implement a source for file names and do the
pattern matching on the basename of files, since it's more
likely one is typing part of the basename when searching for a
file, instead of some string anywhere else in its path.
If the list contains more than one function then the list of
matching candidates from the source is constructed by appending
the results after invoking the first function on all the
potential candidates, then the next function, and so on. The
matching candidates supplied by the first function appear first
in the list of results and then results from the other
functions, respectively.
This attribute has no effect for asynchronous sources (see
attribute `candidates'), since they perform pattern matching
themselves.
Note that FUZZY-MATCH slot will overhide value of this slot.")
(fuzzy-match
:initarg :fuzzy-match
:initform nil
:custom boolean
:documentation
" Enable fuzzy matching in this source.
This will overwrite settings in MATCH slot, and for
sources built with child class `helm-source-in-buffer' the SEARCH slot.
This is an easy way of enabling fuzzy matching, but you can use the MATCH
or SEARCH slots yourself if you want something more elaborated, mixing
different type of match (See `helm-source-buffers' class for example).")
(nomark
:initarg :nomark
:initform nil
:custom boolean
:documentation
" Don't allow marking candidates when this attribute is present.")
(nohighlight
:initarg :nohighlight
:initform nil
:custom boolean
:documentation
" Disable highlighting matches in this source.
This will disable generic highlighting of matches,
but some specialized highlighting can be done from elsewhere,
i.e from `filtered-candidate-transformer' or `filter-one-by-one' slots.
So use this to either disable completely highlighting in your source,
or to disable highlighting and use a specialized highlighting matches
function for this source.
Remember that this function should run AFTER all filter functions if those
filter functions are modifying face properties, though it is possible to
avoid this by using new `add-face-text-property' in your filter functions.")
(allow-dups
:initarg :allow-dups
:initform nil
:custom boolean
:documentation
" Allow helm collecting duplicates candidates.")
(history
:initarg :history
:initform nil
:custom symbol
:documentation
" Allow passing history variable to helm from source.
It should be a quoted symbol.")
(coerce
:initarg :coerce
:initform nil
:custom function
:documentation
" It's a function called with one argument: the selected candidate.
This function is intended for type convertion. In normal case,
the selected candidate (string) is passed to action
function. If coerce function is specified, it is called just
before action function.
Example: converting string to symbol
(coerce . intern)")
(mode-line
:initarg :mode-line
:initform nil
:custom (choice string sexp)
:documentation
" Source local `helm-mode-line-string' (included in
`mode-line-format'). It accepts also variable/function name.")
(header-line
:initarg :header-line
:initform nil
:custom (choice string function)
:documentation
" Source local `header-line-format'.
Have no effect when `helm-echo-input-in-header-line' is non--nil.
It accepts also variable/function name.")
(resume
:initarg :resume
:initform nil
:custom function
:documentation
" Function called with no parameters at end of initialization
when `helm-resume' is started.
If this function try to do something against `helm-buffer', \(e.g updating,
searching etc...\) probably you should run it in a timer to ensure
`helm-buffer' is ready.")
(follow
:initarg :follow
:initform nil
:custom integer
:documentation
" Enable `helm-follow-mode' for this source only.
You must give it a value of 1 or -1, though giving a -1 value
is surely not what you want, e.g: (follow . 1)
See `helm-follow-mode' for more infos.")
(follow-delay
:initarg :follow-delay
:initform nil
:custom integer
:documentation
" `helm-follow-mode' will execute persistent-action after this delay.
Otherwise value of `helm-follow-input-idle-delay' is used if non--nil,
If none of these are found fallback to `helm-input-idle-delay'.")
(dont-plug
:initarg :dont-plug
:initform '(helm-compile-source--persistent-help)
:custom list
:documentation
" A list of compile functions plugin to ignore.")
(matchplugin
:initarg :matchplugin
:initform t
:custom boolean)
(match-part
:initarg :match-part
:initform nil
:custom function
:documentation
" Allow matching only one part of candidate.
If source contain match-part attribute, match is computed only
on part of candidate returned by the call of function provided
by this attribute. The function should have one arg, candidate,
and return only a specific part of candidate.")
(before-init-hook
:initarg :before-init-hook
:initform nil
:custom symbol
:documentation
" A local hook that run at beginning of initilization of this source.
i.e Before the creation of `helm-buffer'.")
(after-init-hook
:initarg :after-init-hook
:initform nil
:custom symbol
:documentation
" A local hook that run at end of initilization of this source.
i.e After the creation of `helm-buffer'."))
"Main interface to define helm sources."
:abstract t)
(defclass helm-source-sync (helm-source)
((candidates
:initform '("ERROR: You must specify the `candidates' slot, either with a list or a function"))
(dont-plug
:initform '(helm-compile-source--multi-match
helm-compile-source--persistent-help
;; Ensure this will not be plugged
;; if user have somewhere old helm-migemo.el.
helm-compile-source--migemo))
(migemo
:initarg :migemo
:initform nil
:custom boolean
:documentation
" Enable migemo.
When multimatch is disabled, you can give the symbol 'nomultimatch as value
to force not using generic migemo matching function.
In this case you have to provide your own migemo matching funtion
that kick in when `helm-migemo-mode' is enabled.
Otherwise it will be available for this source once `helm-migemo-mode'
is enabled when non-nil.")
(match-strict
:initarg :match-strict
:initform nil
:custom function
:documentation
" When specifying a match function within a source and
helm-multi-match is enabled, the result of all matching
functions will be concatened, which in some cases is not what
is wanted. When using `match-strict' only this or these
functions will be used. You can specify those functions as a
list of functions or a single symbol function.
NOTE: This have the same effect as using :MATCHPLUGIN nil."))
"Use this class to make helm sources using a list of candidates.
This list should be given as a normal list, a variable handling a list
or a function returning a list.
Matching is done basically with `string-match' against each candidate.")
(defclass helm-source-async (helm-source)
((candidates-process
:initarg :candidates-process
:initform nil
:custom function
:documentation
" This attribute is used to define a process as candidate.
The value must be a process.
NOTE:
When building the source at runtime you can give directly a process
as value, otherwise wrap the process call into a function.
The process buffer should be nil, otherwise, if you use
`helm-buffer' give to the process a sentinel.")
(matchplugin :initform nil)
(dont-plug :initform '(helm-compile-source--multi-match
helm-compile-source--persistent-help)))
"Use this class to define a helm source calling an external process.
The :candidates slot is not allowed even if described because this class
inherit from `helm-source'.")
(defclass helm-source-in-buffer (helm-source)
((init
:initform 'helm-default-init-source-in-buffer-function)
(data
:initarg :data
:initform nil
:custom (choice list string)
:documentation
" A string or a list that will be used to feed the `helm-candidates-buffer'.
This data will be passed in a function added to the init slot and
the buffer will be build with `helm-init-candidates-in-buffer'.
This is an easy and fast method to build a `candidates-in-buffer' source.")
(dont-plug
:initform '(helm-compile-source--candidates-in-buffer
helm-compile-source--multi-match
helm-compile-source--persistent-help
helm-compile-source--migemo))
(migemo
:initarg :migemo
:initform nil
:custom boolean
:documentation
" Enable migemo.
When multimatch is disabled, you can give the symbol 'nomultimatch as value
to force not using generic migemo matching function.
In this case you have to provide your own migemo matching funtion
that kick in when `helm-migemo-mode' is enabled.
Otherwise it will be available for this source once `helm-migemo-mode'
is enabled when non-nil.")
(candidates
:initform 'helm-candidates-in-buffer)
(volatile
:initform t)
(match
:initform '(identity))
(get-line
:initarg :get-line
:initform 'buffer-substring-no-properties
:custom function
:documentation
" A function like `buffer-substring-no-properties' or `buffer-substring'.
This function converts point of line-beginning and point of line-end,
which represents a candidate computed by `helm-candidates-in-buffer'.
By default, `helm-candidates-in-buffer' uses
`buffer-substring-no-properties'.")
(search
:initarg :search
:initform '(helm-candidates-in-buffer-search-default-fn)
:custom (choice function list)
:documentation
" List of functions like `re-search-forward' or `search-forward'.
Buffer search function used by `helm-candidates-in-buffer'.
By default, `helm-candidates-in-buffer' uses `re-search-forward'.
The function should take one arg PATTERN.
If your search function needs to handle negation like matchplugin,
this function should returns in such case a cons cell of two integers defining
the beg and end positions to match in the line previously matched by
`re-search-forward' or similar, and move point to next line
(See how the `helm-mm-3-search-base' and `helm-fuzzy-search' functions are working).
NOTE: FUZZY-MATCH slot will overhide value of this slot.")
(search-strict
:initarg :search-strict
:initform nil
:custom function
:documentation
" When specifying a search function within a source and
helm-multi-match is enabled, the result of all searching
functions will be concatened, which in some cases is not what
is wanted. When using `search-strict' only this or these
functions will be used. You can specify those functions as a
list of functions or a single symbol function.
NOTE: This have the same effect as using a nil value for
:MATCHPLUGIN slot."))
"Use this source to make helm sources storing candidates inside a buffer.
Contrarily to `helm-source-sync' candidates are matched using a function
like `re-search-forward', see below documentation of :search slot.")
(defclass helm-source-dummy (helm-source)
((candidates
:initform '("dummy"))
(filtered-candidate-transformer
:initform 'helm-dummy-candidate)
(matchplugin
:initform nil)
(accept-empty
:initarg :accept-empty
:initform t
:custom boolean
:documentation
" Allow exiting with an empty string.
You should keep the default value.")
(match
:initform 'identity)
(volatile
:initform t)))
(defclass helm-source-in-file (helm-source-in-buffer)
((init :initform (lambda ()
(let ((file (helm-attr 'candidates-file)))
(with-current-buffer (helm-candidate-buffer 'global)
(insert-file-contents file)))))
(candidates-file
:initarg :candidates-file
:initform nil
:custom string
:documentation "A filename."))
"The contents of the file will be used as candidates in buffer.")
;;; Error functions
;;
;;
(defun helm-default-init-source-in-buffer-function ()
(helm-init-candidates-in-buffer 'global
'("ERROR: No buffer handling your data, use either the `init' slot or the `data' slot.")))
;;; Internal Builder functions.
;;
;;
(defun helm--create-source (object)
"[INTERNAL] Build a helm source from OBJECT.
Where OBJECT is an instance of an eieio class."
(cl-loop for s in (object-slots object)
for slot-val = (slot-value object s)
when slot-val
collect (cons s (unless (eq t slot-val) slot-val))))
(defun helm-make-source (name class &rest args)
"Build a `helm' source named NAME with ARGS for CLASS.
Argument NAME is a string which define the source name, so no need to use
the keyword :name in your source, NAME will be used instead.
Argument CLASS is an eieio class object.
Arguments ARGS are keyword value pairs as defined in CLASS."
(declare (indent 2))
(let ((source (apply #'make-instance class name args)))
(set-slot-value source 'name name)
(helm--setup-source source)
(helm-setup-user-source source)
(helm--create-source source)))
(defun helm-make-type (class &rest args)
(let ((source (apply #'make-instance class args)))
(set-slot-value source 'name nil)
(helm--setup-source source)
(helm--create-source source)))
(defvar helm-mm-default-search-functions)
(defvar helm-mm-default-match-functions)
(defun helm-source-mm-get-search-or-match-fns (source method)
(let ((defmatch (helm-aif (slot-value source 'match)
(helm-mklist it)))
(defmatch-strict (helm-aif (and (eq method 'match)
(slot-value source 'match-strict))
(helm-mklist it)))
(defsearch (helm-aif (and (eq method 'search)
(slot-value source 'search))
(helm-mklist it)))
(defsearch-strict (helm-aif (and (eq method 'search-strict)
(slot-value source 'search-strict))
(helm-mklist it)))
(migemo (slot-value source 'migemo)))
(cl-case method
(match (cond (defmatch-strict)
(migemo
(append helm-mm-default-match-functions
defmatch '(helm-mm-3-migemo-match)))
(defmatch
(append helm-mm-default-match-functions defmatch))
(t helm-mm-default-match-functions)))
(search (cond (defsearch-strict)
(migemo
(append helm-mm-default-search-functions
defsearch '(helm-mm-3-migemo-search)))
(defsearch
(append helm-mm-default-search-functions defsearch))
(t helm-mm-default-search-functions))))))
;;; Modifiers
;;
(cl-defun helm-source-add-action-to-source-if (name fn source predicate
&optional (index 4))
"Same as `helm-add-action-to-source-if' but for SOURCE defined as eieio object.
You can use this inside a `helm--setup-source' method for a SOURCE defined as
an eieio class."
(let* ((actions (slot-value source 'action))
(action-transformers (slot-value source 'action-transformer))
(new-action (list (cons name fn)))
(transformer `(lambda (actions candidate)
(cond ((funcall (quote ,predicate) candidate)
(helm-append-at-nth
actions (quote ,new-action) ,index))
(t actions)))))
(if (functionp actions)
(set-slot-value source 'action (list (cons "Default action" actions)))
(set-slot-value source 'action (helm-interpret-value actions source)))
(when (or (symbolp action-transformers) (functionp action-transformers))
(setq action-transformers (list action-transformers)))
(set-slot-value
source
'action-transformer
(delq nil (append (list transformer) action-transformers)))))
;;; Methods to build sources.
;;
;;
(defun helm-source--persistent-help-string (string source)
(substitute-command-keys
(concat "\\<helm-map>\\[helm-execute-persistent-action]: "
(or (format "%s (keeping session)" string)
(slot-value source 'header-line)))))
(defun helm-source--header-line (source)
(substitute-command-keys
(concat "\\<helm-map>\\[helm-execute-persistent-action]: "
(helm-aif (or (slot-value source 'persistent-action)
(slot-value source 'action))
(cond ((and (symbolp it)
(functionp it)
(eq it 'identity))
"Do Nothing")
((and (symbolp it)
(boundp it)
(listp (symbol-value it))
(stringp (caar (symbol-value it))))
(caar (symbol-value it)))
((or (symbolp it) (functionp it))
(helm-symbol-name it))
((listp it)
(let ((action (car it)))
;; It comes from :action ("foo" . function).
(if (stringp (car action))
(car action)
;; It comes from :persistent-action
;; (function . 'nosplit) Fix Issue #788.
(if (or (symbolp action)
(functionp action))
(helm-symbol-name action)))))
(t ""))
"")
" (keeping session)")))
(defmethod helm--setup-source :primary ((_source helm-source)))
(defmethod helm--setup-source :before ((source helm-source))
(helm-aif (slot-value source 'keymap)
(and (symbolp it) (set-slot-value source 'keymap (symbol-value it))))
(helm-aif (slot-value source 'persistent-help)
(set-slot-value source 'header-line
(helm-source--persistent-help-string it source))
(set-slot-value source 'header-line (helm-source--header-line source)))
(helm-aif (slot-value source 'candidate-number-limit)
(and (symbolp it) (set-slot-value
source 'candidate-number-limit (symbol-value it))))
(when (and (slot-value source 'fuzzy-match) helm-fuzzy-sort-fn)
(set-slot-value source 'filtered-candidate-transformer
(helm-aif (slot-value source 'filtered-candidate-transformer)
(append (helm-mklist it)
(list helm-fuzzy-sort-fn))
(list helm-fuzzy-sort-fn))))
(unless (slot-value source 'nohighlight)
(set-slot-value source 'filtered-candidate-transformer
(helm-aif (slot-value source 'filtered-candidate-transformer)
(append (helm-mklist it)
(list #'helm-fuzzy-highlight-matches))
(list #'helm-fuzzy-highlight-matches)))))
(defmethod helm-setup-user-source ((_source helm-source)))
(defmethod helm--setup-source ((source helm-source-sync))
(when (slot-value source 'fuzzy-match)
(helm-aif (slot-value source 'match)
(set-slot-value source 'match (append (helm-mklist it)
(list helm-fuzzy-match-fn)))
(set-slot-value source 'match helm-fuzzy-match-fn)))
(when (slot-value source 'matchplugin)
(set-slot-value source 'match
(helm-source-mm-get-search-or-match-fns source 'match)))
(helm-aif (and (null (slot-value source 'matchplugin))
(slot-value source 'migemo))
(unless (eq it 'nomultimatch) ; Use own migemo fn.
(set-slot-value source 'match
(append (helm-mklist (slot-value source 'match))
'(helm-mm-3-migemo-match))))))
(defmethod helm--setup-source ((source helm-source-in-buffer))
(let ((cur-init (slot-value source 'init)))
(helm-aif (slot-value source 'data)
(set-slot-value
source
'init (delq
nil
(list
(and (null (eq 'helm-default-init-source-in-buffer-function
cur-init))
cur-init)
(lambda ()
(helm-init-candidates-in-buffer
'global
(if (functionp it) (funcall it) it))))))))
(when (slot-value source 'fuzzy-match)
(helm-aif (slot-value source 'search)
(set-slot-value source 'search (append (helm-mklist it)
(list helm-fuzzy-search-fn)))
(set-slot-value source 'search (list helm-fuzzy-search-fn))))
(when (slot-value source 'matchplugin)
(set-slot-value
source 'search (helm-source-mm-get-search-or-match-fns source 'search)))
(helm-aif (and (null (slot-value source 'matchplugin))
(slot-value source 'migemo))
(unless (eq it 'nomultimatch)
(set-slot-value source 'search
(append (helm-mklist (slot-value source 'search))
'(helm-mm-3-migemo-search)))))
(let ((mtc (slot-value source 'match)))
(cl-assert (or (equal '(identity) mtc)
(eq 'identity mtc))
nil "Invalid slot value for `match'")
(cl-assert (eq (slot-value source 'volatile) t)
nil "Invalid slot value for `volatile'")))
(defmethod helm--setup-source ((source helm-source-async))
(cl-assert (null (slot-value source 'candidates))
nil "Incorrect use of `candidates' use `candidates-process' instead")
(cl-assert (null (slot-value source 'matchplugin))
nil "`matchplugin' not allowed in async sources."))
(defmethod helm--setup-source ((source helm-source-dummy))
(let ((mtc (slot-value source 'match)))
(cl-assert (or (equal '(identity) mtc)
(eq 'identity mtc))
nil "Invalid slot value for `match'")
(cl-assert (eq (slot-value source 'volatile) t)
nil "Invalid slot value for `volatile'")
(cl-assert (equal (slot-value source 'candidates) '("dummy"))
nil "Invalid slot value for `candidates'")
(cl-assert (eq (slot-value source 'accept-empty) t)
nil "Invalid slot value for `accept-empty'")))
;;; User functions
;;
;; Sources
(defmacro helm-build-sync-source (name &rest args)
"Build a synchronous helm source with name NAME.
Args ARGS are keywords provided by `helm-source-sync'."
(declare (indent 1))
`(helm-make-source ,name 'helm-source-sync ,@args))
(defmacro helm-build-async-source (name &rest args)
"Build a asynchronous helm source with name NAME.
Args ARGS are keywords provided by `helm-source-async'."
(declare (indent 1))
`(helm-make-source ,name 'helm-source-async ,@args))
(defmacro helm-build-in-buffer-source (name &rest args)
"Build a helm source with name NAME using `candidates-in-buffer' method.
Args ARGS are keywords provided by `helm-source-in-buffer'."
(declare (indent 1))
`(helm-make-source ,name 'helm-source-in-buffer ,@args))
(defmacro helm-build-dummy-source (name &rest args)
"Build a helm source with name NAME using `dummy' method.
Args ARGS are keywords provided by `helm-source-dummy'."
(declare (indent 1))
`(helm-make-source ,name 'helm-source-dummy ,@args))
(defmacro helm-build-in-file-source (name file &rest args)
"Build a helm source with NAME name using `candidates-in-files' method.
Arg FILE is a filename, the contents of this file will be
used as candidates in buffer.
Args ARGS are keywords provided by `helm-source-in-file'."
(declare (indent 1))
`(helm-make-source ,name 'helm-source-in-file
:candidates-file ,file ,@args))
(provide 'helm-source)