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.

12531 lines
489 KiB

  1. ;;; helm-config.el --- Applications libary for `helm.el'
  2. ;; Filename: helm-config.el
  3. ;; Description: Applications libary for `helm.el'
  4. ;; Original Author: Tassilo Horn <tassilo@member.fsf.org>
  5. ;; This is a fork of original `anything-config.el' created by
  6. ;; Tassilo Horn <tassilo@member.fsf.org>.
  7. ;; Maintainers: Thierry Volpiatto <thierry.volpiatto@gmail.com>
  8. ;; Le Wang
  9. ;; Copyright (C) 2007 ~ 2011, Tassilo Horn, all rights reserved.
  10. ;; Copyright (C) 2009, Andy Stewart, all rights reserved.
  11. ;; Copyright (C) 2009 ~ 2012, rubikitch, all rights reserved.
  12. ;; Copyright (C) 2009 ~ 2012, Thierry Volpiatto, all rights reserved.
  13. ;; Created: 2012-03-15 12:29:23
  14. ;; X-URL: <https://github.com/emacs-helm/helm>
  15. ;; MailingList: <https://groups.google.com/forum/?hl=en&fromgroups#!forum/emacs-anything>
  16. ;; Keywords: helm, helm-config
  17. ;; Compatibility: GNU Emacs 22 ~ 24
  18. ;; Dependencies: `helm.el', `helm-match-plugin.el'.
  19. ;;; This file is NOT part of GNU Emacs
  20. ;;; License
  21. ;;
  22. ;; This program is free software; you can redistribute it and/or modify
  23. ;; it under the terms of the GNU General Public License as published by
  24. ;; the Free Software Foundation; either version 3, or (at your option)
  25. ;; any later version.
  26. ;; This program is distributed in the hope that it will be useful,
  27. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. ;; GNU General Public License for more details.
  30. ;; You should have received a copy of the GNU General Public License
  31. ;; along with this program; see the file COPYING. If not, write to
  32. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  33. ;; Floor, Boston, MA 02110-1301, USA.
  34. ;;; Commentary:
  35. ;;
  36. ;; Predefined configurations for `helm.el'
  37. ;;
  38. ;; For quick start, try `helm-for-files' to open files.
  39. ;;
  40. ;; To configure helm you should define helm command
  41. ;; with your favorite sources, like below:
  42. ;;
  43. ;; (defun my-helm ()
  44. ;; (interactive)
  45. ;; (helm-other-buffer
  46. ;; '(helm-c-source-buffers
  47. ;; helm-c-source-file-name-history
  48. ;; helm-c-source-info-pages
  49. ;; helm-c-source-info-elisp
  50. ;; helm-c-source-man-pages
  51. ;; helm-c-source-locate
  52. ;; helm-c-source-emacs-commands)
  53. ;; " *my-helm*"))
  54. ;;
  55. ;; Then type M-x my-helm to use sources.
  56. ;;
  57. ;; Defining own command is better than setup `helm-sources'
  58. ;; directly, because you can define multiple helm commands with
  59. ;; different sources. Each helm command should have own helm
  60. ;; buffer, because M-x helm-resume revives helm command.
  61. ;; NOTE: What you find on Emacswiki is mostly deprecated and not maintained,
  62. ;; don't complain if you use such code or configuration and something
  63. ;; doesn't work.
  64. ;;; Autodoc documentation:
  65. ;; ---------------------
  66. ;; * Commands defined here are:
  67. ;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "helm-" :docstring t)
  68. ;; `helm-configuration'
  69. ;; Customize `helm'.
  70. ;; `helm-c-buffer-help'
  71. ;; Help command for helm buffers.
  72. ;; `helm-ff-help'
  73. ;; Help command for `helm-find-files'.
  74. ;; `helm-read-file-name-help'
  75. ;; Not documented.
  76. ;; `helm-generic-file-help'
  77. ;; Not documented.
  78. ;; `helm-grep-help'
  79. ;; Not documented.
  80. ;; `helm-pdfgrep-help'
  81. ;; Not documented.
  82. ;; `helm-etags-help'
  83. ;; The help function for etags.
  84. ;; `helm-c-ucs-help'
  85. ;; Help command for `helm-ucs'.
  86. ;; `helm-c-bookmark-help'
  87. ;; Help command for bookmarks.
  88. ;; `helm-show-this-source-only'
  89. ;; Show all candidates of this source.
  90. ;; `helm-test-sources'
  91. ;; List all helm sources for test.
  92. ;; `helm-select-source'
  93. ;; [OBSOLETE] Select source.
  94. ;; `helm-insert-buffer-name'
  95. ;; Insert buffer name.
  96. ;; `helm-quit-and-find-file'
  97. ;; Drop into `helm-find-files' from `helm'.
  98. ;; `helm-mark-all'
  99. ;; Mark all visible unmarked candidates in current source.
  100. ;; `helm-unmark-all'
  101. ;; Unmark all candidates in all sources of current helm session.
  102. ;; `helm-toggle-all-marks'
  103. ;; Toggle all marks.
  104. ;; `helm-buffer-diff-persistent'
  105. ;; Toggle diff buffer without quitting helm.
  106. ;; `helm-buffer-revert-persistent'
  107. ;; Revert buffer without quitting helm.
  108. ;; `helm-buffer-save-persistent'
  109. ;; Save buffer without quitting helm.
  110. ;; `helm-buffer-run-kill-buffers'
  111. ;; Run kill buffer action from `helm-c-source-buffers-list'.
  112. ;; `helm-buffer-run-grep'
  113. ;; Run Grep action from `helm-c-source-buffers-list'.
  114. ;; `helm-buffer-run-zgrep'
  115. ;; Run Grep action from `helm-c-source-buffers-list'.
  116. ;; `helm-buffer-run-query-replace-regexp'
  117. ;; Run Query replace regexp action from `helm-c-source-buffers-list'.
  118. ;; `helm-buffer-run-query-replace'
  119. ;; Run Query replace action from `helm-c-source-buffers-list'.
  120. ;; `helm-buffer-switch-other-window'
  121. ;; Run switch to other window action from `helm-c-source-buffers-list'.
  122. ;; `helm-buffer-switch-other-frame'
  123. ;; Run switch to other frame action from `helm-c-source-buffers-list'.
  124. ;; `helm-buffer-switch-to-elscreen'
  125. ;; Run switch to elscreen action from `helm-c-source-buffers-list'.
  126. ;; `helm-buffer-run-ediff'
  127. ;; Run ediff action from `helm-c-source-buffers-list'.
  128. ;; `helm-buffer-run-ediff-merge'
  129. ;; Run ediff action from `helm-c-source-buffers-list'.
  130. ;; `helm-ff-run-toggle-auto-update'
  131. ;; Not documented.
  132. ;; `helm-ff-run-switch-to-history'
  133. ;; Run Switch to history action from `helm-c-source-find-files'.
  134. ;; `helm-ff-run-grep'
  135. ;; Run Grep action from `helm-c-source-find-files'.
  136. ;; `helm-ff-run-pdfgrep'
  137. ;; Run Pdfgrep action from `helm-c-source-find-files'.
  138. ;; `helm-ff-run-zgrep'
  139. ;; Run Grep action from `helm-c-source-find-files'.
  140. ;; `helm-ff-run-copy-file'
  141. ;; Run Copy file action from `helm-c-source-find-files'.
  142. ;; `helm-ff-run-rename-file'
  143. ;; Run Rename file action from `helm-c-source-find-files'.
  144. ;; `helm-ff-run-byte-compile-file'
  145. ;; Run Byte compile file action from `helm-c-source-find-files'.
  146. ;; `helm-ff-run-load-file'
  147. ;; Run Load file action from `helm-c-source-find-files'.
  148. ;; `helm-ff-run-eshell-command-on-file'
  149. ;; Run eshell command on file action from `helm-c-source-find-files'.
  150. ;; `helm-ff-run-ediff-file'
  151. ;; Run Ediff file action from `helm-c-source-find-files'.
  152. ;; `helm-ff-run-ediff-merge-file'
  153. ;; Run Ediff merge file action from `helm-c-source-find-files'.
  154. ;; `helm-ff-run-symlink-file'
  155. ;; Run Symlink file action from `helm-c-source-find-files'.
  156. ;; `helm-ff-run-hardlink-file'
  157. ;; Run Hardlink file action from `helm-c-source-find-files'.
  158. ;; `helm-ff-run-delete-file'
  159. ;; Run Delete file action from `helm-c-source-find-files'.
  160. ;; `helm-ff-run-complete-fn-at-point'
  161. ;; Run complete file name action from `helm-c-source-find-files'.
  162. ;; `helm-ff-run-switch-to-eshell'
  163. ;; Run switch to eshell action from `helm-c-source-find-files'.
  164. ;; `helm-ff-run-switch-other-window'
  165. ;; Run switch to other window action from `helm-c-source-find-files'.
  166. ;; `helm-ff-run-switch-other-frame'
  167. ;; Run switch to other frame action from `helm-c-source-find-files'.
  168. ;; `helm-ff-run-open-file-externally'
  169. ;; Run open file externally command action from `helm-c-source-find-files'.
  170. ;; `helm-ff-run-locate'
  171. ;; Run locate action from `helm-c-source-find-files'.
  172. ;; `helm-ff-run-gnus-attach-files'
  173. ;; Run gnus attach files command action from `helm-c-source-find-files'.
  174. ;; `helm-ff-run-etags'
  175. ;; Run Etags command action from `helm-c-source-find-files'.
  176. ;; `helm-ff-run-print-file'
  177. ;; Run Print file action from `helm-c-source-find-files'.
  178. ;; `helm-ff-run-toggle-basename'
  179. ;; Not documented.
  180. ;; `helm-find-files-down-one-level'
  181. ;; Go down one level like unix command `cd ..'.
  182. ;; `helm-ff-properties-persistent'
  183. ;; Show properties without quitting helm.
  184. ;; `helm-ff-persistent-delete'
  185. ;; Delete current candidate without quitting.
  186. ;; `helm-ff-run-kill-buffer-persistent'
  187. ;; Execute `helm-ff-kill-buffer-fname' whitout quitting.
  188. ;; `helm-ff-rotate-left-persistent'
  189. ;; Rotate image left without quitting helm.
  190. ;; `helm-ff-rotate-right-persistent'
  191. ;; Rotate image right without quitting helm.
  192. ;; `helm-c-goto-precedent-file'
  193. ;; Go to precedent file in helm grep/etags buffers.
  194. ;; `helm-c-goto-next-file'
  195. ;; Go to precedent file in helm grep/etags buffers.
  196. ;; `helm-c-grep-run-persistent-action'
  197. ;; Run grep persistent action from `helm-do-grep-1'.
  198. ;; `helm-c-grep-run-default-action'
  199. ;; Run grep default action from `helm-do-grep-1'.
  200. ;; `helm-c-grep-run-other-window-action'
  201. ;; Run grep goto other window action from `helm-do-grep-1'.
  202. ;; `helm-c-grep-run-save-buffer'
  203. ;; Run grep save results action from `helm-do-grep-1'.
  204. ;; `helm-yank-text-at-point'
  205. ;; Yank text at point in minibuffer.
  206. ;; `helm-c-bookmark-run-jump-other-window'
  207. ;; Jump to bookmark from keyboard.
  208. ;; `helm-c-bookmark-run-delete'
  209. ;; Delete bookmark from keyboard.
  210. ;; `helm-c-bmkext-run-edit'
  211. ;; Run `bmkext-edit-bookmark' from keyboard.
  212. ;; `helm-yaoddmuse-cache-pages'
  213. ;; Fetch the list of files on emacswiki and create cache file.
  214. ;; `helm-eval-new-line-and-indent'
  215. ;; Not documented.
  216. ;; `helm-call-source-from-helm'
  217. ;; Call helm source within `helm' session.
  218. ;; `helm-create-from-helm'
  219. ;; Run `helm-create' from `helm' as a fallback.
  220. ;; `helm-c-ucs-persistent-insert'
  221. ;; Not documented.
  222. ;; `helm-c-ucs-persistent-forward'
  223. ;; Not documented.
  224. ;; `helm-c-ucs-persistent-backward'
  225. ;; Not documented.
  226. ;; `helm-c-ucs-persistent-delete'
  227. ;; Not documented.
  228. ;; `helm-lisp-completion-at-point'
  229. ;; Helm lisp symbol completion at point.
  230. ;; `helm-c-complete-file-name-at-point'
  231. ;; Complete file name at point.
  232. ;; `helm-lisp-completion-at-point-or-indent'
  233. ;; First call indent and second call complete lisp symbol.
  234. ;; `helm-lisp-completion-or-file-name-at-point'
  235. ;; Complete lisp symbol or filename at point.
  236. ;; `helm-w32-shell-execute-open-file'
  237. ;; Not documented.
  238. ;; `helm-c-set-variable'
  239. ;; Set value to VAR interactively.
  240. ;; `helm-c-adaptive-save-history'
  241. ;; Save history information to file given by `helm-c-adaptive-history-file'.
  242. ;; `helm-c-reset-adaptative-history'
  243. ;; Delete all `helm-c-adaptive-history' and his file.
  244. ;; `helm-mini'
  245. ;; Preconfigured `helm' lightweight version (buffer -> recentf).
  246. ;; `helm-for-files'
  247. ;; Preconfigured `helm' for opening files.
  248. ;; `helm-recentf'
  249. ;; Preconfigured `helm' for `recentf'.
  250. ;; `helm-info-at-point'
  251. ;; Preconfigured `helm' for searching info at point.
  252. ;; `helm-show-kill-ring'
  253. ;; Preconfigured `helm' for `kill-ring'.
  254. ;; `helm-minibuffer-history'
  255. ;; Preconfigured `helm' for `minibuffer-history'.
  256. ;; `helm-gentoo'
  257. ;; Preconfigured `helm' for gentoo linux.
  258. ;; `helm-imenu'
  259. ;; Preconfigured `helm' for `imenu'.
  260. ;; `helm-google-suggest'
  261. ;; Preconfigured `helm' for google search with google suggest.
  262. ;; `helm-yahoo-suggest'
  263. ;; Preconfigured `helm' for Yahoo searching with Yahoo suggest.
  264. ;; `helm-for-buffers'
  265. ;; Preconfigured `helm' for buffers.
  266. ;; `helm-buffers-list'
  267. ;; Preconfigured `helm' to list buffers.
  268. ;; `helm-bbdb'
  269. ;; Preconfigured `helm' for BBDB.
  270. ;; `helm-locate'
  271. ;; Preconfigured `helm' for Locate.
  272. ;; `helm-w3m-bookmarks'
  273. ;; Preconfigured `helm' for w3m bookmark.
  274. ;; `helm-firefox-bookmarks'
  275. ;; Preconfigured `helm' for firefox bookmark.
  276. ;; `helm-colors'
  277. ;; Preconfigured `helm' for color.
  278. ;; `helm-bookmarks'
  279. ;; Preconfigured `helm' for bookmarks.
  280. ;; `helm-c-pp-bookmarks'
  281. ;; Preconfigured `helm' for bookmarks (pretty-printed).
  282. ;; `helm-c-insert-latex-math'
  283. ;; Preconfigured helm for latex math symbols completion.
  284. ;; `helm-register'
  285. ;; Preconfigured `helm' for Emacs registers.
  286. ;; `helm-man-woman'
  287. ;; Preconfigured `helm' for Man and Woman pages.
  288. ;; `helm-org-keywords'
  289. ;; Preconfigured `helm' for org keywords.
  290. ;; `helm-emms'
  291. ;; Preconfigured `helm' for emms sources.
  292. ;; `helm-eev-anchors'
  293. ;; Preconfigured `helm' for eev anchors.
  294. ;; `helm-bm-list'
  295. ;; Preconfigured `helm' for visible bookmarks.
  296. ;; `helm-timers'
  297. ;; Preconfigured `helm' for timers.
  298. ;; `helm-list-emacs-process'
  299. ;; Preconfigured `helm' for emacs process.
  300. ;; `helm-occur'
  301. ;; Preconfigured Helm for Occur source.
  302. ;; `helm-browse-code'
  303. ;; Preconfigured helm to browse code.
  304. ;; `helm-org-headlines'
  305. ;; Preconfigured helm to show org headlines.
  306. ;; `helm-regexp'
  307. ;; Preconfigured helm to build regexps.
  308. ;; `helm-c-copy-files-async'
  309. ;; Preconfigured helm to copy file list FLIST to DEST asynchronously.
  310. ;; `helm-find-files'
  311. ;; Preconfigured `helm' for helm implementation of `find-file'.
  312. ;; `helm-write-file'
  313. ;; Preconfigured `helm' providing completion for `write-file'.
  314. ;; `helm-insert-file'
  315. ;; Preconfigured `helm' providing completion for `insert-file'.
  316. ;; `helm-dired-rename-file'
  317. ;; Preconfigured `helm' to rename files from dired.
  318. ;; `helm-dired-copy-file'
  319. ;; Preconfigured `helm' to copy files from dired.
  320. ;; `helm-dired-symlink-file'
  321. ;; Preconfigured `helm' to symlink files from dired.
  322. ;; `helm-dired-hardlink-file'
  323. ;; Preconfigured `helm' to hardlink files from dired.
  324. ;; `helm-do-grep'
  325. ;; Preconfigured helm for grep.
  326. ;; `helm-do-pdfgrep'
  327. ;; Preconfigured helm for pdfgrep.
  328. ;; `helm-c-etags-select'
  329. ;; Preconfigured helm for etags.
  330. ;; `helm-filelist'
  331. ;; Preconfigured `helm' to open files instantly.
  332. ;; `helm-filelist+'
  333. ;; Preconfigured `helm' to open files/buffers/bookmarks instantly.
  334. ;; `helm-M-x'
  335. ;; Preconfigured `helm' for Emacs commands.
  336. ;; `helm-manage-advice'
  337. ;; Preconfigured `helm' to disable/enable function advices.
  338. ;; `helm-bookmark-ext'
  339. ;; Preconfigured `helm' for bookmark-extensions sources.
  340. ;; `helm-simple-call-tree'
  341. ;; Preconfigured `helm' for simple-call-tree. List function relationships.
  342. ;; `helm-mark-ring'
  343. ;; Preconfigured `helm' for `helm-c-source-mark-ring'.
  344. ;; `helm-global-mark-ring'
  345. ;; Preconfigured `helm' for `helm-c-source-global-mark-ring'.
  346. ;; `helm-all-mark-rings'
  347. ;; Preconfigured `helm' for `helm-c-source-global-mark-ring' and `helm-c-source-mark-ring'.
  348. ;; `helm-yaoddmuse-emacswiki-edit-or-view'
  349. ;; Preconfigured `helm' to edit or view EmacsWiki page.
  350. ;; `helm-yaoddmuse-emacswiki-post-library'
  351. ;; Preconfigured `helm' to post library to EmacsWiki.
  352. ;; `helm-eval-expression'
  353. ;; Preconfigured helm for `helm-c-source-evaluation-result'.
  354. ;; `helm-eval-expression-with-eldoc'
  355. ;; Preconfigured helm for `helm-c-source-evaluation-result' with `eldoc' support.
  356. ;; `helm-calcul-expression'
  357. ;; Preconfigured helm for `helm-c-source-calculation-result'.
  358. ;; `helm-surfraw'
  359. ;; Preconfigured `helm' to search PATTERN with search ENGINE.
  360. ;; `helm-call-source'
  361. ;; Preconfigured `helm' to call helm source.
  362. ;; `helm-execute-helm-command'
  363. ;; Preconfigured `helm' to execute preconfigured `helm'.
  364. ;; `helm-create'
  365. ;; Preconfigured `helm' to do many create actions from STRING.
  366. ;; `helm-top'
  367. ;; Preconfigured `helm' for top command.
  368. ;; `helm-select-xfont'
  369. ;; Preconfigured `helm' to select Xfont.
  370. ;; `helm-world-time'
  371. ;; Preconfigured `helm' to show world time.
  372. ;; `helm-apt'
  373. ;; Preconfigured `helm' : frontend of APT package manager.
  374. ;; `helm-esh-pcomplete'
  375. ;; Preconfigured helm to provide helm completion in eshell.
  376. ;; `helm-eshell-history'
  377. ;; Preconfigured helm for eshell history.
  378. ;; `helm-c-run-external-command'
  379. ;; Preconfigured `helm' to run External PROGRAM asyncronously from Emacs.
  380. ;; `helm-ratpoison-commands'
  381. ;; Preconfigured `helm' to execute ratpoison commands.
  382. ;; `helm-ucs'
  383. ;; Preconfigured helm for `ucs-names' math symbols.
  384. ;; `helm-c-apropos'
  385. ;; Preconfigured helm to describe commands, functions, variables and faces.
  386. ;; `helm-xrandr-set'
  387. ;; Not documented.
  388. ;; * User variables defined here:
  389. ;; [EVAL] (autodoc-document-lisp-buffer :type 'user-variable :prefix "helm-" :var-value t)
  390. ;; `helm-c-adaptive-history-file'
  391. ;; Default Value: "~/.emacs.d/helm-c-adaptive-history"
  392. ;; `helm-c-adaptive-history-length'
  393. ;; Default Value: 50
  394. ;; `helm-c-google-suggest-url'
  395. ;; Default Value: "http://google.com/complete/search?output=toolbar&q="
  396. ;; `helm-c-google-suggest-search-url'
  397. ;; Default Value: "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
  398. ;; `helm-google-suggest-use-curl-p'
  399. ;; Default Value: nil
  400. ;; `helm-c-yahoo-suggest-url'
  401. ;; Default Value: "http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=G [...]
  402. ;; `helm-c-yahoo-suggest-search-url'
  403. ;; Default Value: "http://search.yahoo.com/search?&ei=UTF-8&fr&h=c&p="
  404. ;; `helm-c-boring-buffer-regexp'
  405. ;; Default Value: "\\ (\\` \\)\\|\\*helm\\|\\*helm-mode\\| \\*Echo Area\\| \\*Minibuf"
  406. ;; `helm-c-boring-file-regexp'
  407. ;; Default Value: "/\\ (?:\\(?:\\.\\(?:git\\|hg\\|svn\\)\\|CVS\\|_darcs\\)\\)\\(?:/\\|$\\)\\| [...]
  408. ;; `helm-kill-ring-threshold'
  409. ;; Default Value: 10
  410. ;; `helm-c-kill-ring-max-lines-number'
  411. ;; Default Value: nil
  412. ;; `helm-su-or-sudo'
  413. ;; Default Value: "su"
  414. ;; `helm-for-files-prefered-list'
  415. ;; Default Value: (helm-c-source-ffap-line helm-c-source-ffap-guesser helm-c-sou [...]
  416. ;; `helm-create--actions-private'
  417. ;; Default Value: nil
  418. ;; `helm-allow-skipping-current-buffer'
  419. ;; Default Value: nil
  420. ;; `helm-c-enable-eval-defun-hack'
  421. ;; Default Value: t
  422. ;; `helm-tramp-verbose'
  423. ;; Default Value: 0
  424. ;; `helm-raise-command'
  425. ;; Default Value: nil
  426. ;; `helm-command-map-prefix-key'
  427. ;; Default Value: "<f5> a"
  428. ;; `helm-c-browse-code-regexp-lisp'
  429. ;; Default Value: "^ * (def\\(un\\|subst\\|macro\\|face\\|alias\\|advice\\|struct\\|type\\|th [...]
  430. ;; `helm-c-browse-code-regexp-python'
  431. ;; Default Value: "\\<def\\>\\|\\<class\\>"
  432. ;; `helm-c-browse-code-regexp-alist'
  433. ;; Default Value: ((lisp-interaction-mode . "^ *(def\\(un\\|subst\\|macro\\|face\\|alias\\|a [...]
  434. ;; `helm-c-external-programs-associations'
  435. ;; Default Value: nil
  436. ;; `helm-ff-auto-update-initial-value'
  437. ;; Default Value: t
  438. ;; `helm-c-copy-async-prefered-emacs'
  439. ;; Default Value: "emacs"
  440. ;; `helm-ff-lynx-style-map'
  441. ;; Default Value: t
  442. ;; `helm-ff-history-max-length'
  443. ;; Default Value: 100
  444. ;; `helm-ff-smart-completion'
  445. ;; Default Value: t
  446. ;; `helm-ff-default-kbsize'
  447. ;; Default Value: 1024.0
  448. ;; `helm-ff-tramp-not-fancy'
  449. ;; Default Value: t
  450. ;; `helm-ff-exif-data-program'
  451. ;; Default Value: "exiftran"
  452. ;; `helm-ff-exif-data-program-args'
  453. ;; Default Value: "-d"
  454. ;; `helm-c-grep-use-ioccur-style-keys'
  455. ;; Default Value: t
  456. ;; `helm-c-pdfgrep-default-read-command'
  457. ;; Default Value: "xpdf '%f' %p"
  458. ;; `helm-c-etags-tag-file-name'
  459. ;; Default Value: "TAGS"
  460. ;; `helm-c-etags-tag-file-search-limit'
  461. ;; Default Value: 10
  462. ;; `helm-c-etags-use-regexp-search'
  463. ;; Default Value: nil
  464. ;; `helm-c-etags-search-regexp'
  465. ;; Default Value: "^.+: .+ \\<%s"
  466. ;; `helm-c-filelist-file-name'
  467. ;; Default Value: nil
  468. ;; `helm-c-eldoc-in-minibuffer-show-fn'
  469. ;; Default Value: helm-c-show-info-in-mode-line
  470. ;; `helm-c-turn-on-show-completion'
  471. ;; Default Value: t
  472. ;; `helm-c-show-completion-use-special-display'
  473. ;; Default Value: t
  474. ;; `helm-c-show-completion-min-window-height'
  475. ;; Default Value: 7
  476. ;; `helm-lisp-completion-or-indent-delay'
  477. ;; Default Value: 0.6
  478. ;; `helm-c-default-external-file-browser'
  479. ;; Default Value: "nautilus"
  480. ;; `helm-c-use-adaptative-sorting'
  481. ;; Default Value: nil
  482. ;; `helm-ff-newfile-prompt-p'
  483. ;; Default Value: t
  484. ;; `helm-ff-avfs-directory'
  485. ;; Default Value: nil
  486. ;; `helm-ff-file-compressed-list'
  487. ;; Default Value: ("gz" "bz2" "zip" "7z")
  488. ;; `helm-locate-db-file-regexp'
  489. ;; Default Value: "m?locate.db$"
  490. ;; `helm-c-locate-command'
  491. ;; Default Value: nil
  492. ;; `helm-c-show-info-in-mode-line-delay'
  493. ;; Default Value: 12
  494. ;; `helm-c-copy-files-async-log-file'
  495. ;; Default Value: "/tmp/dired.log"
  496. ;; `helm-ff-printer-list'
  497. ;; Default Value: nil
  498. ;; `helm-ff-transformer-show-only-basename'
  499. ;; Default Value: nil
  500. ;; `helm-ff-quick-delete-dont-prompt-for-deletion'
  501. ;; Default Value: nil
  502. ;; `helm-ff-signal-error-on-dot-files'
  503. ;; Default Value: t
  504. ;; `helm-completing-read-handlers-alist'
  505. ;; Default Value: ((describe-function . helm-completing-read-symbols) (describe-variabl [...]
  506. ;; * Helm sources defined here:
  507. ;; [EVAL] (autodoc-document-lisp-buffer :type 'helm-source :prefix "helm-" :any-sname t)
  508. ;; `helm-c-source-regexp' (Regexp Builder)
  509. ;; `helm-c-source-buffers' (Buffers)
  510. ;; `helm-c-source-buffer-not-found' (Create buffer)
  511. ;; `helm-c-source-buffers-list' (Buffers)
  512. ;; `helm-c-source-file-name-history' (File Name History)
  513. ;; `helm-c-source-files-in-current-dir' (Files from Current Directory)
  514. ;; `helm-c-source-files-in-current-dir+' (Files from Current Directory)
  515. ;; `helm-c-source-find-files' (Find Files)
  516. ;; `helm-c-source-write-file' (Write File)
  517. ;; `helm-c-source-insert-file' (Insert File)
  518. ;; `helm-c-source-copy-files' (Copy Files)
  519. ;; `helm-c-source-symlink-files' (Symlink Files)
  520. ;; `helm-c-source-hardlink-files' (Hardlink Files)
  521. ;; `helm-c-source-file-cache' (File Cache)
  522. ;; `helm-c-source-locate' (Locate)
  523. ;; `helm-c-source-recentf' (Recentf)
  524. ;; `helm-c-source-ffap-guesser' (File at point)
  525. ;; `helm-c-source-ffap-line' (File/Lineno at point)
  526. ;; `helm-c-source-files-in-all-dired' (Files in all dired buffer.)
  527. ;; `helm-c-source-filelist' (FileList)
  528. ;; `helm-c-source-info-pages' (Info Pages)
  529. ;; `helm-c-source-man-pages' (Manual Pages)
  530. ;; `helm-c-source-complex-command-history' (Complex Command History)
  531. ;; `helm-c-source-extended-command-history' (Emacs Commands History)
  532. ;; `helm-c-source-emacs-commands' (Emacs Commands)
  533. ;; `helm-c-source-emacs-functions' (Emacs Functions)
  534. ;; `helm-c-source-emacs-functions-with-abbrevs' (Emacs Functions)
  535. ;; `helm-c-source-advice' (Function Advice)
  536. ;; `helm-c-source-emacs-variables' (Emacs Variables)
  537. ;; `helm-c-source-lacarte' (Lacarte)
  538. ;; `helm-c-source-bookmarks' (Bookmarks)
  539. ;; `helm-c-source-bookmark-set' (Set Bookmark)
  540. ;; `helm-c-source-bm' (Visible Bookmarks)
  541. ;; `helm-c-source-bookmarks-ssh' (Bookmarks-ssh)
  542. ;; `helm-c-source-bookmarks-su' (Bookmarks-root)
  543. ;; `helm-c-source-bookmarks-local' (Bookmarks-Local)
  544. ;; `helm-c-source-bmkext-addressbook' (Bookmark Addressbook)
  545. ;; `helm-c-source-bookmark-w3m' (Bookmark W3m)
  546. ;; `helm-c-source-bookmark-images' (Bookmark Images)
  547. ;; `helm-c-source-bookmark-man' (Bookmark Woman&Man)
  548. ;; `helm-c-source-bookmark-gnus' (Bookmark Gnus)
  549. ;; `helm-c-source-bookmark-info' (Bookmark Info)
  550. ;; `helm-c-source-bookmark-files&dirs' (Bookmark Files&Directories)
  551. ;; `helm-c-source-bookmark-su-files&dirs' (Bookmark Root-Files&Directories)
  552. ;; `helm-c-source-bookmark-ssh-files&dirs' (Bookmark Ssh-Files&Directories)
  553. ;; `helm-c-source-firefox-bookmarks' (Firefox Bookmarks)
  554. ;; `helm-c-source-w3m-bookmarks' (W3m Bookmarks)
  555. ;; `helm-c-source-elisp-library-scan' (Elisp libraries (Scan))
  556. ;; `helm-c-source-imenu' (Imenu)
  557. ;; `helm-c-source-ctags' (Exuberant ctags)
  558. ;; `helm-c-source-etags-select' (Etags)
  559. ;; `helm-c-source-semantic' (Semantic Tags)
  560. ;; `helm-c-source-simple-call-tree-functions-callers' (Function is called by)
  561. ;; `helm-c-source-simple-call-tree-callers-functions' (Function calls)
  562. ;; `helm-c-source-commands-and-options-in-file' (Commands/Options in file)
  563. ;; `helm-c-source-customize-face' (Customize Face)
  564. ;; `helm-c-source-colors' (Colors)
  565. ;; `helm-c-source-tracker-search' (Tracker Search)
  566. ;; `helm-c-source-mac-spotlight' (mdfind)
  567. ;; `helm-c-source-picklist' (Picklist)
  568. ;; `helm-c-source-kill-ring' (Kill Ring)
  569. ;; `helm-c-source-mark-ring' (mark-ring)
  570. ;; `helm-c-source-global-mark-ring' (global-mark-ring)
  571. ;; `helm-c-source-register' (Registers)
  572. ;; `helm-c-source-latex-math' (Latex Math Menu)
  573. ;; `helm-c-source-fixme' (TODO/FIXME/DRY comments)
  574. ;; `helm-c-source-rd-headline' (RD HeadLine)
  575. ;; `helm-c-source-oddmuse-headline' (Oddmuse HeadLine)
  576. ;; `helm-c-source-emacs-source-defun' (Emacs Source DEFUN)
  577. ;; `helm-c-source-emacs-lisp-expectations' (Emacs Lisp Expectations)
  578. ;; `helm-c-source-emacs-lisp-toplevels' (Emacs Lisp Toplevel / Level 4 Comment / Linkd Star)
  579. ;; `helm-c-source-yaoddmuse-emacswiki-edit-or-view' (Yaoddmuse Edit or View (EmacsWiki))
  580. ;; `helm-c-source-yaoddmuse-emacswiki-post-library' (Yaoddmuse Post library (EmacsWiki))
  581. ;; `helm-c-source-eev-anchor' (Anchors)
  582. ;; `helm-c-source-org-headline' (Org HeadLine)
  583. ;; `helm-c-source-org-keywords' (Org Keywords)
  584. ;; `helm-c-source-bbdb' (BBDB)
  585. ;; `helm-c-source-evaluation-result' (Evaluation Result)
  586. ;; `helm-c-source-calculation-result' (Calculation Result)
  587. ;; `helm-c-source-google-suggest' (Google Suggest)
  588. ;; `helm-c-source-yahoo-suggest' (Yahoo Suggest)
  589. ;; `helm-c-source-emms-streams' (Emms Streams)
  590. ;; `helm-c-source-emms-dired' (Music Directory)
  591. ;; `helm-c-source-emms-files' (Emms files)
  592. ;; `helm-c-source-jabber-contacts' (Jabber Contacts)
  593. ;; `helm-c-source-call-source' (Call helm source)
  594. ;; `helm-c-source-helm-commands' (Preconfigured Helm)
  595. ;; `helm-c-source-occur' (Occur)
  596. ;; `helm-c-source-browse-code' (Browse code)
  597. ;; `helm-c-source-create' (Create)
  598. ;; `helm-c-source-minibuffer-history' (Minibuffer History)
  599. ;; `helm-c-source-elscreen' (Elscreen)
  600. ;; `helm-c-source-top' (Top (Press C-c C-u to refresh))
  601. ;; `helm-c-source-absolute-time-timers' (Absolute Time Timers)
  602. ;; `helm-c-source-idle-time-timers' (Idle Time Timers)
  603. ;; `helm-c-source-xrandr-change-resolution' (Change Resolution)
  604. ;; `helm-c-source-xfonts' (X Fonts)
  605. ;; `helm-c-source-ucs' (Ucs names)
  606. ;; `helm-c-source-emacs-process' (Emacs Process)
  607. ;; `helm-c-source-time-world' (Time World List)
  608. ;; `helm-c-source-apt' (APT)
  609. ;; `helm-c-source-gentoo' (Portage sources)
  610. ;; `helm-c-source-use-flags' (Use Flags)
  611. ;; `helm-c-source-ratpoison-commands' (Ratpoison Commands)
  612. ;; `helm-c-source-esh' (Eshell completions)
  613. ;; `helm-c-source-eshell-history' (Eshell history)
  614. ;; *** END auto-documentation
  615. ;;; For Maintainers:
  616. ;;
  617. ;; Install developer-tools/autodoc.el and
  618. ;; Evaluate (autodoc-update-all) before commit or run it interactively.
  619. ;; This function generates helm-c-source-* / functions / options list.
  620. ;;
  621. ;; [EVAL IT] (autodoc-update-all)
  622. ;;
  623. ;; Please write details documentation about function, then others will
  624. ;; read code more easier. -- Andy Stewart
  625. ;;
  626. ;;; Change log:
  627. ;;
  628. ;; Change log of this file is found at
  629. ;; http://repo.or.cz/w/helm-config.git/history/master:/helm-config.el
  630. ;;
  631. ;; Change log of this project is found at
  632. ;; http://repo.or.cz/w/helm-config.git?a=shortlog
  633. ;;; Contributors:
  634. ;;
  635. ;; Tamas Patrovics
  636. ;; Tassilo Horn <tassilo@member.fsf.org>
  637. ;; Vagn Johansen <gonz808@hotmail.com>
  638. ;; Mathias Dahl <mathias.dahl@gmail.com>
  639. ;; Bill Clementson <billclem@gmail.com>
  640. ;; Stefan Kamphausen (see http://www.skamphausen.de for more informations)
  641. ;; Drew Adams <drew.adams@oracle.com>
  642. ;; Jason McBrayer <jmcbray@carcosa.net>
  643. ;; Andy Stewart <lazycat.manatee@gmail.com>
  644. ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
  645. ;; rubikitch <rubikitch@ruby-lang.org>
  646. ;; Scott Vokes <vokes.s@gmail.com>
  647. ;; Kenichirou Oyama <k1lowxb@gmail.com>
  648. ;;; TODO
  649. ;;
  650. ;; - Fix documentation, now many functions haven't documentations.
  651. ;;
  652. ;;; Code:
  653. ;;; Require
  654. ;;
  655. ;;
  656. (require 'helm)
  657. (require 'thingatpt)
  658. (require 'ffap)
  659. (require 'cl)
  660. (eval-when-compile (require 'dired))
  661. (require 'dired-aux)
  662. (require 'dired-x)
  663. (require 'tramp)
  664. (require 'grep)
  665. (require 'url)
  666. (require 'xml)
  667. (eval-when-compile (require 'org)) ; Shut up byte compiler about org-directory.
  668. (eval-when-compile (require 'semantic nil t))
  669. (require 'helm-match-plugin)
  670. ;;; Declare external functions
  671. ;;
  672. ;;
  673. (declare-function gnus-dired-attach "ext:gnus-dired.el" (files-to-attach))
  674. (declare-function image-dired-display-image "image-dired.el" (file &optional original-size))
  675. (declare-function image-dired-update-property "image-dired.el" (prop value))
  676. (declare-function woman-file-name-all-completions "woman.el" (topic))
  677. (declare-function Man-getpage-in-background "man.el" (topic))
  678. (declare-function simple-call-tree-analyze "ext:simple-call-tree.el" (&optional test))
  679. (declare-function yaoddmuse-update-pagename "ext:yaoddmuse.el" (&optional unforced))
  680. (declare-function yaoddmuse-get-library-list "ext:yaoddmuse.el" (&optional dirs string))
  681. (declare-function org-get-current-options "ext:org-exp.el")
  682. (declare-function emms-streams "ext:emms-streams")
  683. (declare-function emms-stream-delete-bookmark "ext:emms-streams")
  684. (declare-function emms-stream-add-bookmark "ext:emms-streams" (name url fd type))
  685. (declare-function emms-stream-save-bookmarks-file "ext:emms-streams")
  686. (declare-function emms-stream-quit "ext:emms-streams")
  687. (declare-function with-current-emms-playlist "ext:emms" (&rest body))
  688. (declare-function emms-playlist-tracks-in-region "ext:emms" (beg end))
  689. (declare-function emms-playlist-first "ext:emms")
  690. (declare-function emms-playlist-mode-play-smart "ext:emms-playlist-mode")
  691. (declare-function term-line-mode "term")
  692. (declare-function term-char-mode "term")
  693. (declare-function term-send-input "term")
  694. (declare-function term-send-eof "term")
  695. (declare-function Info-index-nodes "info" (&optional file))
  696. (declare-function Info-goto-node "info" (&optional fork))
  697. (declare-function Info-find-node "info.el" (filename nodename &optional no-going-back))
  698. (declare-function elscreen-find-screen-by-buffer "ext:elscreen.el" (buffer &optional create))
  699. (declare-function elscreen-find-file "ext:elscreen.el" (filename))
  700. (declare-function elscreen-goto "ext:elscreen.el" (screen))
  701. (declare-function semantic-format-tag-summarize "ext:format.el" (tag &optional parent color) t)
  702. (declare-function semantic-tag-components "ext:tag.el" (tag) t)
  703. (declare-function semantic-go-to-tag "ext:tag-file.el" (tag) t)
  704. (declare-function semantic-tag-type "ext:tag-file.el" (tag) t)
  705. (declare-function semantic-tag-class "ext:tag-file.el" (tag) t)
  706. (declare-function bbdb "ext:bbdb-com")
  707. (declare-function bbdb-current-record "ext:bbdb-com")
  708. (declare-function bbdb-redisplay-one-record "ext:bbdb-com")
  709. (declare-function bbdb-record-net "ext:bbdb-com" (string) t)
  710. (declare-function bbdb-current-record "ext:bbdb-com")
  711. (declare-function bbdb-dwim-net-address "ext:bbdb-com")
  712. (declare-function bbdb-records "ext:bbdb-com"
  713. (&optional dont-check-disk already-in-db-buffer))
  714. (declare-function eshell-read-aliases-list "em-alias")
  715. (declare-function eshell-send-input "esh-mode" (&optional use-region queue-p no-newline))
  716. (declare-function eshell-bol "esh-mode")
  717. (declare-function eldoc-current-symbol "eldoc")
  718. (declare-function eldoc-get-fnsym-args-string "eldoc" (sym &optional index))
  719. (declare-function eldoc-get-var-docstring "eldoc" (sym))
  720. (declare-function eldoc-fnsym-in-current-sexp "eldoc")
  721. (declare-function find-library-name "find-func.el" (library))
  722. (declare-function adoc-construct "ext:auto-document.el" (buf))
  723. (declare-function adoc-first-line "ext:auto-document.el" (str))
  724. (declare-function adoc-prin1-to-string "ext:auto-document.el" (object))
  725. (declare-function secure-hash "ext:fns.c" (algorithm object &optional start end binary))
  726. (declare-function w32-shell-execute "ext:w32fns.c" (operation document &optional parameters show-flag))
  727. (declare-function undo-tree-restore-state-from-register "ext:undo-tree.el" (register))
  728. ;;; compatibility
  729. ;;
  730. ;;
  731. (unless (fboundp 'window-system)
  732. (defun window-system (&optional arg)
  733. window-system))
  734. (unless (fboundp 'make-composed-keymap)
  735. (defun make-composed-keymap (maps &optional parent)
  736. "Construct a new keymap composed of MAPS and inheriting from PARENT.
  737. When looking up a key in the returned map, the key is looked in each
  738. keymap of MAPS in turn until a binding is found.
  739. If no binding is found in MAPS, the lookup continues in PARENT, if non-nil.
  740. As always with keymap inheritance, a nil binding in MAPS overrides
  741. any corresponding binding in PARENT, but it does not override corresponding
  742. bindings in other keymaps of MAPS.
  743. MAPS can be a list of keymaps or a single keymap.
  744. PARENT if non-nil should be a keymap."
  745. `(keymap
  746. ,@(if (keymapp maps) (list maps) maps)
  747. ,@parent)))
  748. (unless (fboundp 'apply-partially)
  749. (defun apply-partially (fun &rest args)
  750. "Return a function that is a partial application of FUN to ARGS.
  751. ARGS is a list of the first N arguments to pass to FUN.
  752. The result is a new function which does the same as FUN, except that
  753. the first N arguments are fixed at the values with which this function
  754. was called."
  755. (lexical-let ((fun fun) (args1 args))
  756. (lambda (&rest args2) (apply fun (append args1 args2))))))
  757. ;;; Customize
  758. ;;
  759. ;;
  760. (defgroup helm-config nil
  761. "Predefined configurations for `helm.el'."
  762. :group 'helm)
  763. (defcustom helm-c-adaptive-history-file
  764. "~/.emacs.d/helm-c-adaptive-history"
  765. "Path of file where history information is stored."
  766. :type 'string
  767. :group 'helm-config)
  768. (defcustom helm-c-adaptive-history-length 50
  769. "Maximum number of candidates stored for a source."
  770. :type 'number
  771. :group 'helm-config)
  772. (defcustom helm-c-google-suggest-url
  773. "http://google.com/complete/search?output=toolbar&q="
  774. "URL used for looking up Google suggestions."
  775. :type 'string
  776. :group 'helm-config)
  777. (defcustom helm-c-google-suggest-search-url
  778. "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
  779. "URL used for Google searching."
  780. :type 'string
  781. :group 'helm-config)
  782. (defcustom helm-google-suggest-use-curl-p nil
  783. "When non--nil use CURL to get info from `helm-c-google-suggest-url'.
  784. Otherwise `url-retrieve-synchronously' is used."
  785. :type 'boolean
  786. :group 'helm-config)
  787. (defcustom helm-c-yahoo-suggest-url
  788. "http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=Generic&query="
  789. "Url used for looking up Yahoo suggestions."
  790. :type 'string
  791. :group 'helm-config)
  792. (defcustom helm-c-yahoo-suggest-search-url
  793. "http://search.yahoo.com/search?&ei=UTF-8&fr&h=c&p="
  794. "Url used for Yahoo searching."
  795. :type 'string
  796. :group 'helm-config)
  797. (defcustom helm-c-boring-buffer-regexp
  798. (rx (or
  799. (group bos " ")
  800. ;; helm-buffers
  801. "*helm" "*helm-mode"
  802. ;; echo area
  803. " *Echo Area" " *Minibuf"))
  804. "The regexp that match boring buffers.
  805. Buffer candidates matching this regular expression will be
  806. filtered from the list of candidates if the
  807. `helm-c-skip-boring-buffers' candidate transformer is used, or
  808. they will be displayed with face `file-name-shadow' if
  809. `helm-c-shadow-boring-buffers' is used."
  810. :type 'string
  811. :group 'helm-config)
  812. ;; (string-match helm-c-boring-buffer-regexp "buf")
  813. ;; (string-match helm-c-boring-buffer-regexp " hidden")
  814. ;; (string-match helm-c-boring-buffer-regexp " *Minibuf-1*")
  815. (defcustom helm-c-boring-file-regexp
  816. (rx (or
  817. ;; Boring directories
  818. (and "/" (or ".svn" "CVS" "_darcs" ".git" ".hg") (or "/" eol))
  819. ;; Boring files
  820. (and line-start ".#")
  821. (and (or ".class" ".la" ".o" "~") eol)))
  822. "The regexp that match boring files.
  823. File candidates matching this regular expression will be
  824. filtered from the list of candidates if the
  825. `helm-c-skip-boring-files' candidate transformer is used, or
  826. they will be displayed with face `file-name-shadow' if
  827. `helm-c-shadow-boring-files' is used."
  828. :type 'string
  829. :group 'helm-config)
  830. (defcustom helm-kill-ring-threshold 10
  831. "Minimum length to be listed by `helm-c-source-kill-ring'."
  832. :type 'integer
  833. :group 'helm-config)
  834. (defcustom helm-c-kill-ring-max-lines-number nil
  835. "Max number of lines displayed per candidate in kill-ring browser.
  836. If nil or zero, don't truncate candidate, show all."
  837. :type 'integer
  838. :group 'helm-config)
  839. (defcustom helm-su-or-sudo "su"
  840. "What command to use for root access."
  841. :type 'string
  842. :group 'helm-config)
  843. (defcustom helm-for-files-prefered-list
  844. '(helm-c-source-ffap-line
  845. helm-c-source-ffap-guesser
  846. helm-c-source-buffers-list
  847. helm-c-source-recentf
  848. helm-c-source-bookmarks
  849. helm-c-source-file-cache
  850. helm-c-source-files-in-current-dir+
  851. helm-c-source-locate)
  852. "Your prefered sources to find files."
  853. :type 'list
  854. :group 'helm-config)
  855. (defcustom helm-create--actions-private nil
  856. "User defined actions for `helm-create' / `helm-c-source-create'.
  857. It is a list of (DISPLAY . FUNCTION) pairs like `action'
  858. attribute of `helm-sources'.
  859. It is prepended to predefined pairs."
  860. :type 'list
  861. :group 'helm-config)
  862. (defcustom helm-allow-skipping-current-buffer nil
  863. "Show current buffer or not in helm buffer"
  864. :type 'boolean
  865. :group 'helm-config)
  866. (defcustom helm-c-enable-eval-defun-hack t
  867. "If non-nil, execute `helm' using the source at point when C-M-x is pressed.
  868. This hack is invoked when pressing C-M-x in the form \
  869. (defvar helm-c-source-XXX ...) or (setq helm-c-source-XXX ...)."
  870. :type 'boolean
  871. :group 'helm-config)
  872. (defcustom helm-tramp-verbose 0
  873. "Just like `tramp-verbose' but specific to helm.
  874. When set to 0 don't show tramp messages in helm.
  875. If you want to have the default tramp messages set it to 3."
  876. :type 'integer
  877. :group 'helm-config)
  878. (defcustom helm-raise-command nil
  879. "A shell command to jump to a window running specific program.
  880. Need external program wmctrl.
  881. This will be use with `format', so use something like \"wmctrl -xa %s\"."
  882. :type 'string
  883. :group 'helm-config)
  884. (defun helm-set-helm-command-map-prefix-key (var key)
  885. "The customize set function for `helm-command-map-prefix-key'."
  886. (when (boundp var)
  887. (define-key global-map (read-kbd-macro (symbol-value var)) nil))
  888. (set var key)
  889. (define-key global-map
  890. (read-kbd-macro (symbol-value var)) 'helm-command-map))
  891. (defcustom helm-command-map-prefix-key "C-x c"
  892. "The prefix key for all `helm-command-map' commands."
  893. :type 'string
  894. :set 'helm-set-helm-command-map-prefix-key
  895. :group 'helm-config)
  896. (defcustom helm-c-browse-code-regexp-lisp
  897. "^ *\(def\\(un\\|subst\\|macro\\|face\\|alias\\|advice\\|struct\\|\
  898. type\\|theme\\|var\\|group\\|custom\\|const\\|method\\|class\\)"
  899. "Regexp used to parse lisp buffer when browsing code."
  900. :type 'string
  901. :group 'helm-config)
  902. (defcustom helm-c-browse-code-regexp-python
  903. "\\<def\\>\\|\\<class\\>"
  904. "Regexp used to parse python buffer when browsing code."
  905. :type 'string
  906. :group 'helm-config)
  907. (defcustom helm-c-browse-code-regexp-alist
  908. `((lisp-interaction-mode . ,helm-c-browse-code-regexp-lisp)
  909. (emacs-lisp-mode . ,helm-c-browse-code-regexp-lisp)
  910. (lisp-mode . ,helm-c-browse-code-regexp-lisp)
  911. (python-mode . ,helm-c-browse-code-regexp-python))
  912. "Alist to store regexps for browsing code corresponding \
  913. to a specific `major-mode'."
  914. :type 'list
  915. :group 'helm-config)
  916. (defcustom helm-c-external-programs-associations nil
  917. "Alist to store externals programs associated with file extension.
  918. This variable overhide setting in .mailcap file.
  919. e.g : '\(\(\"jpg\" . \"gqview\"\) (\"pdf\" . \"xpdf\"\)\) "
  920. :type 'list
  921. :group 'helm-config)
  922. (defcustom helm-ff-auto-update-initial-value t
  923. "Auto update when only one candidate directory is matched.
  924. This is the default value when starting `helm-find-files'."
  925. :group 'helm-config
  926. :type 'boolean)
  927. (defcustom helm-c-copy-async-prefered-emacs "emacs"
  928. "Path to the emacs you want to use for copying async.
  929. Emacs versions < 24 fail to copy directory due to a bug not fixed
  930. in `copy-directory'."
  931. :group 'helm-config
  932. :type 'string)
  933. (defcustom helm-ff-lynx-style-map t
  934. "Use arrow keys to navigate with `helm-find-files'.
  935. You will have to restart Emacs or reeval `helm-find-files-map'
  936. and `helm-c-read-file-map' for this take effect."
  937. :group 'helm-config
  938. :type 'boolean)
  939. (defcustom helm-ff-history-max-length 100
  940. "Number of elements shown in `helm-find-files' history."
  941. :group 'helm-config
  942. :type 'integer)
  943. (defcustom helm-ff-smart-completion t
  944. "Try to complete filenames smarter when non--nil.
  945. See `helm-ff-transform-fname-for-completion' for more info."
  946. :group 'helm-config
  947. :type 'boolean)
  948. (defcustom helm-ff-default-kbsize 1024.0
  949. "Default Kbsize to use for showing files size.
  950. It is a float, usually 1024.0 but could be 1000.0 on some systems."
  951. :group 'helm-config
  952. :type 'float)
  953. (defcustom helm-ff-tramp-not-fancy t
  954. "No colors when listing remote files when set to non--nil.
  955. This make listing much faster, specially on slow machines."
  956. :group 'helm-config
  957. :type 'boolean)
  958. (defcustom helm-ff-exif-data-program "exiftran"
  959. "Program used to extract exif data of an image file."
  960. :group 'helm-config
  961. :type 'string)
  962. (defcustom helm-ff-exif-data-program-args "-d"
  963. "*Arguments used for `helm-ff-exif-data-program'."
  964. :group 'helm-config
  965. :type 'string)
  966. (defcustom helm-c-grep-use-ioccur-style-keys t
  967. "Use Arrow keys to jump to occurences."
  968. :group 'helm-config
  969. :type 'boolean)
  970. (defcustom helm-c-pdfgrep-default-read-command "xpdf '%f' %p"
  971. "Default command to read pdf files from pdfgrep.
  972. Where '%f' format spec is filename and '%p' is page number"
  973. :group 'helm-config
  974. :type 'string)
  975. (defcustom helm-c-etags-tag-file-name "TAGS"
  976. "Etags tag file name."
  977. :type 'string
  978. :group 'helm-config)
  979. (defcustom helm-c-etags-tag-file-search-limit 10
  980. "The limit level of directory to search tag file.
  981. Don't search tag file deeply if outside this value."
  982. :type 'number
  983. :group 'helm-config)
  984. (defcustom helm-c-etags-use-regexp-search nil
  985. "When non--nil search etags candidates by regexp.
  986. This disable helm-match-plugin when enabled.
  987. When nil search is performed directly on patter and *match-plugin is used
  988. if available. You can customize `helm-c-etags-search-regexp'."
  989. :group 'helm-config
  990. :type 'boolean)
  991. (defcustom helm-c-etags-search-regexp "^.+: .+ \\<%s"
  992. "Regexp that match tags in an helm etags buffer.
  993. The format spec is replaced by pattern.
  994. This regexp have no effect when `helm-c-etags-use-regexp-search'
  995. is nil."
  996. :group 'helm-config
  997. :type 'regexp)
  998. (defcustom helm-c-eldoc-in-minibuffer-show-fn
  999. 'helm-c-show-info-in-mode-line
  1000. "A function to display eldoc info.
  1001. Should take one arg: the string to display."
  1002. :group 'helm-config
  1003. :type 'symbol)
  1004. (defcustom helm-c-turn-on-show-completion t
  1005. "Display candidate in buffer while moving selection when non--nil."
  1006. :group 'helm-config
  1007. :type 'boolean)
  1008. (defcustom helm-c-show-completion-use-special-display t
  1009. "A special display will be used in lisp completion if non--nil.
  1010. All functions that are wrapped in macro `with-helm-show-completion'
  1011. will be affected."
  1012. :group 'helm-config
  1013. :type 'boolean)
  1014. (defcustom helm-c-show-completion-min-window-height 7
  1015. "Minimum completion window height used in show completion.
  1016. This is used in macro `with-helm-show-completion'."
  1017. :group 'helm-config
  1018. :type 'integer)
  1019. (defcustom helm-lisp-completion-or-indent-delay 0.6
  1020. "After this delay `helm-lisp-completion-counter' is reset to 0.
  1021. This allow to indent again without completing lisp symbol after this delay.
  1022. Default is 0.6 seconds."
  1023. :group 'helm-config
  1024. :type 'number)
  1025. (defcustom helm-c-default-external-file-browser "nautilus"
  1026. "Default external file browser for your system.
  1027. Directories will be opened externally with it when
  1028. opening file externally in `helm-find-files'.
  1029. Set to nil if you do not have external file browser
  1030. or do not want to use it.
  1031. Windows users should set that to \"explorer.exe\"."
  1032. :group 'helm-config
  1033. :type 'string)
  1034. (defcustom helm-c-use-adaptative-sorting nil
  1035. "Wheter to use or not adaptative sorting.
  1036. Even if a source use it, it will have no effect when set to nil."
  1037. :type 'boolean
  1038. :group 'helm-config)
  1039. (defcustom helm-ff-newfile-prompt-p t
  1040. "Whether Prompt or not when creating new file.
  1041. This set `ffap-newfile-prompt'."
  1042. :type 'boolean
  1043. :group 'helm-config)
  1044. (defcustom helm-ff-avfs-directory nil
  1045. "The default avfs directory, usually '.avfs'.
  1046. When this is set you will be able to expand archive filenames with `C-z'
  1047. inside an avfs directory mounted with mountavfs.
  1048. See <http://sourceforge.net/projects/avf/>."
  1049. :type 'boolean
  1050. :group 'helm-config)
  1051. (defcustom helm-ff-file-compressed-list '("gz" "bz2" "zip" "7z")
  1052. "Minimal list of compressed files extension."
  1053. :type 'list
  1054. :group 'helm-config)
  1055. (defcustom helm-locate-db-file-regexp "m?locate\.db$"
  1056. "Default regexp to match locate database.
  1057. If nil Search in all files."
  1058. :type 'string
  1059. :group 'helm-config)
  1060. (defcustom helm-ff-locate-db-filename "locate.db"
  1061. "The basename of the locatedb file you use locally in your directories.
  1062. When this is set and `helm' find such a file in the directory from
  1063. where you launch locate, it will use this file and will not prompt you
  1064. for a db file.
  1065. Note that this happen only when locate is launched with a prefix arg."
  1066. :group 'helm-config
  1067. :type 'string)
  1068. (defcustom helm-c-locate-command nil
  1069. "A list of arguments for locate program.
  1070. If nil it will be calculated when `helm-locate' startup
  1071. with these default values for different systems:
  1072. Gnu/linux: \"locate -i -r %s\"
  1073. berkeley-unix: \"locate -i %s\"
  1074. windows-nt: \"es -i -r %s\"
  1075. Others: \"locate %s\"
  1076. This string will be passed to format so it should end with `%s'.
  1077. The \"-r\" option must be the last option."
  1078. :type 'string
  1079. :group 'helm-config)
  1080. (defcustom helm-c-show-info-in-mode-line-delay 12
  1081. "Eldoc will show info in mode-line during this delay if user is idle."
  1082. :type 'integer
  1083. :group 'helm-config)
  1084. (defcustom helm-c-copy-files-async-log-file "/tmp/dired.log"
  1085. "The file used to communicate with two emacs when copying files async."
  1086. :type 'string
  1087. :group 'helm-config)
  1088. (defcustom helm-ff-printer-list nil
  1089. "A list of available printers on your system.
  1090. When non--nil let you choose a printer to print file.
  1091. Otherwise when nil the variable `printer-name' will be used.
  1092. On Unix based systems (lpstat command needed) you don't need to set this,
  1093. `helm-ff-find-printers' will find a list of available printers for you."
  1094. :type 'list
  1095. :group 'helm-config)
  1096. (defcustom helm-ff-transformer-show-only-basename nil
  1097. "Show only basename of candidates in `helm-find-files'.
  1098. This can be toggled at anytime from `helm-find-files' with \
  1099. \\<helm-find-files-map>0\\[helm-ff-run-toggle-basename]."
  1100. :type 'boolean
  1101. :group 'helm-config)
  1102. (defcustom helm-ff-quick-delete-dont-prompt-for-deletion nil
  1103. "Don't ask in persistent deletion of files when non--nil."
  1104. :group 'helm-config
  1105. :type 'boolean)
  1106. (defcustom helm-ff-signal-error-on-dot-files t
  1107. "Signal error when file is `.' or `..' on file deletion when non--nil.
  1108. Default is non--nil.
  1109. WARNING: Setting this to nil is unsafe and can cause deletion of a whole tree."
  1110. :group 'helm-config
  1111. :type 'boolean)
  1112. (defcustom helm-completing-read-handlers-alist
  1113. '((describe-function . helm-completing-read-symbols)
  1114. (describe-variable . helm-completing-read-symbols)
  1115. (debug-on-entry . helm-completing-read-symbols)
  1116. (find-function . helm-completing-read-symbols)
  1117. (trace-function . helm-completing-read-symbols)
  1118. (trace-function-background . helm-completing-read-symbols)
  1119. (find-tag . helm-completing-read-with-cands-in-buffer)
  1120. (ffap-alternate-file . nil))
  1121. "Alist of handlers to replace `completing-read', `read-file-name' in `helm-mode'.
  1122. Each entry is a cons cell like \(emacs_command . completing-read_handler\)
  1123. where key and value are symbols.
  1124. Each key is an Emacs command that use originaly `completing-read'.
  1125. Each value maybe an helm function that take same arguments as
  1126. `completing-read' plus NAME and BUFFER, where NAME is the name of the new
  1127. helm source and BUFFER the name of the buffer we will use.
  1128. This function prefix name must start by \"helm\".
  1129. See `helm-completing-read-symbols' for example.
  1130. If the value of an entry is nil completion will fall back to
  1131. emacs vanilla behavior.
  1132. e.g If you want to disable helm completion for `describe-function':
  1133. \(describe-function . nil\).
  1134. Ido is also supported, you can use `ido-completing-read' and
  1135. `ido-read-file-name' as value of an entry or just 'ido.
  1136. e.g ido completion for `find-file':
  1137. \(find-file . ido\)
  1138. same as
  1139. \(find-file . ido-read-file-name\)
  1140. Note that you don't need to enable `ido-mode' for this to work."
  1141. :group 'helm-config
  1142. :type '(alist :key-type symbol :value-type symbol))
  1143. (defcustom helm-M-x-requires-pattern 2
  1144. "Value of requires-pattern for `helm-M-x'.
  1145. Set it to 0 to disable requires-pattern in `helm-M-x'."
  1146. :group 'helm-config
  1147. :type 'boolean)
  1148. ;;; Build info-index sources with info-index plug-in.
  1149. ;;
  1150. ;;
  1151. (defun helm-c-build-info-index-command (name doc source buffer)
  1152. "Define an helm command NAME with documentation DOC.
  1153. Arg SOURCE will be an existing helm source named
  1154. `helm-c-source-info-<NAME>' and BUFFER a string buffer name."
  1155. (eval (list 'defun name nil doc
  1156. (list 'interactive)
  1157. (list 'helm
  1158. :sources source
  1159. :buffer buffer
  1160. :candidate-number-limit 1000))))
  1161. (defun helm-c-define-info-index-sources (var-value &optional commands)
  1162. "Define helm sources named helm-c-source-info-<NAME>.
  1163. Sources are generated for all entries of `helm-c-default-info-index-list'.
  1164. If COMMANDS arg is non--nil build also commands named `helm-info<NAME>'.
  1165. Where NAME is one of `helm-c-default-info-index-list'."
  1166. (loop with symbols = (loop for str in var-value
  1167. collect
  1168. (intern (concat "helm-c-source-info-" str)))
  1169. for sym in symbols
  1170. for str in var-value
  1171. do (set sym (list (cons 'name (format "Info index: %s" str))
  1172. (cons 'info-index str)))
  1173. when commands
  1174. do (let ((com (intern (concat "helm-info-" str))))
  1175. (helm-c-build-info-index-command com
  1176. (format "Predefined helm for %s info." str) sym
  1177. (format "*helm info %s*" str)))))
  1178. (defun helm-info-index-set (var value)
  1179. (set var value)
  1180. (helm-c-define-info-index-sources value t))
  1181. (defcustom helm-c-default-info-index-list
  1182. '("elisp" "cl" "org" "gnus" "tramp" "ratpoison"
  1183. "zsh" "bash" "coreutils" "fileutils"
  1184. "find" "sh-utils" "textutils" "libc"
  1185. "make" "automake" "autoconf" "emacs-lisp-intro"
  1186. "emacs" "elib" "eieio" "gauche-refe" "guile"
  1187. "guile-tut" "goops" "screen" "latex" "gawk"
  1188. "sed" "m4" "wget" "binutils" "as" "bfd" "gprof"
  1189. "ld" "diff" "flex" "grep" "gzip" "libtool"
  1190. "texinfo" "info" "gdb" "stabs" "cvsbook" "cvs"
  1191. "bison" "id-utils" "global")
  1192. "Info Manual entries to use for building helm info index commands."
  1193. :group 'helm-config
  1194. :type 'list
  1195. :set 'helm-info-index-set)
  1196. (defcustom helm-c-register-max-offset 160
  1197. "Max size of string register entries before truncating."
  1198. :group 'helm-config
  1199. :type 'integer)
  1200. ;;; General internal variables
  1201. ;;
  1202. ;; Some internals variable that need to be loaded
  1203. ;; here to avoid compiler warnings.
  1204. (defvar helm-c-external-commands-list nil
  1205. "A list of all external commands the user can execute. If this
  1206. variable is not set by the user, it will be calculated
  1207. automatically.")
  1208. (defvar helm-c-show-completion-overlay nil)
  1209. ;;; Faces
  1210. ;;
  1211. ;;
  1212. (defface helm-buffer-saved-out
  1213. '((t (:foreground "red")))
  1214. "*Face used for buffer files modified outside of emacs."
  1215. :group 'helm-config)
  1216. (defface helm-buffer-not-saved
  1217. '((t (:foreground "Indianred2")))
  1218. "*Face used for buffer files not already saved on disk."
  1219. :group 'helm-config)
  1220. (defface helm-ff-prefix
  1221. '((t (:background "yellow" :foreground "black")))
  1222. "*Face used to prefix new file or url paths in `helm-find-files'."
  1223. :group 'helm-config)
  1224. (defface helm-ff-executable
  1225. '((t (:foreground "green")))
  1226. "*Face used for executable files in `helm-find-files'."
  1227. :group 'helm-config)
  1228. (defface helm-ff-directory
  1229. '((t (:foreground "DarkRed" :background "LightGray")))
  1230. "*Face used for directories in `helm-find-files'."
  1231. :group 'helm-config)
  1232. (defface helm-ff-symlink
  1233. '((t (:foreground "DarkOrange")))
  1234. "*Face used for symlinks in `helm-find-files'."
  1235. :group 'helm-config)
  1236. (defface helm-ff-invalid-symlink
  1237. '((t (:foreground "black" :background "red")))
  1238. "*Face used for invalid symlinks in `helm-find-files'."
  1239. :group 'helm-config)
  1240. (defface helm-ff-file
  1241. '((t (:foreground "CadetBlue" :underline t)))
  1242. "*Face used for file names in `helm-find-files'."
  1243. :group 'helm-config)
  1244. (defface helm-grep-match
  1245. '((t (:inherit match)))
  1246. "Face used to highlight grep matches."
  1247. :group 'helm-config)
  1248. (defface helm-grep-file
  1249. '((t (:foreground "BlueViolet" :underline t)))
  1250. "Face used to highlight grep results filenames."
  1251. :group 'helm-config)
  1252. (defface helm-grep-lineno
  1253. '((t (:foreground "Darkorange1")))
  1254. "Face used to highlight grep number lines."
  1255. :group 'helm-config)
  1256. (defface helm-grep-running
  1257. '((t (:foreground "Red")))
  1258. "Face used in mode line when grep is running."
  1259. :group 'helm-config)
  1260. (defface helm-grep-finish
  1261. '((t (:foreground "Green")))
  1262. "Face used in mode line when grep is finish."
  1263. :group 'helm-config)
  1264. (defface helm-M-x-key-face '((t (:foreground "orange" :underline t)))
  1265. "*Face used in helm-M-x to show keybinding."
  1266. :group 'helm)
  1267. (defface helm-bmkext-info
  1268. '((t (:foreground "green")))
  1269. "*Face used for W3m Emacs bookmarks (not w3m bookmarks)."
  1270. :group 'helm)
  1271. (defface helm-bmkext-w3m
  1272. '((t (:foreground "yellow")))
  1273. "*Face used for W3m Emacs bookmarks (not w3m bookmarks)."
  1274. :group 'helm)
  1275. (defface helm-bmkext-gnus
  1276. '((t (:foreground "magenta")))
  1277. "*Face used for Gnus bookmarks."
  1278. :group 'helm)
  1279. (defface helm-bmkext-man
  1280. '((t (:foreground "Orange4")))
  1281. "*Face used for Woman/man bookmarks."
  1282. :group 'helm)
  1283. (defface helm-bmkext-no--file
  1284. '((t (:foreground "grey")))
  1285. "*Face used for non--file bookmarks."
  1286. :group 'helm)
  1287. (defface helm-bmkext-file
  1288. '((t (:foreground "Deepskyblue2")))
  1289. "*Face used for non--file bookmarks."
  1290. :group 'helm)
  1291. (defface helm-bookmarks-su-face '((t (:foreground "red")))
  1292. "Face for su/sudo bookmarks."
  1293. :group 'helm)
  1294. (defface helm-w3m-bookmarks-face '((t (:foreground "cyan1" :underline t)))
  1295. "Face for w3m bookmarks" :group 'helm)
  1296. (defface helm-emms-playlist
  1297. '((t (:foreground "Springgreen4" :underline t)))
  1298. "*Face used for tracks in current emms playlist."
  1299. :group 'helm)
  1300. (defface helm-apt-installed
  1301. '((t (:foreground "green")))
  1302. "*Face used for apt installed candidates."
  1303. :group 'helm)
  1304. (defface helm-apt-deinstalled
  1305. '((t (:foreground "DimGray")))
  1306. "*Face used for apt deinstalled candidates."
  1307. :group 'helm)
  1308. (defface helm-gentoo-match-face '((t (:foreground "red")))
  1309. "Face for helm-gentoo installed packages."
  1310. :group 'traverse-faces)
  1311. (defface helm-lisp-show-completion
  1312. '((t (:background "DarkSlateGray")))
  1313. "*Face used for showing candidates in `helm-lisp-completion'."
  1314. :group 'helm-config)
  1315. (defface helm-lisp-completion-info
  1316. '((t (:foreground "red")))
  1317. "*Face used for showing info in `helm-lisp-completion'."
  1318. :group 'helm-config)
  1319. (defface helm-overlay-line-face '((t (:background "IndianRed4" :underline t)))
  1320. "Face for source header in the helm buffer." :group 'helm)
  1321. ;;;###autoload
  1322. (defun helm-configuration ()
  1323. "Customize `helm'."
  1324. (interactive)
  1325. (customize-group "helm-config"))
  1326. ;;; Helm-command-map
  1327. ;;
  1328. ;;
  1329. ;;;###autoload
  1330. (defvar helm-command-map)
  1331. (define-prefix-command 'helm-command-map)
  1332. (define-key helm-command-map (kbd "<SPC>") 'helm-execute-helm-command)
  1333. (define-key helm-command-map (kbd "a") 'helm-c-apropos)
  1334. (define-key helm-command-map (kbd "e") 'helm-c-etags-select)
  1335. (define-key helm-command-map (kbd "l") 'helm-locate)
  1336. (define-key helm-command-map (kbd "s") 'helm-surfraw)
  1337. (define-key helm-command-map (kbd "r") 'helm-regexp)
  1338. (define-key helm-command-map (kbd "w") 'helm-w3m-bookmarks)
  1339. (define-key helm-command-map (kbd "x") 'helm-firefox-bookmarks)
  1340. (define-key helm-command-map (kbd "#") 'helm-emms)
  1341. (define-key helm-command-map (kbd "m") 'helm-man-woman)
  1342. (define-key helm-command-map (kbd "t") 'helm-top)
  1343. (define-key helm-command-map (kbd "i") 'helm-imenu)
  1344. (define-key helm-command-map (kbd "<tab>") 'helm-lisp-completion-at-point)
  1345. (define-key helm-command-map (kbd "p") 'helm-list-emacs-process)
  1346. (define-key helm-command-map (kbd "C-x r b") 'helm-c-pp-bookmarks)
  1347. (define-key helm-command-map (kbd "M-y") 'helm-show-kill-ring)
  1348. (define-key helm-command-map (kbd "C-c <SPC>") 'helm-all-mark-rings)
  1349. (define-key helm-command-map (kbd "C-x C-f") 'helm-find-files)
  1350. (define-key helm-command-map (kbd "f") 'helm-for-files)
  1351. (define-key helm-command-map (kbd "C-:") 'helm-eval-expression-with-eldoc)
  1352. (define-key helm-command-map (kbd "C-,") 'helm-calcul-expression)
  1353. (define-key helm-command-map (kbd "M-x") 'helm-M-x)
  1354. (define-key helm-command-map (kbd "C-x C-w") 'helm-write-file)
  1355. (define-key helm-command-map (kbd "C-x i") 'helm-insert-file)
  1356. (define-key helm-command-map (kbd "M-s o") 'helm-occur)
  1357. (define-key helm-command-map (kbd "M-g s") 'helm-do-grep)
  1358. (define-key helm-command-map (kbd "c") 'helm-colors)
  1359. (define-key helm-command-map (kbd "F") 'helm-select-xfont)
  1360. (define-key helm-command-map (kbd "C-c f") 'helm-recentf)
  1361. (define-key helm-command-map (kbd "C-c g") 'helm-google-suggest)
  1362. (define-key helm-command-map (kbd "h i") 'helm-info-at-point)
  1363. (define-key helm-command-map (kbd "h r") 'helm-info-emacs)
  1364. (define-key helm-command-map (kbd "h g") 'helm-info-gnus)
  1365. (define-key helm-command-map (kbd "C-x C-b") 'helm-buffers-list)
  1366. (define-key helm-command-map (kbd "C-c C-b") 'helm-browse-code)
  1367. (define-key helm-command-map (kbd "C-x r i") 'helm-register)
  1368. (define-key helm-command-map (kbd "C-c C-x") 'helm-c-run-external-command)
  1369. ;; In Emacs 23.1.50, minibuffer-local-must-match-filename-map was renamed to
  1370. ;; minibuffer-local-filename-must-match-map.
  1371. (defvar minibuffer-local-filename-must-match-map (make-sparse-keymap)) ;; Emacs 23.1.+
  1372. (defvar minibuffer-local-must-match-filename-map (make-sparse-keymap)) ;; Older Emacsen
  1373. (dolist (map (list minibuffer-local-filename-completion-map
  1374. minibuffer-local-completion-map
  1375. minibuffer-local-must-match-filename-map
  1376. minibuffer-local-filename-must-match-map
  1377. minibuffer-local-map
  1378. minibuffer-local-isearch-map
  1379. minibuffer-local-must-match-map
  1380. minibuffer-local-ns-map))
  1381. (define-key map "\C-r" 'helm-minibuffer-history))
  1382. ;;; Menu
  1383. ;;
  1384. ;;
  1385. (easy-menu-define nil global-map
  1386. "`helm' menu"
  1387. '("Helm"
  1388. ["All helm commands" helm-execute-helm-command t]
  1389. ["Find any Files/Buffers" helm-for-files t]
  1390. ["Helm Everywhere (Toggle)" helm-mode t]
  1391. "----"
  1392. ("Files:"
  1393. ["Find files" helm-find-files t]
  1394. ["Recent Files" helm-recentf t]
  1395. ["Locate" helm-locate t]
  1396. ["Bookmarks" helm-c-pp-bookmarks t])
  1397. ("Buffers:"
  1398. ["Find buffers" helm-buffers-list t])
  1399. ("Commands:"
  1400. ["Emacs Commands" helm-M-x t]
  1401. ["Externals Commands" helm-c-run-external-command t])
  1402. ("Help:"
  1403. ["Helm Apropos" helm-c-apropos t])
  1404. ("Info:"
  1405. ["Info at point" helm-info-at-point t]
  1406. ["Emacs Manual index" helm-info-emacs t]
  1407. ["Gnus Manual index" helm-info-gnus t])
  1408. ("Org:"
  1409. ["Org keywords" helm-org-keywords t]
  1410. ["Org headlines" helm-org-headlines t])
  1411. ("Tools:"
  1412. ["Occur" helm-occur t]
  1413. ["Grep" helm-do-grep t]
  1414. ["Etags" helm-c-etags-select t]
  1415. ["Lisp complete at point" helm-lisp-completion-at-point t]
  1416. ["Browse Kill ring" helm-show-kill-ring t]
  1417. ["Browse register" helm-register t]
  1418. ["Browse code" helm-browse-code t]
  1419. ["Mark Ring" helm-all-mark-rings t]
  1420. ["Regexp handler" helm-regexp t]
  1421. ["Colors & Faces" helm-colors t]
  1422. ["Show xfonts" helm-select-xfont t]
  1423. ["Ucs Symbols" helm-ucs t]
  1424. ["Imenu" helm-imenu t]
  1425. ["Google Suggest" helm-google-suggest t]
  1426. ["Eval expression" helm-eval-expression-with-eldoc t]
  1427. ["Calcul expression" helm-calcul-expression t]
  1428. ["Man pages" helm-man-woman t]
  1429. ["Top externals process" helm-top t]
  1430. ["Emacs internals process" helm-list-emacs-process t])
  1431. "----"
  1432. ["Prefered Options" helm-configuration t]))
  1433. ;;; Helm map add ons
  1434. ;;
  1435. ;;
  1436. (define-key helm-map (kbd "C-x C-f") 'helm-quit-and-find-file)
  1437. (define-key helm-map (kbd "M-m") 'helm-toggle-all-marks)
  1438. (define-key helm-map (kbd "C-w") 'helm-yank-text-at-point)
  1439. ;;; Specialized keymaps
  1440. ;;
  1441. ;;
  1442. (defvar helm-c-buffer-map
  1443. (let ((map (copy-keymap helm-map)))
  1444. (define-key map (kbd "C-c ?") 'helm-c-buffer-help)
  1445. ;; No need to have separate command for grep and zgrep
  1446. ;; as we don't use recursivity for buffers.
  1447. ;; So use zgrep for both as it is capable to handle non--compressed files.
  1448. (define-key map (kbd "M-g s") 'helm-buffer-run-zgrep)
  1449. (define-key map (kbd "C-c o") 'helm-buffer-switch-other-window)
  1450. (define-key map (kbd "C-c C-o") 'helm-buffer-switch-other-frame)
  1451. (define-key map (kbd "C-c =") 'helm-buffer-run-ediff)
  1452. (define-key map (kbd "M-=") 'helm-buffer-run-ediff-merge)
  1453. (define-key map (kbd "C-=") 'helm-buffer-diff-persistent)
  1454. (define-key map (kbd "M-U") 'helm-buffer-revert-persistent)
  1455. (define-key map (kbd "M-D") 'helm-buffer-run-kill-buffers)
  1456. (define-key map (kbd "C-x C-s") 'helm-buffer-save-persistent)
  1457. (define-key map (kbd "C-M-%") 'helm-buffer-run-query-replace-regexp)
  1458. (define-key map (kbd "M-%") 'helm-buffer-run-query-replace)
  1459. (define-key map (kbd "M-m") 'helm-toggle-all-marks)
  1460. (define-key map (kbd "M-a") 'helm-mark-all)
  1461. (when (locate-library "elscreen")
  1462. (define-key map (kbd "<C-tab>") 'helm-buffer-switch-to-elscreen))
  1463. (delq nil map))
  1464. "Keymap for buffer sources in helm.")
  1465. (defvar helm-find-files-map
  1466. (let ((map (copy-keymap helm-map)))
  1467. (define-key map (kbd "C-]") 'helm-ff-run-toggle-basename)
  1468. (define-key map (kbd "C-x C-f") 'helm-ff-run-locate)
  1469. (define-key map (kbd "M-g s") 'helm-ff-run-grep)
  1470. (define-key map (kbd "M-g p") 'helm-ff-run-pdfgrep)
  1471. (define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
  1472. (define-key map (kbd "M-.") 'helm-ff-run-etags)
  1473. (define-key map (kbd "M-R") 'helm-ff-run-rename-file)
  1474. (define-key map (kbd "M-C") 'helm-ff-run-copy-file)
  1475. (define-key map (kbd "M-B") 'helm-ff-run-byte-compile-file)
  1476. (define-key map (kbd "M-L") 'helm-ff-run-load-file)
  1477. (define-key map (kbd "M-S") 'helm-ff-run-symlink-file)
  1478. (define-key map (kbd "M-H") 'helm-ff-run-hardlink-file)
  1479. (define-key map (kbd "M-D") 'helm-ff-run-delete-file)
  1480. (define-key map (kbd "M-K") 'helm-ff-run-kill-buffer-persistent)
  1481. (define-key map (kbd "C-d") 'helm-ff-persistent-delete)
  1482. (define-key map (kbd "M-e") 'helm-ff-run-switch-to-eshell)
  1483. (define-key map (kbd "<M-tab>") 'helm-ff-run-complete-fn-at-point)
  1484. (define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
  1485. (define-key map (kbd "C-c C-o") 'helm-ff-run-switch-other-frame)
  1486. (define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
  1487. (define-key map (kbd "M-!") 'helm-ff-run-eshell-command-on-file)
  1488. (define-key map (kbd "C-=") 'helm-ff-run-ediff-file)
  1489. (define-key map (kbd "C-c =") 'helm-ff-run-ediff-merge-file)
  1490. (define-key map (kbd "M-p") 'helm-ff-run-switch-to-history)
  1491. (define-key map (kbd "M-i") 'helm-ff-properties-persistent)
  1492. (define-key map (kbd "C-c ?") 'helm-ff-help)
  1493. (define-key map (kbd "C-}") 'helm-narrow-window)
  1494. (define-key map (kbd "C-{") 'helm-enlarge-window)
  1495. (define-key map (kbd "C-<backspace>") 'helm-ff-run-toggle-auto-update)
  1496. (define-key map (kbd "M-a") 'helm-mark-all)
  1497. (define-key map (kbd "M-m") 'helm-toggle-all-marks)
  1498. (define-key map (kbd "M-u") 'helm-unmark-all)
  1499. (define-key map (kbd "C-c C-a") 'helm-ff-run-gnus-attach-files)
  1500. (define-key map (kbd "C-c p") 'helm-ff-run-print-file)
  1501. ;; Next 2 have no effect if candidate is not an image file.
  1502. (define-key map (kbd "M-l") 'helm-ff-rotate-left-persistent)
  1503. (define-key map (kbd "M-r") 'helm-ff-rotate-right-persistent)
  1504. (define-key map (kbd "C-.") 'helm-find-files-down-one-level)
  1505. (define-key map (kbd "C-l") 'helm-find-files-down-one-level)
  1506. (define-key map (kbd "C-h C-b") 'helm-send-bug-report-from-helm)
  1507. (define-key map (kbd "C-h C-d") 'helm-debug-output)
  1508. (when helm-ff-lynx-style-map
  1509. (define-key map (kbd "<left>") 'helm-find-files-down-one-level)
  1510. (define-key map (kbd "<right>") 'helm-execute-persistent-action))
  1511. (delq nil map))
  1512. "Keymap for `helm-find-files'.")
  1513. (defvar helm-c-read-file-map
  1514. (let ((map (copy-keymap helm-map)))
  1515. (define-key map (kbd "C-]") 'helm-ff-run-toggle-basename)
  1516. (define-key map (kbd "C-.") 'helm-find-files-down-one-level)
  1517. (define-key map (kbd "C-l") 'helm-find-files-down-one-level)
  1518. (define-key map (kbd "C-<backspace>") 'helm-ff-run-toggle-auto-update)
  1519. (define-key map (kbd "C-c ?") 'helm-read-file-name-help)
  1520. (when helm-ff-lynx-style-map
  1521. (define-key map (kbd "<left>") 'helm-find-files-down-one-level)
  1522. (define-key map (kbd "<right>") 'helm-execute-persistent-action)
  1523. (define-key map (kbd "C-o") nil)
  1524. (define-key map (kbd "<M-left>") 'helm-previous-source)
  1525. (define-key map (kbd "<M-right>") 'helm-next-source))
  1526. (delq nil map))
  1527. "Keymap for `helm-c-read-file-name'.")
  1528. (defvar helm-generic-files-map
  1529. (let ((map (copy-keymap helm-map)))
  1530. (define-key map (kbd "M-g s") 'helm-ff-run-grep)
  1531. (define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
  1532. (define-key map (kbd "M-g p") 'helm-ff-run-pdfgrep)
  1533. (define-key map (kbd "M-D") 'helm-ff-run-delete-file)
  1534. (define-key map (kbd "C-=") 'helm-ff-run-ediff-file)
  1535. (define-key map (kbd "C-c =") 'helm-ff-run-ediff-merge-file)
  1536. (define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
  1537. (define-key map (kbd "M-i") 'helm-ff-properties-persistent)
  1538. (define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
  1539. (define-key map (kbd "C-w") 'helm-yank-text-at-point)
  1540. (define-key map (kbd "C-c ?") 'helm-generic-file-help)
  1541. map)
  1542. "Generic Keymap for files.")
  1543. (defvar helm-c-grep-map
  1544. (let ((map (copy-keymap helm-map)))
  1545. (define-key map (kbd "M-<down>") 'helm-c-goto-next-file)
  1546. (define-key map (kbd "M-<up>") 'helm-c-goto-precedent-file)
  1547. (define-key map (kbd "C-c o") 'helm-c-grep-run-other-window-action)
  1548. (define-key map (kbd "C-w") 'helm-yank-text-at-point)
  1549. (define-key map (kbd "C-x C-s") 'helm-c-grep-run-save-buffer)
  1550. (when helm-c-grep-use-ioccur-style-keys
  1551. (define-key map (kbd "<right>") 'helm-c-grep-run-persistent-action)
  1552. (define-key map (kbd "<left>") 'helm-c-grep-run-default-action))
  1553. (define-key map (kbd "C-c ?") 'helm-grep-help)
  1554. (delq nil map))
  1555. "Keymap used in Grep sources.")
  1556. (defvar helm-c-pdfgrep-map
  1557. (let ((map (copy-keymap helm-map)))
  1558. (define-key map (kbd "M-<down>") 'helm-c-goto-next-file)
  1559. (define-key map (kbd "M-<up>") 'helm-c-goto-precedent-file)
  1560. (define-key map (kbd "C-w") 'helm-yank-text-at-point)
  1561. (define-key map (kbd "C-c ?") 'helm-pdfgrep-help)
  1562. map)
  1563. "Keymap used in pdfgrep.")
  1564. (defvar helm-c-etags-map
  1565. (let ((map (copy-keymap helm-map)))
  1566. (define-key map (kbd "M-<down>") 'helm-c-goto-next-file)
  1567. (define-key map (kbd "M-<up>") 'helm-c-goto-precedent-file)
  1568. (define-key map (kbd "C-w") 'helm-yank-text-at-point)
  1569. (define-key map (kbd "C-c ?") 'helm-etags-help)
  1570. map)
  1571. "Keymap used in Etags.")
  1572. (defvar helm-eval-expression-map
  1573. (let ((map (copy-keymap helm-map)))
  1574. (define-key map (kbd "<C-return>") 'helm-eval-new-line-and-indent)
  1575. (define-key map (kbd "<tab>") 'lisp-indent-line)
  1576. (define-key map (kbd "<C-tab>") 'lisp-complete-symbol)
  1577. (define-key map (kbd "C-p") 'previous-line)
  1578. (define-key map (kbd "C-n") 'next-line)
  1579. (define-key map (kbd "<up>") 'previous-line)
  1580. (define-key map (kbd "<down>") 'next-line)
  1581. (define-key map (kbd "<right>") 'forward-char)
  1582. (define-key map (kbd "<left>") 'backward-char)
  1583. map))
  1584. (defvar helm-c-ucs-map
  1585. (let ((map (copy-keymap helm-map)))
  1586. (define-key map (kbd "<C-backspace>") 'helm-c-ucs-persistent-delete)
  1587. (define-key map (kbd "<C-left>") 'helm-c-ucs-persistent-backward)
  1588. (define-key map (kbd "<C-right>") 'helm-c-ucs-persistent-forward)
  1589. (define-key map (kbd "<C-return>") 'helm-c-ucs-persistent-insert)
  1590. (define-key map (kbd "C-c ?") 'helm-c-ucs-help)
  1591. map)
  1592. "Keymap for `helm-ucs'.")
  1593. (defvar helm-c-bookmark-map
  1594. (let ((map (copy-keymap helm-map)))
  1595. (define-key map (kbd "C-c o") 'helm-c-bookmark-run-jump-other-window)
  1596. (define-key map (kbd "C-d") 'helm-c-bookmark-run-delete)
  1597. (when (locate-library "bookmark-extensions")
  1598. (define-key map (kbd "M-e") 'helm-c-bmkext-run-edit))
  1599. (define-key map (kbd "C-c ?") 'helm-c-bookmark-help)
  1600. (delq nil map))
  1601. "Generic Keymap for emacs bookmark sources.")
  1602. (defvar helm-esh-on-file-map
  1603. (let ((map (copy-keymap helm-map)))
  1604. (define-key map (kbd "C-c ?") 'helm-esh-help)
  1605. map)
  1606. "Keymap for `helm-find-files-eshell-command-on-file'.")
  1607. (defvar helm-eshell-history-map
  1608. (let ((map (copy-keymap helm-map)))
  1609. (define-key map (kbd "M-p") 'helm-next-line)
  1610. map)
  1611. "Keymap for `helm-eshell-history'.")
  1612. (defvar helm-kill-ring-map
  1613. (let ((map (copy-keymap helm-map)))
  1614. (define-key map (kbd "M-y") 'helm-next-line)
  1615. (define-key map (kbd "M-u") 'helm-previous-line)
  1616. map)
  1617. "Keymap for `helm-show-kill-ring'.")
  1618. (defvar helm-occur-map
  1619. (let ((map (copy-keymap helm-map)))
  1620. (define-key map (kbd "C-M-%") 'helm-occur-run-query-replace-regexp)
  1621. map)
  1622. "Keymap for `helm-occur'.")
  1623. ;;; Embeded documentation.
  1624. ;;
  1625. ;;
  1626. (defun helm-c-list-preconfigured-helm ()
  1627. "Collect preconfigured helm functions in this file."
  1628. (loop with doc
  1629. with sym
  1630. for entry in (cdr (assoc
  1631. (file-truename (locate-library "helm-config"))
  1632. load-history))
  1633. if (and (consp entry)
  1634. (eq (car entry) 'defun)
  1635. (string-match "^Preconfigured.+$"
  1636. (setq doc (or (documentation (setq sym (cdr entry)))
  1637. ""))))
  1638. collect (cons sym (match-string 0 doc))))
  1639. (defun helm-c-format-preconfigured-helm ()
  1640. (mapcar (lambda (x) (format "\\[%s] : %s\n" (car x) (cdr x)))
  1641. (helm-c-list-preconfigured-helm)))
  1642. ;;; Global help message - Used by `helm-help'
  1643. ;;
  1644. ;;
  1645. (setq helm-help-message
  1646. (lambda ()
  1647. (concat
  1648. "\\<helm-map>"
  1649. "`helm' is QuickSilver-like candidate-selection framework.
  1650. Narrow the list by typing some pattern,
  1651. Multiple patterns are allowed by splitting by space.
  1652. Select with natural Emacs operations, choose with RET.
  1653. If you have any problems, press C-c C-x C-b!!
  1654. Feel free to send bug reports. I'll fix them.
  1655. The steps are described in the beginning of helm.el file.
  1656. == Basic Operations ==
  1657. C-p, Up: Previous Line
  1658. C-n, Down : Next Line
  1659. M-v, PageUp : Previous Page
  1660. C-v, PageDown : Next Page
  1661. Enter : Execute first (default) action / Select
  1662. M-< : First Line
  1663. M-> : Last Line
  1664. M-PageUp, C-M-S-v, C-M-y : Previous Page (other-window)
  1665. M-PageDown, C-M-v : Next Page (other-window)
  1666. Tab, C-i : Show action list
  1667. Left : Previous Source
  1668. Right, C-o : Next Source
  1669. C-k : Delete pattern
  1670. C-z : Persistent Action (Execute action with helm session kept)
  1671. C-c C-x C-b: Send a bug report
  1672. == Shortcuts For 2nd/3rd Action ==
  1673. \\[helm-select-2nd-action-or-end-of-line] : Execute 2nd Action (if the minibuffer cursor is at end of line)
  1674. \\[helm-select-3rd-action] : Execute 3rd Action
  1675. == Visible Marks ==
  1676. Visible marks store candidate. Some actions uses marked candidates.
  1677. \\[helm-toggle-visible-mark] : Toggle Visible Mark
  1678. \\[helm-prev-visible-mark] : Previous Mark
  1679. \\[helm-next-visible-mark] : Next Mark
  1680. == Miscellaneous Commands ==
  1681. \\[helm-toggle-resplit-window] : Toggle vertical/horizontal split helm window
  1682. \\[helm-quit-and-find-file] : Drop into `find-file'
  1683. \\[helm-delete-current-selection] : Delete Selected Item (visually)
  1684. \\[helm-kill-selection-and-quit] : Set Item Into the kill-ring And Quit
  1685. \\[helm-yank-selection] : Yank Selected Item Into Pattern
  1686. \\[helm-follow-mode] : Toggle Automatical Execution Of Persistent Action
  1687. \\[helm-force-update] : Recalculate And Redisplay Candidates
  1688. == Global Commands ==
  1689. \\<global-map>\\[helm-resume] revives last `helm' session.
  1690. It is very useful, so you should bind any key.
  1691. Single source is executed by \\[helm-call-source].
  1692. == Preconfigured `helm' ==
  1693. Preconfigured `helm' is commands that uses `helm' interface.
  1694. You can use them without configuration.
  1695. "
  1696. (apply 'concat (helm-c-format-preconfigured-helm))
  1697. "
  1698. Enjoy!")))
  1699. ;;; `helm-buffer-list' help
  1700. ;;
  1701. ;;
  1702. (defvar helm-c-buffer-help-message
  1703. "== Helm Buffer ==
  1704. \nTips:
  1705. You can enter a partial name of major-mode (e.g lisp, sh) to narrow down buffers.
  1706. Enter then a space and a pattern to narrow down to buffers matching this pattern.
  1707. \nSpecific commands for `helm-buffers-list':
  1708. \\<helm-c-buffer-map>
  1709. \\[helm-buffer-run-zgrep]\t\t->Grep Buffer(s) works as zgrep too. (C-u grep all buffers but non--file buffers).
  1710. \\[helm-buffer-switch-other-window]\t\t->Switch other window.
  1711. \\[helm-buffer-switch-other-frame]\t\t->Switch other frame.
  1712. \\[helm-buffer-run-query-replace-regexp]\t\t->Query replace regexp in marked buffers.
  1713. \\[helm-buffer-run-query-replace]\t\t->Query replace in marked buffers.
  1714. \\[helm-buffer-switch-to-elscreen]\t\t->Find buffer in Elscreen.
  1715. \\[helm-buffer-run-ediff]\t\t->Ediff current buffer with candidate. If two marked buffers ediff those buffers.
  1716. \\[helm-buffer-run-ediff-merge]\t\t->Ediff merge current buffer with candidate. If two marked buffers ediff merge those buffers.
  1717. \\[helm-buffer-diff-persistent]\t\t->Toggle Diff buffer with saved file without quitting.
  1718. \\[helm-buffer-revert-persistent]\t\t->Revert buffer without quitting.
  1719. \\[helm-buffer-save-persistent]\t\t->Save buffer without quitting.
  1720. \\[helm-buffer-run-kill-buffers]\t\t->Delete marked buffers and quit.
  1721. \\[helm-toggle-all-marks]\t\t->Toggle all marks.
  1722. \\[helm-mark-all]\t\t->Mark all.
  1723. \\[helm-c-buffer-help]\t\t->Display this help.
  1724. \n== Helm Map ==
  1725. \\{helm-map}")
  1726. ;;;###autoload
  1727. (defun helm-c-buffer-help ()
  1728. "Help command for helm buffers."
  1729. (interactive)
  1730. (let ((helm-help-message helm-c-buffer-help-message))
  1731. (helm-help)))
  1732. ;;; Find files help (`helm-find-files')
  1733. ;;
  1734. ;;
  1735. (defvar helm-ff-help-message
  1736. "== Helm Find Files ==
  1737. \nTips:
  1738. \n- Enter `~/' at end of pattern to quickly reach home directory.
  1739. - Enter `/' at end of pattern to quickly reach root of your file system.
  1740. - Enter `./' at end of pattern to quickly reach `default-directory' (initial start of session).
  1741. - You can complete with partial basename \(e.g \"fb\" will complete \"foobar\"\).
  1742. - Use `C-u C-z' to watch an image.
  1743. - To browse images directories turn on `helm-follow-mode' and navigate with arrow keys.
  1744. - When entered ediff, hitting `C-g' will ask you to use locate to find the file to ediff with.
  1745. \nSpecific commands for `helm-find-files':
  1746. \\<helm-find-files-map>
  1747. \\[helm-ff-run-locate]\t\t->Run Locate on basename of candidate (C-u to specify locate db).
  1748. \\[helm-ff-run-grep]\t\t->Run Grep (C-u Recursive).
  1749. \\[helm-ff-run-pdfgrep]\t\t->Run Pdfgrep on marked files.
  1750. \\[helm-ff-run-zgrep]\t\t->Run zgrep (C-u Recursive).
  1751. \\[helm-ff-run-etags]\t\t->Run Etags (C-u use thing-at-point `C-u C-u' reload cache)
  1752. \\[helm-ff-run-rename-file]\t\t->Rename File (C-u Follow).
  1753. \\[helm-ff-run-copy-file]\t\t->Copy File (C-u Follow).
  1754. \\[helm-ff-run-byte-compile-file]\t\t->Byte Compile File (C-u Load).
  1755. \\[helm-ff-run-load-file]\t\t->Load File.
  1756. \\[helm-ff-run-symlink-file]\t\t->Symlink File.
  1757. \\[helm-ff-run-hardlink-file]\t\t->Hardlink file.
  1758. \\[helm-ff-run-delete-file]\t\t->Delete File.
  1759. \\[helm-ff-run-kill-buffer-persistent]\t\t->Kill buffer candidate without quitting.
  1760. \\[helm-ff-persistent-delete]\t\t->Delete file without quitting.
  1761. \\[helm-ff-run-switch-to-eshell]\t\t->Switch to Eshell.
  1762. \\[helm-ff-run-eshell-command-on-file]\t\t->Eshell command on file (C-u Run on all marked files at once).
  1763. \\[helm-ff-run-ediff-file]\t\t->Ediff file.
  1764. \\[helm-ff-run-ediff-merge-file]\t\t->Ediff merge file.
  1765. \\[helm-ff-run-complete-fn-at-point]\t\t->Complete file name at point.
  1766. \\[helm-ff-run-switch-other-window]\t\t->Switch other window.
  1767. \\[helm-ff-run-switch-other-frame]\t\t->Switch other frame.
  1768. \\[helm-ff-run-open-file-externally]\t\t->Open file with external program (C-u to choose).
  1769. \\[helm-ff-rotate-left-persistent]\t\t->Rotate Image Left.
  1770. \\[helm-ff-rotate-right-persistent]\t\t->Rotate Image Right.
  1771. \\[helm-find-files-down-one-level]\t\t->Go down precedent directory.
  1772. \\[helm-ff-run-switch-to-history]\t\t->Switch to helm find-files history.
  1773. \\[helm-ff-properties-persistent]\t\t->Show file properties in a tooltip.
  1774. \\[helm-mark-all]\t\t->Mark all visibles candidates.
  1775. \\[helm-ff-run-toggle-auto-update]\t->Toggle auto expansion of directories.
  1776. \\[helm-unmark-all]\t\t->Unmark all candidates, visibles and invisibles.
  1777. \\[helm-ff-run-gnus-attach-files]\t\t->Gnus attach files to message buffer.
  1778. \\[helm-ff-run-print-file]\t\t->Print file, (C-u to refresh printers list).
  1779. \\[helm-enlarge-window]\t\t->Enlarge helm window.
  1780. \\[helm-narrow-window]\t\t->Narrow helm window.
  1781. \\[helm-ff-run-toggle-basename]\t\t->Toggle basename/fullpath.
  1782. \\[helm-send-bug-report-from-helm]\t\t->Send Bug report.
  1783. \\[helm-ff-help]\t\t->Display this help info.
  1784. \n== Helm Map ==
  1785. \\{helm-map}")
  1786. ;;;###autoload
  1787. (defun helm-ff-help ()
  1788. "Help command for `helm-find-files'."
  1789. (interactive)
  1790. (let ((helm-help-message helm-ff-help-message))
  1791. (helm-help)))
  1792. ;;; Help for `helm-c-read-file-name'
  1793. ;;
  1794. ;;
  1795. (defvar helm-read-file-name-help-message
  1796. "== Helm read file name Map ==\
  1797. \nSpecific commands for helm-c-read-file-name:
  1798. \\<helm-c-read-file-map>
  1799. \\[helm-find-files-down-one-level]\t\t->Go down precedent directory.
  1800. \\[helm-ff-run-toggle-auto-update]\t->Toggle auto expansion of directories.
  1801. \\[helm-next-source]\t->Goto next source.
  1802. \\[helm-previous-source]\t->Goto previous source.
  1803. \\[helm-read-file-name-help]\t\t->Display this help info.
  1804. \n== Helm Map ==
  1805. \\{helm-map}")
  1806. ;;;###autoload
  1807. (defun helm-read-file-name-help ()
  1808. (interactive)
  1809. (let ((helm-help-message helm-read-file-name-help-message))
  1810. (helm-help)))
  1811. ;;; Generic file help - Used by locate.
  1812. ;;
  1813. ;;
  1814. (defvar helm-generic-file-help-message
  1815. "== Helm Generic files Map ==\
  1816. \nSpecific commands for helm locate and others files sources:
  1817. \\<helm-generic-files-map>
  1818. \\[helm-ff-run-grep]\t\t->Run grep (C-u recurse).
  1819. \\[helm-ff-run-pdfgrep]\t\t->Run Pdfgrep on marked files.
  1820. \\[helm-ff-run-delete-file]\t\t->Delete file.
  1821. \\[helm-ff-run-ediff-file]\t\t->Ediff file.
  1822. \\[helm-ff-run-ediff-merge-file]\t\t->Ediff merge file.
  1823. \\[helm-ff-run-switch-other-window]\t\t->Switch other window.
  1824. \\[helm-ff-properties-persistent]\t\t->Show file properties.
  1825. \\[helm-yank-text-at-point]\t\t->Yank text at point.
  1826. \\[helm-ff-run-open-file-externally]\t\t->Open file with external program (C-u to choose).
  1827. \nLocate tips:
  1828. You can add after writing search pattern any of the locate command line options.
  1829. e.g -b, -e, -n <number>...etc.
  1830. See Man locate for more infos.
  1831. \n== Helm Map ==
  1832. \\{helm-map}")
  1833. ;;;###autoload
  1834. (defun helm-generic-file-help ()
  1835. (interactive)
  1836. (let ((helm-help-message helm-generic-file-help-message))
  1837. (helm-help)))
  1838. ;;; Grep help
  1839. ;;
  1840. ;;
  1841. (defvar helm-grep-help-message
  1842. "== Helm Grep Map ==\
  1843. \nHelm Grep tips:
  1844. You can start grep with a prefix arg to recurse in subdirectories.
  1845. You can use wild card when selecting files (e.g *.el)
  1846. You can grep in many differents directories by marking files or wild cards.
  1847. You can save your results in a grep-mode buffer, see below.
  1848. \nSpecific commands for Helm Grep:
  1849. \\<helm-c-grep-map>
  1850. \\[helm-c-goto-next-file]\t->Next File.
  1851. \\[helm-c-goto-precedent-file]\t\t->Precedent File.
  1852. \\[helm-yank-text-at-point]\t\t->Yank Text at point in minibuffer.
  1853. \\[helm-c-grep-run-other-window-action]\t\t->Jump other window.
  1854. \\[helm-c-grep-run-persistent-action]\t\t->Run persistent action (Same as `C-z').
  1855. \\[helm-c-grep-run-default-action]\t\t->Run default action (Same as RET).
  1856. \\[helm-c-grep-run-save-buffer]\t\t->Save to a `grep-mode' enabled buffer.
  1857. \\[helm-grep-help]\t\t->Show this help.
  1858. \n== Helm Map ==
  1859. \\{helm-map}")
  1860. ;;;###autoload
  1861. (defun helm-grep-help ()
  1862. (interactive)
  1863. (let ((helm-help-message helm-grep-help-message))
  1864. (helm-help)))
  1865. ;;; Pdf grep help
  1866. ;;
  1867. ;;
  1868. (defvar helm-pdfgrep-help-message
  1869. "== Helm PdfGrep Map ==\
  1870. \nSpecific commands for Pdf Grep:
  1871. \\<helm-c-pdfgrep-map>
  1872. \\[helm-c-goto-next-file]\t->Next File.
  1873. \\[helm-c-goto-precedent-file]\t\t->Precedent File.
  1874. \\[helm-yank-text-at-point]\t\t->Yank Text at point in minibuffer.
  1875. \\[helm-pdfgrep-help]\t\t->Show this help.
  1876. \n== Helm Map ==
  1877. \\{helm-map}")
  1878. ;;;###autoload
  1879. (defun helm-pdfgrep-help ()
  1880. (interactive)
  1881. (let ((helm-help-message helm-pdfgrep-help-message))
  1882. (helm-help)))
  1883. ;;; Etags help
  1884. ;;
  1885. ;;
  1886. (defvar helm-etags-help-message
  1887. "== Helm Etags Map ==\
  1888. \nSpecific commands for Etags:
  1889. \\<helm-c-etags-map>
  1890. \\[helm-c-goto-next-file]\t->Next File.
  1891. \\[helm-c-goto-precedent-file]\t\t->Precedent File.
  1892. \\[helm-yank-text-at-point]\t\t->Yank Text at point in minibuffer.
  1893. \\[helm-etags-help]\t\t->Show this help.
  1894. \n== Helm Map ==
  1895. \\{helm-map}")
  1896. ;;;###autoload
  1897. (defun helm-etags-help ()
  1898. "The help function for etags."
  1899. (interactive)
  1900. (let ((helm-help-message helm-etags-help-message))
  1901. (helm-help)))
  1902. ;;; Ucs help
  1903. ;;
  1904. ;;
  1905. (defvar helm-c-ucs-help-message
  1906. "== Helm Ucs ==
  1907. \nSpecific commands for `helm-ucs':
  1908. \\<helm-c-ucs-map>
  1909. \\[helm-c-ucs-persistent-insert]\t->Insert char.
  1910. \\[helm-c-ucs-persistent-forward]\t->Forward char.
  1911. \\[helm-c-ucs-persistent-backward]\t->Backward char.
  1912. \\[helm-c-ucs-persistent-delete]\t->Delete char backward.
  1913. \\[helm-c-ucs-help]\t\t->Show this help.
  1914. \n== Helm Map ==
  1915. \\{helm-map}")
  1916. (defun helm-c-ucs-help ()
  1917. "Help command for `helm-ucs'."
  1918. (interactive)
  1919. (let ((helm-help-message helm-c-ucs-help-message))
  1920. (helm-help)))
  1921. ;;; Bookmark help
  1922. ;;
  1923. ;;
  1924. (defvar helm-bookmark-help-message
  1925. "== Helm bookmark name Map ==\
  1926. \nSpecific commands for bookmarks:
  1927. \\<helm-c-bookmark-map>
  1928. \\[helm-c-bookmark-run-jump-other-window]\t\t->Jump other window.
  1929. \\[helm-c-bookmark-run-delete]\t\t->Delete bookmark.
  1930. \\[helm-c-bmkext-run-edit]\t\t->Edit bookmark (only for bmkext).
  1931. \\[helm-c-bookmark-help]\t\t->Run this help.
  1932. \n== Helm Map ==
  1933. \\{helm-map}")
  1934. (defun helm-c-bookmark-help ()
  1935. "Help command for bookmarks."
  1936. (interactive)
  1937. (let ((helm-help-message helm-bookmark-help-message))
  1938. (helm-help)))
  1939. ;;; Eshell command on file help
  1940. ;;
  1941. ;;
  1942. (defvar helm-c-esh-help-message
  1943. "== Helm eshell on file ==
  1944. \nTips:
  1945. - Passing extra args after filename:
  1946. Normally your command or alias will be called with file as argument.
  1947. e.g <command> 'candidate_file'
  1948. But you can also pass an argument or more after 'candidate_file' like this:
  1949. <command> %s [extra_args]\n
  1950. 'candidate_file' will be inserted at '%s' and your command will look at this:
  1951. <command> 'candidate_file' [args]
  1952. - Specify many files as args (marked files):
  1953. e.g <command> file1 file2 ...
  1954. Please restart and use a prefix arg to call `helm-find-files-eshell-command-on-file'.
  1955. Otherwise your command will be called many times like this:
  1956. <command> file1 <command> file2 etc...
  1957. \nSpecific commands for `helm-find-files-eshell-command-on-file':
  1958. \\<helm-esh-on-file-map>
  1959. \\[helm-esh-help]\t\t->Display this help.
  1960. \n== Helm Map ==
  1961. \\{helm-map}")
  1962. (defun helm-esh-help ()
  1963. "Help command for `helm-find-files-eshell-command-on-file'."
  1964. (interactive)
  1965. (let ((helm-help-message helm-c-esh-help-message))
  1966. (helm-help)))
  1967. ;;; Mode line strings
  1968. ;;
  1969. ;;
  1970. (defvar helm-buffer-mode-line-string
  1971. '("Buffer(s)"
  1972. "\\<helm-c-buffer-map>\
  1973. \\[helm-c-buffer-help]:Help, \
  1974. \\<helm-map>\
  1975. \\[helm-select-action]:Acts,\
  1976. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  1977. \\[helm-select-3rd-action]:NthAct,\
  1978. \\[helm-send-bug-report-from-helm]:BugReport."
  1979. "String displayed in mode-line in `helm-c-source-buffers-list'"))
  1980. (defvar helm-ff-mode-line-string
  1981. "\\<helm-find-files-map>\
  1982. \\[helm-ff-help]:Help, \
  1983. \\[helm-send-bug-report-from-helm]:BugReport, \
  1984. \\<helm-map>\
  1985. \\[helm-select-action]:Acts, \
  1986. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  1987. \\[helm-select-3rd-action]:NthAct"
  1988. "String displayed in mode-line in `helm-c-source-find-files'")
  1989. (defvar helm-read-file-name-mode-line-string
  1990. "\\<helm-c-read-file-map>\
  1991. \\[helm-read-file-name-help]:Help, \
  1992. \\<helm-map>\
  1993. \\[helm-select-action]:Acts,\
  1994. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  1995. \\[helm-select-3rd-action]:NthAct"
  1996. "String displayed in mode-line in `helm-c-source-find-files'")
  1997. (defvar helm-generic-file-mode-line-string
  1998. "\\<helm-generic-files-map>\
  1999. \\[helm-generic-file-help]:Help, \
  2000. \\<helm-map>\
  2001. \\[helm-select-action]:Acts,\
  2002. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2003. \\[helm-select-3rd-action]:NthAct,\
  2004. \\[helm-send-bug-report-from-helm]:BugReport."
  2005. "String displayed in mode-line in Locate.")
  2006. (defvar helm-grep-mode-line-string
  2007. "\\<helm-c-grep-map>\
  2008. \\[helm-grep-help]:Help,\
  2009. \\<helm-map>\
  2010. \\[helm-select-action]:Acts,\
  2011. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2012. \\[helm-select-3rd-action]:NthAct,\
  2013. \\[helm-send-bug-report-from-helm]:BugReport."
  2014. "String displayed in mode-line in `helm-do-grep'.")
  2015. (defvar helm-pdfgrep-mode-line-string
  2016. "\\<helm-c-pdfgrep-map>\
  2017. \\[helm-pdfgrep-help]:Help,\
  2018. \\<helm-map>\
  2019. \\[helm-select-action]:Acts,\
  2020. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2021. \\[helm-select-3rd-action]:NthAct,\
  2022. \\[helm-send-bug-report-from-helm]:BugReport."
  2023. "String displayed in mode-line in `helm-do-pdfgrep'.")
  2024. (defvar helm-etags-mode-line-string
  2025. "\\<helm-c-etags-map>\
  2026. \\[helm-etags-help]:Help,\
  2027. \\<helm-map>\
  2028. \\[helm-select-action]:Acts,\
  2029. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2030. \\[helm-select-3rd-action]:NthAct,\
  2031. \\[helm-send-bug-report-from-helm]:BugReport."
  2032. "String displayed in mode-line in `helm-c-etags-select'.")
  2033. (defvar helm-c-ucs-mode-line-string
  2034. "\\<helm-c-ucs-map>\
  2035. \\[helm-c-ucs-help]:Help, \
  2036. \\<helm-map>\
  2037. \\[helm-select-action]:Acts,\
  2038. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2039. \\[helm-select-3rd-action]:NthAct."
  2040. "String displayed in mode-line in `helm-ucs'.")
  2041. (defvar helm-bookmark-mode-line-string
  2042. '("Bookmark(s)"
  2043. "\\<helm-c-bookmark-map>\
  2044. \\[helm-c-bookmark-help]:Help, \
  2045. \\<helm-map>\
  2046. \\[helm-select-action]:Acts,\
  2047. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2048. \\[helm-select-3rd-action]:NthAct,\
  2049. \\[helm-send-bug-report-from-helm]:BugReport."
  2050. "String displayed in mode-line in `helm-c-source-buffers-list'"))
  2051. (defvar helm-occur-mode-line
  2052. "\\<helm-map>\
  2053. \\[helm-help]:Help,\
  2054. \\<helm-occur-map>\
  2055. \\[helm-occur-run-query-replace-regexp]:Query replace regexp,\
  2056. \\<helm-map>\
  2057. \\[helm-select-action]:Acts,\
  2058. \\[helm-exit-minibuffer]/\\[helm-select-2nd-action-or-end-of-line]/\
  2059. \\[helm-select-3rd-action]:NthAct,\
  2060. \\[helm-send-bug-report-from-helm]:BugReport.")
  2061. ;;; Utilities Functions
  2062. ;;
  2063. ;;
  2064. (defun helm-ff-find-printers ()
  2065. "Return a list of available printers on Unix systems."
  2066. (when (executable-find "lpstat")
  2067. (let ((printer-list (with-temp-buffer
  2068. (call-process "lpstat" nil t nil "-a")
  2069. (split-string (buffer-string) "\n"))))
  2070. (loop for p in printer-list
  2071. for printer = (car (split-string p))
  2072. when printer
  2073. collect printer))))
  2074. ;; Shut up byte compiler in emacs24*.
  2075. (defun helm-c-switch-to-buffer (buffer-or-name)
  2076. "Same as `switch-to-buffer' whithout warnings at compile time."
  2077. (with-no-warnings
  2078. (switch-to-buffer buffer-or-name)))
  2079. (defun* helm-c-position (item seq &key (test 'eq) all)
  2080. "A simple and faster replacement of CL `position'.
  2081. Return position of first occurence of ITEM found in SEQ.
  2082. Argument SEQ can be a string, in this case ITEM have to be a char.
  2083. Argument ALL, if non--nil specify to return a list of positions of
  2084. all ITEM found in SEQ."
  2085. (let ((key (if (stringp seq) 'across 'in)))
  2086. (eval
  2087. `(loop for c ,key seq
  2088. for index from 0
  2089. when (funcall test c item)
  2090. if all collect index into ls
  2091. else return index
  2092. finally return ls))))
  2093. (defun helm-c-get-pid-from-process-name (process-name)
  2094. "Get pid from running process PROCESS-NAME."
  2095. (loop with process-list = (list-system-processes)
  2096. for pid in process-list
  2097. for process = (assoc-default 'comm (process-attributes pid))
  2098. when (and process (string-match process-name process))
  2099. return pid))
  2100. (defun* helm-current-buffer-narrowed-p (&optional
  2101. (buffer helm-current-buffer))
  2102. "Check if BUFFER is narrowed.
  2103. Default is `helm-current-buffer'."
  2104. (with-current-buffer buffer
  2105. (let ((beg (point-min))
  2106. (end (point-max))
  2107. (total (buffer-size)))
  2108. (or (/= beg 1) (/= end (1+ total))))))
  2109. (defun helm-region-active-p ()
  2110. (and transient-mark-mode mark-active (/= (mark) (point))))
  2111. (defun helm-goto-char (loc)
  2112. "Go to char, revealing if necessary."
  2113. (goto-char loc)
  2114. (when (or (eq major-mode 'org-mode)
  2115. (and (boundp 'outline-minor-mode)
  2116. outline-minor-mode))
  2117. (require 'org) ; On some old Emacs versions org may not be loaded.
  2118. (org-reveal)))
  2119. (defun helm-goto-line (lineno &optional noanim)
  2120. "Goto LINENO opening only outline headline if needed.
  2121. Animation is used unless NOANIM is non--nil."
  2122. (goto-char (point-min))
  2123. (helm-goto-char (point-at-bol lineno))
  2124. (unless noanim
  2125. (helm-match-line-color-current-line)
  2126. (sit-for 0.3)
  2127. (helm-match-line-cleanup)))
  2128. (defun helm-show-this-source-only ()
  2129. "Show all candidates of this source."
  2130. (interactive)
  2131. (let (helm-candidate-number-limit)
  2132. (helm-set-source-filter
  2133. (list (assoc-default 'name (helm-get-current-source))))))
  2134. ;;;###autoload
  2135. (defun helm-test-sources ()
  2136. "List all helm sources for test.
  2137. The output is sexps which are evaluated by \\[eval-last-sexp]."
  2138. (interactive)
  2139. (with-output-to-temp-buffer "*Helm Test Sources*"
  2140. (mapc (lambda (s) (princ (format ";; (helm '%s)\n" s)))
  2141. (apropos-internal "^helm-c-source" #'boundp))
  2142. (pop-to-buffer standard-output)))
  2143. (defun helm-displaying-source-names ()
  2144. "Display sources name."
  2145. (with-current-buffer helm-buffer
  2146. (goto-char (point-min))
  2147. (loop with pos
  2148. while (setq pos (next-single-property-change (point) 'helm-header))
  2149. do (goto-char pos)
  2150. collect (buffer-substring-no-properties (point-at-bol)(point-at-eol))
  2151. do (forward-line 1))))
  2152. (defun helm-c-match-on-file-name (candidate)
  2153. "Return non-nil if `helm-pattern' match basename of filename CANDIDATE."
  2154. (string-match helm-pattern (file-name-nondirectory candidate)))
  2155. (defun helm-c-match-on-directory-name (candidate)
  2156. "Return non-nil if `helm-pattern' match directory part of CANDIDATE."
  2157. (helm-aif (file-name-directory candidate)
  2158. (string-match helm-pattern it)))
  2159. (defun helm-c-match-on-basename (candidate)
  2160. "Return non-nil if `helm-pattern' match basename of filename CANDIDATE."
  2161. (string-match helm-pattern (helm-c-basename candidate)))
  2162. (defun helm-c-string-match (candidate)
  2163. "Return non-nil if `helm-pattern' match CANDIDATE.
  2164. The match is done with `string-match'."
  2165. (string-match helm-pattern candidate))
  2166. (defun helm-c-skip-entries (list regexp)
  2167. "Remove entries which matches REGEXP from LIST."
  2168. (remove-if (lambda (x) (and (stringp x) (string-match regexp x)))
  2169. list))
  2170. (defun helm-c-shadow-entries (list regexp)
  2171. "Display elements of LIST matching REGEXP with the `file-name-shadow' face."
  2172. (mapcar (lambda (file)
  2173. ;; Add shadow face property to boring files.
  2174. (let ((face (if (facep 'file-name-shadow)
  2175. 'file-name-shadow
  2176. ;; fall back to default on XEmacs
  2177. 'default)))
  2178. (if (string-match regexp file)
  2179. (setq file (propertize file 'face face))))
  2180. file)
  2181. list))
  2182. (defsubst helm-c-stringify (str-or-sym)
  2183. "Get string of STR-OR-SYM."
  2184. (if (stringp str-or-sym)
  2185. str-or-sym
  2186. (symbol-name str-or-sym)))
  2187. (defsubst helm-c-symbolify (str-or-sym)
  2188. "Get symbol of STR-OR-SYM."
  2189. (if (symbolp str-or-sym)
  2190. str-or-sym
  2191. (intern str-or-sym)))
  2192. (defun helm-c-describe-function (func)
  2193. "FUNC is symbol or string."
  2194. (describe-function (helm-c-symbolify func)))
  2195. (defun helm-c-describe-variable (var)
  2196. "VAR is symbol or string."
  2197. (describe-variable (helm-c-symbolify var)))
  2198. (defun helm-c-find-function (func)
  2199. "FUNC is symbol or string."
  2200. (find-function (helm-c-symbolify func)))
  2201. (defun helm-c-find-variable (var)
  2202. "VAR is symbol or string."
  2203. (find-variable (helm-c-symbolify var)))
  2204. (defun helm-c-kill-new (candidate &optional replace)
  2205. "CANDIDATE is symbol or string.
  2206. See `kill-new' for argument REPLACE."
  2207. (kill-new (helm-c-stringify candidate) replace))
  2208. (defun* helm-fast-remove-dups (seq &key (test 'eq))
  2209. "Remove duplicates elements in list SEQ.
  2210. This is same as `remove-duplicates' but with memoisation.
  2211. It is much faster, especially in large lists.
  2212. A test function can be provided with TEST argument key.
  2213. Default is `eq'."
  2214. (loop with cont = (make-hash-table :test test)
  2215. for elm in seq
  2216. unless (gethash elm cont)
  2217. do (puthash elm elm cont)
  2218. finally return
  2219. (loop for i being the hash-values in cont collect i)))
  2220. (defadvice eval-defun (after helm-source-hack activate)
  2221. "Allow immediate execution of helm source when evaling it.
  2222. See `helm-c-enable-eval-defun-hack'."
  2223. (when helm-c-enable-eval-defun-hack
  2224. (let ((varsym (save-excursion
  2225. (beginning-of-defun)
  2226. (forward-char 1)
  2227. (when (memq (read (current-buffer)) '(defvar setq))
  2228. (read (current-buffer))))))
  2229. (when (string-match "^helm-c-source-" (symbol-name varsym))
  2230. (helm varsym)))))
  2231. ;; (progn (ad-disable-advice 'eval-defun 'after 'helm-source-hack) (ad-update 'eval-defun))
  2232. ;; Move this function from helm.el and redefine here
  2233. ;; to avoid an unneeded defadvice.
  2234. (defun helm-quit-and-find-file ()
  2235. "Drop into `helm-find-files' from `helm'.
  2236. If current selection is a buffer or a file, `helm-find-files'
  2237. from its directory."
  2238. (interactive)
  2239. (helm-run-after-quit
  2240. (lambda (f)
  2241. (if (file-exists-p f)
  2242. (helm-find-files-1 (file-name-directory f)
  2243. (if helm-ff-transformer-show-only-basename
  2244. (helm-c-basename f) f))
  2245. (helm-find-files-1 f)))
  2246. (helm-aif (get-buffer (helm-get-selection))
  2247. (or (buffer-file-name it)
  2248. (car (rassoc it dired-buffers))
  2249. (and (with-current-buffer it
  2250. (eq major-mode 'org-agenda-mode))
  2251. org-directory
  2252. (expand-file-name org-directory))
  2253. default-directory)
  2254. (let ((sel (helm-get-selection)))
  2255. (cond ((or (file-remote-p sel)
  2256. (file-exists-p sel))
  2257. (expand-file-name sel))
  2258. ((string-match ffap-url-regexp sel)
  2259. sel)
  2260. (t default-directory))))))
  2261. (defmacro* helm-c-walk-directory (directory &key path (directories t) match)
  2262. "Walk through DIRECTORY tree.
  2263. PATH can be one of basename, relative, or full.
  2264. DIRECTORIES when non--nil (default) return also directories names, otherwise
  2265. skip directories names.
  2266. MATCH match only filenames matching regexp MATCH."
  2267. `(let (result
  2268. (fn (case ,path
  2269. (basename 'file-name-nondirectory)
  2270. (relative 'file-relative-name)
  2271. (full 'identity)
  2272. (t 'file-name-nondirectory))))
  2273. (labels ((ls-R (dir)
  2274. (loop with ls = (directory-files dir t directory-files-no-dot-files-regexp)
  2275. for f in ls
  2276. if (file-directory-p f)
  2277. do (progn (when ,directories
  2278. (push (funcall fn f) result))
  2279. ;; Don't recurse in directory symlink.
  2280. (unless (file-symlink-p f)
  2281. (ls-R f)))
  2282. else do
  2283. (unless (and ,match (not (string-match ,match (file-name-nondirectory f))))
  2284. (push (funcall fn f) result)))))
  2285. (ls-R ,directory)
  2286. (nreverse result))))
  2287. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Helm Applications ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2288. ;;; Helm regexp.
  2289. ;;
  2290. ;;
  2291. (defvar helm-build-regexp-history nil)
  2292. (defun helm-c-query-replace-regexp (candidate)
  2293. "Query replace regexp from `helm-regexp'.
  2294. With a prefix arg replace only matches surrounded by word boundaries,
  2295. i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
  2296. (let ((regexp (funcall (helm-attr 'regexp))))
  2297. (apply 'query-replace-regexp
  2298. (helm-c-query-replace-args regexp))))
  2299. (defun helm-c-kill-regexp-as-sexp (candidate)
  2300. "Kill regexp in a format usable in lisp code."
  2301. (helm-c-regexp-kill-new
  2302. (prin1-to-string (funcall (helm-attr 'regexp)))))
  2303. (defun helm-c-kill-regexp (candidate)
  2304. "Kill regexp as it is in `helm-pattern'."
  2305. (helm-c-regexp-kill-new (funcall (helm-attr 'regexp))))
  2306. (defun helm-c-query-replace-args (regexp)
  2307. "create arguments of `query-replace-regexp' action in `helm-regexp'."
  2308. (let ((region-only (helm-region-active-p)))
  2309. (list
  2310. regexp
  2311. (query-replace-read-to regexp
  2312. (format "Query replace %sregexp %s"
  2313. (if helm-current-prefix-arg "word " "")
  2314. (if region-only "in region " ""))
  2315. t)
  2316. helm-current-prefix-arg
  2317. (when region-only (region-beginning))
  2318. (when region-only (region-end)))))
  2319. (defvar helm-c-source-regexp
  2320. '((name . "Regexp Builder")
  2321. (init . (lambda ()
  2322. (helm-candidate-buffer helm-current-buffer)))
  2323. (candidates-in-buffer)
  2324. (get-line . helm-c-regexp-get-line)
  2325. (persistent-action . helm-c-regexp-persistent-action)
  2326. (persistent-help . "Show this line")
  2327. (multiline)
  2328. (delayed)
  2329. (requires-pattern . 2)
  2330. (mode-line . "Press TAB to select action.")
  2331. (regexp . (lambda () helm-input))
  2332. (action . (("Kill Regexp as sexp" . helm-c-kill-regexp-as-sexp)
  2333. ("Query Replace Regexp (C-u Not inside word.)"
  2334. . helm-c-query-replace-regexp)
  2335. ("Kill Regexp" . helm-c-kill-regexp)))))
  2336. (defun helm-c-regexp-get-line (s e)
  2337. (propertize
  2338. (apply 'concat
  2339. ;; Line contents
  2340. (format "%5d: %s" (line-number-at-pos (1- s)) (buffer-substring s e))
  2341. ;; subexps
  2342. (loop for i from 0 to (1- (/ (length (match-data)) 2))
  2343. collect (format "\n %s'%s'"
  2344. (if (zerop i) "Group 0: " (format "Group %d: " i))
  2345. (match-string i))))
  2346. ;; match beginning
  2347. ;; KLUDGE: point of helm-candidate-buffer is +1 than that of helm-current-buffer.
  2348. ;; It is implementation problem of candidates-in-buffer.
  2349. 'helm-realvalue
  2350. (1- s)))
  2351. (defun helm-c-regexp-persistent-action (pt)
  2352. (helm-goto-char pt)
  2353. (helm-persistent-highlight-point))
  2354. (defun helm-c-regexp-kill-new (input)
  2355. (kill-new input)
  2356. (message "Killed: %s" input))
  2357. (defun helm-quote-whitespace (candidate)
  2358. "Quote whitespace, if some, in string CANDIDATE."
  2359. (replace-regexp-in-string " " "\\\\ " candidate))
  2360. ;;; Toggle all marks.
  2361. ;;
  2362. ;;
  2363. ;;;###autoload
  2364. (defun helm-mark-all ()
  2365. "Mark all visible unmarked candidates in current source."
  2366. (interactive)
  2367. (with-helm-window
  2368. (save-excursion
  2369. (goto-char (helm-get-previous-header-pos))
  2370. (helm-next-line)
  2371. (let* ((next-head (helm-get-next-header-pos))
  2372. (end (and next-head
  2373. (save-excursion
  2374. (goto-char next-head)
  2375. (forward-line -1)
  2376. (point))))
  2377. (maxpoint (or end (point-max))))
  2378. (while (< (point) maxpoint)
  2379. (helm-mark-current-line)
  2380. (let* ((prefix (get-text-property (point-at-bol) 'display))
  2381. (cand (helm-get-selection))
  2382. (bn (and (helm-file-completion-source-p)
  2383. (helm-c-basename cand)))
  2384. (src (assoc-default 'name (helm-get-current-source))))
  2385. (when (and (not (helm-this-visible-mark))
  2386. (not (or (string= prefix "[?]")
  2387. (string= prefix "[@]"))))
  2388. ;; Don't mark possibles directories ending with . or ..
  2389. ;; autosave files/links and non--existent file.
  2390. (unless
  2391. (and (or (helm-file-completion-source-p)
  2392. (equal src "Files from Current Directory"))
  2393. (or (string-match "^\\.#.*\\|^#.*#$\\|\\.$" bn)
  2394. ;; We need to test here when not using a transformer
  2395. ;; that tag prefix (i.e on tramp)
  2396. (not (file-exists-p cand))))
  2397. (helm-make-visible-mark))))
  2398. (forward-line 1) (end-of-line))))
  2399. (helm-mark-current-line)
  2400. (message "%s candidates marked" (length helm-marked-candidates))))
  2401. ;;;###autoload
  2402. (defun helm-unmark-all ()
  2403. "Unmark all candidates in all sources of current helm session."
  2404. (interactive)
  2405. (with-helm-window
  2406. (let ((len (length helm-marked-candidates)))
  2407. (save-excursion
  2408. (helm-clear-visible-mark))
  2409. (setq helm-marked-candidates nil)
  2410. (helm-mark-current-line)
  2411. (message "%s candidates unmarked" len))))
  2412. ;;;###autoload
  2413. (defun helm-toggle-all-marks ()
  2414. "Toggle all marks.
  2415. Mark all visible candidates of current source or unmark all candidates
  2416. visible or invisible in all sources of current helm session"
  2417. (interactive)
  2418. (let ((marked (helm-marked-candidates)))
  2419. (if (and (>= (length marked) 1)
  2420. (with-helm-window helm-visible-mark-overlays))
  2421. (helm-unmark-all)
  2422. (helm-mark-all))))
  2423. ;;; Buffers
  2424. ;;
  2425. ;;
  2426. (defun helm-c-buffer-list ()
  2427. "Return a list of buffer names.
  2428. The first buffer in the list will be the last recently used
  2429. buffer that is not the current buffer unless
  2430. `helm-allow-skipping-current-buffer' is nil."
  2431. (let ((buffers (mapcar 'buffer-name (buffer-list))))
  2432. (if helm-allow-skipping-current-buffer
  2433. (progn
  2434. (setq buffers (remove (buffer-name helm-current-buffer) buffers))
  2435. (append (cdr buffers) (list (car buffers))))
  2436. buffers)))
  2437. (defvar helm-c-source-buffers
  2438. '((name . "Buffers")
  2439. (candidates . helm-c-buffer-list)
  2440. (type . buffer)))
  2441. (defvar helm-c-source-buffer-not-found
  2442. `((name . "Create buffer")
  2443. (dummy)
  2444. (filtered-candidate-transformer (lambda (cands source)
  2445. (list helm-pattern)))
  2446. (keymap . ,helm-map)
  2447. (action . (lambda (candidate)
  2448. (helm-c-switch-to-buffer (get-buffer-create candidate))))))
  2449. ;;; Buffers-list (was buffers+)
  2450. ;;
  2451. ;;
  2452. (defun helm-c-highlight-buffers (buffers)
  2453. "Transformer function to highlight BUFFERS list.
  2454. Should be called after others transformers i.e (boring buffers)."
  2455. (loop with buflist = (if helm-allow-skipping-current-buffer
  2456. buffers
  2457. (cons (pop (cdr buffers)) buffers))
  2458. for i in buflist
  2459. for buf = (get-buffer i)
  2460. for bfname = (buffer-file-name buf)
  2461. collect
  2462. (cond (;; A dired buffer.
  2463. (rassoc buf dired-buffers)
  2464. (propertize i 'face 'helm-ff-directory
  2465. 'help-echo (car (rassoc buf dired-buffers))))
  2466. ;; A buffer file modified somewhere outside of emacs.
  2467. ((and bfname (not (file-remote-p bfname))
  2468. (file-exists-p bfname)
  2469. (not (verify-visited-file-modtime buf)))
  2470. (propertize i 'face 'helm-buffer-saved-out
  2471. 'help-echo bfname))
  2472. ;; A new buffer file not already saved on disk.
  2473. ((and bfname (not (file-remote-p bfname))
  2474. (not (verify-visited-file-modtime buf)))
  2475. (propertize i 'face 'helm-buffer-not-saved
  2476. 'help-echo bfname))
  2477. ;; A Remote buffer file modified and not saved on disk.
  2478. ((and bfname (file-remote-p bfname) (buffer-modified-p buf))
  2479. (let ((prefix (propertize
  2480. " " 'display
  2481. (propertize "@ " 'face 'helm-ff-prefix))))
  2482. (cons (concat prefix (propertize i 'face 'helm-ff-symlink
  2483. 'help-echo bfname)) i)))
  2484. ;; A buffer file modified and not saved on disk.
  2485. ((and bfname (buffer-modified-p buf))
  2486. (propertize i 'face 'helm-ff-symlink
  2487. 'help-echo bfname))
  2488. ;; A remote buffer file not modified and saved on disk.
  2489. ((and bfname (file-remote-p bfname))
  2490. (let ((prefix (propertize
  2491. " " 'display
  2492. (propertize "@ " 'face 'helm-ff-prefix))))
  2493. (cons (concat prefix (propertize i 'face 'font-lock-type-face
  2494. 'help-echo bfname)) i)))
  2495. ;; A buffer file not modified and saved on disk.
  2496. (bfname
  2497. (propertize i 'face 'font-lock-type-face
  2498. 'help-echo bfname))
  2499. ;; Any non--file buffer.
  2500. (t (propertize i 'face 'italic)))))
  2501. (defvar helm-c-source-buffers-list
  2502. `((name . "Buffers")
  2503. (candidates . helm-c-buffer-list)
  2504. (type . buffer)
  2505. (match helm-c-buffer-match-major-mode)
  2506. (candidate-transformer helm-c-skip-boring-buffers
  2507. helm-c-highlight-buffers)
  2508. (persistent-action . helm-c-buffers-list-persistent-action)
  2509. (keymap . ,helm-c-buffer-map)
  2510. (volatile)
  2511. (mode-line . helm-buffer-mode-line-string)
  2512. (persistent-help . "Show this buffer / C-u \\[helm-execute-persistent-action]: Kill this buffer")))
  2513. (defvaralias 'helm-c-source-buffers+ 'helm-c-source-buffers-list)
  2514. (defun helm-c-buffer-match-major-mode (candidate)
  2515. "Match maybe buffer by major-mode.
  2516. If you give a major-mode or partial major-mode,
  2517. it will list all buffers of this major-mode and/or buffers with name
  2518. matching this major-mode.
  2519. If you add a space after major-mode and then a space,
  2520. it will match all buffers of the major-mode
  2521. before space matching pattern after space.
  2522. If you give a pattern which doesn't match a major-mode, it will search buffer
  2523. with name matching pattern."
  2524. (let* ((cand (replace-regexp-in-string "^\\s-\\{1\\}" "" candidate))
  2525. (buf (get-buffer cand)))
  2526. (when buf
  2527. (with-current-buffer buf
  2528. (let ((mjm (symbol-name major-mode))
  2529. (split (split-string helm-pattern)))
  2530. (cond ((string-match "\\s-$" helm-pattern)
  2531. (string-match (car split) mjm))
  2532. ((string-match "\\s-" helm-pattern)
  2533. (and (string-match (car split) mjm)
  2534. (string-match (cadr split) cand)))
  2535. (t (or (string-match helm-pattern mjm)
  2536. (string-match helm-pattern cand)))))))))
  2537. (defun helm-c-buffer-query-replace-1 (&optional regexp-flag)
  2538. "Query replace in marked buffers.
  2539. If REGEXP-FLAG is given use `query-replace-regexp'."
  2540. (let ((fn (if regexp-flag 'query-replace-regexp 'query-replace))
  2541. (prompt (if regexp-flag "Query replace regexp" "Query replace"))
  2542. (bufs (helm-marked-candidates)))
  2543. (loop
  2544. with replace = (query-replace-read-from prompt regexp-flag)
  2545. with tostring = (unless (consp replace)
  2546. (query-replace-read-to
  2547. replace prompt regexp-flag))
  2548. for buf in bufs
  2549. do
  2550. (save-window-excursion
  2551. (helm-c-switch-to-buffer buf)
  2552. (save-excursion
  2553. (let ((case-fold-search t))
  2554. (goto-char (point-min))
  2555. (if (consp replace)
  2556. (apply fn (list (car replace) (cdr replace)))
  2557. (apply fn (list replace tostring)))))))))
  2558. (defun helm-c-buffer-query-replace-regexp (candidate)
  2559. (helm-c-buffer-query-replace-1 'regexp))
  2560. (defun helm-c-buffer-query-replace (candidate)
  2561. (helm-c-buffer-query-replace-1))
  2562. (defun helm-buffer-toggle-diff (candidate)
  2563. "Toggle diff buffer CANDIDATE with it's file."
  2564. (if (get-buffer-window "*Diff*")
  2565. (kill-buffer "*Diff*")
  2566. (diff-buffer-with-file (get-buffer candidate))))
  2567. ;;;###autoload
  2568. (defun helm-buffer-diff-persistent ()
  2569. "Toggle diff buffer without quitting helm."
  2570. (interactive)
  2571. (helm-attrset 'diff-action 'helm-buffer-toggle-diff)
  2572. (helm-execute-persistent-action 'diff-action))
  2573. (defun helm-buffer-revert-and-update (candidate)
  2574. (let ((marked (helm-marked-candidates)))
  2575. (loop for buf in marked do (helm-revert-buffer buf))
  2576. (helm-force-update candidate)))
  2577. ;;;###autoload
  2578. (defun helm-buffer-revert-persistent ()
  2579. "Revert buffer without quitting helm."
  2580. (interactive)
  2581. (helm-attrset 'revert-action 'helm-buffer-revert-and-update)
  2582. (helm-execute-persistent-action 'revert-action 'onewindow))
  2583. (defun helm-buffer-save-and-update (candidate)
  2584. (let ((marked (helm-marked-candidates))
  2585. (enable-recursive-minibuffers t))
  2586. (loop for buf in marked do
  2587. (with-current-buffer (get-buffer buf)
  2588. (save-buffer)))
  2589. (helm-force-update candidate)))
  2590. ;;;###autoload
  2591. (defun helm-buffer-save-persistent ()
  2592. "Save buffer without quitting helm."
  2593. (interactive)
  2594. (helm-attrset 'save-action 'helm-buffer-save-and-update)
  2595. (helm-execute-persistent-action 'save-action 'onewindow))
  2596. ;;;###autoload
  2597. (defun helm-buffer-run-kill-buffers ()
  2598. "Run kill buffer action from `helm-c-source-buffers-list'."
  2599. (interactive)
  2600. (helm-c-quit-and-execute-action 'helm-kill-marked-buffers))
  2601. ;;;###autoload
  2602. (defun helm-buffer-run-grep ()
  2603. "Run Grep action from `helm-c-source-buffers-list'."
  2604. (interactive)
  2605. (helm-c-quit-and-execute-action 'helm-c-grep-buffers))
  2606. ;;;###autoload
  2607. (defun helm-buffer-run-zgrep ()
  2608. "Run Grep action from `helm-c-source-buffers-list'."
  2609. (interactive)
  2610. (helm-c-quit-and-execute-action 'helm-c-zgrep-buffers))
  2611. ;;;###autoload
  2612. (defun helm-buffer-run-query-replace-regexp ()
  2613. "Run Query replace regexp action from `helm-c-source-buffers-list'."
  2614. (interactive)
  2615. (helm-c-quit-and-execute-action 'helm-c-buffer-query-replace-regexp))
  2616. ;;;###autoload
  2617. (defun helm-buffer-run-query-replace ()
  2618. "Run Query replace action from `helm-c-source-buffers-list'."
  2619. (interactive)
  2620. (helm-c-quit-and-execute-action 'helm-c-buffer-query-replace))
  2621. ;;;###autoload
  2622. (defun helm-buffer-switch-other-window ()
  2623. "Run switch to other window action from `helm-c-source-buffers-list'."
  2624. (interactive)
  2625. (helm-c-quit-and-execute-action 'switch-to-buffer-other-window))
  2626. ;;;###autoload
  2627. (defun helm-buffer-switch-other-frame ()
  2628. "Run switch to other frame action from `helm-c-source-buffers-list'."
  2629. (interactive)
  2630. (helm-c-quit-and-execute-action 'switch-to-buffer-other-frame))
  2631. ;;;###autoload
  2632. (defun helm-buffer-switch-to-elscreen ()
  2633. "Run switch to elscreen action from `helm-c-source-buffers-list'."
  2634. (interactive)
  2635. (helm-c-quit-and-execute-action 'helm-find-buffer-on-elscreen))
  2636. ;;;###autoload
  2637. (defun helm-buffer-run-ediff ()
  2638. "Run ediff action from `helm-c-source-buffers-list'."
  2639. (interactive)
  2640. (helm-c-quit-and-execute-action 'helm-ediff-marked-buffers))
  2641. (defun helm-buffer-run-ediff-merge ()
  2642. "Run ediff action from `helm-c-source-buffers-list'."
  2643. (interactive)
  2644. (helm-c-quit-and-execute-action 'helm-ediff-marked-buffers-merge))
  2645. (defun helm-c-buffers-persistent-kill (buffer)
  2646. "Persistent action to kill buffer."
  2647. (with-current-buffer (get-buffer buffer)
  2648. (if (and (buffer-modified-p)
  2649. (buffer-file-name (current-buffer)))
  2650. (progn
  2651. (save-buffer)
  2652. (kill-buffer buffer))
  2653. (kill-buffer buffer)))
  2654. (helm-delete-current-selection))
  2655. (defun helm-c-buffers-list-persistent-action (candidate)
  2656. (if current-prefix-arg
  2657. (helm-c-buffers-persistent-kill candidate)
  2658. (helm-c-switch-to-buffer candidate)))
  2659. ;;;; <File>
  2660. ;;
  2661. ;;
  2662. ;;; File name history
  2663. (defvar helm-c-source-file-name-history
  2664. '((name . "File Name History")
  2665. (candidates . file-name-history)
  2666. (match helm-c-match-on-basename)
  2667. (type . file)))
  2668. ;;; Files in current dir
  2669. ;;
  2670. ;;
  2671. (defvar helm-c-source-files-in-current-dir
  2672. '((name . "Files from Current Directory")
  2673. (candidates . (lambda ()
  2674. (with-helm-current-buffer
  2675. (directory-files (helm-c-current-directory)))))
  2676. ;; volatile is not needed, I think.
  2677. (type . file)))
  2678. (defun helm-c-highlight-files (files)
  2679. (loop for i in files
  2680. if (file-directory-p i)
  2681. collect (propertize (file-name-nondirectory i)
  2682. 'face 'helm-ff-directory
  2683. 'help-echo (expand-file-name i))
  2684. else
  2685. collect (propertize (file-name-nondirectory i)
  2686. 'face 'helm-ff-file
  2687. 'help-echo (expand-file-name i))))
  2688. (defvar helm-c-source-files-in-current-dir+
  2689. `((name . "Files from Current Directory")
  2690. (candidates . (lambda ()
  2691. (with-helm-current-buffer
  2692. (directory-files (helm-c-current-directory) t))))
  2693. (keymap . ,helm-generic-files-map)
  2694. (help-message . helm-generic-file-help-message)
  2695. (mode-line . helm-generic-file-mode-line-string)
  2696. (candidate-transformer helm-c-highlight-files)
  2697. ;; volatile is not needed, I think.
  2698. (type . file)))
  2699. ;;; Helm-find-files - The helm files browser.
  2700. ;;
  2701. ;;
  2702. ;; Internal.
  2703. (defvar helm-c-find-files-doc-header " (`C-l': Go to precedent level)"
  2704. "*The doc that is inserted in the Name header of a find-files or dired source.")
  2705. (defvar helm-ff-auto-update-flag nil
  2706. "Internal, flag to turn on/off auto-update in `helm-find-files'.
  2707. Don't set it directly, use instead `helm-ff-auto-update-initial-value'.")
  2708. (defvar helm-ff-last-expanded nil
  2709. "Store last expanded directory or file.")
  2710. (defvar helm-ff-default-directory nil)
  2711. (defvar helm-ff-history nil)
  2712. (defvar helm-ff-cand-to-mark nil)
  2713. (defvar helm-ff-url-regexp
  2714. "\\`\\(news\\(post\\)?:\\|nntp:\\|mailto:\\|file:\\|\\(ftp\\|https?\\|telnet\\|gopher\\|www\\|wais\\):/?/?\\).*"
  2715. "Same as `ffap-url-regexp' but match earlier possible url.")
  2716. (defvar helm-c-source-find-files
  2717. `((name . "Find Files")
  2718. (header-name . (lambda (name)
  2719. (concat name helm-c-find-files-doc-header)))
  2720. ;; It is needed for filenames with capital letters
  2721. (disable-shortcuts)
  2722. (init . (lambda ()
  2723. (setq helm-ff-auto-update-flag
  2724. helm-ff-auto-update-initial-value)))
  2725. (candidates . helm-find-files-get-candidates)
  2726. (filtered-candidate-transformer helm-c-find-files-transformer)
  2727. (persistent-action . helm-find-files-persistent-action)
  2728. (persistent-help . "Hit1 Expand Candidate, Hit2 or (C-u) Find file")
  2729. (mode-line . helm-ff-mode-line-string)
  2730. (volatile)
  2731. (candidate-number-limit . 9999)
  2732. (action-transformer . helm-find-files-action-transformer)
  2733. (action
  2734. . ,(delq
  2735. nil
  2736. `(("Find File" . helm-c-find-file-or-marked)
  2737. ("Find file in Dired" . helm-c-point-file-in-dired)
  2738. ,(and (locate-library "elscreen")
  2739. '("Find file in Elscreen" . helm-elscreen-find-file))
  2740. ,(and (locate-library "popwin")
  2741. '("Find file in popup window" . popwin:find-file))
  2742. ("Checksum File" . helm-ff-checksum)
  2743. ("Complete at point `M-tab'"
  2744. . helm-c-insert-file-name-completion-at-point)
  2745. ("Open file externally `C-c C-x, C-u to choose'"
  2746. . helm-c-open-file-externally)
  2747. ("Grep File(s) `M-g s, C-u Recurse'" . helm-find-files-grep)
  2748. ("Zgrep File(s) `M-g z, C-u Recurse'" . helm-ff-zgrep)
  2749. ("Switch to Eshell `M-e'" . helm-ff-switch-to-eshell)
  2750. ("Etags `M-., C-u tap, C-u C-u reload tag file'" . helm-ff-etags-select)
  2751. ("Eshell command on file(s) `M-!, C-u run on all marked at once.'"
  2752. . helm-find-files-eshell-command-on-file)
  2753. ("Find file as root" . helm-find-file-as-root)
  2754. ("Find file in hex dump" . hexl-find-file)
  2755. ("Ediff File `C-='" . helm-find-files-ediff-files)
  2756. ("Ediff Merge File `C-c ='" . helm-find-files-ediff-merge-files)
  2757. ("Delete File(s) `M-D'" . helm-delete-marked-files)
  2758. ("Copy file(s) `M-C, C-u to follow'" . helm-find-files-copy)
  2759. ("Copy file(s) Async" . helm-ff-copy-async)
  2760. ("Rename file(s) `M-R, C-u to follow'" . helm-find-files-rename)
  2761. ("Serial rename files" . helm-ff-serial-rename)
  2762. ("Serial rename by symlinking files" . helm-ff-serial-rename-by-symlink)
  2763. ("Serial rename by copying files" . helm-ff-serial-rename-by-copying)
  2764. ("Symlink files(s) `M-S, C-u to follow'" . helm-find-files-symlink)
  2765. ("Relsymlink file(s) `C-u to follow'" . helm-find-files-relsymlink)
  2766. ("Hardlink file(s) `M-H, C-u to follow'" . helm-find-files-hardlink)
  2767. ("Find file other window `C-o'" . find-file-other-window)
  2768. ("Switch to history `M-p'" . helm-find-files-switch-to-hist)
  2769. ("Find file other frame `C-c C-o'" . find-file-other-frame)
  2770. ("Print File `C-c p, C-u to refresh'" . helm-ff-print)
  2771. ("Locate `C-x C-f, C-u to specify locate db'" . helm-ff-locate))))))
  2772. (defun helm-find-files-set-prompt-for-action (action files)
  2773. "Set prompt for action ACTION for FILES."
  2774. (let ((len (length files)))
  2775. (format "%s *%s File(s)\n%s to: "
  2776. action len
  2777. (mapconcat (lambda (f)
  2778. (format "- %s\n" f)) files ""))))
  2779. (defun helm-dwim-target-directory ()
  2780. "Return value of `default-directory' of buffer in other window.
  2781. If there is only one window return the value ot `default-directory'
  2782. for current buffer."
  2783. (with-helm-current-buffer
  2784. (let ((num-windows (length (window-list))))
  2785. (if (> num-windows 1)
  2786. (save-selected-window
  2787. (other-window 1)
  2788. default-directory)
  2789. (car helm-ff-history)))))
  2790. (defun helm-find-files-do-action (action)
  2791. "Generic function for creating action from `helm-c-source-find-files'.
  2792. ACTION must be an action supported by `helm-dired-action'."
  2793. (let* ((ifiles (mapcar 'expand-file-name ; Allow modify '/foo/.' -> '/foo'
  2794. (helm-marked-candidates)))
  2795. (cand (helm-get-selection)) ; Target
  2796. (prompt (helm-find-files-set-prompt-for-action
  2797. (capitalize (symbol-name action)) ifiles))
  2798. (parg helm-current-prefix-arg)
  2799. (dest (helm-c-read-file-name
  2800. prompt
  2801. :preselect (if helm-ff-transformer-show-only-basename
  2802. (helm-c-basename cand) cand)
  2803. :initial-input (helm-dwim-target-directory)
  2804. :history (helm-find-files-history :comp-read nil))))
  2805. (helm-dired-action
  2806. dest :files ifiles :action action :follow parg)))
  2807. (defun helm-find-files-copy (candidate)
  2808. "Copy files from `helm-find-files'."
  2809. (helm-find-files-do-action 'copy))
  2810. (defun helm-find-files-rename (candidate)
  2811. "Rename files from `helm-find-files'."
  2812. (helm-find-files-do-action 'rename))
  2813. (defun helm-find-files-symlink (candidate)
  2814. "Symlink files from `helm-find-files'."
  2815. (helm-find-files-do-action 'symlink))
  2816. (defun helm-find-files-relsymlink (candidate)
  2817. "Relsymlink files from `helm-find-files'."
  2818. (helm-find-files-do-action 'relsymlink))
  2819. (defun helm-find-files-hardlink (candidate)
  2820. "Hardlink files from `helm-find-files'."
  2821. (helm-find-files-do-action 'hardlink))
  2822. (defun helm-find-files-byte-compile (candidate)
  2823. "Byte compile elisp files from `helm-find-files'."
  2824. (let ((files (helm-marked-candidates))
  2825. (parg helm-current-prefix-arg))
  2826. (loop for fname in files
  2827. do (byte-compile-file fname parg))))
  2828. (defun helm-find-files-load-files (candidate)
  2829. "Load elisp files from `helm-find-files'."
  2830. (let ((files (helm-marked-candidates)))
  2831. (loop for fname in files
  2832. do (load fname))))
  2833. (defun helm-find-files-ediff-files-1 (candidate &optional merge)
  2834. "Generic function to ediff/merge files in `helm-find-files'."
  2835. (let ((bname (helm-c-basename candidate))
  2836. (prompt (if merge "Ediff Merge `%s' With File: "
  2837. "Ediff `%s' With File: "))
  2838. (fun (if merge 'ediff-merge-files 'ediff-files)))
  2839. (funcall fun
  2840. candidate
  2841. (condition-case quit
  2842. (helm-c-read-file-name
  2843. (format prompt bname))
  2844. (quit ;; Hit C-g ask user to fallback to locate.
  2845. (if (y-or-n-p "Search file for ediff with locate? ")
  2846. (helm-c-locate-read-file-name
  2847. (format prompt bname)
  2848. ;; Check if -b option is available.
  2849. (if (and (eq system-type 'windows-nt)
  2850. (string-match "^es" helm-c-locate-command))
  2851. bname
  2852. (concat bname " -b")))
  2853. (error "Error: Ediff Operation aborted")))))))
  2854. (defun helm-find-files-ediff-files (candidate)
  2855. (helm-find-files-ediff-files-1 candidate))
  2856. (defun helm-find-files-ediff-merge-files (candidate)
  2857. (helm-find-files-ediff-files-1 candidate 'merge))
  2858. (defun helm-find-files-grep (candidate)
  2859. "Default action to grep files from `helm-find-files'."
  2860. (helm-do-grep-1 (helm-marked-candidates)
  2861. helm-current-prefix-arg))
  2862. (defun helm-ff-zgrep (candidate)
  2863. "Default action to zgrep files from `helm-find-files'."
  2864. (let ((prefarg helm-current-prefix-arg)
  2865. (ls (helm-marked-candidates)))
  2866. (helm-ff-zgrep-1 ls prefarg)))
  2867. (defun helm-ff-pdfgrep (candidate)
  2868. "Default action to pdfgrep files from `helm-find-files'."
  2869. (let ((cands (loop for file in (helm-marked-candidates)
  2870. if (or (string= (file-name-extension file) "pdf")
  2871. (string= (file-name-extension file) "PDF"))
  2872. collect file))
  2873. (helm-c-pdfgrep-default-function 'helm-c-pdfgrep-init))
  2874. (when cands
  2875. (helm-do-pdfgrep-1 cands))))
  2876. (defun helm-ff-etags-select (candidate)
  2877. "Default action to jump to etags from `helm-find-files'."
  2878. (when (get-buffer helm-action-buffer)
  2879. (kill-buffer helm-action-buffer))
  2880. (let ((default-directory helm-ff-default-directory))
  2881. (helm-c-etags-select helm-current-prefix-arg)))
  2882. (defun helm-find-files-switch-to-hist (candidate)
  2883. "Switch to helm-find-files history."
  2884. (helm-find-files t))
  2885. ;;; Asynchronous copy of files.
  2886. ;;
  2887. ;;
  2888. (defun helm-c-copy-files-async-1 (flist dest)
  2889. "Copy a list of Files FLIST to DEST asynchronously.
  2890. It use another emacs process to do the job.
  2891. Communication with background emacs is done with temp file
  2892. `helm-c-copy-files-async-log-file'."
  2893. (start-file-process "emacs-batch" nil helm-c-copy-async-prefered-emacs
  2894. "-Q" "--batch" "--eval"
  2895. (format "(progn
  2896. (require 'dired) (require 'cl)
  2897. (let ((dired-recursive-copies 'always)
  2898. failures success
  2899. (ovw-count 0)
  2900. (cpf-count 0))
  2901. (dolist (f '%S)
  2902. (condition-case err
  2903. (let ((file-exists (file-exists-p
  2904. (expand-file-name
  2905. (file-name-nondirectory (directory-file-name f))
  2906. (file-name-directory
  2907. (file-name-as-directory \"%s\"))))))
  2908. (dired-copy-file f \"%s\" t)
  2909. (if file-exists
  2910. (progn (push (cons \"Overwriting\" f) success)
  2911. (incf ovw-count))
  2912. (push (cons \"Copying\" f) success)
  2913. (incf cpf-count)))
  2914. (file-error
  2915. (push (dired-make-relative
  2916. (expand-file-name
  2917. (file-name-nondirectory (directory-file-name f))
  2918. (file-name-directory \"%s\")))
  2919. failures))))
  2920. (with-current-buffer (find-file-noselect \"%s\")
  2921. (erase-buffer)
  2922. (when failures
  2923. (dolist (fail (reverse failures))
  2924. (insert (concat \"Failed to copy \" fail \"\n\"))))
  2925. (when success
  2926. (loop for (a . s) in (reverse success) do
  2927. (insert (concat a \" \" s \" to %s done\n\"))))
  2928. (and (/= cpf-count 0) (insert (concat (int-to-string cpf-count) \" File(s) Copied\n\")))
  2929. (and (/= ovw-count 0) (insert (concat (int-to-string ovw-count) \" File(s) Overwrited\n\")))
  2930. (and failures (insert (concat (int-to-string (length failures)) \" File(s) Failed to copy\n\")))
  2931. (save-buffer))))"
  2932. flist dest dest dest helm-c-copy-files-async-log-file dest)))
  2933. (defun helm-c-copy-async-with-log (flist dest)
  2934. "Copy file list FLIST to DEST showing log.
  2935. Log is send to `helm-c-copy-files-async-log-file'.
  2936. Copying is done asynchronously with `helm-c-copy-files-async-1'."
  2937. (declare (special auto-revert-interval))
  2938. (pop-to-buffer (find-file-noselect helm-c-copy-files-async-log-file))
  2939. (set (make-local-variable 'auto-revert-interval) 1)
  2940. (erase-buffer)
  2941. (insert "Wait copying files...\n")
  2942. (sit-for 0.5) (save-buffer)
  2943. (goto-char (point-max))
  2944. (auto-revert-mode 1)
  2945. (helm-c-copy-files-async-1 flist dest))
  2946. (defun helm-ff-copy-async (candidate)
  2947. "Helm find files action to copy files async.
  2948. Copying is done asynchronously with `helm-c-copy-files-async-1'."
  2949. (let* ((flist (helm-marked-candidates))
  2950. (dest (helm-c-read-file-name
  2951. (helm-find-files-set-prompt-for-action
  2952. "Copy Async" flist)
  2953. :preselect candidate
  2954. :initial-input (car helm-ff-history)
  2955. :history (helm-find-files-history :comp-read nil))))
  2956. (helm-c-copy-async-with-log flist dest)))
  2957. (defvar eshell-command-aliases-list nil)
  2958. (defvar helm-eshell-command-on-file-input-history nil)
  2959. (defun helm-find-files-eshell-command-on-file-1 (candidate &optional map)
  2960. "Run `eshell-command' on CANDIDATE or marked candidates.
  2961. This is done possibly with an eshell alias, if no alias found, you can type in
  2962. an eshell command.
  2963. Basename of CANDIDATE can be a wild-card.
  2964. e.g you can do \"eshell-command command *.el\"
  2965. Where \"*.el\" is the CANDIDATE.
  2966. It is possible to do eshell-command command <CANDIDATE> <some more args>
  2967. like this: \"command %s some more args\".
  2968. If MAP is given run `eshell-command' on all marked files at once,
  2969. Otherwise, run `eshell-command' on each marked files.
  2970. In other terms, with a prefix arg do on the three marked files
  2971. \"foo\" \"bar\" \"baz\":
  2972. \"eshell-command command foo bar baz\"
  2973. otherwise do
  2974. \"eshell-command command foo\"
  2975. \"eshell-command command bar\"
  2976. \"eshell-command command baz\"
  2977. Note:
  2978. If `eshell' or `eshell-command' have not been run once,
  2979. or if you have no eshell aliases `eshell-command-aliases-list'
  2980. will not be loaded first time you use this."
  2981. (when (or eshell-command-aliases-list
  2982. (y-or-n-p "Eshell is not loaded, run eshell-command without alias anyway? "))
  2983. (and eshell-command-aliases-list (eshell-read-aliases-list))
  2984. (let* ((cand-list (helm-marked-candidates))
  2985. (default-directory (or helm-ff-default-directory
  2986. ;; If candidate is an url *-ff-default-directory is nil
  2987. ;; so keep value of default-directory.
  2988. default-directory))
  2989. (command (helm-comp-read
  2990. "Command: "
  2991. (loop for (a . c) in eshell-command-aliases-list
  2992. when (string-match "\\(\\$1\\|\\$\\*\\)$" (car c))
  2993. collect (propertize a 'help-echo (car c)) into ls
  2994. finally return (sort ls 'string<))
  2995. :buffer "*esh command on file*"
  2996. :name "Eshell command"
  2997. :keymap helm-esh-on-file-map
  2998. :mode-line
  2999. '("Eshell alias"
  3000. "C-c ?: Help, \\[universal-argument]: Insert output at point")
  3001. :input-history
  3002. 'helm-eshell-command-on-file-input-history))
  3003. (alias-value (car (assoc-default command eshell-command-aliases-list))))
  3004. (when (and (= (length cand-list) 1)
  3005. (string-match "[*]" (helm-c-basename (car cand-list))))
  3006. (setq cand-list (file-expand-wildcards (car cand-list) t)))
  3007. ;; Be sure output don't go in current buffer
  3008. ;; but allow sending output to current buffer
  3009. ;; if a prefix arg have been passed during the
  3010. ;; `helm-comp-read' call.
  3011. (setq current-prefix-arg helm-current-prefix-arg)
  3012. ;; MAP have been set before calling `helm-comp-read'
  3013. ;; by `helm-current-prefix-arg'.
  3014. (if (and (or map ; prefix-arg
  3015. (and alias-value
  3016. ;; If command is an alias be sure it accept
  3017. ;; more than one arg i.e $*.
  3018. (string-match "\\$\\*$" alias-value)))
  3019. (> (length cand-list) 1))
  3020. ;; Run eshell-command with ALL marked files as arguments.
  3021. (let ((mapfiles (mapconcat 'shell-quote-argument cand-list " ")))
  3022. (if (string-match "'%s'\\|\"%s\"\\|%s" command)
  3023. (eshell-command (format command mapfiles)) ; See [1]
  3024. (eshell-command (format "%s %s" command mapfiles))))
  3025. ;; Run eshell-command on EACH marked files.
  3026. (loop for i in cand-list
  3027. for bn = (helm-c-basename i)
  3028. for files = (format "'%s'" i)
  3029. for com = (if (string-match "'%s'\\|\"%s\"\\|%s" command)
  3030. ;; [1] This allow to enter other args AFTER filename
  3031. ;; i.e <command %s some_more_args>
  3032. (format command files)
  3033. (format "%s %s" command files))
  3034. do (eshell-command com))))))
  3035. (defun helm-find-files-eshell-command-on-file (candidate)
  3036. "Run `eshell-command' on CANDIDATE or marked candidates.
  3037. See `helm-find-files-eshell-command-on-file-1' for more info."
  3038. (helm-find-files-eshell-command-on-file-1
  3039. candidate helm-current-prefix-arg))
  3040. (defun helm-ff-switch-to-eshell (candidate)
  3041. "Switch to eshell and cd to `helm-ff-default-directory'."
  3042. (flet ((cd-eshell ()
  3043. (goto-char (point-max))
  3044. (insert
  3045. (format "cd '%s'" helm-ff-default-directory))
  3046. (eshell-send-input)))
  3047. (if (get-buffer "*eshell*")
  3048. (progn
  3049. (helm-c-switch-to-buffer "*eshell*")
  3050. (cd-eshell))
  3051. (call-interactively 'eshell)
  3052. (cd-eshell))))
  3053. (defun helm-ff-serial-rename-action (method)
  3054. "Rename all marked files to `helm-ff-default-directory' with METHOD.
  3055. See `helm-ff-serial-rename-1'."
  3056. (let* ((cands (helm-marked-candidates))
  3057. (def-name (car cands))
  3058. (name (read-string "NewName: "
  3059. (replace-regexp-in-string
  3060. "[0-9]+$" ""
  3061. (helm-c-basename
  3062. def-name
  3063. (file-name-extension def-name)))))
  3064. (start (read-number "StartAtNumber: "))
  3065. (extension (read-string "Extension: "
  3066. (file-name-extension (car cands))))
  3067. (dir (expand-file-name
  3068. (helm-c-read-file-name
  3069. "Serial Rename to directory: "
  3070. :initial-input
  3071. (expand-file-name helm-ff-default-directory)
  3072. :test 'file-directory-p
  3073. :must-match t)))
  3074. (res (loop for f in cands
  3075. for bn = (helm-c-basename f)
  3076. for count from start
  3077. concat (format "%s <-> %s%s.%s\n"
  3078. bn name count extension))))
  3079. (if (y-or-n-p
  3080. (format "Result:\n %sRename like this to <%s> ? " res dir))
  3081. (progn
  3082. (helm-ff-serial-rename-1
  3083. dir cands name start extension :method method)
  3084. (message nil)
  3085. (helm-find-files-1 dir))
  3086. (message "Operation aborted"))))
  3087. (defun helm-ff-member-directory-p (file directory)
  3088. (let ((dir-file (expand-file-name
  3089. (file-name-as-directory (file-name-directory file))))
  3090. (cur-dir (expand-file-name (file-name-as-directory directory))))
  3091. (string= dir-file cur-dir)))
  3092. (defun* helm-ff-serial-rename-1
  3093. (directory collection new-name start-at-num extension &key (method 'rename))
  3094. "rename files in COLLECTION to DIRECTORY with the prefix name NEW-NAME.
  3095. Rename start at number START-AT-NUM - ex: prefixname-01.jpg.
  3096. EXTENSION is the file extension to use, in empty prompt,
  3097. reuse the original extension of file.
  3098. METHOD can be one of rename, copy or symlink.
  3099. Files will be renamed if they are files of current directory, otherwise they
  3100. will be treated with METHOD.
  3101. Default METHOD is rename."
  3102. ;; Maybe remove directories selected by error in collection.
  3103. (setq collection (remove-if 'file-directory-p collection))
  3104. (flet ((symlink-file (file dest)
  3105. (let ((flist (list file)))
  3106. (helm-dired-action
  3107. dest :action 'symlink :files flist))))
  3108. (let* ((tmp-dir (file-name-as-directory
  3109. (concat (file-name-as-directory directory)
  3110. (symbol-name (gensym "tmp")))))
  3111. (fn (case method
  3112. (copy 'copy-file)
  3113. (symlink 'symlink-file)
  3114. (rename 'rename-file)
  3115. (t (error "Error: Unknow method %s" method)))))
  3116. (make-directory tmp-dir)
  3117. (unwind-protect
  3118. (progn
  3119. ;; Rename all files to tmp-dir with new-name.
  3120. ;; If files are not from start directory, use method
  3121. ;; to move files to tmp-dir.
  3122. (loop for i in collection
  3123. for count from start-at-num
  3124. for fnum = (if (< count 10) "0%s" "%s")
  3125. for nname = (concat tmp-dir new-name (format fnum count)
  3126. (if (not (string= extension ""))
  3127. (format ".%s" (replace-regexp-in-string
  3128. "[.]" "" extension))
  3129. (file-name-extension i 'dot)))
  3130. do (if (helm-ff-member-directory-p i directory)
  3131. (rename-file i nname)
  3132. (funcall fn i nname)))
  3133. ;; Now move all from tmp-dir to destination.
  3134. (loop with dirlist = (directory-files
  3135. tmp-dir t directory-files-no-dot-files-regexp)
  3136. for f in dirlist do
  3137. (if (file-symlink-p f)
  3138. (symlink-file (file-truename f)
  3139. (concat (file-name-as-directory directory)
  3140. (helm-c-basename f)))
  3141. (rename-file f directory))))
  3142. (delete-directory tmp-dir t)))))
  3143. (defun helm-ff-serial-rename (candidate)
  3144. "Serial rename all marked files to `helm-ff-default-directory'.
  3145. Rename only file of current directory, and symlink files coming from
  3146. other directories.
  3147. See `helm-ff-serial-rename-1'."
  3148. (helm-ff-serial-rename-action 'rename))
  3149. (defun helm-ff-serial-rename-by-symlink (candidate)
  3150. "Serial rename all marked files to `helm-ff-default-directory'.
  3151. Rename only file of current directory, and symlink files coming from
  3152. other directories.
  3153. See `helm-ff-serial-rename-1'."
  3154. (helm-ff-serial-rename-action 'symlink))
  3155. (defun helm-ff-serial-rename-by-copying (candidate)
  3156. "Serial rename all marked files to `helm-ff-default-directory'.
  3157. Rename only file of current directory, and copy files coming from
  3158. other directories.
  3159. See `helm-ff-serial-rename-1'."
  3160. (helm-ff-serial-rename-action 'copy))
  3161. (defun helm-c-quit-and-execute-action (action)
  3162. "Quit current helm session and execute ACTION."
  3163. (setq helm-saved-action action)
  3164. (helm-exit-minibuffer))
  3165. (defun helm-ff-toggle-auto-update (candidate)
  3166. (setq helm-ff-auto-update-flag (not helm-ff-auto-update-flag))
  3167. (message "[Auto expansion %s]"
  3168. (if helm-ff-auto-update-flag "enabled" "disabled")))
  3169. ;;;###autoload
  3170. (defun helm-ff-run-toggle-auto-update ()
  3171. (interactive)
  3172. (when (helm-file-completion-source-p)
  3173. (helm-attrset 'toggle-auto-update 'helm-ff-toggle-auto-update)
  3174. (helm-execute-persistent-action 'toggle-auto-update)))
  3175. ;;;###autoload
  3176. (defun helm-ff-run-switch-to-history ()
  3177. "Run Switch to history action from `helm-c-source-find-files'."
  3178. (interactive)
  3179. (when (helm-file-completion-source-p)
  3180. (helm-c-quit-and-execute-action 'helm-find-files-switch-to-hist)))
  3181. ;;;###autoload
  3182. (defun helm-ff-run-grep ()
  3183. "Run Grep action from `helm-c-source-find-files'."
  3184. (interactive)
  3185. (when (helm-file-completion-source-p)
  3186. (helm-c-quit-and-execute-action 'helm-find-files-grep)))
  3187. ;;;###autoload
  3188. (defun helm-ff-run-pdfgrep ()
  3189. "Run Pdfgrep action from `helm-c-source-find-files'."
  3190. (interactive)
  3191. (when (helm-file-completion-source-p)
  3192. (helm-c-quit-and-execute-action 'helm-ff-pdfgrep)))
  3193. ;;;###autoload
  3194. (defun helm-ff-run-zgrep ()
  3195. "Run Grep action from `helm-c-source-find-files'."
  3196. (interactive)
  3197. (when (helm-file-completion-source-p)
  3198. (helm-c-quit-and-execute-action 'helm-ff-zgrep)))
  3199. ;;;###autoload
  3200. (defun helm-ff-run-copy-file ()
  3201. "Run Copy file action from `helm-c-source-find-files'."
  3202. (interactive)
  3203. (when (helm-file-completion-source-p)
  3204. (helm-c-quit-and-execute-action 'helm-find-files-copy)))
  3205. ;;;###autoload
  3206. (defun helm-ff-run-rename-file ()
  3207. "Run Rename file action from `helm-c-source-find-files'."
  3208. (interactive)
  3209. (when (helm-file-completion-source-p)
  3210. (helm-c-quit-and-execute-action 'helm-find-files-rename)))
  3211. ;;;###autoload
  3212. (defun helm-ff-run-byte-compile-file ()
  3213. "Run Byte compile file action from `helm-c-source-find-files'."
  3214. (interactive)
  3215. (when (helm-file-completion-source-p)
  3216. (helm-c-quit-and-execute-action 'helm-find-files-byte-compile)))
  3217. ;;;###autoload
  3218. (defun helm-ff-run-load-file ()
  3219. "Run Load file action from `helm-c-source-find-files'."
  3220. (interactive)
  3221. (when (helm-file-completion-source-p)
  3222. (helm-c-quit-and-execute-action 'helm-find-files-load-files)))
  3223. ;;;###autoload
  3224. (defun helm-ff-run-eshell-command-on-file ()
  3225. "Run eshell command on file action from `helm-c-source-find-files'."
  3226. (interactive)
  3227. (when (helm-file-completion-source-p)
  3228. (helm-c-quit-and-execute-action
  3229. 'helm-find-files-eshell-command-on-file)))
  3230. ;;;###autoload
  3231. (defun helm-ff-run-ediff-file ()
  3232. "Run Ediff file action from `helm-c-source-find-files'."
  3233. (interactive)
  3234. (when (helm-file-completion-source-p)
  3235. (helm-c-quit-and-execute-action 'helm-find-files-ediff-files)))
  3236. ;;;###autoload
  3237. (defun helm-ff-run-ediff-merge-file ()
  3238. "Run Ediff merge file action from `helm-c-source-find-files'."
  3239. (interactive)
  3240. (when (helm-file-completion-source-p)
  3241. (helm-c-quit-and-execute-action
  3242. 'helm-find-files-ediff-merge-files)))
  3243. ;;;###autoload
  3244. (defun helm-ff-run-symlink-file ()
  3245. "Run Symlink file action from `helm-c-source-find-files'."
  3246. (interactive)
  3247. (when (helm-file-completion-source-p)
  3248. (helm-c-quit-and-execute-action 'helm-find-files-symlink)))
  3249. ;;;###autoload
  3250. (defun helm-ff-run-hardlink-file ()
  3251. "Run Hardlink file action from `helm-c-source-find-files'."
  3252. (interactive)
  3253. (when (helm-file-completion-source-p)
  3254. (helm-c-quit-and-execute-action 'helm-find-files-hardlink)))
  3255. ;;;###autoload
  3256. (defun helm-ff-run-delete-file ()
  3257. "Run Delete file action from `helm-c-source-find-files'."
  3258. (interactive)
  3259. (when (helm-file-completion-source-p)
  3260. (helm-c-quit-and-execute-action 'helm-delete-marked-files)))
  3261. ;;;###autoload
  3262. (defun helm-ff-run-complete-fn-at-point ()
  3263. "Run complete file name action from `helm-c-source-find-files'."
  3264. (interactive)
  3265. (when (helm-file-completion-source-p)
  3266. (helm-c-quit-and-execute-action
  3267. 'helm-c-insert-file-name-completion-at-point)))
  3268. ;;;###autoload
  3269. (defun helm-ff-run-switch-to-eshell ()
  3270. "Run switch to eshell action from `helm-c-source-find-files'."
  3271. (interactive)
  3272. (when (helm-file-completion-source-p)
  3273. (helm-c-quit-and-execute-action 'helm-ff-switch-to-eshell)))
  3274. ;;;###autoload
  3275. (defun helm-ff-run-switch-other-window ()
  3276. "Run switch to other window action from `helm-c-source-find-files'."
  3277. (interactive)
  3278. (when (helm-file-completion-source-p)
  3279. (helm-c-quit-and-execute-action 'find-file-other-window)))
  3280. ;;;###autoload
  3281. (defun helm-ff-run-switch-other-frame ()
  3282. "Run switch to other frame action from `helm-c-source-find-files'."
  3283. (interactive)
  3284. (when (helm-file-completion-source-p)
  3285. (helm-c-quit-and-execute-action 'find-file-other-frame)))
  3286. ;;;###autoload
  3287. (defun helm-ff-run-open-file-externally ()
  3288. "Run open file externally command action from `helm-c-source-find-files'."
  3289. (interactive)
  3290. (when (helm-file-completion-source-p)
  3291. (helm-c-quit-and-execute-action 'helm-c-open-file-externally)))
  3292. (defun helm-ff-locate (candidate)
  3293. "Locate action function for `helm-find-files'."
  3294. (let ((input (concat (helm-c-basename
  3295. (expand-file-name
  3296. candidate
  3297. helm-ff-default-directory))
  3298. ;; The locate '-b' option doesn't exists
  3299. ;; in everything.
  3300. (unless (and (eq system-type 'windows-nt)
  3301. (string-match "^es" helm-c-locate-command))
  3302. " -b")))
  3303. (helm-mp-highlight-delay 0.7))
  3304. (helm-locate-1 helm-current-prefix-arg input 'from-ff)))
  3305. ;;;###autoload
  3306. (defun helm-ff-run-locate ()
  3307. "Run locate action from `helm-c-source-find-files'."
  3308. (interactive)
  3309. (when (helm-file-completion-source-p)
  3310. (helm-c-quit-and-execute-action 'helm-ff-locate)))
  3311. ;;;###autoload
  3312. (defun helm-ff-run-gnus-attach-files ()
  3313. "Run gnus attach files command action from `helm-c-source-find-files'."
  3314. (interactive)
  3315. (when (helm-file-completion-source-p)
  3316. (helm-c-quit-and-execute-action 'helm-ff-gnus-attach-files)))
  3317. ;;;###autoload
  3318. (defun helm-ff-run-etags ()
  3319. "Run Etags command action from `helm-c-source-find-files'."
  3320. (interactive)
  3321. (when (helm-file-completion-source-p)
  3322. (helm-c-quit-and-execute-action 'helm-ff-etags-select)))
  3323. (defun helm-ff-print (candidate)
  3324. "Print marked files.
  3325. You have to set in order
  3326. variables `lpr-command',`lpr-switches' and/or `printer-name'.
  3327. e.g:
  3328. \(setq lpr-command \"gtklp\"\)
  3329. \(setq lpr-switches '(\"-P\")\)
  3330. \(setq printer-name \"Epson-Stylus-Photo-R265\"\)
  3331. Same as `dired-do-print' but for helm."
  3332. (when (or helm-current-prefix-arg
  3333. (not helm-ff-printer-list))
  3334. (setq helm-ff-printer-list
  3335. (helm-ff-find-printers)))
  3336. (let* ((file-list (helm-marked-candidates))
  3337. (len (length file-list))
  3338. (printer-name (if helm-ff-printer-list
  3339. (helm-comp-read
  3340. "Printer: " helm-ff-printer-list)
  3341. printer-name))
  3342. (command (read-string
  3343. (format "Print *%s File(s):\n%s with: "
  3344. len
  3345. (mapconcat
  3346. (lambda (f) (format "- %s\n" f))
  3347. file-list ""))
  3348. (when (and lpr-command
  3349. (or lpr-switches
  3350. printer-name))
  3351. (mapconcat 'identity
  3352. (cons lpr-command
  3353. (append (if (stringp lpr-switches)
  3354. (list lpr-switches)
  3355. lpr-switches)
  3356. (list printer-name)))
  3357. " "))))
  3358. (file-args (mapconcat #'(lambda (x)
  3359. (format "'%s'" x))
  3360. file-list " "))
  3361. (cmd-line (concat command " " file-args)))
  3362. (if command
  3363. (start-process-shell-command "helm-print" nil cmd-line)
  3364. (error "Error: Please verify your printer settings in Emacs."))))
  3365. ;;;###autoload
  3366. (defun helm-ff-run-print-file ()
  3367. "Run Print file action from `helm-c-source-find-files'."
  3368. (interactive)
  3369. (when (helm-file-completion-source-p)
  3370. (helm-c-quit-and-execute-action 'helm-ff-print)))
  3371. (defun helm-ff-checksum (file)
  3372. "Calculate the checksum of FILE.
  3373. Provide completion on different algorithms to use on Emacs24.
  3374. On Emacs23 only 'sha1' is available.
  3375. The checksum is copied to kill-ring."
  3376. (let ((algo-list (and (fboundp 'secure-hash)
  3377. '(md5 sha1 sha224 sha256 sha384 sha512))))
  3378. (kill-new
  3379. (if algo-list
  3380. (secure-hash (intern
  3381. (helm-comp-read
  3382. "Algorithm: " algo-list))
  3383. file)
  3384. (sha1 (with-temp-buffer
  3385. (insert-file-contents file)
  3386. (buffer-string)))))
  3387. (message "Checksum copied to kill-ring.")))
  3388. (defun helm-ff-toggle-basename (candidate)
  3389. (setq helm-ff-transformer-show-only-basename
  3390. (not helm-ff-transformer-show-only-basename))
  3391. (let ((target (if helm-ff-transformer-show-only-basename
  3392. (helm-c-basename candidate) candidate)))
  3393. (helm-force-update target)))
  3394. (defun helm-ff-run-toggle-basename ()
  3395. (interactive)
  3396. (when (helm-file-completion-source-p)
  3397. (helm-attrset 'toggle-basename 'helm-ff-toggle-basename)
  3398. (helm-execute-persistent-action 'toggle-basename)))
  3399. (defun* helm-reduce-file-name (fname level &key unix-close expand)
  3400. "Reduce FNAME by LEVEL from end or beginning depending LEVEL value.
  3401. If LEVEL is positive reduce from end else from beginning.
  3402. If UNIX-CLOSE is non--nil close filename with /.
  3403. If EXPAND is non--nil expand-file-name."
  3404. (let* ((exp-fname (expand-file-name fname))
  3405. (fname-list (split-string (if (or (string= fname "~/") expand)
  3406. exp-fname fname) "/" t))
  3407. (len (length fname-list))
  3408. (pop-list (if (< level 0)
  3409. (subseq fname-list (* level -1))
  3410. (subseq fname-list 0 (- len level))))
  3411. (result (mapconcat 'identity pop-list "/"))
  3412. (empty (string= result "")))
  3413. (when unix-close (setq result (concat result "/")))
  3414. (if (string-match "^~" result)
  3415. (if (string= result "~/") "~/" result)
  3416. (if (< level 0)
  3417. (if empty "../" (concat "../" result))
  3418. (cond ((eq system-type 'windows-nt)
  3419. (if empty (expand-file-name "/") ; Expand to "/" or "c:/".
  3420. result))
  3421. (empty "/")
  3422. (t
  3423. (concat "/" result)))))))
  3424. ;; Internal
  3425. (defvar helm-file-completion-sources
  3426. '("Find Files" "Read File Name"
  3427. "Read File Name History" "Copy Files"
  3428. "Rename Files" "Symlink Files"
  3429. "Hardlink Files" "Write File" "Insert File")
  3430. "Sources that use the *find-files mechanism can be added here.
  3431. Sources generated by `helm-mode' don't need to be added here, it will
  3432. be done automatically.
  3433. You should not modify this yourself unless you know what you do.")
  3434. (defun helm-file-completion-source-p ()
  3435. "Return non--nil if current source is a file completion source.
  3436. A source is a file completion source if it is
  3437. one of `helm-file-completion-sources'.
  3438. Return nil if helm is not running."
  3439. (let ((cur-source (cdr (assoc 'name (helm-get-current-source)))))
  3440. (loop for i in helm-file-completion-sources
  3441. thereis (string= cur-source i))))
  3442. (defun helm-find-files-down-one-level (arg)
  3443. "Go down one level like unix command `cd ..'.
  3444. If prefix numeric arg is given go ARG level down."
  3445. (interactive "p")
  3446. (when (and (helm-file-completion-source-p)
  3447. (not (helm-ff-invalid-tramp-name-p)))
  3448. (with-helm-window
  3449. (setq helm-follow-mode nil))
  3450. ;; When going to precedent level we want to be at the line
  3451. ;; corresponding to actual directory, so store this info
  3452. ;; in `helm-ff-last-expanded'.
  3453. (if (and (not (file-directory-p helm-pattern))
  3454. (file-exists-p helm-pattern))
  3455. (setq helm-ff-last-expanded helm-pattern)
  3456. (setq helm-ff-last-expanded helm-ff-default-directory))
  3457. (let ((new-pattern (helm-reduce-file-name helm-pattern arg
  3458. :unix-close t :expand t)))
  3459. (helm-set-pattern new-pattern))))
  3460. (defun helm-ff-retrieve-last-expanded ()
  3461. "Move overlay to last visited directory `helm-ff-last-expanded'.
  3462. This happen after using `helm-find-files-down-one-level',
  3463. or hitting C-z on \"..\"."
  3464. (when (and helm-ff-last-expanded
  3465. (helm-file-completion-source-p))
  3466. (let ((presel (if helm-ff-transformer-show-only-basename
  3467. (helm-c-basename
  3468. (directory-file-name helm-ff-last-expanded))
  3469. (directory-file-name helm-ff-last-expanded))))
  3470. (with-helm-window
  3471. (when (re-search-forward
  3472. (concat "^" (regexp-quote presel) "$") nil t)
  3473. (forward-line 0)
  3474. (helm-mark-current-line)))
  3475. (setq helm-ff-last-expanded nil))))
  3476. (add-hook 'helm-after-update-hook 'helm-ff-retrieve-last-expanded)
  3477. ;; Auto-update - helm-find-files auto expansion of directories.
  3478. ;;
  3479. (defun helm-ff-update-when-only-one-matched ()
  3480. "Expand to directory when sole completion.
  3481. When only one candidate is remaining and it is a directory,
  3482. expand to this directory."
  3483. (when (and helm-ff-auto-update-flag
  3484. (helm-file-completion-source-p)
  3485. (not (helm-ff-invalid-tramp-name-p)))
  3486. (let* ((history-p (string= (assoc-default
  3487. 'name (helm-get-current-source))
  3488. "Read File Name History"))
  3489. (pat (if (string-match tramp-file-name-regexp
  3490. helm-pattern)
  3491. (helm-create-tramp-name helm-pattern)
  3492. helm-pattern))
  3493. (completed-p (string= (file-name-as-directory pat)
  3494. helm-ff-default-directory)))
  3495. (when (and (or
  3496. ;; Only one candidate remaining
  3497. ;; and at least 2 char in basename.
  3498. (and (<= (helm-approximate-candidate-number) 2)
  3499. (>= (length (helm-c-basename helm-pattern)) 2))
  3500. ;; Already completed.
  3501. completed-p)
  3502. (not history-p)) ; Don't try to auto complete in history.
  3503. (with-helm-window
  3504. (let ((cur-cand (prog2
  3505. (unless completed-p
  3506. ;; Only one non--existing candidate
  3507. ;; and one directory candidate, move to it.
  3508. (helm-next-line))
  3509. (helm-get-selection))))
  3510. (when (and (stringp cur-cand) (file-directory-p cur-cand))
  3511. (if (and (not (string-match "^.*[.]\\{1,2\\}$" cur-cand)) ; [1]
  3512. ;; Maybe we are here because completed-p is true
  3513. ;; but check this again to be sure. (Windows fix)
  3514. (<= (helm-approximate-candidate-number) 2)) ; [2]
  3515. ;; If after going to next line the candidate
  3516. ;; is not one of "." or ".." [1]
  3517. ;; and only one candidate is remaining [2],
  3518. ;; assume candidate is a new directory to expand, and do it.
  3519. (helm-set-pattern (file-name-as-directory cur-cand))
  3520. ;; The candidate is one of "." or ".."
  3521. ;; that mean we have entered the last letter of the directory name
  3522. ;; in prompt, so expansion is already done, just add the "/" at end
  3523. ;; of name unless helm-pattern ends with "."
  3524. ;; (i.e we are writing something starting with ".")
  3525. (unless (string-match "^.*[.]\\{1\\}$" helm-pattern)
  3526. (helm-set-pattern
  3527. ;; Need to expand-file-name to avoid e.g /ssh:host:./ in prompt.
  3528. (expand-file-name (file-name-as-directory helm-pattern)))))
  3529. (helm-check-minibuffer-input-1))))))))
  3530. (add-hook 'helm-after-update-hook 'helm-ff-update-when-only-one-matched)
  3531. ;; Allow expanding to home directory or root
  3532. ;; when entering respectively "~/" or "//" at end of pattern.
  3533. ;; e.g /home/thierry/labo/helm-config-qp/~/
  3534. ;; will expand to "~/"
  3535. ;; and /home/thierry/labo/helm-config-qp//
  3536. ;; will expand to "/"
  3537. (defun helm-ff-auto-expand-to-home-or-root ()
  3538. "Goto home, root or default directory when pattern ends with ~/, /, or ./.
  3539. This happen only in function using sources that are
  3540. `helm-file-completion-source-p' compliant."
  3541. (when (and (helm-file-completion-source-p)
  3542. (string-match ".*\\(/~/\\|/\\{2\\}\\|/[.]\\{1\\}/\\)$"
  3543. helm-pattern)
  3544. (not (string-match helm-ff-url-regexp helm-pattern)))
  3545. (let ((match (match-string 1 helm-pattern)))
  3546. (cond ((string= match "//")
  3547. ;; Expand to "/" or "c:/"
  3548. (setq helm-pattern (expand-file-name "/")))
  3549. ((string= match "/~/")
  3550. (if (eq system-type 'windows-nt)
  3551. (setq helm-pattern (file-name-as-directory (getenv "HOME")))
  3552. (setq helm-pattern "~/")))
  3553. ((string= match "/./")
  3554. (setq helm-pattern
  3555. (with-helm-current-buffer
  3556. (expand-file-name default-directory))))))
  3557. (setq helm-ff-default-directory helm-pattern)
  3558. ;; For some reasons, i must use here with-current-buffer => mini buffer
  3559. ;; and not `helm-set-pattern' that use with-selected-window => mini win.
  3560. (with-current-buffer (window-buffer (minibuffer-window))
  3561. (delete-minibuffer-contents)
  3562. (insert helm-pattern))))
  3563. (add-hook 'helm-after-update-hook 'helm-ff-auto-expand-to-home-or-root)
  3564. (defun helm-c-point-file-in-dired (file)
  3565. "Put point on filename FILE in dired buffer."
  3566. (dired (file-name-directory file))
  3567. (dired-goto-file file))
  3568. (defun helm-create-tramp-name (fname)
  3569. "Build filename for `helm-pattern' like /su:: or /sudo::."
  3570. (apply #'tramp-make-tramp-file-name
  3571. (loop with v = (tramp-dissect-file-name fname)
  3572. for i across v collect i)))
  3573. (defun* helm-ff-tramp-hostnames (&optional (pattern helm-pattern))
  3574. "Get a list of hosts for tramp method found in `helm-pattern'.
  3575. Argument PATTERN default to `helm-pattern', it is here only for debugging
  3576. purpose."
  3577. (when (string-match tramp-file-name-regexp pattern)
  3578. (let ((method (match-string 1 pattern))
  3579. (tn (match-string 0 pattern))
  3580. (all-methods (mapcar 'car tramp-methods)))
  3581. (helm-fast-remove-dups
  3582. (loop for (f . h) in (tramp-get-completion-function method)
  3583. append (loop for e in (funcall f (car h))
  3584. for host = (and (consp e) (cadr e))
  3585. when (and host (not (member host all-methods)))
  3586. collect (concat tn host)))
  3587. :test 'equal))))
  3588. (defun helm-ff-before-action-hook-fn ()
  3589. "Exit helm when user try to execute action on an invalid tramp fname."
  3590. (let ((cand (helm-get-selection)))
  3591. (when (and (helm-file-completion-source-p)
  3592. (helm-ff-invalid-tramp-name-p cand) ; Check candidate.
  3593. (helm-ff-invalid-tramp-name-p)) ; check helm-pattern.
  3594. (error "Error: Unknow file or directory `%s'" cand))))
  3595. (add-hook 'helm-before-action-hook 'helm-ff-before-action-hook-fn)
  3596. (defun* helm-ff-invalid-tramp-name-p (&optional (pattern helm-pattern))
  3597. "Return non--nil when PATTERN is an invalid tramp filename."
  3598. (string= (helm-ff-set-pattern pattern)
  3599. "Invalid tramp file name"))
  3600. (defun helm-ff-set-pattern (pattern)
  3601. "Handle tramp filenames in `helm-pattern'."
  3602. (let ((methods (mapcar 'car tramp-methods))
  3603. (reg "\\`/\\([^[/:]+\\|[^/]+]\\):.*:")
  3604. cur-method tramp-name)
  3605. (cond ((string= pattern "") "")
  3606. ((string-match ".*\\(~?/?[.]\\{1\\}/\\)$" pattern)
  3607. (with-helm-current-buffer
  3608. (expand-file-name default-directory)))
  3609. ((and (string-match ".*\\(~//\\|//\\)$" pattern)
  3610. (not (string-match helm-ff-url-regexp helm-pattern)))
  3611. (expand-file-name "/") ; Expand to "/" or "c:/"
  3612. )
  3613. ((string-match "^~\\|.*/~/$" pattern)
  3614. (let* ((home (expand-file-name (getenv "HOME"))))
  3615. (replace-match home nil t pattern)))
  3616. ;; Match "/method:maybe_hostname:"
  3617. ((and (string-match reg pattern)
  3618. (setq cur-method (match-string 1 pattern))
  3619. (member cur-method methods))
  3620. (setq tramp-name (helm-create-tramp-name
  3621. (match-string 0 pattern)))
  3622. (replace-match tramp-name nil t pattern))
  3623. ;; Match "/hostname:"
  3624. ((and (string-match tramp-file-name-regexp pattern)
  3625. (setq cur-method (match-string 1 pattern))
  3626. (and cur-method (not (member cur-method methods))))
  3627. (setq tramp-name (helm-create-tramp-name
  3628. (match-string 0 pattern)))
  3629. (replace-match tramp-name nil t pattern))
  3630. ;; Match "/method:" in this case don't try to connect.
  3631. ((and (not (string-match reg pattern))
  3632. (string-match tramp-file-name-regexp pattern)
  3633. (member (match-string 1 pattern) methods))
  3634. "Invalid tramp file name") ; Write in helm-buffer.
  3635. ;; PATTERN is a directory, end it with "/".
  3636. ;; This will make PATTERN not ending yet with "/"
  3637. ;; candidate for `helm-ff-default-directory',
  3638. ;; allowing `helm-ff-retrieve-last-expanded' to retrieve it
  3639. ;; when descending level.
  3640. ((file-directory-p pattern)
  3641. (file-name-as-directory pattern))
  3642. ;; Return PATTERN unchanged.
  3643. (t pattern))))
  3644. (defun helm-find-files-get-candidates (&optional require-match)
  3645. "Create candidate list for `helm-c-source-find-files'."
  3646. (let* ((path (helm-ff-set-pattern helm-pattern))
  3647. (path-name-dir (if (file-directory-p path)
  3648. (file-name-as-directory path)
  3649. (file-name-directory path)))
  3650. (tramp-verbose helm-tramp-verbose)) ; No tramp message when 0.
  3651. (set-text-properties 0 (length path) nil path)
  3652. ;; Don't set now `helm-pattern' if `path' == "Invalid tramp file name"
  3653. ;; like that the actual value (e.g /ssh:) is passed to
  3654. ;; `helm-ff-tramp-hostnames'.
  3655. (unless (string= path "Invalid tramp file name")
  3656. (setq helm-pattern (helm-ff-transform-fname-for-completion path)))
  3657. (setq helm-ff-default-directory
  3658. (if (string= helm-pattern "")
  3659. (expand-file-name "/") ; Expand to "/" or "c:/"
  3660. ;; If path is an url *default-directory have to be nil.
  3661. (unless (or (string-match helm-ff-url-regexp path)
  3662. (string-match ffap-url-regexp path))
  3663. path-name-dir)))
  3664. (cond ((string= path "Invalid tramp file name")
  3665. (or (helm-ff-tramp-hostnames) ; Hostnames completion.
  3666. (prog2
  3667. ;; `helm-pattern' have not been modified yet.
  3668. ;; Set it here to the value of `path' that should be now
  3669. ;; "Invalid tramp file name" and set the candidates list
  3670. ;; to ("Invalid tramp file name") to make `helm-pattern'
  3671. ;; match single candidate "Invalid tramp file name".
  3672. (setq helm-pattern path)
  3673. ;; "Invalid tramp file name" is now printed
  3674. ;; in `helm-buffer'.
  3675. (list path))))
  3676. ((or (file-regular-p path)
  3677. ;; `ffap-url-regexp' don't match until url is complete.
  3678. (string-match helm-ff-url-regexp path)
  3679. (and (not (file-exists-p path)) (string-match "/$" path))
  3680. (and ffap-url-regexp (string-match ffap-url-regexp path)))
  3681. (list path))
  3682. ((string= path "") (helm-ff-directory-files "/" t))
  3683. ((and (file-directory-p path) (not (file-readable-p path)))
  3684. (list (format "Opening directory: access denied, `%s'" path)))
  3685. ((file-directory-p path) (helm-ff-directory-files path t))
  3686. (t
  3687. (append (list path) ; No need to check for must-match.
  3688. (helm-ff-directory-files path-name-dir t))))))
  3689. (defun helm-ff-directory-files (directory &optional full)
  3690. "List contents of DIRECTORY.
  3691. Argument FULL mean absolute path.
  3692. It is same as `directory-files' but always returns the
  3693. dotted filename '.' and '..' on root directories on Windows
  3694. systems."
  3695. (setq directory (expand-file-name directory))
  3696. (let ((ls (directory-files directory full))
  3697. dot dot2 lsdir)
  3698. (if (or
  3699. ;; A windows volume.
  3700. (string-match "^[a-zA-Z]\\{1\\}:/$" directory)
  3701. ;; Empty directories on ftp hosts may have no dot dirs.
  3702. (and (file-remote-p directory)
  3703. (string-match "^/ftp:" directory)))
  3704. (progn (setq dot (concat directory "."))
  3705. (setq dot2 (concat directory ".."))
  3706. (setq lsdir (remove dot2 (remove dot ls)))
  3707. (append (list dot dot2) lsdir))
  3708. ls)))
  3709. (defun helm-ff-transform-fname-for-completion (fname)
  3710. "Return FNAME with it's basename modified as a regexp.
  3711. e.g foo => f.*o.*o .
  3712. If basename contain one or more space or FNAME is a valid directory name
  3713. return FNAME unchanged."
  3714. (let ((bn (helm-c-basename fname)))
  3715. (if (or (not helm-ff-smart-completion)
  3716. (string-match "\\s-" bn)
  3717. (string-match "/$" fname) ; Allow mkdir.
  3718. (file-directory-p fname)
  3719. (string-match helm-ff-url-regexp fname))
  3720. fname ; Fall back to match-plugin.
  3721. (setq bn (if (> (length bn) 2) ; Normal completion on first 2 char.
  3722. (mapconcat 'identity (split-string bn "" t) ".*") bn))
  3723. (concat (file-name-directory fname) bn))))
  3724. (defun helm-ff-save-history ()
  3725. "Store the last value of `helm-ff-default-directory' \
  3726. in `helm-ff-history'."
  3727. (when (and helm-ff-default-directory
  3728. (helm-file-completion-source-p))
  3729. (push helm-ff-default-directory helm-ff-history)))
  3730. (add-hook 'helm-cleanup-hook 'helm-ff-save-history)
  3731. (defun helm-ff-valid-symlink-p (file)
  3732. (file-exists-p (file-truename file)))
  3733. (defun helm-ff-properties (candidate)
  3734. "Show file properties of CANDIDATE in a tooltip or message."
  3735. (let ((type (helm-ff-attributes candidate :type t))
  3736. (dired-line (helm-ff-attributes candidate :dired t :human-size t)))
  3737. (if (window-system)
  3738. (tooltip-show
  3739. (concat
  3740. (helm-c-basename candidate) "\n"
  3741. "Type: " type "\n"
  3742. (when (string= type "symlink")
  3743. (format "True name: '%s'\n"
  3744. (cond ((string-match "^\.#" (helm-c-basename candidate))
  3745. "Autosave symlink")
  3746. ((helm-ff-valid-symlink-p candidate)
  3747. (file-truename candidate))
  3748. (t "Invalid Symlink"))))
  3749. dired-line))
  3750. (message dired-line) (sit-for 5))))
  3751. ;;;###autoload
  3752. (defun helm-ff-properties-persistent ()
  3753. "Show properties without quitting helm."
  3754. (interactive)
  3755. (helm-attrset 'properties-action 'helm-ff-properties)
  3756. (helm-execute-persistent-action 'properties-action))
  3757. ;;;###autoload
  3758. (defun helm-ff-persistent-delete ()
  3759. "Delete current candidate without quitting."
  3760. (interactive)
  3761. (helm-attrset 'quick-delete 'helm-ff-quick-delete)
  3762. (helm-execute-persistent-action 'quick-delete))
  3763. (defun helm-ff-dot-file-p (file)
  3764. "Check if FILE is `.' or `..'."
  3765. (member (helm-c-basename file) '("." "..")))
  3766. (defun helm-ff-quick-delete (candidate)
  3767. "Delete file CANDIDATE without quitting."
  3768. (let ((presel (prog1 (save-excursion
  3769. (let (sel)
  3770. (helm-next-line)
  3771. (setq sel (helm-get-selection))
  3772. (if (string= sel candidate)
  3773. (progn (helm-previous-line)
  3774. (helm-get-selection))
  3775. sel)))
  3776. (helm-mark-current-line))))
  3777. (setq presel (if (and helm-ff-transformer-show-only-basename
  3778. (not (helm-ff-dot-file-p presel)))
  3779. (helm-c-basename presel) presel))
  3780. (if helm-ff-quick-delete-dont-prompt-for-deletion
  3781. (helm-c-delete-file candidate
  3782. helm-ff-signal-error-on-dot-files)
  3783. (save-selected-window
  3784. (when (y-or-n-p (format "Really Delete file `%s'? " candidate))
  3785. (helm-c-delete-file candidate
  3786. helm-ff-signal-error-on-dot-files)
  3787. (message nil))))
  3788. (helm-force-update presel)))
  3789. (defun helm-ff-kill-buffer-fname (candidate)
  3790. (let* ((buf (get-file-buffer candidate))
  3791. (buf-name (buffer-name buf)))
  3792. (if buf
  3793. (progn
  3794. (kill-buffer buf) (message "Buffer `%s' killed" buf))
  3795. (message "No buffer to kill"))))
  3796. (defun helm-ff-kill-or-find-buffer-fname (candidate)
  3797. "Find file CANDIDATE or kill it's buffer if it is visible.
  3798. Never kill `helm-current-buffer'.
  3799. Never kill buffer modified.
  3800. This is called normally on third hit of \
  3801. \\<helm-map>\\[helm-execute-persistent-action]
  3802. in `helm-find-files-persistent-action'."
  3803. (let* ((buf (get-file-buffer candidate))
  3804. (buf-name (buffer-name buf)))
  3805. (if (and buf (get-buffer-window buf)
  3806. (not (eq buf (get-buffer helm-current-buffer)))
  3807. (not (buffer-modified-p buf)))
  3808. (progn
  3809. (kill-buffer buf) (message "Buffer `%s' killed" buf-name))
  3810. (find-file candidate))))
  3811. ;;;###autoload
  3812. (defun helm-ff-run-kill-buffer-persistent ()
  3813. "Execute `helm-ff-kill-buffer-fname' whitout quitting."
  3814. (interactive)
  3815. (when (helm-file-completion-source-p)
  3816. (helm-attrset 'kill-buffer-fname 'helm-ff-kill-buffer-fname)
  3817. (helm-execute-persistent-action 'kill-buffer-fname)))
  3818. (defun helm-ff-human-size (size)
  3819. "Return a string showing SIZE of a file in human readable form.
  3820. SIZE can be an integer or a float depending it's value.
  3821. `file-attributes' will take care of that to avoid overflow error.
  3822. KBSIZE if a floating point number, default value is 1024.0."
  3823. (let ((M (cons "M" (/ size (expt helm-ff-default-kbsize 2))))
  3824. (G (cons "G" (/ size (expt helm-ff-default-kbsize 3))))
  3825. (K (cons "K" (/ size helm-ff-default-kbsize)))
  3826. (B (cons "B" size)))
  3827. (loop with result = B
  3828. for (a . b) in
  3829. (loop for (x . y) in (list M G K B)
  3830. unless (< y 1) collect (cons x y))
  3831. when (< b (cdr result)) do (setq result (cons a b))
  3832. finally return (if (string= (car result) "B")
  3833. (format "%s" size)
  3834. (format "%.1f%s" (cdr result) (car result))))))
  3835. (defun* helm-ff-attributes
  3836. (file &key type links uid gid access-time modif-time
  3837. status size mode gid-change inode device-num dired human-size)
  3838. "Easy interface for `file-attributes'."
  3839. (let ((all (destructuring-bind
  3840. (type links uid gid access-time modif-time
  3841. status size mode gid-change inode device-num)
  3842. (file-attributes file 'string)
  3843. (list :type type
  3844. :links links
  3845. :uid uid
  3846. :gid gid
  3847. :access-time access-time
  3848. :modif-time modif-time
  3849. :status status
  3850. :size size
  3851. :mode mode
  3852. :gid-change gid-change
  3853. :inode inode
  3854. :device-num device-num))))
  3855. (cond (type
  3856. (let ((result (getf all :type)))
  3857. (cond ((stringp result)
  3858. "symlink")
  3859. (result "directory")
  3860. (t "file"))))
  3861. (links (getf all :links))
  3862. (uid (getf all :uid))
  3863. (gid (getf all :gid))
  3864. (access-time
  3865. (format-time-string "%Y-%m-%d %R" (getf all :access-time)))
  3866. (modif-time
  3867. (format-time-string "%Y-%m-%d %R" (getf all :modif-time)))
  3868. (status
  3869. (format-time-string "%Y-%m-%d %R" (getf all :status)))
  3870. (size (if human-size (helm-ff-human-size (getf all :size))
  3871. (getf all :size)))
  3872. (mode (getf all :mode))
  3873. (gid-change (getf all :gid-change))
  3874. (inode (getf all :inode))
  3875. (device-num (getf all :device-num))
  3876. (dired
  3877. (concat
  3878. (getf all :mode) " "
  3879. (number-to-string (getf all :links)) " "
  3880. (getf all :uid) ":"
  3881. (getf all :gid) " "
  3882. (if human-size (helm-ff-human-size (getf all :size))
  3883. (int-to-string (getf all :size))) " "
  3884. (format-time-string "%Y-%m-%d %R" (getf all :modif-time))))
  3885. (t all))))
  3886. (defun helm-ff-prefix-filename (fname &optional file-or-symlinkp new-file)
  3887. "Return filename FNAME maybe prefixed with [?] or [@].
  3888. If FILE-OR-SYMLINKP is non--nil this mean we assume FNAME is an
  3889. existing filename or valid symlink and there is no need to test it.
  3890. NEW-FILE when non--nil mean FNAME is a non existing file and
  3891. return FNAME prefixed with [?]."
  3892. (let* ((prefix-new (propertize
  3893. " " 'display
  3894. (propertize "[?]" 'face 'helm-ff-prefix)))
  3895. (prefix-url (propertize
  3896. " " 'display
  3897. (propertize "[@]" 'face 'helm-ff-prefix))))
  3898. (cond ((or file-or-symlinkp (file-exists-p fname)) fname)
  3899. ((or (string-match helm-ff-url-regexp fname)
  3900. (string-match ffap-url-regexp fname))
  3901. (concat prefix-url " " fname))
  3902. ((or new-file (not (file-exists-p fname)))
  3903. (concat prefix-new " " fname)))))
  3904. (defun helm-c-find-files-transformer (files sources)
  3905. "Transformer for `helm-c-source-find-files'.
  3906. Tramp files are not highlighted unless `helm-ff-tramp-not-fancy'
  3907. is non--nil."
  3908. (if (and (string-match tramp-file-name-regexp helm-pattern)
  3909. helm-ff-tramp-not-fancy)
  3910. (if helm-ff-transformer-show-only-basename
  3911. (loop for i in files collect
  3912. (if (string-match "[.]\\{1,2\\}$" i)
  3913. i (cons (helm-c-basename i) i)))
  3914. files)
  3915. (helm-ff-highlight-files files sources)))
  3916. (defun helm-ff-highlight-files (files sources)
  3917. "Candidate transformer for `helm-c-source-find-files' without icons."
  3918. (loop for i in files
  3919. for disp = (if (and helm-ff-transformer-show-only-basename
  3920. (not (string-match "[.]\\{1,2\\}$" i))
  3921. (not (string-match ffap-url-regexp i))
  3922. (not (string-match helm-ff-url-regexp i)))
  3923. (helm-c-basename i) i)
  3924. collect
  3925. (cond ((and (stringp (car (file-attributes i)))
  3926. (not (helm-ff-valid-symlink-p i))
  3927. (not (string-match "^\.#" (helm-c-basename i))))
  3928. (cons (helm-ff-prefix-filename
  3929. (propertize disp 'face 'helm-ff-invalid-symlink) t)
  3930. i))
  3931. ((stringp (car (file-attributes i)))
  3932. (cons (helm-ff-prefix-filename
  3933. (propertize disp 'face 'helm-ff-symlink) t)
  3934. i))
  3935. ((eq t (car (file-attributes i)))
  3936. (cons (helm-ff-prefix-filename
  3937. (propertize disp 'face 'helm-ff-directory) t)
  3938. i))
  3939. ((file-executable-p i)
  3940. (cons (helm-ff-prefix-filename
  3941. (propertize disp 'face 'helm-ff-executable) t)
  3942. i))
  3943. ((file-exists-p i)
  3944. (cons (helm-ff-prefix-filename
  3945. (propertize disp 'face 'helm-ff-file) t)
  3946. i))
  3947. (t
  3948. (cons (helm-ff-prefix-filename
  3949. (propertize disp 'face 'helm-ff-file) nil 'new-file)
  3950. i)))))
  3951. (defun helm-find-files-action-transformer (actions candidate)
  3952. "Action transformer for `helm-c-source-find-files'."
  3953. (cond ((with-helm-current-buffer
  3954. (eq major-mode 'message-mode))
  3955. (append (subseq actions 0 4)
  3956. '(("Gnus attach file(s)" . helm-ff-gnus-attach-files))
  3957. (subseq actions 4)))
  3958. ((string-match (image-file-name-regexp) candidate)
  3959. (append (subseq actions 0 4)
  3960. '(("Rotate image right `M-r'" . helm-ff-rotate-image-right)
  3961. ("Rotate image left `M-l'" . helm-ff-rotate-image-left))
  3962. (subseq actions 4)))
  3963. ((string-match "\.el$" (helm-aif (helm-marked-candidates)
  3964. (car it) candidate))
  3965. (append (subseq actions 0 4)
  3966. '(("Byte compile lisp file(s) `M-B, C-u to load'"
  3967. . helm-find-files-byte-compile)
  3968. ("Load File(s) `M-L'" . helm-find-files-load-files))
  3969. (subseq actions 4)))
  3970. ((and (string-match "\.html?$" candidate)
  3971. (file-exists-p candidate))
  3972. (append (subseq actions 0 4)
  3973. '(("Browse url file" . browse-url-of-file))
  3974. (subseq actions 5)))
  3975. ((or (string= (file-name-extension candidate) "pdf")
  3976. (string= (file-name-extension candidate) "PDF"))
  3977. (append (subseq actions 0 4)
  3978. '(("Pdfgrep File(s)" . helm-ff-pdfgrep))
  3979. (subseq actions 5)))
  3980. (t actions)))
  3981. (defun helm-ff-gnus-attach-files (candidate)
  3982. "Run `gnus-dired-attach' on `helm-marked-candidates' or CANDIDATE."
  3983. (let ((flist (helm-marked-candidates)))
  3984. (gnus-dired-attach flist)))
  3985. (defun helm-ff-rotate-current-image-1 (file &optional num-arg)
  3986. "Rotate current image at NUM-ARG degrees.
  3987. This is a destructive operation on FILE made by external tool mogrify."
  3988. (declare (special image-dired-display-image-buffer))
  3989. (setq file (file-truename file)) ; For symlinked images.
  3990. ;; When FILE is not an image-file, do nothing.
  3991. (when (string-match (image-file-name-regexp) file)
  3992. (if (executable-find "mogrify")
  3993. (progn
  3994. (shell-command (format "mogrify -rotate %s %s"
  3995. (or num-arg 90)
  3996. (shell-quote-argument file)))
  3997. (when (buffer-live-p image-dired-display-image-buffer)
  3998. (kill-buffer image-dired-display-image-buffer))
  3999. (image-dired-display-image file)
  4000. (message nil)
  4001. (display-buffer (get-buffer image-dired-display-image-buffer)))
  4002. (error "mogrify not found"))))
  4003. (defun helm-ff-rotate-image-left (candidate)
  4004. "Rotate image file CANDIDATE left.
  4005. This affect directly file CANDIDATE."
  4006. (helm-ff-rotate-current-image-1 candidate -90))
  4007. (defun helm-ff-rotate-image-right (candidate)
  4008. "Rotate image file CANDIDATE right.
  4009. This affect directly file CANDIDATE."
  4010. (helm-ff-rotate-current-image-1 candidate))
  4011. (defun helm-ff-rotate-left-persistent ()
  4012. "Rotate image left without quitting helm."
  4013. (interactive)
  4014. (helm-attrset 'image-action1 'helm-ff-rotate-image-left)
  4015. (helm-execute-persistent-action 'image-action1))
  4016. (defun helm-ff-rotate-right-persistent ()
  4017. "Rotate image right without quitting helm."
  4018. (interactive)
  4019. (helm-attrset 'image-action2 'helm-ff-rotate-image-right)
  4020. (helm-execute-persistent-action 'image-action2))
  4021. (defun helm-ff-exif-data (candidate)
  4022. "Extract exif data from file CANDIDATE using `helm-ff-exif-data-program'."
  4023. (if (and helm-ff-exif-data-program
  4024. (executable-find helm-ff-exif-data-program))
  4025. (shell-command-to-string (format "%s %s %s"
  4026. helm-ff-exif-data-program
  4027. helm-ff-exif-data-program-args
  4028. candidate))
  4029. (format "No program %s found to extract exif"
  4030. helm-ff-exif-data-program)))
  4031. (defun helm-find-files-persistent-action (candidate)
  4032. "Open subtree CANDIDATE without quitting helm.
  4033. If CANDIDATE is not a directory expand CANDIDATE filename.
  4034. If CANDIDATE is alone, open file CANDIDATE filename.
  4035. That's mean:
  4036. First hit on C-z expand CANDIDATE second hit open file.
  4037. If a prefix arg is given or `helm-follow-mode' is on open file."
  4038. (let ((follow (buffer-local-value
  4039. 'helm-follow-mode
  4040. (get-buffer-create helm-buffer)))
  4041. (new-pattern (helm-get-selection))
  4042. (num-lines-buf (with-current-buffer helm-buffer
  4043. (count-lines (point-min) (point-max)))))
  4044. (flet ((insert-in-minibuffer (fname)
  4045. (with-selected-window (minibuffer-window)
  4046. (unless follow
  4047. (delete-minibuffer-contents)
  4048. (set-text-properties 0 (length fname) nil fname)
  4049. (insert fname)))))
  4050. (cond ((and (string= (helm-ff-set-pattern helm-pattern)
  4051. "Invalid tramp file name")
  4052. (string-match tramp-file-name-regexp candidate))
  4053. ;; First hit insert hostname and
  4054. ;; second hit insert ":" and expand.
  4055. (if (string= candidate helm-pattern)
  4056. (insert-in-minibuffer (concat candidate ":"))
  4057. (insert-in-minibuffer candidate)))
  4058. (;; A symlink directory, expand it's truename.
  4059. (and (file-directory-p candidate) (file-symlink-p candidate))
  4060. (insert-in-minibuffer (file-name-as-directory
  4061. (file-truename
  4062. (expand-file-name candidate)))))
  4063. ;; A directory, open it.
  4064. ((file-directory-p candidate)
  4065. (when (string= (helm-c-basename candidate) "..")
  4066. (setq helm-ff-last-expanded helm-ff-default-directory))
  4067. (insert-in-minibuffer (file-name-as-directory
  4068. (expand-file-name candidate))))
  4069. ;; A symlink file, expand to it's true name. (first hit)
  4070. ((and (file-symlink-p candidate) (not current-prefix-arg) (not follow))
  4071. (insert-in-minibuffer (file-truename candidate)))
  4072. ;; A regular file, expand it, (first hit)
  4073. ((and (>= num-lines-buf 3) (not current-prefix-arg) (not follow))
  4074. (insert-in-minibuffer new-pattern))
  4075. ;; An image file and it is the second hit on C-z,
  4076. ;; show the file in `image-dired'.
  4077. ((string-match (image-file-name-regexp) candidate)
  4078. (when (buffer-live-p image-dired-display-image-buffer)
  4079. (kill-buffer image-dired-display-image-buffer))
  4080. (image-dired-display-image candidate)
  4081. (message nil)
  4082. (helm-c-switch-to-buffer image-dired-display-image-buffer)
  4083. (with-current-buffer image-dired-display-image-buffer
  4084. (let ((exif-data (helm-ff-exif-data candidate)))
  4085. (image-dired-update-property 'help-echo exif-data))))
  4086. ;; Allow browsing archive on avfs fs.
  4087. ;; Assume volume is already mounted with mountavfs.
  4088. ((and helm-ff-avfs-directory
  4089. (string-match
  4090. (regexp-quote (expand-file-name helm-ff-avfs-directory))
  4091. (file-name-directory candidate))
  4092. (helm-ff-file-compressed-p candidate))
  4093. (insert-in-minibuffer (concat candidate "#")))
  4094. ;; On second hit we open file.
  4095. ;; On Third hit we kill it's buffer maybe.
  4096. (t
  4097. (helm-ff-kill-or-find-buffer-fname candidate))))))
  4098. (defun helm-ff-file-compressed-p (candidate)
  4099. "Whether CANDIDATE is a compressed file or not."
  4100. (member (file-name-extension candidate)
  4101. helm-ff-file-compressed-list))
  4102. (defun helm-c-insert-file-name-completion-at-point (candidate)
  4103. "Insert file name completion at point."
  4104. (with-helm-current-buffer
  4105. (if buffer-read-only
  4106. (error "Error: Buffer `%s' is read-only" (buffer-name))
  4107. (let* ((end (point))
  4108. (guess (substring-no-properties (thing-at-point 'filename)))
  4109. (beg (- (point) (length guess)))
  4110. (full-path-p (or (string-match-p (concat "^" (getenv "HOME")) guess)
  4111. (string-match-p "^[^\~]" guess))))
  4112. (set-text-properties 0 (length candidate) nil candidate)
  4113. (if (and guess (not (string= guess ""))
  4114. (string-match-p "^~\\|/.*" guess))
  4115. (progn
  4116. (delete-region beg end)
  4117. (insert (if full-path-p
  4118. (expand-file-name candidate)
  4119. (abbreviate-file-name candidate))))
  4120. (error "Aborting completion: No valid file name at point"))))))
  4121. (defun* helm-find-files-history (&key (comp-read t))
  4122. "The `helm-find-files' history.
  4123. Show the first `helm-ff-history-max-length' elements of
  4124. `helm-ff-history' in an `helm-comp-read'."
  4125. (let ((history (when helm-ff-history
  4126. (helm-fast-remove-dups helm-ff-history
  4127. :test 'equal))))
  4128. (when history
  4129. (setq helm-ff-history
  4130. (if (>= (length history) helm-ff-history-max-length)
  4131. (subseq history 0 helm-ff-history-max-length)
  4132. history))
  4133. (if comp-read
  4134. (helm-comp-read
  4135. "Switch to Directory: "
  4136. helm-ff-history
  4137. :name "Helm Find Files History"
  4138. :must-match t)
  4139. helm-ff-history))))
  4140. (defun helm-find-files-1 (fname &optional preselect)
  4141. "Find FNAME with `helm' completion.
  4142. Like `find-file' but with `helm' support.
  4143. Use it for non--interactive calls of `helm-find-files'."
  4144. (when (get-buffer helm-action-buffer)
  4145. (kill-buffer helm-action-buffer))
  4146. (let ((helm-mp-highlight-delay nil)
  4147. ;; Be sure we don't erase the precedent minibuffer if some.
  4148. (helm-ff-auto-update-initial-value
  4149. (and helm-ff-auto-update-initial-value
  4150. (not (minibuffer-window-active-p (minibuffer-window)))))
  4151. helm-samewindow)
  4152. (helm :sources 'helm-c-source-find-files
  4153. :input fname
  4154. :preselect preselect
  4155. :keymap helm-find-files-map
  4156. :prompt "Find Files or Url: "
  4157. :buffer "*Helm Find Files*")))
  4158. (defun helm-find-files-initial-input (&optional input)
  4159. "Return INPUT if present, otherwise try to guess it."
  4160. (or (and input (or (and (file-remote-p input) input)
  4161. (expand-file-name input)))
  4162. (helm-find-files-input
  4163. (ffap-guesser)
  4164. (thing-at-point 'filename))))
  4165. (defun helm-find-files-input (fap tap)
  4166. "Default input of `helm-find-files'."
  4167. (let* ((def-dir (helm-c-current-directory))
  4168. (lib (helm-find-library-at-point))
  4169. (url (helm-ff-find-url-at-point))
  4170. (remp (and fap (file-remote-p fap)))
  4171. (file-p (and (not remp)
  4172. fap
  4173. (not (string= fap ""))
  4174. (file-exists-p fap)
  4175. tap (not (string= tap ""))
  4176. (file-exists-p
  4177. (file-name-directory (expand-file-name tap def-dir))))))
  4178. (cond (lib) ; e.g we are inside a require sexp.
  4179. (url) ; String at point is an hyperlink.
  4180. (remp fap)
  4181. (file-p (expand-file-name tap def-dir))
  4182. (t (and (not (string= fap "")) fap)))))
  4183. (defun helm-c-current-directory ()
  4184. "Return current-directory name at point.
  4185. Useful in dired buffers when there is inserted subdirs."
  4186. (if (eq major-mode 'dired-mode)
  4187. (dired-current-directory)
  4188. default-directory))
  4189. (defun helm-ff-find-url-at-point ()
  4190. "Try to find link to an url in text-property at point."
  4191. (let* ((he (get-text-property (point) 'help-echo))
  4192. (ov (overlays-at (point)))
  4193. (ov-he (and ov (overlay-get
  4194. (car (overlays-at (point))) 'help-echo)))
  4195. (w3m-l (get-text-property (point) 'w3m-href-anchor))
  4196. (nt-prop (get-text-property (point) 'nt-link)))
  4197. ;; Org link.
  4198. (when (and (stringp he) (string-match "^LINK: " he))
  4199. (setq he (replace-match "" t t he)))
  4200. (loop for i in (list he ov-he w3m-l nt-prop)
  4201. thereis (and (stringp i) (string-match ffap-url-regexp i) i))))
  4202. (defun helm-find-library-at-point ()
  4203. "Try to find library path at point.
  4204. Find inside `require' and `declare-function' sexp."
  4205. (require 'find-func)
  4206. (let* ((beg-sexp (save-excursion (search-backward "(" (point-at-bol) t)))
  4207. (end-sexp (save-excursion (search-forward ")" (point-at-eol) t)))
  4208. (sexp (and beg-sexp end-sexp
  4209. (buffer-substring-no-properties
  4210. (1+ beg-sexp) (1- end-sexp)))))
  4211. (ignore-errors
  4212. (cond ((and sexp (string-match "require \'.+[^)]" sexp))
  4213. (find-library-name
  4214. (replace-regexp-in-string
  4215. "'\\|\)\\|\(" ""
  4216. ;; If require use third arg, ignore it,
  4217. ;; always use library path found in `load-path'.
  4218. (second (split-string (match-string 0 sexp))))))
  4219. ((and sexp (string-match-p "^declare-function" sexp))
  4220. (find-library-name
  4221. (replace-regexp-in-string
  4222. "\"\\|ext:" ""
  4223. (third (split-string sexp)))))
  4224. (t nil)))))
  4225. ;;; Helm completion for `write-file'.==> C-x C-w
  4226. (defvar helm-c-source-write-file
  4227. `((name . "Write File")
  4228. (header-name . (lambda (name)
  4229. (concat name helm-c-find-files-doc-header)))
  4230. ;; It is needed for filenames with capital letters
  4231. (disable-shortcuts)
  4232. (candidates . helm-find-files-get-candidates)
  4233. (filtered-candidate-transformer helm-c-find-files-transformer)
  4234. (persistent-action . helm-find-files-persistent-action)
  4235. (persistent-help . "Expand Candidate")
  4236. (volatile)
  4237. (action .
  4238. (("Write File" . (lambda (candidate)
  4239. (write-file candidate 'confirm)))))))
  4240. ;;; Helm completion for `insert-file'.==> C-x i
  4241. (defvar helm-c-source-insert-file
  4242. `((name . "Insert File")
  4243. (header-name . (lambda (name)
  4244. (concat name helm-c-find-files-doc-header)))
  4245. ;; It is needed for filenames with capital letters
  4246. (disable-shortcuts)
  4247. (candidates . helm-find-files-get-candidates)
  4248. (filtered-candidate-transformer helm-c-find-files-transformer)
  4249. (persistent-action . helm-find-files-persistent-action)
  4250. (persistent-help . "Expand Candidate")
  4251. (volatile)
  4252. (action .
  4253. (("Insert File" . (lambda (candidate)
  4254. (when (y-or-n-p (format "Really insert %s in %s "
  4255. candidate helm-current-buffer))
  4256. (insert-file-contents candidate))))))))
  4257. ;;; Helm completion for copy, rename and (rel)sym/hard/link files from dired.
  4258. (defvar helm-c-source-copy-files
  4259. `((name . "Copy Files")
  4260. (header-name . (lambda (name)
  4261. (concat name helm-c-find-files-doc-header)))
  4262. ;; It is needed for filenames with capital letters
  4263. (disable-shortcuts)
  4264. (candidates . helm-find-files-get-candidates)
  4265. (filtered-candidate-transformer helm-c-find-files-transformer)
  4266. (persistent-action . helm-find-files-persistent-action)
  4267. (persistent-help . "Expand Candidate")
  4268. (volatile)
  4269. (action .
  4270. (("Copy File"
  4271. . (lambda (candidate)
  4272. (helm-dired-action candidate :action 'copy)))
  4273. ("Copy and Follow"
  4274. . (lambda (candidate)
  4275. (helm-dired-action candidate :action 'copy :follow t)))))))
  4276. (defvar helm-c-source-rename-files
  4277. `((name . "Rename Files")
  4278. (header-name . (lambda (name)
  4279. (concat name helm-c-find-files-doc-header)))
  4280. ;; It is needed for filenames with capital letters
  4281. (disable-shortcuts)
  4282. (candidates . helm-find-files-get-candidates)
  4283. (filtered-candidate-transformer helm-c-find-files-transformer)
  4284. (persistent-action . helm-find-files-persistent-action)
  4285. (persistent-help . "Expand Candidate")
  4286. (volatile)
  4287. (action .
  4288. (("Rename File"
  4289. . (lambda (candidate)
  4290. (helm-dired-action candidate :action 'rename)))
  4291. ("Rename and Follow"
  4292. . (lambda (candidate)
  4293. (helm-dired-action candidate :action 'rename :follow t)))))))
  4294. (defvar helm-c-source-symlink-files
  4295. `((name . "Symlink Files")
  4296. (header-name . (lambda (name)
  4297. (concat name helm-c-find-files-doc-header)))
  4298. ;; It is needed for filenames with capital letters
  4299. (disable-shortcuts)
  4300. (candidates . helm-find-files-get-candidates)
  4301. (filtered-candidate-transformer helm-c-find-files-transformer)
  4302. (persistent-action . helm-find-files-persistent-action)
  4303. (persistent-help . "Expand Candidate")
  4304. (volatile)
  4305. (action
  4306. . (("Symlink File"
  4307. . (lambda (candidate)
  4308. (helm-dired-action candidate :action 'symlink)))
  4309. ("RelSymlink File"
  4310. . (lambda (candidate)
  4311. (helm-dired-action candidate :action 'relsymlink)))))))
  4312. (defvar helm-c-source-hardlink-files
  4313. `((name . "Hardlink Files")
  4314. (header-name . (lambda (name)
  4315. (concat name helm-c-find-files-doc-header)))
  4316. ;; It is needed for filenames with capital letters
  4317. (disable-shortcuts)
  4318. (candidates . helm-find-files-get-candidates)
  4319. (filtered-candidate-transformer helm-c-find-files-transformer)
  4320. (persistent-action . helm-find-files-persistent-action)
  4321. (persistent-help . "Expand Candidate")
  4322. (volatile)
  4323. (action
  4324. . (("Hardlink File"
  4325. . (lambda (candidate)
  4326. (helm-dired-action candidate :action 'hardlink)))))))
  4327. (defun* helm-dired-action (candidate &key action follow (files (dired-get-marked-files)))
  4328. "Copy, rename or symlink file at point or marked files in dired to CANDIDATE.
  4329. ACTION is a key that can be one of 'copy, 'rename, 'symlink, 'relsymlink."
  4330. (when (get-buffer dired-log-buffer) (kill-buffer dired-log-buffer))
  4331. (let ((fn (case action
  4332. ('copy 'dired-copy-file)
  4333. ('rename 'dired-rename-file)
  4334. ('symlink 'make-symbolic-link)
  4335. ('relsymlink 'dired-make-relative-symlink)
  4336. ('hardlink 'dired-hardlink)))
  4337. (marker (case action
  4338. ((copy rename) dired-keep-marker-copy)
  4339. ('symlink dired-keep-marker-symlink)
  4340. ('relsymlink dired-keep-marker-relsymlink)
  4341. ('hardlink dired-keep-marker-hardlink)))
  4342. (dirflag (and (= (length files) 1)
  4343. (file-directory-p (car files))
  4344. (not (file-directory-p candidate)))))
  4345. (dired-create-files
  4346. fn (symbol-name action) files
  4347. ;; CANDIDATE is the destination.
  4348. (if (file-directory-p candidate)
  4349. ;; When CANDIDATE is a directory, build file-name in this directory.
  4350. ;; Else we use CANDIDATE.
  4351. #'(lambda (from)
  4352. (expand-file-name (file-name-nondirectory from) candidate))
  4353. #'(lambda (from) candidate))
  4354. marker)
  4355. (push (file-name-as-directory
  4356. (if (file-directory-p candidate)
  4357. (expand-file-name candidate)
  4358. (file-name-directory candidate)))
  4359. helm-ff-history)
  4360. (when (and follow (not (get-buffer dired-log-buffer)))
  4361. (let ((target (directory-file-name candidate)))
  4362. (unwind-protect
  4363. (progn
  4364. (setq helm-ff-cand-to-mark
  4365. (helm-get-dest-fnames-from-list files candidate dirflag))
  4366. (if (and dirflag (eq action 'rename))
  4367. (helm-find-files-1 (file-name-directory target)
  4368. (if helm-ff-transformer-show-only-basename
  4369. (helm-c-basename target) target))
  4370. (helm-find-files-1 (expand-file-name candidate))))
  4371. (setq helm-ff-cand-to-mark nil))))))
  4372. (defun helm-c-basename (fname &optional ext)
  4373. "Print FNAME with any leading directory components removed.
  4374. If specified, also remove filename extension EXT."
  4375. (if (and ext (or (string= (file-name-extension fname) ext)
  4376. (string= (file-name-extension fname t) ext))
  4377. (not (file-directory-p fname)))
  4378. (file-name-sans-extension (file-name-nondirectory fname))
  4379. (file-name-nondirectory (directory-file-name fname))))
  4380. (defun helm-get-dest-fnames-from-list (flist dest-cand rename-dir-flag)
  4381. "Transform filenames of FLIST to abs of DEST-CAND.
  4382. If RENAME-DIR-FLAG is non--nil collect the `directory-file-name' of transformed
  4383. members of FLIST."
  4384. ;; At this point files have been renamed/copied at destination.
  4385. ;; That's mean DEST-CAND exists.
  4386. (loop
  4387. with dest = (expand-file-name dest-cand)
  4388. for src in flist
  4389. for basename-src = (helm-c-basename src)
  4390. for fname = (cond (rename-dir-flag (directory-file-name dest))
  4391. ((file-directory-p dest)
  4392. (concat (file-name-as-directory dest) basename-src))
  4393. (t dest))
  4394. when (file-exists-p fname)
  4395. collect fname into tmp-list
  4396. finally return (sort tmp-list 'string<)))
  4397. (defun helm-ff-maybe-mark-candidates ()
  4398. "Mark all candidates of list `helm-ff-cand-to-mark'."
  4399. (when (and (string= (assoc-default 'name (helm-get-current-source))
  4400. (assoc-default 'name helm-c-source-find-files))
  4401. helm-ff-cand-to-mark)
  4402. (with-helm-window
  4403. (while helm-ff-cand-to-mark
  4404. (if (string= (car helm-ff-cand-to-mark) (helm-get-selection))
  4405. (progn
  4406. (helm-make-visible-mark)
  4407. (helm-next-line)
  4408. (setq helm-ff-cand-to-mark (cdr helm-ff-cand-to-mark)))
  4409. (helm-next-line)))
  4410. (unless (helm-this-visible-mark)
  4411. (helm-prev-visible-mark)))))
  4412. (add-hook 'helm-after-update-hook #'helm-ff-maybe-mark-candidates)
  4413. (defun* helm-dired-do-action-on-file (&key action)
  4414. (let* ((files (dired-get-marked-files))
  4415. (len (length files))
  4416. (fname (if (> len 1)
  4417. (format "* %d Files" len)
  4418. (car files)))
  4419. (source (case action
  4420. ('copy 'helm-c-source-copy-files)
  4421. ('rename 'helm-c-source-rename-files)
  4422. ('symlink 'helm-c-source-symlink-files)
  4423. ('hardlink 'helm-c-source-hardlink-files)))
  4424. (prompt-fm (case action
  4425. ('copy "Copy %s to: ")
  4426. ('rename "Rename %s to: ")
  4427. ('symlink "Symlink %s to: ")
  4428. ('hardlink "Hardlink %s to: ")))
  4429. (buffer (case action
  4430. ('copy "*Helm Copy Files*")
  4431. ('rename "*Helm Rename Files*")
  4432. ('symlink "*Helm Symlink Files*")
  4433. ('hardlink "*Helm Hardlink Files*")))
  4434. (helm-mp-highlight-delay nil))
  4435. (helm :sources source
  4436. :input (or (dired-dwim-target-directory)
  4437. (expand-file-name (helm-c-current-directory)))
  4438. :preselect (dired-get-filename)
  4439. :prompt (format prompt-fm fname)
  4440. :keymap helm-c-read-file-map
  4441. :buffer buffer)))
  4442. ;;;###autoload
  4443. (define-minor-mode helm-dired-mode ()
  4444. "Enable helm completion in Dired functions.
  4445. Bindings affected are C, R, S, H.
  4446. This is deprecated for Emacs24+ users, use `helm-mode' instead."
  4447. :group 'helm-config
  4448. :global t
  4449. (if helm-dired-mode
  4450. (progn
  4451. (substitute-key-definition
  4452. 'dired-do-copy 'helm-dired-copy-file dired-mode-map)
  4453. (substitute-key-definition
  4454. 'dired-do-rename 'helm-dired-rename-file dired-mode-map)
  4455. (substitute-key-definition
  4456. 'dired-do-symlink 'helm-dired-symlink-file dired-mode-map)
  4457. (substitute-key-definition
  4458. 'dired-do-hardlink 'helm-dired-hardlink-file dired-mode-map))
  4459. (substitute-key-definition
  4460. 'helm-dired-copy-file 'dired-do-copy dired-mode-map)
  4461. (substitute-key-definition
  4462. 'helm-dired-rename-file 'dired-do-rename dired-mode-map)
  4463. (substitute-key-definition
  4464. 'helm-dired-symlink-file 'dired-do-symlink dired-mode-map)
  4465. (substitute-key-definition
  4466. 'helm-dired-hardlink-file 'dired-do-hardlink dired-mode-map)))
  4467. (defalias 'helm-dired-bindings 'helm-dired-mode)
  4468. (defun* helm-c-read-file-name
  4469. (prompt
  4470. &key
  4471. (name "Read File Name")
  4472. (initial-input (expand-file-name default-directory))
  4473. (buffer "*Helm Completions*")
  4474. test
  4475. (preselect nil)
  4476. (history nil)
  4477. must-match
  4478. (marked-candidates nil)
  4479. (alistp t)
  4480. (persistent-action 'helm-find-files-persistent-action)
  4481. (persistent-help "Hit1 Expand Candidate, Hit2 or (C-u) Find file"))
  4482. "Read a file name with helm completion.
  4483. It is helm `read-file-name' emulation.
  4484. Argument PROMPT is the default prompt to use.
  4485. Keys description:
  4486. - NAME: Source name, default to \"Read File Name\".
  4487. - INITIAL-INPUT: Where to start read file name, default to `default-directory'.
  4488. - BUFFER: `helm-buffer' name default to \"*Helm Completions*\".
  4489. - TEST: A predicate called with one arg 'candidate'.
  4490. - PRESELECT: helm preselection.
  4491. - HISTORY: Display HISTORY in a special source.
  4492. - MUST-MATCH: Can be 'confirm, nil, or t.
  4493. - MARKED-CANDIDATES: When non--nil return a list of marked candidates.
  4494. - ALISTP: Don't use `all-completions' in history (take effect only on history).
  4495. - PERSISTENT-ACTION: a persistent action function.
  4496. - PERSISTENT-HELP: persistent help message."
  4497. (when (get-buffer helm-action-buffer)
  4498. (kill-buffer helm-action-buffer))
  4499. ;; Assume completion have been already required,
  4500. ;; so always use 'confirm.
  4501. (when (eq must-match 'confirm-after-completion)
  4502. (setq must-match 'confirm))
  4503. (flet ((action-fn (candidate)
  4504. (if marked-candidates
  4505. (helm-marked-candidates)
  4506. (identity candidate))))
  4507. (let* ((helm-mp-highlight-delay nil)
  4508. ;; Be sure we don't erase the underlying minibuffer if some.
  4509. (helm-ff-auto-update-initial-value
  4510. (and helm-ff-auto-update-initial-value
  4511. (not (minibuffer-window-active-p (minibuffer-window)))))
  4512. helm-same-window
  4513. (hist (and history (helm-comp-read-get-candidates
  4514. history nil nil alistp)))
  4515. (minibuffer-completion-confirm must-match)
  4516. (must-match-map (when must-match
  4517. (let ((map (make-sparse-keymap)))
  4518. (define-key map (kbd "RET")
  4519. 'helm-confirm-and-exit-minibuffer)
  4520. map)))
  4521. (helm-map (if must-match-map
  4522. (make-composed-keymap
  4523. must-match-map helm-c-read-file-map)
  4524. helm-c-read-file-map)))
  4525. (or (helm
  4526. :sources
  4527. `(((name . ,(format "%s History" name))
  4528. (header-name . (lambda (name)
  4529. (concat name helm-c-find-files-doc-header)))
  4530. (disable-shortcuts)
  4531. (mode-line . helm-read-file-name-mode-line-string)
  4532. (candidates . hist)
  4533. (persistent-action . ,persistent-action)
  4534. (persistent-help . ,persistent-help)
  4535. (action . ,'action-fn))
  4536. ((name . ,name)
  4537. (header-name . (lambda (name)
  4538. (concat name helm-c-find-files-doc-header)))
  4539. (init . (lambda ()
  4540. (setq helm-ff-auto-update-flag
  4541. helm-ff-auto-update-initial-value)))
  4542. ;; It is needed for filenames with capital letters
  4543. (disable-shortcuts)
  4544. (mode-line . helm-read-file-name-mode-line-string)
  4545. (candidates
  4546. . (lambda ()
  4547. (if test
  4548. (loop with hn = (helm-ff-tramp-hostnames)
  4549. for i in (helm-find-files-get-candidates
  4550. must-match)
  4551. when (or (member i hn) ; A tramp host
  4552. (funcall test i) ; Test ok
  4553. (not (file-exists-p i))) ; A new file.
  4554. collect i)
  4555. (helm-find-files-get-candidates must-match))))
  4556. (filtered-candidate-transformer helm-c-find-files-transformer)
  4557. (persistent-action . ,persistent-action)
  4558. (candidate-number-limit . 9999)
  4559. (toggle-auto-update . helm-ff-toggle-auto-update)
  4560. (persistent-help . ,persistent-help)
  4561. (volatile)
  4562. (action . ,'action-fn)))
  4563. :input initial-input
  4564. :prompt prompt
  4565. :resume 'noresume
  4566. :buffer buffer
  4567. :preselect preselect)
  4568. (when (and (not (string= helm-pattern ""))
  4569. (eq helm-exit-status 0)
  4570. (eq must-match 'confirm))
  4571. (identity helm-pattern))
  4572. (keyboard-quit)))))
  4573. ;;; File Cache
  4574. (defvar helm-c-file-cache-initialized-p nil)
  4575. (defvar helm-c-file-cache-files nil)
  4576. (defvar helm-c-source-file-cache
  4577. `((name . "File Cache")
  4578. (init
  4579. . (lambda ()
  4580. (require 'filecache nil t)
  4581. (unless helm-c-file-cache-initialized-p
  4582. (setq helm-c-file-cache-files
  4583. (loop for item in file-cache-alist append
  4584. (destructuring-bind (base &rest dirs) item
  4585. (loop for dir in dirs collect
  4586. (concat dir base)))))
  4587. (defadvice file-cache-add-file (after file-cache-list activate)
  4588. (add-to-list 'helm-c-file-cache-files (expand-file-name file)))
  4589. (setq helm-c-file-cache-initialized-p t))))
  4590. (keymap . ,helm-generic-files-map)
  4591. (help-message . helm-generic-file-help-message)
  4592. (mode-line . helm-generic-file-mode-line-string)
  4593. (candidates . helm-c-file-cache-files)
  4594. (match helm-c-match-on-basename)
  4595. (type . file)))
  4596. ;;; Locate
  4597. ;;
  4598. ;;
  4599. ;; NOTE for WINDOZE users:
  4600. ;; You have to install Everything with his command line interface here:
  4601. ;; http://www.voidtools.com/download.php
  4602. (defun helm-ff-find-locatedb (&optional from-ff)
  4603. "Try to find if a local locatedb file is available.
  4604. The search is done in `helm-ff-default-directory' or
  4605. fall back to `default-directory' if FROM-FF is nil."
  4606. (when helm-ff-locate-db-filename
  4607. (cond ((and helm-ff-default-directory
  4608. from-ff
  4609. (file-exists-p (expand-file-name
  4610. helm-ff-locate-db-filename
  4611. helm-ff-default-directory))
  4612. (expand-file-name
  4613. helm-ff-locate-db-filename
  4614. helm-ff-default-directory)))
  4615. ((and (not from-ff)
  4616. (file-exists-p (expand-file-name
  4617. helm-ff-locate-db-filename
  4618. default-directory))
  4619. (expand-file-name
  4620. helm-ff-locate-db-filename
  4621. default-directory))))))
  4622. (defun helm-locate-1 (&optional localdb init from-ff)
  4623. "Generic function to run Locate.
  4624. if LOCALDB is non--nil search and use a local locate db file.
  4625. INIT is a string to use as initial input in prompt.
  4626. See `helm-locate-with-db' and `helm-locate'."
  4627. (helm-locate-with-db
  4628. (and localdb
  4629. (or (helm-ff-find-locatedb from-ff)
  4630. (helm-c-read-file-name
  4631. "LocateDBFiles: "
  4632. :initial-input (or helm-ff-default-directory
  4633. default-directory)
  4634. :marked-candidates t
  4635. :preselect helm-locate-db-file-regexp
  4636. :test #'(lambda (x)
  4637. (if helm-locate-db-file-regexp
  4638. ;; Select only locate db files and directories
  4639. ;; to allow navigation.
  4640. (or (string-match
  4641. helm-locate-db-file-regexp x)
  4642. (file-directory-p x))
  4643. x)))))
  4644. init))
  4645. ;; (helm-locate-1 t)
  4646. (defun helm-locate-with-db (&optional db initial-input)
  4647. "Run locate -d DB.
  4648. If DB is not given or nil use locate without -d option.
  4649. Argument DB can be given as a string or list of db files.
  4650. Argument INITIAL-INPUT is a string to use as initial-input.
  4651. See also `helm-locate'."
  4652. (when (and db (stringp db)) (setq db (list db)))
  4653. (unless helm-c-locate-command
  4654. (setq helm-c-locate-command
  4655. (case system-type
  4656. ('gnu/linux "locate -i -r %s")
  4657. ('berkeley-unix "locate -i %s")
  4658. ('windows-nt "es -i -r %s")
  4659. (t "locate %s"))))
  4660. (let ((helm-c-locate-command
  4661. (if db
  4662. (replace-regexp-in-string
  4663. "locate"
  4664. (format "locate -d %s"
  4665. (mapconcat 'identity
  4666. ;; Remove eventually
  4667. ;; marked directories by error.
  4668. (loop for i in db
  4669. unless (file-directory-p i)
  4670. collect i) ":"))
  4671. helm-c-locate-command)
  4672. helm-c-locate-command)))
  4673. (helm :sources 'helm-c-source-locate
  4674. :buffer "*helm locate*"
  4675. :input initial-input
  4676. :keymap helm-generic-files-map)))
  4677. ;; (helm-locate-with-db "~/locate.db")
  4678. (defun helm-c-locate-init ()
  4679. "Initialize async locate process for `helm-c-source-locate'."
  4680. (setq mode-line-format
  4681. '(" " mode-line-buffer-identification " "
  4682. (line-number-mode "%l") " "
  4683. (:eval (propertize "(Locate Process Running) "
  4684. 'face '((:foreground "red"))))))
  4685. (prog1
  4686. (start-process-shell-command "locate-process" nil
  4687. (format helm-c-locate-command
  4688. helm-pattern))
  4689. (set-process-sentinel (get-process "locate-process")
  4690. #'(lambda (process event)
  4691. (when (string= event "finished\n")
  4692. (with-helm-window
  4693. (force-mode-line-update nil)
  4694. (helm-update-move-first-line)))))))
  4695. (defvar helm-c-source-locate
  4696. `((name . "Locate")
  4697. (candidates . helm-c-locate-init)
  4698. (type . file)
  4699. (requires-pattern . 3)
  4700. (keymap . ,helm-generic-files-map)
  4701. (help-message . helm-generic-file-help-message)
  4702. (candidate-number-limit . 9999)
  4703. (mode-line . helm-generic-file-mode-line-string)
  4704. (delayed))
  4705. "Find files matching the current input pattern with locate.")
  4706. (defun helm-c-locate-read-file-name (prompt &optional init)
  4707. "Search a file with locate and return it's filename.
  4708. Use argument PROMPT and INIT for `helm' arguments
  4709. prompt and input."
  4710. (helm :sources
  4711. '((name . "Locate")
  4712. (candidates . helm-c-locate-init)
  4713. (action . identity)
  4714. (requires-pattern . 3)
  4715. (candidate-number-limit . 9999)
  4716. (mode-line . helm-generic-file-mode-line-string)
  4717. (delayed))
  4718. :prompt prompt
  4719. :input init
  4720. :buffer "*helm locate rfn*"))
  4721. ;;; Helm Incremental Grep.
  4722. ;;
  4723. ;;
  4724. ;; Allow to grep incrementally with helm interface.
  4725. ;; It allow also to Grep files recursively without using 'find' shell command.
  4726. ;; On Windows you will need at least Grep version 2.5.4 of Gnuwin32.
  4727. (defvar helm-c-grep-default-command
  4728. "grep -d skip %e -niH -e %p %f"
  4729. "Default grep format command for `helm-do-grep-1'.
  4730. Where:
  4731. '%e' format spec is for --exclude or --include grep options.
  4732. '%p' format spec is for pattern.
  4733. '%f' format spec is for filenames.")
  4734. (defvar helm-c-grep-default-recurse-command
  4735. "grep -d recurse %e -niH -e %p %f"
  4736. "Default recursive grep format command for `helm-do-grep-1'.
  4737. See `helm-c-grep-default-command' for format specs.")
  4738. (defvar helm-c-default-zgrep-command "zgrep -niH -e %p %f")
  4739. (defvar helm-c-rzgrep-cache (make-hash-table :test 'equal))
  4740. (defvar helm-c-grep-default-function 'helm-c-grep-init)
  4741. (defvar helm-c-grep-debug-command-line nil
  4742. "Turn on helm grep command-line debugging when non--nil.")
  4743. (defvar helm-c-zgrep-recurse-flag nil)
  4744. (defvar helm-c-grep-history nil)
  4745. (defvar helm-c-grep-max-length-history 100
  4746. "*Max number of elements to save in `helm-c-grep-history'.")
  4747. (defun helm-c-grep-prepare-candidates (candidates)
  4748. "Prepare filenames and directories CANDIDATES for grep command line."
  4749. ;; If one or more candidate is a directory, search in all files
  4750. ;; of this candidate (e.g /home/user/directory/*).
  4751. ;; If r option is enabled search also in subdidrectories.
  4752. ;; We need here to expand wildcards to support crap windows filenames
  4753. ;; as grep doesn't accept quoted wildcards (e.g "dir/*.el").
  4754. (if helm-c-zgrep-recurse-flag
  4755. (mapconcat 'shell-quote-argument candidates " ")
  4756. (loop for i in candidates append
  4757. (cond ( ;; Candidate is a directory and we use recursion.
  4758. (and (file-directory-p i)
  4759. (helm-c-grep-recurse-p))
  4760. (list (expand-file-name i)))
  4761. ;; Candidate is a directory, search in all files.
  4762. ((file-directory-p i)
  4763. (file-expand-wildcards
  4764. (concat (file-name-as-directory (expand-file-name i)) "*") t))
  4765. ;; Candidate is a file or wildcard and we use recursion, use the
  4766. ;; current directory instead of candidate.
  4767. ((and (or (file-exists-p i) (string-match "\*" i))
  4768. (helm-c-grep-recurse-p))
  4769. (list (expand-file-name
  4770. (directory-file-name ; Needed for windoze.
  4771. (file-name-directory (directory-file-name i))))))
  4772. ;; Candidate use wildcard.
  4773. ((string-match "^\*" (helm-c-basename i))
  4774. (file-expand-wildcards i t))
  4775. ;; Else should be one or more file.
  4776. (t (list i))) into all-files
  4777. finally return
  4778. (mapconcat 'shell-quote-argument all-files " "))))
  4779. (defun helm-c-grep-recurse-p ()
  4780. "Check if `helm-do-grep-1' have switched to recursive."
  4781. (let ((args (replace-regexp-in-string
  4782. "grep" "" helm-c-grep-default-command)))
  4783. (string-match-p "r\\|recurse" args)))
  4784. (defun helm-c-grep-init (only-files &optional include zgrep)
  4785. "Start an asynchronous grep process in ONLY-FILES list."
  4786. (let* ((fnargs (helm-c-grep-prepare-candidates
  4787. (if (file-remote-p helm-ff-default-directory)
  4788. (mapcar #'(lambda (x)
  4789. (file-remote-p x 'localname))
  4790. only-files)
  4791. only-files)))
  4792. (ignored-files (mapconcat
  4793. #'(lambda (x)
  4794. (concat "--exclude=" (shell-quote-argument x)))
  4795. grep-find-ignored-files " "))
  4796. (ignored-dirs (mapconcat
  4797. ;; Need grep version >=2.5.4 of Gnuwin32 on windoze.
  4798. #'(lambda (x)
  4799. (concat "--exclude-dir=" (shell-quote-argument x)))
  4800. grep-find-ignored-directories " "))
  4801. (exclude (if (helm-c-grep-recurse-p)
  4802. (concat (or include ignored-files) " " ignored-dirs)
  4803. ignored-files))
  4804. (cmd-line (format-spec
  4805. helm-c-grep-default-command
  4806. (delq nil
  4807. (list (unless zgrep (cons ?e exclude))
  4808. (cons ?p (shell-quote-argument helm-pattern))
  4809. (cons ?f fnargs))))))
  4810. (when helm-c-grep-debug-command-line
  4811. (with-current-buffer (get-buffer-create "*any grep debug*")
  4812. (goto-char (point-max))
  4813. (insert (concat ">>> " cmd-line "\n\n"))))
  4814. (setq mode-line-format
  4815. '(" " mode-line-buffer-identification " "
  4816. (line-number-mode "%l") " "
  4817. (:eval (when (get-process "grep-process")
  4818. (propertize "[Grep Process Running] "
  4819. 'face 'helm-grep-running)))))
  4820. (force-mode-line-update nil)
  4821. (prog1
  4822. (let ((default-directory helm-ff-default-directory))
  4823. (start-file-process-shell-command "grep-process" nil cmd-line))
  4824. (message nil)
  4825. (set-process-sentinel
  4826. (get-process "grep-process")
  4827. #'(lambda (process event)
  4828. (when (string= event "finished\n")
  4829. (with-helm-window
  4830. (helm-update-move-first-line)
  4831. (setq mode-line-format
  4832. '(" " mode-line-buffer-identification " "
  4833. (line-number-mode "%l") " "
  4834. (:eval (propertize
  4835. (format "[Grep Process Finished - (%s results)] "
  4836. (let ((nlines (1- (count-lines
  4837. (point-min)
  4838. (point-max)))))
  4839. (if (> nlines 0) nlines 0)))
  4840. 'face 'helm-grep-finish))))
  4841. (force-mode-line-update nil))))))))
  4842. (defun helm-c-grep-action (candidate &optional where mark)
  4843. "Define a default action for `helm-do-grep' on CANDIDATE.
  4844. WHERE can be one of other-window, elscreen, other-frame."
  4845. (let* ((split (helm-c-grep-split-line candidate))
  4846. (lineno (string-to-number (nth 1 split)))
  4847. (loc-fname (car split))
  4848. (tramp-method (file-remote-p helm-ff-default-directory 'method))
  4849. (tramp-host (file-remote-p helm-ff-default-directory 'host))
  4850. (tramp-prefix (concat "/" tramp-method ":" tramp-host ":"))
  4851. (fname (if tramp-host
  4852. (concat tramp-prefix loc-fname) loc-fname)))
  4853. (case where
  4854. (other-window (find-file-other-window fname))
  4855. (elscreen (helm-elscreen-find-file fname))
  4856. (other-frame (find-file-other-frame fname))
  4857. (grep (helm-c-grep-save-results-1))
  4858. (t (find-file fname)))
  4859. (unless (eq where 'grep)
  4860. (helm-goto-line lineno))
  4861. (when mark
  4862. (set-marker (mark-marker) (point))
  4863. (push-mark (point) 'nomsg))
  4864. ;; Save history
  4865. (unless (or helm-in-persistent-action
  4866. (string= helm-pattern ""))
  4867. (setq helm-c-grep-history
  4868. (cons helm-pattern
  4869. (delete helm-pattern helm-c-grep-history)))
  4870. (when (> (length helm-c-grep-history)
  4871. helm-c-grep-max-length-history)
  4872. (setq helm-c-grep-history
  4873. (delete (car (last helm-c-grep-history))
  4874. helm-c-grep-history))))))
  4875. (defun helm-c-grep-other-window (candidate)
  4876. "Jump to result in other window from helm grep."
  4877. (helm-c-grep-action candidate 'other-window))
  4878. (defun helm-c-grep-other-frame (candidate)
  4879. "Jump to result in other frame from helm grep."
  4880. (helm-c-grep-action candidate 'other-frame))
  4881. (defun helm-c-grep-jump-elscreen (candidate)
  4882. "Jump to result in elscreen from helm grep."
  4883. (helm-c-grep-action candidate 'elscreen))
  4884. (defun helm-c-grep-save-results (_candidate)
  4885. (helm-c-grep-action _candidate 'grep))
  4886. (defun helm-c-grep-save-results-1 ()
  4887. "Save helm grep result in a `grep-mode' buffer."
  4888. (let ((buf "*grep*")
  4889. new-buf)
  4890. (when (get-buffer buf)
  4891. (setq new-buf (read-string "GrepBufferName: " buf))
  4892. (loop for b in (helm-c-buffer-list)
  4893. when (and (string= new-buf b)
  4894. (not (y-or-n-p
  4895. (format "Buffer `%s' already exists overwrite? "
  4896. new-buf))))
  4897. do (setq new-buf (read-string "GrepBufferName: " "*grep ")))
  4898. (setq buf new-buf))
  4899. (with-current-buffer (get-buffer-create buf)
  4900. (let ((inhibit-read-only t))
  4901. (erase-buffer)
  4902. (insert "-*- mode: grep -*-\n\n"
  4903. (format "Grep Results for `%s':\n\n" helm-pattern))
  4904. (save-excursion
  4905. (insert (with-current-buffer helm-buffer
  4906. (goto-char (point-min)) (forward-line 1)
  4907. (buffer-substring (point) (point-max))))
  4908. (grep-mode))))
  4909. (message "Helm Grep Results saved in `%s' buffer" buf)))
  4910. (defun helm-c-grep-persistent-action (candidate)
  4911. "Persistent action for `helm-do-grep'.
  4912. With a prefix arg record CANDIDATE in `mark-ring'."
  4913. (if current-prefix-arg
  4914. (helm-c-grep-action candidate nil 'mark)
  4915. (helm-c-grep-action candidate))
  4916. (helm-match-line-color-current-line))
  4917. (defun helm-c-grep-guess-extensions (files)
  4918. "Try to guess file extensions in FILES list when using grep recurse.
  4919. These extensions will be added to command line with --include arg of grep."
  4920. (loop
  4921. with glob-list = nil
  4922. with lst = (if (file-directory-p (car files))
  4923. (directory-files
  4924. (car files) nil
  4925. directory-files-no-dot-files-regexp)
  4926. files)
  4927. for i in lst
  4928. for ext = (file-name-extension i t)
  4929. for glob = (and ext (not (string= ext ""))
  4930. (concat "*" ext))
  4931. unless (or (not glob)
  4932. (member glob glob-list)
  4933. (member glob grep-find-ignored-files))
  4934. collect glob into glob-list
  4935. finally return glob-list))
  4936. (defun helm-do-grep-1 (only &optional recurse zgrep)
  4937. "Launch grep with a list of ONLY files.
  4938. When RECURSE is given use -r option of grep and prompt user
  4939. to set the --include args of grep.
  4940. You can give more than one arg separated by space.
  4941. e.g *.el *.py *.tex.
  4942. If it's empty --exclude `grep-find-ignored-files' is used instead."
  4943. (let* ((helm-compile-source-functions
  4944. ;; rule out helm-match-plugin because the input is one regexp.
  4945. (delq 'helm-compile-source--match-plugin
  4946. (copy-sequence helm-compile-source-functions)))
  4947. (exts (helm-c-grep-guess-extensions only))
  4948. (globs (and (not zgrep) (mapconcat 'identity exts " ")))
  4949. (include-files (and recurse (not zgrep)
  4950. (read-string "OnlyExt(*.[ext]): "
  4951. globs)))
  4952. ;; Set `minibuffer-history' AFTER includes-files
  4953. ;; to avoid storing wild-cards here.
  4954. (minibuffer-history helm-c-grep-history)
  4955. (helm-c-grep-default-command (cond ((and recurse zgrep) helm-c-default-zgrep-command)
  4956. (recurse helm-c-grep-default-recurse-command)
  4957. (zgrep helm-c-default-zgrep-command)
  4958. (t helm-c-grep-default-command)))
  4959. ;; Disable match-plugin and use here own highlighting.
  4960. (helm-mp-highlight-delay nil))
  4961. (when include-files
  4962. (setq include-files
  4963. (and (not (string= include-files ""))
  4964. (mapconcat #'(lambda (x)
  4965. (concat "--include=" (shell-quote-argument x)))
  4966. (split-string include-files) " "))))
  4967. ;; When called as action from an other source e.g *-find-files
  4968. ;; we have to kill action buffer.
  4969. (when (get-buffer helm-action-buffer)
  4970. (kill-buffer helm-action-buffer))
  4971. ;; `helm-find-files' haven't already started,
  4972. ;; give a default value to `helm-ff-default-directory'.
  4973. (setq helm-ff-default-directory (or helm-ff-default-directory
  4974. default-directory))
  4975. (helm
  4976. :sources
  4977. `(((name . "Grep")
  4978. (header-name . (lambda (name)
  4979. (concat name "(C-c ? Help)")))
  4980. (candidates
  4981. . (lambda ()
  4982. (funcall helm-c-grep-default-function only include-files zgrep)))
  4983. (filtered-candidate-transformer helm-c-grep-cand-transformer)
  4984. (candidate-number-limit . 9999)
  4985. (mode-line . helm-grep-mode-line-string)
  4986. (keymap . ,helm-c-grep-map)
  4987. (action . ,(delq
  4988. nil
  4989. `(("Find File" . helm-c-grep-action)
  4990. ("Find file other frame" . helm-c-grep-other-frame)
  4991. ,(and (locate-library "elscreen")
  4992. '("Find file in Elscreen"
  4993. . helm-c-grep-jump-elscreen))
  4994. ("Save results in grep buffer" . helm-c-grep-save-results)
  4995. ("Find file other window" . helm-c-grep-other-window))))
  4996. (persistent-action . helm-c-grep-persistent-action)
  4997. (persistent-help . "Jump to line (`C-u' Record in mark ring)")
  4998. (requires-pattern . 3)
  4999. (delayed)))
  5000. :buffer "*helm grep*")))
  5001. (defun helm-ff-zgrep-1 (flist recursive)
  5002. (unwind-protect
  5003. (let* ((def-dir (or helm-ff-default-directory
  5004. default-directory))
  5005. (only (if recursive
  5006. (or (gethash def-dir helm-c-rzgrep-cache)
  5007. (puthash
  5008. def-dir
  5009. (helm-c-walk-directory
  5010. def-dir
  5011. :directories nil
  5012. :path 'full
  5013. :match ".*\\(\.gz\\|\.bz\\|\.xz\\|\.lzma\\)$")
  5014. helm-c-rzgrep-cache))
  5015. flist)))
  5016. (when recursive (setq helm-c-zgrep-recurse-flag t))
  5017. (helm-do-grep-1 only recursive 'zgrep))
  5018. (setq helm-c-zgrep-recurse-flag nil)))
  5019. (defun helm-c-grep-split-line (line)
  5020. "Split a grep output line."
  5021. (let (beg fname lineno str)
  5022. ;; Don't print until grep line is valid.
  5023. (when (string-match "\\(.*\\)\\(:[0-9]+:\\)\\(.*\\)" line)
  5024. (with-temp-buffer
  5025. (insert line)
  5026. (goto-char (point-min))
  5027. (setq beg (point))
  5028. (forward-char 2)
  5029. (re-search-forward ":" nil t)
  5030. (setq fname (buffer-substring-no-properties beg (1- (point))))
  5031. (setq beg (point))
  5032. (re-search-forward ":" nil t)
  5033. (setq lineno (buffer-substring-no-properties beg (1- (point))))
  5034. (setq str (buffer-substring-no-properties (point) (point-at-eol))))
  5035. (list fname lineno str))))
  5036. (defun helm-c-grep-cand-transformer (candidates sources)
  5037. "Filtered candidate transformer function for `helm-do-grep'."
  5038. (loop for i in candidates
  5039. for split = (and i (helm-c-grep-split-line i))
  5040. for fname = (car split)
  5041. for lineno = (nth 1 split)
  5042. for str = (nth 2 split)
  5043. when (and fname lineno str)
  5044. collect
  5045. (cons (concat (propertize (file-name-nondirectory fname)
  5046. 'face 'helm-grep-file
  5047. 'help-echo fname) ":"
  5048. (propertize lineno 'face 'helm-grep-lineno) ":"
  5049. (helm-c-grep-highlight-match str))
  5050. i)))
  5051. (defun helm-c-grep-highlight-match (str)
  5052. "Highlight in string STR all occurences matching `helm-pattern'."
  5053. (condition-case nil
  5054. (with-temp-buffer
  5055. (insert str)
  5056. (goto-char (point-min))
  5057. (while (and (re-search-forward helm-pattern nil t)
  5058. (> (- (match-end 0) (match-beginning 0)) 0))
  5059. (add-text-properties
  5060. (match-beginning 0) (match-end 0)
  5061. '(face helm-grep-match)))
  5062. (buffer-string))
  5063. (error nil)))
  5064. ;; Go to next or precedent file (common to etags and grep).
  5065. (defun helm-c-goto-next-or-prec-file (n)
  5066. "Go to next or precedent candidate file in helm grep/etags buffers.
  5067. If N is positive go forward otherwise go backward."
  5068. (with-helm-window
  5069. (let* ((current-line-list (split-string
  5070. (buffer-substring
  5071. (point-at-bol)
  5072. (point-at-eol)) ":"))
  5073. (current-fname (nth 0 current-line-list))
  5074. (fn-b-o-f (if (eq n 1) 'eobp 'bobp)))
  5075. (catch 'break
  5076. (while (not (funcall fn-b-o-f))
  5077. (forward-line n) ; Go forward or backward depending of n value.
  5078. (unless (search-forward current-fname (point-at-eol) t)
  5079. (helm-mark-current-line)
  5080. (throw 'break nil))))
  5081. (cond ((and (eq n 1) (eobp))
  5082. (re-search-backward ".")
  5083. (forward-line 0)
  5084. (helm-mark-current-line))
  5085. ((and (< n 1) (bobp))
  5086. (forward-line 1)
  5087. (helm-mark-current-line))))))
  5088. ;;;###autoload
  5089. (defun helm-c-goto-precedent-file ()
  5090. "Go to precedent file in helm grep/etags buffers."
  5091. (interactive)
  5092. (helm-c-goto-next-or-prec-file -1))
  5093. ;;;###autoload
  5094. (defun helm-c-goto-next-file ()
  5095. "Go to precedent file in helm grep/etags buffers."
  5096. (interactive)
  5097. (helm-c-goto-next-or-prec-file 1))
  5098. ;;;###autoload
  5099. (defun helm-c-grep-run-persistent-action ()
  5100. "Run grep persistent action from `helm-do-grep-1'."
  5101. (interactive)
  5102. (helm-attrset 'jump-persistent 'helm-c-grep-persistent-action)
  5103. (helm-execute-persistent-action 'jump-persistent))
  5104. ;;;###autoload
  5105. (defun helm-c-grep-run-default-action ()
  5106. "Run grep default action from `helm-do-grep-1'."
  5107. (interactive)
  5108. (helm-c-quit-and-execute-action 'helm-c-grep-action))
  5109. ;;;###autoload
  5110. (defun helm-c-grep-run-other-window-action ()
  5111. "Run grep goto other window action from `helm-do-grep-1'."
  5112. (interactive)
  5113. (helm-c-quit-and-execute-action 'helm-c-grep-other-window))
  5114. ;;;###autoload
  5115. (defun helm-c-grep-run-save-buffer ()
  5116. "Run grep save results action from `helm-do-grep-1'."
  5117. (interactive)
  5118. (helm-c-quit-and-execute-action 'helm-c-grep-save-results))
  5119. ;; Grep buffers
  5120. (defun helm-c-grep-buffers-1 (candidate &optional zgrep)
  5121. "Run grep on all file--buffers or CANDIDATE if it is a file--buffer.
  5122. If one of selected buffers is not a file--buffer,
  5123. it is ignored and grep will run on all others file--buffers.
  5124. If only one candidate is selected and it is not a file--buffer,
  5125. switch to this buffer and run `helm-occur'.
  5126. If a prefix arg is given run grep on all buffers ignoring non--file-buffers."
  5127. (let* ((prefarg (or current-prefix-arg helm-current-prefix-arg))
  5128. (cands (if prefarg
  5129. (buffer-list)
  5130. (helm-marked-candidates)))
  5131. (win-conf (current-window-configuration))
  5132. ;; Non--fname buffers are ignored.
  5133. (bufs (loop for buf in cands
  5134. for fname = (buffer-file-name (get-buffer buf))
  5135. when fname
  5136. collect (expand-file-name fname))))
  5137. (if bufs
  5138. (if zgrep
  5139. (helm-do-grep-1 bufs nil 'zgrep)
  5140. (helm-do-grep-1 bufs))
  5141. ;; bufs is empty, thats mean we have only CANDIDATE
  5142. ;; and it is not a buffer-filename, fallback to occur.
  5143. (helm-c-switch-to-buffer candidate)
  5144. (when (get-buffer helm-action-buffer)
  5145. (kill-buffer helm-action-buffer))
  5146. (helm-occur)
  5147. (when (eq helm-exit-status 1)
  5148. (set-window-configuration win-conf)))))
  5149. (defun helm-c-grep-buffers (candidate)
  5150. "Action to grep buffers."
  5151. (helm-c-grep-buffers-1 candidate))
  5152. (defun helm-c-zgrep-buffers (candidate)
  5153. "Action to zgrep buffers."
  5154. (helm-c-grep-buffers-1 candidate 'zgrep))
  5155. ;;; Helm interface for pdfgrep
  5156. ;; pdfgrep program <http://pdfgrep.sourceforge.net/>
  5157. ;; and a pdf-reader (e.g xpdf) are needed.
  5158. ;;
  5159. (defvar helm-c-pdfgrep-default-command "pdfgrep --color never -niH %s %s")
  5160. (defvar helm-c-pdfgrep-default-function 'helm-c-pdfgrep-init)
  5161. (defvar helm-c-pdfgrep-debug-command-line nil)
  5162. (defun helm-c-pdfgrep-init (only-files)
  5163. "Start an asynchronous pdfgrep process in ONLY-FILES list."
  5164. (let* ((fnargs (helm-c-grep-prepare-candidates
  5165. (if (file-remote-p helm-ff-default-directory)
  5166. (mapcar #'(lambda (x)
  5167. (file-remote-p x 'localname))
  5168. only-files)
  5169. only-files)))
  5170. (cmd-line (format helm-c-pdfgrep-default-command
  5171. helm-pattern
  5172. fnargs)))
  5173. (when helm-c-pdfgrep-debug-command-line
  5174. (with-current-buffer (get-buffer-create "*any pdfgrep debug*")
  5175. (goto-char (point-max))
  5176. (insert (concat ">>> " cmd-line "\n\n"))))
  5177. (setq mode-line-format
  5178. '(" " mode-line-buffer-identification " "
  5179. (line-number-mode "%l") " "
  5180. (:eval (propertize "(Pdfgrep Process Running) "
  5181. 'face '((:foreground "red"))))))
  5182. (prog1
  5183. (let ((default-directory helm-ff-default-directory))
  5184. (start-file-process-shell-command "pdfgrep-process" nil cmd-line))
  5185. (message nil)
  5186. (set-process-sentinel
  5187. (get-process "pdfgrep-process")
  5188. #'(lambda (process event)
  5189. (when (string= event "finished\n")
  5190. (with-helm-window
  5191. (helm-update-move-first-line))
  5192. (force-mode-line-update nil)))))))
  5193. (defun helm-do-pdfgrep-1 (only)
  5194. "Launch pdfgrep with a list of ONLY files."
  5195. (unless (executable-find "pdfgrep")
  5196. (error "Error: No such program `pdfgrep'."))
  5197. (let* ((helm-compile-source-functions
  5198. ;; rule out helm-match-plugin because the input is one regexp.
  5199. (delq 'helm-compile-source--match-plugin
  5200. (copy-sequence helm-compile-source-functions)))
  5201. ;; Disable match-plugin and use here own highlighting.
  5202. (helm-mp-highlight-delay nil))
  5203. ;; When called as action from an other source e.g *-find-files
  5204. ;; we have to kill action buffer.
  5205. (when (get-buffer helm-action-buffer)
  5206. (kill-buffer helm-action-buffer))
  5207. ;; If `helm-find-files' haven't already started,
  5208. ;; give a default value to `helm-ff-default-directory'.
  5209. (setq helm-ff-default-directory (or helm-ff-default-directory
  5210. default-directory))
  5211. (helm
  5212. :sources
  5213. `(((name . "PdfGrep")
  5214. (candidates
  5215. . (lambda ()
  5216. (funcall helm-c-pdfgrep-default-function only)))
  5217. (filtered-candidate-transformer helm-c-grep-cand-transformer)
  5218. (candidate-number-limit . 9999)
  5219. (mode-line . helm-pdfgrep-mode-line-string)
  5220. (action . helm-c-pdfgrep-action)
  5221. (persistent-help . "Jump to PDF Page")
  5222. (requires-pattern . 3)
  5223. (delayed)))
  5224. :keymap helm-c-pdfgrep-map
  5225. :buffer "*helm grep*")))
  5226. (defun helm-c-pdfgrep-action (candidate)
  5227. (let* ((split (helm-c-grep-split-line candidate))
  5228. (pageno (nth 1 split))
  5229. (fname (car split)))
  5230. (start-file-process-shell-command
  5231. "pdf-reader" nil
  5232. (format-spec helm-c-pdfgrep-default-read-command
  5233. (list (cons ?f fname) (cons ?p pageno))))))
  5234. ;; Yank text at point.
  5235. ;;
  5236. ;;
  5237. ;; Internal
  5238. (defvar helm-yank-point nil)
  5239. ;;;###autoload
  5240. (defun helm-yank-text-at-point ()
  5241. "Yank text at point in minibuffer."
  5242. (interactive)
  5243. (let (input)
  5244. (flet ((insert-in-minibuffer (word)
  5245. (with-selected-window (minibuffer-window)
  5246. (let ((str helm-pattern))
  5247. (delete-minibuffer-contents)
  5248. (set-text-properties 0 (length word) nil word)
  5249. (insert (concat str word))))))
  5250. (with-helm-current-buffer
  5251. ;; Start to initial point if C-w have never been hit.
  5252. (unless helm-yank-point (setq helm-yank-point (point)))
  5253. (and helm-yank-point (goto-char helm-yank-point))
  5254. (forward-word 1)
  5255. (setq input (buffer-substring-no-properties helm-yank-point (point)))
  5256. (setq helm-yank-point (point))) ; End of last forward-word
  5257. (insert-in-minibuffer input))))
  5258. (defun helm-reset-yank-point ()
  5259. (setq helm-yank-point nil))
  5260. (add-hook 'helm-after-persistent-action-hook 'helm-reset-yank-point)
  5261. (add-hook 'helm-cleanup-hook 'helm-reset-yank-point)
  5262. ;;; Recentf files
  5263. ;;
  5264. ;;
  5265. (defvar helm-c-source-recentf
  5266. `((name . "Recentf")
  5267. (init . (lambda ()
  5268. (require 'recentf)
  5269. (or recentf-mode (recentf-mode 1))))
  5270. ;; Needed for filenames with capitals letters.
  5271. (disable-shortcuts)
  5272. (candidates . recentf-list)
  5273. (keymap . ,helm-generic-files-map)
  5274. (help-message . helm-generic-file-help-message)
  5275. (mode-line . helm-generic-file-mode-line-string)
  5276. (match helm-c-match-on-basename)
  5277. (type . file))
  5278. "See (info \"(emacs)File Conveniences\").
  5279. Set `recentf-max-saved-items' to a bigger value if default is too small.")
  5280. ;;; ffap
  5281. (eval-when-compile (require 'ffap))
  5282. (defvar helm-c-source-ffap-guesser
  5283. `((name . "File at point")
  5284. (init . (lambda () (require 'ffap)))
  5285. (candidates . (lambda ()
  5286. (helm-aif
  5287. (with-helm-current-buffer
  5288. (ffap-guesser))
  5289. (list it))))
  5290. (keymap . ,helm-generic-files-map)
  5291. (help-message . helm-generic-file-help-message)
  5292. (mode-line . helm-generic-file-mode-line-string)
  5293. (type . file)))
  5294. ;;; ffap with line number
  5295. (defun helm-c-ffap-file-line-at-point ()
  5296. "Get (FILENAME . LINENO) at point."
  5297. (helm-aif (let (ffap-alist) (ffap-file-at-point))
  5298. (save-excursion
  5299. (beginning-of-line)
  5300. (when (and (search-forward it nil t)
  5301. (looking-at ":\\([0-9]+\\)"))
  5302. (cons it (string-to-number (match-string 1)))))))
  5303. (defun helm-c-ffap-line-candidates ()
  5304. (with-helm-current-buffer
  5305. (helm-attrset 'ffap-line-location (helm-c-ffap-file-line-at-point)))
  5306. (helm-aif (helm-attr 'ffap-line-location)
  5307. (destructuring-bind (file . line) it
  5308. (list (cons (format "%s (line %d)" file line) file)))))
  5309. ;;; Goto line after opening file by `helm-c-source-ffap-line'.
  5310. (defun helm-c-ffap-line-goto-line ()
  5311. (when (car (helm-attr 'ffap-line-location))
  5312. (unwind-protect
  5313. (ignore-errors
  5314. (with-selected-window
  5315. (get-buffer-window
  5316. (get-file-buffer (car (helm-attr 'ffap-line-location))))
  5317. (helm-goto-line (cdr (helm-attr 'ffap-line-location)))))
  5318. (helm-attrset 'ffap-line-location nil))))
  5319. (add-hook 'helm-after-action-hook 'helm-c-ffap-line-goto-line)
  5320. (add-hook 'helm-after-persistent-action-hook 'helm-c-ffap-line-goto-line)
  5321. (defvar helm-c-source-ffap-line
  5322. `((name . "File/Lineno at point")
  5323. (init . (lambda () (require 'ffap)))
  5324. (candidates . helm-c-ffap-line-candidates)
  5325. (keymap . ,helm-map)
  5326. (type . file)))
  5327. ;;; list of files gleaned from every dired buffer
  5328. (defun helm-c-files-in-all-dired-candidates ()
  5329. (save-excursion
  5330. (mapcan
  5331. (lambda (dir)
  5332. (cond ((listp dir) ;filelist
  5333. dir)
  5334. ((equal "" (file-name-nondirectory dir)) ;dir
  5335. (directory-files dir t))
  5336. (t ;wildcard
  5337. (file-expand-wildcards dir t))))
  5338. (delq nil
  5339. (mapcar (lambda (buf)
  5340. (set-buffer buf)
  5341. (when (eq major-mode 'dired-mode)
  5342. (if (consp dired-directory)
  5343. (cdr dired-directory) ;filelist
  5344. dired-directory))) ;dir or wildcard
  5345. (buffer-list))))))
  5346. ;; (dired '("~/" "~/.emacs-custom.el" "~/.emacs.bmk"))
  5347. (defvar helm-c-source-files-in-all-dired
  5348. '((name . "Files in all dired buffer.")
  5349. (candidates . helm-c-files-in-all-dired-candidates)
  5350. (type . file)))
  5351. ;;;; <info>
  5352. ;;; Info pages
  5353. (defvar helm-c-info-pages nil
  5354. "All info pages on system.
  5355. Will be calculated the first time you invoke helm with this
  5356. source.")
  5357. (defun helm-c-info-pages-init ()
  5358. "Collect candidates for initial Info node Top."
  5359. (if helm-c-info-pages
  5360. helm-c-info-pages
  5361. (let ((info-topic-regexp "\\* +\\([^:]+: ([^)]+)[^.]*\\)\\.")
  5362. topics)
  5363. (require 'info)
  5364. (with-temp-buffer
  5365. (Info-find-node "dir" "top")
  5366. (goto-char (point-min))
  5367. (while (re-search-forward info-topic-regexp nil t)
  5368. (push (match-string-no-properties 1) topics))
  5369. (kill-buffer))
  5370. (setq helm-c-info-pages topics))))
  5371. (defvar helm-c-source-info-pages
  5372. `((name . "Info Pages")
  5373. (init . helm-c-info-pages-init)
  5374. (candidates . helm-c-info-pages)
  5375. (action . (("Show with Info" .(lambda (node-str)
  5376. (info (replace-regexp-in-string
  5377. "^[^:]+: " "" node-str))))))
  5378. (requires-pattern . 2)))
  5379. ;;; Man and woman UI
  5380. ;;
  5381. ;;
  5382. (defvar helm-c-man-pages nil
  5383. "All man pages on system.
  5384. Will be calculated the first time you invoke helm with this
  5385. source.")
  5386. (defun helm-c-man-default-action (candidate)
  5387. "Default action for jumping to a woman or man page from helm."
  5388. (let ((wfiles (woman-file-name-all-completions candidate)))
  5389. (condition-case err
  5390. (if (> (length wfiles) 1)
  5391. (woman-find-file
  5392. (helm-comp-read
  5393. "ManFile: " wfiles :must-match t))
  5394. (woman candidate))
  5395. ;; If woman is unable to format correctly
  5396. ;; use man instead.
  5397. (error (kill-buffer) ; Kill woman buffer.
  5398. (let ((Man-notify-method 'meek))
  5399. (Man-getpage-in-background candidate))))))
  5400. (defvar helm-c-source-man-pages
  5401. `((name . "Manual Pages")
  5402. (candidates . (lambda ()
  5403. (if helm-c-man-pages
  5404. helm-c-man-pages
  5405. ;; XEmacs doesn't have a woman :)
  5406. (setq helm-c-man-pages
  5407. (ignore-errors
  5408. (require 'woman)
  5409. (woman-file-name "")
  5410. (sort (mapcar 'car woman-topic-all-completions)
  5411. 'string-lessp))))))
  5412. (action ("Show with Woman" . helm-c-man-default-action))
  5413. ;; Woman does not work OS X
  5414. ;; http://xahlee.org/emacs/modernization_man_page.html
  5415. (action-transformer . (lambda (actions candidate)
  5416. (if (eq system-type 'darwin)
  5417. '(("Show with Man" . man))
  5418. actions)))
  5419. (requires-pattern . 2)))
  5420. ;;;; <Command>
  5421. ;;; Helm M-x - Enhanced M-x UI
  5422. ;;
  5423. ;;
  5424. ;; Another replacement of `M-x' that act exactly like the
  5425. ;; vanilla Emacs one, no problem of windows configuration, prefix args
  5426. ;; can be passed before calling `M-x' (e.g C-u M-x..) but also during
  5427. ;; helm invocation.
  5428. ;; Documentation of commands available without quitting,
  5429. ;; Show keybindings of commands.
  5430. ;; Show history.
  5431. (defvar helm-M-x-input-history nil)
  5432. (defun* helm-M-x-get-major-mode-command-alist (mode-map)
  5433. "Return alist of MODE-MAP."
  5434. (loop for key being the key-seqs of mode-map using (key-bindings com)
  5435. for str-key = (key-description key)
  5436. for ismenu = (string-match "<menu-bar>" str-key)
  5437. unless ismenu collect (cons str-key com)))
  5438. (defun helm-get-mode-map-from-mode (mode)
  5439. "Guess the mode-map name according to MODE.
  5440. Some modes don't use conventional mode-map name
  5441. so we need to guess mode-map name. e.g python-mode ==> py-mode-map.
  5442. Return nil if no mode-map found."
  5443. (loop
  5444. ;; Start with a conventional mode-map name.
  5445. with mode-map = (intern-soft (format "%s-map" mode))
  5446. with mode-string = (symbol-name mode)
  5447. with mode-name = (replace-regexp-in-string "-mode" "" mode-string)
  5448. while (not mode-map)
  5449. for count downfrom (length mode-name)
  5450. ;; Return when no result after parsing entire string.
  5451. when (eq count 0) return nil
  5452. for sub-name = (substring mode-name 0 count)
  5453. do (setq mode-map (intern-soft (format "%s-map" (concat sub-name "-mode"))))
  5454. finally return mode-map))
  5455. (defun helm-M-x-current-mode-map-alist ()
  5456. "Return mode-map alist of current `major-mode'."
  5457. (let ((map (helm-get-mode-map-from-mode major-mode)))
  5458. (when (and map (boundp map))
  5459. (helm-M-x-get-major-mode-command-alist (symbol-value map)))))
  5460. (defun helm-M-x-transformer (candidates sources)
  5461. "filtered-candidate-transformer to show bindings in emacs commands.
  5462. Show global bindings and local bindings according to current `major-mode'."
  5463. (with-helm-current-buffer
  5464. (loop with local-map = (helm-M-x-current-mode-map-alist)
  5465. for cand in candidates
  5466. for local-key = (car (rassq cand local-map))
  5467. for key = (substitute-command-keys (format "\\[%s]" cand))
  5468. collect
  5469. (cons (cond ((and (string-match "^M-x" key) local-key)
  5470. (format "%s (%s)"
  5471. cand (propertize
  5472. local-key
  5473. 'face 'helm-M-x-key-face)))
  5474. ((string-match "^M-x" key) cand)
  5475. (t (format "%s (%s)"
  5476. cand (propertize
  5477. key
  5478. 'face 'helm-M-x-key-face))))
  5479. cand) into ls
  5480. finally return
  5481. (sort ls #'(lambda (x y) (string-lessp (car x) (car y)))))))
  5482. ;;; Complex command history
  5483. ;;
  5484. ;;
  5485. (defvar helm-c-source-complex-command-history
  5486. '((name . "Complex Command History")
  5487. (candidates . (lambda () (mapcar 'prin1-to-string command-history)))
  5488. (type . sexp)))
  5489. ;;; M-x history (not related to `helm-M-x')
  5490. ;;
  5491. ;;
  5492. (defvar helm-c-source-extended-command-history
  5493. '((name . "Emacs Commands History")
  5494. (candidates
  5495. . (lambda ()
  5496. (helm-fast-remove-dups extended-command-history :test 'equal)))
  5497. (type . command)))
  5498. ;;; Emacs commands (Basic source for emacs commands)
  5499. ;;
  5500. ;;
  5501. (defvar helm-c-source-emacs-commands
  5502. '((name . "Emacs Commands")
  5503. (candidates . (lambda ()
  5504. (let (commands)
  5505. (mapatoms (lambda (a)
  5506. (if (commandp a)
  5507. (push (symbol-name a)
  5508. commands))))
  5509. (sort commands 'string-lessp))))
  5510. (type . command)
  5511. (requires-pattern . 2))
  5512. "Source for completing and invoking Emacs commands.
  5513. A command is a function with interactive spec that can
  5514. be invoked with `M-x'.
  5515. To get non-interactive functions listed, use
  5516. `helm-c-source-emacs-functions'.")
  5517. ;;;; <Function>
  5518. ;;; Emacs functions
  5519. ;;
  5520. ;;
  5521. (defvar helm-c-source-emacs-functions
  5522. '((name . "Emacs Functions")
  5523. (candidates . (lambda ()
  5524. (let (commands)
  5525. (mapatoms (lambda (a)
  5526. (if (functionp a)
  5527. (push (symbol-name a) commands))))
  5528. (sort commands 'string-lessp))))
  5529. (type . function)
  5530. (requires-pattern . 2))
  5531. "Source for completing Emacs functions.")
  5532. ;;; With abbrev expansion
  5533. ;;; Similar to my exec-abbrev-cmd.el
  5534. ;;; See http://www.tsdh.de/cgi-bin/wiki.pl/exec-abbrev-cmd.el
  5535. (defvar helm-c-function-abbrev-regexp nil
  5536. "The regexp for `helm-c-source-emacs-functions-with-abbrevs'.
  5537. Regexp built from the current `helm-pattern' interpreting it
  5538. as abbreviation.
  5539. Only for internal use.")
  5540. (defun helm-c-match-function-by-abbrev (candidate)
  5541. "Return non-nil if `helm-pattern' is an abbreviation of the function CANDIDATE.
  5542. Abbreviations are made by taking the first character from each
  5543. word in the function's name, e.g. \"bb\" is an abbrev for
  5544. `bury-buffer', \"stb\" is an abbrev for `helm-c-switch-to-buffer'."
  5545. (string-match helm-c-function-abbrev-regexp candidate))
  5546. (defvar helm-c-source-emacs-functions-with-abbrevs
  5547. (append helm-c-source-emacs-functions
  5548. '((match helm-c-match-function-by-abbrev
  5549. helm-c-string-match))
  5550. '((init
  5551. . (lambda ()
  5552. (defadvice helm-update
  5553. (before helm-c-update-function-abbrev-regexp activate)
  5554. (let ((char-list (append helm-pattern nil))
  5555. (str "^"))
  5556. (dolist (c char-list)
  5557. (setq str (concat str (list c) "[^-]*-")))
  5558. (setq str (concat (substring str 0 (1- (length str))) "$"))
  5559. (setq helm-c-function-abbrev-regexp str))))))))
  5560. (defvar helm-c-source-advice
  5561. '((name . "Function Advice")
  5562. (candidates . helm-c-advice-candidates)
  5563. (action ("Toggle Enable/Disable" . helm-c-advice-toggle))
  5564. (persistent-action . helm-c-advice-persistent-action)
  5565. (multiline)
  5566. (persistent-help . "Describe function / C-u C-z: Toggle advice")))
  5567. ;; (let ((debug-on-signal t))(helm 'helm-c-source-advice))
  5568. ;; (testadvice)
  5569. (defun helm-c-advice-candidates ()
  5570. (require 'advice)
  5571. (loop for (fname) in ad-advised-functions
  5572. for function = (intern fname)
  5573. append
  5574. (loop for class in ad-advice-classes append
  5575. (loop for advice in (ad-get-advice-info-field function class)
  5576. for enabled = (ad-advice-enabled advice)
  5577. collect
  5578. (cons (format
  5579. "%s %s %s"
  5580. (if enabled "Enabled " "Disabled")
  5581. (propertize fname 'face 'font-lock-function-name-face)
  5582. (ad-make-single-advice-docstring advice class nil))
  5583. (list function class advice))))))
  5584. (defun helm-c-advice-persistent-action (func-class-advice)
  5585. (if current-prefix-arg
  5586. (helm-c-advice-toggle func-class-advice)
  5587. (describe-function (car func-class-advice))))
  5588. (defun helm-c-advice-toggle (func-class-advice)
  5589. (destructuring-bind (function class advice) func-class-advice
  5590. (cond ((ad-advice-enabled advice)
  5591. (ad-advice-set-enabled advice nil)
  5592. (message "Disabled"))
  5593. (t ;disabled
  5594. (ad-advice-set-enabled advice t)
  5595. (message "Enabled")))
  5596. (ad-activate function)
  5597. (and helm-in-persistent-action
  5598. (helm-c-advice-update-current-display-string))))
  5599. (defun helm-c-advice-update-current-display-string ()
  5600. (helm-edit-current-selection
  5601. (let ((newword (cond ((looking-at "Disabled") "Enabled")
  5602. ((looking-at "Enabled") "Disabled")))
  5603. realvalue)
  5604. (when newword
  5605. (delete-region (point) (progn (forward-word 1) (point)))
  5606. (insert newword)))))
  5607. ;;;; <Variable>
  5608. ;;; Emacs variables
  5609. ;;
  5610. ;;
  5611. (defvar helm-c-source-emacs-variables
  5612. '((name . "Emacs Variables")
  5613. (candidates . (lambda ()
  5614. (sort (all-completions "" obarray 'boundp) 'string-lessp)))
  5615. (type . variable)
  5616. (requires-pattern . 2))
  5617. "Source for completing Emacs variables.")
  5618. ;;; LaCarte
  5619. (defvar helm-c-source-lacarte
  5620. '((name . "Lacarte")
  5621. (init . (lambda () (require 'lacarte )))
  5622. (candidates . (lambda () (delete '(nil) (lacarte-get-overall-menu-item-alist))))
  5623. (candidate-number-limit . 9999)
  5624. (action . helm-c-call-interactively))
  5625. "Needs lacarte.el.
  5626. http://www.emacswiki.org/cgi-bin/wiki/download/lacarte.el")
  5627. ;;; Bookmarks
  5628. ;;
  5629. ;;
  5630. ;; Bind some faces for bookmarks.
  5631. (defvar helm-c-bookmarks-face1 'helm-ff-directory)
  5632. (defvar helm-c-bookmarks-face2 'helm-ff-file)
  5633. (defvar helm-c-bookmarks-face3 'helm-bookmarks-su-face)
  5634. (eval-when-compile (require 'bookmark))
  5635. (defvar helm-c-source-bookmarks
  5636. `((name . "Bookmarks")
  5637. (init . (lambda ()
  5638. (require 'bookmark)))
  5639. (candidates . bookmark-all-names)
  5640. (type . bookmark))
  5641. "See (info \"(emacs)Bookmarks\").")
  5642. ;;; bookmark-set
  5643. (defvar helm-c-source-bookmark-set
  5644. '((name . "Set Bookmark")
  5645. (dummy)
  5646. (action . bookmark-set))
  5647. "See (info \"(emacs)Bookmarks\").")
  5648. ;;; Visible Bookmarks
  5649. ;; (install-elisp "http://cvs.savannah.gnu.org/viewvc/*checkout*/bm/bm/bm.el")
  5650. ;; http://d.hatena.ne.jp/grandVin/20080911/1221114327
  5651. (defvar helm-c-source-bm
  5652. '((name . "Visible Bookmarks")
  5653. (init . helm-c-bm-init)
  5654. (candidates-in-buffer)
  5655. (type . line))
  5656. "Needs bm.el.
  5657. http://www.nongnu.org/bm/")
  5658. (defun helm-c-bm-init ()
  5659. "Init function for `helm-c-source-bm'."
  5660. (when (require 'bm nil t)
  5661. (with-no-warnings
  5662. (let ((bookmarks (bm-lists))
  5663. (buf (helm-candidate-buffer 'global)))
  5664. (dolist (bm (sort* (append (car bookmarks) (cdr bookmarks))
  5665. '< :key 'overlay-start))
  5666. (let ((start (overlay-start bm))
  5667. (end (overlay-end bm))
  5668. (annotation (or (overlay-get bm 'annotation) "")))
  5669. (unless (< (- end start) 1) ; org => (if (< (- end start) 2)
  5670. (let ((str (format "%5d: [%s]: %s\n"
  5671. (line-number-at-pos start)
  5672. annotation
  5673. (buffer-substring start (1- end)))))
  5674. (with-current-buffer buf (insert str))))))))))
  5675. ;;; Special bookmarks
  5676. (defvar helm-c-source-bookmarks-ssh
  5677. '((name . "Bookmarks-ssh")
  5678. (init . (lambda ()
  5679. (require 'bookmark)))
  5680. (candidates . (lambda () (helm-c-collect-bookmarks :ssh t)))
  5681. (type . bookmark))
  5682. "See (info \"(emacs)Bookmarks\").")
  5683. (defvar helm-c-source-bookmarks-su
  5684. '((name . "Bookmarks-root")
  5685. (init . (lambda ()
  5686. (require 'bookmark)))
  5687. (candidates . (lambda () (helm-c-collect-bookmarks :su t)))
  5688. (filtered-candidate-transformer helm-c-highlight-bookmark-su)
  5689. (type . bookmark))
  5690. "See (info \"(emacs)Bookmarks\").")
  5691. (defvar helm-c-source-bookmarks-local
  5692. '((name . "Bookmarks-Local")
  5693. (init . (lambda ()
  5694. (require 'bookmark)))
  5695. (candidates . (lambda () (helm-c-collect-bookmarks :local t)))
  5696. (filtered-candidate-transformer
  5697. helm-c-adaptive-sort
  5698. helm-c-highlight-bookmark)
  5699. (type . bookmark))
  5700. "See (info \"(emacs)Bookmarks\").")
  5701. (defun* helm-c-collect-bookmarks (&key local su sudo ssh)
  5702. (let* ((lis-all (bookmark-all-names))
  5703. (lis-loc (cond (local (loop for i in lis-all
  5704. unless (string-match "^(ssh)\\|^(su)" i)
  5705. collect i))
  5706. (su (loop for i in lis-all
  5707. when (string-match "^(su)" i)
  5708. collect i))
  5709. (sudo (loop for i in lis-all
  5710. when (string-match "^(sudo)" i)
  5711. collect i))
  5712. (ssh (loop for i in lis-all
  5713. when (string-match "^(ssh)" i)
  5714. collect i)))))
  5715. (sort lis-loc 'string-lessp)))
  5716. (defun helm-c-bookmark-root-logged-p ()
  5717. (catch 'break
  5718. (dolist (i (mapcar #'buffer-name (buffer-list)))
  5719. (when (string-match (format "*tramp/%s ." helm-su-or-sudo) i)
  5720. (throw 'break t)))))
  5721. (defun helm-c-highlight-bookmark-su (files source)
  5722. (if (helm-c-bookmark-root-logged-p)
  5723. (helm-c-highlight-bookmark files source)
  5724. (helm-c-highlight-not-logged files source)))
  5725. (defun helm-c-highlight-not-logged (files source)
  5726. (loop for i in files
  5727. collect (propertize i 'face helm-c-bookmarks-face3)))
  5728. (defun helm-c-highlight-bookmark (bookmarks source)
  5729. "Used as `candidate-transformer' to colorize bookmarks.
  5730. Work both with standard Emacs bookmarks and bookmark-extensions.el."
  5731. (loop for i in bookmarks
  5732. for isfile = (bookmark-get-filename i)
  5733. for bufp = (and (fboundp 'bmkext-get-buffer-name)
  5734. (bmkext-get-buffer-name i))
  5735. for handlerp = (and (fboundp 'bookmark-get-handler)
  5736. (bookmark-get-handler i))
  5737. for isw3m = (and (fboundp 'bmkext-w3m-bookmark-p)
  5738. (bmkext-w3m-bookmark-p i))
  5739. for isgnus = (and (fboundp 'bmkext-gnus-bookmark-p)
  5740. (bmkext-gnus-bookmark-p i))
  5741. for isman = (and (fboundp 'bmkext-man-bookmark-p) ; Man
  5742. (bmkext-man-bookmark-p i))
  5743. for iswoman = (and (fboundp 'bmkext-woman-bookmark-p) ; Woman
  5744. (bmkext-woman-bookmark-p i))
  5745. for handlerp = (bookmark-get-handler i)
  5746. for isannotation = (bookmark-get-annotation i)
  5747. for isabook = (string= (bookmark-prop-get i 'type) "addressbook")
  5748. for isinfo = (eq handlerp 'Info-bookmark-jump)
  5749. ;; Add a * if bookmark have annotation
  5750. if (and isannotation (not (string-equal isannotation "")))
  5751. do (setq i (concat "*" i))
  5752. collect (cond (;; info buffers
  5753. isinfo
  5754. (propertize i 'face 'helm-bmkext-info 'help-echo isfile))
  5755. (;; w3m buffers
  5756. isw3m
  5757. (propertize i 'face 'helm-bmkext-w3m 'help-echo isfile))
  5758. (;; gnus buffers
  5759. isgnus
  5760. (propertize i 'face 'helm-bmkext-gnus 'help-echo isfile))
  5761. (;; Man Woman
  5762. (or iswoman isman)
  5763. (propertize i 'face 'helm-bmkext-man 'help-echo isfile))
  5764. (;; Addressbook
  5765. isabook
  5766. (propertize i 'face '((:foreground "Tomato"))))
  5767. (;; directories
  5768. (and isfile (file-directory-p isfile))
  5769. (propertize i 'face helm-c-bookmarks-face1 'help-echo isfile))
  5770. (;; regular files
  5771. t
  5772. (propertize i 'face 'helm-bmkext-file 'help-echo isfile)))))
  5773. (defun helm-c-bookmark-jump (candidate)
  5774. "Jump to bookmark from keyboard."
  5775. (let ((current-prefix-arg helm-current-prefix-arg))
  5776. (bookmark-jump candidate)))
  5777. ;;;###autoload
  5778. (defun helm-c-bookmark-run-jump-other-window ()
  5779. "Jump to bookmark from keyboard."
  5780. (interactive)
  5781. (helm-c-quit-and-execute-action 'bookmark-jump-other-window))
  5782. ;;;###autoload
  5783. (defun helm-c-bookmark-run-delete ()
  5784. "Delete bookmark from keyboard."
  5785. (interactive)
  5786. (when (y-or-n-p "Delete bookmark?")
  5787. (helm-c-quit-and-execute-action 'helm-delete-marked-bookmarks)))
  5788. ;;; Sources to filter bookmark-extensions bookmarks.
  5789. ;;
  5790. ;;
  5791. ;; Dependency: http://mercurial.intuxication.org/hg/emacs-bookmark-extension
  5792. ;; If you want to enable google-maps in addressbook you will need
  5793. ;; Julien Danjou google-maps-el package available here:
  5794. ;; http://julien.danjou.info/google-maps-el.html
  5795. (defun helm-c-bmkext-filter-setup-alist (fn &rest args)
  5796. "Return a filtered `bookmark-alist' sorted alphabetically."
  5797. (loop
  5798. with alist = (if args
  5799. (apply #'(lambda (x) (funcall fn x)) args)
  5800. (funcall fn))
  5801. for i in alist
  5802. for b = (car i)
  5803. collect b into sa
  5804. finally return (sort sa 'string-lessp)))
  5805. ;;;###autoload
  5806. (defun helm-c-bmkext-run-edit ()
  5807. "Run `bmkext-edit-bookmark' from keyboard."
  5808. (interactive)
  5809. (helm-c-quit-and-execute-action 'bmkext-edit-bookmark))
  5810. ;;; Addressbook.
  5811. ;;
  5812. ;;
  5813. (defvar helm-c-source-bmkext-addressbook
  5814. '((name . "Bookmark Addressbook")
  5815. (init . (lambda ()
  5816. (require 'bookmark-extensions)
  5817. (bookmark-maybe-load-default-file)))
  5818. (candidates . helm-c-bmkext-addressbook-setup-alist)
  5819. (persistent-action
  5820. . (lambda (candidate)
  5821. (let ((bmk (helm-bookmark-get-bookmark-from-name
  5822. candidate)))
  5823. (bookmark--jump-via bmk 'pop-to-buffer))))
  5824. (persistent-help . "Show contact - Prefix with C-u to append")
  5825. (filtered-candidate-transformer
  5826. helm-c-adaptive-sort
  5827. helm-c-highlight-bookmark)
  5828. (action . (("Show Contact(s)"
  5829. . (lambda (candidate)
  5830. (let* ((contacts (helm-marked-candidates))
  5831. (current-prefix-arg (or helm-current-prefix-arg
  5832. (> (length contacts) 1))))
  5833. (bookmark-jump
  5834. (helm-bookmark-get-bookmark-from-name (car contacts)))
  5835. (helm-aif (cdr contacts)
  5836. (loop for bmk in it do
  5837. (bookmark-jump
  5838. (helm-bookmark-get-bookmark-from-name bmk)))))))
  5839. ("Send Mail"
  5840. . (lambda (candidate)
  5841. (let* ((contacts (helm-marked-candidates))
  5842. (bmk (helm-bookmark-get-bookmark-from-name
  5843. (car contacts)))
  5844. (append (message-buffers)))
  5845. (if append
  5846. (addressbook-set-mail-buffer1 bmk 'append)
  5847. (addressbook-set-mail-buffer1 bmk))
  5848. (setq contacts (cdr contacts))
  5849. (when contacts
  5850. (loop for bmk in contacts do
  5851. (addressbook-set-mail-buffer1 bmk 'append))))))
  5852. ("Edit Bookmark"
  5853. . (lambda (candidate)
  5854. (let ((bmk (helm-bookmark-get-bookmark-from-name
  5855. candidate)))
  5856. (addressbook-bookmark-edit
  5857. (assoc bmk bookmark-alist)))))
  5858. ("Insert Email at point"
  5859. . (lambda (candidate)
  5860. (let* ((bmk (helm-bookmark-get-bookmark-from-name
  5861. candidate))
  5862. (mlist (split-string
  5863. (assoc-default
  5864. 'email (assoc bmk bookmark-alist))
  5865. ", ")))
  5866. (insert
  5867. (if (> (length mlist) 1)
  5868. (helm-comp-read
  5869. "Insert Mail Address: " mlist :must-match t)
  5870. (car mlist))))))
  5871. ("Show annotation"
  5872. . (lambda (candidate)
  5873. (let ((bmk (helm-bookmark-get-bookmark-from-name
  5874. candidate)))
  5875. (bookmark-show-annotation bmk))))
  5876. ("Edit annotation"
  5877. . (lambda (candidate)
  5878. (let ((bmk (helm-bookmark-get-bookmark-from-name
  5879. candidate)))
  5880. (bookmark-edit-annotation bmk))))
  5881. ("Show Google map"
  5882. . (lambda (candidate)
  5883. (let* ((bmk (helm-bookmark-get-bookmark-from-name
  5884. candidate))
  5885. (full-bmk (assoc bmk bookmark-alist)))
  5886. (addressbook-google-map full-bmk))))))))
  5887. (defun helm-c-bmkext-addressbook-setup-alist ()
  5888. "Specialized filter function for bookmarks w3m."
  5889. (helm-c-bmkext-filter-setup-alist 'bmkext-addressbook-alist-only))
  5890. ;; W3m bookmarks from bookmark-extensions.
  5891. (defvar helm-c-source-bookmark-w3m
  5892. '((name . "Bookmark W3m")
  5893. (init . (lambda ()
  5894. (require 'bookmark-extensions)
  5895. (bookmark-maybe-load-default-file)))
  5896. (candidates . helm-c-bookmark-w3m-setup-alist)
  5897. (filtered-candidate-transformer
  5898. helm-c-adaptive-sort
  5899. helm-c-highlight-bookmark)
  5900. (type . bookmark)))
  5901. (defun helm-c-bookmark-w3m-setup-alist ()
  5902. "Specialized filter function for bookmarks w3m."
  5903. (helm-c-bmkext-filter-setup-alist 'bmkext-w3m-alist-only))
  5904. ;; Images
  5905. (defvar helm-c-source-bookmark-images
  5906. '((name . "Bookmark Images")
  5907. (init . (lambda ()
  5908. (require 'bookmark-extensions)
  5909. (bookmark-maybe-load-default-file)))
  5910. (candidates . helm-c-bookmark-images-setup-alist)
  5911. (filtered-candidate-transformer
  5912. helm-c-adaptive-sort
  5913. helm-c-highlight-bookmark)
  5914. (type . bookmark)))
  5915. (defun helm-c-bookmark-images-setup-alist ()
  5916. "Specialized filter function for images bookmarks."
  5917. (helm-c-bmkext-filter-setup-alist 'bmkext-image-file-alist-only))
  5918. ;; Woman Man
  5919. (defvar helm-c-source-bookmark-man
  5920. '((name . "Bookmark Woman&Man")
  5921. (init . (lambda ()
  5922. (require 'bookmark-extensions)
  5923. (bookmark-maybe-load-default-file)))
  5924. (candidates . helm-c-bookmark-man-setup-alist)
  5925. (filtered-candidate-transformer
  5926. helm-c-adaptive-sort
  5927. helm-c-highlight-bookmark)
  5928. (type . bookmark)))
  5929. (defun helm-c-bookmark-man-setup-alist ()
  5930. "Specialized filter function for bookmarks w3m."
  5931. (append (helm-c-bmkext-filter-setup-alist 'bmkext-man-alist-only)
  5932. (helm-c-bmkext-filter-setup-alist 'bmkext-woman-alist-only)))
  5933. ;; Gnus
  5934. (defvar helm-c-source-bookmark-gnus
  5935. '((name . "Bookmark Gnus")
  5936. (init . (lambda ()
  5937. (require 'bookmark-extensions)
  5938. (bookmark-maybe-load-default-file)))
  5939. (candidates . helm-c-bookmark-gnus-setup-alist)
  5940. (filtered-candidate-transformer
  5941. helm-c-adaptive-sort
  5942. helm-c-highlight-bookmark)
  5943. (type . bookmark)))
  5944. (defun helm-c-bookmark-gnus-setup-alist ()
  5945. "Specialized filter function for bookmarks gnus."
  5946. (helm-c-bmkext-filter-setup-alist 'bmkext-gnus-alist-only))
  5947. ;; Info
  5948. (defvar helm-c-source-bookmark-info
  5949. '((name . "Bookmark Info")
  5950. (init . (lambda ()
  5951. (require 'bookmark-extensions)
  5952. (bookmark-maybe-load-default-file)))
  5953. (candidates . helm-c-bookmark-info-setup-alist)
  5954. (filtered-candidate-transformer
  5955. helm-c-adaptive-sort
  5956. helm-c-highlight-bookmark)
  5957. (type . bookmark)))
  5958. (defun helm-c-bookmark-info-setup-alist ()
  5959. "Specialized filter function for bookmarks info."
  5960. (helm-c-bmkext-filter-setup-alist 'bmkext-info-alist-only))
  5961. ;; Local Files&directories
  5962. (defvar helm-c-source-bookmark-files&dirs
  5963. '((name . "Bookmark Files&Directories")
  5964. (init . (lambda ()
  5965. (require 'bookmark-extensions)
  5966. (bookmark-maybe-load-default-file)))
  5967. (candidates . helm-c-bookmark-local-files-setup-alist)
  5968. (filtered-candidate-transformer
  5969. helm-c-adaptive-sort
  5970. helm-c-highlight-bookmark)
  5971. (type . bookmark)))
  5972. (defun helm-c-bookmark-local-files-setup-alist ()
  5973. "Specialized filter function for bookmarks locals files."
  5974. (helm-c-bmkext-filter-setup-alist 'bmkext-local-file-alist-only))
  5975. ;; Su Files&directories
  5976. (defvar helm-c-source-bookmark-su-files&dirs
  5977. '((name . "Bookmark Root-Files&Directories")
  5978. (init . (lambda ()
  5979. (require 'bookmark-extensions)
  5980. (bookmark-maybe-load-default-file)))
  5981. (candidates . helm-c-bookmark-su-files-setup-alist)
  5982. (filtered-candidate-transformer
  5983. helm-c-adaptive-sort
  5984. helm-c-highlight-bookmark-su)
  5985. (type . bookmark)))
  5986. (defun helm-c-bookmark-su-files-setup-alist ()
  5987. "Specialized filter function for bookmarks su/sudo files."
  5988. (declare (special bmkext-su-or-sudo-regexp))
  5989. (loop
  5990. with l = (helm-c-bmkext-filter-setup-alist 'bmkext-remote-file-alist-only)
  5991. for i in l
  5992. for isfile = (bookmark-get-filename i)
  5993. for istramp = (and isfile (boundp 'tramp-file-name-regexp)
  5994. (save-match-data
  5995. (string-match tramp-file-name-regexp isfile)))
  5996. for issu = (and istramp
  5997. (string-match bmkext-su-or-sudo-regexp isfile))
  5998. if issu
  5999. collect i))
  6000. ;; Ssh Files&directories
  6001. (defvar helm-c-source-bookmark-ssh-files&dirs
  6002. '((name . "Bookmark Ssh-Files&Directories")
  6003. (init . (lambda ()
  6004. (require 'bookmark-extensions)
  6005. (bookmark-maybe-load-default-file)))
  6006. (candidates . helm-c-bookmark-ssh-files-setup-alist)
  6007. (filtered-candidate-transformer . helm-c-adaptive-sort)
  6008. (type . bookmark)))
  6009. (defun helm-c-bookmark-ssh-files-setup-alist ()
  6010. "Specialized filter function for bookmarks ssh files."
  6011. (loop
  6012. with l = (helm-c-bmkext-filter-setup-alist 'bmkext-remote-file-alist-only)
  6013. for i in l
  6014. for isfile = (bookmark-get-filename i)
  6015. for istramp = (and isfile (boundp 'tramp-file-name-regexp)
  6016. (save-match-data
  6017. (string-match tramp-file-name-regexp isfile)))
  6018. for isssh = (and istramp
  6019. (string-match "/ssh:" isfile))
  6020. if isssh
  6021. collect i))
  6022. ;;; Firefox bookmarks
  6023. ;;
  6024. ;;
  6025. ;; You will have to set firefox to import bookmarks in his html file bookmarks.html.
  6026. ;; (only for firefox versions >=3)
  6027. ;; To achieve that, open about:config in firefox and double click on this line to enable value
  6028. ;; to true:
  6029. ;; user_pref("browser.bookmarks.autoExportHTML", false);
  6030. ;; You should have now:
  6031. ;; user_pref("browser.bookmarks.autoExportHTML", true);
  6032. ;; NOTE: This is also working in the same way for mozilla aka seamonkey.
  6033. (defvar helm-firefox-bookmark-url-regexp "\\(https\\|http\\|ftp\\|about\\|file\\)://[^ \"]*")
  6034. (defvar helm-firefox-bookmarks-regexp ">\\([^><]+.[^</a>]\\)")
  6035. (defun helm-get-firefox-user-init-dir ()
  6036. "Guess the default Firefox user directory name."
  6037. (let* ((moz-dir (concat (getenv "HOME") "/.mozilla/firefox/"))
  6038. (moz-user-dir
  6039. (with-current-buffer (find-file-noselect (concat moz-dir "profiles.ini"))
  6040. (goto-char (point-min))
  6041. (prog1
  6042. (when (search-forward "Path=" nil t)
  6043. (buffer-substring-no-properties (point) (point-at-eol)))
  6044. (kill-buffer)))))
  6045. (file-name-as-directory (concat moz-dir moz-user-dir))))
  6046. (defun helm-guess-firefox-bookmark-file ()
  6047. "Return the path of the Firefox bookmarks file."
  6048. (concat (helm-get-firefox-user-init-dir) "bookmarks.html"))
  6049. (defun helm-html-bookmarks-to-alist (file url-regexp bmk-regexp)
  6050. "Parse html bookmark FILE and return an alist with (title . url) as elements."
  6051. (let (bookmarks-alist url title)
  6052. (with-temp-buffer
  6053. (insert-file-contents file)
  6054. (goto-char (point-min))
  6055. (while (re-search-forward "href=\\|^ *<DT><A HREF=" nil t)
  6056. (forward-line 0)
  6057. (when (re-search-forward url-regexp nil t)
  6058. (setq url (match-string 0)))
  6059. (when (re-search-forward bmk-regexp nil t)
  6060. (setq title (match-string 1)))
  6061. (push (cons title url) bookmarks-alist)
  6062. (forward-line)))
  6063. (nreverse bookmarks-alist)))
  6064. (defvar helm-c-firefox-bookmarks-alist nil)
  6065. (defvar helm-c-source-firefox-bookmarks
  6066. '((name . "Firefox Bookmarks")
  6067. (init . (lambda ()
  6068. (setq helm-c-firefox-bookmarks-alist
  6069. (helm-html-bookmarks-to-alist
  6070. (helm-guess-firefox-bookmark-file)
  6071. helm-firefox-bookmark-url-regexp
  6072. helm-firefox-bookmarks-regexp))))
  6073. (candidates . (lambda ()
  6074. (mapcar #'car helm-c-firefox-bookmarks-alist)))
  6075. (filtered-candidate-transformer
  6076. helm-c-adaptive-sort
  6077. helm-c-highlight-firefox-bookmarks)
  6078. (action . (("Browse Url"
  6079. . (lambda (candidate)
  6080. (helm-c-browse-url
  6081. (helm-c-firefox-bookmarks-get-value candidate))))
  6082. ("Copy Url"
  6083. . (lambda (elm)
  6084. (kill-new (helm-c-w3m-bookmarks-get-value elm))))))))
  6085. (defun helm-c-firefox-bookmarks-get-value (elm)
  6086. (assoc-default elm helm-c-firefox-bookmarks-alist))
  6087. (defun helm-c-highlight-firefox-bookmarks (bookmarks source)
  6088. (loop for i in bookmarks
  6089. collect (propertize
  6090. i 'face '((:foreground "YellowGreen"))
  6091. 'help-echo (helm-c-firefox-bookmarks-get-value i))))
  6092. ;;; W3m bookmark - helm interface.
  6093. ;;
  6094. ;;
  6095. ;; Some users have the emacs-w3m library in load-path
  6096. ;; without having the w3m executable :-;
  6097. ;; So check if w3m program is present before trying to load
  6098. ;; emacs-w3m.
  6099. (eval-when-compile
  6100. (when (executable-find "w3m")
  6101. (require 'w3m-bookmark nil t)))
  6102. (defvar w3m-bookmark-file "~/.w3m/bookmark.html")
  6103. (defvar helm-w3m-bookmarks-regexp ">\\([^><]+.[^</a>]\\)")
  6104. (defvar helm-w3m-bookmark-url-regexp "\\(https\\|http\\|ftp\\|file\\)://[^>]*")
  6105. (defvar helm-c-w3m-bookmarks-alist nil)
  6106. (defvar helm-c-source-w3m-bookmarks
  6107. '((name . "W3m Bookmarks")
  6108. (init . (lambda ()
  6109. (setq helm-c-w3m-bookmarks-alist
  6110. (helm-html-bookmarks-to-alist
  6111. w3m-bookmark-file
  6112. helm-w3m-bookmark-url-regexp
  6113. helm-w3m-bookmarks-regexp))))
  6114. (candidates . (lambda ()
  6115. (mapcar #'car helm-c-w3m-bookmarks-alist)))
  6116. (filtered-candidate-transformer
  6117. helm-c-adaptive-sort
  6118. helm-c-highlight-w3m-bookmarks)
  6119. (action . (("Browse Url"
  6120. . (lambda (candidate)
  6121. (helm-c-w3m-browse-bookmark candidate)))
  6122. ("Copy Url"
  6123. . (lambda (elm)
  6124. (kill-new (helm-c-w3m-bookmarks-get-value elm))))
  6125. ("Browse Url Externally"
  6126. . (lambda (candidate)
  6127. (helm-c-w3m-browse-bookmark candidate t)))
  6128. ("Delete Bookmark"
  6129. . (lambda (candidate)
  6130. (helm-c-w3m-delete-bookmark candidate)))
  6131. ("Rename Bookmark"
  6132. . (lambda (candidate)
  6133. (helm-c-w3m-rename-bookmark candidate)))))
  6134. (persistent-action . (lambda (candidate)
  6135. (if current-prefix-arg
  6136. (helm-c-w3m-browse-bookmark candidate t)
  6137. (helm-c-w3m-browse-bookmark candidate nil t))))
  6138. (persistent-help . "Open URL with emacs-w3m in new tab / \
  6139. C-u \\[helm-execute-persistent-action]: Open URL with Firefox"))
  6140. "Needs w3m and emacs-w3m.
  6141. http://w3m.sourceforge.net/
  6142. http://emacs-w3m.namazu.org/")
  6143. (defun helm-c-w3m-bookmarks-get-value (elm)
  6144. (replace-regexp-in-string
  6145. "\"" "" (cdr (assoc elm helm-c-w3m-bookmarks-alist))))
  6146. (defun helm-c-w3m-browse-bookmark (elm &optional use-external new-tab)
  6147. (let* ((fn (if use-external 'helm-c-browse-url 'w3m-browse-url))
  6148. (arg (and (eq fn 'w3m-browse-url) new-tab)))
  6149. (funcall fn (helm-c-w3m-bookmarks-get-value elm) arg)))
  6150. (defun helm-c-highlight-w3m-bookmarks (bookmarks source)
  6151. (loop for i in bookmarks
  6152. collect (propertize
  6153. i 'face 'helm-w3m-bookmarks-face
  6154. 'help-echo (helm-c-w3m-bookmarks-get-value i))))
  6155. (defun helm-c-w3m-delete-bookmark (elm)
  6156. "Delete w3m bookmark from `w3m-bookmark-file'."
  6157. (with-current-buffer
  6158. (find-file-literally w3m-bookmark-file)
  6159. (goto-char (point-min))
  6160. (when (re-search-forward elm nil t)
  6161. (beginning-of-line)
  6162. (delete-region (point)
  6163. (line-end-position))
  6164. (delete-blank-lines))
  6165. (save-buffer)
  6166. (kill-buffer)))
  6167. (defun helm-c-w3m-rename-bookmark (elm)
  6168. "Rename w3m bookmark in `w3m-bookmark-file'."
  6169. (let* ((old-title (replace-regexp-in-string ">" "" elm))
  6170. (new-title (read-string "NewTitle: " old-title)))
  6171. (with-current-buffer
  6172. (find-file-literally w3m-bookmark-file)
  6173. (goto-char (point-min))
  6174. (when (re-search-forward (concat elm "<") nil t)
  6175. (goto-char (1- (point)))
  6176. (delete-char (- (length old-title)))
  6177. (insert new-title))
  6178. (save-buffer)
  6179. (kill-buffer))))
  6180. ;;;; <Library>
  6181. ;;; Elisp library scan
  6182. ;;
  6183. ;;
  6184. (defvar helm-c-source-elisp-library-scan
  6185. '((name . "Elisp libraries (Scan)")
  6186. (init . (helm-c-elisp-library-scan-init))
  6187. (candidates-in-buffer)
  6188. (action ("Find library"
  6189. . (lambda (candidate) (find-file (find-library-name candidate))))
  6190. ("Find library other window"
  6191. . (lambda (candidate)
  6192. (find-file-other-window (find-library-name candidate))))
  6193. ("Load library"
  6194. . (lambda (candidate) (load-library candidate))))))
  6195. (defun helm-c-elisp-library-scan-init ()
  6196. "Init helm buffer status."
  6197. (let ((helm-buffer (helm-candidate-buffer 'global))
  6198. (library-list (helm-c-elisp-library-scan-list)))
  6199. (with-current-buffer helm-buffer
  6200. (dolist (library library-list)
  6201. (insert (format "%s\n" library))))))
  6202. (defun helm-c-elisp-library-scan-list (&optional dirs string)
  6203. "Do completion for file names passed to `locate-file'.
  6204. DIRS is directory to search path.
  6205. STRING is string to match."
  6206. ;; Use `load-path' as path when ignore `dirs'.
  6207. (or dirs (setq dirs load-path))
  6208. ;; Init with blank when ignore `string'.
  6209. (or string (setq string ""))
  6210. ;; Get library list.
  6211. (let ((string-dir (file-name-directory string))
  6212. ;; File regexp that suffix match `load-file-rep-suffixes'.
  6213. (match-regexp (format "^.*\\.el%s$" (regexp-opt load-file-rep-suffixes)))
  6214. name
  6215. names)
  6216. (dolist (dir dirs)
  6217. (unless dir
  6218. (setq dir default-directory))
  6219. (if string-dir
  6220. (setq dir (expand-file-name string-dir dir)))
  6221. (when (file-directory-p dir)
  6222. (dolist (file (file-name-all-completions
  6223. (file-name-nondirectory string) dir))
  6224. ;; Suffixes match `load-file-rep-suffixes'.
  6225. (setq name (if string-dir (concat string-dir file) file))
  6226. (if (string-match match-regexp name)
  6227. (add-to-list 'names name)))))
  6228. names))
  6229. ;;;; <Programming>
  6230. ;;
  6231. ;;
  6232. ;;; Imenu
  6233. ;;
  6234. ;;
  6235. (defvar helm-c-imenu-delimiter " / ")
  6236. (defvar helm-c-imenu-index-filter nil)
  6237. (make-variable-buffer-local 'helm-c-imenu-index-filter)
  6238. (defvar helm-c-cached-imenu-alist nil)
  6239. (make-variable-buffer-local 'helm-c-cached-imenu-alist)
  6240. (defvar helm-c-cached-imenu-candidates nil)
  6241. (make-variable-buffer-local 'helm-c-cached-imenu-candidates)
  6242. (defvar helm-c-cached-imenu-tick nil)
  6243. (make-variable-buffer-local 'helm-c-cached-imenu-tick)
  6244. (eval-when-compile (require 'imenu))
  6245. (setq imenu-auto-rescan t)
  6246. (defun helm-imenu-create-candidates (entry)
  6247. "Create candidates with ENTRY."
  6248. (if (listp (cdr entry))
  6249. (mapcan
  6250. (lambda (sub)
  6251. (if (consp (cdr sub))
  6252. (mapcar
  6253. (lambda (subentry)
  6254. (concat (car entry) helm-c-imenu-delimiter subentry))
  6255. (helm-imenu-create-candidates sub))
  6256. (list (concat (car entry) helm-c-imenu-delimiter (car sub)))))
  6257. (cdr entry))
  6258. (list entry)))
  6259. (defvar helm-c-source-imenu
  6260. '((name . "Imenu")
  6261. (init . (lambda () (require 'imenu)))
  6262. (candidates . helm-c-imenu-candidates)
  6263. (persistent-action . (lambda (elm)
  6264. (helm-c-imenu-default-action elm)
  6265. (unless (fboundp 'semantic-imenu-tag-overlay)
  6266. (helm-match-line-color-current-line))))
  6267. (persistent-help . "Show this entry")
  6268. (action . helm-c-imenu-default-action))
  6269. "See (info \"(emacs)Imenu\")")
  6270. (defun helm-c-imenu-candidates ()
  6271. (with-helm-current-buffer
  6272. (let ((tick (buffer-modified-tick)))
  6273. (if (eq helm-c-cached-imenu-tick tick)
  6274. helm-c-cached-imenu-candidates
  6275. (setq imenu--index-alist nil)
  6276. (setq helm-c-cached-imenu-tick tick
  6277. helm-c-cached-imenu-candidates
  6278. (ignore-errors
  6279. (mapcan
  6280. 'helm-imenu-create-candidates
  6281. (setq helm-c-cached-imenu-alist
  6282. (let ((index (imenu--make-index-alist)))
  6283. (if helm-c-imenu-index-filter
  6284. (funcall helm-c-imenu-index-filter index)
  6285. index))))))
  6286. (setq helm-c-cached-imenu-candidates
  6287. (mapcar #'(lambda (x)
  6288. (if (stringp x)
  6289. x
  6290. (car x)))
  6291. helm-c-cached-imenu-candidates))))))
  6292. (setq imenu-default-goto-function 'imenu-default-goto-function)
  6293. (defun helm-c-imenu-default-action (elm)
  6294. "The default action for `helm-c-source-imenu'."
  6295. (let ((path (split-string elm helm-c-imenu-delimiter))
  6296. (alist helm-c-cached-imenu-alist))
  6297. (dolist (elm path)
  6298. (setq alist (assoc elm alist)))
  6299. (imenu alist)))
  6300. ;;; Ctags
  6301. ;;
  6302. ;;
  6303. (defvar helm-c-ctags-modes
  6304. '( c-mode c++-mode awk-mode csharp-mode java-mode javascript-mode lua-mode
  6305. makefile-mode pascal-mode perl-mode cperl-mode php-mode python-mode
  6306. scheme-mode sh-mode slang-mode sql-mode tcl-mode ))
  6307. (defun helm-c-source-ctags-init ()
  6308. (when (and buffer-file-name
  6309. (memq major-mode helm-c-ctags-modes)
  6310. (helm-current-buffer-is-modified))
  6311. (with-current-buffer (helm-candidate-buffer 'local)
  6312. (call-process-shell-command
  6313. (if (string-match "\\.el\\.gz$" helm-buffer-file-name)
  6314. (format "ctags -e -u -f- --language-force=lisp --fields=n =(zcat %s) "
  6315. helm-buffer-file-name)
  6316. (format "ctags -e -u -f- --fields=n %s " helm-buffer-file-name))
  6317. nil (current-buffer))
  6318. (goto-char (point-min))
  6319. (forward-line 2)
  6320. (delete-region (point-min) (point))
  6321. (loop while (and (not (eobp)) (search-forward "\001" (point-at-eol) t))
  6322. for lineno-start = (point)
  6323. for lineno = (buffer-substring
  6324. lineno-start
  6325. (1- (search-forward "," (point-at-eol) t)))
  6326. do
  6327. (beginning-of-line)
  6328. (insert (format "%5s:" lineno))
  6329. (search-forward "\177" (point-at-eol) t)
  6330. (delete-region (1- (point)) (point-at-eol))
  6331. (forward-line 1)))))
  6332. (defvar helm-c-source-ctags
  6333. '((name . "Exuberant ctags")
  6334. (init . helm-c-source-ctags-init)
  6335. (candidates-in-buffer)
  6336. (adjust)
  6337. (type . line))
  6338. "Needs Exuberant Ctags.
  6339. http://ctags.sourceforge.net/")
  6340. ;;; Etags
  6341. ;;
  6342. ;;
  6343. ;; helm-etags.el is deprecated, if this file is found,
  6344. ;; warn user at compile time.
  6345. (eval-when-compile
  6346. (when (locate-library "helm-etags.el")
  6347. (display-warning
  6348. '(helm-config)
  6349. "You are using obsolete library `helm-etags.el' and should remove it."
  6350. :warning)))
  6351. (defvar helm-c-etags-tag-file-dir nil
  6352. "Etags file directory.")
  6353. (defvar helm-c-etags-mtime-alist nil
  6354. "Store the last modification time of etags files here.")
  6355. (defvar helm-c-etags-cache (make-hash-table :test 'equal)
  6356. "Cache content of etags files used here for faster access.")
  6357. (defun helm-c-etags-get-tag-file (&optional directory)
  6358. "Return the path of etags file if found."
  6359. ;; Get tag file from `default-directory' or upper directory.
  6360. (let ((current-dir (helm-c-etags-find-tag-file-directory
  6361. (or directory default-directory))))
  6362. ;; Return nil if not find tag file.
  6363. (when current-dir
  6364. ;; Set tag file directory.
  6365. (setq helm-c-etags-tag-file-dir current-dir)
  6366. (expand-file-name helm-c-etags-tag-file-name current-dir))))
  6367. (defun helm-c-etags-find-tag-file-directory (current-dir)
  6368. "Try to find the directory containing tag file.
  6369. If not found in CURRENT-DIR search in upper directory."
  6370. (flet ((file-exists? (dir)
  6371. (let ((tag-path (expand-file-name
  6372. helm-c-etags-tag-file-name dir)))
  6373. (and (stringp tag-path)
  6374. (file-regular-p tag-path)
  6375. (file-readable-p tag-path)))))
  6376. (loop with count = 0
  6377. until (file-exists? current-dir)
  6378. ;; Return nil if outside the value of
  6379. ;; `helm-c-etags-tag-file-search-limit'.
  6380. if (= count helm-c-etags-tag-file-search-limit)
  6381. do (return nil)
  6382. ;; Or search upper directories.
  6383. else
  6384. do (incf count)
  6385. (setq current-dir (expand-file-name (concat current-dir "../")))
  6386. finally return current-dir)))
  6387. (defun helm-c-source-etags-header-name (x)
  6388. "Create header name for this helm etags session."
  6389. (concat "Etags in "
  6390. (with-helm-current-buffer
  6391. (helm-c-etags-get-tag-file))))
  6392. (defmacro helm-c-etags-create-buffer (file)
  6393. "Create the `helm-buffer' based on contents of etags tag FILE."
  6394. `(let* ((tag-fname ,file)
  6395. max
  6396. (split (with-current-buffer (find-file-noselect tag-fname)
  6397. (prog1
  6398. (split-string (buffer-string) "\n" 'omit-nulls)
  6399. (setq max (line-number-at-pos (point-max)))
  6400. (kill-buffer))))
  6401. (progress-reporter (make-progress-reporter "Loading tag file..." 0 max)))
  6402. (loop
  6403. with fname
  6404. with cand
  6405. for i in split for count from 0
  6406. for elm = (unless (string-match "^\x0c" i)
  6407. (helm-aif (string-match "\177" i)
  6408. (substring i 0 it)
  6409. i))
  6410. do (cond ((and elm (string-match "^\\(.+\\),[0-9]+" elm))
  6411. (setq fname (match-string 1 elm)))
  6412. (elm (setq cand (concat fname ": " elm)))
  6413. (t (setq cand nil)))
  6414. when cand do (progn
  6415. (insert (concat cand "\n"))
  6416. (progress-reporter-update progress-reporter count)))))
  6417. (defun helm-c-etags-init ()
  6418. "Feed `helm-buffer' using `helm-c-etags-cache' or tag file.
  6419. If no entry in cache, create one."
  6420. (let ((tagfile (helm-c-etags-get-tag-file)))
  6421. (when tagfile
  6422. (with-current-buffer (helm-candidate-buffer 'global)
  6423. (helm-aif (gethash tagfile helm-c-etags-cache)
  6424. ;; An entry is present in cache, insert it.
  6425. (insert it)
  6426. ;; No entry, create a new buffer using content of tag file (slower).
  6427. (helm-c-etags-create-buffer tagfile)
  6428. ;; Store content of buffer in cache.
  6429. (puthash tagfile (buffer-string) helm-c-etags-cache)
  6430. ;; Store or set the last modification of tag file.
  6431. (helm-aif (assoc tagfile helm-c-etags-mtime-alist)
  6432. ;; If an entry exists modify it.
  6433. (setcdr it (helm-c-etags-mtime tagfile))
  6434. ;; No entry create a new one.
  6435. (add-to-list 'helm-c-etags-mtime-alist
  6436. (cons tagfile (helm-c-etags-mtime tagfile)))))))))
  6437. (defvar helm-c-source-etags-select
  6438. '((name . "Etags")
  6439. (header-name . helm-c-source-etags-header-name)
  6440. (init . helm-c-etags-init)
  6441. (candidates-in-buffer)
  6442. (search . (helm-c-etags-search-fn))
  6443. (mode-line . helm-etags-mode-line-string)
  6444. (action . helm-c-etags-default-action)
  6445. (persistent-action . (lambda (candidate)
  6446. (helm-c-etags-default-action candidate)
  6447. (helm-match-line-color-current-line))))
  6448. "Helm source for Etags.")
  6449. (defun helm-c-etags-search-fn (pattern)
  6450. "Search function for `helm-c-source-etags-select'."
  6451. (re-search-forward
  6452. (if helm-c-etags-use-regexp-search
  6453. (format helm-c-etags-search-regexp pattern)
  6454. pattern)
  6455. nil t))
  6456. (defun helm-c-etags-default-action (candidate)
  6457. "Helm default action to jump to an etags entry."
  6458. (let* ((split (split-string candidate ": "))
  6459. (fname (expand-file-name
  6460. (car split) helm-c-etags-tag-file-dir))
  6461. (elm (cadr split)))
  6462. (find-file fname)
  6463. (goto-char (point-min))
  6464. (search-forward elm nil t)
  6465. (goto-char (match-beginning 0))))
  6466. (defun helm-c-etags-mtime (file)
  6467. "Last modification time of etags tag FILE."
  6468. (cadr (nth 5 (file-attributes file))))
  6469. (defun helm-c-etags-file-modified-p (file)
  6470. "Check if tag FILE have been modified in this session.
  6471. If FILE is nil return nil."
  6472. (let ((last-modif (and file
  6473. (assoc-default file helm-c-etags-mtime-alist))))
  6474. (and last-modif
  6475. (/= last-modif (helm-c-etags-mtime file)))))
  6476. ;;; Semantic
  6477. ;;
  6478. ;;
  6479. (defvar helm-semantic-candidates nil)
  6480. (defun helm-semantic-construct-candidates (tags depth)
  6481. (when (require 'semantic nil t)
  6482. (apply
  6483. 'append
  6484. (mapcar
  6485. (lambda (tag)
  6486. (if (listp tag)
  6487. (let ((type (semantic-tag-type tag))
  6488. (class (semantic-tag-class tag)))
  6489. (if (or (and (stringp type)
  6490. (or (string= type "class")
  6491. (string= type "namespace")))
  6492. (eq class 'function)
  6493. (eq class 'variable))
  6494. (cons (cons (concat (make-string (* depth 2) ?\s)
  6495. (semantic-format-tag-summarize tag nil t))
  6496. tag)
  6497. (helm-semantic-construct-candidates
  6498. (semantic-tag-components tag) (1+ depth)))))))
  6499. tags))))
  6500. (defun helm-semantic-default-action (candidate)
  6501. (let ((tag (cdr (assoc candidate helm-semantic-candidates))))
  6502. (semantic-go-to-tag tag)))
  6503. (defvar helm-c-source-semantic
  6504. '((name . "Semantic Tags")
  6505. (init . (lambda ()
  6506. (setq helm-semantic-candidates
  6507. (ignore-errors (helm-semantic-construct-candidates
  6508. (semantic-fetch-tags) 0)))))
  6509. (candidates . (lambda ()
  6510. (if helm-semantic-candidates
  6511. (mapcar 'car helm-semantic-candidates))))
  6512. (persistent-action . (lambda (elm)
  6513. (helm-semantic-default-action elm)
  6514. (helm-match-line-color-current-line)))
  6515. (persistent-help . "Show this entry")
  6516. (action . helm-semantic-default-action)
  6517. "Needs semantic in CEDET.
  6518. http://cedet.sourceforge.net/semantic.shtml
  6519. http://cedet.sourceforge.net/"))
  6520. ;;; Helm interface of `simple-call-tree.el'.
  6521. ;;
  6522. ;; <http://www.emacswiki.org/cgi-bin/wiki/download/simple-call-tree.el>
  6523. ;;
  6524. ;; Function is called by
  6525. (defvar helm-c-source-simple-call-tree-functions-callers
  6526. '((name . "Function is called by")
  6527. (init . helm-c-simple-call-tree-functions-callers-init)
  6528. (multiline)
  6529. (candidates . helm-c-simple-call-tree-candidates)
  6530. (persistent-action . helm-c-simple-call-tree-persistent-action)
  6531. (persistent-help . "Show function definitions by rotation")
  6532. (action ("Find definition selected by persistent-action" .
  6533. helm-c-simple-call-tree-find-definition)))
  6534. "Needs simple-call-tree.el.
  6535. http://www.emacswiki.org/cgi-bin/wiki/download/simple-call-tree.el")
  6536. (defvar helm-c-simple-call-tree-tick nil)
  6537. (make-variable-buffer-local 'helm-c-simple-call-tree-tick)
  6538. (defun helm-c-simple-call-tree-analyze-maybe ()
  6539. (unless (eq (buffer-chars-modified-tick) helm-c-simple-call-tree-tick)
  6540. (simple-call-tree-analyze)
  6541. (setq helm-c-simple-call-tree-tick (buffer-chars-modified-tick))))
  6542. (defun helm-c-simple-call-tree-init-base (function message)
  6543. (require 'simple-call-tree)
  6544. (with-no-warnings
  6545. (when (helm-current-buffer-is-modified)
  6546. (helm-c-simple-call-tree-analyze-maybe)
  6547. (let ((list (funcall function simple-call-tree-alist)))
  6548. (with-current-buffer (helm-candidate-buffer 'local)
  6549. (dolist (entry list)
  6550. (let ((funcs (concat " " (mapconcat #'identity (cdr entry) "\n "))))
  6551. (insert (car entry) message
  6552. (if (string= funcs " ")
  6553. " no functions."
  6554. funcs)
  6555. "\n\n"))))))))
  6556. (defun helm-c-simple-call-tree-functions-callers-init ()
  6557. (helm-c-simple-call-tree-init-base 'simple-call-tree-invert
  6558. " is called by\n"))
  6559. (defun helm-c-simple-call-tree-candidates ()
  6560. (with-current-buffer (helm-candidate-buffer)
  6561. (split-string (buffer-string) "\n\n")))
  6562. (defvar helm-c-simple-call-tree-related-functions nil)
  6563. (defvar helm-c-simple-call-tree-function-index 0)
  6564. (defun helm-c-simple-call-tree-persistent-action (candidate)
  6565. (unless (eq last-command 'helm-execute-persistent-action)
  6566. (setq helm-c-simple-call-tree-related-functions
  6567. (delete "no functions."
  6568. (split-string
  6569. (replace-regexp-in-string " \\| is called by\\| calls "
  6570. "" candidate)
  6571. "\n")))
  6572. (setq helm-c-simple-call-tree-function-index -1))
  6573. (incf helm-c-simple-call-tree-function-index)
  6574. (helm-c-simple-call-tree-find-definition candidate))
  6575. (defun helm-c-simple-call-tree-find-definition (candidate)
  6576. (find-function
  6577. (intern
  6578. (nth (mod helm-c-simple-call-tree-function-index
  6579. (length helm-c-simple-call-tree-related-functions))
  6580. helm-c-simple-call-tree-related-functions))))
  6581. ;;; Function calls
  6582. (defvar helm-c-source-simple-call-tree-callers-functions
  6583. '((name . "Function calls")
  6584. (init . helm-c-simple-call-tree-callers-functions-init)
  6585. (multiline)
  6586. (candidates . helm-c-simple-call-tree-candidates)
  6587. (persistent-action . helm-c-simple-call-tree-persistent-action)
  6588. (persistent-help . "Show function definitions by rotation")
  6589. (action ("Find definition selected by persistent-action" .
  6590. helm-c-simple-call-tree-find-definition)))
  6591. "Needs simple-call-tree.el.
  6592. http://www.emacswiki.org/cgi-bin/wiki/download/simple-call-tree.el")
  6593. (defun helm-c-simple-call-tree-callers-functions-init ()
  6594. (helm-c-simple-call-tree-init-base 'identity " calls \n"))
  6595. ;;; Helm UI of auto-document.el
  6596. ;;
  6597. ;; <http://www.emacswiki.org/cgi-bin/wiki/download/auto-document.el>
  6598. ;;
  6599. ;; Commands/Options with doc
  6600. (defvar helm-c-auto-document-data nil)
  6601. (make-variable-buffer-local 'helm-c-auto-document-data)
  6602. (defvar helm-c-source-commands-and-options-in-file
  6603. '((name . "Commands/Options in file")
  6604. (header-name
  6605. . (lambda (x) (format "Commands/Options in %s"
  6606. (buffer-local-value 'buffer-file-name
  6607. helm-current-buffer))))
  6608. (candidates . helm-command-and-options-candidates)
  6609. (multiline)
  6610. (action . imenu))
  6611. "List Commands and Options with doc. It needs auto-document.el .
  6612. http://www.emacswiki.org/cgi-bin/wiki/download/auto-document.el")
  6613. (eval-when-compile (require 'auto-document nil t))
  6614. (defun helm-command-and-options-candidates ()
  6615. (with-helm-current-buffer
  6616. (when (and (require 'auto-document nil t)
  6617. (eq major-mode 'emacs-lisp-mode)
  6618. (or (helm-current-buffer-is-modified)
  6619. (not helm-c-auto-document-data)))
  6620. (or imenu--index-alist (imenu--make-index-alist t))
  6621. (setq helm-c-auto-document-data
  6622. (destructuring-bind (commands options)
  6623. (adoc-construct helm-current-buffer)
  6624. (append
  6625. (loop for (command . doc) in commands
  6626. for cmdname = (symbol-name command)
  6627. collect
  6628. (cons
  6629. (format "Command: %s\n %s"
  6630. (propertize cmdname 'face font-lock-function-name-face)
  6631. (adoc-first-line doc))
  6632. (assoc cmdname imenu--index-alist)))
  6633. (loop with var-alist = (cdr (assoc "Variables" imenu--index-alist))
  6634. for (option doc default) in options
  6635. for optname = (symbol-name option)
  6636. collect
  6637. (cons
  6638. (format "Option: %s\n %s\n default = %s"
  6639. (propertize optname 'face font-lock-variable-name-face)
  6640. (adoc-first-line doc)
  6641. (adoc-prin1-to-string default))
  6642. (assoc optname
  6643. var-alist)))))))
  6644. helm-c-auto-document-data))
  6645. ;;;; <Color and Face>
  6646. ;;
  6647. ;;; Customize Face
  6648. ;;
  6649. ;;
  6650. (defvar helm-c-source-customize-face
  6651. '((name . "Customize Face")
  6652. (init . (lambda ()
  6653. (unless (helm-candidate-buffer)
  6654. (save-selected-window
  6655. (list-faces-display))
  6656. (helm-candidate-buffer (get-buffer "*Faces*")))))
  6657. (candidates-in-buffer)
  6658. (get-line . buffer-substring)
  6659. (action . (lambda (line)
  6660. (customize-face (intern (car (split-string line))))))
  6661. (requires-pattern . 3))
  6662. "See (info \"(emacs)Faces\")")
  6663. ;;; Colors browser
  6664. ;;
  6665. ;;
  6666. (defvar helm-c-source-colors
  6667. '((name . "Colors")
  6668. (init . (lambda () (unless (helm-candidate-buffer)
  6669. (save-selected-window
  6670. (list-colors-display))
  6671. (helm-candidate-buffer (get-buffer "*Colors*")))))
  6672. (candidates-in-buffer)
  6673. (get-line . buffer-substring)
  6674. (action
  6675. ("Copy Name" . (lambda (candidate)
  6676. (kill-new (helm-c-colors-get-name candidate))))
  6677. ("Copy RGB" . (lambda (candidate)
  6678. (kill-new (helm-c-colors-get-rgb candidate))))
  6679. ("Insert Name" . (lambda (candidate)
  6680. (with-helm-current-buffer
  6681. (insert (helm-c-colors-get-name candidate)))))
  6682. ("Insert RGB" . (lambda (candidate)
  6683. (with-helm-current-buffer
  6684. (insert (helm-c-colors-get-rgb candidate))))))))
  6685. (defun helm-c-colors-get-name (candidate)
  6686. "Get color name."
  6687. (replace-regexp-in-string
  6688. " " ""
  6689. (with-temp-buffer
  6690. (insert (capitalize candidate))
  6691. (goto-char (point-min))
  6692. (search-forward-regexp "\\s-\\{2,\\}")
  6693. (delete-region (point) (point-max))
  6694. (buffer-string))))
  6695. (defun helm-c-colors-get-rgb (candidate)
  6696. "Get color RGB."
  6697. (replace-regexp-in-string
  6698. " " ""
  6699. (with-temp-buffer
  6700. (insert (capitalize candidate))
  6701. (goto-char (point-max))
  6702. (search-backward-regexp "\\s-\\{2,\\}")
  6703. (delete-region (point) (point-min))
  6704. (buffer-string))))
  6705. ;;;; <Search Engine>
  6706. ;;; Tracker desktop search
  6707. (defvar helm-c-source-tracker-search
  6708. '((name . "Tracker Search")
  6709. (candidates . (lambda ()
  6710. (start-process "tracker-search-process" nil
  6711. "tracker-search"
  6712. helm-pattern)))
  6713. (type . file)
  6714. (requires-pattern . 3)
  6715. (delayed))
  6716. "Source for retrieving files matching the current input pattern
  6717. with the tracker desktop search.")
  6718. ;;; Spotlight (MacOS X desktop search)
  6719. (defvar helm-c-source-mac-spotlight
  6720. '((name . "mdfind")
  6721. (candidates
  6722. . (lambda () (start-process "mdfind-process" nil "mdfind" helm-pattern)))
  6723. (type . file)
  6724. (requires-pattern . 3)
  6725. (delayed))
  6726. "Source for retrieving files via Spotlight's command line
  6727. utility mdfind.")
  6728. ;;; Picklist
  6729. (defvar helm-c-source-picklist
  6730. '((name . "Picklist")
  6731. (candidates . (lambda () (mapcar 'car picklist-list)))
  6732. (type . file)))
  6733. ;;; Kill ring
  6734. ;;
  6735. ;;
  6736. (defvar helm-c-source-kill-ring
  6737. `((name . "Kill Ring")
  6738. (init . (lambda () (helm-attrset 'last-command last-command)))
  6739. (candidates . helm-c-kill-ring-candidates)
  6740. (filtered-candidate-transformer helm-c-kill-ring-transformer)
  6741. (action . helm-c-kill-ring-action)
  6742. (keymap . ,helm-kill-ring-map)
  6743. (last-command)
  6744. (migemo)
  6745. (multiline))
  6746. "Source for browse and insert contents of kill-ring.")
  6747. (defun helm-c-kill-ring-candidates ()
  6748. (loop for kill in (helm-fast-remove-dups kill-ring :test 'equal)
  6749. unless (or (< (length kill) helm-kill-ring-threshold)
  6750. (string-match "^[\\s\\t]+$" kill))
  6751. collect kill))
  6752. (defun helm-c-kill-ring-transformer (candidates source)
  6753. "Display only the `helm-c-kill-ring-max-lines-number' lines of candidate."
  6754. (loop for i in candidates
  6755. for nlines = (with-temp-buffer (insert i) (count-lines (point-min) (point-max)))
  6756. if (and helm-c-kill-ring-max-lines-number
  6757. (> nlines helm-c-kill-ring-max-lines-number))
  6758. collect (cons
  6759. (with-temp-buffer
  6760. (insert i)
  6761. (goto-char (point-min))
  6762. (concat
  6763. (buffer-substring
  6764. (point-min)
  6765. (save-excursion
  6766. (forward-line helm-c-kill-ring-max-lines-number)
  6767. (point)))
  6768. "[...]")) i)
  6769. else collect i))
  6770. (defun helm-c-kill-ring-action (str)
  6771. "Insert STR in `kill-ring' and set STR to the head.
  6772. If this action is executed just after `yank',
  6773. replace with STR as yanked string."
  6774. (setq kill-ring (delete str kill-ring))
  6775. (if (not (eq (helm-attr 'last-command) 'yank))
  6776. (insert-for-yank str)
  6777. ;; from `yank-pop'
  6778. (let ((inhibit-read-only t)
  6779. (before (< (point) (mark t))))
  6780. (if before
  6781. (funcall (or yank-undo-function 'delete-region) (point) (mark t))
  6782. (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
  6783. (setq yank-undo-function nil)
  6784. (set-marker (mark-marker) (point) (current-buffer))
  6785. (insert-for-yank str)
  6786. ;; Set the window start back where it was in the yank command,
  6787. ;; if possible.
  6788. (set-window-start (selected-window) yank-window-start t)
  6789. (if before
  6790. ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  6791. ;; It is cleaner to avoid activation, even though the command
  6792. ;; loop would deactivate the mark because we inserted text.
  6793. (goto-char (prog1 (mark t)
  6794. (set-marker (mark-marker) (point) (current-buffer)))))))
  6795. (kill-new str))
  6796. ;;;; <Mark ring>
  6797. ;; DO NOT include these sources in `helm-sources' use
  6798. ;; the commands `helm-mark-ring', `helm-global-mark-ring' or
  6799. ;; `helm-all-mark-rings' instead.
  6800. (defun helm-c-source-mark-ring-candidates ()
  6801. (flet ((get-marks (pos)
  6802. (save-excursion
  6803. (goto-char pos)
  6804. (beginning-of-line)
  6805. (let ((line (car (split-string (thing-at-point 'line) "[\n\r]"))))
  6806. (when (string= "" line)
  6807. (setq line "<EMPTY LINE>"))
  6808. (format "%7d: %s" (line-number-at-pos) line)))))
  6809. (with-helm-current-buffer
  6810. (loop
  6811. with marks = (if (mark) (cons (mark-marker) mark-ring) mark-ring)
  6812. with recip = nil
  6813. for i in marks
  6814. for m = (get-marks i)
  6815. unless (member m recip)
  6816. collect m into recip
  6817. finally return recip))))
  6818. (defvar helm-mark-ring-cache nil)
  6819. (defvar helm-c-source-mark-ring
  6820. '((name . "mark-ring")
  6821. (init . (lambda ()
  6822. (setq helm-mark-ring-cache
  6823. (ignore-errors (helm-c-source-mark-ring-candidates)))))
  6824. (candidates . (lambda ()
  6825. (helm-aif helm-mark-ring-cache
  6826. it)))
  6827. (action . (("Goto line"
  6828. . (lambda (candidate)
  6829. (helm-goto-line (string-to-number candidate))))))
  6830. (persistent-action . (lambda (candidate)
  6831. (helm-goto-line (string-to-number candidate))
  6832. (helm-match-line-color-current-line)))
  6833. (persistent-help . "Show this line")))
  6834. ;;; Global-mark-ring
  6835. (defvar helm-c-source-global-mark-ring
  6836. '((name . "global-mark-ring")
  6837. (candidates . helm-c-source-global-mark-ring-candidates)
  6838. (action . (("Goto line"
  6839. . (lambda (candidate)
  6840. (let ((items (split-string candidate ":")))
  6841. (helm-c-switch-to-buffer (second items))
  6842. (helm-goto-line (string-to-number (car items))))))))
  6843. (persistent-action . (lambda (candidate)
  6844. (let ((items (split-string candidate ":")))
  6845. (helm-c-switch-to-buffer (second items))
  6846. (helm-goto-line (string-to-number (car items)))
  6847. (helm-match-line-color-current-line))))
  6848. (persistent-help . "Show this line")))
  6849. (defun helm-c-source-global-mark-ring-candidates ()
  6850. (flet ((buf-fn (m)
  6851. (with-current-buffer (marker-buffer m)
  6852. (goto-char m)
  6853. (beginning-of-line)
  6854. (let (line)
  6855. (if (string= "" line)
  6856. (setq line "<EMPTY LINE>")
  6857. (setq line (car (split-string (thing-at-point 'line)
  6858. "[\n\r]"))))
  6859. (format "%7d:%s: %s"
  6860. (line-number-at-pos) (marker-buffer m) line)))))
  6861. (loop
  6862. with marks = global-mark-ring
  6863. with recip = nil
  6864. for i in marks
  6865. for gm = (unless (or (string-match
  6866. "^ " (format "%s" (marker-buffer i)))
  6867. (null (marker-buffer i)))
  6868. (buf-fn i))
  6869. when (and gm (not (member gm recip)))
  6870. collect gm into recip
  6871. finally return recip)))
  6872. ;;;; <Register>
  6873. ;;; Insert from register
  6874. (defvar helm-c-source-register
  6875. '((name . "Registers")
  6876. (candidates . helm-c-register-candidates)
  6877. (action-transformer . helm-c-register-action-transformer)
  6878. (multiline)
  6879. (action))
  6880. "See (info \"(emacs)Registers\")")
  6881. (defun helm-c-register-candidates ()
  6882. "Collecting register contents and appropriate commands."
  6883. (loop for (char . val) in register-alist
  6884. for key = (single-key-description char)
  6885. for string-actions =
  6886. (cond
  6887. ((numberp val)
  6888. (list (int-to-string val)
  6889. 'insert-register
  6890. 'increment-register))
  6891. ((markerp val)
  6892. (let ((buf (marker-buffer val)))
  6893. (if (null buf)
  6894. (list "a marker in no buffer")
  6895. (list (concat
  6896. "a buffer position:"
  6897. (buffer-name buf)
  6898. ", position "
  6899. (int-to-string (marker-position val)))
  6900. 'jump-to-register
  6901. 'insert-register))))
  6902. ((and (consp val) (window-configuration-p (car val)))
  6903. (list "window configuration."
  6904. 'jump-to-register))
  6905. ((and (consp val) (frame-configuration-p (car val)))
  6906. (list "frame configuration."
  6907. 'jump-to-register))
  6908. ((and (consp val) (eq (car val) 'file))
  6909. (list (concat "file:"
  6910. (prin1-to-string (cdr val))
  6911. ".")
  6912. 'jump-to-register))
  6913. ((and (consp val) (eq (car val) 'file-query))
  6914. (list (concat "file:a file-query reference: file "
  6915. (car (cdr val))
  6916. ", position "
  6917. (int-to-string (car (cdr (cdr val))))
  6918. ".")
  6919. 'jump-to-register))
  6920. ((consp val)
  6921. (let ((lines (format "%4d" (length val))))
  6922. (list (format "%s: %s\n" lines
  6923. (truncate-string-to-width
  6924. (mapconcat 'identity (list (car val))
  6925. "^J") (- (window-width) 15)))
  6926. 'insert-register)))
  6927. ((stringp val)
  6928. (list
  6929. ;; without properties
  6930. (concat (substring-no-properties
  6931. val 0 (min (length val) helm-c-register-max-offset))
  6932. (if (> (length val) helm-c-register-max-offset)
  6933. "[...]" ""))
  6934. 'insert-register
  6935. 'append-to-register
  6936. 'prepend-to-register))
  6937. ((vectorp val)
  6938. (list
  6939. "Undo-tree entry."
  6940. 'undo-tree-restore-state-from-register))
  6941. (t
  6942. "GARBAGE!"))
  6943. collect (cons (format "register %3s: %s" key (car string-actions))
  6944. (cons char (cdr string-actions)))))
  6945. (defun helm-c-register-action-transformer (actions register-and-functions)
  6946. "Decide actions by the contents of register."
  6947. (loop with func-actions =
  6948. '((insert-register
  6949. "Insert Register" .
  6950. (lambda (c) (insert-register (car c))))
  6951. (jump-to-register
  6952. "Jump to Register" .
  6953. (lambda (c) (jump-to-register (car c))))
  6954. (append-to-register
  6955. "Append Region to Register" .
  6956. (lambda (c) (append-to-register
  6957. (car c) (region-beginning) (region-end))))
  6958. (prepend-to-register
  6959. "Prepend Region to Register" .
  6960. (lambda (c) (prepend-to-register
  6961. (car c) (region-beginning) (region-end))))
  6962. (increment-register
  6963. "Increment Prefix Arg to Register" .
  6964. (lambda (c) (increment-register
  6965. helm-current-prefix-arg (car c))))
  6966. (undo-tree-restore-state-from-register
  6967. "Restore Undo-tree register"
  6968. (lambda (c) (and (fboundp 'undo-tree-restore-state-from-register)
  6969. (undo-tree-restore-state-from-register (car c))))))
  6970. for func in (cdr register-and-functions)
  6971. for cell = (assq func func-actions)
  6972. when cell
  6973. collect (cdr cell)))
  6974. ;;; Latex completion
  6975. (defun helm-c-latex-math-candidates ()
  6976. "Collect candidates for latex math completion."
  6977. (declare (special LaTeX-math-menu))
  6978. (loop for i in (cddr LaTeX-math-menu)
  6979. for elm = (loop for s in i when (vectorp s)
  6980. collect (cons (aref s 0) (aref s 1)))
  6981. append elm))
  6982. (defvar helm-c-source-latex-math
  6983. '((name . "Latex Math Menu")
  6984. (init . (lambda ()
  6985. (with-helm-current-buffer
  6986. (LaTeX-math-mode 1))))
  6987. (candidate-number-limit . 9999)
  6988. (candidates . helm-c-latex-math-candidates)
  6989. (action . (lambda (candidate)
  6990. (call-interactively candidate)))))
  6991. ;;;; <Headline Extraction>
  6992. (defvar helm-c-source-fixme
  6993. '((name . "TODO/FIXME/DRY comments")
  6994. (headline . "^.*\\<\\(TODO\\|FIXME\\|DRY\\)\\>.*$")
  6995. (adjust)
  6996. (recenter))
  6997. "Show TODO/FIXME/DRY comments in current file.")
  6998. (defvar helm-c-source-rd-headline
  6999. '((name . "RD HeadLine")
  7000. (headline "^= \\(.+\\)$" "^== \\(.+\\)$" "^=== \\(.+\\)$" "^==== \\(.+\\)$")
  7001. (condition . (memq major-mode '(rdgrep-mode rd-mode)))
  7002. (migemo)
  7003. (subexp . 1))
  7004. "Show RD headlines.
  7005. RD is Ruby's POD.
  7006. http://en.wikipedia.org/wiki/Ruby_Document_format")
  7007. (defvar helm-c-source-oddmuse-headline
  7008. '((name . "Oddmuse HeadLine")
  7009. (headline "^= \\(.+\\) =$" "^== \\(.+\\) ==$"
  7010. "^=== \\(.+\\) ===$" "^==== \\(.+\\) ====$")
  7011. (condition . (memq major-mode '(oddmuse-mode yaoddmuse-mode)))
  7012. (migemo)
  7013. (subexp . 1))
  7014. "Show Oddmuse headlines, such as EmacsWiki.")
  7015. (defvar helm-c-source-emacs-source-defun
  7016. '((name . "Emacs Source DEFUN")
  7017. (headline . "DEFUN\\|DEFVAR")
  7018. (condition . (string-match "/emacs2[0-9].+/src/.+c$"
  7019. (or buffer-file-name ""))))
  7020. "Show DEFUN/DEFVAR in Emacs C source file.")
  7021. (defvar helm-c-source-emacs-lisp-expectations
  7022. '((name . "Emacs Lisp Expectations")
  7023. (headline . "(desc[ ]\\|(expectations")
  7024. (condition . (eq major-mode 'emacs-lisp-mode)))
  7025. "Show descriptions (desc) in Emacs Lisp Expectations.
  7026. http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
  7027. (defvar helm-c-source-emacs-lisp-toplevels
  7028. '((name . "Emacs Lisp Toplevel / Level 4 Comment / Linkd Star")
  7029. (headline . "^(\\|(@\\*\\|^;;;;")
  7030. (get-line . buffer-substring)
  7031. (condition . (eq major-mode 'emacs-lisp-mode))
  7032. (adjust))
  7033. "Show top-level forms, level 4 comments and linkd stars (optional) in Emacs Lisp.
  7034. linkd.el is optional because linkd stars are extracted by regexp.
  7035. http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el")
  7036. ;;; Helm yaoddmuse
  7037. ;;
  7038. ;; Be sure to have yaoddmuse.el installed
  7039. ;; install-elisp may be required if you want to install elisp file from here.
  7040. (defvar helm-yaoddmuse-use-cache-file nil)
  7041. (defvar helm-c-yaoddmuse-cache-file "~/.emacs.d/yaoddmuse-cache.el")
  7042. (defvar helm-c-yaoddmuse-ew-cache nil)
  7043. (defun helm-yaoddmuse-get-candidates ()
  7044. (declare (special yaoddmuse-pages-hash))
  7045. (if helm-yaoddmuse-use-cache-file
  7046. (ignore-errors
  7047. (unless helm-c-yaoddmuse-ew-cache
  7048. (load helm-c-yaoddmuse-cache-file)
  7049. (setq helm-c-yaoddmuse-ew-cache
  7050. (gethash "EmacsWiki" yaoddmuse-pages-hash)))
  7051. helm-c-yaoddmuse-ew-cache)
  7052. (yaoddmuse-update-pagename t)
  7053. (gethash "EmacsWiki" yaoddmuse-pages-hash)))
  7054. (defvar helm-c-source-yaoddmuse-emacswiki-edit-or-view
  7055. '((name . "Yaoddmuse Edit or View (EmacsWiki)")
  7056. (candidates . helm-yaoddmuse-get-candidates)
  7057. (action . (("Edit page" . (lambda (candidate)
  7058. (yaoddmuse-edit "EmacsWiki" candidate)))
  7059. ("Browse page"
  7060. . (lambda (candidate)
  7061. (yaoddmuse-browse-page "EmacsWiki" candidate)))
  7062. ("Browse page other window"
  7063. . (lambda (candidate)
  7064. (if (one-window-p)
  7065. (split-window-vertically))
  7066. (yaoddmuse-browse-page "EmacsWiki" candidate)))
  7067. ("Browse diff"
  7068. . (lambda (candidate)
  7069. (yaoddmuse-browse-page-diff "EmacsWiki" candidate)))
  7070. ("Copy URL"
  7071. . (lambda (candidate)
  7072. (kill-new (yaoddmuse-url "EmacsWiki" candidate))
  7073. (message "Have copy page %s's URL to yank." candidate)))
  7074. ("Create page"
  7075. . (lambda (candidate)
  7076. (yaoddmuse-edit "EmacsWiki" helm-input)))
  7077. ("Update cache"
  7078. . (lambda (candidate)
  7079. (if helm-yaoddmuse-use-cache-file
  7080. (progn
  7081. (helm-yaoddmuse-cache-pages t)
  7082. (setq helm-c-yaoddmuse-ew-cache
  7083. (gethash "EmacsWiki" yaoddmuse-pages-hash)))
  7084. (yaoddmuse-update-pagename))))))
  7085. (action-transformer helm-c-yaoddmuse-action-transformer))
  7086. "Needs yaoddmuse.el.
  7087. http://www.emacswiki.org/emacs/download/yaoddmuse.el")
  7088. (defvar helm-c-source-yaoddmuse-emacswiki-post-library
  7089. '((name . "Yaoddmuse Post library (EmacsWiki)")
  7090. (init . (helm-yaoddmuse-init))
  7091. (candidates-in-buffer)
  7092. (action . (("Post library and Browse"
  7093. . (lambda (candidate)
  7094. (yaoddmuse-post-file
  7095. (find-library-name candidate)
  7096. "EmacsWiki"
  7097. (file-name-nondirectory (find-library-name candidate))
  7098. nil t)))
  7099. ("Post library"
  7100. . (lambda (candidate)
  7101. (yaoddmuse-post-file
  7102. (find-library-name candidate)
  7103. "EmacsWiki"
  7104. (file-name-nondirectory
  7105. (find-library-name candidate))))))))
  7106. "Needs yaoddmuse.el.
  7107. http://www.emacswiki.org/emacs/download/yaoddmuse.el")
  7108. (defun helm-c-yaoddmuse-action-transformer (actions candidate)
  7109. "Allow the use of `install-elisp' only on elisp files."
  7110. (if (string-match "\.el$" candidate)
  7111. (append actions '(("Install Elisp"
  7112. . (lambda (elm)
  7113. (install-elisp-from-emacswiki elm)))))
  7114. actions))
  7115. ;;;###autoload
  7116. (defun helm-yaoddmuse-cache-pages (&optional load)
  7117. "Fetch the list of files on emacswiki and create cache file.
  7118. If load is non--nil load the file and feed `yaoddmuse-pages-hash'."
  7119. (interactive)
  7120. (declare (special yaoddmuse-pages-hash))
  7121. (yaoddmuse-update-pagename)
  7122. (save-excursion
  7123. (find-file helm-c-yaoddmuse-cache-file)
  7124. (erase-buffer)
  7125. (insert "(puthash \"EmacsWiki\" '(")
  7126. (loop for i in (gethash "EmacsWiki" yaoddmuse-pages-hash)
  7127. do
  7128. (insert (concat "(\"" (car i) "\") ")))
  7129. (insert ") yaoddmuse-pages-hash)\n")
  7130. (save-buffer)
  7131. (kill-buffer (current-buffer))
  7132. (when (or current-prefix-arg
  7133. load)
  7134. (load helm-c-yaoddmuse-cache-file))))
  7135. (defun helm-yaoddmuse-init ()
  7136. "Init helm buffer status."
  7137. (let ((helm-buffer (helm-candidate-buffer 'global))
  7138. (library-list (yaoddmuse-get-library-list)))
  7139. (with-current-buffer helm-buffer
  7140. ;; Insert library name.
  7141. (dolist (library library-list)
  7142. (insert (format "%s\n" library)))
  7143. ;; Sort lines.
  7144. (sort-lines nil (point-min) (point-max)))))
  7145. ;;; Eev anchors
  7146. (defvar helm-c-source-eev-anchor
  7147. '((name . "Anchors")
  7148. (candidates
  7149. . (lambda ()
  7150. (ignore-errors
  7151. (with-helm-current-buffer
  7152. (loop initially (goto-char (point-min))
  7153. while (re-search-forward
  7154. (format ee-anchor-format "\\([^\.].+\\)") nil t)
  7155. for anchor = (match-string-no-properties 1)
  7156. collect (cons (format "%5d:%s"
  7157. (line-number-at-pos (match-beginning 0))
  7158. (format ee-anchor-format anchor))
  7159. anchor))))))
  7160. (persistent-action . (lambda (item)
  7161. (ee-to item)
  7162. (helm-match-line-color-current-line)))
  7163. (persistent-help . "Show this entry")
  7164. (action . (("Goto link" . ee-to)))))
  7165. ;;; Org headlines
  7166. ;;
  7167. ;;
  7168. (defvar helm-c-source-org-headline
  7169. `((name . "Org HeadLine")
  7170. (headline
  7171. ,@(mapcar
  7172. (lambda (num)
  7173. (format "^\\*\\{%d\\} \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
  7174. num))
  7175. (number-sequence 1 8)))
  7176. (condition . (eq major-mode 'org-mode))
  7177. (migemo)
  7178. (subexp . 1)
  7179. (persistent-action . (lambda (elm)
  7180. (helm-c-action-line-goto elm)
  7181. (org-cycle)))
  7182. (action-transformer
  7183. . (lambda (actions candidate)
  7184. '(("Go to Line" . helm-c-action-line-goto)
  7185. ("Refile to this Headline" . helm-c-org-headline-refile)
  7186. ("Insert Link to This Headline"
  7187. . helm-c-org-headline-insert-link-to-headline)))))
  7188. "Show Org headlines.
  7189. org-mode is very very much extended text-mode/outline-mode.
  7190. See (find-library \"org.el\")
  7191. See http://orgmode.org for the latest version.")
  7192. (defun helm-c-org-headline-insert-link-to-headline (lineno-and-content)
  7193. (insert
  7194. (save-excursion
  7195. (helm-goto-line (car lineno-and-content))
  7196. (and (looking-at org-complex-heading-regexp)
  7197. (org-make-link-string (concat "*" (match-string 4)))))))
  7198. (defun helm-c-org-headline-refile (lineno-and-content)
  7199. "Refile current org entry to LINENO-AND-CONTENT."
  7200. (with-helm-current-buffer
  7201. (org-cut-subtree)
  7202. (helm-goto-line (car lineno-and-content))
  7203. (org-end-of-subtree t t)
  7204. (let ((org-yank-adjusted-subtrees t))
  7205. (org-yank))))
  7206. ;;; Org keywords
  7207. ;;
  7208. ;;
  7209. (defvar helm-c-source-org-keywords
  7210. '((name . "Org Keywords")
  7211. (init . helm-c-org-keywords-init)
  7212. (candidates . helm-c-org-keywords-candidates)
  7213. (action . helm-c-org-keywords-insert)
  7214. (persistent-action . helm-c-org-keywords-show-help)
  7215. (persistent-help . "Show an example and info page to describe this keyword.")
  7216. (keywords-examples)
  7217. (keywords)))
  7218. (defvar helm-c-org-keywords-info-location
  7219. '(("#+TITLE:" . "(org)Export options")
  7220. ("#+AUTHOR:" . "(org)Export options")
  7221. ("#+DATE:" . "(org)Export options")
  7222. ("#+EMAIL:" . "(org)Export options")
  7223. ("#+DESCRIPTION:" . "(org)Export options")
  7224. ("#+KEYWORDS:" . "(org)Export options")
  7225. ("#+LANGUAGE:" . "(org)Export options")
  7226. ("#+TEXT:" . "(org)Export options")
  7227. ("#+TEXT:" . "(org)Export options")
  7228. ("#+OPTIONS:" . "(org)Export options")
  7229. ("#+BIND:" . "(org)Export options")
  7230. ("#+LINK_UP:" . "(org)Export options")
  7231. ("#+LINK_HOME:" . "(org)Export options")
  7232. ("#+LATEX_HEADER:" . "(org)Export options")
  7233. ("#+EXPORT_SELECT_TAGS:" . "(org)Export options")
  7234. ("#+EXPORT_EXCLUDE_TAGS:" . "(org)Export options")
  7235. ("#+INFOJS_OPT" . "(org)Javascript support")
  7236. ("#+BEGIN_HTML" . "(org)Quoting HTML tags")
  7237. ("#+BEGIN_LaTeX" . "(org)Quoting LaTeX code")
  7238. ("#+ORGTBL" . "(org)Radio tables")
  7239. ("#+HTML:" . "(org)Quoting HTML tags")
  7240. ("#+LaTeX:" . "(org)Quoting LaTeX code")
  7241. ("#+BEGIN:" . "(org)Dynamic blocks") ;clocktable columnview
  7242. ("#+BEGIN_EXAMPLE" . "(org)Literal examples")
  7243. ("#+BEGIN_QUOTE" . "(org)Paragraphs")
  7244. ("#+BEGIN_VERSE" . "(org)Paragraphs")
  7245. ("#+BEGIN_SRC" . "(org)Literal examples")
  7246. ("#+CAPTION" . "(org)Tables in HTML export")
  7247. ("#+LABEL" . "(org)Tables in LaTeX export")
  7248. ("#+ATTR_HTML" . "(org)Links")
  7249. ("#+ATTR_LaTeX" . "(org)Images in LaTeX export")))
  7250. (defun helm-c-org-keywords-init ()
  7251. (unless (helm-attr 'keywords-examples)
  7252. (require 'org)
  7253. (helm-attrset 'keywords-examples
  7254. (append
  7255. (mapcar
  7256. (lambda (x)
  7257. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7258. (cons (match-string 2 x) (match-string 1 x)))
  7259. (org-split-string (org-get-current-options) "\n"))
  7260. (mapcar 'list org-additional-option-like-keywords)))
  7261. (helm-attrset 'keywords (mapcar 'car (helm-attr 'keywords-examples)))))
  7262. (defun helm-c-org-keywords-candidates ()
  7263. (and (or (eq (buffer-local-value 'major-mode helm-current-buffer) 'org-mode)
  7264. (eq (buffer-local-value 'major-mode helm-current-buffer) 'message-mode))
  7265. (helm-attr 'keywords)))
  7266. (defun helm-c-org-keywords-insert (keyword)
  7267. (cond ((and (string-match "BEGIN" keyword)
  7268. (helm-region-active-p))
  7269. (let ((beg (region-beginning))
  7270. (end (region-end)))
  7271. (goto-char end)
  7272. (insert "\n#+" (replace-regexp-in-string
  7273. "BEGIN" "END" keyword) "\n")
  7274. (goto-char beg)
  7275. (insert "#+" keyword " ")
  7276. (save-excursion (insert "\n"))))
  7277. ((string-match "BEGIN" keyword)
  7278. (insert "#+" keyword " ")
  7279. (save-excursion
  7280. (insert "\n#+" (replace-regexp-in-string
  7281. "BEGIN" "END" keyword) "\n")))
  7282. (t (insert "#+" keyword " "))))
  7283. (defun helm-c-org-keywords-show-help (keyword)
  7284. (info (or (assoc-default (concat "#+" keyword) helm-c-org-keywords-info-location)
  7285. "(org)In-buffer settings"))
  7286. (search-forward (concat "#+" keyword) nil t)
  7287. (helm-persistent-highlight-point)
  7288. (message "%s" (or (cdr (assoc keyword (helm-attr 'keywords-examples))) "")))
  7289. ;;; bbdb
  7290. ;;
  7291. ;;
  7292. (defvar bbdb-records)
  7293. (defvar bbdb-buffer-name)
  7294. (defun helm-c-bbdb-candidates ()
  7295. "Return a list of all names in the bbdb database. The format
  7296. is \"Firstname Lastname\"."
  7297. (mapcar (lambda (bbdb-record)
  7298. (replace-regexp-in-string
  7299. "\\s-+$" ""
  7300. (concat (aref bbdb-record 0) " " (aref bbdb-record 1))))
  7301. (bbdb-records)))
  7302. (defun helm-c-bbdb-create-contact (actions candidate)
  7303. "Action transformer that returns only an entry to add the
  7304. current `helm-pattern' as new contact. All other actions are
  7305. removed."
  7306. (if (string= candidate "*Add to contacts*")
  7307. '(("Add to contacts" . (lambda (actions)
  7308. (bbdb-create-internal
  7309. (read-from-minibuffer "Name: " helm-c-bbdb-name)
  7310. (read-from-minibuffer "Company: ")
  7311. (read-from-minibuffer "Email: ")
  7312. nil
  7313. nil
  7314. (read-from-minibuffer "Note: ")))))
  7315. actions))
  7316. (defun helm-c-bbdb-get-record (candidate)
  7317. "Return record that match CANDIDATE."
  7318. (bbdb candidate nil)
  7319. (set-buffer "*BBDB*")
  7320. (bbdb-current-record))
  7321. (defvar helm-c-bbdb-name nil
  7322. "Only for internal use.")
  7323. (defvar helm-c-source-bbdb
  7324. '((name . "BBDB")
  7325. (candidates . helm-c-bbdb-candidates)
  7326. (action ("Send a mail" . helm-c-bbdb-compose-mail)
  7327. ("View person's data" . helm-c-bbdb-view-person-action))
  7328. (filtered-candidate-transformer . (lambda (candidates source)
  7329. (setq helm-c-bbdb-name helm-pattern)
  7330. (if (not candidates)
  7331. (list "*Add to contacts*")
  7332. candidates)))
  7333. (action-transformer . (lambda (actions candidate)
  7334. (helm-c-bbdb-create-contact actions candidate))))
  7335. "Needs BBDB.
  7336. http://bbdb.sourceforge.net/")
  7337. (defun helm-c-bbdb-view-person-action (candidate)
  7338. "View BBDB data of single CANDIDATE or marked candidates."
  7339. (helm-aif (helm-marked-candidates)
  7340. (let ((bbdb-append-records (length it)))
  7341. (dolist (i it)
  7342. (bbdb-redisplay-one-record (helm-c-bbdb-get-record i))))
  7343. (bbdb-redisplay-one-record (helm-c-bbdb-get-record candidate))))
  7344. (defun helm-c-bbdb-collect-mail-addresses ()
  7345. "Return a list of all mail addresses of records in bbdb buffer."
  7346. (with-current-buffer bbdb-buffer-name
  7347. (loop for i in bbdb-records
  7348. if (bbdb-record-net (car i))
  7349. collect (bbdb-dwim-net-address (car i)))))
  7350. (defun helm-c-bbdb-compose-mail (candidate)
  7351. "Compose a mail with all records of bbdb buffer."
  7352. (helm-c-bbdb-view-person-action candidate)
  7353. (let* ((address-list (helm-c-bbdb-collect-mail-addresses))
  7354. (address-str (mapconcat 'identity address-list ",\n ")))
  7355. (compose-mail address-str)))
  7356. ;;; Evaluation Result
  7357. ;;
  7358. ;;
  7359. ;; Internal
  7360. (defvar helm-eldoc-active-minibuffers-list nil)
  7361. (defvar helm-eval-expression-input-history nil)
  7362. (defvar helm-c-source-evaluation-result
  7363. '((name . "Evaluation Result")
  7364. (disable-shortcuts)
  7365. (dummy)
  7366. (multiline)
  7367. (mode-line . "C-RET: nl-and-indent, tab: reindent, C-tab:complete, C-p/n: next/prec-line.")
  7368. (filtered-candidate-transformer . (lambda (candidates source)
  7369. (list
  7370. (condition-case nil
  7371. (with-helm-current-buffer
  7372. (pp-to-string
  7373. (eval (read helm-pattern))))
  7374. (error "Error")))))
  7375. (action . (("Copy result to kill-ring" . (lambda (candidate)
  7376. (with-current-buffer helm-buffer
  7377. (let ((end (save-excursion
  7378. (goto-char (point-max))
  7379. (search-backward "\n")
  7380. (point))))
  7381. (kill-region (point) end)))))
  7382. ("copy sexp to kill-ring" . (lambda (candidate)
  7383. (kill-new helm-input)))))))
  7384. (defun helm-eval-new-line-and-indent ()
  7385. (interactive)
  7386. (newline) (lisp-indent-line))
  7387. (defun helm-eldoc-store-minibuffer ()
  7388. "Store minibuffer buffer name in `helm-eldoc-active-minibuffers-list'."
  7389. (with-selected-window (minibuffer-window)
  7390. (push (buffer-name) helm-eldoc-active-minibuffers-list)))
  7391. (defun helm-eldoc-show-in-eval ()
  7392. "Return eldoc in mode-line for current minibuffer input."
  7393. (let ((buf (with-selected-window (minibuffer-window)
  7394. (buffer-name))))
  7395. (when (member buf helm-eldoc-active-minibuffers-list)
  7396. (let* ((str-all (with-current-buffer buf
  7397. (minibuffer-completion-contents)))
  7398. (sym (when str-all
  7399. (with-temp-buffer
  7400. (insert str-all)
  7401. (goto-char (point-max))
  7402. (unless (looking-back ")\\|\"") (forward-char -1))
  7403. (eldoc-current-symbol))))
  7404. (info-fn (eldoc-fnsym-in-current-sexp))
  7405. (doc (or (eldoc-get-var-docstring sym)
  7406. (eldoc-get-fnsym-args-string
  7407. (car info-fn) (cadr info-fn)))))
  7408. (when doc (funcall helm-c-eldoc-in-minibuffer-show-fn doc))))))
  7409. (defun helm-c-show-info-in-mode-line (str)
  7410. "Display string STR in mode-line."
  7411. (save-selected-window
  7412. (with-current-buffer helm-buffer
  7413. (let ((mode-line-format (concat " " str)))
  7414. (force-mode-line-update)
  7415. (sit-for helm-c-show-info-in-mode-line-delay))
  7416. (force-mode-line-update))))
  7417. ;;; Calculation Result
  7418. ;;
  7419. ;;
  7420. (defvar helm-c-source-calculation-result
  7421. '((name . "Calculation Result")
  7422. (dummy)
  7423. (filtered-candidate-transformer . (lambda (candidates source)
  7424. (list
  7425. (condition-case nil
  7426. (calc-eval helm-pattern)
  7427. (error "error")))))
  7428. (action ("Copy result to kill-ring" . kill-new))))
  7429. ;;; Google Suggestions
  7430. ;;
  7431. ;;
  7432. ;; Internal
  7433. (defvar helm-ggs-max-length-real-flag 0)
  7434. (defvar helm-ggs-max-length-num-flag 0)
  7435. (defun helm-c-google-suggest-fetch (input)
  7436. "Fetch suggestions for INPUT from XML buffer.
  7437. Return an alist with elements like (data . number_results)."
  7438. (setq helm-ggs-max-length-real-flag 0
  7439. helm-ggs-max-length-num-flag 0)
  7440. (let ((request (concat helm-c-google-suggest-url
  7441. (url-hexify-string input))))
  7442. (flet ((fetch ()
  7443. (loop
  7444. with result-alist = (xml-get-children
  7445. (car (xml-parse-region
  7446. (point-min) (point-max)))
  7447. 'CompleteSuggestion)
  7448. for i in result-alist
  7449. for data = (cdr (caadr (assoc 'suggestion i)))
  7450. for nqueries = (cdr (caadr (assoc 'num_queries i)))
  7451. for lqueries = (length (helm-c-ggs-set-number-result
  7452. nqueries))
  7453. for ldata = (length data)
  7454. do
  7455. (progn
  7456. (when (> ldata helm-ggs-max-length-real-flag)
  7457. (setq helm-ggs-max-length-real-flag ldata))
  7458. (when (> lqueries helm-ggs-max-length-num-flag)
  7459. (setq helm-ggs-max-length-num-flag lqueries)))
  7460. collect (cons data nqueries) into cont
  7461. finally return cont)))
  7462. (if helm-google-suggest-use-curl-p
  7463. (with-temp-buffer
  7464. (call-process "curl" nil t nil request)
  7465. (fetch))
  7466. (with-current-buffer
  7467. (url-retrieve-synchronously request)
  7468. (fetch))))))
  7469. (defun helm-c-google-suggest-set-candidates (&optional request-prefix)
  7470. "Set candidates with result and number of google results found."
  7471. (let ((suggestions
  7472. (loop with suggested-results = (helm-c-google-suggest-fetch
  7473. (or (and request-prefix
  7474. (concat request-prefix
  7475. " " helm-pattern))
  7476. helm-pattern))
  7477. for (real . numresult) in suggested-results
  7478. ;; Prepare number of results with ","
  7479. for fnumresult = (helm-c-ggs-set-number-result numresult)
  7480. ;; Calculate number of spaces to add before fnumresult
  7481. ;; if it is smaller than longest result
  7482. ;; `helm-ggs-max-length-num-flag'.
  7483. ;; e.g 1,234,567
  7484. ;; 345,678
  7485. ;; To be sure it is aligned properly.
  7486. for nspaces = (if (< (length fnumresult)
  7487. helm-ggs-max-length-num-flag)
  7488. (- helm-ggs-max-length-num-flag
  7489. (length fnumresult))
  7490. 0)
  7491. ;; Add now the spaces before fnumresult.
  7492. for align-fnumresult = (concat (make-string nspaces ? )
  7493. fnumresult)
  7494. for interval = (- helm-ggs-max-length-real-flag
  7495. (length real))
  7496. for spaces = (make-string (+ 2 interval) ? )
  7497. for display = (format "%s%s(%s results)"
  7498. real spaces align-fnumresult)
  7499. collect (cons display real))))
  7500. (if (loop for (disp . dat) in suggestions
  7501. thereis (equal dat helm-pattern))
  7502. suggestions
  7503. ;; if there is no suggestion exactly matching the input then
  7504. ;; prepend a Search on Google item to the list
  7505. (append
  7506. suggestions
  7507. (list (cons (concat "Search for " "'" helm-input "'" " on Google")
  7508. helm-input))))))
  7509. (defun helm-c-ggs-set-number-result (num)
  7510. (if num
  7511. (progn
  7512. (and (numberp num) (setq num (number-to-string num)))
  7513. (loop for i in (reverse (split-string num "" t))
  7514. for count from 1
  7515. append (list i) into C
  7516. when (= count 3)
  7517. append (list ",") into C
  7518. and do (setq count 0)
  7519. finally return
  7520. (replace-regexp-in-string
  7521. "^," "" (mapconcat 'identity (reverse C) ""))))
  7522. "?"))
  7523. (defvar helm-c-google-suggest-default-browser-function nil
  7524. "*The browse url function you prefer to use with google suggest.
  7525. When nil, use the first browser function available
  7526. See `helm-browse-url-default-browser-alist'.")
  7527. (defun helm-c-google-suggest-action (candidate)
  7528. "Default action to jump to a google suggested candidate."
  7529. (let ((arg (concat helm-c-google-suggest-search-url
  7530. (url-hexify-string candidate))))
  7531. (helm-aif helm-c-google-suggest-default-browser-function
  7532. (funcall it arg)
  7533. (helm-c-browse-url arg))))
  7534. (defvar helm-c-google-suggest-default-function
  7535. 'helm-c-google-suggest-set-candidates
  7536. "Default function to use in helm google suggest.")
  7537. (defvar helm-c-source-google-suggest
  7538. '((name . "Google Suggest")
  7539. (candidates . (lambda ()
  7540. (funcall helm-c-google-suggest-default-function)))
  7541. (action . (("Google Search" . helm-c-google-suggest-action)))
  7542. (volatile)
  7543. (requires-pattern . 3)
  7544. (delayed)))
  7545. (defun helm-c-google-suggest-emacs-lisp ()
  7546. "Try to emacs lisp complete with google suggestions."
  7547. (helm-c-google-suggest-set-candidates "emacs lisp"))
  7548. ;;; Yahoo suggestions
  7549. ;;
  7550. ;;
  7551. (defun helm-c-yahoo-suggest-fetch (input)
  7552. "Fetch Yahoo suggestions for INPUT from XML buffer.
  7553. Return an alist with elements like (data . number_results)."
  7554. (let ((request (concat helm-c-yahoo-suggest-url
  7555. (url-hexify-string input))))
  7556. (flet ((fetch ()
  7557. (loop
  7558. with result-alist = (xml-get-children
  7559. (car (xml-parse-region
  7560. (point-min) (point-max)))
  7561. 'Result)
  7562. for i in result-alist
  7563. collect (caddr i))))
  7564. (with-current-buffer
  7565. (url-retrieve-synchronously request)
  7566. (fetch)))))
  7567. (defun helm-c-yahoo-suggest-set-candidates ()
  7568. "Set candidates with Yahoo results found."
  7569. (let ((suggestions (helm-c-yahoo-suggest-fetch helm-input)))
  7570. (or suggestions
  7571. (append
  7572. suggestions
  7573. (list (cons (concat "Search for " "'" helm-input "'" " on Yahoo")
  7574. helm-input))))))
  7575. (defun helm-c-yahoo-suggest-action (candidate)
  7576. "Default action to jump to a Yahoo suggested candidate."
  7577. (helm-c-browse-url (concat helm-c-yahoo-suggest-search-url
  7578. (url-hexify-string candidate))))
  7579. (defvar helm-c-source-yahoo-suggest
  7580. '((name . "Yahoo Suggest")
  7581. (candidates . helm-c-yahoo-suggest-set-candidates)
  7582. (action . (("Yahoo Search" . helm-c-yahoo-suggest-action)))
  7583. (volatile)
  7584. (requires-pattern . 3)
  7585. (delayed)))
  7586. ;;; Web browser functions.
  7587. ;;
  7588. ;;
  7589. (require 'browse-url)
  7590. ;; If default setting of `w3m-command' is not
  7591. ;; what you want you and you modify it, you will have to reeval
  7592. ;; also `helm-browse-url-default-browser-alist'.
  7593. (defvar w3m-command "/usr/bin/w3m")
  7594. (defvar helm-c-home-url "http://www.google.fr"
  7595. "*Default url to use as home url.")
  7596. (defvar ac-browse-url-chromium-program "chromium-browser")
  7597. (defvar ac-browse-url-uzbl-program "uzbl-browser")
  7598. (defvar helm-browse-url-default-browser-alist
  7599. `((,w3m-command . w3m-browse-url)
  7600. (,browse-url-firefox-program . browse-url-firefox)
  7601. (,ac-browse-url-chromium-program . ac-browse-url-chromium)
  7602. (,ac-browse-url-uzbl-program . ac-browse-url-uzbl)
  7603. (,browse-url-kde-program . browse-url-kde)
  7604. (,browse-url-gnome-moz-program . browse-url-gnome-moz)
  7605. (,browse-url-mozilla-program . browse-url-mozilla)
  7606. (,browse-url-galeon-program . browse-url-galeon)
  7607. (,browse-url-netscape-program . browse-url-netscape)
  7608. (,browse-url-mosaic-program . browse-url-mosaic)
  7609. (,browse-url-xterm-program . browse-url-text-xterm))
  7610. "*Alist of \(executable . function\) to try to find a suitable url browser.")
  7611. (defun* helm-c-generic-browser (url name &rest args)
  7612. "Browse URL with NAME browser."
  7613. (let ((proc (concat name " " url)))
  7614. (message "Starting %s..." name)
  7615. (apply 'start-process proc nil name
  7616. (append args (list url)))
  7617. (set-process-sentinel
  7618. (get-process proc)
  7619. #'(lambda (process event)
  7620. (when (string= event "finished\n")
  7621. (message "%s process %s" process event))))))
  7622. (defun ac-browse-url-chromium (url)
  7623. "Browse URL with google chrome browser."
  7624. (interactive "sURL: ")
  7625. (helm-c-generic-browser
  7626. url ac-browse-url-chromium-program))
  7627. (defun ac-browse-url-uzbl (url &optional ignore)
  7628. "Browse URL with uzbl browser."
  7629. (interactive "sURL: ")
  7630. (helm-c-generic-browser url ac-browse-url-uzbl-program "-u"))
  7631. (defun helm-browse-url-default-browser (url &rest args)
  7632. "Find the first available browser and ask it to load URL."
  7633. (let ((default-browser-fn
  7634. (loop for (exe . fn) in helm-browse-url-default-browser-alist
  7635. thereis (and exe (executable-find exe) fn))))
  7636. (if default-browser-fn
  7637. (apply default-browser-fn url args)
  7638. (error "No usable browser found"))))
  7639. (defun helm-c-browse-url (url &rest args)
  7640. "Default command to browse URL."
  7641. (if browse-url-browser-function
  7642. (browse-url url args)
  7643. (helm-browse-url-default-browser url args)))
  7644. ;;; Surfraw
  7645. ;;
  7646. ;; Need external program surfraw.
  7647. ;; <http://surfraw.alioth.debian.org/>
  7648. (defvar helm-surfraw-default-browser-function nil
  7649. "*The browse url function you prefer to use with surfraw.
  7650. When nil, fallback to `browse-url-browser-function'.")
  7651. ;; Internal
  7652. (defvar helm-surfraw-engines-history nil)
  7653. (defvar helm-surfraw-input-history nil)
  7654. (defun helm-c-build-elvi-list ()
  7655. "Return list of all engines and descriptions handled by surfraw."
  7656. (cdr
  7657. (with-temp-buffer
  7658. (call-process "surfraw" nil t nil
  7659. "-elvi")
  7660. (split-string (buffer-string) "\n"))))
  7661. ;;; Emms
  7662. ;;
  7663. ;;
  7664. (defun helm-emms-stream-edit-bookmark (elm)
  7665. "Change the information of current emms-stream bookmark from helm."
  7666. (declare (special emms-stream-list))
  7667. (let* ((cur-buf helm-current-buffer)
  7668. (bookmark (assoc elm emms-stream-list))
  7669. (name (read-from-minibuffer "Description: "
  7670. (nth 0 bookmark)))
  7671. (url (read-from-minibuffer "URL: "
  7672. (nth 1 bookmark)))
  7673. (fd (read-from-minibuffer "Feed Descriptor: "
  7674. (int-to-string (nth 2 bookmark))))
  7675. (type (read-from-minibuffer "Type (url, streamlist, or lastfm): "
  7676. (format "%s" (car (last bookmark))))))
  7677. (save-window-excursion
  7678. (emms-streams)
  7679. (when (re-search-forward (concat "^" name) nil t)
  7680. (beginning-of-line)
  7681. (emms-stream-delete-bookmark)
  7682. (emms-stream-add-bookmark name url (string-to-number fd) type)
  7683. (emms-stream-save-bookmarks-file)
  7684. (emms-stream-quit)
  7685. (helm-c-switch-to-buffer cur-buf)))))
  7686. (defun helm-emms-stream-delete-bookmark (candidate)
  7687. "Delete emms-streams bookmarks from helm."
  7688. (let* ((cands (helm-marked-candidates))
  7689. (bmks (loop for bm in cands collect
  7690. (car (assoc bm emms-stream-list))))
  7691. (bmk-reg (mapconcat 'regexp-quote bmks "\\|^")))
  7692. (when (y-or-n-p (format "Really delete radios\n -%s: ? "
  7693. (mapconcat 'identity bmks "\n -")))
  7694. (save-window-excursion
  7695. (emms-streams)
  7696. (goto-char (point-min))
  7697. (loop while (re-search-forward bmk-reg nil t)
  7698. do (progn (beginning-of-line)
  7699. (emms-stream-delete-bookmark))
  7700. finally do (progn
  7701. (emms-stream-save-bookmarks-file)
  7702. (emms-stream-quit)))))))
  7703. (defvar helm-c-source-emms-streams
  7704. '((name . "Emms Streams")
  7705. (init . (lambda ()
  7706. (emms-stream-init)))
  7707. (candidates . (lambda ()
  7708. (declare (special emms-stream-list))
  7709. (mapcar 'car emms-stream-list)))
  7710. (action . (("Play" . (lambda (elm)
  7711. (declare (special emms-stream-list))
  7712. (let* ((stream (assoc elm emms-stream-list))
  7713. (fn (intern (concat "emms-play-" (symbol-name (car (last stream))))))
  7714. (url (second stream)))
  7715. (funcall fn url))))
  7716. ("Delete" . helm-emms-stream-delete-bookmark)
  7717. ("Edit" . helm-emms-stream-edit-bookmark)))
  7718. (filtered-candidate-transformer . helm-c-adaptive-sort)))
  7719. ;; Don't forget to set `emms-source-file-default-directory'
  7720. (defvar helm-c-source-emms-dired
  7721. '((name . "Music Directory")
  7722. (candidates . (lambda ()
  7723. (cddr (directory-files emms-source-file-default-directory))))
  7724. (action .
  7725. (("Play Directory" . (lambda (item)
  7726. (emms-play-directory
  7727. (expand-file-name
  7728. item
  7729. emms-source-file-default-directory))))
  7730. ("Open dired in file's directory" . (lambda (item)
  7731. (helm-c-open-dired
  7732. (expand-file-name
  7733. item
  7734. emms-source-file-default-directory))))))
  7735. (filtered-candidate-transformer . helm-c-adaptive-sort)))
  7736. (defun helm-c-emms-files-modifier (candidates source)
  7737. (let ((current-playlist (with-current-emms-playlist
  7738. (loop with cur-list = (emms-playlist-tracks-in-region
  7739. (point-min) (point-max))
  7740. for i in cur-list
  7741. for name = (assoc-default 'name i)
  7742. when name
  7743. collect name))))
  7744. (loop for i in candidates
  7745. if (member (cdr i) current-playlist)
  7746. collect (cons (propertize (car i)
  7747. 'face 'helm-emms-playlist)
  7748. (cdr i)) into lis
  7749. else collect i into lis
  7750. finally return (reverse lis))))
  7751. (defun helm-c-emms-play-current-playlist ()
  7752. "Play current playlist."
  7753. (with-current-emms-playlist
  7754. (emms-playlist-first)
  7755. (emms-playlist-mode-play-smart)))
  7756. (defvar helm-c-source-emms-files
  7757. '((name . "Emms files")
  7758. (candidates . (lambda ()
  7759. (loop for v being the hash-values in emms-cache-db
  7760. for name = (assoc-default 'name v)
  7761. for artist = (or (assoc-default 'info-artist v) "unknown")
  7762. for genre = (or (assoc-default 'info-genre v) "unknown")
  7763. for tracknum = (or (assoc-default 'info-tracknumber v) "unknown")
  7764. for song = (or (assoc-default 'info-title v) "unknown")
  7765. for info = (concat artist " - " genre " - " tracknum ": " song)
  7766. unless (string-match "^\\(http\\|mms\\):" name)
  7767. collect (cons info name))))
  7768. (filtered-candidate-transformer . helm-c-emms-files-modifier)
  7769. (candidate-number-limit . 9999)
  7770. (action . (("Play file" . emms-play-file)
  7771. ("Add to Playlist and play (C-u clear current)"
  7772. . (lambda (candidate)
  7773. (when helm-current-prefix-arg
  7774. (emms-playlist-current-clear))
  7775. (emms-playlist-new)
  7776. (mapc 'emms-add-playlist-file (helm-marked-candidates))
  7777. (unless emms-player-playing-p
  7778. (helm-c-emms-play-current-playlist))))))))
  7779. ;;; Jabber Contacts (jabber.el)
  7780. (defun helm-c-jabber-online-contacts ()
  7781. "List online Jabber contacts."
  7782. (with-no-warnings
  7783. (let (jids)
  7784. (dolist (item (jabber-concat-rosters) jids)
  7785. (when (get item 'connected)
  7786. (push (if (get item 'name)
  7787. (cons (get item 'name) item)
  7788. (cons (symbol-name item) item)) jids))))))
  7789. (defvar helm-c-source-jabber-contacts
  7790. '((name . "Jabber Contacts")
  7791. (init . (lambda () (require 'jabber)))
  7792. (candidates . (lambda () (mapcar 'car (helm-c-jabber-online-contacts))))
  7793. (action . (lambda (x)
  7794. (jabber-chat-with
  7795. (jabber-read-account)
  7796. (symbol-name
  7797. (cdr (assoc x (helm-c-jabber-online-contacts)))))))))
  7798. ;;; Call source.
  7799. (defvar helm-source-select-buffer "*helm source select*")
  7800. (defvar helm-c-source-call-source
  7801. `((name . "Call helm source")
  7802. (candidate-number-limit)
  7803. (candidates
  7804. . (lambda ()
  7805. (loop for vname in (all-completions "helm-c-source-" obarray)
  7806. for var = (intern vname)
  7807. for name = (ignore-errors (assoc-default 'name (symbol-value var)))
  7808. if name collect
  7809. (cons (format "%s `%s'"
  7810. name (propertize vname 'face 'font-lock-variable-name-face))
  7811. var))))
  7812. (action
  7813. . (("Invoke helm with selected source"
  7814. .
  7815. (lambda (candidate)
  7816. (setq helm-candidate-number-limit 9999)
  7817. (helm candidate nil nil nil nil
  7818. helm-source-select-buffer)))
  7819. ("Describe variable" . describe-variable)
  7820. ("Find variable" . find-variable)))
  7821. (persistent-action . describe-variable)
  7822. (persistent-help . "Show description of this source")))
  7823. (defun helm-call-source-from-helm ()
  7824. "Call helm source within `helm' session."
  7825. (interactive)
  7826. (setq helm-input-idle-delay 0)
  7827. (helm-set-sources '(helm-c-source-call-source)))
  7828. ;;; Execute Preconfigured helm.
  7829. (defvar helm-c-source-helm-commands
  7830. '((name . "Preconfigured Helm")
  7831. (candidates . helm-c-helm-commands-candidates)
  7832. (type . command)
  7833. (candidate-number-limit)))
  7834. (defun helm-c-helm-commands-candidates ()
  7835. (loop for (cmd . desc) in (helm-c-list-preconfigured-helm)
  7836. collect (cons (if (where-is-internal cmd nil t)
  7837. (substitute-command-keys (format "M-x %s (\\[%s]) : %s" cmd cmd desc))
  7838. (substitute-command-keys (format "\\[%s] : %s" cmd desc)))
  7839. cmd)))
  7840. ;;; Occur
  7841. ;;
  7842. ;;
  7843. (defun helm-c-occur-init ()
  7844. "Create the initial helm occur buffer.
  7845. If region is active use region as buffer contents
  7846. instead of whole buffer."
  7847. (with-current-buffer (helm-candidate-buffer 'global)
  7848. (erase-buffer)
  7849. (let ((buf-contents
  7850. (with-helm-current-buffer
  7851. (if (helm-region-active-p)
  7852. (buffer-substring (region-beginning) (region-end))
  7853. (buffer-substring (point-min) (point-max))))))
  7854. (insert buf-contents))))
  7855. (defun helm-c-occur-get-line (s e)
  7856. (format "%7d:%s" (line-number-at-pos (1- s)) (buffer-substring s e)))
  7857. (defun helm-c-occur-query-replace-regexp (candidate)
  7858. "Query replace regexp starting from CANDIDATE.
  7859. If region is active ignore CANDIDATE and replace only in region.
  7860. With a prefix arg replace only matches surrounded by word boundaries,
  7861. i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
  7862. (let ((regexp helm-input))
  7863. (unless (helm-region-active-p)
  7864. (helm-c-action-line-goto candidate))
  7865. (apply 'query-replace-regexp
  7866. (helm-c-query-replace-args regexp))))
  7867. (defun helm-occur-run-query-replace-regexp ()
  7868. "Run `query-replace-regexp' in helm occur from keymap."
  7869. (interactive)
  7870. (helm-c-quit-and-execute-action
  7871. 'helm-c-occur-query-replace-regexp))
  7872. (defvar helm-c-source-occur
  7873. `((name . "Occur")
  7874. (init . helm-c-occur-init)
  7875. (candidates-in-buffer)
  7876. (migemo)
  7877. (get-line . helm-c-occur-get-line)
  7878. (display-to-real . helm-c-display-to-real-line)
  7879. (action . (("Go to Line" . helm-c-action-line-goto)
  7880. ("Query replace regexp (C-u Not inside word.)"
  7881. . helm-c-occur-query-replace-regexp)))
  7882. (recenter)
  7883. (mode-line . helm-occur-mode-line)
  7884. (keymap . ,helm-occur-map)
  7885. (requires-pattern . 1)
  7886. (delayed)))
  7887. ;;; Helm browse code.
  7888. (defun helm-c-browse-code-get-line (beg end)
  7889. "Select line if it match the regexp corresponding to current `major-mode'.
  7890. Line is parsed for BEG position to END position."
  7891. (let ((str-line (buffer-substring beg end))
  7892. (regexp (assoc-default major-mode
  7893. helm-c-browse-code-regexp-alist))
  7894. (num-line (if (string= helm-pattern "") beg (1- beg))))
  7895. (when (and regexp (string-match regexp str-line))
  7896. (format "%4d:%s" (line-number-at-pos num-line) str-line))))
  7897. (defvar helm-c-source-browse-code
  7898. '((name . "Browse code")
  7899. (init . (lambda ()
  7900. (helm-candidate-buffer helm-current-buffer)
  7901. (with-helm-current-buffer
  7902. (jit-lock-fontify-now))))
  7903. (candidate-number-limit . 9999)
  7904. (candidates-in-buffer)
  7905. (get-line . helm-c-browse-code-get-line)
  7906. (type . line)
  7907. (recenter)))
  7908. ;; Do many actions for input
  7909. (defvar helm-c-source-create
  7910. '((name . "Create")
  7911. (dummy)
  7912. (action)
  7913. (action-transformer . helm-create--actions))
  7914. "Do many create actions from `helm-pattern'.
  7915. See also `helm-create--actions'.")
  7916. (defun helm-create-from-helm ()
  7917. "Run `helm-create' from `helm' as a fallback."
  7918. (interactive)
  7919. (helm-run-after-quit 'helm-create nil helm-pattern))
  7920. (defun helm-create--actions (&rest ignored)
  7921. "Default actions for `helm-create' / `helm-c-source-create'."
  7922. (remove-if-not
  7923. (lambda (pair) (and (consp pair) (functionp (cdr pair))))
  7924. (append helm-create--actions-private
  7925. '(("find-file" . find-file)
  7926. ("find-file other window" . find-file-other-window)
  7927. ("New buffer" . helm-c-switch-to-buffer)
  7928. ("New buffer other window" . switch-to-buffer-other-window)
  7929. ("Bookmark Set" . bookmark-set)
  7930. ("Set Register" .
  7931. (lambda (x) (set-register (read-char "Register: ") x)))
  7932. ("Insert Linkd star" . linkd-insert-star)
  7933. ("Insert Linkd Tag" . linkd-insert-tag)
  7934. ("Insert Linkd Link" . linkd-insert-link)
  7935. ("Insert Linkd Lisp" . linkd-insert-lisp)
  7936. ("Insert Linkd Wiki" . linkd-insert-wiki)
  7937. ("Google Search" . google)))))
  7938. ;; Minibuffer History
  7939. ;;
  7940. ;;
  7941. (defvar helm-c-source-minibuffer-history
  7942. '((name . "Minibuffer History")
  7943. (header-name . (lambda (name)
  7944. (format "%s (%s)" name minibuffer-history-variable)))
  7945. (candidates
  7946. . (lambda ()
  7947. (let ((history (loop for i in
  7948. (symbol-value minibuffer-history-variable)
  7949. unless (string= "" i) collect i)))
  7950. (if (consp (car history))
  7951. (mapcar 'prin1-to-string history)
  7952. history))))
  7953. (migemo)
  7954. (action . (lambda (candidate)
  7955. (delete-minibuffer-contents)
  7956. (insert candidate)))))
  7957. ;;; Elscreen
  7958. ;;
  7959. ;;
  7960. (defvar helm-c-source-elscreen
  7961. '((name . "Elscreen")
  7962. (candidates
  7963. . (lambda ()
  7964. (if (cdr (elscreen-get-screen-to-name-alist))
  7965. (sort
  7966. (loop for sname in (elscreen-get-screen-to-name-alist)
  7967. append (list (format "[%d] %s" (car sname) (cdr sname))))
  7968. #'(lambda (a b) (compare-strings a nil nil b nil nil))))))
  7969. (action
  7970. . (("Change Screen" .
  7971. (lambda (candidate)
  7972. (elscreen-goto (- (aref candidate 1) (aref "0" 0)))))
  7973. ("Kill Screen(s)" .
  7974. (lambda (candidate)
  7975. (dolist (i (helm-marked-candidates))
  7976. (elscreen-goto (- (aref i 1) (aref "0" 0)))
  7977. (elscreen-kill))))
  7978. ("Only Screen" .
  7979. (lambda (candidate)
  7980. (elscreen-goto (- (aref candidate 1) (aref "0" 0)))
  7981. (elscreen-kill-others)))))))
  7982. ;;;; <System>
  7983. ;;; Top (process)
  7984. (defvar helm-c-top-command "COLUMNS=%s top -b -n 1"
  7985. "Top command (batch mode). %s is replaced with `frame-width'.")
  7986. (defvar helm-c-source-top
  7987. '((name . "Top (Press C-c C-u to refresh)")
  7988. (init . helm-c-top-init)
  7989. (candidates-in-buffer)
  7990. (display-to-real . helm-c-top-display-to-real)
  7991. (persistent-action . helm-c-top-sh-persistent-action)
  7992. (persistent-help . "SIGTERM")
  7993. (action
  7994. ("kill (TERM)" . (lambda (pid)
  7995. (helm-c-top-sh (format "kill -TERM %s" pid))))
  7996. ("kill (KILL)" . (lambda (pid)
  7997. (helm-c-top-sh (format "kill -KILL %s" pid))))
  7998. ("Copy PID" . (lambda (pid) (kill-new pid))))))
  7999. (defun helm-c-top-sh (cmd)
  8000. (message "Executed %s\n%s" cmd (shell-command-to-string cmd)))
  8001. (defun helm-c-top-sh-persistent-action (pid)
  8002. (delete-other-windows)
  8003. (helm-c-top-sh (format "kill -TERM %s" pid))
  8004. (helm-force-update))
  8005. (defun helm-c-top-init ()
  8006. (with-current-buffer (helm-candidate-buffer 'global)
  8007. (call-process-shell-command
  8008. (format helm-c-top-command
  8009. (- (frame-width) (if helm-enable-digit-shortcuts 4 0)))
  8010. nil (current-buffer))))
  8011. (defun helm-c-top-display-to-real (line)
  8012. (car (split-string line)))
  8013. ;;; Timers
  8014. (defvar helm-c-source-absolute-time-timers
  8015. '((name . "Absolute Time Timers")
  8016. (candidates . timer-list)
  8017. (type . timer)))
  8018. (defvar helm-c-source-idle-time-timers
  8019. '((name . "Idle Time Timers")
  8020. (candidates . timer-idle-list)
  8021. (type . timer)))
  8022. (defun helm-c-timer-real-to-display (timer)
  8023. (destructuring-bind (triggered t1 t2 t3 repeat-delay func args idle-delay)
  8024. (append timer nil) ;use `append' to convert vector->list
  8025. (format "%s repeat=%5S %s(%s)"
  8026. (let ((time (list t1 t2 t3)))
  8027. (if idle-delay
  8028. (format-time-string "idle-for=%5s" time)
  8029. (format-time-string "%m/%d %T" time)))
  8030. repeat-delay
  8031. func
  8032. (mapconcat 'prin1-to-string args " "))))
  8033. ;;; X RandR resolution change
  8034. ;;
  8035. ;;
  8036. ;;; FIXME I do not care multi-display.
  8037. (defun helm-c-xrandr-info ()
  8038. "Return a pair with current X screen number and current X display name."
  8039. (with-temp-buffer
  8040. (call-process "xrandr" nil (current-buffer) nil
  8041. "--current")
  8042. (let (screen output)
  8043. (goto-char (point-min))
  8044. (save-excursion
  8045. (when (re-search-forward "\\(^Screen \\)\\([0-9]\\):" nil t)
  8046. (setq screen (match-string 2))))
  8047. (when (re-search-forward "^\\(.*\\) connected" nil t)
  8048. (setq output (match-string 1)))
  8049. (list screen output))))
  8050. (defun helm-c-xrandr-screen ()
  8051. "Return current X screen number."
  8052. (car (helm-c-xrandr-info)))
  8053. (defun helm-c-xrandr-output ()
  8054. "Return current X display name."
  8055. (cadr (helm-c-xrandr-info)))
  8056. (defvar helm-c-source-xrandr-change-resolution
  8057. '((name . "Change Resolution")
  8058. (candidates
  8059. . (lambda ()
  8060. (with-temp-buffer
  8061. (call-process "xrandr" nil (current-buffer) nil
  8062. "--screen" (helm-c-xrandr-screen) "-q")
  8063. (goto-char 1)
  8064. (loop with modes = nil
  8065. while (re-search-forward " \\([0-9]+x[0-9]+\\)" nil t)
  8066. for mode = (match-string 1)
  8067. unless (member mode modes)
  8068. collect mode into modes
  8069. finally return modes))))
  8070. (action
  8071. ("Change Resolution"
  8072. . (lambda (mode)
  8073. (call-process "xrandr" nil nil nil
  8074. "--screen" (helm-c-xrandr-screen)
  8075. "--output" (helm-c-xrandr-output)
  8076. "--mode" mode))))))
  8077. ;;; Xfont selection
  8078. ;;
  8079. ;;
  8080. (defun helm-c-persistent-xfont-action (elm)
  8081. "Show current font temporarily"
  8082. (let ((current-font (cdr (assoc 'font (frame-parameters))))
  8083. (default-font elm))
  8084. (unwind-protect
  8085. (progn (set-frame-font default-font 'keep-size) (sit-for 2))
  8086. (set-frame-font current-font))))
  8087. (defvar helm-c-xfonts-cache nil)
  8088. (defvar helm-c-source-xfonts
  8089. '((name . "X Fonts")
  8090. (init . (lambda ()
  8091. (unless helm-c-xfonts-cache
  8092. (setq helm-c-xfonts-cache
  8093. (x-list-fonts "*")))))
  8094. (candidates . helm-c-xfonts-cache)
  8095. (action . (("Copy to kill ring" . (lambda (elm)
  8096. (kill-new elm)))
  8097. ("Set Font" . (lambda (elm)
  8098. (kill-new elm)
  8099. (set-frame-font elm 'keep-size)
  8100. (message "New font have been copied to kill ring")))))
  8101. (persistent-action . helm-c-persistent-xfont-action)
  8102. (persistent-help . "Switch to this font temporarily")))
  8103. ;;; 𝕌𝕔𝕤 𝕊𝕪𝕞𝕓𝕠𝕝 𝕔𝕠𝕞𝕡𝕝𝕖𝕥𝕚𝕠𝕟
  8104. ;;
  8105. ;;
  8106. (defvar helm-c-ucs-max-len 0)
  8107. (defun helm-c-calculate-ucs-max-len ()
  8108. "Calculate the length of longest `ucs-names' candidate."
  8109. (loop with count = 0
  8110. for (n . v) in (ucs-names)
  8111. for len = (length n)
  8112. if (> len count)
  8113. do (setq count len)
  8114. finally return count))
  8115. (defun helm-c-ucs-init ()
  8116. "Initialize an helm buffer with ucs symbols.
  8117. Only math* symbols are collected."
  8118. (unless (> helm-c-ucs-max-len 0)
  8119. (setq helm-c-ucs-max-len
  8120. (helm-c-calculate-ucs-max-len)))
  8121. (with-current-buffer (helm-candidate-buffer
  8122. (get-buffer-create "*helm ucs*"))
  8123. ;; `ucs-names' fn will not run again, data is cached in
  8124. ;; var `ucs-names'.
  8125. (loop for (n . v) in (ucs-names)
  8126. for len = (length n)
  8127. for diff = (+ (- helm-c-ucs-max-len len) 2)
  8128. unless (string= "" n)
  8129. do (progn (insert (concat
  8130. n ":"
  8131. (make-string
  8132. diff ? )))
  8133. (ucs-insert v)
  8134. (insert "\n")))))
  8135. (defun helm-c-ucs-forward-char (candidate)
  8136. (with-helm-current-buffer
  8137. (forward-char 1)))
  8138. (defun helm-c-ucs-backward-char (candidate)
  8139. (with-helm-current-buffer
  8140. (forward-char -1)))
  8141. (defun helm-c-ucs-delete-backward (candidate)
  8142. (with-helm-current-buffer
  8143. (delete-char -1)))
  8144. (defun helm-c-ucs-insert-char (candidate)
  8145. (with-helm-current-buffer
  8146. (insert
  8147. (replace-regexp-in-string
  8148. " " ""
  8149. (cadr (split-string candidate ":"))))))
  8150. (defun helm-c-ucs-persistent-insert ()
  8151. (interactive)
  8152. (helm-attrset 'action-insert 'helm-c-ucs-insert-char)
  8153. (helm-execute-persistent-action 'action-insert))
  8154. (defun helm-c-ucs-persistent-forward ()
  8155. (interactive)
  8156. (helm-attrset 'action-forward 'helm-c-ucs-forward-char)
  8157. (helm-execute-persistent-action 'action-forward))
  8158. (defun helm-c-ucs-persistent-backward ()
  8159. (interactive)
  8160. (helm-attrset 'action-back 'helm-c-ucs-backward-char)
  8161. (helm-execute-persistent-action 'action-back))
  8162. (defun helm-c-ucs-persistent-delete ()
  8163. (interactive)
  8164. (helm-attrset 'action-delete 'helm-c-ucs-delete-backward)
  8165. (helm-execute-persistent-action 'action-delete))
  8166. (defvar helm-c-source-ucs
  8167. '((name . "Ucs names")
  8168. (init . helm-c-ucs-init)
  8169. (candidate-number-limit . 9999)
  8170. (candidates-in-buffer)
  8171. (mode-line . helm-c-ucs-mode-line-string)
  8172. (action . (("Insert" . helm-c-ucs-insert-char)
  8173. ("Forward char" . helm-c-ucs-forward-char)
  8174. ("Backward char" . helm-c-ucs-backward-char)
  8175. ("Delete char backward" . helm-c-ucs-delete-backward))))
  8176. "Source for collecting `ucs-names' math symbols.")
  8177. ;;; Emacs process
  8178. ;;
  8179. ;;
  8180. (defvar helm-c-source-emacs-process
  8181. '((name . "Emacs Process")
  8182. (candidates . (lambda () (mapcar #'process-name (process-list))))
  8183. (persistent-action . (lambda (elm)
  8184. (delete-process (get-process elm))
  8185. (helm-delete-current-selection)))
  8186. (persistent-help . "Kill Process")
  8187. (action ("Kill Process" . (lambda (elm)
  8188. (delete-process (get-process elm)))))))
  8189. ;;; World time
  8190. ;;
  8191. ;;
  8192. (defvar helm-c-source-time-world
  8193. '((name . "Time World List")
  8194. (init . (lambda ()
  8195. (let ((helm-buffer (helm-candidate-buffer 'global)))
  8196. (with-current-buffer helm-buffer
  8197. (display-time-world-display display-time-world-list)))))
  8198. (candidates-in-buffer)))
  8199. ;;; Helm interface for Debian/Ubuntu packages (apt-*)
  8200. ;;
  8201. ;;
  8202. (defvar helm-c-source-apt
  8203. '((name . "APT")
  8204. (init . helm-c-apt-init)
  8205. (candidates-in-buffer)
  8206. (candidate-transformer helm-c-apt-candidate-transformer)
  8207. (display-to-real . helm-c-apt-display-to-real)
  8208. (requires-pattern . 2)
  8209. (update . helm-c-apt-refresh)
  8210. (action
  8211. ("Show package description" . helm-c-apt-cache-show)
  8212. ("Install package" . helm-c-apt-install)
  8213. ("Reinstall package" . helm-c-apt-reinstall)
  8214. ("Remove package" . helm-c-apt-uninstall)
  8215. ("Purge package" . helm-c-apt-purge))
  8216. (persistent-action . helm-c-apt-persistent-action)
  8217. (persistent-help . "Show package description")))
  8218. (defvar helm-c-apt-query "emacs")
  8219. (defvar helm-c-apt-search-command "apt-cache search '%s'")
  8220. (defvar helm-c-apt-show-command "apt-cache show '%s'")
  8221. (defvar helm-c-apt-installed-packages nil)
  8222. (defvar helm-c-apt-all-packages nil)
  8223. (defvar helm-c-apt-input-history nil)
  8224. (defun helm-c-apt-refresh ()
  8225. "Refresh installed candidates list."
  8226. (setq helm-c-apt-installed-packages nil)
  8227. (setq helm-c-apt-all-packages nil))
  8228. (defun helm-c-apt-persistent-action (candidate)
  8229. "Persistent action for APT source."
  8230. (helm-c-apt-cache-show candidate))
  8231. (defun helm-c-apt-candidate-transformer (candidates)
  8232. "Show installed CANDIDATES and the ones to deinstall in a different color."
  8233. (loop for cand in candidates
  8234. for name = (helm-c-apt-display-to-real cand)
  8235. collect (cond ((string= (assoc-default
  8236. name helm-c-apt-installed-packages)
  8237. "deinstall")
  8238. (propertize cand 'face 'helm-apt-deinstalled))
  8239. ((string= (assoc-default
  8240. name helm-c-apt-installed-packages)
  8241. "install")
  8242. (propertize cand 'face 'helm-apt-installed))
  8243. (t cand))))
  8244. (defun helm-c-apt-init ()
  8245. "Initialize list of debian packages."
  8246. (let ((query ""))
  8247. (unless (and helm-c-apt-installed-packages
  8248. helm-c-apt-all-packages)
  8249. (message "Loading package list...")
  8250. (setq helm-c-apt-installed-packages
  8251. (with-temp-buffer
  8252. (call-process-shell-command "dpkg --get-selections"
  8253. nil (current-buffer))
  8254. (loop for i in (split-string (buffer-string) "\n" t)
  8255. for p = (split-string i)
  8256. collect (cons (car p) (cadr p)))))
  8257. (setq helm-c-apt-all-packages
  8258. (with-current-buffer
  8259. (helm-candidate-buffer
  8260. (get-buffer-create (format "*helm-apt*")))
  8261. (erase-buffer)
  8262. (call-process-shell-command
  8263. (format helm-c-apt-search-command query)
  8264. nil (current-buffer))))
  8265. (message "Loading package list done")
  8266. (sit-for 0.5))))
  8267. (defun helm-c-apt-display-to-real (line)
  8268. "Return only name of a debian package.
  8269. LINE is displayed like:
  8270. package name - description."
  8271. (car (split-string line " - ")))
  8272. (defun helm-c-shell-command-if-needed (command)
  8273. "Run shell command COMMAND to describe package.
  8274. If a buffer named COMMAND already exists, just switch to it."
  8275. (let ((buf (get-buffer command)))
  8276. (helm-c-switch-to-buffer (get-buffer-create command))
  8277. (unless buf (insert (shell-command-to-string command)))))
  8278. (defun helm-c-apt-cache-show (package)
  8279. "Show information on apt package PACKAGE."
  8280. (helm-c-shell-command-if-needed
  8281. (format helm-c-apt-show-command package)))
  8282. (defun helm-c-apt-install (package)
  8283. "Run 'apt-get install' shell command on PACKAGE."
  8284. (helm-c-apt-generic-action :action 'install))
  8285. (defun helm-c-apt-reinstall (package)
  8286. "Run 'apt-get install --reinstall' shell command on PACKAGE."
  8287. (helm-c-apt-generic-action :action 'reinstall))
  8288. (defun helm-c-apt-uninstall (package)
  8289. "Run 'apt-get remove' shell command on PACKAGE."
  8290. (helm-c-apt-generic-action :action 'uninstall))
  8291. (defun helm-c-apt-purge (package)
  8292. "Run 'apt-get purge' shell command on PACKAGE."
  8293. (helm-c-apt-generic-action :action 'purge))
  8294. (defun* helm-c-apt-generic-action (&key action)
  8295. "Run 'apt-get ACTION'.
  8296. Support install, remove and purge actions."
  8297. (ansi-term (getenv "SHELL") "helm apt")
  8298. (term-line-mode)
  8299. (let ((command (case action
  8300. ('install "sudo apt-get install ")
  8301. ('reinstall "sudo apt-get install --reinstall ")
  8302. ('uninstall "sudo apt-get remove ")
  8303. ('purge "sudo apt-get purge ")
  8304. (t (error "Unknow action"))))
  8305. (beg (point))
  8306. end
  8307. (cand-list (mapconcat #'(lambda (x) (format "'%s'" x))
  8308. (helm-marked-candidates) " ")))
  8309. (goto-char (point-max))
  8310. (insert (concat command cand-list))
  8311. (setq end (point))
  8312. (if (y-or-n-p (format "%s package" (symbol-name action)))
  8313. (progn
  8314. (setq helm-c-external-commands-list nil)
  8315. (setq helm-c-apt-installed-packages nil)
  8316. (term-char-mode) (term-send-input))
  8317. (delete-region beg end) (term-send-eof) (kill-buffer))))
  8318. ;; (helm-c-apt-install "jed")
  8319. ;;; Helm UI for gentoo portage.
  8320. ;;
  8321. ;;
  8322. (defvar helm-c-gentoo-use-flags nil)
  8323. (defvar helm-c-gentoo-buffer "*helm-gentoo-output*")
  8324. (defvar helm-c-cache-gentoo nil)
  8325. (defvar helm-c-cache-world nil)
  8326. (defvar helm-c-source-gentoo
  8327. '((name . "Portage sources")
  8328. (init . (lambda ()
  8329. (get-buffer-create helm-c-gentoo-buffer)
  8330. (unless helm-c-cache-gentoo
  8331. (helm-c-gentoo-setup-cache))
  8332. (unless helm-c-cache-world
  8333. (setq helm-c-cache-world (helm-c-gentoo-get-world)))
  8334. (helm-c-gentoo-init-list)))
  8335. (candidates-in-buffer)
  8336. (match . identity)
  8337. (candidate-transformer helm-c-highlight-world)
  8338. (action . (("Show package" . (lambda (elm)
  8339. (helm-c-gentoo-eshell-action elm "eix")))
  8340. ("Show history" . (lambda (elm)
  8341. (if (member elm helm-c-cache-world)
  8342. (helm-c-gentoo-eshell-action elm "genlop -qe")
  8343. (message "No infos on packages not yet installed"))))
  8344. ("Copy in kill-ring" . kill-new)
  8345. ("insert at point" . insert)
  8346. ("Browse HomePage" . (lambda (elm)
  8347. (let ((urls (helm-c-gentoo-get-url elm)))
  8348. (browse-url (helm-comp-read "Url: " urls :must-match t)))))
  8349. ("Show extra infos" . (lambda (elm)
  8350. (if (member elm helm-c-cache-world)
  8351. (helm-c-gentoo-eshell-action elm "genlop -qi")
  8352. (message "No infos on packages not yet installed"))))
  8353. ("Show use flags" . (lambda (elm)
  8354. (helm-c-gentoo-default-action elm "equery" "-C" "u")
  8355. (font-lock-add-keywords nil '(("^\+.*" . font-lock-variable-name-face)))
  8356. (font-lock-mode 1)))
  8357. ("Run emerge pretend" . (lambda (elm)
  8358. (helm-c-gentoo-eshell-action elm "emerge -p")))
  8359. ("Emerge" . (lambda (elm)
  8360. (helm-gentoo-install elm :action 'install)))
  8361. ("Unmerge" . (lambda (elm)
  8362. (helm-gentoo-install elm :action 'uninstall)))
  8363. ("Show dependencies" . (lambda (elm)
  8364. (helm-c-gentoo-default-action elm "equery" "-C" "d")))
  8365. ("Show related files" . (lambda (elm)
  8366. (helm-c-gentoo-default-action elm "equery" "files")))
  8367. ("Refresh" . (lambda (elm)
  8368. (helm-c-gentoo-setup-cache)
  8369. (setq helm-c-cache-world (helm-c-gentoo-get-world))))))))
  8370. (defun* helm-gentoo-install (candidate &key action)
  8371. (setq helm-c-external-commands-list nil)
  8372. (ansi-term (getenv "SHELL") "Gentoo emerge")
  8373. (term-line-mode)
  8374. (let ((command (case action
  8375. ('install "sudo emerge -av ")
  8376. ('uninstall "sudo emerge -avC ")
  8377. (t (error "Unknow action"))))
  8378. (elms (mapconcat 'identity (helm-marked-candidates) " "))
  8379. (beg (point)) end)
  8380. (goto-char (point-max))
  8381. (insert (concat command elms))
  8382. (setq end (point))
  8383. (term-char-mode) (term-send-input)))
  8384. (defun helm-c-gentoo-default-action (elm command &rest args)
  8385. "Gentoo default action that use `helm-c-gentoo-buffer'."
  8386. (if (member elm helm-c-cache-world)
  8387. (progn
  8388. (helm-c-switch-to-buffer helm-c-gentoo-buffer)
  8389. (erase-buffer)
  8390. (let ((com-list (append args (list elm))))
  8391. (apply #'call-process command nil t nil
  8392. com-list)))
  8393. (message "No infos on packages not yet installed")))
  8394. (defvar helm-c-source-use-flags
  8395. '((name . "Use Flags")
  8396. (init . (lambda ()
  8397. (unless helm-c-gentoo-use-flags
  8398. (helm-c-gentoo-setup-use-flags-cache))
  8399. (helm-c-gentoo-get-use)))
  8400. (candidates-in-buffer)
  8401. (match . identity)
  8402. (candidate-transformer helm-c-highlight-local-use)
  8403. (action . (("Description"
  8404. . (lambda (elm)
  8405. (helm-c-switch-to-buffer helm-c-gentoo-buffer)
  8406. (erase-buffer)
  8407. (apply #'call-process "euse" nil t nil
  8408. `("-i"
  8409. ,elm))
  8410. (font-lock-add-keywords nil `((,elm . font-lock-variable-name-face)))
  8411. (font-lock-mode 1)))
  8412. ("Enable"
  8413. . (lambda (elm)
  8414. (helm-c-gentoo-eshell-action elm "*sudo -p Password: euse -E")))
  8415. ("Disable"
  8416. . (lambda (elm)
  8417. (helm-c-gentoo-eshell-action elm "*sudo -p Password: euse -D")))
  8418. ("Remove"
  8419. . (lambda (elm)
  8420. (helm-c-gentoo-eshell-action elm "*sudo -p Password: euse -P")))
  8421. ("Show which dep use this flag"
  8422. . (lambda (elm)
  8423. (helm-c-switch-to-buffer helm-c-gentoo-buffer)
  8424. (erase-buffer)
  8425. (apply #'call-process "equery" nil t nil
  8426. `("-C"
  8427. "h"
  8428. ,elm))))))))
  8429. (defun helm-c-gentoo-init-list ()
  8430. "Initialize buffer with all packages in Portage."
  8431. (let* ((portage-buf (get-buffer-create "*helm-gentoo*"))
  8432. (buf (helm-candidate-buffer 'portage-buf)))
  8433. (with-current-buffer buf
  8434. (dolist (i helm-c-cache-gentoo)
  8435. (insert (concat i "\n"))))))
  8436. (defun helm-c-gentoo-setup-cache ()
  8437. "Set up `helm-c-cache-gentoo'"
  8438. (setq helm-c-cache-gentoo
  8439. (split-string (with-temp-buffer
  8440. (call-process "eix" nil t nil
  8441. "--only-names")
  8442. (buffer-string)))))
  8443. (defun helm-c-gentoo-eshell-action (elm command)
  8444. (when (get-buffer "*EShell Command Output*")
  8445. (kill-buffer "*EShell Command Output*"))
  8446. (message "Wait searching...")
  8447. (let ((buf-fname (buffer-file-name helm-current-buffer)))
  8448. (if (and buf-fname (string-match tramp-file-name-regexp buf-fname))
  8449. (progn
  8450. (save-window-excursion
  8451. (pop-to-buffer "*scratch*")
  8452. (eshell-command (format "%s %s" command elm)))
  8453. (pop-to-buffer "*EShell Command Output*"))
  8454. (eshell-command (format "%s %s" command elm)))))
  8455. (defun helm-c-gentoo-get-use ()
  8456. "Initialize buffer with all use flags."
  8457. (let* ((use-buf (get-buffer-create "*helm-gentoo-use*"))
  8458. (buf (helm-candidate-buffer 'use-buf)))
  8459. (with-current-buffer buf
  8460. (dolist (i helm-c-gentoo-use-flags)
  8461. (insert (concat i "\n"))))))
  8462. (defun helm-c-gentoo-setup-use-flags-cache ()
  8463. "Setup `helm-c-gentoo-use-flags'"
  8464. (setq helm-c-gentoo-use-flags
  8465. (split-string (with-temp-buffer
  8466. (call-process "eix" nil t nil
  8467. "--print-all-useflags")
  8468. (buffer-string)))))
  8469. (defun helm-c-gentoo-get-url (elm)
  8470. "Return a list of urls from eix output."
  8471. (loop
  8472. with url-list = (split-string
  8473. (with-temp-buffer
  8474. (call-process "eix" nil t nil
  8475. elm "--format" "<homepage>\n")
  8476. (buffer-string)))
  8477. with all
  8478. for i in url-list
  8479. when (and (string-match "^http://.*" i)
  8480. (not (member i all)))
  8481. collect i into all
  8482. finally return all))
  8483. (defun helm-c-gentoo-get-world ()
  8484. "Return list of all installed package on your system."
  8485. (split-string (with-temp-buffer
  8486. (call-process "qlist" nil t nil
  8487. "-I")
  8488. (buffer-string))))
  8489. (defun helm-c-gentoo-get-local-use ()
  8490. (split-string (with-temp-buffer
  8491. (call-process "portageq" nil t nil
  8492. "envvar"
  8493. "USE")
  8494. (buffer-string))))
  8495. (defun helm-c-highlight-world (eix)
  8496. "Highlight all installed package."
  8497. (loop for i in eix
  8498. if (member i helm-c-cache-world)
  8499. collect (propertize i 'face 'helm-gentoo-match-face)
  8500. else
  8501. collect i))
  8502. (defun helm-c-highlight-local-use (use-flags)
  8503. (let ((local-uses (helm-c-gentoo-get-local-use)))
  8504. (loop for i in use-flags
  8505. if (member i local-uses)
  8506. collect (propertize i 'face 'helm-gentoo-match-face)
  8507. else
  8508. collect i)))
  8509. ;;; Helm ratpoison UI
  8510. ;;
  8511. ;;
  8512. (defvar helm-c-source-ratpoison-commands
  8513. '((name . "Ratpoison Commands")
  8514. (init . helm-c-ratpoison-commands-init)
  8515. (candidates-in-buffer)
  8516. (action ("Execute the command" . helm-c-ratpoison-commands-execute))
  8517. (display-to-real . helm-c-ratpoison-commands-display-to-real)
  8518. (candidate-number-limit)))
  8519. (defun helm-c-ratpoison-commands-init ()
  8520. (unless (helm-candidate-buffer)
  8521. (with-current-buffer (helm-candidate-buffer 'global)
  8522. ;; with ratpoison prefix key
  8523. (save-excursion
  8524. (call-process "ratpoison" nil (current-buffer) nil "-c" "help"))
  8525. (while (re-search-forward "^\\([^ ]+\\) \\(.+\\)$" nil t)
  8526. (replace-match "<ratpoison> \\1: \\2"))
  8527. (goto-char (point-max))
  8528. ;; direct binding
  8529. (save-excursion
  8530. (call-process "ratpoison" nil (current-buffer) nil "-c" "help top"))
  8531. (while (re-search-forward "^\\([^ ]+\\) \\(.+\\)$" nil t)
  8532. (replace-match "\\1: \\2")))))
  8533. (defun helm-c-ratpoison-commands-display-to-real (display)
  8534. (and (string-match ": " display)
  8535. (substring display (match-end 0))))
  8536. (defun helm-c-ratpoison-commands-execute (candidate)
  8537. (call-process "ratpoison" nil nil nil "-ic" candidate))
  8538. ;;; Helm `completing-read' replacement
  8539. ;;
  8540. ;;
  8541. (defun helm-comp-read-get-candidates (collection &optional test sort-fn alistp)
  8542. "Convert COLLECTION to list removing elements that don't match TEST.
  8543. See `helm-comp-read' about supported COLLECTION arguments.
  8544. SORT-FN is a predicate to sort COLLECTION.
  8545. ALISTP when non--nil will not use `all-completions' to collect
  8546. candidates because it doesn't handle alists correctly for helm.
  8547. i.e In `all-completions' the keys \(cars of elements\)
  8548. are the possible completions. In helm we want to use the cdr instead
  8549. like \(display . real\).
  8550. e.g
  8551. \(setq A '((a . 1) (b . 2) (c . 3)))
  8552. ==>((a . 1) (b . 2) (c . 3))
  8553. \(helm-comp-read \"test: \" A :alistp nil
  8554. :exec-when-only-one t
  8555. :initial-input \"a\")
  8556. ==>\"a\"
  8557. \(helm-comp-read \"test: \" A :alistp t
  8558. :exec-when-only-one t
  8559. :initial-input \"1\")
  8560. ==>\"1\"
  8561. See docstring of `all-completions' for more info.
  8562. If COLLECTION is an `obarray', a TEST should be needed. See `obarray'."
  8563. (let ((cands
  8564. (cond ((and (eq collection obarray) test)
  8565. (all-completions "" collection test))
  8566. ((and (vectorp collection) test)
  8567. (loop for i across collection when (funcall test i) collect i))
  8568. ((vectorp collection)
  8569. (loop for i across collection collect i))
  8570. ((and alistp test)
  8571. (loop for i in collection when (funcall test i) collect i))
  8572. ((and (symbolp collection) (boundp collection))
  8573. (symbol-value collection))
  8574. (alistp collection)
  8575. ((and collection test)
  8576. (all-completions "" collection test))
  8577. (t (all-completions "" collection)))))
  8578. (if sort-fn (sort cands sort-fn) cands)))
  8579. (defun helm-cr-default-transformer (candidates source)
  8580. "Default filter candidate function for `helm-comp-read'.
  8581. Do nothing, just return candidate list unmodified."
  8582. candidates)
  8583. (defun* helm-comp-read (prompt collection
  8584. &key
  8585. test
  8586. initial-input
  8587. default
  8588. preselect
  8589. (buffer "*Helm Completions*")
  8590. must-match
  8591. (requires-pattern 0)
  8592. (history nil)
  8593. input-history
  8594. (persistent-action nil)
  8595. (persistent-help "DoNothing")
  8596. (mode-line helm-mode-line-string)
  8597. (keymap helm-map)
  8598. (name "Helm Completions")
  8599. candidates-in-buffer
  8600. exec-when-only-one
  8601. (volatile t)
  8602. sort
  8603. (fc-transformer 'helm-cr-default-transformer)
  8604. (marked-candidates nil)
  8605. (alistp t))
  8606. "Read a string in the minibuffer, with helm completion.
  8607. It is helm `completing-read' equivalent.
  8608. - PROMPT is the prompt name to use.
  8609. - COLLECTION can be a list, vector, obarray or hash-table.
  8610. It can be also a function that receives three arguments:
  8611. the values string, predicate and t. See `all-completions' for more details.
  8612. Keys description:
  8613. - TEST: A predicate called with one arg i.e candidate.
  8614. - INITIAL-INPUT: Same as input arg in `helm'.
  8615. - PRESELECT: See preselect arg of `helm'.
  8616. - DEFAULT: This option is used only for compatibility with regular
  8617. Emacs `completing-read'.
  8618. - BUFFER: Name of helm-buffer.
  8619. - MUST-MATCH: Candidate selected must be one of COLLECTION.
  8620. - REQUIRES-PATTERN: Same as helm attribute, default is 0.
  8621. - HISTORY: A list containing specific history, default is nil.
  8622. When it is non--nil, all elements of HISTORY are displayed in
  8623. a special source before COLLECTION.
  8624. - INPUT-HISTORY: A symbol. the minibuffer input history will be
  8625. stored there, if nil or not provided, `minibuffer-history'
  8626. will be used instead.
  8627. - PERSISTENT-ACTION: A function called with one arg i.e candidate.
  8628. - PERSISTENT-HELP: A string to document PERSISTENT-ACTION.
  8629. - MODE-LINE: A string or list to display in mode line.
  8630. (See `helm-mode-line-string')
  8631. - KEYMAP: A keymap to use in this `helm-comp-read'.
  8632. (The keymap will be shared with history source)
  8633. - NAME: The name related to this local source.
  8634. - EXEC-WHEN-ONLY-ONE: Bound `helm-execute-action-at-once-if-one'
  8635. to non--nil. (possibles values are t or nil).
  8636. - VOLATILE: Use volatile attribute \(enabled by default\).
  8637. - SORT: A predicate to give to `sort' e.g `string-lessp'.
  8638. - FC-TRANSFORMER: A `filtered-candidate-transformer' function.
  8639. - MARKED-CANDIDATES: If non--nil return candidate or marked candidates as a list.
  8640. - ALISTP: \(default is non--nil\) See `helm-comp-read-get-candidates'.
  8641. - CANDIDATES-IN-BUFFER: when non--nil use a source build with
  8642. `helm-candidates-in-buffer' which is much faster.
  8643. Argument VOLATILE have no effect when CANDIDATES-IN-BUFFER is non--nil.
  8644. Any prefix args passed during `helm-comp-read' invocation will be recorded
  8645. in `helm-current-prefix-arg', otherwise if prefix args were given before
  8646. `helm-comp-read' invocation, the value of `current-prefix-arg' will be used.
  8647. That's mean you can pass prefix args before or after calling a command
  8648. that use `helm-comp-read' See `helm-M-x' for example."
  8649. (when (get-buffer helm-action-buffer)
  8650. (kill-buffer helm-action-buffer))
  8651. (flet ((action-fn (candidate)
  8652. (if marked-candidates
  8653. (helm-marked-candidates)
  8654. (identity candidate))))
  8655. ;; Assume completion have been already required,
  8656. ;; so always use 'confirm.
  8657. (when (eq must-match 'confirm-after-completion)
  8658. (setq must-match 'confirm))
  8659. (let* ((minibuffer-completion-confirm must-match)
  8660. (must-match-map (when must-match
  8661. (let ((map (make-sparse-keymap)))
  8662. (define-key map (kbd "RET")
  8663. 'helm-confirm-and-exit-minibuffer)
  8664. map)))
  8665. (helm-map (if must-match-map
  8666. (make-composed-keymap
  8667. must-match-map (or keymap helm-map))
  8668. (or keymap helm-map)))
  8669. (src-hist `((name . ,(format "%s History" name))
  8670. (candidates
  8671. . (lambda ()
  8672. (let ((all (helm-comp-read-get-candidates
  8673. history nil nil ,alistp)))
  8674. (delete
  8675. ""
  8676. (helm-fast-remove-dups
  8677. (if (and default (not (string= default "")))
  8678. (delq nil (cons default
  8679. (delete default all)))
  8680. all)
  8681. :test 'equal)))))
  8682. (filtered-candidate-transformer
  8683. . (lambda (candidates sources)
  8684. (loop for i in candidates
  8685. do (set-text-properties 0 (length i) nil i)
  8686. collect i)))
  8687. (persistent-action . ,persistent-action)
  8688. (persistent-help . ,persistent-help)
  8689. (mode-line . ,mode-line)
  8690. (action . ,'action-fn)))
  8691. (src `((name . ,name)
  8692. (candidates
  8693. . (lambda ()
  8694. (let ((cands (helm-comp-read-get-candidates
  8695. collection test sort alistp)))
  8696. (unless (or must-match (string= helm-pattern ""))
  8697. (setq cands (append (list helm-pattern) cands)))
  8698. (if (and default (not (string= default "")))
  8699. (delq nil (cons default (delete default cands)))
  8700. cands))))
  8701. (filtered-candidate-transformer ,fc-transformer)
  8702. (requires-pattern . ,requires-pattern)
  8703. (persistent-action . ,persistent-action)
  8704. (persistent-help . ,persistent-help)
  8705. (mode-line . ,mode-line)
  8706. (action . ,'action-fn)))
  8707. (src-1 `((name . ,name)
  8708. (init
  8709. . (lambda ()
  8710. (let ((cands (helm-comp-read-get-candidates
  8711. collection test sort alistp)))
  8712. (unless (or must-match (string= helm-pattern ""))
  8713. (setq cands (append (list helm-pattern) cands)))
  8714. (with-current-buffer (helm-candidate-buffer 'global)
  8715. (loop for i in
  8716. (if (and default (not (string= default "")))
  8717. (delq nil (cons default (delete default cands)))
  8718. cands)
  8719. do (insert (concat i "\n")))))))
  8720. (candidates-in-buffer)
  8721. (filtered-candidate-transformer ,fc-transformer)
  8722. (requires-pattern . ,requires-pattern)
  8723. (persistent-action . ,persistent-action)
  8724. (persistent-help . ,persistent-help)
  8725. (mode-line . ,mode-line)
  8726. (action . ,'action-fn)))
  8727. (src-list (list src-hist
  8728. (if candidates-in-buffer
  8729. src-1
  8730. (if volatile
  8731. (append src '((volatile)))
  8732. src))))
  8733. (helm-execute-action-at-once-if-one exec-when-only-one))
  8734. (or
  8735. (helm
  8736. :sources src-list
  8737. :input initial-input
  8738. :default default
  8739. :preselect preselect
  8740. :prompt prompt
  8741. :resume 'noresume
  8742. :keymap helm-map
  8743. :history (and (symbolp input-history) input-history)
  8744. :buffer buffer)
  8745. (when (and (eq helm-exit-status 0)
  8746. (eq must-match 'confirm))
  8747. ;; Return empty string only if it is the DEFAULT
  8748. ;; value and helm-pattern is empty.
  8749. ;; otherwise return helm-pattern
  8750. (if (and (string= helm-pattern "") default)
  8751. default (identity helm-pattern)))
  8752. (unless (or (eq helm-exit-status 1)
  8753. must-match) ; FIXME this should not be needed now.
  8754. default)
  8755. (keyboard-quit)))))
  8756. ;; Generic completing-read
  8757. ;;
  8758. ;; Support also function as collection.
  8759. ;; e.g M-x man is supported.
  8760. ;; Support hash-table and vectors as collection.
  8761. ;; NOTE:
  8762. ;; Some crap emacs functions may not be supported
  8763. ;; like ffap-alternate-file (bad use of completing-read)
  8764. ;; and maybe others.
  8765. ;; Provide a mode `helm-mode' which turn on
  8766. ;; helm in all `completing-read' and `read-file-name' in Emacs.
  8767. ;;
  8768. (defvar helm-completion-mode-string " Helm")
  8769. (defvar helm-completion-mode-quit-message
  8770. "Helm completion disabled")
  8771. (defvar helm-completion-mode-start-message
  8772. "Helm completion enabled")
  8773. ;;; Specialized handlers
  8774. ;;
  8775. ;;
  8776. (defun helm-completing-read-symbols
  8777. (prompt collection test require-match init
  8778. hist default inherit-input-method name buffer)
  8779. "Specialized function for fast symbols completion in `helm-mode'."
  8780. (or
  8781. (helm
  8782. :sources `((name . ,name)
  8783. (init . (lambda ()
  8784. (with-current-buffer (helm-candidate-buffer 'global)
  8785. (goto-char (point-min))
  8786. (when (and default (stringp default)
  8787. ;; Some defaults args result as
  8788. ;; (symbol-name nil) == "nil".
  8789. ;; e.g debug-on-entry.
  8790. (not (string= default "nil"))
  8791. (not (string= default "")))
  8792. (insert (concat default "\n")))
  8793. (loop with all = (all-completions "" collection test)
  8794. for sym in all
  8795. unless (and default (eq sym default))
  8796. do (insert (concat sym "\n"))))))
  8797. (persistent-action . helm-lisp-completion-persistent-action)
  8798. (persistent-help . "Show brief doc in mode-line")
  8799. (candidates-in-buffer)
  8800. (action . identity))
  8801. :prompt prompt
  8802. :buffer buffer
  8803. :input init
  8804. :history hist
  8805. :resume 'noresume
  8806. :default (or default ""))
  8807. (keyboard-quit)))
  8808. ;;; Generic completing read
  8809. ;;
  8810. ;;
  8811. (defun helm-completing-read-default-1
  8812. (prompt collection test require-match
  8813. init hist default inherit-input-method
  8814. name buffer &optional cands-in-buffer exec-when-only-one)
  8815. "Call `helm-comp-read' with same args as `completing-read'.
  8816. Extra optional arg CANDS-IN-BUFFER mean use `candidates-in-buffer'
  8817. method which is faster.
  8818. It should be used when candidate list don't need to rebuild dynamically."
  8819. (let ((history (or (car-safe hist) hist)))
  8820. (helm-comp-read
  8821. prompt collection
  8822. :test test
  8823. :history history
  8824. :input-history history
  8825. :must-match require-match
  8826. :alistp nil ; Be sure `all-completions' is used.
  8827. :name name
  8828. :requires-pattern (if (and (string= default "")
  8829. (or (eq require-match 'confirm)
  8830. (eq require-match
  8831. 'confirm-after-completion)))
  8832. 1 0)
  8833. :candidates-in-buffer cands-in-buffer
  8834. :exec-when-only-one exec-when-only-one
  8835. :buffer buffer
  8836. ;; If DEF is not provided, fallback to empty string
  8837. ;; to avoid `thing-at-point' to be appended on top of list
  8838. :default (or default "")
  8839. ;; Use `regexp-quote' to fix initial input
  8840. ;; with special characters (e.g nnimap+gmail:)
  8841. :initial-input (and (stringp init) (regexp-quote init)))))
  8842. (defun helm-completing-read-with-cands-in-buffer
  8843. (prompt collection test require-match
  8844. init hist default inherit-input-method
  8845. name buffer)
  8846. "Same as `helm-completing-read-default-1' but use candidates-in-buffer."
  8847. ;; Some commands like find-tag may use `read-file-name' from inside
  8848. ;; the calculation of collection. in this case it clash with
  8849. ;; candidates-in-buffer that reuse precedent data (files) which is wrong.
  8850. ;; So (re)calculate collection outside of main helm-session.
  8851. (let ((cands (all-completions "" collection)))
  8852. (helm-completing-read-default-1 prompt cands test require-match
  8853. init hist default inherit-input-method
  8854. name buffer t)))
  8855. (defun* helm-completing-read-default
  8856. (prompt collection &optional
  8857. predicate require-match
  8858. initial-input hist def
  8859. inherit-input-method)
  8860. "An helm replacement of `completing-read'.
  8861. This function should be used only as a `completing-read-function'.
  8862. Don't use it directly, use instead `helm-comp-read' in your programs.
  8863. See documentation of `completing-read' and `all-completions' for details."
  8864. (declare (special helm-mode))
  8865. (let* ((current-command this-command)
  8866. (str-command (symbol-name current-command))
  8867. (buf-name (format "*helm-mode-%s*" str-command))
  8868. (entry (assq current-command
  8869. helm-completing-read-handlers-alist))
  8870. (def-com (cdr-safe entry))
  8871. (str-defcom (and def-com (symbol-name def-com)))
  8872. (def-args (list prompt collection predicate require-match
  8873. initial-input hist def inherit-input-method))
  8874. ;; Append the two extra args needed to set the buffer and source name
  8875. ;; in helm specialized functions.
  8876. (any-args (append def-args (list str-command buf-name)))
  8877. helm-completion-mode-start-message ; Be quiet
  8878. helm-completion-mode-quit-message
  8879. (minibuffer-completion-table collection)
  8880. (minibuffer-completion-predicate predicate)
  8881. ;; Be sure this pesty *completion* buffer doesn't popup.
  8882. (minibuffer-setup-hook (remove 'minibuffer-completion-help
  8883. minibuffer-setup-hook)))
  8884. (when (eq def-com 'ido) (setq def-com 'ido-completing-read))
  8885. (unless (or (not entry) def-com)
  8886. ;; An entry in *read-handlers-alist exists but have
  8887. ;; a nil value, so we exit from here, disable `helm-mode'
  8888. ;; and run the command again with it original behavior.
  8889. ;; `helm-mode' will be restored on exit.
  8890. (return-from helm-completing-read-default
  8891. (unwind-protect
  8892. (progn
  8893. (helm-mode -1)
  8894. (apply completing-read-function def-args))
  8895. (helm-mode 1))))
  8896. ;; If we use now `completing-read' we MUST turn off `helm-mode'
  8897. ;; to avoid infinite recursion and CRASH. It will be reenabled on exit.
  8898. (when (or (eq def-com 'completing-read)
  8899. ;; All specialized functions are prefixed by "helm"
  8900. (and (stringp str-defcom)
  8901. (not (string-match "^helm" str-defcom))))
  8902. (helm-mode -1))
  8903. (unwind-protect
  8904. (cond (;; An helm specialized function exists, run it.
  8905. (and def-com helm-mode)
  8906. (apply def-com any-args))
  8907. (;; Try to handle `ido-completing-read' everywhere.
  8908. (and def-com (eq def-com 'ido-completing-read))
  8909. (setcar (memq collection def-args)
  8910. (all-completions "" collection predicate))
  8911. (apply def-com def-args))
  8912. (;; User set explicitely `completing-read' or something similar
  8913. ;; in *read-handlers-alist, use this with exactly the same
  8914. ;; args as in `completing-read'.
  8915. ;; If we are here `helm-mode' is now disabled.
  8916. def-com
  8917. (apply def-com def-args))
  8918. (t ; Fall back to classic `helm-comp-read'.
  8919. (helm-completing-read-default-1
  8920. prompt collection predicate require-match
  8921. initial-input hist def inherit-input-method
  8922. str-command buf-name)))
  8923. (helm-mode 1)
  8924. ;; When exiting minibuffer, `this-command' is set to
  8925. ;; `helm-exit-minibuffer', which is unwanted when starting
  8926. ;; on another `completing-read', so restore `this-command' to
  8927. ;; initial value when exiting.
  8928. (setq this-command current-command))))
  8929. (defun* helm-generic-read-file-name
  8930. (prompt &optional dir default-filename mustmatch initial predicate)
  8931. "An helm replacement of `read-file-name'."
  8932. (declare (special helm-mode))
  8933. (let* ((default (and default-filename
  8934. (if (listp default-filename)
  8935. (car default-filename)
  8936. default-filename)))
  8937. (init (or default initial dir default-directory))
  8938. (ini-input (and init (expand-file-name init)))
  8939. (current-command this-command)
  8940. (str-command (symbol-name current-command))
  8941. (helm-file-completion-sources
  8942. (cons str-command
  8943. (remove str-command helm-file-completion-sources)))
  8944. (buf-name (format "*helm-mode-%s*" str-command))
  8945. (entry (assq current-command
  8946. helm-completing-read-handlers-alist))
  8947. (def-com (cdr-safe entry))
  8948. (str-defcom (symbol-name def-com))
  8949. (def-args (list prompt dir default-filename mustmatch initial predicate))
  8950. ;; Append the two extra args needed to set the buffer and source name
  8951. ;; in helm specialized functions.
  8952. (any-args (append def-args (list str-command buf-name)))
  8953. (ido-state ido-mode)
  8954. helm-completion-mode-start-message ; Be quiet
  8955. helm-completion-mode-quit-message ; Same here
  8956. fname)
  8957. ;; Some functions that normally call `completing-read' can switch
  8958. ;; brutally to `read-file-name' (e.g find-tag), in this case
  8959. ;; the helm specialized function will fail because it is build
  8960. ;; for `completing-read', so set it to 'incompatible to be sure
  8961. ;; we switch to `helm-c-read-file-name' and don't try to call it
  8962. ;; with wrong number of args.
  8963. (when (and def-com (> (length (help-function-arglist def-com)) 8))
  8964. (setq def-com 'incompatible))
  8965. (when (eq def-com 'ido) (setq def-com 'ido-read-file-name))
  8966. (unless (or (not entry) def-com)
  8967. (return-from helm-generic-read-file-name
  8968. (unwind-protect
  8969. (progn
  8970. (helm-mode -1)
  8971. (apply read-file-name-function def-args))
  8972. (helm-mode 1))))
  8973. ;; If we use now `read-file-name' we MUST turn off `helm-mode'
  8974. ;; to avoid infinite recursion and CRASH. It will be reenabled on exit.
  8975. (when (or (eq def-com 'read-file-name)
  8976. (eq def-com 'ido-read-file-name)
  8977. (and (stringp str-defcom)
  8978. (not (string-match "^helm" str-defcom))))
  8979. (helm-mode -1))
  8980. (unwind-protect
  8981. (setq fname
  8982. (cond (;; A specialized function exists, run it
  8983. ;; with the two extra args specific to helm..
  8984. (and def-com helm-mode
  8985. (not (eq def-com 'ido-read-file-name))
  8986. (not (eq def-com 'incompatible)))
  8987. (apply def-com any-args))
  8988. (;; Def-com value is `ido-read-file-name'
  8989. ;; run it with default args.
  8990. (and def-com (eq def-com 'ido-read-file-name))
  8991. (ido-mode 1)
  8992. (apply def-com def-args))
  8993. (;; Def-com value is `read-file-name'
  8994. ;; run it with default args.
  8995. (eq def-com 'read-file-name)
  8996. (apply def-com def-args))
  8997. (t ; Fall back to classic `helm-c-read-file-name'.
  8998. (helm-c-read-file-name
  8999. prompt
  9000. :name str-command
  9001. :buffer buf-name
  9002. :initial-input (expand-file-name init dir)
  9003. :alistp nil
  9004. :must-match mustmatch
  9005. :test predicate))))
  9006. (helm-mode 1)
  9007. (ido-mode (if ido-state 1 -1))
  9008. ;; Same comment as in `helm-completing-read-default'.
  9009. (setq this-command current-command))
  9010. fname))
  9011. ;;;###autoload
  9012. (define-minor-mode helm-mode
  9013. "Toggle generic helm completion.
  9014. All functions in Emacs that use `completing-read'
  9015. or `read-file-name' and friends will use helm interface
  9016. when this mode is turned on.
  9017. However you can modify this behavior for functions of your choice
  9018. with `helm-completing-read-handlers-alist'.
  9019. Called with a positive arg, turn on unconditionally, with a
  9020. negative arg turn off.
  9021. You can turn it on with `helm-mode'.
  9022. Some crap emacs functions may not be supported,
  9023. e.g `ffap-alternate-file' and maybe others
  9024. You can add such functions to `helm-completing-read-handlers-alist'
  9025. with a nil value.
  9026. Note: This mode will work only partially on Emacs23."
  9027. :group 'helm
  9028. :global t
  9029. :lighter helm-completion-mode-string
  9030. (declare (special completing-read-function))
  9031. (if helm-mode
  9032. (progn
  9033. (setq completing-read-function 'helm-completing-read-default
  9034. read-file-name-function 'helm-generic-read-file-name)
  9035. (message helm-completion-mode-start-message))
  9036. (setq completing-read-function (and (fboundp 'completing-read-default)
  9037. 'completing-read-default)
  9038. read-file-name-function (and (fboundp 'read-file-name-default)
  9039. 'read-file-name-default))
  9040. (message helm-completion-mode-quit-message)))
  9041. ;;; Eshell completion.
  9042. ;;
  9043. ;; Enable like this in .emacs:
  9044. ;;
  9045. ;; (add-hook 'eshell-mode-hook
  9046. ;; #'(lambda ()
  9047. ;; (define-key eshell-mode-map [remap pcomplete] 'helm-esh-pcomplete)))
  9048. ;;
  9049. (defvar helm-c-source-esh
  9050. '((name . "Eshell completions")
  9051. (init . (lambda ()
  9052. (setq pcomplete-current-completions nil
  9053. pcomplete-last-completion-raw nil)
  9054. ;; Eshell-command add this hook in all minibuffers
  9055. ;; Remove it for the helm one. (Fixed in Emacs24)
  9056. (remove-hook 'minibuffer-setup-hook 'eshell-mode)))
  9057. (candidates . helm-esh-get-candidates)
  9058. (action . helm-ec-insert))
  9059. "Helm source for Eshell completion.")
  9060. ;; Internal.
  9061. (defvar helm-ec-target "")
  9062. (defun helm-ec-insert (candidate)
  9063. "Replace text at point with CANDIDATE.
  9064. The function that call this should set `helm-ec-target' to thing at point.
  9065. This is the same as `ac-insert', just inlined here for compatibility."
  9066. (let ((pt (point)))
  9067. (when (and helm-ec-target
  9068. (search-backward helm-ec-target nil t)
  9069. (string= (buffer-substring (point) pt) helm-ec-target))
  9070. (delete-region (point) pt)))
  9071. (insert (helm-quote-whitespace candidate)))
  9072. (defun helm-esh-get-candidates ()
  9073. "Get candidates for eshell completion using `pcomplete'."
  9074. (catch 'pcompleted
  9075. (let* ((pcomplete-stub)
  9076. pcomplete-seen pcomplete-norm-func
  9077. pcomplete-args pcomplete-last pcomplete-index
  9078. (pcomplete-autolist pcomplete-autolist)
  9079. (pcomplete-suffix-list pcomplete-suffix-list))
  9080. (with-helm-current-buffer
  9081. (loop with table = (pcomplete-completions)
  9082. with entry = (condition-case nil
  9083. ;; On Emacs24 `try-completion' return
  9084. ;; pattern when more than one result.
  9085. ;; Otherwise Emacs23 return nil, which
  9086. ;; is wrong, in this case use pattern
  9087. ;; to behave like Emacs24.
  9088. (or (try-completion helm-pattern
  9089. (pcomplete-entries))
  9090. helm-pattern)
  9091. ;; In Emacs23 `pcomplete-entries' may fail
  9092. ;; with error, so try this instead.
  9093. (error
  9094. nil
  9095. (let ((fc (car (last
  9096. (pcomplete-parse-arguments)))))
  9097. ;; Check if last arg require fname completion.
  9098. (and (file-name-directory fc) fc))))
  9099. for i in (all-completions pcomplete-stub table)
  9100. for file-cand = (and entry
  9101. (if (file-remote-p i) i
  9102. (expand-file-name
  9103. i (file-name-directory entry))))
  9104. if (and file-cand (or (file-remote-p file-cand)
  9105. (file-exists-p file-cand)))
  9106. collect file-cand into ls
  9107. else collect i into ls
  9108. finally return
  9109. (if (and entry (not (string= entry "")) (file-exists-p entry))
  9110. (append (list (expand-file-name entry default-directory)) ls)
  9111. ls))))))
  9112. ;;; Eshell history.
  9113. ;;
  9114. ;;
  9115. (defvar helm-c-source-eshell-history
  9116. `((name . "Eshell history")
  9117. (init . (lambda ()
  9118. (let (eshell-hist-ignoredups)
  9119. ;; Write the content's of ring to file.
  9120. (eshell-write-history eshell-history-file-name t)
  9121. (with-current-buffer (helm-candidate-buffer 'global)
  9122. (insert-file-contents eshell-history-file-name)))
  9123. ;; Same comment as in `helm-c-source-esh'
  9124. (remove-hook 'minibuffer-setup-hook 'eshell-mode)))
  9125. (candidates-in-buffer)
  9126. (keymap . ,helm-eshell-history-map)
  9127. (filtered-candidate-transformer . (lambda (candidates sources)
  9128. (reverse candidates)))
  9129. (candidate-number-limit . 9999)
  9130. (action . (lambda (candidate)
  9131. (eshell-kill-input)
  9132. (insert candidate))))
  9133. "Helm source for Eshell history.")
  9134. ;;; Show completion - an alternative of helm-show-completion.el.
  9135. ;;
  9136. ;; Provide show completion with macro `with-helm-show-completion'.
  9137. ;; Called each time cursor move in helm-buffer.
  9138. (defun helm-c-show-completion ()
  9139. (with-helm-current-buffer
  9140. (overlay-put helm-c-show-completion-overlay
  9141. 'display (helm-get-selection))))
  9142. (defun helm-c-show-completion-init-overlay (beg end)
  9143. (and helm-c-turn-on-show-completion
  9144. (setq helm-c-show-completion-overlay (make-overlay beg end))
  9145. (overlay-put helm-c-show-completion-overlay
  9146. 'face 'helm-lisp-show-completion)))
  9147. (defun helm-c-show-completion-display-function (buffer)
  9148. "A special resized helm window is used depending on position in BUFFER."
  9149. (with-selected-window (selected-window)
  9150. (let* ((screen-size (+ (count-screen-lines (window-start) (point) t)
  9151. 1 ; mode-line
  9152. (if header-line-format 1 0))) ; header-line
  9153. (def-size (- (window-height)
  9154. helm-c-show-completion-min-window-height))
  9155. (upper-height (max window-min-height (min screen-size def-size)))
  9156. split-window-keep-point)
  9157. (recenter -1)
  9158. (set-window-buffer (if (active-minibuffer-window)
  9159. (minibuffer-selected-window)
  9160. (split-window nil upper-height))
  9161. buffer))))
  9162. (defmacro with-helm-show-completion (beg end &rest body)
  9163. "Show helm candidate in an overlay at point.
  9164. BEG and END are the beginning and end position of the current completion
  9165. in `helm-current-buffer'.
  9166. BODY is an helm call where we want to enable show completion.
  9167. If `helm-c-turn-on-show-completion' is nil just do nothing."
  9168. (declare (indent 2) (debug t))
  9169. `(let ((helm-move-selection-after-hook
  9170. (and helm-c-turn-on-show-completion
  9171. (append (list 'helm-c-show-completion)
  9172. helm-move-selection-after-hook))))
  9173. (unwind-protect
  9174. (progn
  9175. (helm-c-show-completion-init-overlay ,beg ,end)
  9176. (let ((helm-display-function
  9177. (if helm-c-show-completion-use-special-display
  9178. 'helm-c-show-completion-display-function
  9179. 'helm-default-display-buffer)))
  9180. ,@body))
  9181. (and helm-c-turn-on-show-completion
  9182. (delete-overlay helm-c-show-completion-overlay)))))
  9183. ;;; Lisp symbol completion.
  9184. ;;
  9185. ;;
  9186. ;;;###autoload
  9187. (defun helm-lisp-completion-at-point ()
  9188. "Helm lisp symbol completion at point."
  9189. (interactive)
  9190. (let* ((data (lisp-completion-at-point))
  9191. (beg (car data))
  9192. (end (point)) ; 'cadr data' is wrong when no space after point.
  9193. (plist (nthcdr 3 data))
  9194. (pred (plist-get plist :predicate))
  9195. (lgst-len 0)
  9196. (target (and beg end (buffer-substring-no-properties beg end)))
  9197. (candidates (all-completions target (nth 2 data) pred))
  9198. (helm-quit-if-no-candidate t)
  9199. (helm-execute-action-at-once-if-one t)
  9200. (helm-match-plugin-enabled
  9201. (member 'helm-compile-source--match-plugin
  9202. helm-compile-source-functions)))
  9203. (if candidates
  9204. (with-helm-show-completion beg end
  9205. ;; Overlay is initialized now in helm-current-buffer.
  9206. (helm
  9207. :sources
  9208. '((name . "Lisp completion")
  9209. (init . (lambda ()
  9210. (with-current-buffer (helm-candidate-buffer 'global)
  9211. (loop for sym in candidates
  9212. for len = (length sym)
  9213. when (> len lgst-len) do (setq lgst-len len)
  9214. do (insert (concat sym "\n"))))))
  9215. (candidates-in-buffer)
  9216. (persistent-action . helm-lisp-completion-persistent-action)
  9217. (persistent-help . "Show brief doc in mode-line")
  9218. (filtered-candidate-transformer helm-lisp-completion-transformer)
  9219. (action . (lambda (candidate)
  9220. (delete-region beg end)
  9221. (insert candidate))))
  9222. :input (if helm-match-plugin-enabled (concat target " ") target)))
  9223. (message "[No Match]"))))
  9224. (defun helm-lisp-completion-persistent-action (candidate)
  9225. (let ((cursor-in-echo-area t)
  9226. mode-line-in-non-selected-windows)
  9227. (helm-c-show-info-in-mode-line
  9228. (propertize
  9229. (helm-c-get-first-line-documentation
  9230. (intern candidate))
  9231. 'face 'helm-lisp-completion-info))))
  9232. (defun helm-lisp-completion-transformer (candidates source)
  9233. "Helm candidates transformer for lisp completion."
  9234. (declare (special lgst-len))
  9235. (loop for c in candidates
  9236. for sym = (intern c)
  9237. for annot = (cond ((commandp sym) " (Com)")
  9238. ((fboundp sym) " (Fun)")
  9239. ((boundp sym) " (Var)")
  9240. ((facep sym) " (Face)"))
  9241. for spaces = (make-string (- lgst-len (length c)) ? )
  9242. collect (cons (concat c spaces annot) c)))
  9243. (defun helm-c-get-first-line-documentation (sym)
  9244. "Return first line documentation of symbol SYM.
  9245. If SYM is not documented, return \"Not documented\"."
  9246. (let ((doc (cond ((fboundp sym)
  9247. (documentation sym t))
  9248. ((boundp sym)
  9249. (documentation-property sym 'variable-documentation t))
  9250. ((facep sym)
  9251. (face-documentation sym))
  9252. (t nil))))
  9253. (if (and doc (not (string= doc ""))
  9254. ;; `documentation' return "\n\n(args...)"
  9255. ;; for CL-style functions.
  9256. (not (string-match-p "^\n\n" doc)))
  9257. (car (split-string doc "\n"))
  9258. "Not documented")))
  9259. ;;; File completion.
  9260. ;;
  9261. ;; Complete file name at point.
  9262. (defun helm-c-thing-before-point ()
  9263. "Get symbol name before point.
  9264. Borrowed from helm-complete.el, inlined here for compatibility."
  9265. (save-excursion
  9266. (let ((beg (point)))
  9267. ;; older regexp "\(\\|\\s-\\|^\\|\\_<\\|\r\\|'\\|#'"
  9268. (when (re-search-backward
  9269. "\\_<" (field-beginning nil nil (point-at-bol)) t)
  9270. (buffer-substring-no-properties beg (match-end 0))))))
  9271. ;;;###autoload
  9272. (defun helm-c-complete-file-name-at-point ()
  9273. "Complete file name at point."
  9274. (interactive)
  9275. (let* ((init (substring-no-properties (thing-at-point 'filename)))
  9276. (end (point))
  9277. (beg (- (point) (length init)))
  9278. (helm-quit-if-no-candidate t)
  9279. (helm-execute-action-at-once-if-one t)
  9280. completion)
  9281. (with-helm-show-completion beg end
  9282. (setq completion (helm-c-read-file-name "FileName: "
  9283. :initial-input init)))
  9284. (helm-c-insert-file-name-completion-at-point completion)))
  9285. ;; Internal
  9286. (defvar helm-lisp-completion-counter 0)
  9287. ;;;###autoload
  9288. (defun helm-lisp-completion-at-point-or-indent (arg)
  9289. "First call indent and second call complete lisp symbol.
  9290. The second call should happen before `helm-lisp-completion-or-indent-delay',
  9291. after this delay, next call will indent again.
  9292. After completion, next call is always indent.
  9293. See that like click and double mouse click.
  9294. One hit indent, two quick hits maybe indent and complete."
  9295. (interactive "P")
  9296. ;; Be sure `indent-for-tab-command' will not try
  9297. ;; to use `completion-at-point'.
  9298. (let ((tab-always-indent (if (eq tab-always-indent 'complete)
  9299. t tab-always-indent)))
  9300. (incf helm-lisp-completion-counter)
  9301. (unwind-protect
  9302. (if (> helm-lisp-completion-counter 1)
  9303. (helm-lisp-completion-or-file-name-at-point)
  9304. (indent-for-tab-command arg))
  9305. ;; After `helm-lisp-completion-or-indent-delay' seconds
  9306. ;; reset to 0.
  9307. (run-with-timer helm-lisp-completion-or-indent-delay nil
  9308. #'(lambda ()
  9309. (setq helm-lisp-completion-counter 0)))
  9310. ;; Always reset to 0 at second hit.
  9311. (when (eq helm-lisp-completion-counter 2)
  9312. (setq helm-lisp-completion-counter 0)))))
  9313. ;;;###autoload
  9314. (defun helm-lisp-completion-or-file-name-at-point ()
  9315. "Complete lisp symbol or filename at point.
  9316. Filename completion happen if filename is started in
  9317. or between double quotes."
  9318. (interactive)
  9319. (let ((tap (substring-no-properties (thing-at-point 'filename))))
  9320. (if (and tap (string-match "^\\(~/\\|/\\|[a-zA-Z]\:/\\).*" tap)
  9321. (save-excursion (search-backward "\"" (point-at-bol) t)))
  9322. (helm-c-complete-file-name-at-point)
  9323. (helm-lisp-completion-at-point))))
  9324. (defun helm-c-apropos-init (test default)
  9325. "Init candidates buffer for `helm-c-apropos' sources."
  9326. (with-current-buffer (helm-candidate-buffer 'global)
  9327. (goto-char (point-min))
  9328. (when (and default (stringp default)
  9329. ;; Some defaults args result as
  9330. ;; (symbol-name nil) == "nil".
  9331. ;; e.g debug-on-entry.
  9332. (not (string= default "nil"))
  9333. (funcall test (intern default)))
  9334. (insert (concat default "\n")))
  9335. (loop with all = (all-completions "" obarray test)
  9336. for sym in all
  9337. unless (and default (eq sym default))
  9338. do (insert (concat sym "\n")))))
  9339. ;;; Run Externals commands within Emacs with helm completion
  9340. ;;
  9341. ;;
  9342. (defvar helm-external-command-history nil)
  9343. (defun helm-c-external-commands-list-1 (&optional sort)
  9344. "Returns a list of all external commands the user can execute.
  9345. If `helm-c-external-commands-list' is non-nil it will
  9346. return its contents. Else it calculates all external commands
  9347. and sets `helm-c-external-commands-list'."
  9348. (if helm-c-external-commands-list
  9349. helm-c-external-commands-list
  9350. (setq helm-c-external-commands-list
  9351. (loop
  9352. with paths = (split-string (getenv "PATH") path-separator)
  9353. with completions = ()
  9354. for dir in paths
  9355. when (and (file-exists-p dir) (file-accessible-directory-p dir))
  9356. for lsdir = (loop for i in (directory-files dir t)
  9357. for bn = (file-name-nondirectory i)
  9358. when (and (not (member bn completions))
  9359. (not (file-directory-p i))
  9360. (file-executable-p i))
  9361. collect bn)
  9362. append lsdir into completions
  9363. finally return (if sort (sort completions 'string-lessp) completions)))))
  9364. (defun helm-run-or-raise (exe &optional file)
  9365. "Generic command that run asynchronously EXE.
  9366. If EXE is already running just jump to his window if `helm-raise-command'
  9367. is non--nil.
  9368. When FILE argument is provided run EXE with FILE.
  9369. In this case EXE must be provided as \"EXE %s\"."
  9370. (lexical-let* ((real-com (car (split-string (replace-regexp-in-string
  9371. "%s" "" exe))))
  9372. (proc (if file (concat real-com " " file) real-com)))
  9373. (if (get-process proc)
  9374. (if helm-raise-command
  9375. (shell-command (format helm-raise-command real-com))
  9376. (error "Error: %s is already running" real-com))
  9377. (when (loop for i in helm-c-external-commands-list thereis real-com)
  9378. (message "Starting %s..." real-com)
  9379. (if file
  9380. (start-process-shell-command
  9381. proc nil (format exe (shell-quote-argument
  9382. (if (eq system-type 'windows-nt)
  9383. (helm-w32-prepare-filename file)
  9384. file))))
  9385. (start-process-shell-command proc nil real-com))
  9386. (set-process-sentinel
  9387. (get-process proc)
  9388. #'(lambda (process event)
  9389. (when (and (string= event "finished\n")
  9390. helm-raise-command
  9391. (not (helm-c-get-pid-from-process-name real-com)))
  9392. (shell-command (format helm-raise-command "emacs")))
  9393. (message "%s process...Finished." process))))
  9394. (setq helm-c-external-commands-list
  9395. (cons real-com
  9396. (delete real-com helm-c-external-commands-list))))))
  9397. ;;; Generic action functions
  9398. ;;
  9399. ;;
  9400. (defun helm-c-file-buffers (filename)
  9401. "Returns a list of buffer names corresponding to FILENAME."
  9402. (let ((name (expand-file-name filename))
  9403. (buf-list ()))
  9404. (dolist (buf (buffer-list) buf-list)
  9405. (let ((bfn (buffer-file-name buf)))
  9406. (when (and bfn (string= name bfn))
  9407. (push (buffer-name buf) buf-list))))))
  9408. (defun helm-revert-buffer (candidate)
  9409. (with-current-buffer candidate
  9410. (when (or (buffer-modified-p)
  9411. (not (verify-visited-file-modtime
  9412. (get-buffer candidate))))
  9413. (revert-buffer t t))))
  9414. (defun helm-revert-marked-buffers (ignore)
  9415. (mapc 'helm-revert-buffer (helm-marked-candidates)))
  9416. (defun helm-kill-marked-buffers (ignore)
  9417. (mapc 'kill-buffer (helm-marked-candidates)))
  9418. (defun helm-c-delete-file (file &optional error-if-dot-file-p)
  9419. "Delete the given file after querying the user.
  9420. Ask to kill buffers associated with that file, too."
  9421. (when (and error-if-dot-file-p
  9422. (helm-ff-dot-file-p file))
  9423. (error "Error: Cannot operate on `.' or `..'"))
  9424. (let ((buffers (helm-c-file-buffers file)))
  9425. (if (< emacs-major-version 24)
  9426. ;; `dired-delete-file' in Emacs versions < 24
  9427. ;; doesn't support delete-by-moving-to-trash
  9428. ;; so use `delete-directory' and `delete-file'
  9429. ;; that handle it.
  9430. (cond ((and (not (file-symlink-p file))
  9431. (file-directory-p file)
  9432. (directory-files file t dired-re-no-dot))
  9433. (when (y-or-n-p (format "Recursive delete of `%s'? " file))
  9434. (delete-directory file 'recursive)))
  9435. ((and (not (file-symlink-p file))
  9436. (file-directory-p file))
  9437. (delete-directory file))
  9438. (t (delete-file file)))
  9439. (dired-delete-file
  9440. file 'dired-recursive-deletes delete-by-moving-to-trash))
  9441. (when buffers
  9442. (dolist (buf buffers)
  9443. (when (y-or-n-p (format "Kill buffer %s, too? " buf))
  9444. (kill-buffer buf))))))
  9445. (defun helm-get-mailcap-for-file (filename)
  9446. "Get the command to use for FILENAME from mailcap files.
  9447. The command is like <command %s> and is meant to use with `format'."
  9448. (mailcap-parse-mailcaps)
  9449. (let* ((ext (file-name-extension filename))
  9450. (mime (when ext (mailcap-extension-to-mime ext)))
  9451. (result (when mime (mailcap-mime-info mime))))
  9452. ;; If elisp file have no associations in .mailcap
  9453. ;; `mailcap-maybe-eval' is returned, in this case just return nil.
  9454. (when (stringp result) result)))
  9455. (defun helm-get-default-program-for-file (filename)
  9456. "Try to find a default program to open FILENAME.
  9457. Try first in `helm-c-external-programs-associations' and then in mailcap file
  9458. if nothing found return nil."
  9459. (let* ((ext (file-name-extension filename))
  9460. (def-prog (assoc-default ext helm-c-external-programs-associations)))
  9461. (cond ((and def-prog (not (string= def-prog "")))
  9462. (concat def-prog " %s"))
  9463. ((and helm-c-default-external-file-browser
  9464. (file-directory-p filename))
  9465. (concat helm-c-default-external-file-browser " %s"))
  9466. (t (helm-get-mailcap-for-file filename)))))
  9467. (defun helm-c-open-file-externally (file)
  9468. "Open FILE with an external program.
  9469. Try to guess which program to use with `helm-get-default-program-for-file'.
  9470. If not found or a prefix arg is given query the user which tool to use."
  9471. (let* ((fname (expand-file-name file))
  9472. (collection (helm-c-external-commands-list-1 'sort))
  9473. (def-prog (helm-get-default-program-for-file fname))
  9474. (real-prog-name (if (or helm-current-prefix-arg (not def-prog))
  9475. ;; Prefix arg or no default program.
  9476. (prog1
  9477. (helm-comp-read
  9478. "Program: " collection
  9479. :must-match t
  9480. :name "Open file Externally"
  9481. :history helm-external-command-history)
  9482. ;; Always prompt to set this program as default.
  9483. (setq def-prog nil))
  9484. ;; No prefix arg or default program exists.
  9485. (replace-regexp-in-string " %s\\| '%s'" "" def-prog)))
  9486. (program (concat real-prog-name " %s")))
  9487. (unless (or def-prog ; Association exists, no need to record it.
  9488. ;; Don't try to record non--filenames associations (e.g urls).
  9489. (not (file-exists-p fname)))
  9490. (when
  9491. (y-or-n-p
  9492. (format
  9493. "Do you want to make `%s' the default program for this kind of files? "
  9494. real-prog-name))
  9495. (helm-aif (assoc (file-name-extension fname)
  9496. helm-c-external-programs-associations)
  9497. (setq helm-c-external-programs-associations
  9498. (delete it helm-c-external-programs-associations)))
  9499. (push (cons (file-name-extension fname)
  9500. (read-string
  9501. "Program (Add args maybe and confirm): " real-prog-name))
  9502. helm-c-external-programs-associations)
  9503. (customize-save-variable 'helm-c-external-programs-associations
  9504. helm-c-external-programs-associations)))
  9505. (helm-run-or-raise program file)
  9506. (setq helm-external-command-history
  9507. (cons real-prog-name
  9508. (delete real-prog-name
  9509. (loop for i in helm-external-command-history
  9510. when (executable-find i) collect i))))))
  9511. (defun helm-c-find-file-or-marked (candidate)
  9512. "Open file CANDIDATE or open helm marked files in background."
  9513. (let ((marked (helm-marked-candidates))
  9514. (ffap-newfile-prompt helm-ff-newfile-prompt-p)
  9515. (find-file-wildcards nil))
  9516. (if (> (length marked) 1)
  9517. ;; Open all marked files in background and display
  9518. ;; the first one.
  9519. (progn (mapc 'find-file-noselect (cdr marked))
  9520. (find-file (car marked)))
  9521. (if (and (not (file-exists-p candidate))
  9522. (and ffap-url-regexp
  9523. (not (string-match ffap-url-regexp candidate)))
  9524. (string-match "/$" candidate))
  9525. ;; A a non--existing filename ending with /
  9526. ;; Create a directory and jump to it.
  9527. (when (y-or-n-p (format "Create directory `%s'? " candidate))
  9528. (let ((dirfname (directory-file-name candidate)))
  9529. (if (file-exists-p dirfname)
  9530. (error "Mkdir: Unable to create directory `%s': file exists."
  9531. (helm-c-basename dirfname))
  9532. (make-directory candidate 'parent)))
  9533. (helm-find-files-1 candidate))
  9534. ;; A non--existing filename NOT ending with / or
  9535. ;; an existing filename, create or jump to it.
  9536. (find-file-at-point (car marked))))))
  9537. (defun helm-delete-marked-files (ignore)
  9538. (let* ((files (helm-marked-candidates))
  9539. (len (length files)))
  9540. (if (not (y-or-n-p
  9541. (format "Delete *%s File(s):\n%s"
  9542. len
  9543. (mapconcat (lambda (f) (format "- %s\n" f)) files ""))))
  9544. (message "(No deletions performed)")
  9545. (dolist (i files)
  9546. (set-text-properties 0 (length i) nil i)
  9547. (helm-c-delete-file i helm-ff-signal-error-on-dot-files))
  9548. (message "%s File(s) deleted" len))))
  9549. (defun helm-ediff-marked-buffers (candidate &optional merge)
  9550. "Ediff 2 marked buffers or CANDIDATE and `helm-current-buffer'.
  9551. With optional arg MERGE call `ediff-merge-buffers'."
  9552. (let ((lg-lst (length (helm-marked-candidates)))
  9553. buf1 buf2)
  9554. (case lg-lst
  9555. (0
  9556. (error "Error:You have to mark at least 1 buffer"))
  9557. (1
  9558. (setq buf1 helm-current-buffer
  9559. buf2 (first (helm-marked-candidates))))
  9560. (2
  9561. (setq buf1 (first (helm-marked-candidates))
  9562. buf2 (second (helm-marked-candidates))))
  9563. (t
  9564. (error "Error:To much buffers marked!")))
  9565. (if merge
  9566. (ediff-merge-buffers buf1 buf2)
  9567. (ediff-buffers buf1 buf2))))
  9568. (defun helm-ediff-marked-buffers-merge (candidate)
  9569. "Ediff merge `helm-current-buffer' with CANDIDATE.
  9570. See `helm-ediff-marked-buffers'."
  9571. (helm-ediff-marked-buffers candidate t))
  9572. (defun helm-bookmark-get-bookmark-from-name (bmk)
  9573. "Return bookmark name even if it is a bookmark with annotation.
  9574. e.g prepended with *.
  9575. Return nil if bmk is not a valid bookmark."
  9576. (let ((bookmark (replace-regexp-in-string "\*" "" bmk)))
  9577. (if (assoc bookmark bookmark-alist)
  9578. bookmark
  9579. (when (assoc bmk bookmark-alist)
  9580. bmk))))
  9581. (defun helm-delete-marked-bookmarks (ignore)
  9582. "Delete this bookmark or all marked bookmarks."
  9583. (dolist (i (helm-marked-candidates))
  9584. (bookmark-delete (helm-bookmark-get-bookmark-from-name i)
  9585. 'batch)))
  9586. (defun helm-require-or-error (feature function)
  9587. (or (require feature nil t)
  9588. (error "Need %s to use `%s'." feature function)))
  9589. (defun helm-find-buffer-on-elscreen (candidate)
  9590. "Open buffer in new screen, if marked buffers open all in elscreens."
  9591. (helm-require-or-error 'elscreen 'helm-find-buffer-on-elscreen)
  9592. (helm-aif (helm-marked-candidates)
  9593. (dolist (i it)
  9594. (let ((target-screen (elscreen-find-screen-by-buffer
  9595. (get-buffer i) 'create)))
  9596. (elscreen-goto target-screen)))
  9597. (let ((target-screen (elscreen-find-screen-by-buffer
  9598. (get-buffer candidate) 'create)))
  9599. (elscreen-goto target-screen))))
  9600. (defun helm-elscreen-find-file (file)
  9601. (helm-require-or-error 'elscreen 'helm-elscreen-find-file)
  9602. (elscreen-find-file file))
  9603. (defun helm-w32-prepare-filename (file)
  9604. "Convert filename FILE to something usable by external w32 executables."
  9605. (replace-regexp-in-string ; For UNC paths
  9606. "/" "\\"
  9607. (replace-regexp-in-string ; Strip cygdrive paths
  9608. "/cygdrive/\\(.\\)" "\\1:"
  9609. file nil nil) nil t))
  9610. ;;;###autoload
  9611. (defun helm-w32-shell-execute-open-file (file)
  9612. (interactive "fOpen file:")
  9613. (with-no-warnings
  9614. (w32-shell-execute "open" (helm-w32-prepare-filename file))))
  9615. (defun helm-c-open-file-with-default-tool (file)
  9616. "Open FILE with the default tool on this platform."
  9617. (if (eq system-type 'windows-nt)
  9618. (helm-w32-shell-execute-open-file file)
  9619. (start-process "helm-c-open-file-with-default-tool"
  9620. nil
  9621. (cond ((eq system-type 'gnu/linux)
  9622. "xdg-open")
  9623. ((or (eq system-type 'darwin) ;; Mac OS X
  9624. (eq system-type 'macos)) ;; Mac OS 9
  9625. "open"))
  9626. file)))
  9627. (defun helm-c-open-dired (file)
  9628. "Opens a dired buffer in FILE's directory. If FILE is a
  9629. directory, open this directory."
  9630. (if (file-directory-p file)
  9631. (dired file)
  9632. (dired (file-name-directory file))
  9633. (dired-goto-file file)))
  9634. (defun helm-c-display-to-real-line (candidate)
  9635. (if (string-match "^ *\\([0-9]+\\):\\(.*\\)$" candidate)
  9636. (list (string-to-number (match-string 1 candidate)) (match-string 2 candidate))
  9637. (error "Line number not found")))
  9638. (defun helm-c-action-line-goto (lineno-and-content)
  9639. (apply #'helm-goto-file-line (helm-interpret-value (helm-attr 'target-file))
  9640. (append lineno-and-content
  9641. (list (if (and (helm-attr-defined 'target-file)
  9642. (not helm-in-persistent-action))
  9643. 'find-file-other-window
  9644. 'find-file)))))
  9645. (defun* helm-c-action-file-line-goto (file-line-content &optional (find-file-function #'find-file))
  9646. (apply #'helm-goto-file-line
  9647. (if (stringp file-line-content)
  9648. ;; Case: filtered-candidate-transformer is skipped
  9649. (cdr (helm-c-filtered-candidate-transformer-file-line-1 file-line-content))
  9650. file-line-content)))
  9651. (require 'compile)
  9652. (defun helm-c-filtered-candidate-transformer-file-line (candidates source)
  9653. (delq nil (mapcar 'helm-c-filtered-candidate-transformer-file-line-1 candidates)))
  9654. (defun helm-c-filtered-candidate-transformer-file-line-1 (candidate)
  9655. (when (string-match "^\\(.+?\\):\\([0-9]+\\):\\(.*\\)$" candidate)
  9656. (let ((filename (match-string 1 candidate))
  9657. (lineno (match-string 2 candidate))
  9658. (content (match-string 3 candidate)))
  9659. (cons (format "%s:%s\n %s"
  9660. (propertize filename 'face compilation-info-face)
  9661. (propertize lineno 'face compilation-line-face)
  9662. content)
  9663. (list (expand-file-name
  9664. filename
  9665. (or (helm-interpret-value (helm-attr 'default-directory))
  9666. (and (helm-candidate-buffer)
  9667. (buffer-local-value
  9668. 'default-directory (helm-candidate-buffer)))))
  9669. (string-to-number lineno) content)))))
  9670. (defun* helm-goto-file-line (file lineno content &optional (find-file-function #'find-file))
  9671. (helm-aif (helm-attr 'before-jump-hook)
  9672. (funcall it))
  9673. (when file (funcall find-file-function file))
  9674. (if (helm-attr-defined 'adjust)
  9675. (helm-c-goto-line-with-adjustment lineno content)
  9676. (helm-goto-line lineno))
  9677. (unless (helm-attr-defined 'recenter)
  9678. (set-window-start (get-buffer-window helm-current-buffer) (point)))
  9679. (helm-aif (helm-attr 'after-jump-hook)
  9680. (funcall it))
  9681. (when helm-in-persistent-action
  9682. (helm-match-line-color-current-line)))
  9683. (defun helm-find-file-as-root (candidate)
  9684. (find-file (concat "/" helm-su-or-sudo "::" (expand-file-name candidate))))
  9685. (defun helm-find-many-files (ignore)
  9686. (mapc 'find-file (helm-marked-candidates)))
  9687. ;; borrowed from etags.el
  9688. ;; (helm-c-goto-line-with-adjustment (line-number-at-pos) ";; borrowed from etags.el")
  9689. (defun helm-c-goto-line-with-adjustment (line line-content)
  9690. (let ((startpos)
  9691. offset found pat)
  9692. ;; This constant is 1/2 the initial search window.
  9693. ;; There is no sense in making it too small,
  9694. ;; since just going around the loop once probably
  9695. ;; costs about as much as searching 2000 chars.
  9696. (setq offset 1000
  9697. found nil
  9698. pat (concat (if (eq selective-display t)
  9699. "\\(^\\|\^m\\) *" "^ *") ;allow indent
  9700. (regexp-quote line-content)))
  9701. ;; If no char pos was given, try the given line number.
  9702. (setq startpos (progn (helm-goto-line line) (point)))
  9703. (or startpos (setq startpos (point-min)))
  9704. ;; First see if the tag is right at the specified location.
  9705. (goto-char startpos)
  9706. (setq found (looking-at pat))
  9707. (while (and (not found)
  9708. (progn
  9709. (goto-char (- startpos offset))
  9710. (not (bobp))))
  9711. (setq found
  9712. (re-search-forward pat (+ startpos offset) t)
  9713. offset (* 3 offset))) ; expand search window
  9714. (or found
  9715. (re-search-forward pat nil t)
  9716. (error "not found")))
  9717. ;; Position point at the right place
  9718. ;; if the search string matched an extra Ctrl-m at the beginning.
  9719. (and (eq selective-display t)
  9720. (looking-at "\^m")
  9721. (forward-char 1))
  9722. (beginning-of-line))
  9723. (helm-document-attribute 'default-directory "type . file-line"
  9724. "`default-directory' to interpret file.")
  9725. (helm-document-attribute 'before-jump-hook "type . file-line / line"
  9726. "Function to call before jumping to the target location.")
  9727. (helm-document-attribute 'after-jump-hook "type . file-line / line"
  9728. "Function to call after jumping to the target location.")
  9729. (helm-document-attribute 'adjust "type . file-line"
  9730. "Search around line matching line contents.")
  9731. (helm-document-attribute 'recenter "type . file-line / line"
  9732. "`recenter' after jumping.")
  9733. (helm-document-attribute 'target-file "type . line"
  9734. "Goto line of target-file.")
  9735. ;;;###autoload
  9736. (defun helm-c-call-interactively (cmd-or-name)
  9737. "Execute CMD-OR-NAME as Emacs command.
  9738. It is added to `extended-command-history'.
  9739. `helm-current-prefix-arg' is used as the command's prefix argument."
  9740. (setq extended-command-history
  9741. (cons (helm-c-stringify cmd-or-name)
  9742. (delete (helm-c-stringify cmd-or-name) extended-command-history)))
  9743. (let ((current-prefix-arg helm-current-prefix-arg)
  9744. (cmd (helm-c-symbolify cmd-or-name)))
  9745. (if (stringp (symbol-function cmd))
  9746. (execute-kbd-macro (symbol-function cmd))
  9747. (setq this-command cmd)
  9748. (call-interactively cmd))))
  9749. ;;;###autoload
  9750. (defun helm-c-set-variable (var)
  9751. "Set value to VAR interactively."
  9752. (interactive)
  9753. (let ((sym (helm-c-symbolify var)))
  9754. (set sym (eval-minibuffer (format "Set %s: " var)
  9755. (prin1-to-string (symbol-value sym))))))
  9756. ;; (setq hh 12)
  9757. ;; (helm-c-set-variable 'hh)
  9758. ;;; Persistent Action Helpers
  9759. ;;
  9760. ;;
  9761. (defvar helm-match-line-overlay-face nil)
  9762. (defvar helm-match-line-overlay nil)
  9763. (defun helm-match-line-color-current-line (&optional start end buf face rec)
  9764. "Highlight and underline current position"
  9765. (let ((args (list (or start (line-beginning-position))
  9766. (or end (1+ (line-end-position)))
  9767. buf)))
  9768. (if (not helm-match-line-overlay)
  9769. (setq helm-match-line-overlay (apply 'make-overlay args))
  9770. (apply 'move-overlay helm-match-line-overlay args)))
  9771. (overlay-put helm-match-line-overlay
  9772. 'face (or face helm-match-line-overlay-face))
  9773. (when rec
  9774. (goto-char start)
  9775. (recenter)))
  9776. (defalias 'helm-persistent-highlight-point 'helm-match-line-color-current-line)
  9777. (setq helm-match-line-overlay-face 'helm-overlay-line-face)
  9778. (defun helm-match-line-cleanup ()
  9779. (when helm-match-line-overlay
  9780. (delete-overlay helm-match-line-overlay)
  9781. (setq helm-match-line-overlay nil)))
  9782. (defun helm-match-line-update ()
  9783. (when helm-match-line-overlay
  9784. (delete-overlay helm-match-line-overlay)
  9785. (helm-match-line-color-current-line)))
  9786. (add-hook 'helm-cleanup-hook 'helm-match-line-cleanup)
  9787. (add-hook 'helm-after-persistent-action-hook 'helm-match-line-update)
  9788. ;;; Actions Transformers
  9789. ;;
  9790. ;;
  9791. ;;; Files
  9792. (defun helm-c-transform-file-load-el (actions candidate)
  9793. "Add action to load the file CANDIDATE if it is an emacs lisp
  9794. file. Else return ACTIONS unmodified."
  9795. (if (member (file-name-extension candidate) '("el" "elc"))
  9796. (append actions '(("Load Emacs Lisp File" . load-file)))
  9797. actions))
  9798. (defun helm-c-transform-file-browse-url (actions candidate)
  9799. "Add an action to browse the file CANDIDATE if it in a html
  9800. file or URL. Else return ACTIONS unmodified."
  9801. (let ((browse-action '("Browse with Browser" . browse-url)))
  9802. (cond ((string-match "^http\\|^ftp" candidate)
  9803. (cons browse-action actions))
  9804. ((string-match "\\.html?$" candidate)
  9805. (append actions (list browse-action)))
  9806. (t actions))))
  9807. ;;; Function
  9808. (defun helm-c-transform-function-call-interactively (actions candidate)
  9809. "Add an action to call the function CANDIDATE interactively if
  9810. it is a command. Else return ACTIONS unmodified."
  9811. (if (commandp (intern-soft candidate))
  9812. (append actions '(("Call Interactively"
  9813. .
  9814. helm-c-call-interactively)))
  9815. actions))
  9816. ;;;; S-Expressions
  9817. (defun helm-c-transform-sexp-eval-command-sexp (actions candidate)
  9818. "If CANDIDATE's `car' is a command, then add an action to
  9819. evaluate it and put it onto the `command-history'."
  9820. (if (commandp (car (read candidate)))
  9821. ;; Make it first entry
  9822. (cons '("Eval and put onto command-history" .
  9823. (lambda (sexp)
  9824. (let ((sym (read sexp)))
  9825. (eval sym)
  9826. (setq command-history
  9827. (cons sym command-history)))))
  9828. actions)
  9829. actions))
  9830. ;;; Candidate Transformers
  9831. ;;
  9832. ;;
  9833. ;;; Buffers
  9834. (defun helm-c-skip-boring-buffers (buffers)
  9835. (helm-c-skip-entries buffers helm-c-boring-buffer-regexp))
  9836. (defun helm-c-skip-current-buffer (buffers)
  9837. "[DEPRECATED] Skip current buffer in buffer lists.
  9838. This transformer should not be used as this is now handled directly
  9839. in `helm-c-buffer-list' and `helm-c-highlight-buffers'."
  9840. (if helm-allow-skipping-current-buffer
  9841. (remove (buffer-name helm-current-buffer) buffers)
  9842. buffers))
  9843. (defun helm-c-shadow-boring-buffers (buffers)
  9844. "Buffers matching `helm-c-boring-buffer-regexp' will be
  9845. displayed with the `file-name-shadow' face if available."
  9846. (helm-c-shadow-entries buffers helm-c-boring-buffer-regexp))
  9847. (defvar helm-c-buffer-display-string-functions
  9848. '(helm-c-buffer-display-string--compilation
  9849. helm-c-buffer-display-string--shell
  9850. helm-c-buffer-display-string--eshell)
  9851. "Functions to setup display string for buffer.
  9852. Function has one argument, buffer name.
  9853. If it returns string, use it.
  9854. If it returns nil, display buffer name.
  9855. See `helm-c-buffer-display-string--compilation' for example.")
  9856. (defun helm-c-transform-buffer-display-string (buffers)
  9857. "Setup display string for buffer candidates
  9858. using `helm-c-buffer-display-string-functions'."
  9859. (loop for buf in buffers
  9860. if (consp buf)
  9861. collect buf
  9862. else
  9863. for disp = (progn (set-buffer buf)
  9864. (run-hook-with-args-until-success
  9865. 'helm-c-buffer-display-string-functions buf))
  9866. collect (if disp (cons disp buf) buf)))
  9867. (defun helm-c-buffer-display-string--compilation (buf)
  9868. (helm-aif (car compilation-arguments)
  9869. (format "%s: %s [%s]" buf it default-directory)))
  9870. (defun helm-c-buffer-display-string--eshell (buf)
  9871. (declare (special eshell-history-ring))
  9872. (when (eq major-mode 'eshell-mode)
  9873. (format "%s: %s [%s]" buf
  9874. (ignore-errors (ring-ref eshell-history-ring 0))
  9875. default-directory)))
  9876. (defun helm-c-buffer-display-string--shell (buf)
  9877. (when (eq major-mode 'shell-mode)
  9878. (format "%s: %s [%s]" buf
  9879. (ignore-errors (ring-ref comint-input-ring 0))
  9880. default-directory)))
  9881. ;;; Files
  9882. (defun helm-c-shadow-boring-files (files)
  9883. "Files matching `helm-c-boring-file-regexp' will be
  9884. displayed with the `file-name-shadow' face if available."
  9885. (helm-c-shadow-entries files helm-c-boring-file-regexp))
  9886. (defun helm-c-skip-boring-files (files)
  9887. "Files matching `helm-c-boring-file-regexp' will be skipped."
  9888. (helm-c-skip-entries files helm-c-boring-file-regexp))
  9889. ;; (helm-c-skip-boring-files '("README" "/src/.svn/hoge"))
  9890. (defun helm-c-skip-current-file (files)
  9891. "Current file will be skipped."
  9892. (remove (buffer-file-name helm-current-buffer) files))
  9893. (defun helm-c-w32-pathname-transformer (args)
  9894. "Change undesirable features of windows pathnames to ones more acceptable to
  9895. other candidate transformers."
  9896. (if (eq system-type 'windows-nt)
  9897. (helm-transform-mapcar
  9898. (lambda (x)
  9899. (replace-regexp-in-string
  9900. "/cygdrive/\\(.\\)" "\\1:"
  9901. (replace-regexp-in-string "\\\\" "/" x)))
  9902. args)
  9903. args))
  9904. (defun helm-c-shorten-home-path (files)
  9905. "Replaces /home/user with ~."
  9906. (let ((home (replace-regexp-in-string "\\\\" "/" ; stupid Windows...
  9907. (getenv "HOME"))))
  9908. (helm-transform-mapcar
  9909. (lambda (file)
  9910. (if (and (stringp file) (string-match home file))
  9911. (cons (replace-match "~" nil nil file) file)
  9912. file))
  9913. files)))
  9914. ;;; Functions
  9915. (defun helm-c-mark-interactive-functions (functions)
  9916. "Mark interactive functions (commands) with (i) after the function name."
  9917. (let (list)
  9918. (loop for function in functions
  9919. do (push (cons (concat function
  9920. (when (commandp (intern-soft function)) " (i)"))
  9921. function)
  9922. list)
  9923. finally (return (nreverse list)))))
  9924. ;;; Adaptive Sorting of Candidates
  9925. ;;
  9926. ;;
  9927. ;; Internal
  9928. (defvar helm-c-adaptive-done nil
  9929. "nil if history information is not yet stored for the current
  9930. selection.")
  9931. (defvar helm-c-adaptive-history nil
  9932. "Contains the stored history information.
  9933. Format: ((SOURCE-NAME (SELECTED-CANDIDATE (PATTERN . NUMBER-OF-USE) ...) ...) ...)")
  9934. ;; Should run at beginning of `helm-initial-setup'.
  9935. (add-hook 'helm-before-initialize-hook #'(lambda ()
  9936. (when helm-c-use-adaptative-sorting
  9937. (setq helm-c-adaptive-done nil))))
  9938. ;; Should run at beginning of `helm-exit-minibuffer'.
  9939. (add-hook 'helm-before-action-hook #'(lambda ()
  9940. (when helm-c-use-adaptative-sorting
  9941. (helm-c-adaptive-store-selection))))
  9942. ;; Should run at beginning of `helm-select-action'.
  9943. (add-hook 'helm-select-action-hook #'(lambda ()
  9944. (when helm-c-use-adaptative-sorting
  9945. (helm-c-adaptive-store-selection))))
  9946. (defun helm-c-source-use-adaptative-p (&optional source-name)
  9947. "Return current source only if it use adaptative history, nil otherwise."
  9948. (when helm-c-use-adaptative-sorting
  9949. (let* ((source (or source-name (helm-get-current-source)))
  9950. (adapt-source (or (assoc-default 'filtered-candidate-transformer
  9951. (assoc (assoc-default 'type source)
  9952. helm-type-attributes))
  9953. (assoc-default 'candidate-transformer
  9954. (assoc (assoc-default 'type source)
  9955. helm-type-attributes))
  9956. (assoc-default 'filtered-candidate-transformer source)
  9957. (assoc-default 'candidate-transformer source))))
  9958. (if (listp adapt-source)
  9959. (when (member 'helm-c-adaptive-sort adapt-source) source)
  9960. (when (eq adapt-source 'helm-c-adaptive-sort) source)))))
  9961. (defun helm-c-adaptive-store-selection ()
  9962. "Store history information for the selected candidate."
  9963. (unless helm-c-adaptive-done
  9964. (setq helm-c-adaptive-done t)
  9965. (let ((source (helm-c-source-use-adaptative-p)))
  9966. (when source
  9967. (let* ((source-name (or (assoc-default 'type source)
  9968. (assoc-default 'name source)))
  9969. (source-info (or (assoc source-name helm-c-adaptive-history)
  9970. (progn
  9971. (push (list source-name) helm-c-adaptive-history)
  9972. (car helm-c-adaptive-history))))
  9973. (selection (helm-get-selection))
  9974. (selection-info (progn
  9975. (setcdr source-info
  9976. (cons
  9977. (let ((found (assoc selection (cdr source-info))))
  9978. (if (not found)
  9979. ;; new entry
  9980. (list selection)
  9981. ;; move entry to the beginning of the
  9982. ;; list, so that it doesn't get
  9983. ;; trimmed when the history is
  9984. ;; truncated
  9985. (setcdr source-info
  9986. (delete found (cdr source-info)))
  9987. found))
  9988. (cdr source-info)))
  9989. (cadr source-info)))
  9990. (pattern-info (progn
  9991. (setcdr selection-info
  9992. (cons
  9993. (let ((found (assoc helm-pattern (cdr selection-info))))
  9994. (if (not found)
  9995. ;; new entry
  9996. (cons helm-pattern 0)
  9997. ;; move entry to the beginning of the
  9998. ;; list, so if two patterns used the
  9999. ;; same number of times then the one
  10000. ;; used last appears first in the list
  10001. (setcdr selection-info
  10002. (delete found (cdr selection-info)))
  10003. found))
  10004. (cdr selection-info)))
  10005. (cadr selection-info))))
  10006. ;; increase usage count
  10007. (setcdr pattern-info (1+ (cdr pattern-info)))
  10008. ;; truncate history if needed
  10009. (if (> (length (cdr selection-info)) helm-c-adaptive-history-length)
  10010. (setcdr selection-info
  10011. (subseq (cdr selection-info) 0 helm-c-adaptive-history-length))))))))
  10012. (defun helm-c-adaptative-maybe-load-history ()
  10013. (when (and helm-c-use-adaptative-sorting
  10014. (file-readable-p helm-c-adaptive-history-file))
  10015. (load-file helm-c-adaptive-history-file)))
  10016. (add-hook 'emacs-startup-hook 'helm-c-adaptative-maybe-load-history)
  10017. (add-hook 'kill-emacs-hook 'helm-c-adaptive-save-history)
  10018. (defun helm-c-adaptive-save-history (&optional arg)
  10019. "Save history information to file given by `helm-c-adaptive-history-file'."
  10020. (interactive "p")
  10021. (when helm-c-use-adaptative-sorting
  10022. (with-temp-buffer
  10023. (insert
  10024. ";; -*- mode: emacs-lisp -*-\n"
  10025. ";; History entries used for helm adaptive display.\n")
  10026. (prin1 `(setq helm-c-adaptive-history ',helm-c-adaptive-history)
  10027. (current-buffer))
  10028. (insert ?\n)
  10029. (write-region (point-min) (point-max) helm-c-adaptive-history-file nil
  10030. (unless arg 'quiet)))))
  10031. (defun helm-c-adaptive-sort (candidates source)
  10032. "Sort the CANDIDATES for SOURCE by usage frequency.
  10033. This is a filtered candidate transformer you can use for the
  10034. attribute `filtered-candidate-transformer' of a source in
  10035. `helm-sources' or a type in `helm-type-attributes'."
  10036. (let* ((source-name (or (assoc-default 'type source)
  10037. (assoc-default 'name source)))
  10038. (source-info (assoc source-name helm-c-adaptive-history)))
  10039. (if source-info
  10040. (let ((usage
  10041. ;; ... assemble a list containing the (CANIDATE . USAGE-COUNT)
  10042. ;; pairs
  10043. (mapcar (lambda (candidate-info)
  10044. (let ((count 0))
  10045. (dolist (pattern-info (cdr candidate-info))
  10046. (if (not (equal (car pattern-info)
  10047. helm-pattern))
  10048. (incf count (cdr pattern-info))
  10049. ;; if current pattern is equal to the previously
  10050. ;; used one then this candidate has priority
  10051. ;; (that's why its count is boosted by 10000) and
  10052. ;; it only has to compete with other candidates
  10053. ;; which were also selected with the same pattern
  10054. (setq count (+ 10000 (cdr pattern-info)))
  10055. (return)))
  10056. (cons (car candidate-info) count)))
  10057. (cdr source-info)))
  10058. sorted)
  10059. (if (and usage (consp usage))
  10060. ;; sort the list in descending order, so candidates with highest
  10061. ;; priorty come first
  10062. (progn
  10063. (setq usage (sort usage (lambda (first second)
  10064. (> (cdr first) (cdr second)))))
  10065. ;; put those candidates first which have the highest usage count
  10066. (dolist (info usage)
  10067. (when (member* (car info) candidates
  10068. :test 'helm-c-adaptive-compare)
  10069. (push (car info) sorted)
  10070. (setq candidates (remove* (car info) candidates
  10071. :test 'helm-c-adaptive-compare))))
  10072. ;; and append the rest
  10073. (append (reverse sorted) candidates nil))
  10074. (message "Your `%s' is maybe corrupted or too old, \
  10075. you should reinitialize it with `helm-c-reset-adaptative-history'"
  10076. helm-c-adaptive-history-file)
  10077. (sit-for 1)
  10078. candidates))
  10079. ;; if there is no information stored for this source then do nothing
  10080. candidates)))
  10081. ;;;###autoload
  10082. (defun helm-c-reset-adaptative-history ()
  10083. "Delete all `helm-c-adaptive-history' and his file.
  10084. Useful when you have a old or corrupted `helm-c-adaptive-history-file'."
  10085. (interactive)
  10086. (when (y-or-n-p "Really delete all your `helm-c-adaptive-history'? ")
  10087. (setq helm-c-adaptive-history nil)
  10088. (delete-file helm-c-adaptive-history-file)))
  10089. (defun helm-c-adaptive-compare (x y)
  10090. "Compare candidates X and Y taking into account that the
  10091. candidate can be in (DISPLAY . REAL) format."
  10092. (equal (if (listp x)
  10093. (cdr x)
  10094. x)
  10095. (if (listp y)
  10096. (cdr y)
  10097. y)))
  10098. ;;; Outliner
  10099. ;;
  10100. ;;
  10101. (defvar helm-outline-goto-near-line-flag t)
  10102. (defvar helm-outline-using nil)
  10103. (defun helm-after-update-hook--outline ()
  10104. (if (and (eq helm-outline-using t)
  10105. (eq helm-outline-goto-near-line-flag t))
  10106. (helm-outline-goto-near-line)))
  10107. (add-hook 'helm-after-update-hook 'helm-after-update-hook--outline)
  10108. (defun helm-outline-goto-near-line ()
  10109. (with-helm-window
  10110. ;; TODO need consideration whether to update position by every input.
  10111. (when t ; (equal helm-pattern "")
  10112. (helm-goto-line 2)
  10113. (let ((lineno (with-helm-current-buffer
  10114. (line-number-at-pos (car helm-current-position)))))
  10115. (block exit
  10116. (while (<= (progn (skip-chars-forward " ")
  10117. (or (number-at-point) lineno))
  10118. lineno)
  10119. (forward-line 1)
  10120. (when (eobp)
  10121. (forward-line -1)
  10122. (return-from exit))))
  10123. (forward-line -1)
  10124. (and (bobp) (forward-line 1))
  10125. (and (helm-pos-header-line-p) (forward-line -2))
  10126. (helm-mark-current-line)))))
  10127. ;;; Plug-in
  10128. ;;
  10129. ;;
  10130. ;; Plug-in: info-index
  10131. (defun* helm-c-info-init (&optional (file (helm-attr 'info-file)))
  10132. (let (result)
  10133. (unless (helm-candidate-buffer)
  10134. (save-window-excursion
  10135. (info file)
  10136. (let (Info-history
  10137. (tobuf (helm-candidate-buffer 'global))
  10138. (infobuf (current-buffer))
  10139. s e)
  10140. (dolist (node (or (helm-attr 'index-nodes) (Info-index-nodes)))
  10141. (Info-goto-node node)
  10142. (goto-char (point-min))
  10143. (while (search-forward "\n* " nil t)
  10144. (unless (search-forward "Menu:\n" (1+ (point-at-eol)) t)
  10145. '(save-current-buffer (buffer-substring-no-properties (point-at-bol) (point-at-eol)) result)
  10146. (setq s (point-at-bol)
  10147. e (point-at-eol))
  10148. (with-current-buffer tobuf
  10149. (insert-buffer-substring infobuf s e)
  10150. (insert "\n"))))))))))
  10151. (defun helm-c-info-goto (node-line)
  10152. (Info-goto-node (car node-line))
  10153. (helm-goto-line (cdr node-line)))
  10154. (defun helm-c-info-display-to-real (line)
  10155. (and (string-match
  10156. ;; This regexp is stolen from Info-apropos-matches
  10157. "\\* +\\([^\n]*.+[^\n]*\\):[ \t]+\\([^\n]*\\)\\.\\(?:[ \t\n]*(line +\\([0-9]+\\))\\)?" line)
  10158. (cons (format "(%s)%s" (helm-attr 'info-file) (match-string 2 line))
  10159. (string-to-number (or (match-string 3 line) "1")))))
  10160. (defun helm-c-make-info-source (source file)
  10161. `(,@source
  10162. (name . ,(concat "Info Index: " file))
  10163. (info-file . ,file)
  10164. (init . helm-c-info-init)
  10165. (display-to-real . helm-c-info-display-to-real)
  10166. (get-line . buffer-substring)
  10167. (candidates-in-buffer)
  10168. (action ("Goto node" . helm-c-info-goto))))
  10169. (defun helm-compile-source--info-index (source)
  10170. (helm-aif (helm-interpret-value (assoc-default 'info-index source))
  10171. (helm-c-make-info-source source it)
  10172. source))
  10173. (add-to-list 'helm-compile-source-functions 'helm-compile-source--info-index)
  10174. (helm-document-attribute 'info-index "info-index plugin"
  10175. "Create a source of info index very easily.
  10176. ex. (defvar helm-c-source-info-wget '((info-index . \"wget\"))")
  10177. (helm-document-attribute 'index-nodes "info-index plugin (optional)"
  10178. "Index nodes of info file.
  10179. If it is omitted, `Info-index-nodes' is used to collect index nodes.
  10180. Some info files are missing index specification.
  10181. ex. See `helm-c-source-info-screen'.")
  10182. ;; Plug-in: candidates-file
  10183. (defun helm-compile-source--candidates-file (source)
  10184. (if (assoc-default 'candidates-file source)
  10185. `((init helm-p-candidats-file-init
  10186. ,@(let ((orig-init (assoc-default 'init source)))
  10187. (cond ((null orig-init) nil)
  10188. ((functionp orig-init) (list orig-init))
  10189. (t orig-init))))
  10190. (candidates-in-buffer)
  10191. ,@source)
  10192. source))
  10193. (add-to-list 'helm-compile-source-functions 'helm-compile-source--candidates-file)
  10194. (defun helm-p-candidats-file-init ()
  10195. (destructuring-bind (file &optional updating)
  10196. (helm-mklist (helm-attr 'candidates-file))
  10197. (setq file (helm-interpret-value file))
  10198. (with-current-buffer (helm-candidate-buffer (find-file-noselect file))
  10199. (when updating
  10200. (buffer-disable-undo)
  10201. (font-lock-mode -1)
  10202. (auto-revert-mode 1)))))
  10203. (helm-document-attribute 'candidates-file "candidates-file plugin"
  10204. "Use a file as the candidates buffer.
  10205. 1st argument is a filename, string or function name or variable name.
  10206. If optional 2nd argument is non-nil, the file opened with `auto-revert-mode'.")
  10207. ;; Plug-in: headline
  10208. (defun helm-compile-source--helm-headline (source)
  10209. (if (assoc-default 'headline source)
  10210. (append '((init . helm-headline-init)
  10211. (get-line . buffer-substring)
  10212. (type . line))
  10213. source
  10214. '((candidates-in-buffer)
  10215. (persistent-help . "Show this line")))
  10216. source))
  10217. (add-to-list 'helm-compile-source-functions 'helm-compile-source--helm-headline)
  10218. (defun helm-headline-init ()
  10219. (when (and (helm-current-buffer-is-modified)
  10220. (with-helm-current-buffer
  10221. (eval (or (helm-attr 'condition) t))))
  10222. (helm-headline-make-candidate-buffer
  10223. (helm-interpret-value (helm-attr 'headline))
  10224. (helm-interpret-value (helm-attr 'subexp)))))
  10225. (helm-document-attribute 'headline "Headline plug-in"
  10226. "Regexp string for helm-headline to scan.")
  10227. (helm-document-attribute 'condition "Headline plug-in"
  10228. "A sexp representing the condition to use helm-headline.")
  10229. (helm-document-attribute 'subexp "Headline plug-in"
  10230. "Display (match-string-no-properties subexp).")
  10231. ;; Le Wang: Note on how `helm-head-line-get-candidates' works with a list
  10232. ;; of regexps.
  10233. ;;
  10234. ;; 1. Create list of ((title . start-of-match) . hiearchy)
  10235. ;; 2. Sort this list by start-of-match.
  10236. ;; 3. Go through sorted list and return titles that reflect full hiearchy.
  10237. ;;
  10238. ;; It's quite brilliantly written.
  10239. ;;
  10240. (defun helm-headline-get-candidates (regexp subexp)
  10241. (with-helm-current-buffer
  10242. (save-excursion
  10243. (goto-char (point-min))
  10244. (if (functionp regexp) (setq regexp (funcall regexp)))
  10245. (let (hierarchy curhead)
  10246. (flet ((matched ()
  10247. (if (numberp subexp)
  10248. (cons (match-string-no-properties subexp) (match-beginning subexp))
  10249. (cons (buffer-substring (point-at-bol) (point-at-eol))
  10250. (point-at-bol))))
  10251. (hierarchies (headlines)
  10252. (1+ (loop for (_ . hierarchy) in headlines
  10253. maximize hierarchy)))
  10254. (vector-0-n (v n)
  10255. (loop for i from 0 to hierarchy
  10256. collecting (aref curhead i)))
  10257. (arrange (headlines)
  10258. (unless (null headlines) ; FIX headlines empty bug!
  10259. (loop with curhead = (make-vector (hierarchies headlines) "")
  10260. for ((str . pt) . hierarchy) in headlines
  10261. do (aset curhead hierarchy str)
  10262. collecting
  10263. (cons
  10264. (format "H%d:%s" (1+ hierarchy)
  10265. (mapconcat 'identity (vector-0-n curhead hierarchy) " / "))
  10266. pt)))))
  10267. (if (listp regexp)
  10268. (arrange
  10269. (sort
  10270. (loop for re in regexp
  10271. for hierarchy from 0
  10272. do (goto-char (point-min))
  10273. appending
  10274. (loop
  10275. while (re-search-forward re nil t)
  10276. collect (cons (matched) hierarchy)))
  10277. (lambda (a b) (> (cdar b) (cdar a)))))
  10278. (loop while (re-search-forward regexp nil t)
  10279. collect (matched))))))))
  10280. (defun helm-headline-make-candidate-buffer (regexp subexp)
  10281. (with-current-buffer (helm-candidate-buffer 'local)
  10282. (loop for (content . pos) in (helm-headline-get-candidates regexp subexp)
  10283. do (insert
  10284. (format "%5d:%s\n"
  10285. (with-helm-current-buffer
  10286. (line-number-at-pos pos))
  10287. content)))))
  10288. (defun helm-headline-goto-position (pos recenter)
  10289. (goto-char pos)
  10290. (unless recenter
  10291. (set-window-start (get-buffer-window helm-current-buffer) (point))))
  10292. ;; Plug-in: persistent-help
  10293. (defun helm-compile-source--persistent-help (source)
  10294. (append source '((header-line . helm-persistent-help-string))))
  10295. (add-to-list 'helm-compile-source-functions 'helm-compile-source--persistent-help)
  10296. (defun helm-persistent-help-string ()
  10297. (substitute-command-keys
  10298. (concat "\\<helm-map>\\[helm-execute-persistent-action]: "
  10299. (or (helm-interpret-value (helm-attr 'persistent-help))
  10300. (helm-aif (or (assoc-default 'persistent-action
  10301. (helm-get-current-source))
  10302. (assoc-default 'action
  10303. (helm-get-current-source)))
  10304. (cond ((symbolp it) (symbol-name it))
  10305. ((listp it) (or (ignore-errors (caar it)) ""))))
  10306. "")
  10307. " (keeping session)")))
  10308. (helm-document-attribute 'persistent-help "persistent-help plug-in"
  10309. "A string to explain persistent-action of this source.
  10310. It also accepts a function or a variable name.")
  10311. ;;; (helm '(((name . "persistent-help test")(candidates "a")(persistent-help . "TEST"))))
  10312. ;; Plug-in: Type customize
  10313. (defun helm-c-uniq-list (lst)
  10314. "Like `remove-duplicates' in CL.
  10315. But cut deeper duplicates and test by `equal'. "
  10316. (reverse (remove-duplicates (reverse lst) :test 'equal)))
  10317. (defvar helm-additional-type-attributes nil)
  10318. (defun helm-c-arrange-type-attribute (type spec)
  10319. "Override type attributes by `define-helm-type-attribute'.
  10320. The SPEC is like source. The symbol `REST' is replaced
  10321. with original attribute value.
  10322. Example: Set `play-sound-file' as default action
  10323. (helm-c-arrange-type-attribute 'file
  10324. '((action (\"Play sound\" . play-sound-file)
  10325. REST ;; Rest of actions (find-file, find-file-other-window, etc...)."
  10326. (add-to-list 'helm-additional-type-attributes
  10327. (cons type
  10328. (loop with typeattr = (assoc-default
  10329. type helm-type-attributes)
  10330. for (attr . value) in spec
  10331. if (listp value)
  10332. collect (cons attr
  10333. (helm-c-uniq-list
  10334. (loop for v in value
  10335. if (eq v 'REST)
  10336. append
  10337. (assoc-default attr typeattr)
  10338. else
  10339. collect v)))
  10340. else
  10341. collect (cons attr value)))))
  10342. (put 'helm-c-arrange-type-attribute 'lisp-indent-function 1)
  10343. (defun helm-compile-source--type-customize (source)
  10344. (helm-aif (assoc-default (assoc-default 'type source)
  10345. helm-additional-type-attributes)
  10346. (append it source)
  10347. source))
  10348. (add-to-list 'helm-compile-source-functions
  10349. 'helm-compile-source--type-customize t)
  10350. ;; Plug-in: default-action
  10351. (defun helm-compile-source--default-action (source)
  10352. (helm-aif (assoc-default 'default-action source)
  10353. (append `((action ,it ,@(remove it (assoc-default 'action source))))
  10354. source)
  10355. source))
  10356. (add-to-list 'helm-compile-source-functions
  10357. 'helm-compile-source--default-action t)
  10358. (helm-document-attribute 'default-action "default-action plug-in"
  10359. "Default action.")
  10360. ;;; Type Attributes
  10361. ;;
  10362. ;;
  10363. (define-helm-type-attribute 'buffer
  10364. `((action
  10365. ("Switch to buffer" . helm-c-switch-to-buffer)
  10366. ,(and (locate-library "popwin") '("Switch to buffer in popup window" . popwin:popup-buffer))
  10367. ("Switch to buffer other window" . switch-to-buffer-other-window)
  10368. ("Switch to buffer other frame" . switch-to-buffer-other-frame)
  10369. ,(and (locate-library "elscreen") '("Display buffer in Elscreen" . helm-find-buffer-on-elscreen))
  10370. ("Query replace regexp" . helm-c-buffer-query-replace-regexp)
  10371. ("Query replace" . helm-c-buffer-query-replace)
  10372. ("View buffer" . view-buffer)
  10373. ("Display buffer" . display-buffer)
  10374. ("Grep buffers (C-u grep all buffers)" . helm-c-zgrep-buffers)
  10375. ("Revert buffer(s)" . helm-revert-marked-buffers)
  10376. ("Insert buffer" . insert-buffer)
  10377. ("Kill buffer(s)" . helm-kill-marked-buffers)
  10378. ("Diff with file" . diff-buffer-with-file)
  10379. ("Ediff Marked buffers" . helm-ediff-marked-buffers)
  10380. ("Ediff Merge marked buffers" . (lambda (candidate)
  10381. (helm-ediff-marked-buffers candidate t))))
  10382. (persistent-help . "Show this buffer")
  10383. (candidate-transformer helm-c-skip-boring-buffers
  10384. helm-c-transform-buffer-display-string))
  10385. "Buffer or buffer name.")
  10386. (define-helm-type-attribute 'file
  10387. `((action
  10388. ("Find file" . helm-find-many-files)
  10389. ,(and (locate-library "popwin") '("Find file in popup window" . popwin:find-file))
  10390. ("Find file as root" . helm-find-file-as-root)
  10391. ("Find file other window" . find-file-other-window)
  10392. ("Find file other frame" . find-file-other-frame)
  10393. ("Open dired in file's directory" . helm-c-open-dired)
  10394. ("Grep File(s) `C-u recurse'" . helm-find-files-grep)
  10395. ("Zgrep File(s) `C-u Recurse'" . helm-ff-zgrep)
  10396. ("Pdfgrep File(s)" . helm-ff-pdfgrep)
  10397. ("Checksum File" . helm-ff-checksum)
  10398. ("Ediff File" . helm-find-files-ediff-files)
  10399. ("Ediff Merge File" . helm-find-files-ediff-merge-files)
  10400. ("View file" . view-file)
  10401. ("Insert file" . insert-file)
  10402. ("Delete file(s)" . helm-delete-marked-files)
  10403. ("Open file externally (C-u to choose)" . helm-c-open-file-externally)
  10404. ("Open file with default tool" . helm-c-open-file-with-default-tool)
  10405. ("Find file in hex dump" . hexl-find-file))
  10406. (persistent-help . "Show this file")
  10407. (action-transformer helm-c-transform-file-load-el
  10408. helm-c-transform-file-browse-url)
  10409. (candidate-transformer helm-c-w32-pathname-transformer
  10410. helm-c-skip-current-file
  10411. helm-c-skip-boring-files
  10412. helm-c-shorten-home-path))
  10413. "File name.")
  10414. (let ((actions '(("Describe command" . describe-function)
  10415. ("Add command to kill ring" . helm-c-kill-new)
  10416. ("Go to command's definition" . find-function)
  10417. ("Debug on entry" . debug-on-entry)
  10418. ("Cancel debug on entry" . cancel-debug-on-entry)
  10419. ("Trace function" . trace-function)
  10420. ("Trace function (background)" . trace-function-background)
  10421. ("Untrace function" . untrace-function))))
  10422. (define-helm-type-attribute 'command
  10423. `((action ("Call interactively" . helm-c-call-interactively)
  10424. ,@actions)
  10425. (coerce . helm-c-symbolify)
  10426. (persistent-action . describe-function))
  10427. "Command. (string or symbol)")
  10428. (define-helm-type-attribute 'function
  10429. `((action . ,actions)
  10430. (action-transformer helm-c-transform-function-call-interactively)
  10431. (candidate-transformer helm-c-mark-interactive-functions)
  10432. (coerce . helm-c-symbolify))
  10433. "Function. (string or symbol)"))
  10434. (define-helm-type-attribute 'variable
  10435. '((action ("Describe variable" . describe-variable)
  10436. ("Add variable to kill ring" . helm-c-kill-new)
  10437. ("Go to variable's definition" . find-variable)
  10438. ("Set variable" . helm-c-set-variable))
  10439. (coerce . helm-c-symbolify))
  10440. "Variable.")
  10441. (define-helm-type-attribute 'sexp
  10442. '((action ("Eval s-expression" . (lambda (c) (eval (read c))))
  10443. ("Add s-expression to kill ring" . kill-new))
  10444. (action-transformer helm-c-transform-sexp-eval-command-sexp))
  10445. "String representing S-Expressions.")
  10446. (define-helm-type-attribute 'bookmark
  10447. `((coerce . helm-bookmark-get-bookmark-from-name)
  10448. (action
  10449. ("Jump to bookmark" . helm-c-bookmark-jump)
  10450. ("Jump to BM other window" . bookmark-jump-other-window)
  10451. ("Bookmark edit annotation" . bookmark-edit-annotation)
  10452. ("Bookmark show annotation" . bookmark-show-annotation)
  10453. ("Delete bookmark(s)" . helm-delete-marked-bookmarks)
  10454. ,@(and (locate-library "bookmark-extensions")
  10455. `(("Edit Bookmark" . bmkext-edit-bookmark)))
  10456. ("Rename bookmark" . bookmark-rename)
  10457. ("Relocate bookmark" . bookmark-relocate))
  10458. (keymap . ,helm-c-bookmark-map)
  10459. (mode-line . helm-bookmark-mode-line-string))
  10460. "Bookmark name.")
  10461. (define-helm-type-attribute 'line
  10462. '((display-to-real . helm-c-display-to-real-line)
  10463. (action ("Go to Line" . helm-c-action-line-goto)))
  10464. "LINENO:CONTENT string, eg. \" 16:foo\".
  10465. Optional `target-file' attribute is a name of target file.
  10466. Optional `before-jump-hook' attribute is a function with no
  10467. arguments which is called before jumping to position.
  10468. Optional `after-jump-hook' attribute is a function with no
  10469. arguments which is called after jumping to position.
  10470. If `adjust' attribute is specified, searches the line whose
  10471. content is CONTENT near the LINENO.
  10472. If `recenter' attribute is specified, the line is displayed at
  10473. the center of window, otherwise at the top of window.
  10474. ")
  10475. (define-helm-type-attribute 'file-line
  10476. `((filtered-candidate-transformer helm-c-filtered-candidate-transformer-file-line)
  10477. (multiline)
  10478. (action ("Go to" . helm-c-action-file-line-goto)))
  10479. "FILENAME:LINENO:CONTENT string, eg. \"~/.emacs:16:;; comment\".
  10480. Optional `default-directory' attribute is a default-directory
  10481. FILENAME is interpreted.
  10482. Optional `before-jump-hook' attribute is a function with no
  10483. arguments which is called before jumping to position.
  10484. Optional `after-jump-hook' attribute is a function with no
  10485. arguments which is called after jumping to position.
  10486. If `adjust' attribute is specified, searches the line whose
  10487. content is CONTENT near the LINENO.
  10488. If `recenter' attribute is specified, the line is displayed at
  10489. the center of window, otherwise at the top of window.
  10490. ")
  10491. (define-helm-type-attribute 'timer
  10492. '((real-to-display . helm-c-timer-real-to-display)
  10493. (action ("Cancel Timer" . cancel-timer)
  10494. ("Describe Function" . (lambda (tm) (describe-function (timer--function tm))))
  10495. ("Find Function" . (lambda (tm) (find-function (timer--function tm)))))
  10496. (persistent-action . (lambda (tm) (describe-function (timer--function tm))))
  10497. (persistent-help . "Describe Function"))
  10498. "Timer.")
  10499. ;;; Default `helm-sources'
  10500. ;; Setting `helm-sources' is DEPRECATED, but it seems that newbies
  10501. ;; tend to invoke M-x helm directly. So I offer default setting.
  10502. (setq helm-sources
  10503. '(helm-c-source-buffers-list
  10504. helm-c-source-recentf
  10505. helm-c-source-files-in-current-dir+))
  10506. ;;; Preconfigured Helm
  10507. ;;
  10508. ;;
  10509. ;;;###autoload
  10510. (defun helm-mini ()
  10511. "Preconfigured `helm' lightweight version \(buffer -> recentf\)."
  10512. (interactive)
  10513. (helm-other-buffer '(helm-c-source-buffers-list
  10514. helm-c-source-recentf
  10515. helm-c-source-buffer-not-found)
  10516. "*helm mini*"))
  10517. ;;;###autoload
  10518. (defun helm-for-files ()
  10519. "Preconfigured `helm' for opening files.
  10520. ffap -> recentf -> buffer -> bookmark -> file-cache -> files-in-current-dir -> locate."
  10521. (interactive)
  10522. (helm-other-buffer helm-for-files-prefered-list "*helm for files*"))
  10523. ;;;###autoload
  10524. (defun helm-recentf ()
  10525. "Preconfigured `helm' for `recentf'."
  10526. (interactive)
  10527. (helm-other-buffer 'helm-c-source-recentf "*helm recentf*"))
  10528. ;;;###autoload
  10529. (defun helm-info-at-point (arg)
  10530. "Preconfigured `helm' for searching info at point.
  10531. With a prefix-arg insert symbol at point."
  10532. (interactive "P")
  10533. (let ((helm-c-google-suggest-default-function
  10534. 'helm-c-google-suggest-emacs-lisp))
  10535. (helm :sources '(helm-c-source-info-elisp
  10536. helm-c-source-info-cl
  10537. helm-c-source-info-pages
  10538. helm-c-source-google-suggest)
  10539. :input (and arg (thing-at-point 'symbol))
  10540. :buffer "*helm info*")))
  10541. ;;;###autoload
  10542. (defun helm-show-kill-ring ()
  10543. "Preconfigured `helm' for `kill-ring'.
  10544. It is drop-in replacement of `yank-pop'.
  10545. You may bind this command to M-y.
  10546. First call open the kill-ring browser, next calls move to next line."
  10547. (interactive)
  10548. (helm :sources 'helm-c-source-kill-ring
  10549. :buffer "*helm kill-ring*"))
  10550. ;;;###autoload
  10551. (defun helm-minibuffer-history ()
  10552. "Preconfigured `helm' for `minibuffer-history'."
  10553. (interactive)
  10554. (let ((enable-recursive-minibuffers t))
  10555. (helm-other-buffer 'helm-c-source-minibuffer-history
  10556. "*helm minibuffer-history*")))
  10557. ;;;###autoload
  10558. (defun helm-gentoo ()
  10559. "Preconfigured `helm' for gentoo linux."
  10560. (interactive)
  10561. (helm-other-buffer '(helm-c-source-gentoo
  10562. helm-c-source-use-flags)
  10563. "*helm gentoo*"))
  10564. ;;;###autoload
  10565. (defun helm-imenu ()
  10566. "Preconfigured `helm' for `imenu'."
  10567. (interactive)
  10568. (helm :sources 'helm-c-source-imenu
  10569. :buffer "*helm imenu*"))
  10570. ;;;###autoload
  10571. (defun helm-google-suggest ()
  10572. "Preconfigured `helm' for google search with google suggest."
  10573. (interactive)
  10574. (helm-other-buffer 'helm-c-source-google-suggest "*helm google*"))
  10575. ;;;###autoload
  10576. (defun helm-yahoo-suggest ()
  10577. "Preconfigured `helm' for Yahoo searching with Yahoo suggest."
  10578. (interactive)
  10579. (helm-other-buffer 'helm-c-source-yahoo-suggest "*helm yahoo*"))
  10580. ;;; Converted from helm-show-*-only
  10581. ;;;###autoload
  10582. (defun helm-for-buffers ()
  10583. "Preconfigured `helm' for buffers."
  10584. (interactive)
  10585. (helm-other-buffer 'helm-c-source-buffers "*helm for buffers*"))
  10586. ;;;###autoload
  10587. (defun helm-buffers-list ()
  10588. "Preconfigured `helm' to list buffers.
  10589. It is an enhanced version of `helm-for-buffers'."
  10590. (interactive)
  10591. (helm :sources '(helm-c-source-buffers-list
  10592. helm-c-source-buffer-not-found)
  10593. :buffer "*helm buffers*" :keymap helm-c-buffer-map))
  10594. (defalias 'helm-buffers+ 'helm-buffers-list
  10595. "Preconfigured `helm' to list buffers.
  10596. It is an alias of `helm-buffers-list'.")
  10597. ;;;###autoload
  10598. (defun helm-bbdb ()
  10599. "Preconfigured `helm' for BBDB.
  10600. Needs BBDB.
  10601. http://bbdb.sourceforge.net/"
  10602. (interactive)
  10603. (helm-other-buffer 'helm-c-source-bbdb "*helm bbdb*"))
  10604. ;;;###autoload
  10605. (defun helm-locate (arg)
  10606. "Preconfigured `helm' for Locate.
  10607. Note: you can add locate options after entering pattern.
  10608. See 'man locate' for valid options.
  10609. You can specify a specific database with prefix argument ARG \(C-u\).
  10610. Many databases can be used: navigate and mark them.
  10611. See also `helm-locate-with-db'.
  10612. To create a user specific db, use
  10613. \"updatedb -l 0 -o db_path -U directory\".
  10614. Where db_path is a filename matched by
  10615. `helm-locate-db-file-regexp'."
  10616. (interactive "P")
  10617. (setq helm-ff-default-directory default-directory)
  10618. (helm-locate-1 arg))
  10619. ;;;###autoload
  10620. (defun helm-w3m-bookmarks ()
  10621. "Preconfigured `helm' for w3m bookmark.
  10622. Needs w3m and emacs-w3m.
  10623. http://w3m.sourceforge.net/
  10624. http://emacs-w3m.namazu.org/"
  10625. (interactive)
  10626. (helm-other-buffer 'helm-c-source-w3m-bookmarks
  10627. "*helm w3m bookmarks*"))
  10628. ;;;###autoload
  10629. (defun helm-firefox-bookmarks ()
  10630. "Preconfigured `helm' for firefox bookmark.
  10631. You will have to enable html bookmarks in firefox:
  10632. open about:config in firefox and double click on this line to enable value \
  10633. to true:
  10634. user_pref(\"browser.bookmarks.autoExportHTML\", false);
  10635. You should have now:
  10636. user_pref(\"browser.bookmarks.autoExportHTML\", true);
  10637. After closing firefox, you will be able to browse you bookmarks.
  10638. "
  10639. (interactive)
  10640. (helm-other-buffer 'helm-c-source-firefox-bookmarks
  10641. "*Helm Firefox*"))
  10642. ;;;###autoload
  10643. (defun helm-colors ()
  10644. "Preconfigured `helm' for color."
  10645. (interactive)
  10646. (helm-other-buffer
  10647. '(helm-c-source-colors helm-c-source-customize-face)
  10648. "*helm colors*"))
  10649. ;;;###autoload
  10650. (defun helm-bookmarks ()
  10651. "Preconfigured `helm' for bookmarks."
  10652. (interactive)
  10653. (helm-other-buffer 'helm-c-source-bookmarks "*helm bookmarks*"))
  10654. ;;;###autoload
  10655. (defun helm-c-pp-bookmarks ()
  10656. "Preconfigured `helm' for bookmarks (pretty-printed)."
  10657. (interactive)
  10658. (helm-other-buffer '(helm-c-source-bookmarks-local
  10659. helm-c-source-bookmarks-su
  10660. helm-c-source-bookmarks-ssh)
  10661. "*helm pp bookmarks*"))
  10662. ;;;###autoload
  10663. (defun helm-c-insert-latex-math ()
  10664. "Preconfigured helm for latex math symbols completion."
  10665. (interactive)
  10666. (helm-other-buffer 'helm-c-source-latex-math "*helm latex*"))
  10667. ;;;###autoload
  10668. (defun helm-register ()
  10669. "Preconfigured `helm' for Emacs registers."
  10670. (interactive)
  10671. (helm-other-buffer 'helm-c-source-register "*helm register*"))
  10672. ;;;###autoload
  10673. (defun helm-man-woman ()
  10674. "Preconfigured `helm' for Man and Woman pages."
  10675. (interactive)
  10676. (helm-other-buffer 'helm-c-source-man-pages "*Helm man woman*"))
  10677. ;;;###autoload
  10678. (defun helm-org-keywords ()
  10679. "Preconfigured `helm' for org keywords."
  10680. (interactive)
  10681. (helm-other-buffer 'helm-c-source-org-keywords "*org keywords*"))
  10682. ;;;###autoload
  10683. (defun helm-emms ()
  10684. "Preconfigured `helm' for emms sources."
  10685. (interactive)
  10686. (helm :sources '(helm-c-source-emms-streams
  10687. helm-c-source-emms-files
  10688. helm-c-source-emms-dired)
  10689. :buffer "*Helm Emms*"))
  10690. ;;;###autoload
  10691. (defun helm-eev-anchors ()
  10692. "Preconfigured `helm' for eev anchors."
  10693. (interactive)
  10694. (helm-other-buffer 'helm-c-source-eev-anchor "*Helm eev anchors*"))
  10695. ;;;###autoload
  10696. (defun helm-bm-list ()
  10697. "Preconfigured `helm' for visible bookmarks.
  10698. Needs bm.el
  10699. http://cvs.savannah.gnu.org/viewvc/*checkout*/bm/bm/bm.el"
  10700. (interactive)
  10701. (let ((helm-outline-using t))
  10702. (helm-other-buffer 'helm-c-source-bm "*helm bm list*")))
  10703. ;;;###autoload
  10704. (defun helm-timers ()
  10705. "Preconfigured `helm' for timers."
  10706. (interactive)
  10707. (helm-other-buffer '(helm-c-source-absolute-time-timers
  10708. helm-c-source-idle-time-timers)
  10709. "*helm timers*"))
  10710. ;;;###autoload
  10711. (defun helm-list-emacs-process ()
  10712. "Preconfigured `helm' for emacs process."
  10713. (interactive)
  10714. (helm-other-buffer 'helm-c-source-emacs-process "*helm process*"))
  10715. ;;;###autoload
  10716. (defun helm-occur ()
  10717. "Preconfigured Helm for Occur source.
  10718. If region is active, search only in region,
  10719. otherwise search in whole buffer."
  10720. (interactive)
  10721. (let ((helm-compile-source-functions
  10722. ;; rule out helm-match-plugin because the input is one regexp.
  10723. (delq 'helm-compile-source--match-plugin
  10724. (copy-sequence helm-compile-source-functions))))
  10725. (helm :sources 'helm-c-source-occur
  10726. :buffer "*Helm Occur*"
  10727. :history 'helm-c-grep-history)))
  10728. ;;;###autoload
  10729. (defun helm-browse-code ()
  10730. "Preconfigured helm to browse code."
  10731. (interactive)
  10732. (helm :sources 'helm-c-source-browse-code
  10733. :buffer "*helm browse code*"
  10734. :default (thing-at-point 'symbol)))
  10735. ;;;###autoload
  10736. (defun helm-org-headlines ()
  10737. "Preconfigured helm to show org headlines."
  10738. (interactive)
  10739. (helm-other-buffer 'helm-c-source-org-headline "*org headlines*"))
  10740. ;;;###autoload
  10741. (defun helm-regexp ()
  10742. "Preconfigured helm to build regexps.
  10743. `query-replace-regexp' can be run from there against found regexp."
  10744. (interactive)
  10745. (save-restriction
  10746. (let ((helm-compile-source-functions
  10747. ;; rule out helm-match-plugin because the input is one regexp.
  10748. (delq 'helm-compile-source--match-plugin
  10749. (copy-sequence helm-compile-source-functions))))
  10750. (when (and (helm-region-active-p)
  10751. ;; Don't narrow to region if buffer is already narrowed.
  10752. (not (helm-current-buffer-narrowed-p)))
  10753. (narrow-to-region (region-beginning) (region-end)))
  10754. (helm :sources helm-c-source-regexp
  10755. :buffer "*helm regexp*"
  10756. :prompt "Regexp: "
  10757. :history 'helm-build-regexp-history))))
  10758. ;;;###autoload
  10759. (defun helm-c-copy-files-async ()
  10760. "Preconfigured helm to copy file list FLIST to DEST asynchronously."
  10761. (interactive)
  10762. (let* ((flist (helm-c-read-file-name
  10763. "Copy File async: "
  10764. :marked-candidates t))
  10765. (dest (helm-c-read-file-name
  10766. "Copy File async To: "
  10767. :preselect (car flist)
  10768. :initial-input (car helm-ff-history)
  10769. :history (helm-find-files-history :comp-read nil))))
  10770. (helm-c-copy-async-with-log flist dest)))
  10771. ;;;###autoload
  10772. (defun helm-find-files (arg)
  10773. "Preconfigured `helm' for helm implementation of `find-file'.
  10774. Called with a prefix arg show history if some.
  10775. Don't call it from programs, use `helm-find-files-1' instead.
  10776. This is the starting point for nearly all actions you can do on files."
  10777. (interactive "P")
  10778. (let ((any-input (if (and arg helm-ff-history)
  10779. (helm-find-files-history)
  10780. (helm-find-files-initial-input)))
  10781. (presel (buffer-file-name (current-buffer))))
  10782. (when (and (eq major-mode 'org-agenda-mode)
  10783. org-directory
  10784. (not any-input))
  10785. (setq any-input (expand-file-name org-directory)))
  10786. (set-text-properties 0 (length any-input) nil any-input)
  10787. (if any-input
  10788. (helm-find-files-1 any-input)
  10789. (setq any-input (expand-file-name (helm-c-current-directory)))
  10790. (helm-find-files-1
  10791. any-input (if helm-ff-transformer-show-only-basename
  10792. (and presel (helm-c-basename presel))
  10793. presel)))))
  10794. ;;;###autoload
  10795. (defun helm-write-file ()
  10796. "Preconfigured `helm' providing completion for `write-file'."
  10797. (interactive)
  10798. (let ((helm-mp-highlight-delay nil))
  10799. (helm :sources 'helm-c-source-write-file
  10800. :input (expand-file-name default-directory)
  10801. :prompt "Write buffer to file: "
  10802. :buffer "*Helm write file*")))
  10803. ;;;###autoload
  10804. (defun helm-insert-file ()
  10805. "Preconfigured `helm' providing completion for `insert-file'."
  10806. (interactive)
  10807. (let ((helm-mp-highlight-delay nil))
  10808. (helm :sources 'helm-c-source-insert-file
  10809. :input (expand-file-name default-directory)
  10810. :prompt "Insert file: "
  10811. :buffer "*Helm insert file*")))
  10812. ;;;###autoload
  10813. (defun helm-dired-rename-file ()
  10814. "Preconfigured `helm' to rename files from dired."
  10815. (interactive)
  10816. (helm-dired-do-action-on-file :action 'rename))
  10817. ;;;###autoload
  10818. (defun helm-dired-copy-file ()
  10819. "Preconfigured `helm' to copy files from dired."
  10820. (interactive)
  10821. (helm-dired-do-action-on-file :action 'copy))
  10822. ;;;###autoload
  10823. (defun helm-dired-symlink-file ()
  10824. "Preconfigured `helm' to symlink files from dired."
  10825. (interactive)
  10826. (helm-dired-do-action-on-file :action 'symlink))
  10827. ;;;###autoload
  10828. (defun helm-dired-hardlink-file ()
  10829. "Preconfigured `helm' to hardlink files from dired."
  10830. (interactive)
  10831. (helm-dired-do-action-on-file :action 'hardlink))
  10832. ;;;###autoload
  10833. (defun helm-do-grep ()
  10834. "Preconfigured helm for grep.
  10835. Contrarily to Emacs `grep' no default directory is given, but
  10836. the full path of candidates in ONLY.
  10837. That allow to grep different files not only in `default-directory' but anywhere
  10838. by marking them (C-<SPACE>). If one or more directory is selected
  10839. grep will search in all files of these directories.
  10840. You can use also wildcard in the base name of candidate.
  10841. If a prefix arg is given use the -r option of grep.
  10842. The prefix arg can be passed before or after start.
  10843. See also `helm-do-grep-1'."
  10844. (interactive)
  10845. (let ((only (helm-c-read-file-name
  10846. "Search in file(s): "
  10847. :marked-candidates t
  10848. :preselect (or (dired-get-filename nil t)
  10849. (buffer-file-name (current-buffer)))))
  10850. (prefarg (or current-prefix-arg helm-current-prefix-arg)))
  10851. (helm-do-grep-1 only prefarg)))
  10852. ;;;###autoload
  10853. (defun helm-do-zgrep ()
  10854. "Preconfigured helm for zgrep."
  10855. (interactive)
  10856. (let ((prefarg (or current-prefix-arg helm-current-prefix-arg))
  10857. (ls (helm-c-read-file-name
  10858. "Search in file(s): "
  10859. :marked-candidates t
  10860. :preselect (or (dired-get-filename nil t)
  10861. (buffer-file-name (current-buffer))))))
  10862. (helm-ff-zgrep-1 ls prefarg)))
  10863. ;;;###autoload
  10864. (defun helm-do-pdfgrep ()
  10865. "Preconfigured helm for pdfgrep."
  10866. (interactive)
  10867. (let ((only (helm-c-read-file-name
  10868. "Search in file(s): "
  10869. :marked-candidates t
  10870. :test #'(lambda (file)
  10871. (or (string= (file-name-extension file) "pdf")
  10872. (string= (file-name-extension file) "PDF")
  10873. (file-directory-p file)))
  10874. :preselect (or (dired-get-filename nil t)
  10875. (buffer-file-name (current-buffer)))))
  10876. (helm-c-grep-default-function 'helm-c-pdfgrep-init))
  10877. (helm-do-pdfgrep-1 only)))
  10878. ;;;###autoload
  10879. (defun helm-c-etags-select (arg)
  10880. "Preconfigured helm for etags.
  10881. Called with one prefix arg use symbol at point as initial input.
  10882. Called with two prefix arg reinitialize cache.
  10883. If tag file have been modified reinitialize cache."
  10884. (interactive "P")
  10885. (let ((tag (helm-c-etags-get-tag-file))
  10886. (init (and (equal arg '(4)) (thing-at-point 'symbol)))
  10887. (helm-quit-if-no-candidate t)
  10888. (helm-execute-action-at-once-if-one t)
  10889. (helm-compile-source-functions
  10890. (if helm-c-etags-use-regexp-search
  10891. ;; rule out helm-match-plugin because the input is one regexp.
  10892. (delq 'helm-compile-source--match-plugin
  10893. (copy-sequence helm-compile-source-functions))
  10894. helm-compile-source-functions)))
  10895. (when (or (equal arg '(16))
  10896. (and helm-c-etags-mtime-alist
  10897. (helm-c-etags-file-modified-p tag)))
  10898. (remhash tag helm-c-etags-cache))
  10899. (if (and tag (file-exists-p tag))
  10900. (helm :sources 'helm-c-source-etags-select
  10901. :keymap helm-c-etags-map
  10902. :input init
  10903. :buffer "*helm etags*")
  10904. (message "Error: No tag file found, please create one with etags shell command."))))
  10905. ;;;###autoload
  10906. (defun helm-M-x ()
  10907. "Preconfigured `helm' for Emacs commands.
  10908. It is `helm' replacement of regular `M-x' `execute-extended-command'."
  10909. (interactive)
  10910. (let* (in-help
  10911. help-cand
  10912. special-display-buffer-names
  10913. special-display-regexps
  10914. helm-persistent-action-use-special-display
  10915. (history (loop with hist
  10916. for i in extended-command-history
  10917. for com = (intern i)
  10918. when (fboundp com)
  10919. collect i into hist finally return hist)))
  10920. (flet ((pers-help (candidate)
  10921. (let ((hbuf (get-buffer (help-buffer))))
  10922. (if (and in-help (string= candidate help-cand))
  10923. (progn
  10924. ;; When M-x is started from a help buffer,
  10925. ;; Don't kill it as it is helm-current-buffer.
  10926. (unless (equal hbuf helm-current-buffer)
  10927. (kill-buffer hbuf))
  10928. (setq in-help nil))
  10929. ;; Be sure helm-current-buffer
  10930. ;; have not a dedicated window.
  10931. (set-window-dedicated-p
  10932. (get-buffer-window helm-current-buffer) nil)
  10933. (describe-function (intern candidate))
  10934. (message nil) ; Erase the new stupid message Type "q"[...]
  10935. (setq in-help t))
  10936. (setq help-cand candidate))))
  10937. (let* ((command (helm-comp-read
  10938. "M-x " obarray
  10939. :test 'commandp
  10940. :requires-pattern helm-M-x-requires-pattern
  10941. :name "Emacs Commands"
  10942. :buffer "*helm M-x*"
  10943. :persistent-action 'pers-help
  10944. :persistent-help "Describe this command"
  10945. :history history
  10946. :must-match t
  10947. :candidates-in-buffer t
  10948. :fc-transformer 'helm-M-x-transformer))
  10949. (sym-com (intern command)))
  10950. (unless current-prefix-arg
  10951. (setq current-prefix-arg helm-current-prefix-arg))
  10952. ;; Avoid having `this-command' set to *exit-minibuffer.
  10953. (setq this-command sym-com)
  10954. (call-interactively sym-com)
  10955. (setq extended-command-history
  10956. (cons command (delete command history)))))))
  10957. ;;;###autoload
  10958. (defun helm-manage-advice ()
  10959. "Preconfigured `helm' to disable/enable function advices."
  10960. (interactive)
  10961. (helm-other-buffer 'helm-c-source-advice "*helm advice*"))
  10962. ;;;###autoload
  10963. (defun helm-bookmark-ext ()
  10964. "Preconfigured `helm' for bookmark-extensions sources.
  10965. Needs bookmark-ext.el:
  10966. <http://mercurial.intuxication.org/hg/emacs-bookmark-extension>.
  10967. Contain also `helm-c-source-google-suggest'."
  10968. (interactive)
  10969. (helm
  10970. :sources
  10971. '(helm-c-source-bookmark-files&dirs
  10972. helm-c-source-bookmark-w3m
  10973. helm-c-source-google-suggest
  10974. helm-c-source-bmkext-addressbook
  10975. helm-c-source-bookmark-gnus
  10976. helm-c-source-bookmark-info
  10977. helm-c-source-bookmark-man
  10978. helm-c-source-bookmark-images
  10979. helm-c-source-bookmark-su-files&dirs
  10980. helm-c-source-bookmark-ssh-files&dirs)
  10981. :prompt "SearchBookmark: "
  10982. :buffer "*helm bmkext*"))
  10983. ;;;###autoload
  10984. (defun helm-simple-call-tree ()
  10985. "Preconfigured `helm' for simple-call-tree. List function relationships.
  10986. Needs simple-call-tree.el.
  10987. http://www.emacswiki.org/cgi-bin/wiki/download/simple-call-tree.el"
  10988. (interactive)
  10989. (helm-other-buffer
  10990. '(helm-c-source-simple-call-tree-functions-callers
  10991. helm-c-source-simple-call-tree-callers-functions)
  10992. "*helm simple-call-tree*"))
  10993. ;;;###autoload
  10994. (defun helm-mark-ring ()
  10995. "Preconfigured `helm' for `helm-c-source-mark-ring'."
  10996. (interactive)
  10997. (helm :sources 'helm-c-source-mark-ring))
  10998. ;;;###autoload
  10999. (defun helm-global-mark-ring ()
  11000. "Preconfigured `helm' for `helm-c-source-global-mark-ring'."
  11001. (interactive)
  11002. (helm :sources 'helm-c-source-global-mark-ring))
  11003. ;;;###autoload
  11004. (defun helm-all-mark-rings ()
  11005. "Preconfigured `helm' for `helm-c-source-global-mark-ring' and \
  11006. `helm-c-source-mark-ring'."
  11007. (interactive)
  11008. (helm :sources '(helm-c-source-mark-ring
  11009. helm-c-source-global-mark-ring)))
  11010. ;;;###autoload
  11011. (defun helm-yaoddmuse-emacswiki-edit-or-view ()
  11012. "Preconfigured `helm' to edit or view EmacsWiki page.
  11013. Needs yaoddmuse.el.
  11014. http://www.emacswiki.org/emacs/download/yaoddmuse.el"
  11015. (interactive)
  11016. (helm :sources 'helm-c-source-yaoddmuse-emacswiki-edit-or-view))
  11017. ;;;###autoload
  11018. (defun helm-yaoddmuse-emacswiki-post-library ()
  11019. "Preconfigured `helm' to post library to EmacsWiki.
  11020. Needs yaoddmuse.el.
  11021. http://www.emacswiki.org/emacs/download/yaoddmuse.el"
  11022. (interactive)
  11023. (helm :sources 'helm-c-source-yaoddmuse-emacswiki-post-library))
  11024. ;;;###autoload
  11025. (defun helm-eval-expression (arg)
  11026. "Preconfigured helm for `helm-c-source-evaluation-result'."
  11027. (interactive "P")
  11028. (helm :sources 'helm-c-source-evaluation-result
  11029. :input (when arg (thing-at-point 'sexp))
  11030. :buffer "*helm eval*"
  11031. :history 'helm-eval-expression-input-history
  11032. :keymap helm-eval-expression-map))
  11033. ;;;###autoload
  11034. (defun helm-eval-expression-with-eldoc ()
  11035. "Preconfigured helm for `helm-c-source-evaluation-result' with `eldoc' support. "
  11036. (interactive)
  11037. (declare (special eldoc-idle-delay))
  11038. (let ((timer (run-with-idle-timer eldoc-idle-delay
  11039. 'repeat 'helm-eldoc-show-in-eval))
  11040. (minibuffer-completing-symbol t) ; Enable lisp completion.
  11041. (completion-cycle-threshold t)) ; Always cycle, no pesty completion buffer (emacs24 only).
  11042. (unwind-protect
  11043. (minibuffer-with-setup-hook
  11044. 'helm-eldoc-store-minibuffer
  11045. (call-interactively 'helm-eval-expression))
  11046. (and timer (cancel-timer timer))
  11047. (setq helm-eldoc-active-minibuffers-list
  11048. (cdr helm-eldoc-active-minibuffers-list)))))
  11049. ;;;###autoload
  11050. (defun helm-calcul-expression ()
  11051. "Preconfigured helm for `helm-c-source-calculation-result'."
  11052. (interactive)
  11053. (helm-other-buffer 'helm-c-source-calculation-result "*helm calcul*"))
  11054. ;;;###autoload
  11055. (defun helm-surfraw (pattern engine)
  11056. "Preconfigured `helm' to search PATTERN with search ENGINE."
  11057. (interactive (list (read-string "SearchFor: "
  11058. nil 'helm-surfraw-input-history)
  11059. (helm-comp-read
  11060. "Engine: "
  11061. (helm-c-build-elvi-list)
  11062. :must-match t
  11063. :name "Surfraw Search Engines"
  11064. :history helm-surfraw-engines-history)))
  11065. (let* ((engine-nodesc (car (split-string engine)))
  11066. (url (with-temp-buffer
  11067. (apply 'call-process "surfraw" nil t nil
  11068. ;;JAVE
  11069. (append (list engine-nodesc "-p") (split-string pattern)))
  11070. (replace-regexp-in-string
  11071. "\n" "" (buffer-string))))
  11072. (browse-url-browser-function (or helm-surfraw-default-browser-function
  11073. browse-url-browser-function)))
  11074. (if (string= engine-nodesc "W")
  11075. (helm-c-browse-url helm-c-home-url)
  11076. (helm-c-browse-url url)
  11077. (setq helm-surfraw-engines-history
  11078. (cons engine (delete engine helm-surfraw-engines-history))))))
  11079. ;;;###autoload
  11080. (defun helm-call-source ()
  11081. "Preconfigured `helm' to call helm source."
  11082. (interactive)
  11083. (helm :sources 'helm-c-source-call-source
  11084. :buffer helm-source-select-buffer))
  11085. ;;;###autoload
  11086. (defun helm-execute-helm-command ()
  11087. "Preconfigured `helm' to execute preconfigured `helm'."
  11088. (interactive)
  11089. (helm-other-buffer 'helm-c-source-helm-commands
  11090. "*helm commands*"))
  11091. ;;;###autoload
  11092. (defun helm-create (&optional string initial-input)
  11093. "Preconfigured `helm' to do many create actions from STRING.
  11094. See also `helm-create--actions'."
  11095. (interactive)
  11096. (setq string (or string (read-string "Create Helm: " initial-input)))
  11097. (helm :sources '(((name . "Helm Create")
  11098. (header-name . (lambda (_) (format "Action for \"%s\"" string)))
  11099. (candidates . helm-create--actions)
  11100. (candidate-number-limit)
  11101. (action . (lambda (func) (funcall func string)))))))
  11102. ;;;###autoload
  11103. (defun helm-top ()
  11104. "Preconfigured `helm' for top command."
  11105. (interactive)
  11106. (let ((helm-samewindow t)
  11107. (helm-enable-shortcuts)
  11108. (helm-display-function 'helm-default-display-buffer)
  11109. (helm-candidate-number-limit 9999))
  11110. (save-window-excursion
  11111. (delete-other-windows)
  11112. (helm-other-buffer 'helm-c-source-top "*helm top*"))))
  11113. ;;;###autoload
  11114. (defun helm-select-xfont ()
  11115. "Preconfigured `helm' to select Xfont."
  11116. (interactive)
  11117. (helm-other-buffer 'helm-c-source-xfonts "*helm select* xfont"))
  11118. ;;;###autoload
  11119. (defun helm-world-time ()
  11120. "Preconfigured `helm' to show world time."
  11121. (interactive)
  11122. (helm-other-buffer 'helm-c-source-time-world "*helm world time*"))
  11123. ;;;###autoload
  11124. (defun helm-apt (arg)
  11125. "Preconfigured `helm' : frontend of APT package manager.
  11126. With a prefix arg reload cache."
  11127. (interactive "P")
  11128. (let ((query (read-string "Search Package: " nil 'helm-c-apt-input-history)))
  11129. (when arg (helm-c-apt-refresh))
  11130. (helm :sources 'helm-c-source-apt
  11131. :prompt "Search Package: "
  11132. :input query
  11133. :history 'helm-c-apt-input-history)))
  11134. ;;;###autoload
  11135. (defun helm-esh-pcomplete ()
  11136. "Preconfigured helm to provide helm completion in eshell."
  11137. (interactive)
  11138. (let* ((helm-quit-if-no-candidate t)
  11139. (helm-execute-action-at-once-if-one t)
  11140. (target (thing-at-point 'symbol))
  11141. (end (point))
  11142. (beg (or (and target (- end (length target)))
  11143. ;; Nothing at point.
  11144. (progn (insert " ") (point)))))
  11145. (setq helm-ec-target (or target " "))
  11146. (with-helm-show-completion beg end
  11147. (helm :sources 'helm-c-source-esh
  11148. :buffer "*helm pcomplete*"
  11149. :input (helm-ff-set-pattern ; Handle tramp filenames.
  11150. (car (last (ignore-errors ; Needed in lisp symbols completion.
  11151. (pcomplete-parse-arguments)))))))))
  11152. ;;;###autoload
  11153. (defun helm-eshell-history ()
  11154. "Preconfigured helm for eshell history."
  11155. (interactive)
  11156. (let* ((end (point))
  11157. (beg (save-excursion (eshell-bol) (point)))
  11158. (input (buffer-substring beg end))
  11159. flag-empty)
  11160. (when (eq beg end)
  11161. (insert " ")
  11162. (setq flag-empty t)
  11163. (setq end (point)))
  11164. (unwind-protect
  11165. (with-helm-show-completion beg end
  11166. (helm :sources 'helm-c-source-eshell-history
  11167. :buffer "*Eshell history*"
  11168. :input input))
  11169. (when (and flag-empty
  11170. (looking-back " "))
  11171. (delete-char -1)))))
  11172. ;;;###autoload
  11173. (defun helm-c-run-external-command (program)
  11174. "Preconfigured `helm' to run External PROGRAM asyncronously from Emacs.
  11175. If program is already running exit with error.
  11176. You can set your own list of commands with
  11177. `helm-c-external-commands-list'."
  11178. (interactive (list
  11179. (helm-comp-read
  11180. "RunProgram: "
  11181. (helm-c-external-commands-list-1 'sort)
  11182. :must-match t
  11183. :name "External Commands"
  11184. :history helm-external-command-history)))
  11185. (helm-run-or-raise program)
  11186. (setq helm-external-command-history
  11187. (cons program (delete program
  11188. (loop for i in helm-external-command-history
  11189. when (executable-find i) collect i)))))
  11190. ;;;###autoload
  11191. (defun helm-ratpoison-commands ()
  11192. "Preconfigured `helm' to execute ratpoison commands."
  11193. (interactive)
  11194. (helm-other-buffer 'helm-c-source-ratpoison-commands
  11195. "*helm ratpoison commands*"))
  11196. ;;;###autoload
  11197. (defun helm-ucs ()
  11198. "Preconfigured helm for `ucs-names' math symbols."
  11199. (interactive)
  11200. (helm :sources 'helm-c-source-ucs
  11201. :keymap helm-c-ucs-map))
  11202. ;;;###autoload
  11203. (defun helm-c-apropos ()
  11204. "Preconfigured helm to describe commands, functions, variables and faces."
  11205. (interactive)
  11206. (let ((default (thing-at-point 'symbol)))
  11207. (helm :sources
  11208. `(((name . "Commands")
  11209. (init . (lambda ()
  11210. (helm-c-apropos-init 'commandp ,default)))
  11211. (persistent-action . helm-lisp-completion-persistent-action)
  11212. (persistent-help . "Show brief doc in mode-line")
  11213. (candidates-in-buffer)
  11214. (action . (lambda (candidate)
  11215. (describe-function (intern candidate)))))
  11216. ((name . "Functions")
  11217. (init . (lambda ()
  11218. (helm-c-apropos-init #'(lambda (x) (and (fboundp x)
  11219. (not (commandp x))))
  11220. ,default)))
  11221. (persistent-action . helm-lisp-completion-persistent-action)
  11222. (persistent-help . "Show brief doc in mode-line")
  11223. (candidates-in-buffer)
  11224. (action . (lambda (candidate)
  11225. (describe-function (intern candidate)))))
  11226. ((name . "Variables")
  11227. (init . (lambda ()
  11228. (helm-c-apropos-init 'boundp ,default)))
  11229. (persistent-action . helm-lisp-completion-persistent-action)
  11230. (persistent-help . "Show brief doc in mode-line")
  11231. (candidates-in-buffer)
  11232. (action . (lambda (candidate)
  11233. (describe-variable (intern candidate)))))
  11234. ((name . "Faces")
  11235. (init . (lambda ()
  11236. (helm-c-apropos-init 'facep ,default)))
  11237. (persistent-action . helm-lisp-completion-persistent-action)
  11238. (persistent-help . "Show brief doc in mode-line")
  11239. (candidates-in-buffer)
  11240. (filtered-candidate-transformer . (lambda (candidates source)
  11241. (loop for c in candidates
  11242. collect (propertize c 'face (intern c)))))
  11243. (action . (lambda (candidate)
  11244. (describe-face (intern candidate)))))
  11245. ((name . "Helm attributes")
  11246. (candidates . (lambda ()
  11247. (mapcar 'symbol-name helm-additional-attributes)))
  11248. (action . (lambda (candidate)
  11249. (with-output-to-temp-buffer "*Help*"
  11250. (princ (get (intern candidate) 'helm-attrdoc))))))))))
  11251. ;;;###autoload
  11252. (defun helm-xrandr-set ()
  11253. (interactive)
  11254. (helm :sources 'helm-c-source-xrandr-change-resolution
  11255. :buffer "*helm xrandr*"))
  11256. ;;; Unit tests are now in ../developer-tools/unit-test-helm-config.el.
  11257. (provide 'helm-config)
  11258. ;; Local Variables:
  11259. ;; coding: utf-8
  11260. ;; End:
  11261. ;;; helm-config.el ends here