summaryrefslogtreecommitdiff
path: root/helm-source.el
blob: deceb26fe89f4a528855378f2d86f1f75565b77d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
;;; helm-source.el --- Helm source creation. -*- lexical-binding: t -*-

;; Copyright (C) 2015 ~ 2019  Thierry Volpiatto <thierry.volpiatto@gmail.com>

;; Author: Thierry Volpiatto <thierry.volpiatto@gmail.com>
;; 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")

;;; Advice Emacs fn
;;  Make Classes's docstrings more readable by removing al the
;;  unnecessary crap.

(defun helm-source--cl--print-table (_header rows)
  "Advice for `cl--print-table' to make readable class slots docstrings."
  (let ((format "%s\n\n  Initform=%s\n\n%s"))
    (dolist (row rows)
      (setcar row (propertize (car row) 'face 'bold))
      (setcdr row (nthcdr 1 (cdr row)))
      (insert "\n* " (apply #'format format row) "\n"))))


(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.

  Do NOT use this for asynchronous sources, use `candidates-process'
  instead.

  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.

  This function, generally should not compute candidates according to
  `helm-pattern' which defeat all the Helm's matching mechanism
  i.e. multiple pattern matching and/or fuzzy matching.
  If you want to do so, use :match-dynamic slot to be sure matching
  occur only in :candidates function and there is no conflict with
  other match functions.")

   (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.")

   (keymap
    :initarg :keymap
    :initform helm-map
    :custom sexp
    :documentation
    "  Specific keymap for this source.
  default value is `helm-map'.")

   (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.
  Example:

    (defun foo-persistent-action (candidate)
       (do-something candidate))

    :persistent-action '(foo-persistent-action . never-split) ; Don't split
  or
    :persistent-action 'foo-persistent-action ; Split

  When specifying :persistent-action by slot directly, foo-persistent-action
  will be executed without quitting helm when hitting `C-j'.

  Note that other persistent actions can be defined using other
  bindings than `C-j' by simply defining an interactive function bound
  to a key in the keymap source.
  The function should create a new attribute in source before calling
  `helm-execute-persistent-action' on this attribute.
  Example:

     (defun helm-ff-persistent-delete ()
       \"Delete current candidate without quitting.\"
       (interactive)
       (with-helm-alive-p
         (helm-attrset 'quick-delete '(helm-ff-quick-delete . never-split))
         (helm-execute-persistent-action 'quick-delete)))

  This function is then bound in `helm-find-files-map'.")

   (persistent-action-if
    :initarg :persistent-action-if
    :initform nil
    :custom function
    :documentation
    "  Similar from persistent action but it is a function that should
  return an object suitable for persistent action when called , i.e. a
  function or a cons cell.
  Example:

     (defun foo-persistent-action (candidate)
       (cond (something
              ;; Don't split helm-window.
              (cons (lambda (_ignore)
                      (do-something candidate))
                    'no-split))
             ;; Split helm-window.
             (something-else
              (lambda (_ignore)
                (do-something-else candidate)))))

     :persistent-action-if 'foo-persistent-action

  Here when hitting `C-j' one of the lambda's will be executed
  depending on something or something-else condition, splitting or not
  splitting as needed.
  See `helm-find-files-persistent-action-if' definition as another example.")

   (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' or in `minibuffer' depending
  of value of `helm-echo-input-in-header-line' and `helm-display-header-line'.")

   (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 (choice boolean integer)
    :documentation
    "  Allow multiline candidates.
  When non-nil candidates will be separated by `helm-candidate-separator'.
  You can customize the color of this separator with `helm-separator' face.
  Value of multiline can be an integer which specify the maximum size of the
  multiline string to display, if multiline string is longer than this value
  it will be truncated.")

   (requires-pattern
    :initarg :requires-pattern
    :initform 0
    :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
  `candidate-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 `candidate-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
    "  Transform the selected candidate when passing it to action.

  Function called with one parameter, the selected candidate.

  Avoid recomputing all candidates with candidate-transformer
  or filtered-candidate-transformer to give a new value to REAL,
  instead the selected candidate is transformed only when passing it
  to action.

  Note that this is NOT a transformer,
  so the display will not be modified by this function.")

   (real-to-display
    :initarg :real-to-display
    :initform nil
    :custom function
    :documentation
    "  Recompute all candidates computed previously with other transformers.

  Function called with one parameter, the selected candidate.

  The real value of candidates will be shown in display.
  Note: This have nothing to do with display-to-real.
  It is unuseful as the same can be performed by using more than
  one function in transformers, it is kept only for backward compatibility.")

   (marked-with-props
    :initarg :marked-with-props
    :initform nil
    :custom (choice boolean symbol)
    :documentation
    "  Get candidates with their properties in `helm-marked-candidates'.
  Allow using the FORCE-DISPLAY-PART of `helm-get-selection' in marked
  candidates, use t or 'withprop to pass it to `helm-get-selection'.")

   (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'), and sources using `match-dynamic'
  since they perform pattern matching themselves.

  Note that FUZZY-MATCH slot will overhide value of this slot.")

   (match-on-real
    :initarg :match-on-real
    :initform nil
    :custom boolean
    :documentation
    "  Match the real value of candidates when non nil.")

   (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).

  This attribute is not supported for asynchronous sources
  since they perform pattern matching themselves.")

   (redisplay
    :initarg :redisplay
    :initform 'identity
    :custom (choice list function)
    :documentation
    "  A function or a list of functions to apply to current list
  of candidates when redisplaying buffer with `helm-redisplay-buffer'.
  This is only interesting for modifying and redisplaying the whole list
  of candidates in async sources.
  It uses `identity' by default for when async sources are mixed with
  normal sources, in this case these normal sources are not modified and
  redisplayed as they are.")

   (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.
  Passing the history variable here have no effect
  so add it also in the `helm' call with the :history keyword.
  The main point of adding the variable here
  is to make it available when resuming.")

   (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'.
  It will be displayed in `header-line' or in `minibuffer' depending
  of value of `helm-echo-input-in-header-line' and `helm-display-header-line'.
  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.
  With a value of 1 enable, a value of -1 or nil disable the mode.
  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'.")

   (multimatch
    :initarg :multimatch
    :initform t
    :custom boolean
    :documentation
    "  Use the multi-match algorithm when non-nil.
  I.e Allow specifying multiple patterns separated by spaces.
  When a pattern is prefixed by \"!\" the negation of this pattern is used,
  i.e match anything but this pattern.
  It is the standard way of matching in helm and is enabled by default.
  It can be used with fuzzy-matching enabled, but as soon helm detect a space,
  each pattern will match by regexp and will not be fuzzy.")

   (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.
  On async sources, as matching is done by the backend, this have
  no effect apart for highlighting matches.")

   (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'.

  Should be a variable (defined with defvar).
  Can be also an anonymous function or a list of functions
  directly added to slot, this is not recommended though.")

   (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'.

  Should be a variable.
  Can be also an anonymous function or a list of functions
  directly added to slot, this is not recommended though.")

   (delayed
    :initarg :delayed
    :initform nil
    :custom (choice null integer)
    :documentation
    "  This slot have no more effect and is just kept for backward compatibility.
  Please don't use it.")

   (must-match
    :initarg :must-match
    :initform nil
    :custom symbol
    :documentation
    "  Same as `completing-read' require-match arg.
  Possible values are:
  - `t' which prevent exiting with an empty helm-buffer i.e. no matches.
  - `confirm' which ask for confirmation i.e. need to press a second
     time RET.
  - `nil' is the default and is doing nothing i.e. returns nil when
    pressing RET with an empty helm-buffer.
  - Any other non nil values e.g. `ignore' allow exiting with
    minibuffer contents as candidate value (in this case helm-buffer
    is empty).")

   (group
    :initarg :group
    :initform helm
    :custom symbol
    :documentation
    "  The current source group, default to `helm' when not specified."))

  "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"))

   (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 :MULTIMATCH nil.")

   (match-dynamic
    :initarg :match-dynamic
    :initform nil
    :custom boolean
    :documentation
    "  Disable all helm matching functions when non nil.
  The :candidates function in this case is in charge of fetching
  candidates dynamically according to `helm-pattern'.
  Note that :volatile is automatically enabled when using this, so no
  need to specify it."))

  "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 function called with no arguments must return a process
  i.e. `processp', it use typically `start-process' or `make-process',
  see (info \"(elisp) Asynchronous Processes\").
  

  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.")

   (multimatch :initform nil))

  "Use this class to define a helm source calling an external process.
The external process is called typically in a `start-process' call to be
asynchronous.

Note that using multiples asynchronous sources is not fully working,
expect weird behavior if you try this.

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, a list or a buffer 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' or directly
  with `helm-candidates-buffer' if data is a buffer.
  This is an easy and fast method to build a `candidates-in-buffer' source.")

   (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 region from point at line-beginning and point
  at line-end in the `helm-candidate-buffer' to a string which will be displayed
  in the `helm-buffer', it takes two args BEG and END.
  By default, `helm-candidates-in-buffer' uses
  `buffer-substring-no-properties' which does no conversion and doesn't carry
  text 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 multimatch,
  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
        :MULTIMATCH slot."))

  "Use this source to make helm sources storing candidates inside a buffer.

The buffer storing candidates is generated by `helm-candidate-buffer' function
and all search are done in this buffer, results are transfered to the `helm-buffer'
when done.
Contrarily to `helm-source-sync' candidates are matched using a function
like `re-search-forward' (see below documentation of `:search' slot) which makes
the search much faster than matching candidates one by one.
If you want to add search functions to your sources, don't use `:match' which
will raise an error, but `:search'.
See `helm-candidates-in-buffer' for more infos.")

(defclass helm-source-dummy (helm-source)
  ((candidates
    :initform '("dummy"))

   (filtered-candidate-transformer
    :initform (lambda (_candidates _source) (list helm-pattern)))

   (multimatch
    :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))
                           (count 1))
                       (with-current-buffer (helm-candidate-buffer 'global)
                         (insert-file-contents file)
                         (goto-char (point-min))
                         (while (not (eobp))
                           (add-text-properties
                            (point-at-bol) (point-at-eol)
                            `(helm-linum ,count))
                           (cl-incf count)
                           (forward-line 1))))))
   (get-line :initform #'buffer-substring)
   (candidates-file
    :initarg :candidates-file
    :initform nil
    :custom string
    :documentation
    "  A filename.
  Each line number of FILE is accessible with helm-linum property
  from candidate display part."))

  "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 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)))
    (setf (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)))
    (setf (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 predicate candidate)
                               (helm-append-at-nth
                                actions new-action index))
                              (t actions)))))
    (cond ((functionp actions)
           (setf (slot-value source 'action) (list (cons "Default action" actions))))
          ((listp actions)
           (setf (slot-value source 'action) (helm-interpret-value actions source))))
    (when (or (symbolp action-transformers) (functionp action-transformers))
      (setq action-transformers (list action-transformers)))
    (setf (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))
  (unless (slot-value source 'group)
    (setf (slot-value source 'group) 'helm))
  (when (slot-value source 'delayed)
    (warn "Deprecated usage of helm `delayed' slot in `%s'"
          (slot-value source 'name)))
  (helm-aif (slot-value source 'keymap)
      (let* ((map (if (symbolp it)
                      (symbol-value it)
                    it))
             (must-match-map (when (slot-value source 'must-match)
                               (let ((map (make-sparse-keymap)))
                                 (define-key map (kbd "RET")
                                   'helm-confirm-and-exit-minibuffer)
                                 map)))
             (loc-map (if must-match-map
                          (make-composed-keymap
                           must-match-map map)
                        map)))
        (setf (slot-value source 'keymap) loc-map)))
  (helm-aif (slot-value source 'persistent-help)
      (setf (slot-value source 'header-line)
            (helm-source--persistent-help-string it source))
    (setf (slot-value source 'header-line) (helm-source--header-line source)))
  (when (slot-value source 'fuzzy-match)
    (cl-assert helm-fuzzy-sort-fn nil "Wrong type argument functionp: nil")
    (setf (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)
    (setf (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))))
  (when (numberp (helm-interpret-value (slot-value source 'multiline)))
    (setf (slot-value source 'filtered-candidate-transformer)
          (helm-aif (slot-value source 'filtered-candidate-transformer)
              (append (helm-mklist it)
                      (list #'helm-multiline-transformer))
            (list #'helm-multiline-transformer))))
  (helm-aif (slot-value source 'requires-pattern)
      (let ((val (if (symbolp it)
                     (symbol-value it)
                   it)))
        (setf (slot-value source 'requires-pattern) val))))

(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)
        (setf (slot-value source 'match)
              (append (helm-mklist it)
                      (list helm-fuzzy-match-fn)))
      (setf (slot-value source 'match) helm-fuzzy-match-fn)))
  (when (slot-value source 'multimatch)
    (setf (slot-value source 'match)
          (helm-source-mm-get-search-or-match-fns source 'match)))
  (helm-aif (and (null (slot-value source 'multimatch))
                 (slot-value source 'migemo))
      (unless (eq it 'nomultimatch) ; Use own migemo fn.
        (setf (slot-value source 'match)
              (append (helm-mklist (slot-value source 'match))
                      '(helm-mm-3-migemo-match)))))
  (when (slot-value source 'match-dynamic)
    (setf (slot-value source 'match) 'identity)
    (setf (slot-value source 'match-part) nil)
    (setf (slot-value source 'multimatch) nil)
    (setf (slot-value source 'fuzzy-match) nil)
    (setf (slot-value source 'volatile) t)))

(defmethod helm--setup-source ((source helm-source-in-buffer))
  (cl-assert (eq (slot-value source 'candidates) 'helm-candidates-in-buffer)
             nil
             (format "Wrong usage of `candidates' attr in `%s' use `data' or `init' instead"
                     (slot-value source 'name)))
  (let ((cur-init (slot-value source 'init)))
    (helm-aif (slot-value source 'data)
        (setf (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
                    (cond ((functionp it) (funcall it))
                          ((and (bufferp it) (buffer-live-p it))
                           (with-current-buffer it (buffer-string)))
                          (t it)))))))))
  (when (slot-value source 'fuzzy-match)
    (helm-aif (slot-value source 'search)
        (setf (slot-value source 'search)
              (append (helm-mklist it)
                      (list helm-fuzzy-search-fn)))
      (setf (slot-value source 'search) (list helm-fuzzy-search-fn))))
  (when (slot-value source 'multimatch)
    (setf (slot-value source 'search)
          (helm-source-mm-get-search-or-match-fns source 'search)))
  (helm-aif (and (null (slot-value source 'multimatch))
                 (slot-value source 'migemo))
      (unless (eq it 'nomultimatch)
        (setf (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 'multimatch))
             nil "`multimatch' not allowed in async sources.")
  (cl-assert (null (slot-value source 'fuzzy-match))
             nil "`fuzzy-match' not supported 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 2))
  `(helm-make-source ,name 'helm-source-in-file
     :candidates-file ,file ,@args))


(provide 'helm-source)

;; Local Variables:
;; byte-compile-warnings: (not obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:

;;; helm-source ends here