Emacs config utilizing prelude as a base
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.

4427 lines
167 KiB

  1. ;;; helm.el --- Emacs incremental and narrowing framework
  2. ;; Copyright (C) 2007 Tamas Patrovics
  3. ;; 2008 ~ 2012 rubikitch <rubikitch@ruby-lang.org>
  4. ;; 2011 ~ 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  5. ;; Original Authors: Tamas Patrovics
  6. ;; rubikitch <rubikitch@ruby-lang.org>
  7. ;; This is a fork of `anything.el' created by Tamas Patrovics.
  8. ;; Maintainers: Thierry Volpiatto <thierry.volpiatto@gmail.com>
  9. ;; Le Wang
  10. ;; Keywords: files, frames, help, matching, outlines,
  11. ;; processes, tools, convenience, helm
  12. ;; X-URL: <https://github.com/emacs-helm/helm>
  13. ;; MailingList: <https://groups.google.com/forum/?hl=en&fromgroups#!forum/emacs-anything>
  14. ;;; This file is NOT part of GNU Emacs
  15. ;;; License
  16. ;;
  17. ;; This file is free software; you can redistribute it and/or modify
  18. ;; it under the terms of the GNU General Public License as published by
  19. ;; the Free Software Foundation; either version 2, or (at your option)
  20. ;; any later version.
  21. ;; This file is distributed in the hope that it will be useful,
  22. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ;; GNU General Public License for more details.
  25. ;; You should have received a copy of the GNU General Public License
  26. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  27. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  28. ;; Boston, MA 02110-1301, USA.
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30. ;;
  31. ;;; Autodoc documentation:
  32. ;; ---------------------
  33. ;; * Commands defined here are:
  34. ;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "helm" :docstring t)
  35. ;; `helm-open-last-log'
  36. ;; Open helm log file of last helm session.
  37. ;; `helm'
  38. ;; Main function to execute helm sources.
  39. ;; `helm-resume'
  40. ;; Resurrect previously invoked `helm'.
  41. ;; `helm-resume-window-only'
  42. ;;
  43. ;; `helm-at-point'
  44. ;; Call helm with symbol at point as initial input.
  45. ;; `helm-force-update'
  46. ;; Force recalculation and update of candidates.
  47. ;; `helm-select-action'
  48. ;; Select an action for the currently selected candidate.
  49. ;; `helm-previous-line'
  50. ;; Move selection to the previous line.
  51. ;; `helm-next-line'
  52. ;; Move selection to the next line.
  53. ;; `helm-previous-page'
  54. ;; Move selection back with a pageful.
  55. ;; `helm-next-page'
  56. ;; Move selection forward with a pageful.
  57. ;; `helm-beginning-of-buffer'
  58. ;; Move selection at the top.
  59. ;; `helm-end-of-buffer'
  60. ;; Move selection at the bottom.
  61. ;; `helm-previous-source'
  62. ;; Move selection to the previous source.
  63. ;; `helm-next-source'
  64. ;; Move selection to the next source.
  65. ;; `helm-select-with-prefix-shortcut'
  66. ;; Invoke default action with prefix shortcut.
  67. ;; `helm-select-with-digit-shortcut'
  68. ;; Invoke default action with digit/alphabet shortcut.
  69. ;; `helm-confirm-and-exit-minibuffer'
  70. ;; Maybe ask for confirmation when exiting helm.
  71. ;; `helm-exit-minibuffer'
  72. ;; Select the current candidate by exiting the minibuffer.
  73. ;; `helm-keyboard-quit'
  74. ;; Quit minibuffer in helm.
  75. ;; `helm-help'
  76. ;; Help of `helm'.
  77. ;; `helm-debug-output'
  78. ;; Show all helm-related variables at this time.
  79. ;; `helm-delete-current-selection'
  80. ;; Delete the currently selected item.
  81. ;; `helm-delete-minibuffer-contents'
  82. ;; Same as `delete-minibuffer-contents' but this is a command.
  83. ;; `helm-toggle-resplit-window'
  84. ;; Toggle resplit helm window, vertically or horizontally.
  85. ;; `helm-narrow-window'
  86. ;; Narrow helm window.
  87. ;; `helm-enlarge-window'
  88. ;; Enlarge helm window.
  89. ;; `helm-select-2nd-action'
  90. ;; Select the 2nd action for the currently selected candidate.
  91. ;; `helm-select-3rd-action'
  92. ;; Select the 3rd action for the currently selected candidate.
  93. ;; `helm-select-4th-action'
  94. ;; Select the 4th action for the currently selected candidate.
  95. ;; `helm-select-2nd-action-or-end-of-line'
  96. ;; Select the 2nd action for the currently selected candidate.
  97. ;; `helm-execute-persistent-action'
  98. ;; Perform the associated action ATTR without quitting helm.
  99. ;; `helm-scroll-other-window'
  100. ;; Scroll other window (not *Helm* window) upward.
  101. ;; `helm-scroll-other-window-down'
  102. ;; Scroll other window (not *Helm* window) downward.
  103. ;; `helm-toggle-visible-mark'
  104. ;; Toggle helm visible mark at point.
  105. ;; `helm-display-all-visible-marks'
  106. ;; Show all `helm' visible marks strings.
  107. ;; `helm-next-visible-mark'
  108. ;; Move next helm visible mark.
  109. ;; `helm-prev-visible-mark'
  110. ;; Move previous helm visible mark.
  111. ;; `helm-yank-selection'
  112. ;; Set minibuffer contents to current selection.
  113. ;; `helm-kill-selection-and-quit'
  114. ;; Store current selection to kill ring.
  115. ;; `helm-follow-mode'
  116. ;; If this mode is on, persistent action is executed everytime the cursor is moved.
  117. ;; `helm-migrate-sources'
  118. ;; Help to migrate to new `helm' way.
  119. ;; `helm-describe-helm-attribute'
  120. ;; Display the full documentation of HELM-ATTRIBUTE.
  121. ;; `helm-send-bug-report'
  122. ;; Send a bug report of helm.el.
  123. ;; `helm-send-bug-report-from-helm'
  124. ;; Send a bug report of helm.el in helm session.
  125. ;; * Variables defined here are:
  126. ;; [EVAL] (autodoc-document-lisp-buffer :type 'internal-variable :prefix "helm-" :docstring t)
  127. ;; `helm-version'
  128. ;; Not documented.
  129. ;; `helm-sources'
  130. ;; A list of sources to use with `helm'.
  131. ;; `helm-type-attributes'
  132. ;; It's a list of (TYPE ATTRIBUTES ...).
  133. ;; `helm-enable-shortcuts'
  134. ;; *Whether to use digit/alphabet shortcut to select the first nine matches.
  135. ;; `helm-shortcut-keys-alist'
  136. ;; Not documented.
  137. ;; `helm-display-source-at-screen-top'
  138. ;; *Display candidates at the top of screen.
  139. ;; `helm-candidate-number-limit'
  140. ;; Apply candidate-number-limit attribute value.
  141. ;; `helm-idle-delay'
  142. ;; *Be idle for this many seconds, before updating in delayed sources.
  143. ;; `helm-input-idle-delay'
  144. ;; Be idle for this many seconds, before updating.
  145. ;; `helm-samewindow'
  146. ;; Use current window to show the candidates.
  147. ;; `helm-source-filter'
  148. ;; A list of source names to be displayed.
  149. ;; `helm-map'
  150. ;; Keymap for helm.
  151. ;; `helm-header-face'
  152. ;; *Face for header lines in the helm buffer.
  153. ;; `helm-selection-face'
  154. ;; *Face for currently selected item.
  155. ;; `helm-buffer'
  156. ;; Buffer showing completions.
  157. ;; `helm-action-buffer'
  158. ;; Buffer showing actions.
  159. ;; `helm-selection-overlay'
  160. ;; Overlay used to highlight the currently selected item.
  161. ;; `helm-digit-overlays'
  162. ;; Overlays for digit shortcuts. See `helm-enable-shortcuts'.
  163. ;; `helm-candidate-cache'
  164. ;; Holds the available candidate withing a single helm invocation.
  165. ;; `helm-pattern'
  166. ;; Not documented.
  167. ;; `helm-input'
  168. ;; Not documented.
  169. ;; `helm-async-processes'
  170. ;; List of information about asynchronous processes managed by helm.
  171. ;; `helm-digit-shortcut-count'
  172. ;; Number of digit shortcuts shown in the helm buffer.
  173. ;; `helm-before-initialize-hook'
  174. ;; Run before helm initialization.
  175. ;; `helm-after-initialize-hook'
  176. ;; Run after helm initialization.
  177. ;; `helm-update-hook'
  178. ;; Run after the helm buffer was updated according the new input pattern.
  179. ;; `helm-after-update-hook'
  180. ;; Run after the helm buffer was updated according the new input pattern.
  181. ;; `helm-cleanup-hook'
  182. ;; Run after helm minibuffer is closed.
  183. ;; `helm-select-action-hook'
  184. ;; Run when opening the action buffer.
  185. ;; `helm-before-action-hook'
  186. ;; Run before executing action.
  187. ;; `helm-after-action-hook'
  188. ;; Run after executing action.
  189. ;; `helm-after-persistent-action-hook'
  190. ;; Run after executing persistent action.
  191. ;; `helm-move-selection-before-hook'
  192. ;; Run before moving selection in `helm-buffer'.
  193. ;; `helm-move-selection-after-hook'
  194. ;; Run after moving selection in `helm-buffer'.
  195. ;; `helm-restored-variables'
  196. ;; Variables which are restored after `helm' invocation.
  197. ;; `helm-saved-selection'
  198. ;; Value of the currently selected object when the action list is shown.
  199. ;; `helm-current-prefix-arg'
  200. ;; Record `current-prefix-arg' when exiting minibuffer.
  201. ;; `helm-candidate-separator'
  202. ;; Candidates separator of `multiline' source.
  203. ;; `helm-current-buffer'
  204. ;; Current buffer when `helm' is invoked.
  205. ;; `helm-buffer-file-name'
  206. ;; Variable `buffer-file-name' when `helm' is invoked.
  207. ;; `helm-saved-action'
  208. ;; Saved value of the currently selected action by key.
  209. ;; `helm-last-sources'
  210. ;; OBSOLETE!! Sources of previously invoked `helm'.
  211. ;; `helm-saved-current-source'
  212. ;; Value of the current source when the action list is shown.
  213. ;; `helm-compiled-sources'
  214. ;; Compiled version of `helm-sources'.
  215. ;; `helm-in-persistent-action'
  216. ;; Flag whether in persistent-action or not.
  217. ;; `helm-quick-update'
  218. ;; If non-nil, suppress displaying sources which are out of screen at first.
  219. ;; `helm-last-sources-local'
  220. ;; Buffer local value of `helm-sources'.
  221. ;; `helm-last-buffer'
  222. ;; `helm-buffer' of previously `helm' session.
  223. ;; `helm-save-configuration-functions'
  224. ;; The functions used to restore/save window or frame configurations.
  225. ;; `helm-persistent-action-use-special-display'
  226. ;; If non-nil, use `special-display-function' in persistent action.
  227. ;; `helm-execute-action-at-once-if-one'
  228. ;; Execute default action and exit when only one candidate is remaining.
  229. ;; `helm-quit-if-no-candidate'
  230. ;; Quit when there is no candidates when non--nil.
  231. ;; `helm-scroll-amount'
  232. ;; Scroll amount when scrolling other window in an helm session.
  233. ;; `helm-display-function'
  234. ;; Function to display *helm* buffer.
  235. ;; `helm-delayed-init-executed'
  236. ;; Not documented.
  237. ;; `helm-mode-line-string'
  238. ;; Help string displayed in mode-line in `helm'.
  239. ;; `helm-help-message'
  240. ;; Detailed help message string for `helm'.
  241. ;; `helm-source-in-each-line-flag'
  242. ;; Non-nil means add helm-source text-property in each candidate.
  243. ;; `helm-debug-forms'
  244. ;; Forms to show in `helm-debug-output'.
  245. ;; `helm-debug'
  246. ;; If non-nil, write log message into *Helm Log* buffer.
  247. ;; `helm-test-candidate-list'
  248. ;; Not documented.
  249. ;; `helm-test-mode'
  250. ;; Not documented.
  251. ;; `helm-source-name'
  252. ;; Not documented.
  253. ;; `helm-candidate-buffer-alist'
  254. ;; Not documented.
  255. ;; `helm-check-minibuffer-input-timer'
  256. ;; Not documented.
  257. ;; `helm-match-hash'
  258. ;; Not documented.
  259. ;; `helm-cib-hash'
  260. ;; Not documented.
  261. ;; `helm-tick-hash'
  262. ;; Not documented.
  263. ;; `helm-issued-errors'
  264. ;; Not documented.
  265. ;; `helm-shortcut-keys'
  266. ;; Not documented.
  267. ;; `helm-once-called-functions'
  268. ;; Not documented.
  269. ;; `helm-follow-mode'
  270. ;; If this mode is on, persistent action is executed everytime the cursor is moved.
  271. ;; `helm-let-variables'
  272. ;; Not documented.
  273. ;; `helm-split-window-state'
  274. ;; Not documented.
  275. ;; `helm-selection-point'
  276. ;; Not documented.
  277. ;; `helm-last-log-file'
  278. ;; Not documented.
  279. ;; `helm-compile-source-functions'
  280. ;; Functions to compile elements of `helm-sources' (plug-in).
  281. ;; `helm-quit'
  282. ;; Not documented.
  283. ;; `helm-additional-attributes'
  284. ;; List of all `helm' attributes.
  285. ;; `helm-buffers'
  286. ;; All of `helm-buffer' in most recently used order.
  287. ;; `helm-current-position'
  288. ;; Restore or save current position in `helm-current-buffer'.
  289. ;; `helm-last-frame-or-window-configuration'
  290. ;; Used to store window or frame configuration when helm start.
  291. ;; `helm-reading-pattern'
  292. ;; Whether in `read-string' in helm or not.
  293. ;; `helm-compile-source-functions-default'
  294. ;; Plug-ins this file provides.
  295. ;; `helm-input-local'
  296. ;; Not documented.
  297. ;; `helm-process-delayed-sources-timer'
  298. ;; Not documented.
  299. ;; `helm-mode-line-string-real'
  300. ;; Not documented.
  301. ;; `helm-exit-status'
  302. ;; Flag to inform whether helm have exited or quitted.
  303. ;; `helm-minibuffer-confirm-state'
  304. ;; Not documented.
  305. ;; `helm-types'
  306. ;; Not documented.
  307. ;; `helm-orig-enable-shortcuts'
  308. ;; Not documented.
  309. ;; `helm-persistent-action-display-window'
  310. ;; Return the window that will be used for presistent action.
  311. ;; `helm-visible-mark-face'
  312. ;; Not documented.
  313. ;; `helm-visible-mark-overlays'
  314. ;; Not documented.
  315. ;; `helm-marked-candidates'
  316. ;; Return marked candidates of current source if any.
  317. ;; `helm-maintainer-mail-address'
  318. ;; Not documented.
  319. ;; `helm-bug-report-salutation'
  320. ;; Not documented.
  321. ;; `helm-no-dump-variables'
  322. ;; Variables not to dump in bug report.
  323. ;; *** END auto-documentation
  324. ;; [EVAL] (autodoc-update-all)
  325. ;;; Commentary:
  326. ;;
  327. ;; Start with M-x helm, narrow the list by typing some pattern,
  328. ;; select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter,
  329. ;; left/right moves between sources. With TAB actions can be selected
  330. ;; if the selected candidate has more than one possible action.
  331. ;;
  332. ;; Note that helm.el provides only the framework and some example
  333. ;; configurations for demonstration purposes. See helm-config.el
  334. ;; for practical, polished, easy to use configurations which can be
  335. ;; used to assemble a custom personalized configuration.
  336. ;;
  337. ;; NOTE: What you find on Emacswiki is mostly deprecated and not maintained,
  338. ;; don't complain if you use such code or configuration and something
  339. ;; doesn't work.
  340. ;;
  341. ;; Here is Japanese translation of `helm-sources' attributes. Thanks.
  342. ;; http://d.hatena.ne.jp/sirocco634/20091012/1255336649
  343. ;;; Bug Report:
  344. ;;
  345. ;; If you have problems, send a bug report via C-c C-x C-b in helm session (best)
  346. ;; or M-x helm-send-bug-report outside helm session.
  347. ;; I implemented bug report feature because I want to know your current state.
  348. ;; It helps me to solve problems easily.
  349. ;; The step is:
  350. ;; 0) Setup mail in Emacs, the easiest way is:
  351. ;; (setq user-mail-address "your@mail.address")
  352. ;; (setq user-full-name "Your Full Name")
  353. ;; (setq smtpmail-smtp-server "your.smtp.server.jp")
  354. ;; (setq mail-user-agent 'message-user-agent)
  355. ;; (setq message-send-mail-function 'message-smtpmail-send-it)
  356. ;; 1) Be sure to use the LATEST version of helm.el.
  357. ;; 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
  358. ;; 3) Use Lisp version instead of compiled one: (load "helm.el")
  359. ;; 4) Do it!
  360. ;; 5) If you got an error, please do not close *Backtrace* buffer.
  361. ;; 6) Type C-c C-x C-b (helm session, best!)
  362. ;; or M-x helm-send-bug-report (outside)
  363. ;; then M-x insert-buffer *Backtrace* (if you got error)
  364. ;; 7) Describe the bug using a precise recipe.
  365. ;; 8) Type C-c C-c to send.
  366. ;;
  367. ;; You can also just report bug to:
  368. ;; https://groups.google.com/group/emacs-helm?hl=en
  369. ;; You can extend `helm' by writing plug-ins. As soon as
  370. ;; `helm' is invoked, `helm-sources' is compiled into basic
  371. ;; attributes, then compiled one is used during invocation.
  372. ;;
  373. ;; The oldest built-in plug-in is `type' attribute: appends
  374. ;; appropriate element of `helm-type-attributes'. Second built-in
  375. ;; plug-in is `candidates-in-buffer': selecting a line from candidates
  376. ;; buffer.
  377. ;;
  378. ;; To write a plug-in:
  379. ;; 1. Define a compiler: helm-compile-source--*
  380. ;; 2. Add compier function to `helm-compile-source-functions'.
  381. ;; 3. (optional) Write helper functions.
  382. ;
  383. ;; Tested on Emacs 22/23/24.
  384. ;;
  385. ;;
  386. ;; Thanks to Vagn Johansen for ideas.
  387. ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
  388. ;; Thanks to Tassilo Horn for fixes.
  389. ;; Thanks to Drew Adams for various fixes
  390. ;; Thanks to IMAKADO for candidates-in-buffer idea.
  391. ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
  392. ;;
  393. ;;; (@* "Index")
  394. ;; If you have library `linkd.el', load
  395. ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
  396. ;; navigate around the sections Linkd mode will
  397. ;; highlight this Index. You can get `linkd.el' here:
  398. ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
  399. ;;
  400. ;;; (@* "Tips")
  401. ;;
  402. ;; `helm' accepts keyword arguments. See docstring.
  403. ;; [EVAL IT] (describe-function 'helm)
  404. ;;
  405. ;; `helm-enable-shortcuts' enables us to select candidate easily.
  406. ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
  407. ;; The prefix key is `helm-select-with-prefix-shortcut'.
  408. ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
  409. ;; e.g.
  410. ;; (setq helm-enable-shortcuts 'prefix)
  411. ;; (define-key helm-map \"@\" 'helm-select-with-prefix-shortcut)
  412. ;;
  413. ;; You can edit current selection using `helm-edit-current-selection'.
  414. ;; It is useful after persistent-action.
  415. ;;
  416. ;; For `helm' users, setting `helm-sources' directly and
  417. ;; invoke M-x helm is obsolete way for now. Try M-x
  418. ;; `helm-migrate-sources'!
  419. ;;
  420. ;; If you want to create helm sources, yasnippet would help you.
  421. ;; http://yasnippet.googlecode.com/
  422. ;;
  423. ;; Then get the snippet from
  424. ;; http://www.emacswiki.org/cgi-bin/wiki/download/helm-source.yasnippet
  425. ;;
  426. ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
  427. ;;
  428. ;; `helm-interpret-value' is useful function to interpret value
  429. ;; like `candidates' attribute.
  430. ;;
  431. ;; (helm-interpret-value "literal") ; => "literal"
  432. ;; (helm-interpret-value (lambda () "lambda")) ; => "lambda"
  433. ;; (let ((source '((name . "lambda with source name"))))
  434. ;; (helm-interpret-value
  435. ;; (lambda () helm-source-name)
  436. ;; source)) ; => "lambda with source name"
  437. ;; (flet ((f () "function symbol"))
  438. ;; (helm-interpret-value 'f)) ; => "function symbol"
  439. ;; (let ((v "variable symbol"))
  440. ;; (helm-interpret-value 'v)) ; => "variable symbol"
  441. ;; (helm-interpret-value 'unbounded-1) ; error
  442. ;;
  443. ;; Now symbols are acceptable as candidates. So you do not have to use
  444. ;; `symbol-name' function. The source is much simpler. For example,
  445. ;; `apropos-internal' returns a list of symbols.
  446. ;;
  447. ;; (helm
  448. ;; '(((name . "Commands")
  449. ;; (candidates . (lambda () (apropos-internal helm-pattern 'commandp)))
  450. ;; (volatile)
  451. ;; (action . describe-function))))
  452. ;;
  453. ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
  454. ;; marked candidate, press M-[ or M-].
  455. ;;
  456. ;; `helm-map' is now Emacs-standard key bindings by default.
  457. ;;
  458. ;; There are many `helm' applications, using `helm' for
  459. ;; selecting candidate. In this case, if there is one candidate or no
  460. ;; candidate, popping up *helm* buffer is irritating. If one
  461. ;; candidate, you want to select it at once. If no candidate, you want
  462. ;; to quit `helm'. Set `helm-execute-action-at-once-if-one'
  463. ;; and `helm-quit-if-no-candidate' to non-nil to remedy it. Note
  464. ;; that setting these variables GLOBALLY is bad idea because of
  465. ;; delayed sources. These are meant to be let-binded.
  466. ;;
  467. ;; ex.
  468. ;; (let ((helm-execute-action-at-once-if-one t)
  469. ;; (helm-quit-if-no-candidate (lambda () (message "No candidate"))))
  470. ;; (helm temporary-sources input))
  471. ;;
  472. ;; `set-frame-configuration' arises flickering. If you hate
  473. ;; flickering, eval:
  474. ;; (setq helm-save-configuration-functions
  475. ;; '(set-window-configuration . current-window-configuration))
  476. ;; at the cost of restoring frame configuration (only window configuration).
  477. ;;
  478. ;; `helm-delete-current-selection' deletes the current line.
  479. ;; It is useful when deleting a candidate in persistent action.
  480. ;; eg. `kill-buffer'.
  481. ;;
  482. ;; [EVAL IT] (describe-function 'helm-delete-current-selection)
  483. ;;
  484. ;; `helm-attr' gets the attribute. `helm-attrset' sets the
  485. ;; attribute. `helm-attr-defined' tests whether the attribute is
  486. ;; defined. They handles source-local variables.
  487. ;;
  488. ;; [EVAL IT] (describe-function 'helm-attr)
  489. ;; [EVAL IT] (describe-function 'helm-attrset)
  490. ;; [EVAL IT] (describe-function 'helm-attr-defined)
  491. ;;
  492. ;; `helm-sources' accepts many attributes to make your life easier.
  493. ;; Now `helm-sources' accepts a list of symbols.
  494. ;;
  495. ;; [EVAL IT] (describe-variable 'helm-sources)
  496. ;;
  497. ;; `helm' has optional arguments. Now you do not have to let-bind
  498. ;; `helm-sources'.
  499. ;;
  500. ;; [EVAL IT] (describe-function 'helm)
  501. ;;
  502. ;; `helm-resume' resumes last `helm' session. Now you do not
  503. ;; have to retype pattern.
  504. ;;
  505. ;; [EVAL IT] (describe-function 'helm-resume)
  506. ;;
  507. ;; `helm-execute-persistent-action' executes action without
  508. ;; quitting `helm'. When popping up a buffer in other window by
  509. ;; persistent action, you can scroll with `helm-scroll-other-window' and
  510. ;; `helm-scroll-other-window-down'. See also `helm-sources' docstring.
  511. ;;
  512. ;; [EVAL IT] (describe-function 'helm-execute-persistent-action)
  513. ;; [EVAL IT] (describe-variable 'helm-sources)
  514. ;;
  515. ;; `helm-select-2nd-action', `helm-select-3rd-action' and
  516. ;; `helm-select-4th-action' select other than default action
  517. ;; without pressing Tab.
  518. ;;
  519. ;; Using `helm-candidate-buffer' and the candidates-in-buffer
  520. ;; attribute is much faster than traditional "candidates and match"
  521. ;; way. And `helm-current-buffer-is-modified' avoids to
  522. ;; recalculate candidates for unmodified buffer. See docstring of
  523. ;; them.
  524. ;;
  525. ;; [EVAL IT] (describe-function 'helm-candidate-buffer)
  526. ;; [EVAL IT] (describe-function 'helm-candidates-in-buffer)
  527. ;; [EVAL IT] (describe-function 'helm-current-buffer-is-modified)
  528. ;;
  529. ;; `helm-current-buffer' and `helm-buffer-file-name' stores
  530. ;; `(current-buffer)' and `buffer-file-name' in the buffer `helm'
  531. ;; is invoked. Use them freely.
  532. ;;
  533. ;; [EVAL IT] (describe-variable 'helm-current-buffer)
  534. ;; [EVAL IT] (describe-variable 'helm-buffer-file-name)
  535. ;;
  536. ;; `helm-completing-read' and `helm-read-file-name' are
  537. ;; experimental implementation. If you are curious, type M-x
  538. ;; helm-read-string-mode. It is a minor mode and toggles on/off.
  539. ;;
  540. ;; Use `helm-test-candidates' to test your handmade helm
  541. ;; sources. It simulates contents of *helm* buffer with pseudo
  542. ;; `helm-sources' and `helm-pattern', without side-effect. So
  543. ;; you can unit-test your helm sources! Let's TDD!
  544. ;;
  545. ;; [EVAL IT] (describe-function 'helm-test-candidates)
  546. ;;
  547. ;; For helm developpers:
  548. ;;
  549. ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
  550. ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
  551. ;; There is an unit-test by Emacs Lisp Expectations in developper-tools directory.
  552. ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
  553. ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
  554. ;;
  555. ;; If you want to create helm sources, see helm-config.el.
  556. ;; It is huge collection of sources. You can learn from examples.
  557. ;; (@* "TODO")
  558. ;;
  559. ;; - process status indication
  560. ;;
  561. ;; - async sources doesn't honor digit-shortcut-count
  562. ;;
  563. ;; - helm-candidate-number-limit can't be nil everywhere
  564. ;; (@* "HISTORY")
  565. ;;
  566. ;; Change log of this file is found at
  567. ;; http://repo.or.cz/w/helm-config.git/history/master:/helm.el
  568. ;;
  569. ;; Change log of this project is found at
  570. ;; http://repo.or.cz/w/helm-config.git?a=shortlog
  571. ;;
  572. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  573. ;;; Code:
  574. (require 'cl)
  575. (defvar helm-version "1.3.9")
  576. ;; (@* "User Configuration")
  577. ;;; Variables
  578. ;;
  579. ;;
  580. ;; [DEPRECATED]
  581. ;; A default value is provided in helm-config.el
  582. (defvar helm-sources nil
  583. "A list of sources to use with `helm'.
  584. It is deprecated, you should not use this.
  585. Use instead individual sources or list of sources of your choice.")
  586. ;; Default values are provided in helm-config.el.
  587. (defvar helm-type-attributes nil
  588. "It's a list of \(TYPE ATTRIBUTES ...\).
  589. ATTRIBUTES are the same as attributes for `helm-sources'.
  590. TYPE connects the value to the appropriate sources.
  591. Don't set this directly, use instead `define-helm-type-attribute'.
  592. This allows specifying common attributes for several sources.
  593. For example, sources which provide files can specify
  594. common attributes with a `file' type.")
  595. (defvaralias 'helm-enable-digit-shortcuts 'helm-enable-shortcuts
  596. "Same as `helm-enable-shortcuts'.
  597. Alphabet shortcuts are available now in `helm-enable-shortcuts'.
  598. `helm-enable-digit-shortcuts' is retained for compatibility.")
  599. (defvar helm-enable-shortcuts nil
  600. "*Whether to use digit/alphabet shortcut to select the first nine matches.
  601. If t then they can be selected using Ctrl+<number>.
  602. If 'prefix then they can be selected using <prefix-key> <alnum>.
  603. The prefix key is `helm-select-with-prefix-shortcut'.
  604. If the <prefix-key> is a letter, pressing twice inputs the letter itself.
  605. e.g.
  606. (setq helm-enable-shortcuts 'prefix)
  607. (define-key helm-map \"@\" 'helm-select-with-prefix-shortcut)
  608. If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
  609. It is not recommended because you cannot input capital letters in pattern.
  610. Keys (digit/alphabet) are listed in `helm-shortcut-keys-alist'.")
  611. (defvar helm-shortcut-keys-alist
  612. '((alphabet . "asdfghjklzxcvbnmqwertyuiop")
  613. (prefix . "asdfghjklzxcvbnmqwertyuiop1234567890")
  614. (t . "123456789")))
  615. (defvar helm-display-source-at-screen-top t
  616. "*Display candidates at the top of screen.
  617. This happen when using `helm-next-source' and `helm-previous-source'.")
  618. (defvar helm-candidate-number-limit 50
  619. "*Limit candidate number globally.
  620. Do not show more candidates than this limit from individual sources.
  621. It is usually pointless to show hundreds of matches
  622. when the pattern is empty, because it is much simpler to type a
  623. few characters to narrow down the list of potential candidates.
  624. Set it to nil if you don't want this limit.")
  625. (defvar helm-idle-delay 0.3
  626. "*Be idle for this many seconds, before updating in delayed sources.
  627. This is useful for sources involving heavy operations
  628. \(like launching external programs\), so that candidates
  629. from the source are not retrieved unnecessarily if the user keeps typing.
  630. It also can be used to declutter the results helm displays,
  631. so that results from certain sources are not shown with every
  632. character typed, only if the user hesitates a bit.")
  633. (defvar helm-input-idle-delay 0.3
  634. "Be idle for this many seconds, before updating.
  635. Unlike `helm-idle-delay', it is also effective for non-delayed sources.
  636. If nil, candidates are collected immediately.
  637. Note: If this value is too low compared to `helm-idle-delay',
  638. you may have duplicated sources when using multiples sources.
  639. Safe value is always >= `helm-idle-delay'.
  640. Default settings are equal value for both.")
  641. (defvar helm-samewindow nil
  642. "Use current window to show the candidates.
  643. If t then Helm doesn't pop up a new window.")
  644. (defvar helm-source-filter nil
  645. "A list of source names to be displayed.
  646. Other sources won't appear in the search results.
  647. If nil then there is no filtering.
  648. See also `helm-set-source-filter'.")
  649. (defvar helm-map
  650. (let ((map (copy-keymap minibuffer-local-map)))
  651. (define-key map (kbd "<down>") 'helm-next-line)
  652. (define-key map (kbd "<up>") 'helm-previous-line)
  653. (define-key map (kbd "C-n") 'helm-next-line)
  654. (define-key map (kbd "C-p") 'helm-previous-line)
  655. (define-key map (kbd "<prior>") 'helm-previous-page)
  656. (define-key map (kbd "<next>") 'helm-next-page)
  657. (define-key map (kbd "M-v") 'helm-previous-page)
  658. (define-key map (kbd "C-v") 'helm-next-page)
  659. (define-key map (kbd "M-<") 'helm-beginning-of-buffer)
  660. (define-key map (kbd "M->") 'helm-end-of-buffer)
  661. (define-key map (kbd "C-g") 'helm-keyboard-quit)
  662. (define-key map (kbd "<right>") 'helm-next-source)
  663. (define-key map (kbd "<left>") 'helm-previous-source)
  664. (define-key map (kbd "<RET>") 'helm-exit-minibuffer)
  665. (define-key map (kbd "C-1") 'helm-select-with-digit-shortcut)
  666. (define-key map (kbd "C-2") 'helm-select-with-digit-shortcut)
  667. (define-key map (kbd "C-3") 'helm-select-with-digit-shortcut)
  668. (define-key map (kbd "C-4") 'helm-select-with-digit-shortcut)
  669. (define-key map (kbd "C-5") 'helm-select-with-digit-shortcut)
  670. (define-key map (kbd "C-6") 'helm-select-with-digit-shortcut)
  671. (define-key map (kbd "C-7") 'helm-select-with-digit-shortcut)
  672. (define-key map (kbd "C-8") 'helm-select-with-digit-shortcut)
  673. (define-key map (kbd "C-9") 'helm-select-with-digit-shortcut)
  674. (loop for c from ?A to ?Z do
  675. (define-key map (make-string 1 c) 'helm-select-with-digit-shortcut))
  676. (define-key map (kbd "C-i") 'helm-select-action)
  677. (define-key map (kbd "C-z") 'helm-execute-persistent-action)
  678. (define-key map (kbd "C-e") 'helm-select-2nd-action-or-end-of-line)
  679. (define-key map (kbd "C-j") 'helm-select-3rd-action)
  680. (define-key map (kbd "C-o") 'helm-next-source)
  681. (define-key map (kbd "C-M-v") 'helm-scroll-other-window)
  682. (define-key map (kbd "M-<next>") 'helm-scroll-other-window)
  683. (define-key map (kbd "C-M-y") 'helm-scroll-other-window-down)
  684. (define-key map (kbd "C-M-S-v") 'helm-scroll-other-window-down)
  685. (define-key map (kbd "M-<prior>") 'helm-scroll-other-window-down)
  686. (define-key map (kbd "<C-M-down>") 'helm-scroll-other-window)
  687. (define-key map (kbd "<C-M-up>") 'helm-scroll-other-window-down)
  688. (define-key map (kbd "C-SPC") 'helm-toggle-visible-mark)
  689. (define-key map (kbd "M-SPC") 'helm-toggle-visible-mark)
  690. (define-key map (kbd "M-[") 'helm-prev-visible-mark)
  691. (define-key map (kbd "M-]") 'helm-next-visible-mark)
  692. (define-key map (kbd "C-k") 'helm-delete-minibuffer-contents)
  693. (define-key map (kbd "C-r") 'undefined)
  694. (define-key map (kbd "C-t") 'helm-toggle-resplit-window)
  695. (define-key map (kbd "C-}") 'helm-narrow-window)
  696. (define-key map (kbd "C-{") 'helm-enlarge-window)
  697. (define-key map (kbd "C-c C-d") 'helm-delete-current-selection)
  698. (define-key map (kbd "C-c C-y") 'helm-yank-selection)
  699. (define-key map (kbd "C-c C-k") 'helm-kill-selection-and-quit)
  700. (define-key map (kbd "C-c C-f") 'helm-follow-mode)
  701. (define-key map (kbd "C-c C-u") 'helm-force-update)
  702. (define-key map (kbd "M-p") 'previous-history-element)
  703. (define-key map (kbd "M-n") 'next-history-element)
  704. ;; Debugging command
  705. (define-key map "\C-c\C-x\C-d" 'helm-debug-output)
  706. (define-key map "\C-c\C-x\C-m" 'helm-display-all-visible-marks)
  707. (define-key map "\C-c\C-x\C-b" 'helm-send-bug-report-from-helm)
  708. ;; Use `describe-mode' key in `global-map'.
  709. (define-key map [f1] nil) ; Allow to eval keymap whithout errors.
  710. (dolist (k (where-is-internal 'describe-mode global-map))
  711. (define-key map k 'helm-help))
  712. map)
  713. "Keymap for helm.")
  714. (defgroup helm nil
  715. "Open helm."
  716. :prefix "helm-" :group 'convenience)
  717. (defface helm-header
  718. '((t (:inherit header-line)))
  719. "Face for header lines in the helm buffer."
  720. :group 'helm)
  721. (defvar helm-header-face 'helm-header
  722. "*Face for header lines in the helm buffer.")
  723. (defface helm-candidate-number
  724. '((t (:background "Yellow" :foreground "black")))
  725. "Face for candidate number in mode-line." :group 'helm)
  726. (defvar helm-selection-face 'highlight
  727. "*Face for currently selected item.")
  728. (defvar helm-buffer "*helm*"
  729. "Buffer showing completions.")
  730. (defvar helm-action-buffer "*helm action*"
  731. "Buffer showing actions.")
  732. (defvar helm-selection-overlay nil
  733. "Overlay used to highlight the currently selected item.")
  734. (defvar helm-digit-overlays nil
  735. "Overlays for digit shortcuts. See `helm-enable-shortcuts'.")
  736. (defvar helm-candidate-cache nil
  737. "Holds the available candidate withing a single helm invocation.")
  738. (defvar helm-pattern
  739. "The input pattern used to update the helm buffer.")
  740. (defvar helm-input
  741. "The input typed in the candidates panel.")
  742. (defvar helm-async-processes nil
  743. "List of information about asynchronous processes managed by helm.")
  744. (defvar helm-digit-shortcut-count 0
  745. "Number of digit shortcuts shown in the helm buffer.")
  746. (defvar helm-before-initialize-hook nil
  747. "Run before helm initialization.
  748. This hook is run before init functions in `helm-sources'.")
  749. (defvar helm-after-initialize-hook nil
  750. "Run after helm initialization.
  751. Global variables are initialized and the helm buffer is created.
  752. But the helm buffer has no contents.")
  753. (defvar helm-update-hook nil
  754. "Run after the helm buffer was updated according the new input pattern.
  755. This hook is run at the beginning of buffer.
  756. The first candidate is selected after running this hook.
  757. See also `helm-after-update-hook'.")
  758. (defvar helm-after-update-hook nil
  759. "Run after the helm buffer was updated according the new input pattern.
  760. This is very similar to `helm-update-hook' but selection is not moved.
  761. It is useful to select a particular object instead of the first one.")
  762. (defvar helm-cleanup-hook nil
  763. "Run after helm minibuffer is closed.
  764. IOW this hook is executed BEFORE performing action.")
  765. (defvar helm-select-action-hook nil
  766. "Run when opening the action buffer.")
  767. (defvar helm-before-action-hook nil
  768. "Run before executing action.
  769. Contrarily to `helm-cleanup-hook',
  770. this hook run before helm minibuffer is closed
  771. and before performing action.")
  772. (defvar helm-after-action-hook nil
  773. "Run after executing action.")
  774. (defvar helm-after-persistent-action-hook nil
  775. "Run after executing persistent action.")
  776. (defvar helm-move-selection-before-hook nil
  777. "Run before moving selection in `helm-buffer'.")
  778. (defvar helm-move-selection-after-hook nil
  779. "Run after moving selection in `helm-buffer'.")
  780. (defvar helm-restored-variables
  781. '(helm-candidate-number-limit
  782. helm-source-filter
  783. helm-source-in-each-line-flag
  784. helm-map
  785. helm-sources)
  786. "Variables which are restored after `helm' invocation.")
  787. (defvar helm-saved-selection nil
  788. "Value of the currently selected object when the action list is shown.")
  789. (defvar helm-current-prefix-arg nil
  790. "Record `current-prefix-arg' when exiting minibuffer.")
  791. (defvar helm-candidate-separator
  792. "--------------------"
  793. "Candidates separator of `multiline' source.")
  794. (defvar helm-current-buffer nil
  795. "Current buffer when `helm' is invoked.")
  796. (defvar helm-buffer-file-name nil
  797. "Variable `buffer-file-name' when `helm' is invoked.")
  798. (defvar helm-saved-action nil
  799. "Saved value of the currently selected action by key.")
  800. (defvar helm-last-sources nil
  801. "OBSOLETE!! Sources of previously invoked `helm'.")
  802. (defvar helm-saved-current-source nil
  803. "Value of the current source when the action list is shown.")
  804. (defvar helm-compiled-sources nil
  805. "Compiled version of `helm-sources'.")
  806. (defvar helm-in-persistent-action nil
  807. "Flag whether in persistent-action or not.")
  808. (defvar helm-quick-update nil
  809. "If non-nil, suppress displaying sources which are out of screen at first.
  810. They are treated as delayed sources at this input.
  811. This flag makes `helm' a bit faster with many sources.")
  812. (defvar helm-last-sources-local nil
  813. "Buffer local value of `helm-sources'.")
  814. (defvar helm-last-buffer nil
  815. "`helm-buffer' of previously `helm' session.")
  816. (defvar helm-save-configuration-functions
  817. '(set-window-configuration . current-window-configuration)
  818. "The functions used to restore/save window or frame configurations.
  819. It is a pair where the car is the function to restore window or frame config,
  820. and the cdr is the function to save the window or frame config.
  821. If you want to save and restore frame configuration, set this variable to
  822. '\(set-frame-configuration . current-frame-configuration\)
  823. Older version saves/restores frame configuration, but the default is changed now
  824. because flickering can occur in some environment. ")
  825. (defvar helm-persistent-action-use-special-display nil
  826. "If non-nil, use `special-display-function' in persistent action.")
  827. (defvar helm-execute-action-at-once-if-one nil
  828. "Execute default action and exit when only one candidate is remaining.
  829. It is useful for `helm' applications.")
  830. (defvar helm-quit-if-no-candidate nil
  831. "Quit when there is no candidates when non--nil.
  832. This variable accepts a function, which is executed if no candidate.
  833. It is useful for `helm' applications.")
  834. (defvar helm-scroll-amount nil
  835. "Scroll amount when scrolling other window in an helm session.
  836. It is used by `helm-scroll-other-window'
  837. and `helm-scroll-other-window-down'.
  838. If you prefer scrolling line by line, set this value to 1.")
  839. (defvar helm-display-function 'helm-default-display-buffer
  840. "Function to display *helm* buffer.
  841. It is `helm-default-display-buffer' by default,
  842. which affects `helm-samewindow'.")
  843. (defvar helm-delayed-init-executed nil)
  844. (defvar helm-mode-line-string "\\<helm-map>\\[helm-help]:help \
  845. \\[helm-select-action]:Acts \
  846. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  847. \\[helm-select-3rd-action]:NthAct \
  848. \\[helm-send-bug-report-from-helm]:BugReport"
  849. "Help string displayed in mode-line in `helm'.
  850. It can be a string or a list of two args, in this case,
  851. first arg is a string that will be used as name for candidates number,
  852. second arg any string to display in mode line.
  853. If nil, use default `mode-line-format'.")
  854. (defvar helm-help-message
  855. "\\<helm-map>The keys that are defined for `helm' are:
  856. \\{helm-map}"
  857. "Detailed help message string for `helm'.
  858. It also accepts function or variable symbol.")
  859. (defvar helm-source-in-each-line-flag nil
  860. "Non-nil means add helm-source text-property in each candidate.
  861. experimental feature.")
  862. (defvaralias 'helm-debug-variables 'helm-debug-forms)
  863. (defvar helm-debug-forms nil
  864. "Forms to show in `helm-debug-output'.
  865. Otherwise all variables started with `helm-' are shown.
  866. It is useful for debug.")
  867. (defvar helm-debug nil
  868. "If non-nil, write log message into *Helm Log* buffer.
  869. If `debug-on-error' is non-nil, write log message regardless of this variable.
  870. It is disabled by default because *Helm Log* grows quickly.")
  871. ;; (@* "Internal Variables")
  872. (defvar helm-test-candidate-list nil)
  873. (defvar helm-test-mode nil)
  874. (defvar helm-source-name nil)
  875. (defvar helm-candidate-buffer-alist nil)
  876. (defvar helm-check-minibuffer-input-timer nil)
  877. (defvar helm-match-hash (make-hash-table :test 'equal))
  878. (defvar helm-cib-hash (make-hash-table :test 'equal))
  879. (defvar helm-tick-hash (make-hash-table :test 'equal))
  880. (defvar helm-issued-errors nil)
  881. (defvar helm-shortcut-keys nil)
  882. (defvar helm-once-called-functions nil)
  883. (defvar helm-follow-mode nil)
  884. (defvar helm-let-variables nil)
  885. (defvar helm-split-window-state nil)
  886. (defvar helm-selection-point nil)
  887. (defvar helm-alive-p nil)
  888. ;; (@* "Utility: logging")
  889. (defun helm-log (format-string &rest args)
  890. "Log message if `debug-on-error' or `helm-debug' is non-nil.
  891. Messages are written to the *Helm Log* buffer.
  892. Argument FORMAT-STRING is a string to use with `format'.
  893. Use optional arguments ARGS like in `format'."
  894. (when (or debug-on-error helm-debug)
  895. (with-current-buffer (get-buffer-create "*Helm Log*")
  896. (buffer-disable-undo)
  897. (set (make-local-variable 'inhibit-read-only) t)
  898. (goto-char (point-max))
  899. (insert (let ((tm (current-time)))
  900. (format "%s.%06d (%s) %s\n"
  901. (format-time-string "%H:%M:%S" tm)
  902. (nth 2 tm)
  903. (helm-log-get-current-function)
  904. (apply #'format (cons format-string args))))))))
  905. (defmacro helm-log-eval (&rest exprs)
  906. "Write each EXPRS evaluation result to the *Helm Log* buffer."
  907. `(helm-log-eval-internal ',exprs))
  908. (defun helm-log-run-hook (hook)
  909. "Run HOOK like `run-hooks' but write these actions to helm log buffer."
  910. (helm-log "executing %s" hook)
  911. (when (boundp hook)
  912. (helm-log-eval (symbol-value hook))
  913. (helm-log-eval (default-value hook)))
  914. (run-hooks hook)
  915. (helm-log "executed %s" hook))
  916. (defun helm-log-eval-internal (exprs)
  917. "Eval EXPRS and write results to helm log buffer."
  918. (dolist (expr exprs)
  919. (condition-case err
  920. (helm-log "%S = %S" expr (eval expr))
  921. (error (helm-log "%S = ERROR!" expr)))))
  922. (defun helm-log-get-current-function ()
  923. "Get function name calling `helm-log'.
  924. The original idea is from `tramp-debug-message'."
  925. (loop with exclude-func-re = "^helm-\\(?:interpret\\|log\\|.*funcall\\)"
  926. for btn from 1 to 40 ;avoid inf-loop
  927. for btf = (second (backtrace-frame btn))
  928. for fn = (if (symbolp btf) (symbol-name btf) "")
  929. if (and (string-match "^helm" fn)
  930. (not (string-match exclude-func-re fn)))
  931. return fn))
  932. (defun helm-log-error (&rest args)
  933. "Accumulate error messages into `helm-issued-errors'.
  934. ARGS are args given to `format'."
  935. (apply 'helm-log (concat "ERROR: " (car args)) (cdr args))
  936. (let ((msg (apply 'format args)))
  937. (unless (member msg helm-issued-errors)
  938. (add-to-list 'helm-issued-errors msg))))
  939. (defvar helm-last-log-file nil)
  940. (defun helm-log-save-maybe ()
  941. "May be save log buffer to `helm-last-log-file'."
  942. (when (stringp helm-debug)
  943. (let ((logdir (expand-file-name (format-time-string "%Y%m%d")
  944. helm-debug)))
  945. (make-directory logdir t)
  946. (with-current-buffer (get-buffer-create "*Helm Log*")
  947. (write-region (point-min) (point-max)
  948. (setq helm-last-log-file
  949. (expand-file-name (format-time-string "%Y%m%d-%H%M%S")
  950. logdir))
  951. nil 'silent)
  952. (erase-buffer)))))
  953. (defun helm-open-last-log ()
  954. "Open helm log file of last helm session."
  955. (interactive)
  956. (if helm-last-log-file
  957. (view-file helm-last-log-file)
  958. (switch-to-buffer "*Helm Log*")))
  959. (defun helm-print-error-messages ()
  960. "Print error messages in `helm-issued-errors'."
  961. (message "%s" (mapconcat 'identity (reverse helm-issued-errors) "\n")))
  962. ;; (helm-log "test")
  963. ;; (switch-to-buffer-other-window "*Helm Log*")
  964. ;; (@* "Programming Tools")
  965. (defmacro helm-aif (test-form then-form &rest else-forms)
  966. "Like `if' but set the result of TEST-FORM in a temprary variable called `it'.
  967. THEN-FORM and ELSE-FORMS are then excuted just like in `if'."
  968. (declare (indent 2) (debug t))
  969. `(let ((it ,test-form))
  970. (if it ,then-form ,@else-forms)))
  971. (defun helm-mklist (obj)
  972. "If OBJ is a list \(but not lambda\), return itself.
  973. Otherwise make a list with one element."
  974. (if (and (listp obj) (not (functionp obj)))
  975. obj
  976. (list obj)))
  977. ;; (@* "Helm API")
  978. (defun helm-buffer-get ()
  979. "Return `helm-action-buffer' if shown otherwise `helm-buffer'."
  980. (if (helm-action-window)
  981. helm-action-buffer
  982. helm-buffer))
  983. (defun helm-window ()
  984. "Window of `helm-buffer'."
  985. (get-buffer-window (helm-buffer-get) 'visible))
  986. (defun helm-action-window ()
  987. "Window of `helm-action-buffer'."
  988. (get-buffer-window helm-action-buffer 'visible))
  989. (defmacro with-helm-window (&rest body)
  990. "Be sure BODY is excuted in the helm window."
  991. (declare (indent 0) (debug t))
  992. `(if helm-test-mode
  993. (with-current-buffer (helm-buffer-get)
  994. ,@body)
  995. (with-selected-window (helm-window)
  996. ,@body)))
  997. (defmacro with-helm-current-buffer (&rest body)
  998. "Eval BODY inside `helm-current-buffer'."
  999. (declare (indent 0) (debug t))
  1000. `(with-current-buffer helm-current-buffer
  1001. ,@body))
  1002. (defmacro with-helm-restore-variables(&rest body)
  1003. "Restore `helm-restored-variables' after executing BODY.
  1004. `post-command-hook' is handled specially."
  1005. (declare (indent 0) (debug t))
  1006. `(let ((--orig-vars (mapcar (lambda (v)
  1007. (cons v (symbol-value v)))
  1008. helm-restored-variables))
  1009. (--post-command-hook-pair (cons post-command-hook
  1010. (default-value 'post-command-hook))))
  1011. (setq post-command-hook '(t))
  1012. (setq-default post-command-hook nil)
  1013. (unwind-protect (progn ,@body)
  1014. (loop for (var . value) in --orig-vars
  1015. do (set var value))
  1016. (setq post-command-hook (car --post-command-hook-pair))
  1017. (setq-default post-command-hook (cdr --post-command-hook-pair))
  1018. (helm-log "restore variables"))))
  1019. (defun* helm-attr (attribute-name &optional
  1020. (src (helm-get-current-source)))
  1021. "Get the value of ATTRIBUTE-NAME of SRC.
  1022. if SRC is omitted, use current source.
  1023. It is useful to write your sources."
  1024. (helm-aif (assq attribute-name src)
  1025. (cdr it)))
  1026. (defun* helm-attr* (attribute-name
  1027. &optional (src (helm-get-current-source)))
  1028. "Pass the value of ATTRIBUTE-NAME of SRC to `helm-interpret-value'.
  1029. if SRC is omitted, use current source.
  1030. It is useful to write your sources."
  1031. (helm-interpret-value (helm-attr attribute-name src)))
  1032. (defun* helm-attr-defined (attribute-name
  1033. &optional (src (helm-get-current-source)))
  1034. "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
  1035. if SRC is omitted, use current source.
  1036. It is useful to write your sources."
  1037. (and (assq attribute-name src) t))
  1038. (defun* helm-attrset (attribute-name value
  1039. &optional
  1040. (src (helm-get-current-source)))
  1041. "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
  1042. if SRC is omitted, use current source.
  1043. It is useful to write your sources."
  1044. (helm-aif (assq attribute-name src)
  1045. (setcdr it value)
  1046. (setcdr src (cons (cons attribute-name value) (cdr src))))
  1047. value)
  1048. (defun helm-set-source-filter (sources)
  1049. "Set the value of `helm-source-filter' to SOURCES and update.
  1050. This function sets a filter for helm sources and it may be
  1051. called while helm is running. It can be used to toggle
  1052. displaying of sources dinamically. For example, additional keys
  1053. can be bound into `helm-map' to display only the file-related
  1054. results if there are too many matches from other sources and
  1055. you're after files only:
  1056. Shift+F shows only file results from some sources:
  1057. \(define-key helm-map \"F\" 'helm-my-show-files-only)
  1058. \(defun helm-my-show-files-only ()
  1059. (interactive)
  1060. (helm-set-source-filter '(\"File Name History\"
  1061. \"Files from Current Directory\")))
  1062. Shift+A shows all results:
  1063. \(define-key helm-map \"A\" 'helm-my-show-all)
  1064. \(defun helm-my-show-all ()
  1065. (interactive)
  1066. (helm-set-source-filter nil))
  1067. Note that you have to prefix the functions with helm- prefix,
  1068. otherwise they won't be bound when Helm is used under
  1069. Iswitchb. The -my- part is added to avoid collisions with
  1070. existing Helm function names."
  1071. (unless (and (listp sources)
  1072. (loop for name in sources always (stringp name)))
  1073. (error "Invalid data in `helm-set-source-filter': %S" sources))
  1074. (setq helm-source-filter sources)
  1075. (helm-log-eval helm-source-filter)
  1076. (helm-update))
  1077. (defun helm-set-sources (sources &optional no-init no-update)
  1078. "Set SOURCES during `helm' invocation.
  1079. If NO-INIT is non-nil, skip executing init functions of SOURCES.
  1080. If NO-UPDATE is non-nil, skip executing `helm-update'."
  1081. (with-current-buffer helm-buffer
  1082. (setq helm-compiled-sources nil
  1083. helm-sources sources
  1084. helm-last-sources-local sources)
  1085. (helm-log-eval helm-compiled-sources helm-sources))
  1086. (unless no-init (helm-funcall-foreach 'init))
  1087. (unless no-update (helm-update)))
  1088. (defvar helm-compile-source-functions
  1089. '(helm-compile-source--type
  1090. helm-compile-source--dummy
  1091. helm-compile-source--disable-shortcuts
  1092. helm-compile-source--candidates-in-buffer)
  1093. "Functions to compile elements of `helm-sources' (plug-in).")
  1094. (defun helm-get-sources ()
  1095. "Return compiled `helm-sources', which is memoized.
  1096. Attributes:
  1097. - type
  1098. `helm-type-attributes' are merged in.
  1099. - candidates-buffer
  1100. candidates, volatile and match attribute are created."
  1101. (cond
  1102. ;; action
  1103. ((helm-action-window)
  1104. helm-sources)
  1105. ;; memoized
  1106. (helm-compiled-sources)
  1107. ;; first time
  1108. (t
  1109. (prog1
  1110. (setq helm-compiled-sources
  1111. (helm-compile-sources
  1112. helm-sources helm-compile-source-functions))
  1113. (helm-log-eval helm-compiled-sources)))))
  1114. (defun* helm-get-selection (&optional (buffer nil buffer-s)
  1115. force-display-part)
  1116. "Return the currently selected item or nil.
  1117. if BUFFER is nil or unspecified, use helm-buffer as default value.
  1118. If FORCE-DISPLAY-PART is non-nil, return the display string.
  1119. If FORCE-DISPLAY-PART value is 'withprop the display string is returned
  1120. with its properties."
  1121. (setq buffer (if (and buffer buffer-s) buffer helm-buffer))
  1122. (unless (helm-empty-buffer-p buffer)
  1123. (with-current-buffer buffer
  1124. (let* ((disp-fn (if (eq force-display-part 'withprop)
  1125. 'buffer-substring
  1126. 'buffer-substring-no-properties))
  1127. (selection
  1128. (or (and (not force-display-part)
  1129. (get-text-property (overlay-start
  1130. helm-selection-overlay)
  1131. 'helm-realvalue))
  1132. ;; It is needed to return properties of DISP in some case,
  1133. ;; e.g for `helm-confirm-and-exit-minibuffer',
  1134. ;; so use `buffer-substring' here when 'withprop is specified.
  1135. (let ((disp (funcall disp-fn
  1136. (overlay-start helm-selection-overlay)
  1137. (1- (overlay-end helm-selection-overlay))))
  1138. (source (helm-get-current-source)))
  1139. (helm-aif (and (not force-display-part)
  1140. (assoc-default 'display-to-real source))
  1141. (helm-funcall-with-source source it disp)
  1142. disp)))))
  1143. (unless (equal selection "")
  1144. (helm-log-eval selection)
  1145. selection)))))
  1146. (defun helm-get-action ()
  1147. "Return the associated action for the selected candidate.
  1148. It is a function symbol \(sole action\) or list
  1149. of \(action-display . function\)."
  1150. (unless (helm-empty-buffer-p (helm-buffer-get))
  1151. (helm-aif (helm-attr 'action-transformer)
  1152. (helm-composed-funcall-with-source
  1153. (helm-get-current-source) it
  1154. (helm-attr 'action) (helm-get-selection))
  1155. (helm-attr 'action))))
  1156. (defun helm-get-current-source ()
  1157. "Return the source for the current selection.
  1158. Use it in init, candidates, action, candidate-transformer,
  1159. filtered-candidate-transformer functions."
  1160. (declare (special source))
  1161. ;; The name `helm-get-current-source' should be used in init function etc.
  1162. (if (and (boundp 'helm-source-name) (stringp helm-source-name))
  1163. source
  1164. (with-current-buffer (helm-buffer-get)
  1165. (or
  1166. ;; This happen only when `helm-source-in-each-line-flag'
  1167. ;; is non--nil and there is candidates in buffer.
  1168. (get-text-property (point) 'helm-source)
  1169. ;; Return nil when no--candidates.
  1170. (block exit
  1171. ;; This goto-char shouldn't be necessary, but point is moved to
  1172. ;; point-min somewhere else which shouldn't happen.
  1173. (goto-char (overlay-start helm-selection-overlay))
  1174. (let* ((header-pos (or (helm-get-previous-header-pos)
  1175. (helm-get-next-header-pos)))
  1176. (source-name
  1177. (save-excursion
  1178. (unless header-pos
  1179. (return-from exit nil))
  1180. (goto-char header-pos)
  1181. (helm-current-line-contents))))
  1182. (loop for source in (helm-get-sources) thereis
  1183. (and (equal (assoc-default 'name source) source-name)
  1184. source))))))))
  1185. (defun helm-buffer-is-modified (buffer)
  1186. "Return non-nil when BUFFER is modified since `helm' was invoked."
  1187. (let* ((b (get-buffer buffer))
  1188. (key (concat (buffer-name b) "/" (helm-attr 'name)))
  1189. (source-tick (or (gethash key helm-tick-hash) 0))
  1190. (buffer-tick (buffer-chars-modified-tick b))
  1191. (modifiedp (/= source-tick buffer-tick)))
  1192. (puthash key buffer-tick helm-tick-hash)
  1193. (helm-log-eval buffer modifiedp)
  1194. modifiedp))
  1195. (defun helm-current-buffer-is-modified ()
  1196. "Check if `helm-current-buffer' is modified since `helm' was invoked."
  1197. (helm-buffer-is-modified helm-current-buffer))
  1198. (defvar helm-quit nil)
  1199. (defun helm-run-after-quit (function &rest args)
  1200. "Perform an action after quitting `helm'.
  1201. The action is to call FUNCTION with arguments ARGS."
  1202. (setq helm-quit t)
  1203. (helm-kill-async-processes)
  1204. (helm-log-eval function args)
  1205. (apply 'run-with-idle-timer 0 nil function args)
  1206. (helm-exit-minibuffer))
  1207. (defun define-helm-type-attribute (type definition &optional doc)
  1208. "Register type attribute of TYPE as DEFINITION with DOC.
  1209. DOC is displayed in `helm-type-attributes' docstring.
  1210. Use this function is better than setting `helm-type-attributes' directly."
  1211. (loop for i in definition do
  1212. ;; without `ignore-errors', error at emacs22
  1213. (ignore-errors (setf i (delete nil i))))
  1214. (helm-add-type-attribute type definition)
  1215. (and doc (helm-document-type-attribute type doc))
  1216. nil)
  1217. (defvaralias 'helm-attributes 'helm-additional-attributes)
  1218. (defvar helm-additional-attributes nil
  1219. "List of all `helm' attributes.")
  1220. (defun helm-document-attribute (attribute short-doc &optional long-doc)
  1221. "Register ATTRIBUTE documentation introduced by plug-in.
  1222. SHORT-DOC is displayed beside attribute name.
  1223. LONG-DOC is displayed below attribute name and short documentation."
  1224. (if long-doc
  1225. (setq short-doc (concat "(" short-doc ")"))
  1226. (setq long-doc short-doc
  1227. short-doc ""))
  1228. (add-to-list 'helm-additional-attributes attribute t)
  1229. (put attribute 'helm-attrdoc
  1230. (concat "- " (symbol-name attribute)
  1231. " " short-doc "\n\n" long-doc "\n")))
  1232. (put 'helm-document-attribute 'lisp-indent-function 2)
  1233. (defun helm-interpret-value (value &optional source)
  1234. "Interpret VALUE as variable, function or literal.
  1235. If VALUE is a function, call it with no arguments and return the value.
  1236. If SOURCE is `helm' source, `helm-source-name' is source name.
  1237. If VALUE is a variable, return the value.
  1238. If VALUE is a symbol, but it is not a function or a variable, cause an error.
  1239. Otherwise, return VALUE itself."
  1240. (cond ((and source (functionp value))
  1241. (helm-funcall-with-source source value))
  1242. ((functionp value)
  1243. (funcall value))
  1244. ((and (symbolp value) (boundp value))
  1245. (symbol-value value))
  1246. ((symbolp value)
  1247. (error "helm-interpret-value: Symbol must be a function or a variable"))
  1248. (t
  1249. value)))
  1250. (defun helm-once (function &rest args)
  1251. "Ensure FUNCTION with ARGS to be called once in `helm' session."
  1252. (let ((spec (cons function args)))
  1253. (unless (member spec helm-once-called-functions)
  1254. (apply function args)
  1255. (push spec helm-once-called-functions))))
  1256. ;; (@* "Core: API helper")
  1257. (defun* helm-empty-buffer-p (&optional (buffer helm-buffer))
  1258. "Check if BUFFER have candidates.
  1259. Default value for BUFFER is `helm-buffer'."
  1260. (zerop (buffer-size (and buffer (get-buffer buffer)))))
  1261. (defun helm-let-eval-varlist (varlist)
  1262. "Return the list of pairs VARLIST with each cdr of pair evaluated.
  1263. If VARLIST contain single elements, those are returned
  1264. as a list of one element."
  1265. (mapcar (lambda (pair)
  1266. (if (listp pair)
  1267. (cons (car pair) (eval (cadr pair)))
  1268. (cons pair nil)))
  1269. varlist))
  1270. ;; [NOT USED]
  1271. ;; (defun helm-let*-eval-varlist (varlist)
  1272. ;; (let ((vars (mapcar (lambda (pair)
  1273. ;; (or (car-safe pair) pair))
  1274. ;; varlist)))
  1275. ;; (eval `(let ,vars
  1276. ;; ,@(mapcar (lambda (pair)
  1277. ;; (if (listp pair)
  1278. ;; `(setq ,(car pair) ,(cadr pair))
  1279. ;; `(setq ,pair nil)))
  1280. ;; varlist)
  1281. ;; (mapcar (lambda (v)
  1282. ;; (cons v (symbol-value v)))
  1283. ;; ',vars)))))
  1284. (defun helm-let-internal (binding bodyfunc)
  1285. "Set BINDING to helm buffer-local variables and Evaluate BODYFUNC.
  1286. BINDING is a list of \(VARNAME . VALUE\) pair."
  1287. (setq helm-let-variables binding)
  1288. (unwind-protect
  1289. (funcall bodyfunc)
  1290. (setq helm-let-variables nil)))
  1291. ;; (@* "Core: tools")
  1292. (defun helm-current-line-contents ()
  1293. "Current line string without properties."
  1294. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
  1295. (defun helm-funcall-with-source (source func &rest args)
  1296. "Call from SOURCE FUNC list or single function FUNC with ARGS.
  1297. FUNC can be a symbol or a list of functions.
  1298. Return the result of last function call."
  1299. (let ((helm-source-name (assoc-default 'name source))
  1300. result)
  1301. (helm-log-eval helm-source-name func args)
  1302. (dolist (func (if (functionp func) (list func) func) result)
  1303. (setq result (apply func args)))))
  1304. (defun helm-funcall-foreach (sym)
  1305. "Call the function SYM for each source if any."
  1306. (dolist (source (helm-get-sources))
  1307. (helm-aif (assoc-default sym source)
  1308. (helm-funcall-with-source source it))))
  1309. (defun helm-normalize-sources (sources)
  1310. "If SOURCES is only one source, make a list of one element."
  1311. (cond ((or (and sources
  1312. (symbolp sources))
  1313. (and (listp sources) (assq 'name sources)))
  1314. (list sources))
  1315. (sources)
  1316. (t helm-sources)))
  1317. (defun helm-approximate-candidate-number (&optional in-current-source)
  1318. "Return approximate number of candidates in `helm-buffer'.
  1319. If IN-CURRENT-SOURCE is provided return number of candidates
  1320. in the source where point is.
  1321. It is used to check if candidate number is 0, 1, or 2+."
  1322. (with-current-buffer helm-buffer
  1323. (save-excursion
  1324. (if in-current-source
  1325. (goto-char (helm-get-previous-header-pos))
  1326. (goto-char (point-min)))
  1327. (forward-line 1)
  1328. (let ((count-multi 1))
  1329. (if (helm-pos-multiline-p)
  1330. (save-excursion
  1331. (loop while (and (not (if in-current-source
  1332. (save-excursion
  1333. (forward-line 2)
  1334. (or (helm-pos-header-line-p) (eobp)))
  1335. (eobp)))
  1336. (search-forward helm-candidate-separator nil t))
  1337. do (incf count-multi)
  1338. finally return count-multi))
  1339. (save-excursion
  1340. (loop with ln = 0
  1341. while (not (if in-current-source
  1342. (or (helm-pos-header-line-p) (eobp))
  1343. (eobp)))
  1344. unless (helm-pos-header-line-p)
  1345. do (incf ln)
  1346. do (forward-line 1) finally return ln)))))))
  1347. (defmacro with-helm-quittable (&rest body)
  1348. "If an error occur in execution of BODY, quit helm safely."
  1349. (declare (indent 0) (debug t))
  1350. `(let (inhibit-quit)
  1351. (condition-case v
  1352. (progn ,@body)
  1353. (quit (setq helm-quit t)
  1354. (exit-minibuffer)
  1355. (keyboard-quit)))))
  1356. (defun helm-compose (arg-lst func-lst)
  1357. "Apply arguments specified in ARG-LST with each function of FUNC-LST.
  1358. The result of each function will be the new `car' of ARG-LST.
  1359. This function allows easy sequencing of transformer functions."
  1360. (dolist (func func-lst)
  1361. (setcar arg-lst (apply func arg-lst)))
  1362. (car arg-lst))
  1363. (defun helm-composed-funcall-with-source (source funcs &rest args)
  1364. "With SOURCE apply `helm-funcall-with-source' with each FUNCS and ARGS.
  1365. This is used in transformers to modify candidates list."
  1366. (if (functionp funcs)
  1367. (apply 'helm-funcall-with-source source funcs args)
  1368. (apply 'helm-funcall-with-source source
  1369. (lambda (&rest args)
  1370. (helm-compose args funcs))
  1371. args)))
  1372. (defun helm-new-timer (variable timer)
  1373. "Give VARIABLE value to TIMER and cancel old timer."
  1374. (helm-aif (symbol-value variable)
  1375. (cancel-timer it))
  1376. (set variable timer))
  1377. ;; (@* "Core: entry point")
  1378. (defconst helm-argument-keys
  1379. '(:sources :input :prompt :resume :preselect :buffer :keymap :default :history))
  1380. ;;;###autoload
  1381. (defun helm (&rest plist)
  1382. "Main function to execute helm sources.
  1383. Keywords supported:
  1384. :sources :input :prompt :resume :preselect :buffer :keymap :default :history
  1385. Extra keywords are supported and can be added, see below.
  1386. When call interactively with no arguments deprecated `helm-sources'
  1387. will be used if non--nil.
  1388. PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
  1389. \(&optional sources input prompt resume preselect buffer keymap default history\).
  1390. Basic keywords are the following:
  1391. \:sources
  1392. Temporary value of `helm-sources'. It also accepts a
  1393. symbol, interpreted as a variable of an helm source. It
  1394. also accepts an alist representing an helm source, which is
  1395. detected by \(assq 'name ANY-SOURCES\)
  1396. \:input
  1397. Temporary value of `helm-pattern', ie. initial input of minibuffer.
  1398. \:prompt
  1399. Prompt other than \"pattern: \".
  1400. \:resume
  1401. If t, Resurrect previously instance of `helm'. Skip the initialization.
  1402. If 'noresume, this instance of `helm' cannot be resumed.
  1403. \:preselect
  1404. Initially selected candidate. Specified by exact candidate or a regexp.
  1405. \:buffer
  1406. `helm-buffer' instead of *helm*.
  1407. \:keymap
  1408. `helm-map' for current `helm' session.
  1409. \:default
  1410. A default argument that will be inserted in minibuffer \
  1411. with \\<minibuffer-local-map>\\[next-history-element].
  1412. When nil of not present `thing-at-point' will be used instead.
  1413. \:history
  1414. By default all minibuffer input is pushed to `minibuffer-history',
  1415. if an argument HISTORY is provided, input will be pushed to HISTORY.
  1416. History element should be a symbol.
  1417. Of course, conventional arguments are supported, the two are same.
  1418. \(helm :sources sources :input input :prompt prompt :resume resume
  1419. :preselect preselect :buffer buffer :keymap keymap :default default
  1420. :history history\)
  1421. \(helm sources input prompt resume preselect buffer keymap default history\)
  1422. Other keywords are interpreted as local variables of this helm session.
  1423. The `helm-' prefix can be omitted. For example,
  1424. \(helm :sources 'helm-c-source-buffers
  1425. :buffer \"*buffers*\" :candidate-number-limit 10\)
  1426. means starting helm session with `helm-c-source-buffers'
  1427. source in *buffers* buffer and set variable `helm-candidate-number-limit'
  1428. to 10 as session local variable."
  1429. (interactive)
  1430. (if (keywordp (car plist))
  1431. (helm-let-internal
  1432. (helm-parse-keys plist)
  1433. (lambda ()
  1434. (apply 'helm
  1435. (mapcar (lambda (key) (plist-get plist key))
  1436. helm-argument-keys))))
  1437. (apply 'helm-internal plist)))
  1438. (defun helm-parse-keys (keys)
  1439. "Parse the KEYS arguments of `helm'.
  1440. Return only the keys that are not in `helm-argument-keys'.
  1441. It is used to set local variables via `helm-let-internal'.
  1442. This allow to add arguments that are not part of `helm-argument-keys',
  1443. but are valid helm attributes.
  1444. i.e :candidate-number-limit will be bound to `helm-candidate-number-limit'
  1445. in source."
  1446. ;; (helm-parse-keys '(:sources ((name . "test")
  1447. ;; (candidates . (a b c)))
  1448. ;; :buffer "toto"
  1449. ;; :candidate-number-limit 4))
  1450. ;; ==> ((helm-candidate-number-limit . 4))
  1451. (loop for (key value) on keys by #'cddr
  1452. for symname = (substring (symbol-name key) 1)
  1453. for sym = (intern (if (string-match "^helm-" symname)
  1454. symname
  1455. (concat "helm-" symname)))
  1456. unless (memq key helm-argument-keys)
  1457. collect (cons sym value)))
  1458. ;;; (@* "Core: entry point helper")
  1459. (defun helm-internal (&optional
  1460. any-sources any-input
  1461. any-prompt any-resume
  1462. any-preselect any-buffer
  1463. any-keymap any-default any-history)
  1464. "The internal helm function called by `helm'.
  1465. For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
  1466. ANY-KEYMAP ANY-DEFAULT ANY-HISTORY See `helm'."
  1467. (helm-log (concat "[Start session] " (make-string 41 ?+)))
  1468. (helm-log-eval any-prompt any-preselect
  1469. any-buffer any-keymap any-default)
  1470. (let ((old-overridding-local-map overriding-local-map))
  1471. (unwind-protect
  1472. (condition-case v
  1473. (let ( ;; It is needed because `helm-source-name' is non-nil
  1474. ;; when `helm' is invoked by action. Awful global scope.
  1475. helm-source-name
  1476. helm-in-persistent-action
  1477. helm-quit
  1478. (case-fold-search t)
  1479. (helm-buffer (or any-buffer helm-buffer))
  1480. ;; cua-mode ; avoid error when region is selected
  1481. )
  1482. (with-helm-restore-variables
  1483. (helm-initialize any-resume any-input any-sources)
  1484. (helm-display-buffer helm-buffer)
  1485. (helm-log "show prompt")
  1486. (unwind-protect
  1487. (helm-read-pattern-maybe
  1488. any-prompt any-input any-preselect
  1489. any-resume any-keymap any-default
  1490. (when (and any-history (symbolp any-history)) any-history))
  1491. (helm-cleanup)))
  1492. (prog1 (unless helm-quit
  1493. (helm-execute-selection-action-1))
  1494. (helm-log (concat "[End session] " (make-string 41 ?-)))))
  1495. (quit
  1496. (helm-restore-position-on-quit)
  1497. (helm-log (concat "[End session (quit)] " (make-string 34 ?-)))
  1498. nil))
  1499. (setq overriding-local-map old-overridding-local-map)
  1500. (helm-log-save-maybe))))
  1501. ;;; Helm resume
  1502. ;;
  1503. ;;
  1504. (defun* helm-resume (&optional
  1505. (any-buffer helm-last-buffer)
  1506. buffer-pattern (any-resume t))
  1507. "Resurrect previously invoked `helm'.
  1508. Called with a prefix arg, allow choosing among all existing
  1509. helm buffers. i.e choose among various helm sessions."
  1510. (interactive)
  1511. (when (or current-prefix-arg buffer-pattern)
  1512. (setq any-buffer (helm-resume-select-buffer buffer-pattern)))
  1513. (setq helm-compiled-sources nil)
  1514. (helm
  1515. :sources (or (buffer-local-value 'helm-last-sources-local (get-buffer any-buffer))
  1516. helm-last-sources helm-sources)
  1517. :input (buffer-local-value 'helm-input-local (get-buffer any-buffer))
  1518. :resume any-resume
  1519. :buffer any-buffer))
  1520. ;;; rubikitch: experimental
  1521. ;;; I use this and check it whether I am convenient.
  1522. ;;; I may introduce an option to control the behavior.
  1523. (defun* helm-resume-window-only (&optional
  1524. (any-buffer helm-last-buffer)
  1525. buffer-pattern)
  1526. (interactive)
  1527. (helm-resume any-buffer buffer-pattern 'window-only))
  1528. (defun helm-resume-p (any-resume)
  1529. "Whether current helm session is resumed or not.
  1530. Just check if ANY-RESUME value is t or window-only."
  1531. (memq any-resume '(t window-only)))
  1532. (defun helm-resume-select-buffer (input)
  1533. "Resume precedent helm session with initial input INPUT."
  1534. (or (helm :sources '(((name . "Resume helm buffer")
  1535. (candidates . helm-buffers)
  1536. (action . identity)))
  1537. :input input
  1538. :resume 'noresume
  1539. :buffer "*helm resume*")
  1540. (keyboard-quit)))
  1541. ;;;###autoload
  1542. (defun helm-at-point (&optional
  1543. any-sources any-input
  1544. any-prompt any-resume
  1545. any-preselect any-buffer)
  1546. "Call helm with symbol at point as initial input.
  1547. ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
  1548. are same args as in `helm'."
  1549. (interactive)
  1550. (helm :sources any-sources
  1551. :input (if current-prefix-arg
  1552. (concat "\\b" (thing-at-point 'symbol) "\\b"
  1553. (if (featurep 'helm-match-plugin) " " ""))
  1554. any-input)
  1555. :prompt any-prompt
  1556. :resume any-resume
  1557. :preselect any-preselect
  1558. :buffer any-buffer))
  1559. ;;;###autoload
  1560. (defun helm-other-buffer (any-sources any-buffer)
  1561. "Simplified interface of `helm' with other `helm-buffer'.
  1562. Call `helm' with only ANY-SOURCES and ANY-BUFFER as args."
  1563. (helm :sources any-sources :buffer any-buffer))
  1564. (defun helm-nest (&rest same-as-helm)
  1565. "Allow calling `helm' whithin a running helm session."
  1566. (with-helm-window
  1567. (let (helm-current-position
  1568. helm-current-buffer
  1569. (orig-helm-current-buffer helm-current-buffer)
  1570. (orig-helm-buffer helm-buffer)
  1571. (orig-helm-last-frame-or-window-configuration
  1572. helm-last-frame-or-window-configuration)
  1573. helm-pattern
  1574. (helm-buffer (or (getf same-as-helm :buffer)
  1575. (nth 5 same-as-helm)
  1576. "*Helm*"))
  1577. helm-sources
  1578. helm-compiled-sources
  1579. (helm-samewindow t)
  1580. (enable-recursive-minibuffers t))
  1581. (unwind-protect
  1582. (apply #'helm same-as-helm)
  1583. (with-current-buffer orig-helm-buffer
  1584. (helm-initialize-overlays orig-helm-buffer)
  1585. (setq helm-buffer (current-buffer))
  1586. (helm-mark-current-line)
  1587. (setq helm-last-frame-or-window-configuration
  1588. orig-helm-last-frame-or-window-configuration)
  1589. (setq cursor-type t)
  1590. (setq helm-current-buffer orig-helm-current-buffer))))))
  1591. ;;; Initialize
  1592. ;;
  1593. ;;
  1594. (defvar helm-buffers nil
  1595. "All of `helm-buffer' in most recently used order.")
  1596. (defun helm-initialize (any-resume any-input any-sources)
  1597. "Start initialization of `helm' session.
  1598. For ANY-RESUME ANY-INPUT and ANY-SOURCES See `helm'."
  1599. (helm-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
  1600. (helm-frame-or-window-configuration 'save)
  1601. (setq helm-sources (helm-normalize-sources any-sources))
  1602. (helm-log "sources = %S" helm-sources)
  1603. (helm-hooks 'setup)
  1604. (helm-current-position 'save)
  1605. (if (helm-resume-p any-resume)
  1606. (helm-initialize-overlays (helm-buffer-get))
  1607. (helm-initial-setup))
  1608. (unless (eq any-resume 'noresume)
  1609. (helm-recent-push helm-buffer 'helm-buffers)
  1610. (setq helm-last-buffer helm-buffer))
  1611. (when any-input (setq helm-input any-input helm-pattern any-input))
  1612. (and (helm-resume-p any-resume) (helm-funcall-foreach 'resume))
  1613. (helm-log "end initialization"))
  1614. (defun helm-execute-selection-action-1 ()
  1615. "Execute current action."
  1616. (helm-log-run-hook 'helm-before-action-hook)
  1617. (unwind-protect
  1618. (helm-execute-selection-action)
  1619. (helm-aif (get-buffer helm-action-buffer)
  1620. (kill-buffer it))
  1621. (helm-log-run-hook 'helm-after-action-hook)))
  1622. (defun helm-restore-position-on-quit ()
  1623. "Restore position in `helm-current-buffer' when quitting."
  1624. (helm-current-position 'restore))
  1625. (defun helm-recent-push (elt list-var)
  1626. "Add ELT to the value of LIST-VAR as most recently used value."
  1627. (let ((m (member elt (symbol-value list-var))))
  1628. (and m (set list-var (delq (car m) (symbol-value list-var))))
  1629. (push elt (symbol-value list-var))))
  1630. ;;; (@* "Core: Accessors")
  1631. ;;; rubikitch: I love to create functions to control variables.
  1632. (defvar helm-current-position nil
  1633. "Cons of \(point . window-start\) when `helm' is invoked.
  1634. It is needed to restore position in `helm-current-buffer'
  1635. when `helm' is keyboard-quitted.")
  1636. (defun helm-current-position (save-or-restore)
  1637. "Restore or save current position in `helm-current-buffer'.
  1638. Argument SAVE-OR-RESTORE is one of save or restore."
  1639. (case save-or-restore
  1640. (save
  1641. (helm-log "Save position at %S" (cons (point) (window-start)))
  1642. (setq helm-current-position (cons (point) (window-start))))
  1643. (restore
  1644. (helm-log "Restore position at %S in buffer %s"
  1645. helm-current-position
  1646. (buffer-name (current-buffer)))
  1647. (goto-char (car helm-current-position))
  1648. ;; Fix this position with the NOFORCE arg of `set-window-start'
  1649. ;; otherwise, if there is some other buffer than `helm-current-buffer'
  1650. ;; one, position will be lost.
  1651. (set-window-start (selected-window) (cdr helm-current-position) t))))
  1652. ;; Internal.
  1653. (defvar helm-last-frame-or-window-configuration nil
  1654. "Used to store window or frame configuration when helm start.")
  1655. (defun helm-frame-or-window-configuration (save-or-restore)
  1656. "Save or restore last frame or window configuration.
  1657. Possible value of SAVE-OR-RESTORE are 'save and 'restore.
  1658. window or frame configuration is saved/restored according to values of
  1659. `helm-save-configuration-functions'."
  1660. (helm-log-eval helm-save-configuration-functions)
  1661. (case save-or-restore
  1662. (save (setq helm-last-frame-or-window-configuration
  1663. (funcall (cdr helm-save-configuration-functions))))
  1664. (restore (funcall (car helm-save-configuration-functions)
  1665. helm-last-frame-or-window-configuration)
  1666. ;; Restore frame focus.
  1667. (let ((frame (and (listp helm-last-frame-or-window-configuration)
  1668. (caadr helm-last-frame-or-window-configuration))))
  1669. ;; If `helm-save-configuration-functions' are window functions
  1670. ;; frame should be nil, use current frame.
  1671. (unless (framep frame)
  1672. (setq frame (selected-frame)))
  1673. (select-frame-set-input-focus frame)))))
  1674. ;; (@* "Core: Display *helm* buffer")
  1675. (defun helm-display-buffer (buf)
  1676. "Display *helm* buffer BUF."
  1677. (let (pop-up-frames)
  1678. (funcall (with-current-buffer buf helm-display-function) buf)))
  1679. (defun helm-default-display-buffer (buf)
  1680. "Default function to display BUF.
  1681. Where BUF is generally `helm-buffer'.
  1682. It use `switch-to-buffer' or `pop-to-buffer' depending of value of
  1683. `helm-samewindow'."
  1684. (funcall (if helm-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
  1685. ;; (@* "Core: initialize")
  1686. (defun helm-initial-setup ()
  1687. "Initialize helm settings and set up the helm buffer."
  1688. (helm-log-run-hook 'helm-before-initialize-hook)
  1689. (setq helm-current-prefix-arg nil)
  1690. (setq helm-once-called-functions nil)
  1691. (setq helm-delayed-init-executed nil)
  1692. (setq helm-alive-p t)
  1693. (setq helm-current-buffer
  1694. (if (minibuffer-window-active-p (minibuffer-window))
  1695. ;; If minibuffer is active be sure to use it's buffer
  1696. ;; as `helm-current-buffer'.
  1697. (window-buffer (active-minibuffer-window))
  1698. (current-buffer)))
  1699. (setq helm-buffer-file-name buffer-file-name)
  1700. (setq helm-issued-errors nil)
  1701. (setq helm-compiled-sources nil)
  1702. (setq helm-saved-current-source nil)
  1703. (if (or (not split-width-threshold)
  1704. (and (integerp split-width-threshold)
  1705. (>= split-width-threshold (+ (frame-width) 4))))
  1706. (setq helm-split-window-state 'vertical)
  1707. (setq helm-split-window-state 'horizontal))
  1708. ;; Call the init function for sources where appropriate
  1709. (helm-funcall-foreach 'init)
  1710. (setq helm-pattern "")
  1711. (setq helm-input "")
  1712. (setq helm-candidate-cache nil)
  1713. (setq helm-last-sources helm-sources)
  1714. (helm-create-helm-buffer)
  1715. (helm-log-run-hook 'helm-after-initialize-hook))
  1716. (defvar helm-reading-pattern nil
  1717. "Whether in `read-string' in helm or not.")
  1718. (defun helm-read-pattern-maybe (any-prompt any-input
  1719. any-preselect any-resume any-keymap
  1720. any-default any-history)
  1721. "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
  1722. For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `helm'."
  1723. (if (helm-resume-p any-resume)
  1724. (helm-mark-current-line t)
  1725. (helm-update any-preselect))
  1726. (with-current-buffer (helm-buffer-get)
  1727. (let ((src-keymap (assoc-default 'keymap (helm-get-current-source))))
  1728. ;; Startup with the first keymap found either in current source
  1729. ;; or helm arg, otherwise use global value of `helm-map'.
  1730. ;; This map will be used as a `minibuffer-local-map'.
  1731. ;; Maybe it will be overriden when changing source
  1732. ;; by `helm-maybe-update-keymap'.
  1733. (set (make-local-variable 'helm-map)
  1734. (or src-keymap any-keymap helm-map))
  1735. (helm-log-eval (helm-approximate-candidate-number)
  1736. helm-execute-action-at-once-if-one
  1737. helm-quit-if-no-candidate)
  1738. (cond ((and helm-execute-action-at-once-if-one
  1739. (= (helm-approximate-candidate-number) 1))
  1740. (ignore))
  1741. ((and helm-quit-if-no-candidate
  1742. (= (helm-approximate-candidate-number) 0))
  1743. (setq helm-quit t)
  1744. (and (functionp helm-quit-if-no-candidate)
  1745. (funcall helm-quit-if-no-candidate)))
  1746. (t
  1747. (let ((helm-reading-pattern t)
  1748. (tap (or any-default
  1749. (with-helm-current-buffer
  1750. (thing-at-point 'symbol)))))
  1751. (read-from-minibuffer (or any-prompt "pattern: ")
  1752. any-input helm-map
  1753. nil any-history tap)))))))
  1754. (defun helm-maybe-update-keymap ()
  1755. "Handle differents keymaps in multiples sources.
  1756. This function is meant to be run in `helm-move-selection-after-hook'.
  1757. It will override `helm-map' with the keymap attribute of current source
  1758. if some when multiples sources are present."
  1759. (with-helm-window
  1760. (let ((kmap (assoc-default 'keymap (helm-get-current-source))))
  1761. (when kmap (setq overriding-local-map kmap)))))
  1762. (add-hook 'helm-move-selection-after-hook 'helm-maybe-update-keymap)
  1763. (defun helm-create-helm-buffer (&optional test-mode)
  1764. "Create newly created `helm-buffer'.
  1765. If TEST-MODE is non-nil, clear `helm-candidate-cache'."
  1766. (when test-mode
  1767. (setq helm-candidate-cache nil))
  1768. (with-current-buffer (get-buffer-create helm-buffer)
  1769. (helm-log "kill local variables: %S" (buffer-local-variables))
  1770. (kill-all-local-variables)
  1771. (set (make-local-variable 'inhibit-read-only) t)
  1772. (buffer-disable-undo)
  1773. (erase-buffer)
  1774. (set (make-local-variable 'inhibit-read-only) t)
  1775. (set (make-local-variable 'helm-last-sources-local) helm-sources)
  1776. (set (make-local-variable 'helm-follow-mode) nil)
  1777. (set (make-local-variable 'helm-display-function) helm-display-function)
  1778. (set (make-local-variable 'helm-selection-point) nil)
  1779. (helm-initialize-persistent-action)
  1780. (helm-log-eval helm-display-function helm-let-variables)
  1781. (loop for (var . val) in helm-let-variables
  1782. do (set (make-local-variable var) val))
  1783. (setq cursor-type nil)
  1784. (setq mode-name "Helm"))
  1785. (helm-initialize-overlays helm-buffer)
  1786. (get-buffer helm-buffer))
  1787. (defun helm-initialize-overlays (buffer)
  1788. "Initialize helm overlays in BUFFER."
  1789. (helm-log "overlay setup")
  1790. (if helm-selection-overlay
  1791. ;; make sure the overlay belongs to the helm buffer if
  1792. ;; it's newly created
  1793. (move-overlay helm-selection-overlay (point-min) (point-min)
  1794. (get-buffer buffer))
  1795. (setq helm-selection-overlay
  1796. (make-overlay (point-min) (point-min) (get-buffer buffer)))
  1797. (overlay-put helm-selection-overlay 'face helm-selection-face))
  1798. (cond (helm-enable-shortcuts
  1799. (setq helm-shortcut-keys
  1800. (assoc-default helm-enable-shortcuts helm-shortcut-keys-alist))
  1801. (unless helm-digit-overlays
  1802. (setq helm-digit-overlays
  1803. (loop for key across helm-shortcut-keys
  1804. for overlay = (make-overlay (point-min) (point-min)
  1805. (get-buffer buffer))
  1806. do (overlay-put overlay 'before-string
  1807. (format "%s - " (upcase (make-string 1 key))))
  1808. collect overlay))))
  1809. (helm-digit-overlays
  1810. (mapc 'delete-overlay helm-digit-overlays)
  1811. (setq helm-digit-overlays nil))))
  1812. (defun helm-hooks (setup-or-cleanup)
  1813. "Add or remove hooks according to SETUP-OR-CLEANUP value.
  1814. if SETUP-OR-CLEANUP value is setup add hooks, any other value
  1815. will remove hooks.
  1816. hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
  1817. (let ((hooks '((post-command-hook helm-check-minibuffer-input)
  1818. (minibuffer-setup-hook helm-print-error-messages))))
  1819. (if (eq setup-or-cleanup 'setup)
  1820. (dolist (args hooks) (apply 'add-hook args))
  1821. (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
  1822. ;; (@* "Core: clean up")
  1823. ;;; TODO move
  1824. (defun helm-cleanup ()
  1825. "Clean up the mess when helm exit or quit."
  1826. (helm-log "start cleanup")
  1827. (with-current-buffer helm-buffer
  1828. ;; rubikitch: I think it is not needed.
  1829. ;; thierry: If you end up for any reasons (error etc...)
  1830. ;; with an helm-buffer staying around (visible),
  1831. ;; You will have no cursor in this buffer when switching to it,
  1832. ;; so I think this is needed.
  1833. (setq cursor-type t)
  1834. ;; Call burry-buffer whithout arg
  1835. ;; to be sure helm-buffer is removed from window.
  1836. (bury-buffer)
  1837. ;; Be sure we call this from helm-buffer.
  1838. (helm-funcall-foreach 'cleanup))
  1839. (helm-new-timer 'helm-check-minibuffer-input-timer nil)
  1840. (helm-kill-async-processes)
  1841. (helm-log-run-hook 'helm-cleanup-hook)
  1842. (helm-hooks 'cleanup)
  1843. (helm-frame-or-window-configuration 'restore)
  1844. (setq helm-alive-p nil)
  1845. ;; This is needed in some cases where last input
  1846. ;; is yielded infinitely in minibuffer after helm session.
  1847. (helm-clean-up-minibuffer))
  1848. (defun helm-clean-up-minibuffer ()
  1849. "Remove contents of minibuffer."
  1850. (let ((miniwin (minibuffer-window)))
  1851. ;; Clean only current minibuffer used by helm.
  1852. ;; i.e The precedent one is active.
  1853. (unless (minibuffer-window-active-p miniwin)
  1854. (with-current-buffer (window-buffer miniwin)
  1855. (delete-minibuffer-contents)))))
  1856. ;; (@* "Core: input handling")
  1857. (defun helm-check-minibuffer-input ()
  1858. "Extract input string from the minibuffer and check if it needs to be handled."
  1859. (let ((delay (with-current-buffer helm-buffer
  1860. (and helm-input-idle-delay
  1861. (max helm-input-idle-delay 0.1)))))
  1862. (if (or (not delay) (helm-action-window))
  1863. (helm-check-minibuffer-input-1)
  1864. (helm-new-timer
  1865. 'helm-check-minibuffer-input-timer
  1866. (run-with-idle-timer delay nil 'helm-check-minibuffer-input-1)))))
  1867. (defun helm-check-minibuffer-input-1 ()
  1868. "Check minibuffer content."
  1869. (with-helm-quittable
  1870. (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
  1871. (helm-check-new-input (minibuffer-contents)))))
  1872. (defun helm-check-new-input (input)
  1873. "Check INPUT string and update the helm buffer if necessary."
  1874. (unless (equal input helm-pattern)
  1875. (setq helm-pattern input)
  1876. (unless (helm-action-window)
  1877. (setq helm-input helm-pattern))
  1878. (helm-log-eval helm-pattern helm-input)
  1879. (helm-update)))
  1880. ;; (@* "Core: source compiler")
  1881. (defvar helm-compile-source-functions-default helm-compile-source-functions
  1882. "Plug-ins this file provides.")
  1883. (defun helm-compile-sources (sources funcs)
  1884. "Compile SOURCES with FUNCS.
  1885. See `helm-compile-source-functions'.
  1886. Helm plug-ins are realized by this function."
  1887. (mapcar
  1888. (lambda (source)
  1889. (loop with source = (if (listp source) source (symbol-value source))
  1890. for f in funcs
  1891. do (setq source (funcall f source))
  1892. finally (return source)))
  1893. sources))
  1894. ;; (@* "Core: plug-in attribute documentation hack")
  1895. ;; `helm-document-attribute' is public API.
  1896. (defadvice documentation-property (after helm-document-attribute activate)
  1897. "Display plug-in attributes' documentation as `helm-sources' docstring."
  1898. (when (eq (ad-get-arg 0) 'helm-sources)
  1899. (setq ad-return-value
  1900. (concat ad-return-value "\n"
  1901. (mapconcat (lambda (sym) (get sym 'helm-attrdoc))
  1902. helm-additional-attributes
  1903. "\n")))))
  1904. ;; (describe-variable 'helm-sources)
  1905. ;; (documentation-property 'helm-sources 'variable-documentation)
  1906. ;; (progn (ad-disable-advice 'documentation-property 'after 'helm-document-attribute) (ad-update 'documentation-property))
  1907. ;; (@* "Core: all candidates")
  1908. (defun helm-process-delayed-init (source)
  1909. "Initialize delayed SOURCE."
  1910. (let ((name (assoc-default 'name source)))
  1911. (unless (member name helm-delayed-init-executed)
  1912. (helm-aif (assoc-default 'delayed-init source)
  1913. (with-current-buffer helm-current-buffer
  1914. (helm-funcall-with-source source it)
  1915. (dolist (f (if (functionp it) (list it) it))
  1916. (add-to-list 'helm-delayed-init-executed name)))))))
  1917. (defun helm-get-candidates (source)
  1918. "Retrieve and return the list of candidates from SOURCE."
  1919. (helm-process-delayed-init source)
  1920. (let* ((candidate-source (assoc-default 'candidates source))
  1921. (type-error (lambda ()
  1922. (error (concat "Candidates must either be a function, "
  1923. " a variable or a list: %s")
  1924. candidate-source)))
  1925. (candidates (condition-case err
  1926. (helm-interpret-value candidate-source source)
  1927. (error (funcall type-error)))))
  1928. (cond ((processp candidates) candidates)
  1929. ((listp candidates) (helm-transform-candidates candidates source))
  1930. (t (funcall type-error)))))
  1931. (defun helm-get-cached-candidates (source)
  1932. "Return the cached value of candidates for SOURCE.
  1933. Cache the candidates if there is not yet a cached value."
  1934. (let* ((name (assoc-default 'name source))
  1935. (candidate-cache (assoc name helm-candidate-cache)))
  1936. (cond (candidate-cache
  1937. (helm-log "use cached candidates")
  1938. (cdr candidate-cache))
  1939. (t
  1940. (helm-log "calculate candidates")
  1941. (let ((candidates (helm-get-candidates source)))
  1942. (cond ((processp candidates)
  1943. (push (cons candidates
  1944. (append source
  1945. (list (cons 'item-count 0)
  1946. (cons 'incomplete-line ""))))
  1947. helm-async-processes)
  1948. (set-process-filter candidates 'helm-output-filter)
  1949. (setq candidates nil))
  1950. ((not (assoc 'volatile source))
  1951. (setq candidate-cache (cons name candidates))
  1952. (push candidate-cache helm-candidate-cache)))
  1953. candidates)))))
  1954. ;;; (@* "Core: candidate transformers")
  1955. (defun helm-transform-mapcar (function args)
  1956. "`mapcar' for candidate-transformer.
  1957. ARGS is (cand1 cand2 ...) or ((disp1 . real1) (disp2 . real2) ...)
  1958. \(helm-transform-mapcar 'upcase '(\"foo\" \"bar\"))
  1959. => (\"FOO\" \"BAR\")
  1960. \(helm-transform-mapcar 'upcase '((\"1st\" . \"foo\") (\"2nd\" . \"bar\")))
  1961. => ((\"1st\" . \"FOO\") (\"2nd\" . \"BAR\"))
  1962. "
  1963. (loop for arg in args
  1964. if (consp arg)
  1965. collect (cons (car arg) (funcall function (cdr arg)))
  1966. else
  1967. collect (funcall function arg)))
  1968. (defun helm-process-candidate-transformer (candidates source)
  1969. "Execute candidate-transformer function on all CANDIDATES of SOURCE."
  1970. (helm-aif (assoc-default 'candidate-transformer source)
  1971. (helm-composed-funcall-with-source source it candidates)
  1972. candidates))
  1973. (defun helm-process-filtered-candidate-transformer (candidates source)
  1974. "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
  1975. (helm-aif (assoc-default 'filtered-candidate-transformer source)
  1976. (helm-composed-funcall-with-source source it candidates source)
  1977. candidates))
  1978. (defun helm-process-filtered-candidate-transformer-maybe (candidates source process-p)
  1979. "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
  1980. This happen if PROCESS-P is non-nil."
  1981. (if process-p
  1982. (helm-process-filtered-candidate-transformer candidates source)
  1983. candidates))
  1984. (defun helm-process-real-to-display (candidates source)
  1985. "Execute real-to-display function on all CANDIDATES of SOURCE."
  1986. (helm-aif (assoc-default 'real-to-display source)
  1987. (setq candidates (helm-funcall-with-source
  1988. source 'mapcar
  1989. (lambda (cand_)
  1990. (if (consp cand_)
  1991. ;; override DISPLAY from candidate-transformer
  1992. (cons (funcall it (cdr cand_)) (cdr cand_))
  1993. (cons (funcall it cand_) cand_)))
  1994. candidates))
  1995. candidates))
  1996. (defun helm-transform-candidates (candidates source &optional process-p)
  1997. "Transform CANDIDATES of SOURCE according to candidate transformers.
  1998. This happen if PROCESS-P is non-nil."
  1999. (helm-process-real-to-display
  2000. (helm-process-filtered-candidate-transformer-maybe
  2001. (helm-process-candidate-transformer candidates source) source process-p)
  2002. source))
  2003. ;; (@* "Core: narrowing candidates")
  2004. (defun helm-candidate-number-limit (source)
  2005. "Apply candidate-number-limit attribute value.
  2006. This overhide variable `helm-candidate-number-limit'.
  2007. e.g:
  2008. If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
  2009. If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
  2010. (helm-aif (assq 'candidate-number-limit source)
  2011. (or (cdr it) 99999999)
  2012. (or helm-candidate-number-limit 99999999)))
  2013. ;; FIXME: Why a defconst here
  2014. (defconst helm-default-match-functions
  2015. (list (lambda (candidate)
  2016. (string-match helm-pattern candidate)))
  2017. "Default functions to match candidates according to `helm-pattern'.")
  2018. (defun helm-compute-matches (source)
  2019. "Compute matched results from SOURCE according to its settings."
  2020. (if debug-on-error
  2021. (helm-compute-matches-internal source)
  2022. (condition-case v
  2023. (helm-compute-matches-internal source)
  2024. (error (helm-log-error
  2025. "helm-compute-matches: error when processing source: %s"
  2026. (assoc-default 'name source))
  2027. nil))))
  2028. (defun helm-candidate-get-display (candidate)
  2029. "Get searched display part from CANDIDATE.
  2030. CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
  2031. (format "%s" (or (car-safe candidate) candidate)))
  2032. (defun helm-process-pattern-transformer (pattern source)
  2033. "Execute pattern-transformer attribute PATTERN function in SOURCE."
  2034. (helm-aif (assoc-default 'pattern-transformer source)
  2035. (helm-composed-funcall-with-source source it pattern)
  2036. pattern))
  2037. (defun helm-match-functions (source)
  2038. (or (assoc-default 'match source)
  2039. helm-default-match-functions))
  2040. (defmacro helm-accumulate-candidates-internal (cand newmatches
  2041. hash item-count limit)
  2042. "Internal, add CAND into NEWMATCHES.
  2043. Use HASH to uniq NEWMATCHES.
  2044. Argument ITEM-COUNT count the matches.
  2045. if ITEM-COUNT reaches LIMIT, exit from inner loop."
  2046. `(unless (gethash ,cand ,hash)
  2047. (puthash ,cand t ,hash)
  2048. (push ,cand ,newmatches)
  2049. (incf ,item-count)
  2050. (when (= ,item-count ,limit)
  2051. (setq exit t)
  2052. (return))))
  2053. (defun helm-take-first-elements (seq n)
  2054. (if (> (length seq) n)
  2055. (setq seq (subseq seq 0 n))
  2056. seq))
  2057. (defun helm-match-from-candidates (cands matchfns limit)
  2058. (let (matches)
  2059. (condition-case nil
  2060. (let ((item-count 0) exit)
  2061. (clrhash helm-match-hash)
  2062. (dolist (match matchfns)
  2063. (let (newmatches)
  2064. (dolist (candidate cands)
  2065. (when (funcall match (helm-candidate-get-display candidate))
  2066. (helm-accumulate-candidates-internal
  2067. candidate newmatches helm-match-hash item-count limit)))
  2068. (setq matches (append matches (reverse newmatches)))
  2069. (if exit (return)))))
  2070. (invalid-regexp (setq matches nil)))
  2071. matches))
  2072. (defun helm-compute-matches-internal (source)
  2073. (save-current-buffer
  2074. (let ((matchfns (helm-match-functions source))
  2075. (helm-source-name (assoc-default 'name source))
  2076. (limit (helm-candidate-number-limit source))
  2077. (helm-pattern (helm-process-pattern-transformer
  2078. helm-pattern source)))
  2079. (helm-process-filtered-candidate-transformer
  2080. (if (or (equal helm-pattern "") (equal matchfns '(identity)))
  2081. (helm-take-first-elements
  2082. (helm-get-cached-candidates source) limit)
  2083. (helm-match-from-candidates
  2084. (helm-get-cached-candidates source) matchfns limit))
  2085. source))))
  2086. ;; (helm '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
  2087. (defun helm-process-source (source)
  2088. "Display matched results from SOURCE according to its settings."
  2089. (helm-log-eval (assoc-default 'name source))
  2090. (if (assq 'direct-insert-match source) ;experimental
  2091. (helm-process-source--direct-insert-match source)
  2092. (let ((matches (helm-compute-matches source)))
  2093. (when matches
  2094. (when helm-test-mode
  2095. (setq helm-test-candidate-list
  2096. `(,@helm-test-candidate-list
  2097. (,(assoc-default 'name source)
  2098. ,matches))))
  2099. (helm-insert-header-from-source source)
  2100. (if (not (assq 'multiline source))
  2101. (mapc 'helm-insert-match-with-digit-overlay matches)
  2102. (let ((start (point)) separate)
  2103. (dolist (match matches)
  2104. (if separate
  2105. (helm-insert-candidate-separator)
  2106. (setq separate t))
  2107. (helm-insert-match-with-digit-overlay match))
  2108. (put-text-property start (point) 'helm-multiline t)))))))
  2109. (defun helm-insert-match-with-digit-overlay (match)
  2110. (declare (special source))
  2111. (helm-put-digit-overlay-maybe)
  2112. (helm-insert-match match 'insert source))
  2113. (defun helm-put-digit-overlay-maybe ()
  2114. (when (and helm-enable-shortcuts
  2115. (not (eq helm-digit-shortcut-count
  2116. (length helm-digit-overlays))))
  2117. (move-overlay (nth helm-digit-shortcut-count
  2118. helm-digit-overlays)
  2119. (point-at-bol)
  2120. (point-at-bol))
  2121. (incf helm-digit-shortcut-count)))
  2122. (defun helm-process-source--direct-insert-match (source)
  2123. "[EXPERIMENTAL] Insert candidates from `helm-candidate-buffer' in SOURCE."
  2124. (helm-log-eval (assoc-default 'name source))
  2125. (let ((helm-source-name (assoc-default 'name source))
  2126. content-buf)
  2127. (funcall (assoc-default 'candidates source))
  2128. (setq content-buf (helm-candidate-buffer))
  2129. (unless (helm-empty-buffer-p content-buf)
  2130. (helm-insert-header-from-source source)
  2131. (insert-buffer-substring content-buf)
  2132. ;; TODO call helm-put-digit-overlay-maybe with loop
  2133. )))
  2134. (defun helm-process-delayed-sources (delayed-sources &optional preselect)
  2135. "Process helm DELAYED-SOURCES.
  2136. Move selection to string or regexp PRESELECT if non--nil.
  2137. This function is called in `helm-process-delayed-sources-timer'
  2138. when emacs is idle for `helm-idle-delay'."
  2139. (with-helm-quittable
  2140. (helm-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
  2141. (with-current-buffer helm-buffer
  2142. (save-excursion
  2143. (goto-char (point-max))
  2144. (mapc 'helm-process-source delayed-sources)
  2145. (when (and (not (helm-empty-buffer-p))
  2146. ;; No selection yet.
  2147. (= (overlay-start helm-selection-overlay)
  2148. (overlay-end helm-selection-overlay)))
  2149. (helm-update-move-first-line 'without-hook)))
  2150. (when preselect (helm-preselect preselect))
  2151. (save-excursion
  2152. (goto-char (point-min))
  2153. (helm-log-run-hook 'helm-update-hook))
  2154. (helm-log-run-hook 'helm-after-update-hook))))
  2155. ;; (@* "Core: *helm* buffer contents")
  2156. (defvar helm-input-local nil)
  2157. (defvar helm-process-delayed-sources-timer nil)
  2158. (defun helm-update (&optional preselect)
  2159. "Update candidates list in `helm-buffer' according to `helm-pattern'.
  2160. Argument PRESELECT is a string or regexp used to move selection to a particular
  2161. place once updating is done. It should be used on single source because search
  2162. is done on whole `helm-buffer' and not on current source."
  2163. (helm-log "start update")
  2164. (setq helm-digit-shortcut-count 0)
  2165. (helm-kill-async-processes)
  2166. (with-current-buffer (helm-buffer-get)
  2167. (set (make-local-variable 'helm-input-local) helm-pattern)
  2168. (erase-buffer)
  2169. (when helm-enable-shortcuts
  2170. (mapc 'delete-overlay helm-digit-overlays))
  2171. (let (delayed-sources
  2172. normal-sources)
  2173. (unwind-protect ; Process normal sources and store delayed one's.
  2174. (setq delayed-sources
  2175. (loop for source in (remove-if-not 'helm-update-source-p
  2176. (helm-get-sources))
  2177. if (helm-delayed-source-p source)
  2178. collect source
  2179. else do (progn (push source normal-sources)
  2180. (helm-process-source source))))
  2181. (helm-log-eval
  2182. (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
  2183. (if helm-test-mode ; Need only to process sources.
  2184. (mapc 'helm-process-source delayed-sources)
  2185. (cond ((and preselect delayed-sources normal-sources)
  2186. ;; Preselection run here when there is
  2187. ;; normal AND delayed sources.
  2188. (helm-log "Update preselect candidate %s" preselect)
  2189. (helm-preselect preselect))
  2190. (delayed-sources ; Preselection and hooks will run later.
  2191. (helm-update-move-first-line 'without-hook))
  2192. (t ; No delayed sources, run the hooks now.
  2193. (helm-update-move-first-line)
  2194. (helm-log-run-hook 'helm-after-update-hook)
  2195. (when preselect
  2196. (helm-log "Update preselect candidate %s" preselect)
  2197. (helm-preselect preselect))))
  2198. (when delayed-sources
  2199. (helm-new-timer
  2200. 'helm-process-delayed-sources-timer
  2201. (run-with-idle-timer
  2202. ;; Be sure helm-idle-delay is >
  2203. ;; to helm-input-idle-delay
  2204. ;; otherwise use value of helm-input-idle-delay
  2205. ;; or 0.1 if == to 0.
  2206. (max helm-idle-delay helm-input-idle-delay 0.1) nil
  2207. 'helm-process-delayed-sources delayed-sources preselect))))
  2208. (helm-log "end update")))))
  2209. (defun helm-update-source-p (source)
  2210. "Wheter SOURCE need updating or not."
  2211. (and (or (not helm-source-filter)
  2212. (member (assoc-default 'name source) helm-source-filter))
  2213. (>= (length helm-pattern)
  2214. (helm-aif (assoc 'requires-pattern source)
  2215. (or (cdr it) 1)
  2216. 0))))
  2217. (defun helm-delayed-source-p (source)
  2218. "Wheter SOURCE is a delayed source or not."
  2219. (or (assoc 'delayed source)
  2220. (and helm-quick-update
  2221. (< (window-height (get-buffer-window (current-buffer)))
  2222. (line-number-at-pos (point-max))))))
  2223. (defun helm-update-move-first-line (&optional without-hook)
  2224. "Goto first line of `helm-buffer'."
  2225. (goto-char (point-min))
  2226. (unless without-hook
  2227. (save-excursion (helm-log-run-hook 'helm-update-hook)))
  2228. (helm-next-line))
  2229. (defun helm-force-update (&optional preselect)
  2230. "Force recalculation and update of candidates.
  2231. If current source has `update' attribute, a function without argument,
  2232. call it before update."
  2233. (interactive)
  2234. (let ((source (helm-get-current-source))
  2235. (selection (helm-get-selection nil t)))
  2236. (when source
  2237. (mapc 'helm-force-update--reinit
  2238. (helm-get-sources)))
  2239. (helm-update preselect)
  2240. ;; If preselect arg exists, `helm-update' should
  2241. ;; have moved to selection, otherwise do it now.
  2242. (unless preselect
  2243. (helm-keep-selection (assoc-default 'name source) selection))
  2244. (with-helm-window (recenter))))
  2245. (defun helm-force-update--reinit (source)
  2246. "Reinit SOURCE by calling his update and/or init functions."
  2247. (helm-aif (helm-funcall-with-source
  2248. source 'helm-candidate-buffer)
  2249. (kill-buffer it))
  2250. (dolist (attr '(update init))
  2251. (helm-aif (assoc-default attr source)
  2252. (helm-funcall-with-source source it)))
  2253. (helm-remove-candidate-cache source))
  2254. (defun helm-keep-selection (source selection)
  2255. "Switch to SOURCE and goto SELECTION."
  2256. (when (and source selection)
  2257. (with-helm-window
  2258. (helm-goto-source source)
  2259. (forward-char -1)
  2260. (if (search-forward selection nil t)
  2261. (forward-line 0)
  2262. (goto-char (point-min))
  2263. (forward-line 1))
  2264. (helm-mark-current-line))))
  2265. (defun helm-remove-candidate-cache (source)
  2266. "Remove SOURCE from `helm-candidate-cache'."
  2267. (setq helm-candidate-cache
  2268. (delete (assoc (assoc-default 'name source)
  2269. helm-candidate-cache)
  2270. helm-candidate-cache)))
  2271. (defun helm-insert-match (match insert-function source)
  2272. "Insert MATCH into `helm-buffer' with INSERT-FUNCTION for SOURCE.
  2273. If MATCH is a list then insert the string intended to appear on the display
  2274. and store the real value in a text property."
  2275. (let ((start (point-at-bol (point)))
  2276. (dispvalue (or (car-safe match) match))
  2277. (realvalue (cdr-safe match)))
  2278. (setq dispvalue
  2279. (cond ((symbolp dispvalue) (symbol-name dispvalue))
  2280. ((numberp dispvalue) (number-to-string dispvalue))
  2281. (t dispvalue)))
  2282. (when (stringp dispvalue)
  2283. (funcall insert-function dispvalue)
  2284. ;; Some sources with candidates-in-buffer have already added
  2285. ;; 'helm-realvalue property when creating candidate buffer.
  2286. (unless (get-text-property start 'helm-realvalue)
  2287. (and realvalue
  2288. (put-text-property start (point-at-eol)
  2289. 'helm-realvalue realvalue)))
  2290. (when helm-source-in-each-line-flag
  2291. (put-text-property start (point-at-eol) 'helm-source source))
  2292. (funcall insert-function "\n"))))
  2293. (defun helm-insert-header-from-source (source)
  2294. "Insert SOURCE name in `helm-buffer' header.
  2295. Maybe insert by overlay additional info after source name if SOURCE have
  2296. header-name attribute."
  2297. (let ((name (assoc-default 'name source)))
  2298. (helm-insert-header
  2299. name
  2300. (helm-aif (assoc-default 'header-name source)
  2301. (helm-funcall-with-source source it name)))))
  2302. (defun helm-insert-header (name &optional display-string)
  2303. "Insert header of source NAME into the helm buffer.
  2304. If DISPLAY-STRING is non--nil and a string, display this additional info
  2305. after the source name by overlay."
  2306. (unless (bobp)
  2307. (let ((start (point)))
  2308. (insert "\n")
  2309. (put-text-property start (point) 'helm-header-separator t)))
  2310. (let ((start (point)))
  2311. (insert name)
  2312. (put-text-property (point-at-bol)
  2313. (point-at-eol) 'helm-header t)
  2314. (when display-string
  2315. (overlay-put (make-overlay (point-at-bol) (point-at-eol))
  2316. 'display display-string))
  2317. (insert "\n")
  2318. (put-text-property start (point) 'face helm-header-face)))
  2319. (defun helm-insert-candidate-separator ()
  2320. "Insert separator of candidates into the helm buffer."
  2321. (insert helm-candidate-separator)
  2322. (put-text-property (point-at-bol)
  2323. (point-at-eol) 'helm-candidate-separator t)
  2324. (insert "\n"))
  2325. ;; (@* "Core: async process")
  2326. (defun helm-output-filter (process string)
  2327. "From PROCESS process output STRING."
  2328. (helm-output-filter-1 (assoc process helm-async-processes) string))
  2329. (defun helm-output-filter-1 (process-assoc string)
  2330. (helm-log-eval string)
  2331. (with-current-buffer helm-buffer
  2332. (let ((source (cdr process-assoc)))
  2333. (save-excursion
  2334. (helm-aif (assoc-default 'insertion-marker source)
  2335. (goto-char it)
  2336. (goto-char (point-max))
  2337. (helm-insert-header-from-source source)
  2338. (setcdr process-assoc
  2339. (append source `((insertion-marker . ,(point-marker))))))
  2340. (helm-output-filter--process-source
  2341. (car process-assoc) string source
  2342. (helm-candidate-number-limit source))))
  2343. (helm-output-filter--post-process)))
  2344. (defun helm-output-filter--process-source (process string source limit)
  2345. (dolist (candidate (helm-transform-candidates
  2346. (helm-output-filter--collect-candidates
  2347. (split-string string "\n")
  2348. (assoc 'incomplete-line source))
  2349. source t))
  2350. (if (not (assq 'multiline source))
  2351. (helm-insert-match candidate 'insert-before-markers source)
  2352. (let ((start (point)))
  2353. (helm-insert-candidate-separator)
  2354. (helm-insert-match candidate 'insert-before-markers source)
  2355. (put-text-property start (point) 'helm-multiline t)))
  2356. (incf (cdr (assoc 'item-count source)))
  2357. (when (>= (assoc-default 'item-count source) limit)
  2358. (helm-kill-async-process process)
  2359. (return))))
  2360. (defun helm-output-filter--collect-candidates (lines incomplete-line-info)
  2361. (helm-log-eval (cdr incomplete-line-info))
  2362. (butlast
  2363. (loop for line in lines collect
  2364. (if (cdr incomplete-line-info)
  2365. (prog1
  2366. (concat (cdr incomplete-line-info) line)
  2367. (setcdr incomplete-line-info nil))
  2368. line)
  2369. finally (setcdr incomplete-line-info line))))
  2370. (defun helm-output-filter--post-process ()
  2371. (helm-log-run-hook 'helm-update-hook)
  2372. (helm-aif (get-buffer-window helm-buffer 'visible)
  2373. (save-selected-window
  2374. (select-window it)
  2375. (helm-skip-noncandidate-line 'next)
  2376. (helm-mark-current-line))))
  2377. (defun helm-kill-async-processes ()
  2378. "Kill all known asynchronous processes of `helm-async-processes'."
  2379. (mapc 'helm-kill-async-process (mapcar 'car helm-async-processes))
  2380. (setq helm-async-processes nil))
  2381. (defun helm-kill-async-process (process)
  2382. "Kill PROCESS and detach the associated functions."
  2383. (set-process-filter process nil)
  2384. (delete-process process))
  2385. ;; (@* "Core: action")
  2386. (defun helm-execute-selection-action (&optional
  2387. selection action
  2388. preserve-saved-action)
  2389. "If a candidate SELECTION is present then perform the associated ACTION on it.
  2390. If PRESERVE-SAVED-ACTION is non-nil don't save action."
  2391. (helm-log "executing action")
  2392. (setq action (helm-get-default-action
  2393. (or action
  2394. helm-saved-action
  2395. (if (get-buffer helm-action-buffer)
  2396. (helm-get-selection helm-action-buffer)
  2397. (helm-get-action)))))
  2398. (let ((source (or helm-saved-current-source
  2399. (helm-get-current-source))))
  2400. (setq selection (or selection
  2401. (helm-get-selection)
  2402. (and (assoc 'accept-empty source) "")))
  2403. (unless preserve-saved-action (setq helm-saved-action nil))
  2404. (if (and selection action)
  2405. (helm-funcall-with-source
  2406. source action
  2407. (helm-coerce-selection selection source)))))
  2408. (defun helm-coerce-selection (selection source)
  2409. "Apply coerce attribute function to SELECTION in SOURCE.
  2410. Coerce source with coerce function."
  2411. (helm-aif (assoc-default 'coerce source)
  2412. (helm-funcall-with-source source it selection)
  2413. selection))
  2414. (defun helm-get-default-action (action)
  2415. "Get the first ACTION value of action list in source."
  2416. (if (and (listp action) (not (functionp action)))
  2417. (cdar action)
  2418. action))
  2419. (defun helm-select-action ()
  2420. "Select an action for the currently selected candidate.
  2421. If action buffer is selected, back to the helm buffer."
  2422. (interactive)
  2423. (helm-log-run-hook 'helm-select-action-hook)
  2424. (cond ((get-buffer-window helm-action-buffer 'visible)
  2425. (set-window-buffer (get-buffer-window helm-action-buffer)
  2426. helm-buffer)
  2427. (kill-buffer helm-action-buffer)
  2428. (helm-set-pattern helm-input 'noupdate))
  2429. (t
  2430. (setq helm-saved-selection (helm-get-selection))
  2431. (unless helm-saved-selection
  2432. (error "Nothing is selected"))
  2433. (setq helm-saved-current-source (helm-get-current-source))
  2434. (let ((actions (helm-get-action)))
  2435. (if (functionp actions)
  2436. (message "Sole action: %s" actions)
  2437. (helm-show-action-buffer actions)
  2438. (helm-delete-minibuffer-contents)
  2439. (setq helm-pattern 'dummy) ; so that it differs from the previous one
  2440. (helm-check-minibuffer-input))))))
  2441. (defun helm-show-action-buffer (actions)
  2442. (with-current-buffer (get-buffer-create helm-action-buffer)
  2443. (erase-buffer)
  2444. (buffer-disable-undo)
  2445. (set-window-buffer (get-buffer-window helm-buffer) helm-action-buffer)
  2446. (set (make-local-variable 'helm-sources)
  2447. `(((name . "Actions")
  2448. (volatile)
  2449. (candidates . ,actions)
  2450. (candidate-number-limit))))
  2451. (set (make-local-variable 'helm-source-filter) nil)
  2452. (set (make-local-variable 'helm-selection-overlay) nil)
  2453. (set (make-local-variable 'helm-digit-overlays) nil)
  2454. (helm-initialize-overlays helm-action-buffer)))
  2455. ;; (@* "Core: selection")
  2456. (defun helm-move-selection-common (move-func unit direction)
  2457. "Move the selection marker to a new position wit function MOVE-FUNC.
  2458. It is determined by UNIT and DIRECTION."
  2459. (unless (or (helm-empty-buffer-p (helm-buffer-get))
  2460. (not (helm-window)))
  2461. (with-helm-window
  2462. (helm-log-run-hook 'helm-move-selection-before-hook)
  2463. (funcall move-func)
  2464. (helm-skip-noncandidate-line direction)
  2465. (helm-display-source-at-screen-top-maybe unit)
  2466. (when (helm-get-previous-header-pos)
  2467. (helm-mark-current-line))
  2468. (helm-display-mode-line (helm-get-current-source))
  2469. (helm-log-run-hook 'helm-move-selection-after-hook))))
  2470. (defun helm-display-source-at-screen-top-maybe (unit)
  2471. (when (and helm-display-source-at-screen-top (eq unit 'source))
  2472. (set-window-start (selected-window)
  2473. (save-excursion (forward-line -1) (point)))))
  2474. (defun helm-skip-noncandidate-line (direction)
  2475. (helm-skip-header-and-separator-line direction)
  2476. (and (bobp) (forward-line 1)) ;skip first header
  2477. (and (eobp) (forward-line -1))) ;avoid last empty line
  2478. (defun helm-skip-header-and-separator-line (direction)
  2479. (while (and (not (bobp))
  2480. (or (helm-pos-header-line-p)
  2481. (helm-pos-candidate-separator-p)))
  2482. (forward-line (if (and (eq direction 'previous)
  2483. (not (eq (point-at-bol) (point-min))))
  2484. -1 1))))
  2485. (defvar helm-mode-line-string-real nil)
  2486. (defun helm-display-mode-line (source)
  2487. (set (make-local-variable 'helm-mode-line-string)
  2488. (helm-interpret-value (or (assoc-default 'mode-line source)
  2489. (default-value 'helm-mode-line-string))
  2490. source))
  2491. (if helm-mode-line-string
  2492. (setq mode-line-format
  2493. '(" " mode-line-buffer-identification " "
  2494. (line-number-mode "L%l") " " (helm-follow-mode "(F) ")
  2495. (:eval (helm-show-candidate-number
  2496. (when (listp helm-mode-line-string)
  2497. (car helm-mode-line-string))))
  2498. " " helm-mode-line-string-real "-%-")
  2499. helm-mode-line-string-real
  2500. (substitute-command-keys (if (listp helm-mode-line-string)
  2501. (cadr helm-mode-line-string)
  2502. helm-mode-line-string)))
  2503. (setq mode-line-format
  2504. (default-value 'mode-line-format)))
  2505. (setq header-line-format
  2506. (helm-interpret-value (assoc-default 'header-line source) source)))
  2507. (defun helm-show-candidate-number (&optional name)
  2508. "Used to display candidate number in mode-line.
  2509. You can specify NAME of candidates e.g \"Buffers\" otherwise
  2510. it is \"Candidate\(s\)\" by default."
  2511. (propertize
  2512. (format "[%s %s]"
  2513. (helm-approximate-candidate-number 'in-current-source)
  2514. (or name "Candidate(s)"))
  2515. 'face 'helm-candidate-number))
  2516. (defun helm-previous-line ()
  2517. "Move selection to the previous line."
  2518. (interactive)
  2519. (helm-move-selection-common
  2520. (lambda ()
  2521. (if (not (helm-pos-multiline-p))
  2522. (forward-line -1) ;double forward-line is meaningful
  2523. (forward-line -1) ;because evaluation order is important
  2524. (helm-skip-header-and-separator-line 'previous)
  2525. (let ((header-pos (helm-get-previous-header-pos))
  2526. (separator-pos (helm-get-previous-candidate-separator-pos)))
  2527. (when header-pos
  2528. (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
  2529. header-pos ; first candidate
  2530. separator-pos))
  2531. (forward-line 1)))))
  2532. 'line 'previous))
  2533. (defun helm-next-line ()
  2534. "Move selection to the next line."
  2535. (interactive)
  2536. (helm-move-selection-common
  2537. (lambda ()
  2538. (if (not (helm-pos-multiline-p))
  2539. (forward-line 1)
  2540. (let ((header-pos (helm-get-next-header-pos))
  2541. (separator-pos (helm-get-next-candidate-separator-pos)))
  2542. (cond ((and separator-pos
  2543. (or (null header-pos) (< separator-pos header-pos)))
  2544. (goto-char separator-pos))
  2545. (header-pos
  2546. (goto-char header-pos))))))
  2547. 'line 'next))
  2548. (defun helm-previous-page ()
  2549. "Move selection back with a pageful."
  2550. (interactive)
  2551. (helm-move-selection-common
  2552. (lambda ()
  2553. (condition-case nil
  2554. (scroll-down)
  2555. (beginning-of-buffer (goto-char (point-min)))))
  2556. 'page 'previous))
  2557. (defun helm-next-page ()
  2558. "Move selection forward with a pageful."
  2559. (interactive)
  2560. (helm-move-selection-common
  2561. (lambda ()
  2562. (condition-case nil
  2563. (scroll-up)
  2564. (end-of-buffer (goto-char (point-max)))))
  2565. 'page 'next))
  2566. (defun helm-beginning-of-buffer ()
  2567. "Move selection at the top."
  2568. (interactive)
  2569. (helm-move-selection-common
  2570. (lambda () (goto-char (point-min)))
  2571. 'edge 'previous))
  2572. (defun helm-end-of-buffer ()
  2573. "Move selection at the bottom."
  2574. (interactive)
  2575. (helm-move-selection-common
  2576. (lambda () (goto-char (point-max)))
  2577. 'edge 'next))
  2578. (defun helm-previous-source ()
  2579. "Move selection to the previous source."
  2580. (interactive)
  2581. (helm-move-selection-common
  2582. (lambda ()
  2583. (forward-line -1)
  2584. (if (bobp)
  2585. (goto-char (point-max))
  2586. (helm-skip-header-and-separator-line 'previous))
  2587. (goto-char (helm-get-previous-header-pos))
  2588. (forward-line 1))
  2589. 'source 'previous))
  2590. (defun helm-next-source ()
  2591. "Move selection to the next source."
  2592. (interactive)
  2593. (helm-move-selection-common
  2594. (lambda ()
  2595. (goto-char (or (helm-get-next-header-pos) (point-min))))
  2596. 'source 'next))
  2597. (defun helm-goto-source (source-or-name)
  2598. "Move the selection to the source SOURCE-OR-NAME."
  2599. (helm-move-selection-common
  2600. (lambda ()
  2601. (goto-char (point-min))
  2602. (let ((name (if (stringp source-or-name) source-or-name
  2603. (assoc-default 'name source-or-name))))
  2604. (condition-case err
  2605. (while (not (string= name (helm-current-line-contents)))
  2606. (goto-char (helm-get-next-header-pos)))
  2607. (error (message "")))))
  2608. 'source 'next))
  2609. (defun helm-mark-current-line (&optional resumep)
  2610. "Move `helm-selection-overlay' to current line.
  2611. Note that this is not related with visibles marks, which are used
  2612. to mark candidates."
  2613. (with-helm-window
  2614. (when resumep
  2615. (goto-char helm-selection-point))
  2616. (move-overlay
  2617. helm-selection-overlay (point-at-bol)
  2618. (if (helm-pos-multiline-p)
  2619. (let ((header-pos (helm-get-next-header-pos))
  2620. (separator-pos (helm-get-next-candidate-separator-pos)))
  2621. (or (and (null header-pos) separator-pos)
  2622. (and header-pos separator-pos (< separator-pos header-pos)
  2623. separator-pos)
  2624. header-pos
  2625. (point-max)))
  2626. (1+ (point-at-eol))))
  2627. (setq helm-selection-point (overlay-start helm-selection-overlay)))
  2628. (helm-follow-execute-persistent-action-maybe))
  2629. (defun helm-this-command-key ()
  2630. (event-basic-type (elt (this-command-keys-vector) 0)))
  2631. ;; (progn (read-key-sequence "Key: ") (p (helm-this-command-key)))
  2632. (defun helm-select-with-shortcut-internal (types get-key-func)
  2633. (if (memq helm-enable-shortcuts types)
  2634. (save-selected-window
  2635. (select-window (helm-window))
  2636. (let* ((key (funcall get-key-func))
  2637. (overlay (ignore-errors (nth (position key helm-shortcut-keys)
  2638. helm-digit-overlays))))
  2639. (if (not (and overlay (overlay-buffer overlay)))
  2640. (when (numberp key)
  2641. (select-window (minibuffer-window))
  2642. (self-insert-command 1))
  2643. (goto-char (overlay-start overlay))
  2644. (helm-mark-current-line)
  2645. (helm-exit-minibuffer))))
  2646. (self-insert-command 1)))
  2647. (defun helm-select-with-prefix-shortcut ()
  2648. "Invoke default action with prefix shortcut."
  2649. (interactive)
  2650. (helm-select-with-shortcut-internal
  2651. '(prefix)
  2652. (lambda () (read-event "Select shortcut key: "))))
  2653. (defun helm-select-with-digit-shortcut ()
  2654. "Invoke default action with digit/alphabet shortcut."
  2655. (interactive)
  2656. (helm-select-with-shortcut-internal
  2657. '(alphabet t) 'helm-this-command-key))
  2658. ;; (setq helm-enable-shortcuts 'prefix)
  2659. ;; (define-key helm-map "@" 'helm-select-with-prefix-shortcut)
  2660. ;; (define-key helm-map (kbd "<f18>") 'helm-select-with-prefix-shortcut)
  2661. (defvar helm-exit-status 0
  2662. "Flag to inform whether helm have exited or quitted.
  2663. Exit with 0 mean helm have exited executing an action.
  2664. Exit with 1 mean helm have quitted with \\[keyboard-quit]
  2665. It is useful for example to restore a window config if helm abort
  2666. in special cases.
  2667. See `helm-exit-minibuffer' and `helm-keyboard-quit'.")
  2668. (defvar helm-minibuffer-confirm-state nil)
  2669. (defun helm-confirm-and-exit-minibuffer ()
  2670. "Maybe ask for confirmation when exiting helm.
  2671. It is similar to `minibuffer-complete-and-exit' adapted to helm.
  2672. If `minibuffer-completion-confirm' value is 'confirm,
  2673. send in minibuffer confirm message and exit on next hit.
  2674. If `minibuffer-completion-confirm' value is t,
  2675. don't exit and send message 'no match'."
  2676. (interactive)
  2677. (let* ((empty-buffer-p (with-current-buffer helm-buffer
  2678. (eq (point-min) (point-max))))
  2679. (unknow (and (not empty-buffer-p)
  2680. (string= (get-text-property
  2681. 0 'display (helm-get-selection nil 'withprop))
  2682. "[?]"))))
  2683. (cond ((and (or empty-buffer-p unknow)
  2684. (eq minibuffer-completion-confirm 'confirm))
  2685. (setq helm-minibuffer-confirm-state
  2686. 'confirm)
  2687. (setq minibuffer-completion-confirm nil)
  2688. (minibuffer-message " [confirm]"))
  2689. ((and (or empty-buffer-p unknow)
  2690. (eq minibuffer-completion-confirm t))
  2691. (minibuffer-message " [No match]"))
  2692. (t
  2693. (setq helm-minibuffer-confirm-state nil)
  2694. (helm-exit-minibuffer)))))
  2695. (add-hook 'helm-after-update-hook 'helm-confirm-and-exit-hook)
  2696. (defun helm-confirm-and-exit-hook ()
  2697. "Restore `minibuffer-completion-confirm' when helm update."
  2698. (unless (or (eq minibuffer-completion-confirm t)
  2699. (not helm-minibuffer-confirm-state))
  2700. (setq minibuffer-completion-confirm
  2701. helm-minibuffer-confirm-state)))
  2702. (defun helm-exit-minibuffer ()
  2703. "Select the current candidate by exiting the minibuffer."
  2704. (interactive)
  2705. (unless helm-current-prefix-arg
  2706. (setq helm-current-prefix-arg current-prefix-arg))
  2707. (setq helm-exit-status 0)
  2708. (exit-minibuffer))
  2709. (defun helm-keyboard-quit ()
  2710. "Quit minibuffer in helm.
  2711. If action buffer is displayed, kill it."
  2712. (interactive)
  2713. (when (get-buffer-window helm-action-buffer 'visible)
  2714. (kill-buffer helm-action-buffer))
  2715. (setq helm-exit-status 1)
  2716. (abort-recursive-edit))
  2717. (defun helm-get-next-header-pos ()
  2718. "Return the position of the next header from point."
  2719. (next-single-property-change (point) 'helm-header))
  2720. (defun helm-get-previous-header-pos ()
  2721. "Return the position of the previous header from point."
  2722. (previous-single-property-change (point) 'helm-header))
  2723. (defun helm-pos-multiline-p ()
  2724. "Return non-nil if the current position is in the multiline source region."
  2725. (get-text-property (point) 'helm-multiline))
  2726. (defun helm-get-next-candidate-separator-pos ()
  2727. "Return the position of the next candidate separator from point."
  2728. (next-single-property-change (point) 'helm-candidate-separator))
  2729. (defun helm-get-previous-candidate-separator-pos ()
  2730. "Return the position of the previous candidate separator from point."
  2731. (previous-single-property-change (point) 'helm-candidate-separator))
  2732. (defun helm-pos-header-line-p ()
  2733. "Return t if the current line is a header line."
  2734. (or (get-text-property (point-at-bol) 'helm-header)
  2735. (get-text-property (point-at-bol) 'helm-header-separator)))
  2736. (defun helm-pos-candidate-separator-p ()
  2737. "Return t if the current line is a candidate separator."
  2738. (get-text-property (point-at-bol) 'helm-candidate-separator))
  2739. ;; (@* "Core: help")
  2740. (defun helm-help-internal (bufname insert-content-fn)
  2741. "Show long message during `helm' session in BUFNAME.
  2742. INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
  2743. (save-window-excursion
  2744. (select-window (helm-window))
  2745. (delete-other-windows)
  2746. (switch-to-buffer (get-buffer-create bufname))
  2747. (erase-buffer)
  2748. (funcall insert-content-fn)
  2749. (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
  2750. (setq cursor-type nil)
  2751. (goto-char 1)
  2752. (helm-help-event-loop)))
  2753. (defun helm-help-event-loop ()
  2754. (ignore-errors
  2755. (loop for event = (read-event) do
  2756. (case event
  2757. ((?\C-v ? ) (scroll-up))
  2758. ((?\M-v ?b) (scroll-down))
  2759. (t (return))))))
  2760. (defun helm-help ()
  2761. "Help of `helm'."
  2762. (interactive)
  2763. (helm-help-internal
  2764. " *Helm Help*"
  2765. (lambda ()
  2766. (insert (substitute-command-keys
  2767. (helm-interpret-value (or (assoc-default
  2768. 'help-message
  2769. (helm-get-current-source))
  2770. helm-help-message))))
  2771. (org-mode))))
  2772. (defun helm-debug-output ()
  2773. "Show all helm-related variables at this time."
  2774. (interactive)
  2775. (helm-help-internal " *Helm Debug*" 'helm-debug-output-function))
  2776. (defun helm-debug-output-function (&optional vars)
  2777. (message "Calculating all helm-related values...")
  2778. (insert "If you debug some variables or forms, set `helm-debug-forms'
  2779. to a list of forms.\n\n")
  2780. (dolist (v (or vars
  2781. helm-debug-forms
  2782. (apropos-internal "^helm-" 'boundp)))
  2783. (insert "** "
  2784. (pp-to-string v) "\n"
  2785. (pp-to-string (with-current-buffer helm-buffer (eval v))) "\n"))
  2786. (message "Calculating all helm-related values...Done"))
  2787. ;; (@* "Core: misc")
  2788. (defun helm-kill-buffer-hook ()
  2789. "Remove tick entry from `helm-tick-hash' when killing a buffer."
  2790. (loop for key being the hash-keys in helm-tick-hash
  2791. if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
  2792. do (remhash key helm-tick-hash)))
  2793. (add-hook 'kill-buffer-hook 'helm-kill-buffer-hook)
  2794. (defun helm-preselect (candidate-or-regexp)
  2795. "Move `helm-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
  2796. (with-helm-window
  2797. (when candidate-or-regexp
  2798. (goto-char (point-min))
  2799. ;; go to first candidate of first source
  2800. (forward-line 1)
  2801. (let ((start (point)))
  2802. (or (re-search-forward
  2803. (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
  2804. (re-search-forward candidate-or-regexp nil t)
  2805. (search-forward candidate-or-regexp nil t)
  2806. (goto-char start))))
  2807. (helm-mark-current-line)))
  2808. (defun helm-delete-current-selection ()
  2809. "Delete the currently selected item."
  2810. (interactive)
  2811. (with-helm-window
  2812. (cond ((helm-pos-multiline-p)
  2813. (helm-aif (helm-get-next-candidate-separator-pos)
  2814. (delete-region (point-at-bol)
  2815. (1+ (progn (goto-char it) (point-at-eol))))
  2816. ;; last candidate
  2817. (goto-char (helm-get-previous-candidate-separator-pos))
  2818. (delete-region (point-at-bol) (point-max)))
  2819. (when (helm-end-of-source-p)
  2820. (goto-char (or (helm-get-previous-candidate-separator-pos)
  2821. (point-min)))
  2822. (forward-line 1)))
  2823. (t
  2824. (delete-region (point-at-bol) (1+ (point-at-eol)))
  2825. (when (helm-end-of-source-p) (forward-line -1))))
  2826. (helm-mark-current-line)))
  2827. (defun helm-end-of-source-p ()
  2828. "Return non--nil if we are at eob or end of source."
  2829. (save-excursion
  2830. (forward-line 1)
  2831. (or (eq (point-at-bol) (point-at-eol))
  2832. (helm-pos-header-line-p)
  2833. (eobp))))
  2834. (defun helm-edit-current-selection-internal (func)
  2835. (with-helm-window
  2836. (beginning-of-line)
  2837. (let ((realvalue (get-text-property (point) 'helm-realvalue)))
  2838. (funcall func)
  2839. (beginning-of-line)
  2840. (and realvalue
  2841. (put-text-property (point) (point-at-eol)
  2842. 'helm-realvalue realvalue))
  2843. (helm-mark-current-line))))
  2844. (defmacro helm-edit-current-selection (&rest forms)
  2845. "Evaluate FORMS at current selection in the helm buffer.
  2846. You can edit the line."
  2847. (declare (indent 0) (debug t))
  2848. `(helm-edit-current-selection-internal
  2849. (lambda () ,@forms)))
  2850. (defun helm-set-pattern (pattern &optional noupdate)
  2851. "Set minibuffer contents to PATTERN.
  2852. if optional NOUPDATE is non-nil, helm buffer is not changed."
  2853. (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
  2854. (delete-minibuffer-contents)
  2855. (insert pattern))
  2856. (when noupdate
  2857. (setq helm-pattern pattern)
  2858. (helm-hooks 'cleanup)
  2859. (run-with-idle-timer 0 nil 'helm-hooks 'setup)))
  2860. (defun helm-delete-minibuffer-contents ()
  2861. "Same as `delete-minibuffer-contents' but this is a command."
  2862. (interactive)
  2863. (helm-set-pattern ""))
  2864. (defalias 'helm-delete-minibuffer-content 'helm-delete-minibuffer-contents)
  2865. ;;; Plugins
  2866. ;;
  2867. ;; (@* "Built-in plug-in: type")
  2868. (defun helm-compile-source--type (source)
  2869. (helm-aif (assoc-default 'type source)
  2870. (append source (assoc-default it helm-type-attributes) nil)
  2871. source))
  2872. ;; `define-helm-type-attribute' is public API.
  2873. (defun helm-add-type-attribute (type definition)
  2874. (helm-aif (assq type helm-type-attributes)
  2875. (setq helm-type-attributes (delete it helm-type-attributes)))
  2876. (push (cons type definition) helm-type-attributes))
  2877. (defvar helm-types nil)
  2878. (defun helm-document-type-attribute (type doc)
  2879. (add-to-list 'helm-types type t)
  2880. (put type 'helm-typeattrdoc
  2881. (concat "- " (symbol-name type) "\n\n" doc "\n")))
  2882. (defadvice documentation-property (after helm-document-type-attribute activate)
  2883. "Display type attributes' documentation as `helm-type-attributes' docstring."
  2884. (when (eq (ad-get-arg 0) 'helm-type-attributes)
  2885. (setq ad-return-value
  2886. (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
  2887. (mapconcat (lambda (sym) (get sym 'helm-typeattrdoc))
  2888. helm-types "\n")))))
  2889. ;; (@* "Built-in plug-in: dummy")
  2890. (defun helm-dummy-candidate (candidate source)
  2891. "Use `helm-pattern' as CANDIDATE in SOURCE."
  2892. ;; `source' is defined in filtered-candidate-transformer
  2893. (list helm-pattern))
  2894. (defun helm-compile-source--dummy (source)
  2895. (if (assoc 'dummy source)
  2896. (append source
  2897. '((candidates "dummy")
  2898. (accept-empty)
  2899. (match identity)
  2900. (filtered-candidate-transformer . helm-dummy-candidate)
  2901. (disable-shortcuts)
  2902. (volatile)))
  2903. source))
  2904. ;; (@* "Built-in plug-in: disable-shortcuts")
  2905. (defvar helm-orig-enable-shortcuts nil)
  2906. (defun helm-save-enable-shortcuts ()
  2907. (helm-once
  2908. (lambda ()
  2909. (setq helm-orig-enable-shortcuts helm-enable-shortcuts
  2910. helm-enable-shortcuts nil))))
  2911. (defun helm-compile-source--disable-shortcuts (source)
  2912. (if (assoc 'disable-shortcuts source)
  2913. (append `((init ,@(helm-mklist (assoc-default 'init source))
  2914. helm-save-enable-shortcuts)
  2915. (resume ,@(helm-mklist (assoc-default 'resume source))
  2916. helm-save-enable-shortcuts)
  2917. (cleanup ,@(helm-mklist (assoc-default 'cleanup source))
  2918. (lambda () (setq helm-enable-shortcuts
  2919. helm-orig-enable-shortcuts))))
  2920. source)
  2921. source))
  2922. ;; (@* "Built-in plug-in: candidates-in-buffer")
  2923. (defun helm-candidates-in-buffer ()
  2924. "Get candidates from the candidates buffer according to `helm-pattern'.
  2925. BUFFER is `helm-candidate-buffer' by default. Each
  2926. candidate must be placed in one line. This function is meant to
  2927. be used in candidates-in-buffer or candidates attribute of an
  2928. helm source. Especially fast for many (1000+) candidates.
  2929. eg.
  2930. '((name . \"many files\")
  2931. (init . (lambda () (with-current-buffer (helm-candidate-buffer 'local)
  2932. (insert-many-filenames))))
  2933. (search re-search-forward) ; optional
  2934. (candidates-in-buffer)
  2935. (type . file))
  2936. +===============================================================+
  2937. | The new way of making and narrowing candidates: Using buffers |
  2938. +===============================================================+
  2939. By default, `helm' makes candidates by evaluating the
  2940. candidates function, then narrows them by `string-match' for each
  2941. candidate.
  2942. But this way is very slow for many candidates. The new way is
  2943. storing all candidates in a buffer and narrowing them by
  2944. `re-search-forward'. Search function is customizable by search
  2945. attribute. The important point is that buffer processing is MUCH
  2946. FASTER than string list processing and is the Emacs way.
  2947. The init function writes all candidates to a newly-created
  2948. candidate buffer. The candidates buffer is created or specified
  2949. by `helm-candidate-buffer'. Candidates are stored in a line.
  2950. The candidates function narrows all candidates, IOW creates a
  2951. subset of candidates dynamically. It is the task of
  2952. `helm-candidates-in-buffer'. As long as
  2953. `helm-candidate-buffer' is used,`(candidates-in-buffer)' is
  2954. sufficient in most cases.
  2955. Note that `(candidates-in-buffer)' is shortcut of three attributes:
  2956. (candidates . helm-candidates-in-buffer)
  2957. (volatile)
  2958. (match identity)
  2959. And `(candidates-in-buffer . func)' is shortcut of three attributes:
  2960. (candidates . func)
  2961. (volatile)
  2962. (match identity)
  2963. The expansion is performed in `helm-get-sources'.
  2964. The candidates-in-buffer attribute implies the volatile attribute.
  2965. The volatile attribute is needed because `helm-candidates-in-buffer'
  2966. creates candidates dynamically and need to be called everytime
  2967. `helm-pattern' changes.
  2968. Because `helm-candidates-in-buffer' plays the role of `match' attribute
  2969. function, specifying `(match identity)' makes the source slightly faster.
  2970. To customize `helm-candidates-in-buffer' behavior, use search,
  2971. get-line and search-from-end attributes. See also `helm-sources' docstring."
  2972. (declare (special source))
  2973. (helm-candidates-in-buffer-1
  2974. (helm-candidate-buffer)
  2975. helm-pattern
  2976. (or (assoc-default 'get-line source)
  2977. #'buffer-substring-no-properties)
  2978. ;; use external variable `source'.
  2979. (or (assoc-default 'search source)
  2980. (if (assoc 'search-from-end source)
  2981. '(helm-candidates-in-buffer-search-from-end)
  2982. '(helm-candidates-in-buffer-search-from-start)))
  2983. (helm-candidate-number-limit source)
  2984. (assoc 'search-from-end source)))
  2985. (defun helm-candidates-in-buffer-search-from-start (pattern)
  2986. "Search PATTERN with `re-search-forward' with bound and noerror args."
  2987. (re-search-forward pattern nil t))
  2988. (defun helm-candidates-in-buffer-search-from-end (pattern)
  2989. "Search PATTERN with `re-search-backward' with bound and noerror args."
  2990. (re-search-backward pattern nil t))
  2991. (defun helm-candidates-in-buffer-1 (buffer pattern get-line-fn
  2992. search-fns limit search-from-end)
  2993. ;; buffer == nil when candidates buffer does not exist.
  2994. (when buffer
  2995. (with-current-buffer buffer
  2996. (let ((start-point (if search-from-end (point-max) (point-min)))
  2997. (endp (if search-from-end #'bobp #'eobp)))
  2998. (goto-char (1- start-point))
  2999. (if (string= pattern "")
  3000. (helm-initial-candidates-from-candidate-buffer
  3001. endp get-line-fn limit search-from-end)
  3002. (helm-search-from-candidate-buffer
  3003. pattern get-line-fn search-fns limit search-from-end
  3004. start-point endp))))))
  3005. (defun helm-point-is-moved (proc)
  3006. "If point is moved after executing PROC, return t, otherwise nil."
  3007. (/= (point) (progn (funcall proc) (point))))
  3008. (defun helm-search-from-candidate-buffer (pattern get-line-fn search-fns
  3009. limit search-from-end
  3010. start-point endp)
  3011. (let (buffer-read-only
  3012. matches exit newmatches)
  3013. (helm-search-from-candidate-buffer-internal
  3014. (lambda ()
  3015. (clrhash helm-cib-hash)
  3016. (dolist (searcher search-fns)
  3017. (goto-char start-point)
  3018. (setq newmatches nil)
  3019. (loop with item-count = 0
  3020. while (funcall searcher pattern)
  3021. for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
  3022. do (helm-accumulate-candidates-internal
  3023. cand newmatches helm-cib-hash item-count limit)
  3024. unless (helm-point-is-moved
  3025. (lambda ()
  3026. (if search-from-end
  3027. (goto-char (1- (point-at-bol)))
  3028. (forward-line 1))))
  3029. return nil)
  3030. (setq matches (append matches (nreverse newmatches)))
  3031. (if exit (return)))
  3032. (delq nil matches)))))
  3033. (defun helm-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
  3034. (delq nil (loop with next-line-fn =
  3035. (if search-from-end
  3036. (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
  3037. #'forward-line)
  3038. until (funcall endp)
  3039. for i from 1 to limit
  3040. collect (funcall get-line-fn (point-at-bol) (point-at-eol))
  3041. do (funcall next-line-fn 1))))
  3042. (defun helm-search-from-candidate-buffer-internal (search-fn)
  3043. (goto-char (point-min))
  3044. (insert "\n")
  3045. (goto-char (point-max))
  3046. (insert "\n")
  3047. (unwind-protect
  3048. (funcall search-fn)
  3049. (goto-char (point-min))
  3050. (delete-char 1)
  3051. (goto-char (1- (point-max)))
  3052. (delete-char 1)
  3053. (set-buffer-modified-p nil)))
  3054. (defun helm-candidate-buffer (&optional create-or-buffer)
  3055. "Register and return a buffer containing candidates of current source.
  3056. `helm-candidate-buffer' searches buffer-local candidates buffer first,
  3057. then global candidates buffer.
  3058. Acceptable values of CREATE-OR-BUFFER:
  3059. - nil (omit)
  3060. Only return the candidates buffer.
  3061. - a buffer
  3062. Register a buffer as a candidates buffer.
  3063. - 'global
  3064. Create a new global candidates buffer,
  3065. named \" *helm candidates:SOURCE*\".
  3066. - other non-nil value
  3067. Create a new local candidates buffer,
  3068. named \" *helm candidates:SOURCE*HELM-CURRENT-BUFFER\"."
  3069. (let* ((global-bname (format " *helm candidates:%s*"
  3070. helm-source-name))
  3071. (local-bname (format " *helm candidates:%s*%s"
  3072. helm-source-name
  3073. (buffer-name helm-current-buffer))))
  3074. (flet ((register-func ()
  3075. (setq helm-candidate-buffer-alist
  3076. (cons (cons helm-source-name create-or-buffer)
  3077. (delete (assoc helm-source-name
  3078. helm-candidate-buffer-alist)
  3079. helm-candidate-buffer-alist))))
  3080. (kill-buffers-func ()
  3081. (loop for b in (buffer-list)
  3082. if (string-match (format "^%s" (regexp-quote global-bname))
  3083. (buffer-name b))
  3084. do (kill-buffer b)))
  3085. (create-func ()
  3086. (with-current-buffer
  3087. (get-buffer-create (if (eq create-or-buffer 'global)
  3088. global-bname
  3089. local-bname))
  3090. (buffer-disable-undo)
  3091. (erase-buffer)
  3092. (font-lock-mode -1)))
  3093. (return-func ()
  3094. (or (get-buffer local-bname)
  3095. (get-buffer global-bname)
  3096. (helm-aif (assoc-default helm-source-name
  3097. helm-candidate-buffer-alist)
  3098. (and (buffer-live-p it) it)))))
  3099. (when create-or-buffer
  3100. (register-func)
  3101. (unless (bufferp create-or-buffer)
  3102. (and (eq create-or-buffer 'global) (kill-buffers-func))
  3103. (create-func)))
  3104. (return-func))))
  3105. (defun helm-compile-source--candidates-in-buffer (source)
  3106. (helm-aif (assoc 'candidates-in-buffer source)
  3107. (append source
  3108. `((candidates . ,(or (cdr it) 'helm-candidates-in-buffer))
  3109. (volatile) (match identity)))
  3110. source))
  3111. ;; (@* "Utility: resplit helm window")
  3112. (defun helm-toggle-resplit-window ()
  3113. "Toggle resplit helm window, vertically or horizontally."
  3114. (interactive)
  3115. (with-helm-window
  3116. (let ((before-height (window-height)))
  3117. (delete-window)
  3118. (set-window-buffer
  3119. (select-window (if (= (window-height) before-height)
  3120. (prog1
  3121. (split-window-vertically)
  3122. (setq helm-split-window-state 'vertical))
  3123. (setq helm-split-window-state 'horizontal)
  3124. (split-window-horizontally)))
  3125. helm-buffer))))
  3126. ;; (@* "Utility: Resize helm window.")
  3127. (defun helm-enlarge-window-1 (n)
  3128. "Enlarge or narrow helm window.
  3129. If N is positive enlarge, if negative narrow."
  3130. (unless helm-samewindow
  3131. (let ((horizontal-p (eq helm-split-window-state 'horizontal)))
  3132. (with-helm-window
  3133. (enlarge-window n horizontal-p)))))
  3134. (defun helm-narrow-window ()
  3135. "Narrow helm window."
  3136. (interactive)
  3137. (helm-enlarge-window-1 -1))
  3138. (defun helm-enlarge-window ()
  3139. "Enlarge helm window."
  3140. (interactive)
  3141. (helm-enlarge-window-1 1))
  3142. ;; (@* "Utility: select another action by key")
  3143. (defun helm-select-nth-action (n)
  3144. "Select the N nth action for the currently selected candidate."
  3145. (setq helm-saved-selection (helm-get-selection))
  3146. (unless helm-saved-selection
  3147. (error "Nothing is selected"))
  3148. (setq helm-saved-action (helm-get-nth-action n (helm-get-action)))
  3149. (helm-exit-minibuffer))
  3150. (defun helm-get-nth-action (n action)
  3151. (cond ((and (zerop n) (functionp action))
  3152. action)
  3153. ((listp action)
  3154. (or (cdr (elt action n))
  3155. (error "No such action")))
  3156. ((and (functionp action) (< 0 n))
  3157. (error "Sole action"))
  3158. (t
  3159. (error "Error in `helm-select-nth-action'"))))
  3160. (defun helm-select-2nd-action ()
  3161. "Select the 2nd action for the currently selected candidate."
  3162. (interactive)
  3163. (helm-select-nth-action 1))
  3164. (defun helm-select-3rd-action ()
  3165. "Select the 3rd action for the currently selected candidate."
  3166. (interactive)
  3167. (helm-select-nth-action 2))
  3168. (defun helm-select-4th-action ()
  3169. "Select the 4th action for the currently selected candidate."
  3170. (interactive)
  3171. (helm-select-nth-action 3))
  3172. (defun helm-select-2nd-action-or-end-of-line ()
  3173. "Select the 2nd action for the currently selected candidate.
  3174. This happen when point is at the end of minibuffer.
  3175. Otherwise goto the end of minibuffer."
  3176. (interactive)
  3177. (if (eolp)
  3178. (helm-select-nth-action 1)
  3179. (end-of-line)))
  3180. ;; (@* "Utility: Persistent Action")
  3181. (defmacro with-helm-display-same-window (&rest body)
  3182. "Execute BODY in the window used for persistent action.
  3183. Make `pop-to-buffer' and `display-buffer' display in the same window."
  3184. (declare (indent 0) (debug t))
  3185. `(let ((display-buffer-function 'helm-persistent-action-display-buffer))
  3186. ,@body))
  3187. (defvar helm-persistent-action-display-window nil)
  3188. (defun helm-initialize-persistent-action ()
  3189. (set (make-local-variable 'helm-persistent-action-display-window) nil))
  3190. (defun* helm-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
  3191. "Perform the associated action ATTR without quitting helm.
  3192. ATTR default is 'persistent-action', but it can be helm else.
  3193. In this case you have to add this new attribute to your source.
  3194. When `helm-samewindow' and ONEWINDOW are non--nil,
  3195. the helm window is never split in persistent action."
  3196. (interactive)
  3197. (helm-log "executing persistent-action")
  3198. (with-helm-window
  3199. (save-selected-window
  3200. (helm-select-persistent-action-window onewindow)
  3201. (helm-log-eval (current-buffer))
  3202. (let ((helm-in-persistent-action t))
  3203. (with-helm-display-same-window
  3204. (helm-execute-selection-action
  3205. nil
  3206. (or (assoc-default attr (helm-get-current-source))
  3207. (helm-get-action))
  3208. t)
  3209. (helm-log-run-hook 'helm-after-persistent-action-hook))))))
  3210. (defun helm-persistent-action-display-window (&optional onewindow)
  3211. "Return the window that will be used for presistent action.
  3212. If ONEWINDOW is non--nil window will not be splitted in persistent action
  3213. if `helm-samewindow' is non--nil also."
  3214. (with-helm-window
  3215. (setq helm-persistent-action-display-window
  3216. (cond ((window-live-p helm-persistent-action-display-window)
  3217. helm-persistent-action-display-window)
  3218. ((and helm-samewindow (one-window-p t) (not onewindow))
  3219. (split-window))
  3220. ((get-buffer-window helm-current-buffer))
  3221. (t
  3222. (next-window (selected-window) 1))))))
  3223. (defun helm-select-persistent-action-window (&optional onewindow)
  3224. "Select the window that will be used for persistent action.
  3225. See `helm-persistent-action-display-window' for how to use ONEWINDOW."
  3226. (select-window (get-buffer-window (helm-buffer-get)))
  3227. (select-window
  3228. (setq minibuffer-scroll-window
  3229. (helm-persistent-action-display-window onewindow))))
  3230. (defun helm-persistent-action-display-buffer (buf &optional not-this-window)
  3231. "Make `pop-to-buffer' and `display-buffer' display in the same window.
  3232. If `helm-persistent-action-use-special-display' is non-nil and
  3233. BUF is to be displayed by `special-display-function', use it.
  3234. Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
  3235. Argument NOT-THIS-WINDOW if present will be used as
  3236. second argument of `display-buffer'."
  3237. (let* ((name (buffer-name buf))
  3238. display-buffer-function pop-up-windows pop-up-frames
  3239. (same-window-regexps
  3240. (unless (and helm-persistent-action-use-special-display
  3241. (or (member name
  3242. (mapcar (lambda (x) (or (car-safe x) x))
  3243. special-display-buffer-names))
  3244. (remove-if-not
  3245. (lambda (x) (string-match (or (car-safe x) x) name))
  3246. special-display-regexps)))
  3247. '("."))))
  3248. (display-buffer buf not-this-window)))
  3249. ;; scroll-other-window(-down)? for persistent-action
  3250. (defun helm-scroll-other-window-base (command)
  3251. (with-selected-window (helm-persistent-action-display-window)
  3252. (funcall command helm-scroll-amount)))
  3253. (defun helm-scroll-other-window ()
  3254. "Scroll other window (not *Helm* window) upward."
  3255. (interactive)
  3256. (helm-scroll-other-window-base 'scroll-up))
  3257. (defun helm-scroll-other-window-down ()
  3258. "Scroll other window (not *Helm* window) downward."
  3259. (interactive)
  3260. (helm-scroll-other-window-base 'scroll-down))
  3261. ;; (@* "Utility: Visible Mark")
  3262. (defface helm-visible-mark
  3263. '((((min-colors 88) (background dark))
  3264. (:background "green1" :foreground "black"))
  3265. (((background dark)) (:background "green" :foreground "black"))
  3266. (((min-colors 88)) (:background "green1"))
  3267. (t (:background "green")))
  3268. "Face for visible mark."
  3269. :group 'helm)
  3270. (defvar helm-visible-mark-face 'helm-visible-mark)
  3271. (defvar helm-visible-mark-overlays nil)
  3272. (defun helm-clear-visible-mark ()
  3273. (with-current-buffer (helm-buffer-get)
  3274. (mapc 'delete-overlay helm-visible-mark-overlays)
  3275. (set (make-local-variable 'helm-visible-mark-overlays) nil)))
  3276. (add-hook 'helm-after-initialize-hook 'helm-clear-visible-mark)
  3277. (defvar helm-marked-candidates nil
  3278. "Marked candadates. List of \(source . real\) pair.")
  3279. (defun helm-this-visible-mark ()
  3280. (loop for o in helm-visible-mark-overlays
  3281. when (equal (point-at-bol) (overlay-start o))
  3282. return o))
  3283. (defun helm-delete-visible-mark (overlay)
  3284. (setq helm-marked-candidates
  3285. (remove
  3286. (cons (helm-get-current-source) (helm-get-selection))
  3287. helm-marked-candidates))
  3288. (delete-overlay overlay)
  3289. (setq helm-visible-mark-overlays
  3290. (delq overlay helm-visible-mark-overlays)))
  3291. (defun helm-make-visible-mark ()
  3292. (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
  3293. (overlay-put o 'face helm-visible-mark-face)
  3294. (overlay-put o 'source (assoc-default 'name (helm-get-current-source)))
  3295. (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
  3296. (overlay-put o 'real (helm-get-selection))
  3297. (add-to-list 'helm-visible-mark-overlays o))
  3298. (push (cons (helm-get-current-source) (helm-get-selection))
  3299. helm-marked-candidates))
  3300. (defun helm-toggle-visible-mark ()
  3301. "Toggle helm visible mark at point."
  3302. (interactive)
  3303. (with-helm-window
  3304. (helm-aif (helm-this-visible-mark)
  3305. (helm-delete-visible-mark it)
  3306. (helm-make-visible-mark))
  3307. (helm-next-line)))
  3308. (defun helm-display-all-visible-marks ()
  3309. "Show all `helm' visible marks strings."
  3310. (interactive)
  3311. (with-helm-window
  3312. (lexical-let ((overlays (reverse helm-visible-mark-overlays)))
  3313. (helm-run-after-quit
  3314. (lambda ()
  3315. (with-output-to-temp-buffer "*helm visible marks*"
  3316. (dolist (o overlays) (princ (overlay-get o 'string)))))))))
  3317. (defun helm-marked-candidates ()
  3318. "Return marked candidates of current source if any.
  3319. Otherwise one element list of current selection.
  3320. It is analogous to `dired-get-marked-files'."
  3321. (with-current-buffer (helm-buffer-get)
  3322. (let ((cands
  3323. (if helm-marked-candidates
  3324. (loop with current-src = (helm-get-current-source)
  3325. for (source . real) in (reverse helm-marked-candidates)
  3326. when (equal current-src source)
  3327. collect (helm-coerce-selection real source))
  3328. (list (helm-get-selection)))))
  3329. (helm-log-eval cands)
  3330. cands)))
  3331. (defun helm-reset-marked-candidates ()
  3332. (with-current-buffer (helm-buffer-get)
  3333. (set (make-local-variable 'helm-marked-candidates) nil)))
  3334. (add-hook 'helm-after-initialize-hook 'helm-reset-marked-candidates)
  3335. ;; (add-hook 'helm-after-action-hook 'helm-reset-marked-candidates)
  3336. (defun helm-current-source-name= (name)
  3337. (save-excursion
  3338. (goto-char (helm-get-previous-header-pos))
  3339. (equal name (helm-current-line-contents))))
  3340. (defun helm-revive-visible-mark ()
  3341. "Restore marked candidates when helm update display."
  3342. (with-current-buffer helm-buffer
  3343. (dolist (o helm-visible-mark-overlays)
  3344. (goto-char (point-min))
  3345. (while (and (search-forward (overlay-get o 'string) nil t)
  3346. (helm-current-source-name= (overlay-get o 'source)))
  3347. ;; Calculate real value of candidate.
  3348. ;; It can be nil if candidate have only a display value.
  3349. (let ((real (get-text-property (point-at-bol 0) 'helm-realvalue)))
  3350. (if real
  3351. ;; Check if real value of current candidate is the same
  3352. ;; that the one stored in overlay.
  3353. (and (string= (overlay-get o 'real) real)
  3354. (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
  3355. (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
  3356. (add-hook 'helm-update-hook 'helm-revive-visible-mark)
  3357. (defun helm-next-point-in-list (curpos points &optional prev)
  3358. (cond
  3359. ;; rule out special cases
  3360. ((null points) curpos)
  3361. ((and prev (< curpos (car points))) curpos)
  3362. ((< (car (last points)) curpos)
  3363. (if prev (car (last points)) curpos))
  3364. (t
  3365. (nth (if prev
  3366. (loop for pt in points
  3367. for i from 0
  3368. if (<= curpos pt)
  3369. return (1- i))
  3370. (loop for pt in points
  3371. for i from 0
  3372. if (< curpos pt)
  3373. return i))
  3374. points))))
  3375. (defun helm-next-visible-mark (&optional prev)
  3376. "Move next helm visible mark.
  3377. If PREV is non-nil move to precedent."
  3378. (interactive)
  3379. (with-helm-window
  3380. (ignore-errors
  3381. (goto-char (helm-next-point-in-list
  3382. (point)
  3383. (sort (mapcar 'overlay-start helm-visible-mark-overlays) '<)
  3384. prev)))
  3385. (helm-mark-current-line)))
  3386. (defun helm-prev-visible-mark ()
  3387. "Move previous helm visible mark."
  3388. (interactive)
  3389. (helm-next-visible-mark t))
  3390. ;; (@* "Utility: Selection Paste")
  3391. (defun helm-yank-selection ()
  3392. "Set minibuffer contents to current selection."
  3393. (interactive)
  3394. (helm-set-pattern (helm-get-selection nil t)))
  3395. (defun helm-kill-selection-and-quit ()
  3396. "Store current selection to kill ring.
  3397. You can paste it by typing \\[yank]."
  3398. (interactive)
  3399. (helm-run-after-quit
  3400. (lambda (sel)
  3401. (kill-new sel)
  3402. (message "Killed: %s" sel))
  3403. (helm-get-selection nil t)))
  3404. ;; (@* "Utility: Automatical execution of persistent-action")
  3405. (add-to-list 'minor-mode-alist '(helm-follow-mode " AFollow"))
  3406. (defun helm-follow-mode ()
  3407. "If this mode is on, persistent action is executed everytime the cursor is moved."
  3408. (interactive)
  3409. (with-current-buffer helm-buffer
  3410. (setq helm-follow-mode (not helm-follow-mode))
  3411. (message "helm-follow-mode is %s"
  3412. (if helm-follow-mode "enabled" "disabled"))))
  3413. (defun helm-follow-execute-persistent-action-maybe ()
  3414. "Execute persistent action in mode `helm-follow-mode'.
  3415. This happen after `helm-input-idle-delay' secs."
  3416. (and (not (get-buffer-window helm-action-buffer 'visible))
  3417. (buffer-local-value 'helm-follow-mode
  3418. (get-buffer-create helm-buffer))
  3419. (sit-for (and helm-input-idle-delay
  3420. (max helm-input-idle-delay 0.1)))
  3421. (helm-window)
  3422. (helm-get-selection)
  3423. (save-excursion
  3424. (helm-execute-persistent-action))))
  3425. ;; (@* "Utility: Migrate `helm-sources' to my-helm command")
  3426. (defun helm-migrate-sources ()
  3427. "Help to migrate to new `helm' way."
  3428. (interactive)
  3429. (with-current-buffer (get-buffer-create "*helm migrate*")
  3430. (erase-buffer)
  3431. (insert (format "\
  3432. Setting `helm-sources' directly is not good because
  3433. `helm' is not for one command. For now, interactive use of
  3434. `helm' (M-x helm) is only for demonstration purpose.
  3435. So you should define commands calling `helm'.
  3436. I help you to migrate to the new way.
  3437. The code below is automatically generated from current
  3438. `helm-sources' value. You can use the `my-helm' command
  3439. now!
  3440. Copy and paste it to your .emacs. Then substitute `my-helm'
  3441. for `helm' bindings in all `define-key', `local-set-key' and
  3442. `global-set-key' calls.
  3443. \(defun my-helm ()
  3444. \"Helm command for you.
  3445. It is automatically generated by `helm-migrate-sources'.\"
  3446. (interactive)
  3447. (helm-other-buffer
  3448. '%S
  3449. \"*my-helm*\"))
  3450. " helm-sources))
  3451. (eval-last-sexp nil)
  3452. (substitute-key-definition 'helm 'my-helm global-map)
  3453. (pop-to-buffer (current-buffer))))
  3454. ;; (@* "Compatibility")
  3455. ;; Copied assoc-default from XEmacs version 21.5.12
  3456. (unless (fboundp 'assoc-default)
  3457. (defun assoc-default (key alist &optional test default)
  3458. "Find object KEY in a pseudo-alist ALIST.
  3459. ALIST is a list of conses or objects. Each element (or the element's car,
  3460. if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
  3461. If that is non-nil, the element matches;
  3462. then `assoc-default' returns the element's cdr, if it is a cons,
  3463. or DEFAULT if the element is not a cons.
  3464. If no element matches, the value is nil.
  3465. If TEST is omitted or nil, `equal' is used."
  3466. (let (found (tail alist) value)
  3467. (while (and tail (not found))
  3468. (let ((elt (car tail)))
  3469. (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
  3470. (setq found t value (if (consp elt) (cdr elt) default))))
  3471. (setq tail (cdr tail)))
  3472. value)))
  3473. ;; Function not available in XEmacs,
  3474. (unless (fboundp 'minibuffer-contents)
  3475. (defun minibuffer-contents ()
  3476. "Return the user input in a minbuffer as a string.
  3477. The current buffer must be a minibuffer."
  3478. (field-string (point-max)))
  3479. (defun delete-minibuffer-contents ()
  3480. "Delete all user input in a minibuffer.
  3481. The current buffer must be a minibuffer."
  3482. (delete-field (point-max))))
  3483. ;; Function not available in older Emacs (<= 22.1).
  3484. (unless (fboundp 'buffer-chars-modified-tick)
  3485. (defun buffer-chars-modified-tick (&optional buffer)
  3486. "Return BUFFER's character-change tick counter.
  3487. Each buffer has a character-change tick counter, which is set to the
  3488. value of the buffer's tick counter (see `buffer-modified-tick'), each
  3489. time text in that buffer is inserted or deleted. By comparing the
  3490. values returned by two individual calls of `buffer-chars-modified-tick',
  3491. you can tell whether a character change occurred in that buffer in
  3492. between these calls. No argument or nil as argument means use current
  3493. buffer as BUFFER."
  3494. (with-current-buffer (or buffer (current-buffer))
  3495. (if (listp buffer-undo-list)
  3496. (length buffer-undo-list)
  3497. (buffer-modified-tick)))))
  3498. ;; (@* "CUA workaround")
  3499. (defadvice cua-delete-region (around helm-avoid-cua activate)
  3500. (ignore-errors ad-do-it))
  3501. (defadvice copy-region-as-kill (around helm-avoid-cua activate)
  3502. (if cua-mode
  3503. (ignore-errors ad-do-it)
  3504. ad-do-it))
  3505. ;;(@* "Attribute Documentation")
  3506. (defun helm-describe-helm-attribute (helm-attribute)
  3507. "Display the full documentation of HELM-ATTRIBUTE.
  3508. HELM-ATTRIBUTE should be a symbol."
  3509. (interactive (list (intern
  3510. (completing-read
  3511. "Describe helm attribute: "
  3512. (mapcar 'symbol-name helm-additional-attributes)
  3513. nil t))))
  3514. (with-output-to-temp-buffer "*Help*"
  3515. (princ (get helm-attribute 'helm-attrdoc))))
  3516. (helm-document-attribute 'name "mandatory"
  3517. " The name of the source. It is also the heading which appears
  3518. above the list of matches from the source. Must be unique.")
  3519. (helm-document-attribute 'header-name "optional"
  3520. " A function returning the display string of the header. Its
  3521. argument is the name of the source. This attribute is useful to
  3522. add an additional information with the source name.")
  3523. (helm-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
  3524. " Specifies how to retrieve candidates from the source. It can
  3525. either be a variable name, a function called with no parameters
  3526. or the actual list of candidates.
  3527. The list must be a list whose members are strings, symbols
  3528. or (DISPLAY . REAL) pairs.
  3529. In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
  3530. in the Helm buffer, but the REAL one is used as action
  3531. argument when the candidate is selected. This allows a more
  3532. readable presentation for candidates which would otherwise be,
  3533. for example, too long or have a common part shared with other
  3534. candidates which can be safely replaced with an abbreviated
  3535. string for display purposes.
  3536. Note that if the (DISPLAY . REAL) form is used then pattern
  3537. matching is done on the displayed string, not on the real
  3538. value.
  3539. If the candidates have to be retrieved asynchronously (for
  3540. example, by an external command which takes a while to run)
  3541. then the function should start the external command
  3542. asynchronously and return the associated process object.
  3543. Helm will take care of managing the process (receiving the
  3544. output from it, killing it if necessary, etc.). The process
  3545. should return candidates matching the current pattern (see
  3546. variable `helm-pattern'.)
  3547. Note that currently results from asynchronous sources appear
  3548. last in the helm buffer regardless of their position in
  3549. `helm-sources'.")
  3550. (helm-document-attribute 'action "mandatory if type attribute is not provided"
  3551. " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
  3552. FUNCTION is called with one parameter: the selected candidate.
  3553. An action other than the default can be chosen from this list
  3554. of actions for the currently selected candidate (by default
  3555. with TAB). The DISPLAY string is shown in the completions
  3556. buffer and the FUNCTION is invoked when an action is
  3557. selected. The first action of the list is the default.")
  3558. (helm-document-attribute 'coerce "optional"
  3559. " It's a function called with one argument: the selected candidate.
  3560. This function is intended for type convertion.
  3561. In normal case, the selected candidate (string) is passed to action function.
  3562. If coerce function is specified, it is called just before action function.
  3563. Example: converting string to symbol
  3564. (coerce . intern)")
  3565. (helm-document-attribute 'type "optional if action attribute is provided"
  3566. " Indicates the type of the items the source returns.
  3567. Merge attributes not specified in the source itself from
  3568. `helm-type-attributes'.
  3569. This attribute is implemented by plug-in.")
  3570. (helm-document-attribute 'init "optional"
  3571. " Function called with no parameters when helm is started. It
  3572. is useful for collecting current state information which can be
  3573. used to create the list of candidates later.
  3574. For example, if a source needs to work with the current
  3575. directory then it can store its value here, because later
  3576. helm does its job in the minibuffer and in the
  3577. `helm-buffer' and the current directory can be different
  3578. there.")
  3579. (helm-document-attribute 'delayed-init "optional"
  3580. " Function called with no parameters before candidate function is
  3581. called. It is similar with `init' attribute, but its
  3582. evaluation is deferred. It is useful to combine with ")
  3583. (helm-document-attribute 'match "optional"
  3584. " List of functions called with one parameter: a candidate. The
  3585. function should return non-nil if the candidate matches the
  3586. current pattern (see variable `helm-pattern').
  3587. This attribute allows the source to override the default
  3588. pattern matching based on `string-match'. It can be used, for
  3589. example, to implement a source for file names and do the
  3590. pattern matching on the basename of files, since it's more
  3591. likely one is typing part of the basename when searching for a
  3592. file, instead of some string anywhere else in its path.
  3593. If the list contains more than one function then the list of
  3594. matching candidates from the source is constructed by appending
  3595. the results after invoking the first function on all the
  3596. potential candidates, then the next function, and so on. The
  3597. matching candidates supplied by the first function appear first
  3598. in the list of results and then results from the other
  3599. functions, respectively.
  3600. This attribute has no effect for asynchronous sources (see
  3601. attribute `candidates'), since they perform pattern matching
  3602. themselves.")
  3603. (helm-document-attribute 'candidate-transformer "optional"
  3604. " It's a function or a list of functions called with one argument
  3605. when the completion list from the source is built. The argument
  3606. is the list of candidates retrieved from the source. The
  3607. function should return a transformed list of candidates which
  3608. will be used for the actual completion. If it is a list of
  3609. functions, it calls each function sequentially.
  3610. This can be used to transform or remove items from the list of
  3611. candidates.
  3612. Note that `candidates' is run already, so the given transformer
  3613. function should also be able to handle candidates with (DISPLAY
  3614. . REAL) format.")
  3615. (helm-document-attribute 'filtered-candidate-transformer "optional"
  3616. " It has the same format as `candidate-transformer', except the
  3617. function is called with two parameters: the candidate list and
  3618. the source.
  3619. This transformer is run on the candidate list which is already
  3620. filtered by the current pattern. While `candidate-transformer'
  3621. is run only once, it is run every time the input pattern is
  3622. changed.
  3623. It can be used to transform the candidate list dynamically, for
  3624. example, based on the current pattern.
  3625. In some cases it may also be more efficent to perform candidate
  3626. transformation here, instead of with `candidate-transformer'
  3627. even if this transformation is done every time the pattern is
  3628. changed. For example, if a candidate set is very large then
  3629. `candidate-transformer' transforms every candidate while only
  3630. some of them will actually be dislpayed due to the limit
  3631. imposed by `helm-candidate-number-limit'.
  3632. Note that `candidates' and `candidate-transformer' is run
  3633. already, so the given transformer function should also be able
  3634. to handle candidates with (DISPLAY . REAL) format.
  3635. This option has no effect for asynchronous sources. (Not yet,
  3636. at least.")
  3637. (helm-document-attribute 'action-transformer "optional"
  3638. " It's a function or a list of functions called with two
  3639. arguments when the action list from the source is
  3640. assembled. The first argument is the list of actions, the
  3641. second is the current selection. If it is a list of functions,
  3642. it calls each function sequentially.
  3643. The function should return a transformed action list.
  3644. This can be used to customize the list of actions based on the
  3645. currently selected candidate.")
  3646. (helm-document-attribute 'pattern-transformer "optional"
  3647. " It's a function or a list of functions called with one argument
  3648. before computing matches. Its argument is `helm-pattern'.
  3649. Functions should return transformed `helm-pattern'.
  3650. It is useful to change interpretation of `helm-pattern'.")
  3651. (helm-document-attribute 'delayed "optional"
  3652. " Candidates from the source are shown only if the user stops
  3653. typing and is idle for `helm-idle-delay' seconds.")
  3654. (helm-document-attribute 'volatile "optional"
  3655. " Indicates the source assembles the candidate list dynamically,
  3656. so it shouldn't be cached within a single Helm
  3657. invocation. It is only applicable to synchronous sources,
  3658. because asynchronous sources are not cached.")
  3659. (helm-document-attribute 'requires-pattern "optional"
  3660. " If present matches from the source are shown only if the
  3661. pattern is not empty. Optionally, it can have an integer
  3662. parameter specifying the required length of input which is
  3663. useful in case of sources with lots of candidates.")
  3664. (helm-document-attribute 'persistent-action "optional"
  3665. " Function called with one parameter; the selected candidate.
  3666. An action performed by `helm-execute-persistent-action'.
  3667. If none, use the default action.")
  3668. (helm-document-attribute 'candidates-in-buffer "optional"
  3669. " Shortcut attribute for making and narrowing candidates using
  3670. buffers. This newly-introduced attribute prevents us from
  3671. forgetting to add volatile and match attributes.
  3672. See docstring of `helm-candidates-in-buffer'.
  3673. (candidates-in-buffer) is equivalent of three attributes:
  3674. (candidates . helm-candidates-in-buffer)
  3675. (volatile)
  3676. (match identity)
  3677. (candidates-in-buffer . candidates-function) is equivalent of:
  3678. (candidates . candidates-function)
  3679. (volatile)
  3680. (match identity)
  3681. This attribute is implemented by plug-in.")
  3682. (helm-document-attribute 'search "optional"
  3683. " List of functions like `re-search-forward' or `search-forward'.
  3684. Buffer search function used by `helm-candidates-in-buffer'.
  3685. By default, `helm-candidates-in-buffer' uses `re-search-forward'.
  3686. This attribute is meant to be used with
  3687. (candidates . helm-candidates-in-buffer) or
  3688. (candidates-in-buffer) in short.")
  3689. (helm-document-attribute 'search-from-end "optional"
  3690. " Make `helm-candidates-in-buffer' search from the end of buffer.
  3691. If this attribute is specified, `helm-candidates-in-buffer' uses
  3692. `re-search-backward' instead.")
  3693. (helm-document-attribute 'get-line "optional"
  3694. " A function like `buffer-substring-no-properties' or `buffer-substring'.
  3695. This function converts point of line-beginning and point of line-end,
  3696. which represents a candidate computed by `helm-candidates-in-buffer'.
  3697. By default, `helm-candidates-in-buffer' uses
  3698. `buffer-substring-no-properties'.")
  3699. (helm-document-attribute 'display-to-real "optional"
  3700. " Function called with one parameter; the selected candidate.
  3701. The function transforms the selected candidate, and the result
  3702. is passed to the action function. The display-to-real
  3703. attribute provides another way to pass other string than one
  3704. shown in Helm buffer.
  3705. Traditionally, it is possible to make candidates,
  3706. candidate-transformer or filtered-candidate-transformer
  3707. function return a list with (DISPLAY . REAL) pairs. But if REAL
  3708. can be generated from DISPLAY, display-to-real is more
  3709. convenient and faster.")
  3710. (helm-document-attribute 'real-to-display "optional"
  3711. " Function called with one parameter; the selected candidate.
  3712. The inverse of display-to-real attribute.
  3713. The function transforms the selected candidate, which is passed
  3714. to the action function, for display. The real-to-display
  3715. attribute provides the other way to pass other string than one
  3716. shown in Helm buffer.
  3717. Traditionally, it is possible to make candidates,
  3718. candidate-transformer or filtered-candidate-transformer
  3719. function return a list with (DISPLAY . REAL) pairs. But if
  3720. DISPLAY can be generated from REAL, real-to-display is more
  3721. convenient.
  3722. Note that DISPLAY parts returned from candidates /
  3723. candidate-transformer are IGNORED as the name `display-to-real'
  3724. says.")
  3725. (helm-document-attribute 'cleanup "optional"
  3726. " Function called with no parameters when *helm* buffer is closed. It
  3727. is useful for killing unneeded candidates buffer.
  3728. Note that the function is executed BEFORE performing action.")
  3729. (helm-document-attribute 'candidate-number-limit "optional"
  3730. " Override `helm-candidate-number-limit' only for this source.")
  3731. (helm-document-attribute 'accept-empty "optional"
  3732. " Pass empty string \"\" to action function.")
  3733. (helm-document-attribute 'disable-shortcuts "optional"
  3734. " Disable `helm-enable-shortcuts' in current `helm' session.
  3735. This attribute is implemented by plug-in.")
  3736. (helm-document-attribute 'dummy "optional"
  3737. " Set `helm-pattern' to candidate. If this attribute is
  3738. specified, The candidates attribute is ignored.
  3739. This attribute is implemented by plug-in.
  3740. This plug-in implies disable-shortcuts plug-in.")
  3741. (helm-document-attribute 'multiline "optional"
  3742. " Enable to selection multiline candidates.")
  3743. (helm-document-attribute 'update "optional"
  3744. (substitute-command-keys
  3745. " Function called with no parameters when \
  3746. \\<helm-map>\\[helm-force-update] is pressed."))
  3747. (helm-document-attribute 'mode-line "optional"
  3748. " source local `helm-mode-line-string'. (included in `mode-line-format')
  3749. It accepts also variable/function name.")
  3750. (helm-document-attribute 'header-line "optional"
  3751. " source local `header-line-format'.
  3752. It accepts also variable/function name. ")
  3753. (helm-document-attribute
  3754. 'resume "optional"
  3755. " Function called with no parameters when `helm-resume' is started.")
  3756. (helm-document-attribute 'keymap "optional"
  3757. " Specific keymap for this source.
  3758. It is useful to have a keymap per source when using more than one source.
  3759. Otherwise, a keymap can be set per command with `helm' argument KEYMAP.
  3760. NOTE: when a source have `helm-map' as keymap attr,
  3761. the global value of `helm-map' will override the actual local one.")
  3762. (helm-document-attribute 'help-message "optional"
  3763. " Help message for this source.
  3764. If not present, `helm-help-message' value will be used.")
  3765. ;; (@* "Bug Report")
  3766. (defvar helm-maintainer-mail-address "emacs-helm@googlegroups.com")
  3767. (defvar helm-bug-report-salutation
  3768. "Describe bug below, using a precise recipe.
  3769. When I executed M-x ...
  3770. How to send a bug report:
  3771. 1) Be sure to use the LATEST version of helm.el.
  3772. 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
  3773. 3) Use Lisp version instead of compiled one: (load \"helm.el\")
  3774. 4) If you got an error, please paste *Backtrace* buffer.
  3775. 5) Type C-c C-c to send.")
  3776. (defvar helm-no-dump-variables
  3777. '(helm-candidate-buffer-alist
  3778. helm-digit-overlays
  3779. helm-help-message
  3780. helm-candidate-cache
  3781. )
  3782. "Variables not to dump in bug report.")
  3783. (defun helm-dumped-variables-in-bug-report ()
  3784. (let ((hash (make-hash-table)))
  3785. (loop for var in (apropos-internal "helm-" 'boundp)
  3786. for vname = (symbol-name var)
  3787. unless (or (string-match "-map$" vname)
  3788. (string-match "^helm-c-source-" vname)
  3789. (string-match "-hash$" vname)
  3790. (string-match "-face$" vname)
  3791. (memq var helm-no-dump-variables))
  3792. collect var)))
  3793. (defun helm-send-bug-report ()
  3794. "Send a bug report of helm.el."
  3795. (interactive)
  3796. (with-current-buffer (or helm-last-buffer
  3797. (current-buffer))
  3798. (reporter-submit-bug-report
  3799. helm-maintainer-mail-address
  3800. "helm.el"
  3801. (helm-dumped-variables-in-bug-report)
  3802. nil nil
  3803. helm-bug-report-salutation)))
  3804. (defun helm-send-bug-report-from-helm ()
  3805. "Send a bug report of helm.el in helm session."
  3806. (interactive)
  3807. (helm-run-after-quit 'helm-send-bug-report))
  3808. ;; Debugging function.
  3809. (defun* helm-test-candidates
  3810. (sources &optional (input "")
  3811. (compile-source-functions
  3812. helm-compile-source-functions-default))
  3813. "Test helper function for helm.
  3814. Given pseudo `helm-sources' and `helm-pattern', returns list like
  3815. ((\"source name1\" (\"candidate1\" \"candidate2\"))
  3816. (\"source name2\" (\"candidate3\" \"candidate4\")))"
  3817. (let ((helm-test-mode t)
  3818. helm-enable-shortcuts
  3819. helm-candidate-cache
  3820. (helm-compile-source-functions compile-source-functions)
  3821. helm-before-initialize-hook
  3822. helm-after-initialize-hook
  3823. helm-update-hook
  3824. helm-test-candidate-list)
  3825. (get-buffer-create helm-buffer)
  3826. (helm-initialize nil input sources)
  3827. (helm-update)
  3828. ;; test-mode spec: select 1st candidate!
  3829. (with-current-buffer helm-buffer
  3830. (forward-line 1)
  3831. (helm-mark-current-line))
  3832. (prog1
  3833. helm-test-candidate-list
  3834. (helm-cleanup))))
  3835. ;; (@* "Unit Tests")
  3836. ;; See developer-tools/unit-test-helm.el
  3837. (provide 'helm)
  3838. ;; Local Variables:
  3839. ;; coding: utf-8
  3840. ;; End:
  3841. ;;; helm.el ends here