Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1207 lines
45 KiB

4 years ago
  1. ;;; helm-source.el --- Helm source creation. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2015 ~ 2019 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  3. ;; Author: Thierry Volpiatto <thierry.volpiatto@gmail.com>
  4. ;; URL: http://github.com/emacs-helm/helm
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Interface to create helm sources easily.
  17. ;; Actually the eieo objects are transformed in alist for compatibility.
  18. ;; In the future this package should allow creating source as eieo objects
  19. ;; without conversion to alist, teaching helm to read such a structure.
  20. ;; The compatibility with alists would be kept.
  21. ;;; Code:
  22. (require 'cl-lib)
  23. (require 'eieio)
  24. (require 'helm-lib)
  25. (defvar helm-fuzzy-sort-fn)
  26. (defvar helm-fuzzy-match-fn)
  27. (defvar helm-fuzzy-search-fn)
  28. (declare-function helm-init-candidates-in-buffer "helm.el")
  29. (declare-function helm-interpret-value "helm.el")
  30. (declare-function helm-fuzzy-highlight-matches "helm.el")
  31. ;;; Advice Emacs fn
  32. ;; Make Classes's docstrings more readable by removing al the
  33. ;; unnecessary crap.
  34. (defun helm-source--cl--print-table (_header rows)
  35. "Advice for `cl--print-table' to make readable class slots docstrings."
  36. (let ((format "%s\n\n Initform=%s\n\n%s"))
  37. (dolist (row rows)
  38. (setcar row (propertize (car row) 'face 'italic))
  39. (setcdr row (nthcdr 1 (cdr row)))
  40. (insert "\n* " (apply #'format format row) "\n"))))
  41. (defgeneric helm--setup-source (source)
  42. "Prepare slots and handle slot errors before creating a helm source.")
  43. (defgeneric helm-setup-user-source (source)
  44. "Allow users modifying slots in SOURCE just before creation.")
  45. ;;; Classes for sources
  46. ;;
  47. ;;
  48. (defclass helm-source ()
  49. ((name
  50. :initarg :name
  51. :initform nil
  52. :custom string
  53. :documentation
  54. " The name of the source.
  55. A string which is also the heading which appears
  56. above the list of matches from the source. Must be unique.")
  57. (header-name
  58. :initarg :header-name
  59. :initform nil
  60. :custom function
  61. :documentation
  62. " A function returning the display string of the header.
  63. Its argument is the name of the source. This attribute is useful to
  64. add an additional information with the source name.
  65. It doesn't modify the name of the source.")
  66. (init
  67. :initarg :init
  68. :initform nil
  69. :custom function
  70. :documentation
  71. " Function called with no parameters when helm is started.
  72. It is useful for collecting current state information which can be
  73. used to create the list of candidates later.
  74. Initialization of `candidates-in-buffer' is done here
  75. with `helm-init-candidates-in-buffer'.")
  76. (candidates
  77. :initarg :candidates
  78. :initform nil
  79. :custom (choice function list)
  80. :documentation
  81. " Specifies how to retrieve candidates from the source.
  82. It can either be a variable name, a function called with no parameters
  83. or the actual list of candidates.
  84. Do NOT use this for asynchronous sources, use `candidates-process'
  85. instead.
  86. The list must be a list whose members are strings, symbols
  87. or (DISPLAY . REAL) pairs.
  88. In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
  89. in the Helm buffer, but the REAL one is used as action
  90. argument when the candidate is selected. This allows a more
  91. readable presentation for candidates which would otherwise be,
  92. for example, too long or have a common part shared with other
  93. candidates which can be safely replaced with an abbreviated
  94. string for display purposes.
  95. Note that if the (DISPLAY . REAL) form is used then pattern
  96. matching is done on the displayed string, not on the real
  97. value.
  98. This function, generally should not compute candidates according to
  99. `helm-pattern' which defeat all the Helm's matching mechanism
  100. i.e. multiple pattern matching and/or fuzzy matching.
  101. If you want to do so, use :match-dynamic slot to be sure matching
  102. occur only in :candidates function and there is no conflict with
  103. other match functions.")
  104. (update
  105. :initarg :update
  106. :initform nil
  107. :custom function
  108. :documentation
  109. " Function called with no parameters at before \"init\" function
  110. when `helm-force-update' is called.")
  111. (cleanup
  112. :initarg :cleanup
  113. :initform nil
  114. :custom function
  115. :documentation
  116. " Function called with no parameters when *helm* buffer is
  117. closed. It is useful for killing unneeded candidates buffer.
  118. Note that the function is executed BEFORE performing action.")
  119. (keymap
  120. :initarg :keymap
  121. :initform helm-map
  122. :custom sexp
  123. :documentation
  124. " Specific keymap for this source.
  125. default value is `helm-map'.")
  126. (action
  127. :initarg :action
  128. :initform 'identity
  129. :custom (alist :key-type string
  130. :value-type function)
  131. :documentation
  132. " An alist of (DISPLAY . FUNCTION) pairs, a variable name or a function.
  133. FUNCTION is called with one parameter: the selected candidate.
  134. An action other than the default can be chosen from this list
  135. of actions for the currently selected candidate (by default
  136. with TAB). The DISPLAY string is shown in the completions
  137. buffer and the FUNCTION is invoked when an action is
  138. selected. The first action of the list is the default.
  139. You should use `helm-make-actions' to build this alist easily.")
  140. (persistent-action
  141. :initarg :persistent-action
  142. :initform nil
  143. :custom function
  144. :documentation
  145. " Can be a either a Function called with one parameter (the
  146. selected candidate) or a cons cell where first element is this
  147. same function and second element a symbol (e.g never-split)
  148. that inform `helm-execute-persistent-action' to not split his
  149. window to execute this persistent action.
  150. Example:
  151. (defun foo-persistent-action (candidate)
  152. (do-something candidate))
  153. :persistent-action '(foo-persistent-action . never-split) ; Don't split
  154. or
  155. :persistent-action 'foo-persistent-action ; Split
  156. When specifying :persistent-action by slot directly, foo-persistent-action
  157. will be executed without quitting helm when hitting `C-j'.
  158. Note that other persistent actions can be defined using other
  159. bindings than `C-j' by simply defining an interactive function bound
  160. to a key in the keymap source.
  161. The function should create a new attribute in source before calling
  162. `helm-execute-persistent-action' on this attribute.
  163. Example:
  164. (defun helm-ff-persistent-delete ()
  165. \"Delete current candidate without quitting.\"
  166. (interactive)
  167. (with-helm-alive-p
  168. (helm-attrset 'quick-delete '(helm-ff-quick-delete . never-split))
  169. (helm-execute-persistent-action 'quick-delete)))
  170. This function is then bound in `helm-find-files-map'.")
  171. (persistent-action-if
  172. :initarg :persistent-action-if
  173. :initform nil
  174. :custom function
  175. :documentation
  176. " Similar from persistent action but it is a function that should
  177. return an object suitable for persistent action when called , i.e. a
  178. function or a cons cell.
  179. Example:
  180. (defun foo-persistent-action (candidate)
  181. (cond (something
  182. ;; Don't split helm-window.
  183. (cons (lambda (_ignore)
  184. (do-something candidate))
  185. 'no-split))
  186. ;; Split helm-window.
  187. (something-else
  188. (lambda (_ignore)
  189. (do-something-else candidate)))))
  190. :persistent-action-if 'foo-persistent-action
  191. Here when hitting `C-j' one of the lambda's will be executed
  192. depending on something or something-else condition, splitting or not
  193. splitting as needed.
  194. See `helm-find-files-persistent-action-if' definition as another example.")
  195. (persistent-help
  196. :initarg :persistent-help
  197. :initform nil
  198. :custom string
  199. :documentation
  200. " A string to explain persistent-action of this source. It also
  201. accepts a function or a variable name.
  202. It will be displayed in `header-line' or in `minibuffer' depending
  203. of value of `helm-echo-input-in-header-line' and `helm-display-header-line'.")
  204. (help-message
  205. :initarg :help-message
  206. :initform nil
  207. :custom (choice string function)
  208. :documentation
  209. " Help message for this source.
  210. If not present, `helm-help-message' value will be used.")
  211. (multiline
  212. :initarg :multiline
  213. :initform nil
  214. :custom (choice boolean integer)
  215. :documentation
  216. " Allow multiline candidates.
  217. When non-nil candidates will be separated by `helm-candidate-separator'.
  218. You can customize the color of this separator with `helm-separator' face.
  219. Value of multiline can be an integer which specify the maximum size of the
  220. multiline string to display, if multiline string is longer than this value
  221. it will be truncated.")
  222. (requires-pattern
  223. :initarg :requires-pattern
  224. :initform nil
  225. :custom integer
  226. :documentation
  227. " If present matches from the source are shown only if the
  228. pattern is not empty. Optionally, it can have an integer
  229. parameter specifying the required length of input which is
  230. useful in case of sources with lots of candidates.")
  231. (candidate-transformer
  232. :initarg :candidate-transformer
  233. :initform nil
  234. :custom (choice function list)
  235. :documentation
  236. " It's a function or a list of functions called with one argument
  237. when the completion list from the source is built. The argument
  238. is the list of candidates retrieved from the source. The
  239. function should return a transformed list of candidates which
  240. will be used for the actual completion. If it is a list of
  241. functions, it calls each function sequentially.
  242. This can be used to transform or remove items from the list of
  243. candidates.
  244. Note that `candidates' is run already, so the given transformer
  245. function should also be able to handle candidates with (DISPLAY
  246. . REAL) format.")
  247. (filtered-candidate-transformer
  248. :initarg :filtered-candidate-transformer
  249. :initform nil
  250. :custom (choice function list)
  251. :documentation
  252. " It has the same format as `candidate-transformer', except the
  253. function is called with two parameters: the candidate list and
  254. the source.
  255. This transformer is run on the candidate list which is already
  256. filtered by the current pattern. While `candidate-transformer'
  257. is run only once, it is run every time the input pattern is
  258. changed.
  259. It can be used to transform the candidate list dynamically, for
  260. example, based on the current pattern.
  261. In some cases it may also be more efficent to perform candidate
  262. transformation here, instead of with `candidate-transformer'
  263. even if this transformation is done every time the pattern is
  264. changed. For example, if a candidate set is very large then
  265. `candidate-transformer' transforms every candidate while only
  266. some of them will actually be displayed due to the limit
  267. imposed by `helm-candidate-number-limit'.
  268. Note that `candidates' and `candidate-transformer' is run
  269. already, so the given transformer function should also be able
  270. to handle candidates with (DISPLAY . REAL) format.")
  271. (filter-one-by-one
  272. :initarg :filter-one-by-one
  273. :initform nil
  274. :custom (choice function list)
  275. :documentation
  276. " A transformer function that treat candidates one by one.
  277. It is called with one arg the candidate.
  278. It is faster than `filtered-candidate-transformer' or
  279. `candidate-transformer', but should be used only in sources
  280. that recompute constantly their candidates, e.g `helm-source-find-files'.
  281. Filtering happen early and candidates are treated
  282. one by one instead of re-looping on the whole list.
  283. If used with `filtered-candidate-transformer' or `candidate-transformer'
  284. these functions should treat the candidates transformed by the
  285. `filter-one-by-one' function in consequence.")
  286. (display-to-real
  287. :initarg :display-to-real
  288. :initform nil
  289. :custom function
  290. :documentation
  291. " Transform the selected candidate when passing it to action.
  292. Function called with one parameter, the selected candidate.
  293. Avoid recomputing all candidates with candidate-transformer
  294. or filtered-candidate-transformer to give a new value to REAL,
  295. instead the selected candidate is transformed only when passing it
  296. to action.
  297. Note that this is NOT a transformer,
  298. so the display will not be modified by this function.")
  299. (real-to-display
  300. :initarg :real-to-display
  301. :initform nil
  302. :custom function
  303. :documentation
  304. " Recompute all candidates computed previously with other transformers.
  305. Function called with one parameter, the selected candidate.
  306. The real value of candidates will be shown in display.
  307. Note: This have nothing to do with display-to-real.
  308. It is unuseful as the same can be performed by using more than
  309. one function in transformers, it is kept only for backward compatibility.")
  310. (marked-with-props
  311. :initarg :marked-with-props
  312. :initform nil
  313. :custom (choice boolean symbol)
  314. :documentation
  315. " Get candidates with their properties in `helm-marked-candidates'.
  316. Allow using the FORCE-DISPLAY-PART of `helm-get-selection' in marked
  317. candidates, use t or 'withprop to pass it to `helm-get-selection'.")
  318. (action-transformer
  319. :initarg :action-transformer
  320. :initform nil
  321. :custom (choice function list)
  322. :documentation
  323. " It's a function or a list of functions called with two
  324. arguments when the action list from the source is
  325. assembled. The first argument is the list of actions, the
  326. second is the current selection. If it is a list of functions,
  327. it calls each function sequentially.
  328. The function should return a transformed action list.
  329. This can be used to customize the list of actions based on the
  330. currently selected candidate.")
  331. (pattern-transformer
  332. :initarg :pattern-transformer
  333. :initform nil
  334. :custom (choice function list)
  335. :documentation
  336. " It's a function or a list of functions called with one argument
  337. before computing matches. Its argument is `helm-pattern'.
  338. Functions should return transformed `helm-pattern'.
  339. It is useful to change interpretation of `helm-pattern'.")
  340. (candidate-number-limit
  341. :initarg :candidate-number-limit
  342. :initform nil
  343. :custom integer
  344. :documentation
  345. " Override `helm-candidate-number-limit' only for this source.")
  346. (volatile
  347. :initarg :volatile
  348. :initform nil
  349. :custom boolean
  350. :documentation
  351. " Indicates the source assembles the candidate list dynamically,
  352. so it shouldn't be cached within a single Helm
  353. invocation. It is only applicable to synchronous sources,
  354. because asynchronous sources are not cached.")
  355. (match
  356. :initarg :match
  357. :initform nil
  358. :custom (choice function list)
  359. :documentation
  360. " List of functions called with one parameter: a candidate. The
  361. function should return non-nil if the candidate matches the
  362. current pattern (see variable `helm-pattern').
  363. When using `candidates-in-buffer' its default value is `identity' and
  364. don't have to be changed, use the `search' slot instead.
  365. This attribute allows the source to override the default
  366. pattern matching based on `string-match'. It can be used, for
  367. example, to implement a source for file names and do the
  368. pattern matching on the basename of files, since it's more
  369. likely one is typing part of the basename when searching for a
  370. file, instead of some string anywhere else in its path.
  371. If the list contains more than one function then the list of
  372. matching candidates from the source is constructed by appending
  373. the results after invoking the first function on all the
  374. potential candidates, then the next function, and so on. The
  375. matching candidates supplied by the first function appear first
  376. in the list of results and then results from the other
  377. functions, respectively.
  378. This attribute has no effect for asynchronous sources (see
  379. attribute `candidates'), and sources using `match-dynamic'
  380. since they perform pattern matching themselves.
  381. Note that FUZZY-MATCH slot will overhide value of this slot.")
  382. (fuzzy-match
  383. :initarg :fuzzy-match
  384. :initform nil
  385. :custom boolean
  386. :documentation
  387. " Enable fuzzy matching in this source.
  388. This will overwrite settings in MATCH slot, and for
  389. sources built with child class `helm-source-in-buffer' the SEARCH slot.
  390. This is an easy way of enabling fuzzy matching, but you can use the MATCH
  391. or SEARCH slots yourself if you want something more elaborated, mixing
  392. different type of match (See `helm-source-buffers' class for example).
  393. This attribute is not supported for asynchronous sources
  394. since they perform pattern matching themselves.")
  395. (redisplay
  396. :initarg :redisplay
  397. :initform 'identity
  398. :custom (choice list function)
  399. :documentation
  400. " A function or a list of functions to apply to current list
  401. of candidates when redisplaying buffer with `helm-redisplay-buffer'.
  402. This is only interesting for modifying and redisplaying the whole list
  403. of candidates in async sources.
  404. It uses `identity' by default for when async sources are mixed with
  405. normal sources, in this case these normal sources are not modified and
  406. redisplayed as they are.")
  407. (nomark
  408. :initarg :nomark
  409. :initform nil
  410. :custom boolean
  411. :documentation
  412. " Don't allow marking candidates when this attribute is present.")
  413. (nohighlight
  414. :initarg :nohighlight
  415. :initform nil
  416. :custom boolean
  417. :documentation
  418. " Disable highlighting matches in this source.
  419. This will disable generic highlighting of matches,
  420. but some specialized highlighting can be done from elsewhere,
  421. i.e from `filtered-candidate-transformer' or `filter-one-by-one' slots.
  422. So use this to either disable completely highlighting in your source,
  423. or to disable highlighting and use a specialized highlighting matches
  424. function for this source.
  425. Remember that this function should run AFTER all filter functions if those
  426. filter functions are modifying face properties, though it is possible to
  427. avoid this by using new `add-face-text-property' in your filter functions.")
  428. (allow-dups
  429. :initarg :allow-dups
  430. :initform nil
  431. :custom boolean
  432. :documentation
  433. " Allow helm collecting duplicates candidates.")
  434. (history
  435. :initarg :history
  436. :initform nil
  437. :custom symbol
  438. :documentation
  439. " Allow passing history variable to helm from source.
  440. It should be a quoted symbol.
  441. Passing the history variable here have no effect
  442. so add it also in the `helm' call with the :history keyword.
  443. The main point of adding the variable here
  444. is to make it available when resuming.")
  445. (coerce
  446. :initarg :coerce
  447. :initform nil
  448. :custom function
  449. :documentation
  450. " It's a function called with one argument: the selected candidate.
  451. This function is intended for type convertion. In normal case,
  452. the selected candidate (string) is passed to action
  453. function. If coerce function is specified, it is called just
  454. before action function.
  455. Example: converting string to symbol
  456. (coerce . intern)")
  457. (mode-line
  458. :initarg :mode-line
  459. :initform nil
  460. :custom (choice string sexp)
  461. :documentation
  462. " Source local `helm-mode-line-string' (included in
  463. `mode-line-format'). It accepts also variable/function name.")
  464. (header-line
  465. :initarg :header-line
  466. :initform nil
  467. :custom (choice string function)
  468. :documentation
  469. " Source local `header-line-format'.
  470. It will be displayed in `header-line' or in `minibuffer' depending
  471. of value of `helm-echo-input-in-header-line' and `helm-display-header-line'.
  472. It accepts also variable/function name.")
  473. (resume
  474. :initarg :resume
  475. :initform nil
  476. :custom function
  477. :documentation
  478. " Function called with no parameters at end of initialization
  479. when `helm-resume' is started.
  480. If this function try to do something against `helm-buffer', \(e.g updating,
  481. searching etc...\) probably you should run it in a timer to ensure
  482. `helm-buffer' is ready.")
  483. (follow
  484. :initarg :follow
  485. :initform nil
  486. :custom integer
  487. :documentation
  488. " Enable `helm-follow-mode' for this source only.
  489. With a value of 1 enable, a value of -1 or nil disable the mode.
  490. See `helm-follow-mode' for more infos.")
  491. (follow-delay
  492. :initarg :follow-delay
  493. :initform nil
  494. :custom integer
  495. :documentation
  496. " `helm-follow-mode' will execute persistent-action after this delay.
  497. Otherwise value of `helm-follow-input-idle-delay' is used if non--nil,
  498. If none of these are found fallback to `helm-input-idle-delay'.")
  499. (multimatch
  500. :initarg :multimatch
  501. :initform t
  502. :custom boolean
  503. :documentation
  504. " Use the multi-match algorithm when non-nil.
  505. I.e Allow specifying multiple patterns separated by spaces.
  506. When a pattern is prefixed by \"!\" the negation of this pattern is used,
  507. i.e match anything but this pattern.
  508. It is the standard way of matching in helm and is enabled by default.
  509. It can be used with fuzzy-matching enabled, but as soon helm detect a space,
  510. each pattern will match by regexp and will not be fuzzy.")
  511. (match-part
  512. :initarg :match-part
  513. :initform nil
  514. :custom function
  515. :documentation
  516. " Allow matching only one part of candidate.
  517. If source contain match-part attribute, match is computed only
  518. on part of candidate returned by the call of function provided
  519. by this attribute. The function should have one arg, candidate,
  520. and return only a specific part of candidate.
  521. On async sources, as matching is done by the backend, this have
  522. no effect apart for highlighting matches.")
  523. (before-init-hook
  524. :initarg :before-init-hook
  525. :initform nil
  526. :custom symbol
  527. :documentation
  528. " A local hook that run at beginning of initilization of this source.
  529. i.e Before the creation of `helm-buffer'.
  530. Should be a variable (defined with defvar).
  531. Can be also an anonymous function or a list of functions
  532. directly added to slot, this is not recommended though.")
  533. (after-init-hook
  534. :initarg :after-init-hook
  535. :initform nil
  536. :custom symbol
  537. :documentation
  538. " A local hook that run at end of initilization of this source.
  539. i.e After the creation of `helm-buffer'.
  540. Should be a variable.
  541. Can be also an anonymous function or a list of functions
  542. directly added to slot, this is not recommended though.")
  543. (delayed
  544. :initarg :delayed
  545. :initform nil
  546. :custom (choice null integer)
  547. :documentation
  548. " This slot have no more effect and is just kept for backward compatibility.
  549. Please don't use it.")
  550. (must-match
  551. :initarg :must-match
  552. :initform nil
  553. :custom symbol
  554. :documentation
  555. " Prevent exiting with empty helm buffer.
  556. For this to work `minibuffer-completion-confirm' must be let-bounded
  557. around the helm call.
  558. Same as `completing-read' require-match arg, possible values are `t'
  559. or `confirm'.")
  560. (group
  561. :initarg :group
  562. :initform helm
  563. :custom symbol
  564. :documentation
  565. " The current source group, default to `helm' when not specified."))
  566. "Main interface to define helm sources."
  567. :abstract t)
  568. (defclass helm-source-sync (helm-source)
  569. ((candidates
  570. :initform '("ERROR: You must specify the `candidates' slot, either with a list or a function"))
  571. (migemo
  572. :initarg :migemo
  573. :initform nil
  574. :custom boolean
  575. :documentation
  576. " Enable migemo.
  577. When multimatch is disabled, you can give the symbol 'nomultimatch as value
  578. to force not using generic migemo matching function.
  579. In this case you have to provide your own migemo matching funtion
  580. that kick in when `helm-migemo-mode' is enabled.
  581. Otherwise it will be available for this source once `helm-migemo-mode'
  582. is enabled when non-nil.")
  583. (match-strict
  584. :initarg :match-strict
  585. :initform nil
  586. :custom function
  587. :documentation
  588. " When specifying a match function within a source and
  589. helm-multi-match is enabled, the result of all matching
  590. functions will be concatened, which in some cases is not what
  591. is wanted. When using `match-strict' only this or these
  592. functions will be used. You can specify those functions as a
  593. list of functions or a single symbol function.
  594. NOTE: This have the same effect as using :MULTIMATCH nil.")
  595. (match-dynamic
  596. :initarg :match-dynamic
  597. :initform nil
  598. :custom boolean
  599. :documentation
  600. " Disable all helm matching functions when non nil.
  601. The :candidates function in this case is in charge of fetching
  602. candidates dynamically according to `helm-pattern'.
  603. Note that :volatile is automatically enabled when using this, so no
  604. need to specify it."))
  605. "Use this class to make helm sources using a list of candidates.
  606. This list should be given as a normal list, a variable handling a list
  607. or a function returning a list.
  608. Matching is done basically with `string-match' against each candidate.")
  609. (defclass helm-source-async (helm-source)
  610. ((candidates-process
  611. :initarg :candidates-process
  612. :initform nil
  613. :custom function
  614. :documentation
  615. " This attribute is used to define a process as candidate.
  616. The function called with no arguments must return a process
  617. i.e. `processp', it use typically `start-process' or `make-process',
  618. see (info \"(elisp) Asynchronous Processes\").
  619. NOTE:
  620. When building the source at runtime you can give directly a process
  621. as value, otherwise wrap the process call into a function.
  622. The process buffer should be nil, otherwise, if you use
  623. `helm-buffer' give to the process a sentinel.")
  624. (multimatch :initform nil))
  625. "Use this class to define a helm source calling an external process.
  626. The external process is called typically in a `start-process' call to be
  627. asynchronous.
  628. Note that using multiples asynchronous sources is not fully working,
  629. expect weird behavior if you try this.
  630. The :candidates slot is not allowed even if described because this class
  631. inherit from `helm-source'.")
  632. (defclass helm-source-in-buffer (helm-source)
  633. ((init
  634. :initform 'helm-default-init-source-in-buffer-function)
  635. (data
  636. :initarg :data
  637. :initform nil
  638. :custom (choice list string)
  639. :documentation
  640. " A string, a list or a buffer that will be used to feed the `helm-candidates-buffer'.
  641. This data will be passed in a function added to the init slot and
  642. the buffer will be build with `helm-init-candidates-in-buffer' or directly
  643. with `helm-candidates-buffer' if data is a buffer.
  644. This is an easy and fast method to build a `candidates-in-buffer' source.")
  645. (migemo
  646. :initarg :migemo
  647. :initform nil
  648. :custom boolean
  649. :documentation
  650. " Enable migemo.
  651. When multimatch is disabled, you can give the symbol 'nomultimatch as value
  652. to force not using generic migemo matching function.
  653. In this case you have to provide your own migemo matching funtion
  654. that kick in when `helm-migemo-mode' is enabled.
  655. Otherwise it will be available for this source once `helm-migemo-mode'
  656. is enabled when non-nil.")
  657. (candidates
  658. :initform 'helm-candidates-in-buffer)
  659. (volatile
  660. :initform t)
  661. (match
  662. :initform '(identity))
  663. (get-line
  664. :initarg :get-line
  665. :initform 'buffer-substring-no-properties
  666. :custom function
  667. :documentation
  668. " A function like `buffer-substring-no-properties' or `buffer-substring'.
  669. This function converts region from point at line-beginning and point
  670. at line-end in the `helm-candidate-buffer' to a string which will be displayed
  671. in the `helm-buffer', it takes two args BEG and END.
  672. By default, `helm-candidates-in-buffer' uses
  673. `buffer-substring-no-properties' which does no conversion and doesn't carry
  674. text properties.")
  675. (search
  676. :initarg :search
  677. :initform '(helm-candidates-in-buffer-search-default-fn)
  678. :custom (choice function list)
  679. :documentation
  680. " List of functions like `re-search-forward' or `search-forward'.
  681. Buffer search function used by `helm-candidates-in-buffer'.
  682. By default, `helm-candidates-in-buffer' uses `re-search-forward'.
  683. The function should take one arg PATTERN.
  684. If your search function needs to handle negation like multimatch,
  685. this function should returns in such case a cons cell of two integers defining
  686. the beg and end positions to match in the line previously matched by
  687. `re-search-forward' or similar, and move point to next line
  688. (See how the `helm-mm-3-search-base' and `helm-fuzzy-search' functions are working).
  689. NOTE: FUZZY-MATCH slot will overhide value of this slot.")
  690. (search-strict
  691. :initarg :search-strict
  692. :initform nil
  693. :custom function
  694. :documentation
  695. " When specifying a search function within a source and
  696. helm-multi-match is enabled, the result of all searching
  697. functions will be concatened, which in some cases is not what
  698. is wanted. When using `search-strict' only this or these
  699. functions will be used. You can specify those functions as a
  700. list of functions or a single symbol function.
  701. NOTE: This have the same effect as using a nil value for
  702. :MULTIMATCH slot."))
  703. "Use this source to make helm sources storing candidates inside a buffer.
  704. The buffer storing candidates is generated by `helm-candidate-buffer' function
  705. and all search are done in this buffer, results are transfered to the `helm-buffer'
  706. when done.
  707. Contrarily to `helm-source-sync' candidates are matched using a function
  708. like `re-search-forward' (see below documentation of `:search' slot) which makes
  709. the search much faster than matching candidates one by one.
  710. If you want to add search functions to your sources, don't use `:match' which
  711. will raise an error, but `:search'.
  712. See `helm-candidates-in-buffer' for more infos.")
  713. (defclass helm-source-dummy (helm-source)
  714. ((candidates
  715. :initform '("dummy"))
  716. (filtered-candidate-transformer
  717. :initform (lambda (_candidates _source) (list helm-pattern)))
  718. (multimatch
  719. :initform nil)
  720. (accept-empty
  721. :initarg :accept-empty
  722. :initform t
  723. :custom boolean
  724. :documentation
  725. " Allow exiting with an empty string.
  726. You should keep the default value.")
  727. (match
  728. :initform 'identity)
  729. (volatile
  730. :initform t)))
  731. (defclass helm-source-in-file (helm-source-in-buffer)
  732. ((init :initform (lambda ()
  733. (let ((file (helm-attr 'candidates-file))
  734. (count 1))
  735. (with-current-buffer (helm-candidate-buffer 'global)
  736. (insert-file-contents file)
  737. (goto-char (point-min))
  738. (while (not (eobp))
  739. (add-text-properties
  740. (point-at-bol) (point-at-eol)
  741. `(helm-linum ,count))
  742. (cl-incf count)
  743. (forward-line 1))))))
  744. (get-line :initform #'buffer-substring)
  745. (candidates-file
  746. :initarg :candidates-file
  747. :initform nil
  748. :custom string
  749. :documentation
  750. " A filename.
  751. Each line number of FILE is accessible with helm-linum property
  752. from candidate display part."))
  753. "The contents of the FILE will be used as candidates in buffer.")
  754. ;;; Error functions
  755. ;;
  756. ;;
  757. (defun helm-default-init-source-in-buffer-function ()
  758. (helm-init-candidates-in-buffer 'global
  759. '("ERROR: No buffer handling your data, use either the `init' slot or the `data' slot.")))
  760. ;;; Internal Builder functions.
  761. ;;
  762. ;;
  763. (defun helm--create-source (object)
  764. "[INTERNAL] Build a helm source from OBJECT.
  765. Where OBJECT is an instance of an eieio class."
  766. (cl-loop for sd in (eieio-class-slots (eieio-object-class object))
  767. for s = (eieio-slot-descriptor-name sd)
  768. for slot-val = (slot-value object s)
  769. when slot-val
  770. collect (cons s (unless (eq t slot-val) slot-val))))
  771. (defun helm-make-source (name class &rest args)
  772. "Build a `helm' source named NAME with ARGS for CLASS.
  773. Argument NAME is a string which define the source name, so no need to use
  774. the keyword :name in your source, NAME will be used instead.
  775. Argument CLASS is an eieio class object.
  776. Arguments ARGS are keyword value pairs as defined in CLASS."
  777. (declare (indent 2))
  778. (let ((source (apply #'make-instance class name args)))
  779. (setf (slot-value source 'name) name)
  780. (helm--setup-source source)
  781. (helm-setup-user-source source)
  782. (helm--create-source source)))
  783. (defun helm-make-type (class &rest args)
  784. (let ((source (apply #'make-instance class args)))
  785. (setf (slot-value source 'name) nil)
  786. (helm--setup-source source)
  787. (helm--create-source source)))
  788. (defvar helm-mm-default-search-functions)
  789. (defvar helm-mm-default-match-functions)
  790. (defun helm-source-mm-get-search-or-match-fns (source method)
  791. (let ((defmatch (helm-aif (slot-value source 'match)
  792. (helm-mklist it)))
  793. (defmatch-strict (helm-aif (and (eq method 'match)
  794. (slot-value source 'match-strict))
  795. (helm-mklist it)))
  796. (defsearch (helm-aif (and (eq method 'search)
  797. (slot-value source 'search))
  798. (helm-mklist it)))
  799. (defsearch-strict (helm-aif (and (eq method 'search-strict)
  800. (slot-value source 'search-strict))
  801. (helm-mklist it)))
  802. (migemo (slot-value source 'migemo)))
  803. (cl-case method
  804. (match (cond (defmatch-strict)
  805. (migemo
  806. (append helm-mm-default-match-functions
  807. defmatch '(helm-mm-3-migemo-match)))
  808. (defmatch
  809. (append helm-mm-default-match-functions defmatch))
  810. (t helm-mm-default-match-functions)))
  811. (search (cond (defsearch-strict)
  812. (migemo
  813. (append helm-mm-default-search-functions
  814. defsearch '(helm-mm-3-migemo-search)))
  815. (defsearch
  816. (append helm-mm-default-search-functions defsearch))
  817. (t helm-mm-default-search-functions))))))
  818. ;;; Modifiers
  819. ;;
  820. (cl-defun helm-source-add-action-to-source-if (name fn source predicate
  821. &optional (index 4))
  822. "Same as `helm-add-action-to-source-if' but for SOURCE defined as eieio object.
  823. You can use this inside a `helm--setup-source' method for a SOURCE defined as
  824. an eieio class."
  825. (let* ((actions (slot-value source 'action))
  826. (action-transformers (slot-value source 'action-transformer))
  827. (new-action (list (cons name fn)))
  828. (transformer (lambda (actions candidate)
  829. (cond ((funcall predicate candidate)
  830. (helm-append-at-nth
  831. actions new-action index))
  832. (t actions)))))
  833. (cond ((functionp actions)
  834. (setf (slot-value source 'action) (list (cons "Default action" actions))))
  835. ((listp actions)
  836. (setf (slot-value source 'action) (helm-interpret-value actions source))))
  837. (when (or (symbolp action-transformers) (functionp action-transformers))
  838. (setq action-transformers (list action-transformers)))
  839. (setf (slot-value source 'action-transformer)
  840. (delq nil (append (list transformer) action-transformers)))))
  841. ;;; Methods to build sources.
  842. ;;
  843. ;;
  844. (defun helm-source--persistent-help-string (string source)
  845. (substitute-command-keys
  846. (concat "\\<helm-map>\\[helm-execute-persistent-action]: "
  847. (or (format "%s (keeping session)" string)
  848. (slot-value source 'header-line)))))
  849. (defun helm-source--header-line (source)
  850. (substitute-command-keys
  851. (concat "\\<helm-map>\\[helm-execute-persistent-action]: "
  852. (helm-aif (or (slot-value source 'persistent-action)
  853. (slot-value source 'action))
  854. (cond ((and (symbolp it)
  855. (functionp it)
  856. (eq it 'identity))
  857. "Do Nothing")
  858. ((and (symbolp it)
  859. (boundp it)
  860. (listp (symbol-value it))
  861. (stringp (caar (symbol-value it))))
  862. (caar (symbol-value it)))
  863. ((or (symbolp it) (functionp it))
  864. (helm-symbol-name it))
  865. ((listp it)
  866. (let ((action (car it)))
  867. ;; It comes from :action ("foo" . function).
  868. (if (stringp (car action))
  869. (car action)
  870. ;; It comes from :persistent-action
  871. ;; (function . 'nosplit) Fix Issue #788.
  872. (if (or (symbolp action)
  873. (functionp action))
  874. (helm-symbol-name action)))))
  875. (t ""))
  876. "")
  877. " (keeping session)")))
  878. (defmethod helm--setup-source :primary ((_source helm-source)))
  879. (defmethod helm--setup-source :before ((source helm-source))
  880. (unless (slot-value source 'group)
  881. (setf (slot-value source 'group) 'helm))
  882. (when (slot-value source 'delayed)
  883. (warn "Deprecated usage of helm `delayed' slot in `%s'"
  884. (slot-value source 'name)))
  885. (helm-aif (slot-value source 'keymap)
  886. (let* ((map (if (symbolp it)
  887. (symbol-value it)
  888. it))
  889. (must-match-map (when (slot-value source 'must-match)
  890. (let ((map (make-sparse-keymap)))
  891. (define-key map (kbd "RET")
  892. 'helm-confirm-and-exit-minibuffer)
  893. map)))
  894. (loc-map (if must-match-map
  895. (make-composed-keymap
  896. must-match-map map)
  897. map)))
  898. (setf (slot-value source 'keymap) loc-map)))
  899. (helm-aif (slot-value source 'persistent-help)
  900. (setf (slot-value source 'header-line)
  901. (helm-source--persistent-help-string it source))
  902. (setf (slot-value source 'header-line) (helm-source--header-line source)))
  903. (when (and (slot-value source 'fuzzy-match) helm-fuzzy-sort-fn)
  904. (setf (slot-value source 'filtered-candidate-transformer)
  905. (helm-aif (slot-value source 'filtered-candidate-transformer)
  906. (append (helm-mklist it)
  907. (list helm-fuzzy-sort-fn))
  908. (list helm-fuzzy-sort-fn))))
  909. (unless (slot-value source 'nohighlight)
  910. (setf (slot-value source 'filtered-candidate-transformer)
  911. (helm-aif (slot-value source 'filtered-candidate-transformer)
  912. (append (helm-mklist it)
  913. (list #'helm-fuzzy-highlight-matches))
  914. (list #'helm-fuzzy-highlight-matches))))
  915. (when (numberp (helm-interpret-value (slot-value source 'multiline)))
  916. (setf (slot-value source 'filtered-candidate-transformer)
  917. (helm-aif (slot-value source 'filtered-candidate-transformer)
  918. (append (helm-mklist it)
  919. (list #'helm-multiline-transformer))
  920. (list #'helm-multiline-transformer)))))
  921. (defmethod helm-setup-user-source ((_source helm-source)))
  922. (defmethod helm--setup-source ((source helm-source-sync))
  923. (when (slot-value source 'fuzzy-match)
  924. (helm-aif (slot-value source 'match)
  925. (setf (slot-value source 'match)
  926. (append (helm-mklist it)
  927. (list helm-fuzzy-match-fn)))
  928. (setf (slot-value source 'match) helm-fuzzy-match-fn)))
  929. (when (slot-value source 'multimatch)
  930. (setf (slot-value source 'match)
  931. (helm-source-mm-get-search-or-match-fns source 'match)))
  932. (helm-aif (and (null (slot-value source 'multimatch))
  933. (slot-value source 'migemo))
  934. (unless (eq it 'nomultimatch) ; Use own migemo fn.
  935. (setf (slot-value source 'match)
  936. (append (helm-mklist (slot-value source 'match))
  937. '(helm-mm-3-migemo-match)))))
  938. (when (slot-value source 'match-dynamic)
  939. (setf (slot-value source 'match) 'identity)
  940. (setf (slot-value source 'match-part) nil)
  941. (setf (slot-value source 'multimatch) nil)
  942. (setf (slot-value source 'fuzzy-match) nil)
  943. (setf (slot-value source 'volatile) t)))
  944. (defmethod helm--setup-source ((source helm-source-in-buffer))
  945. (cl-assert (eq (slot-value source 'candidates) 'helm-candidates-in-buffer)
  946. nil
  947. (format "Wrong usage of `candidates' attr in `%s' use `data' or `init' instead"
  948. (slot-value source 'name)))
  949. (let ((cur-init (slot-value source 'init)))
  950. (helm-aif (slot-value source 'data)
  951. (setf (slot-value source 'init)
  952. (delq
  953. nil
  954. (list
  955. (and (null (eq 'helm-default-init-source-in-buffer-function
  956. cur-init))
  957. cur-init)
  958. (lambda ()
  959. (helm-init-candidates-in-buffer
  960. 'global
  961. (cond ((functionp it) (funcall it))
  962. ((and (bufferp it) (buffer-live-p it))
  963. (with-current-buffer it (buffer-string)))
  964. (t it)))))))))
  965. (when (slot-value source 'fuzzy-match)
  966. (helm-aif (slot-value source 'search)
  967. (setf (slot-value source 'search)
  968. (append (helm-mklist it)
  969. (list helm-fuzzy-search-fn)))
  970. (setf (slot-value source 'search) (list helm-fuzzy-search-fn))))
  971. (when (slot-value source 'multimatch)
  972. (setf (slot-value source 'search)
  973. (helm-source-mm-get-search-or-match-fns source 'search)))
  974. (helm-aif (and (null (slot-value source 'multimatch))
  975. (slot-value source 'migemo))
  976. (unless (eq it 'nomultimatch)
  977. (setf (slot-value source 'search)
  978. (append (helm-mklist (slot-value source 'search))
  979. '(helm-mm-3-migemo-search)))))
  980. (let ((mtc (slot-value source 'match)))
  981. (cl-assert (or (equal '(identity) mtc)
  982. (eq 'identity mtc))
  983. nil "Invalid slot value for `match'")
  984. (cl-assert (eq (slot-value source 'volatile) t)
  985. nil "Invalid slot value for `volatile'")))
  986. (defmethod helm--setup-source ((source helm-source-async))
  987. (cl-assert (null (slot-value source 'candidates))
  988. nil "Incorrect use of `candidates' use `candidates-process' instead")
  989. (cl-assert (null (slot-value source 'multimatch))
  990. nil "`multimatch' not allowed in async sources.")
  991. (cl-assert (null (slot-value source 'fuzzy-match))
  992. nil "`fuzzy-match' not supported in async sources."))
  993. (defmethod helm--setup-source ((source helm-source-dummy))
  994. (let ((mtc (slot-value source 'match)))
  995. (cl-assert (or (equal '(identity) mtc)
  996. (eq 'identity mtc))
  997. nil "Invalid slot value for `match'")
  998. (cl-assert (eq (slot-value source 'volatile) t)
  999. nil "Invalid slot value for `volatile'")
  1000. (cl-assert (equal (slot-value source 'candidates) '("dummy"))
  1001. nil "Invalid slot value for `candidates'")
  1002. (cl-assert (eq (slot-value source 'accept-empty) t)
  1003. nil "Invalid slot value for `accept-empty'")))
  1004. ;;; User functions
  1005. ;;
  1006. ;; Sources
  1007. (defmacro helm-build-sync-source (name &rest args)
  1008. "Build a synchronous helm source with name NAME.
  1009. Args ARGS are keywords provided by `helm-source-sync'."
  1010. (declare (indent 1))
  1011. `(helm-make-source ,name 'helm-source-sync ,@args))
  1012. (defmacro helm-build-async-source (name &rest args)
  1013. "Build a asynchronous helm source with name NAME.
  1014. Args ARGS are keywords provided by `helm-source-async'."
  1015. (declare (indent 1))
  1016. `(helm-make-source ,name 'helm-source-async ,@args))
  1017. (defmacro helm-build-in-buffer-source (name &rest args)
  1018. "Build a helm source with name NAME using `candidates-in-buffer' method.
  1019. Args ARGS are keywords provided by `helm-source-in-buffer'."
  1020. (declare (indent 1))
  1021. `(helm-make-source ,name 'helm-source-in-buffer ,@args))
  1022. (defmacro helm-build-dummy-source (name &rest args)
  1023. "Build a helm source with name NAME using `dummy' method.
  1024. Args ARGS are keywords provided by `helm-source-dummy'."
  1025. (declare (indent 1))
  1026. `(helm-make-source ,name 'helm-source-dummy ,@args))
  1027. (defmacro helm-build-in-file-source (name file &rest args)
  1028. "Build a helm source with NAME name using `candidates-in-files' method.
  1029. Arg FILE is a filename, the contents of this file will be
  1030. used as candidates in buffer.
  1031. Args ARGS are keywords provided by `helm-source-in-file'."
  1032. (declare (indent 2))
  1033. `(helm-make-source ,name 'helm-source-in-file
  1034. :candidates-file ,file ,@args))
  1035. (provide 'helm-source)
  1036. ;; Local Variables:
  1037. ;; byte-compile-warnings: (not obsolete)
  1038. ;; coding: utf-8
  1039. ;; indent-tabs-mode: nil
  1040. ;; End:
  1041. ;;; helm-source ends here