Klimi's new dotfiles with stow.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1855 lignes
82 KiB

il y a 4 ans
  1. ;;; helm-mode.el --- Enable helm completion everywhere. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2012 ~ 2019 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'cl-lib)
  15. (require 'helm)
  16. (require 'helm-lib)
  17. (require 'helm-files)
  18. (defvar crm-separator)
  19. (defvar ido-everywhere)
  20. (defvar completion-flex-nospace)
  21. (declare-function ido-mode "ido.el")
  22. (defgroup helm-mode nil
  23. "Enable helm completion."
  24. :group 'helm)
  25. (defcustom helm-completing-read-handlers-alist
  26. '((find-tag . helm-completing-read-default-find-tag)
  27. (xref-find-definitions . helm-completing-read-default-find-tag)
  28. (xref-find-references . helm-completing-read-default-find-tag)
  29. (tmm-menubar . nil)
  30. (find-file . nil)
  31. (execute-extended-command . nil)
  32. (dired-do-rename . helm-read-file-name-handler-1)
  33. (dired-do-copy . helm-read-file-name-handler-1)
  34. (dired-do-symlink . helm-read-file-name-handler-1)
  35. (dired-do-relsymlink . helm-read-file-name-handler-1)
  36. (dired-do-hardlink . helm-read-file-name-handler-1)
  37. (basic-save-buffer . helm-read-file-name-handler-1)
  38. (write-file . helm-read-file-name-handler-1)
  39. (write-region . helm-read-file-name-handler-1))
  40. "Completing read functions for specific Emacs commands.
  41. By default `helm-mode' use `helm-completing-read-default-handler' to
  42. provide helm completion in each `completing-read' or `read-file-name'
  43. found, but other functions can be specified here for specific
  44. commands. This also allow disabling helm completion for some commands
  45. when needed.
  46. Each entry is a cons cell like (EMACS_COMMAND . COMPLETING-READ_HANDLER)
  47. where key and value are symbols.
  48. Each key is an Emacs command that use originaly `completing-read'.
  49. Each value maybe a helm function that takes same arguments as
  50. `completing-read' plus NAME and BUFFER, where NAME is the name of the new
  51. helm source and BUFFER the name of the buffer we will use, but it can
  52. be also a function not using helm, in this case the function should
  53. take same args as `completing-read' and not be prefixed by \"helm-\".
  54. `helm' will use the name of the command calling `completing-read' as
  55. NAME and BUFFER will be computed as well with NAME but prefixed with
  56. \"*helm-mode-\".
  57. This function prefix name must start by \"helm-\" when it uses helm,
  58. otherwise `helm' assumes the function is not a helm function and
  59. expects same args as `completing-read', this allow you to define a
  60. handler not using helm completion.
  61. Example:
  62. (defun foo/test ()
  63. (interactive)
  64. (message \"%S\" (completing-read \"test: \" '(a b c d e))))
  65. (defun helm-foo/test-completing-read-handler (prompt collection
  66. predicate require-match
  67. initial-input hist def
  68. inherit-input-method
  69. name buffer)
  70. (helm-comp-read prompt collection :marked-candidates t
  71. :name name
  72. :buffer buffer))
  73. (add-to-list 'helm-completing-read-handlers-alist
  74. '(foo/test . helm-foo/test-completing-read-handler))
  75. We want here to make the regular `completing-read' in `foo/test'
  76. returns a list of candidate(s) instead of a single candidate.
  77. Note that this function will be reused for ALL the `completing-read'
  78. of this command, so it should handle all cases, e.g
  79. If first `completing-read' complete against symbols and
  80. second `completing-read' should handle only buffer,
  81. your specialized function should handle the both.
  82. If the value of an entry is nil completion will fall back to
  83. emacs vanilla behavior.
  84. Example:
  85. If you want to disable helm completion for `describe-function', use:
  86. (describe-function . nil)
  87. Ido is also supported, you can use `ido-completing-read' and
  88. `ido-read-file-name' as value of an entry or just 'ido.
  89. Example:
  90. Enable ido completion for `find-file':
  91. (find-file . ido)
  92. same as
  93. (find-file . ido-read-file-name)
  94. Note that you don't need to enable `ido-mode' for this to work, see
  95. `helm-mode' documentation."
  96. :group 'helm-mode
  97. :type '(alist :key-type symbol :value-type symbol))
  98. (defcustom helm-comp-read-case-fold-search helm-case-fold-search
  99. "Default Local setting of `helm-case-fold-search' for `helm-comp-read'.
  100. See `helm-case-fold-search' for more info."
  101. :group 'helm-mode
  102. :type 'symbol)
  103. (defcustom helm-mode-reverse-history t
  104. "Display history source after current source when non nil.
  105. Apply only in `helm-mode' handled commands."
  106. :group 'helm-mode
  107. :type 'boolean)
  108. (defcustom helm-completion-in-region-default-sort-fn
  109. 'helm-completion-in-region-sort-fn
  110. "The default sort function to sort candidates in completion-in-region.
  111. When nil no sorting is done.
  112. The function is a `filtered-candidate-transformer' function which takes
  113. two args CANDIDATES and SOURCE.
  114. It will be used only when `helm-completion-style' is either emacs or
  115. helm, otherwise when helm-fuzzy style is used, the fuzzy sort function
  116. will be used."
  117. :group 'helm-mode
  118. :type 'function)
  119. (defcustom helm-mode-fuzzy-match nil
  120. "Enable fuzzy matching in `helm-mode' globally.
  121. This is deprecated, use instead helm-fuzzy as `helm-completion-style' or
  122. even better 'emacs as `helm-completion-style' and add 'flex to
  123. `completion-styles' (emacs-27) or 'helm-flex if 'flex is not available
  124. in `completion-styles-alist' (emacs-26)."
  125. :group 'helm-mode
  126. :type 'boolean)
  127. (make-obsolete-variable 'helm-mode-fuzzy-match 'helm-completion-style "3.6.0")
  128. (defcustom helm-completion-mark-suffix t
  129. "Push mark at end of suffix when non nil."
  130. :group 'helm-mode
  131. :type 'boolean)
  132. (defvar helm-mode-minibuffer-setup-hook-black-list '(minibuffer-completion-help)
  133. "Incompatible `minibuffer-setup-hook' functions go here.
  134. A list of symbols. Helm-mode is rejecting all lambda's, byte-code fns
  135. and all functions belonging in this list from `minibuffer-setup-hook'.
  136. This is mainly needed to prevent \"*Completions*\" buffers to popup.")
  137. (defface helm-mode-prefix
  138. '((t (:background "red" :foreground "black")))
  139. "Face used for prefix completion."
  140. :group 'helm-mode)
  141. (defvar helm-comp-read-map
  142. (let ((map (make-sparse-keymap)))
  143. (set-keymap-parent map helm-map)
  144. (define-key map (kbd "<C-return>") 'helm-cr-empty-string)
  145. (define-key map (kbd "M-RET") 'helm-cr-empty-string)
  146. map)
  147. "Keymap for `helm-comp-read'.")
  148. (defun helm-mode-delete-char-backward-1 ()
  149. (interactive)
  150. (condition-case err
  151. (call-interactively 'delete-backward-char)
  152. (text-read-only
  153. (if (with-selected-window (minibuffer-window)
  154. (not (string= (minibuffer-contents) "")))
  155. (message "Trying to delete prefix completion, next hit will quit")
  156. (user-error "%s" (car err))))))
  157. (put 'helm-mode-delete-char-backward-1 'helm-only t)
  158. (defun helm-mode-delete-char-backward-2 ()
  159. (interactive)
  160. (condition-case _err
  161. (call-interactively 'delete-backward-char)
  162. (text-read-only
  163. (unless (with-selected-window (minibuffer-window)
  164. (string= (minibuffer-contents) ""))
  165. (with-helm-current-buffer
  166. (run-with-timer 0.1 nil (lambda ()
  167. (call-interactively 'delete-backward-char))))
  168. (helm-keyboard-quit)))))
  169. (put 'helm-mode-delete-char-backward-2 'helm-only t)
  170. (helm-multi-key-defun helm-mode-delete-char-backward-maybe
  171. "Delete char backward when text is not the prefix helm is completing against.
  172. First call warn user about deleting prefix completion.
  173. Second call delete backward char in current-buffer and quit helm completion,
  174. letting user starting a new completion with a new prefix."
  175. '(helm-mode-delete-char-backward-1 helm-mode-delete-char-backward-2) 1)
  176. (defcustom helm-completion-style 'emacs
  177. "Style of completion to use in `completion-in-region'.
  178. This affect only `completion-at-point' and friends, and
  179. the `completing-read' using the default handler
  180. i.e. `helm-completing-read-default-handler'.
  181. NB: This have nothing to do with `completion-styles', it is independent to
  182. helm, but when using emacs as helm-completion-style helm
  183. will use the `completion-styles' for its completions.
  184. Up to the user to configure `completion-styles'.
  185. There is three possible value to use:
  186. - helm, use multi match regular helm completion.
  187. - helm-fuzzy, use fuzzy matching, note that as usual when
  188. entering a space helm switch to multi matching mode.
  189. - emacs, use regular emacs completion according to
  190. `completion-styles', note that even in this style, helm allows using
  191. multi match. Emacs-27 provide a style called `flex' that can be used
  192. aside `helm' style (see `completion-styles-alist'). When `flex' style
  193. is not available (Emacs<27) helm provide `helm-flex' style which is similar to
  194. `flex' and helm fuzzy matching.
  195. For a better experience, if you don't know what to use, set
  196. `completion-styles' to '(flex) if you are using emacs-27 or to
  197. \'(helm-flex) if you are using emacs-26 and keep 'emacs as default
  198. value for `helm-completion-style'. Advanced users can also have a
  199. look to `completion-category-overrides' to set styles according to category.
  200. Please use custom interface or `customize-set-variable' to set this,
  201. NOT `setq'."
  202. :group 'helm-mode
  203. :type '(choice (const :tag "Emacs" emacs)
  204. (const :tag "Helm" helm)
  205. (const :tag "Helm-fuzzy" helm-fuzzy))
  206. :set (lambda (var val)
  207. (set var val)
  208. (if (memq val '(helm helm-fuzzy))
  209. (define-key helm-comp-read-map (kbd "DEL") 'helm-mode-delete-char-backward-maybe)
  210. (define-key helm-comp-read-map (kbd "DEL") 'delete-backward-char))))
  211. (defconst helm-completion--all-styles
  212. (let ((flex (if (assq 'flex completion-styles-alist)
  213. 'flex 'helm-flex)))
  214. (helm-fast-remove-dups
  215. (append (list 'helm flex)
  216. (mapcar 'car completion-styles-alist)))))
  217. (defconst helm-completion--styles-type
  218. `(repeat :tag "with other completion styles"
  219. (choice ,@(mapcar (lambda (x) (list 'const x))
  220. helm-completion--all-styles))))
  221. (defcustom helm-completion-styles-alist '((gud-mode . helm))
  222. "Allow configuring `helm-completion-style' per mode.
  223. Each entry is a cons cell like (mode . style) where style must be a
  224. suitable value for `helm-completion-style'.
  225. When specifying emacs as style for a mode, `completion-styles' can be
  226. specified by using a cons cell specifying completion-styles to use
  227. with helm emacs style, e.g. (foo-mode . (emacs helm flex)) will set
  228. `completion-styles' to '(helm flex) for foo-mode, this affect only
  229. completions happening in buffers and not minibuffer completions,
  230. i.e. completing-read's."
  231. :group 'helm-mode
  232. :type
  233. `(alist :key-type (symbol :tag "Major Mode")
  234. :value-type
  235. (choice :tag "Use helm style or completion styles"
  236. (radio :tag "Helm Style"
  237. (const helm)
  238. (const helm-fuzzy)
  239. (const emacs))
  240. (cons :tag "Completion Styles"
  241. (const :tag "Using Helm `emacs' style" emacs)
  242. ,helm-completion--styles-type))))
  243. ;;; helm-comp-read
  244. ;;
  245. ;;
  246. (defun helm-cr-empty-string ()
  247. "Return empty string."
  248. (interactive)
  249. (with-helm-alive-p
  250. (helm-exit-and-execute-action
  251. (lambda (_candidate)
  252. (identity "")))))
  253. (put 'helm-cr-empty-string 'helm-only t)
  254. (defun helm-mode--keyboard-quit ()
  255. ;; Use this instead of `keyboard-quit'
  256. ;; to avoid deactivating mark in current-buffer.
  257. (let ((debug-on-quit nil))
  258. (signal 'quit nil)))
  259. (cl-defun helm-comp-read-get-candidates (collection &optional
  260. test sort-fn alistp
  261. (input helm-pattern))
  262. "Convert COLLECTION to list removing elements that don't match TEST.
  263. See `helm-comp-read' about supported COLLECTION arguments.
  264. SORT-FN is a predicate to sort COLLECTION.
  265. ALISTP when non--nil will not use `all-completions' to collect
  266. candidates because it doesn't handle alists correctly for helm.
  267. i.e In `all-completions' the car of each pair is used as value.
  268. In helm we want to use the cdr instead like \(display . real\),
  269. so we return the alist as it is with no transformation by
  270. `all-completions'.
  271. e.g
  272. \(setq A '((a . 1) (b . 2) (c . 3)))
  273. ==>((a . 1) (b . 2) (c . 3))
  274. \(helm-comp-read \"test: \" A :alistp nil
  275. :exec-when-only-one t
  276. :initial-input \"a\")
  277. ==>\"a\" Which is not what we expect.
  278. \(helm-comp-read \"test: \" A :alistp t
  279. :exec-when-only-one t
  280. :initial-input \"1\")
  281. ==>\"1\"
  282. See docstring of `all-completions' for more info.
  283. INPUT is the string you want to complete against, defaulting to
  284. `helm-pattern' which is the value of what you enter in minibuffer.
  285. Note that when using a function as COLLECTION this value will be
  286. available with the input argument of the function only when using a
  287. sync source from `helm-comp-read', i.e not using
  288. `:candidates-in-buffer', otherwise the function is called only once
  289. with an empty string as value for `helm-pattern' because
  290. `helm-pattern' is not yet computed, which is what we want otherwise
  291. data would not be fully collected at init time.
  292. If COLLECTION is an `obarray', a TEST should be needed. See `obarray'."
  293. ;; Ensure COLLECTION is computed from `helm-current-buffer'
  294. ;; because some functions used as COLLECTION work
  295. ;; only in the context of current-buffer (Issue #1030) .
  296. (with-helm-current-buffer
  297. (let ((cands
  298. (cond ((vectorp collection)
  299. (all-completions input collection test))
  300. ((and (symbolp collection) (boundp collection)
  301. ;; Issue #324 history is let-bounded and given
  302. ;; quoted as hist argument of completing-read.
  303. ;; See example in `rcirc-browse-url'.
  304. (symbolp (symbol-value collection)))
  305. nil)
  306. ;; When collection is a symbol, most of the time
  307. ;; it should be a symbol used as a minibuffer-history.
  308. ;; The value of this symbol in this case return a list
  309. ;; of string which maybe are converted later as symbol
  310. ;; in special cases.
  311. ;; we treat here commandp as a special case as it return t
  312. ;; also with a string unless its last arg is provided.
  313. ;; Also, the history collections generally collect their
  314. ;; elements as string, so intern them to call predicate.
  315. ((and (symbolp collection) (boundp collection) test)
  316. (let ((predicate (lambda (elm)
  317. (condition-case _err
  318. (if (eq test 'commandp)
  319. (funcall test (intern elm))
  320. (funcall test elm))
  321. (wrong-type-argument
  322. (funcall test (intern elm)))))))
  323. (all-completions input (symbol-value collection) predicate)))
  324. ((and (symbolp collection) (boundp collection))
  325. (all-completions input (symbol-value collection)))
  326. ;; Normally file completion should not be handled here,
  327. ;; but special cases like `find-file-at-point' do it.
  328. ;; Handle here specially such cases.
  329. ((and (functionp collection) (not (string= input ""))
  330. minibuffer-completing-file-name)
  331. (cl-loop for f in (funcall collection input test)
  332. unless (member f '("./" "../"))
  333. if (string-match helm--url-regexp input)
  334. collect f
  335. else
  336. collect (concat (file-name-as-directory
  337. (helm-basedir input))
  338. f)))
  339. ((functionp collection)
  340. (funcall collection input test t))
  341. ((and alistp (null test)) collection)
  342. ;; Next test ensure circular objects are removed
  343. ;; with `all-completions' (Issue #1530).
  344. (t (all-completions input collection test)))))
  345. (if sort-fn (sort cands sort-fn) cands))))
  346. (defun helm-cr--pattern-in-candidates-p (candidates)
  347. (or (assoc helm-pattern candidates)
  348. (assq (intern helm-pattern) candidates)
  349. (member helm-pattern candidates)
  350. (member (downcase helm-pattern) candidates)
  351. (member (upcase helm-pattern) candidates)))
  352. (defun helm-cr-default-transformer (candidates source)
  353. "Default filter candidate function for `helm-comp-read'."
  354. (let ((must-match (helm-attr 'must-match source))
  355. unknown-pattern)
  356. (unless (or (eq must-match t)
  357. (string= helm-pattern "")
  358. (helm-cr--pattern-in-candidates-p candidates))
  359. (setq candidates (append (list
  360. ;; Unquote helm-pattern
  361. ;; when it is added
  362. ;; as candidate: Why? #2015
  363. ;; (replace-regexp-in-string
  364. ;; "\\s\\" "" helm-pattern)
  365. helm-pattern)
  366. candidates))
  367. ;; Notify pattern have been added to candidates.
  368. (setq unknown-pattern t))
  369. (cl-loop for c in candidates
  370. for cand = (if (stringp c)
  371. (replace-regexp-in-string "\\s\\" "" c)
  372. c)
  373. for pat = (replace-regexp-in-string "\\s\\" "" helm-pattern)
  374. if (and (or (equal c pat) (equal c helm-pattern))
  375. unknown-pattern)
  376. collect
  377. (cons (concat (propertize
  378. " " 'display
  379. (propertize "[?]" 'face 'helm-ff-prefix))
  380. c)
  381. c)
  382. into lst
  383. else collect (if (and (stringp cand)
  384. (string-match "\n" cand))
  385. (cons (replace-regexp-in-string "\n" "->" c) c)
  386. c)
  387. into lst
  388. finally return (helm-fast-remove-dups lst :test 'equal))))
  389. (defun helm-comp-read--move-to-first-real-candidate ()
  390. (helm-aif (helm-get-selection nil 'withprop)
  391. (when (string= (get-text-property 0 'display it) "[?]")
  392. (helm-next-line))))
  393. (defun helm-cr-default (default cands)
  394. (delq nil
  395. (cond ((and (stringp default)
  396. (not (string= default ""))
  397. (string= helm-pattern ""))
  398. (cons default (delete default cands)))
  399. ((and (consp default) (string= helm-pattern ""))
  400. (append (cl-loop for d in default
  401. ;; Don't convert
  402. ;; nil to "nil" (i.e the string)
  403. ;; it will be delq'ed on top.
  404. collect (if (null d) d (helm-stringify d)))
  405. cands))
  406. (t cands))))
  407. ;;;###autoload
  408. (cl-defun helm-comp-read (prompt collection
  409. &key
  410. test
  411. initial-input
  412. default
  413. preselect
  414. (buffer "*Helm Completions*")
  415. must-match
  416. fuzzy
  417. reverse-history
  418. (requires-pattern 0)
  419. history
  420. input-history
  421. (case-fold helm-comp-read-case-fold-search)
  422. (del-input t)
  423. (persistent-action nil)
  424. (persistent-help "DoNothing")
  425. (mode-line helm-comp-read-mode-line)
  426. help-message
  427. (keymap helm-comp-read-map)
  428. (name "Helm Completions")
  429. header-name
  430. candidates-in-buffer
  431. match-part
  432. match-dynamic
  433. exec-when-only-one
  434. quit-when-no-cand
  435. (volatile t)
  436. sort
  437. fc-transformer
  438. hist-fc-transformer
  439. marked-candidates
  440. nomark
  441. (alistp t)
  442. (candidate-number-limit helm-candidate-number-limit)
  443. multiline
  444. allow-nest
  445. (group 'helm))
  446. "Read a string in the minibuffer, with helm completion.
  447. It is helm `completing-read' equivalent.
  448. - PROMPT is the prompt name to use.
  449. - COLLECTION can be a list, vector, obarray or hash-table.
  450. It can be also a function that receives three arguments:
  451. the values string, predicate and t. See `all-completions' for more details.
  452. Keys description:
  453. - TEST: A predicate called with one arg i.e candidate.
  454. - INITIAL-INPUT: Same as input arg in `helm'.
  455. - PRESELECT: See preselect arg of `helm'.
  456. - DEFAULT: This option is used only for compatibility with regular
  457. Emacs `completing-read' (Same as DEFAULT arg of `completing-read').
  458. - BUFFER: Name of helm-buffer.
  459. - MUST-MATCH: Candidate selected must be one of COLLECTION.
  460. - FUZZY: Enable fuzzy matching.
  461. - REVERSE-HISTORY: When non--nil display history source after current
  462. source completion.
  463. - REQUIRES-PATTERN: Same as helm attribute, default is 0.
  464. - HISTORY: A list containing specific history, default is nil.
  465. When it is non--nil, all elements of HISTORY are displayed in
  466. a special source before COLLECTION.
  467. - INPUT-HISTORY: A symbol. the minibuffer input history will be
  468. stored there, if nil or not provided, `minibuffer-history'
  469. will be used instead.
  470. - CASE-FOLD: Same as `helm-case-fold-search'.
  471. - DEL-INPUT: Boolean, when non--nil (default) remove the partial
  472. minibuffer input from HISTORY is present.
  473. - PERSISTENT-ACTION: A function called with one arg i.e candidate.
  474. - PERSISTENT-HELP: A string to document PERSISTENT-ACTION.
  475. - MODE-LINE: A string or list to display in mode line.
  476. Default is `helm-comp-read-mode-line'.
  477. - KEYMAP: A keymap to use in this `helm-comp-read'.
  478. (the keymap will be shared with history source)
  479. - NAME: The name related to this local source.
  480. - HEADER-NAME: A function to alter NAME, see `helm'.
  481. - EXEC-WHEN-ONLY-ONE: Bound `helm-execute-action-at-once-if-one'
  482. to non--nil. (possibles values are t or nil).
  483. - VOLATILE: Use volatile attribute.
  484. - SORT: A predicate to give to `sort' e.g `string-lessp'
  485. Use this only on small data as it is ineficient.
  486. If you want to sort faster add a sort function to
  487. FC-TRANSFORMER.
  488. Note that FUZZY when enabled is already providing a sort function.
  489. - FC-TRANSFORMER: A `filtered-candidate-transformer' function
  490. or a list of functions.
  491. - HIST-FC-TRANSFORMER: A `filtered-candidate-transformer'
  492. function for the history source.
  493. - MARKED-CANDIDATES: If non--nil return candidate or marked candidates as a list.
  494. - NOMARK: When non--nil don't allow marking candidates.
  495. - ALISTP: (default is non--nil) See `helm-comp-read-get-candidates'.
  496. - CANDIDATES-IN-BUFFER: when non--nil use a source build with
  497. `helm-source-in-buffer' which is much faster.
  498. Argument VOLATILE have no effect when CANDIDATES-IN-BUFFER is non--nil.
  499. - MATCH-PART: Allow matching only one part of candidate.
  500. See match-part documentation in `helm-source'.
  501. - MATCH-DYNAMIC: See match-dynamic in `helm-source-sync'
  502. It have no effect when used with CANDIDATES-IN-BUFFER.
  503. - ALLOW-NEST: Allow nesting this `helm-comp-read' in a helm session.
  504. See `helm'.
  505. - MULTILINE: See multiline in `helm-source'.
  506. - GROUP: See group in `helm-source'.
  507. Any prefix args passed during `helm-comp-read' invocation will be recorded
  508. in `helm-current-prefix-arg', otherwise if prefix args were given before
  509. `helm-comp-read' invocation, the value of `current-prefix-arg' will be used.
  510. That's mean you can pass prefix args before or after calling a command
  511. that use `helm-comp-read' See `helm-M-x' for example."
  512. (when (get-buffer helm-action-buffer)
  513. (kill-buffer helm-action-buffer))
  514. (let ((action-fn `(("Sole action (Identity)"
  515. . (lambda (candidate)
  516. (if ,marked-candidates
  517. (helm-marked-candidates)
  518. (identity candidate)))))))
  519. (let* ((minibuffer-completion-confirm must-match)
  520. (minibuffer-completion-predicate test)
  521. (minibuffer-completion-table collection)
  522. (helm-read-file-name-mode-line-string
  523. (replace-regexp-in-string "helm-maybe-exit-minibuffer"
  524. "helm-confirm-and-exit-minibuffer"
  525. helm-read-file-name-mode-line-string))
  526. (get-candidates
  527. (lambda ()
  528. (let ((cands (helm-comp-read-get-candidates
  529. ;; If `helm-pattern' is passed as INPUT
  530. ;; and :alistp is nil INPUT is passed to
  531. ;; `all-completions' which defeat helm
  532. ;; matching functions (multi match, fuzzy
  533. ;; etc...) issue #2134.
  534. collection test sort alistp
  535. (if (and match-dynamic (null candidates-in-buffer))
  536. helm-pattern ""))))
  537. (helm-cr-default default cands))))
  538. (history-get-candidates
  539. (lambda ()
  540. (let ((cands (helm-comp-read-get-candidates
  541. history test nil alistp)))
  542. (when cands
  543. (delete "" (helm-cr-default default cands))))))
  544. (src-hist (helm-build-sync-source (format "%s History" name)
  545. :candidates history-get-candidates
  546. :fuzzy-match fuzzy
  547. :multiline multiline
  548. :match-part match-part
  549. :filtered-candidate-transformer
  550. (append '((lambda (candidates sources)
  551. (cl-loop for i in candidates
  552. ;; Input is added to history in completing-read's
  553. ;; and may be regexp-quoted, so unquote it
  554. ;; but check if cand is a string (it may be at this stage
  555. ;; a symbol or nil) Issue #1553.
  556. when (stringp i)
  557. collect (replace-regexp-in-string "\\s\\" "" i))))
  558. (and hist-fc-transformer (helm-mklist hist-fc-transformer)))
  559. :persistent-action persistent-action
  560. :persistent-help persistent-help
  561. :keymap keymap
  562. :must-match must-match
  563. :group group
  564. :mode-line mode-line
  565. :help-message help-message
  566. :action action-fn))
  567. (src (helm-build-sync-source name
  568. :candidates get-candidates
  569. :match-part match-part
  570. :multiline multiline
  571. :header-name header-name
  572. :filtered-candidate-transformer
  573. (let ((transformers (helm-mklist fc-transformer)))
  574. (append transformers
  575. (unless (member 'helm-cr-default-transformer transformers)
  576. '(helm-cr-default-transformer))))
  577. :requires-pattern requires-pattern
  578. :persistent-action persistent-action
  579. :persistent-help persistent-help
  580. :fuzzy-match fuzzy
  581. :keymap keymap
  582. :must-match must-match
  583. :group group
  584. :mode-line mode-line
  585. :match-dynamic match-dynamic
  586. :help-message help-message
  587. :action action-fn
  588. :volatile volatile))
  589. (src-1 (helm-build-in-buffer-source name
  590. :data get-candidates
  591. :match-part match-part
  592. :multiline multiline
  593. :header-name header-name
  594. :filtered-candidate-transformer
  595. (append (helm-mklist fc-transformer)
  596. '(helm-cr-default-transformer))
  597. :requires-pattern requires-pattern
  598. :persistent-action persistent-action
  599. :fuzzy-match fuzzy
  600. :keymap keymap
  601. :must-match must-match
  602. :group group
  603. :persistent-help persistent-help
  604. :mode-line mode-line
  605. :help-message help-message
  606. :action action-fn))
  607. (src-list (list src-hist
  608. (cons (cons 'must-match must-match)
  609. (if candidates-in-buffer
  610. src-1 src))))
  611. (helm-execute-action-at-once-if-one exec-when-only-one)
  612. (helm-quit-if-no-candidate quit-when-no-cand)
  613. result)
  614. (when nomark
  615. (setq src-list (cl-loop for src in src-list
  616. collect (cons '(nomark) src))))
  617. (when reverse-history (setq src-list (nreverse src-list)))
  618. (add-hook 'helm-after-update-hook 'helm-comp-read--move-to-first-real-candidate)
  619. (unwind-protect
  620. (setq result (helm
  621. :sources src-list
  622. :input initial-input
  623. :default default
  624. :preselect preselect
  625. :prompt prompt
  626. :resume 'noresume
  627. :keymap keymap ;; Needed with empty collection.
  628. :allow-nest allow-nest
  629. :candidate-number-limit candidate-number-limit
  630. :case-fold-search case-fold
  631. :history (and (symbolp input-history) input-history)
  632. :buffer buffer))
  633. (remove-hook 'helm-after-update-hook 'helm-comp-read--move-to-first-real-candidate))
  634. ;; Avoid adding an incomplete input to history.
  635. (when (and result history del-input)
  636. (cond ((and (symbolp history) ; History is a symbol.
  637. (not (symbolp (symbol-value history)))) ; Fix Issue #324.
  638. ;; Be sure history is not a symbol with a nil value.
  639. (helm-aif (symbol-value history) (setcar it result)))
  640. ((consp history) ; A list with a non--nil value.
  641. (setcar history result))
  642. (t ; Possibly a symbol with a nil value.
  643. (set history (list result)))))
  644. (or result (helm-mode--keyboard-quit)))))
  645. ;; Generic completing-read
  646. ;;
  647. ;; Support also function as collection.
  648. ;; e.g M-x man is supported.
  649. ;; Support hash-table and vectors as collection.
  650. ;; NOTE:
  651. ;; Some crap emacs functions may not be supported
  652. ;; like ffap-alternate-file (bad use of completing-read)
  653. ;; and maybe others.
  654. ;; Provide a mode `helm-mode' which turn on
  655. ;; helm in all `completing-read' and `read-file-name' in Emacs.
  656. ;;
  657. (defvar helm-completion-mode-string " Helm")
  658. (defvar helm-completion-mode-quit-message
  659. "Helm completion disabled")
  660. (defvar helm-completion-mode-start-message
  661. "Helm completion enabled")
  662. ;;; Specialized handlers
  663. ;;
  664. ;;
  665. (defun helm-completing-read-symbols
  666. (prompt _collection test _require-match init
  667. hist default _inherit-input-method name buffer)
  668. "Specialized function for fast symbols completion in `helm-mode'."
  669. (require 'helm-elisp)
  670. (or
  671. (helm
  672. :sources (helm-build-in-buffer-source name
  673. :init (lambda ()
  674. (helm-apropos-init (lambda (x)
  675. (and (funcall test x)
  676. (not (keywordp x))))
  677. (or (car-safe default) default)))
  678. :filtered-candidate-transformer 'helm-apropos-default-sort-fn
  679. :help-message #'helm-comp-read-help-message
  680. :fuzzy-match helm-mode-fuzzy-match
  681. :persistent-action
  682. (lambda (candidate)
  683. (helm-lisp-completion-persistent-action
  684. candidate name))
  685. :persistent-help (helm-lisp-completion-persistent-help))
  686. :prompt prompt
  687. :buffer buffer
  688. :input init
  689. :history hist
  690. :resume 'noresume
  691. :default (or default ""))
  692. (helm-mode--keyboard-quit)))
  693. ;;; Generic completing read
  694. ;;
  695. ;;
  696. (defun helm-completing-read-default-1
  697. (prompt collection test require-match
  698. init hist default _inherit-input-method
  699. name buffer &optional cands-in-buffer exec-when-only-one)
  700. "Call `helm-comp-read' with same args as `completing-read'.
  701. Extra optional arg CANDS-IN-BUFFER mean use `candidates-in-buffer'
  702. method which is faster.
  703. It should be used when candidate list don't need to rebuild dynamically."
  704. (let ((history (or (car-safe hist) hist))
  705. (initial-input (helm-aif (pcase init
  706. ((pred (stringp)) init)
  707. ;; INIT is a cons cell.
  708. (`(,l . ,_ll) l))
  709. it)))
  710. (helm-comp-read
  711. prompt collection
  712. :test test
  713. :history history
  714. :reverse-history helm-mode-reverse-history
  715. :input-history history
  716. :must-match require-match
  717. :alistp nil
  718. :help-message #'helm-comp-read-help-message
  719. :name name
  720. :requires-pattern (if (and (stringp default)
  721. (string= default "")
  722. (or (eq require-match 'confirm)
  723. (eq require-match
  724. 'confirm-after-completion)))
  725. 1 0)
  726. :candidates-in-buffer cands-in-buffer
  727. :exec-when-only-one exec-when-only-one
  728. :fuzzy helm-mode-fuzzy-match
  729. :buffer buffer
  730. ;; If DEF is not provided, fallback to empty string
  731. ;; to avoid `thing-at-point' to be appended on top of list
  732. :default (or default "")
  733. ;; Fail with special characters (e.g in gnus "nnimap+gmail:")
  734. ;; if regexp-quote is not used.
  735. ;; when init is added to history, it will be unquoted by
  736. ;; helm-comp-read.
  737. :initial-input initial-input)))
  738. (defun helm-completing-read-default-2
  739. (prompt collection predicate require-match
  740. init hist default _inherit-input-method
  741. name buffer &optional exec-when-only-one)
  742. "Call `helm-comp-read' with same args as `completing-read'.
  743. This handler use dynamic matching which allow honouring `completion-styles'."
  744. (let* ((history (or (car-safe hist) hist))
  745. (input (pcase init
  746. ((pred (stringp)) init)
  747. ;; INIT is a cons cell.
  748. (`(,l . ,_ll) l)))
  749. (completion-flex-nospace t)
  750. (completion-styles
  751. (helm--prepare-completion-styles 'nomode))
  752. (metadata (or (completion-metadata (or input "") collection predicate)
  753. '(metadata)))
  754. (afun (or (plist-get completion-extra-properties :annotation-function)
  755. (completion-metadata-get metadata 'annotation-function)))
  756. (file-comp-p (eq (completion-metadata-get metadata 'category) 'file))
  757. (compfn (lambda (str _predicate _action)
  758. (let* ((comps
  759. (completion-all-completions
  760. str ; This is helm-pattern
  761. collection
  762. predicate
  763. (length str)
  764. metadata))
  765. (last-data (last comps))
  766. ;; Helm syle sort fn is added to
  767. ;; metadata only in emacs-27, so in
  768. ;; emacs-26 use helm-generic-sort-fn
  769. ;; which handle both helm and
  770. ;; helm-flex styles. When
  771. ;; helm-completion-style is helm or
  772. ;; helm-fuzzy, sorting will be done
  773. ;; later in FCT.
  774. (sort-fn
  775. (and (eq helm-completion-style 'emacs)
  776. (or
  777. ;; Emacs-27
  778. (completion-metadata-get
  779. metadata 'display-sort-function)
  780. ;; Emacs-26
  781. (lambda (candidates)
  782. (sort candidates #'helm-generic-sort-fn)))))
  783. all)
  784. (when (cdr last-data)
  785. ;; Remove the last element of
  786. ;; comps by side-effect.
  787. (setcdr last-data nil))
  788. (setq helm-completion--sorting-done (and sort-fn t))
  789. (setq all (copy-sequence comps))
  790. ;; Fall back to string-lessp sorting when
  791. ;; str is too small as specialized
  792. ;; sorting may be too slow (flex).
  793. (when (and sort-fn (<= (length str) 1))
  794. (setq sort-fn (lambda (all) (sort all #'string-lessp))))
  795. ;; Default is passed here only with helm
  796. ;; h-c-styles, otherwise with emacs style it is
  797. ;; passed with the :default arg of helm-comp-read
  798. ;; and computed in its get-candidates function.
  799. (append (and default
  800. (memq helm-completion-style '(helm helm-fuzzy))
  801. (list default))
  802. (helm-completion-in-region--initial-filter
  803. (if sort-fn (funcall sort-fn all) all)
  804. afun file-comp-p)))))
  805. (data (if (memq helm-completion-style '(helm helm-fuzzy))
  806. (funcall compfn (or input "") nil nil)
  807. compfn))
  808. (helm-completion-in-region-default-sort-fn
  809. (lambda (candidates _source)
  810. (if (or helm-completion--sorting-done
  811. (string= helm-pattern ""))
  812. candidates
  813. (sort candidates 'helm-generic-sort-fn)))))
  814. (unwind-protect
  815. (helm-comp-read
  816. ;; Completion-at-point and friends have no prompt.
  817. prompt
  818. data
  819. :name name
  820. :initial-input input
  821. :buffer buffer
  822. :history history
  823. :reverse-history helm-mode-reverse-history
  824. ;; In helm h-c-styles default is passed directly in
  825. ;; candidates.
  826. :default (and (eq helm-completion-style 'emacs) default)
  827. :fc-transformer
  828. ;; Ensure sort fn is at the end.
  829. (append '(helm-cr-default-transformer)
  830. (and helm-completion-in-region-default-sort-fn
  831. (list helm-completion-in-region-default-sort-fn)))
  832. :match-dynamic (eq helm-completion-style 'emacs)
  833. :fuzzy (eq helm-completion-style 'helm-fuzzy)
  834. :exec-when-only-one exec-when-only-one
  835. :must-match require-match)
  836. (setq helm-completion--sorting-done nil))))
  837. (defun helm-completing-read-default-find-tag
  838. (prompt collection test require-match
  839. init hist default inherit-input-method
  840. name buffer)
  841. "Specialized `helm-mode' handler for `find-tag'."
  842. ;; Some commands like find-tag may use `read-file-name' from inside
  843. ;; the calculation of collection. in this case it clash with
  844. ;; candidates-in-buffer that reuse precedent data (files) which is wrong.
  845. ;; So (re)calculate collection outside of main helm-session.
  846. (let* ((cands (helm-comp-read-get-candidates
  847. collection test nil nil)))
  848. (helm-completing-read-default-1 prompt cands test require-match
  849. init hist default inherit-input-method
  850. name buffer t)))
  851. (defun helm-completing-read-sync-default-handler
  852. (prompt collection test require-match
  853. init hist default inherit-input-method
  854. name buffer)
  855. "`helm-mode' handler using sync source as backend."
  856. (helm-completing-read-default-1 prompt collection test require-match
  857. init hist default inherit-input-method
  858. name buffer))
  859. (defun helm-completing-read-default-handler
  860. (prompt collection test require-match
  861. init hist default inherit-input-method
  862. name buffer)
  863. "Default `helm-mode' handler for all `completing-read'."
  864. (helm-completing-read-default-2 prompt collection test require-match
  865. init hist default inherit-input-method
  866. name buffer))
  867. (defun helm--generic-read-buffer (prompt &optional default require-match predicate)
  868. "The `read-buffer-function' for `helm-mode'.
  869. Affects `switch-to-buffer' and related."
  870. (let ((collection (helm-buffer-list)))
  871. (helm--completing-read-default
  872. prompt collection predicate require-match nil nil default)))
  873. (cl-defun helm--completing-read-default
  874. (prompt collection &optional
  875. predicate require-match
  876. initial-input hist def
  877. inherit-input-method)
  878. "An helm replacement of `completing-read'.
  879. This function should be used only as a `completing-read-function'.
  880. Don't use it directly, use instead `helm-comp-read' in your programs.
  881. See documentation of `completing-read' and `all-completions' for details."
  882. (let* ((current-command (or (helm-this-command) this-command))
  883. (str-command (helm-symbol-name current-command))
  884. (buf-name (format "*helm-mode-%s*" str-command))
  885. (entry (assq current-command
  886. helm-completing-read-handlers-alist))
  887. (def-com (cdr-safe entry))
  888. (str-defcom (and def-com (helm-symbol-name def-com)))
  889. (def-args (list prompt collection predicate require-match
  890. initial-input hist def inherit-input-method))
  891. ;; Append the two extra args needed to set the buffer and source name
  892. ;; in helm specialized functions.
  893. (any-args (append def-args (list str-command buf-name)))
  894. helm-completion-mode-start-message ; Be quiet
  895. helm-completion-mode-quit-message
  896. ;; Be sure this pesty *completion* buffer doesn't popup.
  897. ;; Note: `minibuffer-with-setup-hook' may setup a lambda
  898. ;; calling `minibuffer-completion-help' or other minibuffer
  899. ;; functions we DONT WANT here, in these cases removing the hook
  900. ;; (a symbol) have no effect. Issue #448.
  901. ;; Because `minibuffer-completion-table' and
  902. ;; `minibuffer-completion-predicate' are not bound
  903. ;; anymore here, these functions should have no effect now,
  904. ;; except in some rare cases like in `woman-file-name',
  905. ;; so remove all incompatible functions
  906. ;; from `minibuffer-setup-hook' (Issue #1205, #1240).
  907. ;; otherwise helm have not the time to close its initial session.
  908. (minibuffer-setup-hook
  909. (cl-loop for h in minibuffer-setup-hook
  910. unless (or (consp h) ; a lambda.
  911. (byte-code-function-p h)
  912. (memq h helm-mode-minibuffer-setup-hook-black-list))
  913. collect h))
  914. ;; Disable hack that could be used before `completing-read'.
  915. ;; i.e (push ?\t unread-command-events).
  916. unread-command-events
  917. (default-handler
  918. ;; If nothing is found in
  919. ;; helm-completing-read-handlers-alist use default
  920. ;; handler.
  921. #'helm-completing-read-default-handler))
  922. (when (eq def-com 'ido) (setq def-com 'ido-completing-read))
  923. (unless (or (not entry) def-com)
  924. ;; An entry in *read-handlers-alist exists but have
  925. ;; a nil value, so we exit from here, disable `helm-mode'
  926. ;; and run the command again with it original behavior.
  927. ;; `helm-mode' will be restored on exit.
  928. (cl-return-from helm--completing-read-default
  929. (unwind-protect
  930. (progn
  931. (helm-mode -1)
  932. (apply completing-read-function def-args))
  933. (helm-mode 1))))
  934. ;; If we use now `completing-read' we MUST turn off `helm-mode'
  935. ;; to avoid infinite recursion and CRASH. It will be reenabled on exit.
  936. (when (or (eq def-com 'completing-read)
  937. ;; All specialized functions are prefixed by "helm"
  938. (and (stringp str-defcom)
  939. (not (string-match "^helm" str-defcom))))
  940. (helm-mode -1))
  941. (unwind-protect
  942. (cond (;; An helm specialized function exists, run it.
  943. (and def-com helm-mode)
  944. (apply def-com any-args))
  945. (;; Try to handle `ido-completing-read' everywhere.
  946. (and def-com (eq def-com 'ido-completing-read))
  947. (setcar (memq collection def-args)
  948. (all-completions "" collection predicate))
  949. (apply def-com def-args))
  950. (;; User set explicitely `completing-read' or something similar
  951. ;; in *read-handlers-alist, use this with exactly the same
  952. ;; args as in `completing-read'.
  953. ;; If we are here `helm-mode' is now disabled.
  954. def-com
  955. (apply def-com def-args))
  956. (;; Use by default a in-buffer handler unless
  957. ;; COLLECTION is a function.
  958. t
  959. (funcall default-handler
  960. prompt collection predicate require-match
  961. initial-input hist def inherit-input-method
  962. str-command buf-name)))
  963. (helm-mode 1)
  964. ;; When exiting minibuffer, `this-command' is set to
  965. ;; `helm-exit-minibuffer', which is unwanted when starting
  966. ;; on another `completing-read', so restore `this-command' to
  967. ;; initial value when exiting.
  968. (setq this-command current-command))))
  969. ;;; Generic read-file-name
  970. ;;
  971. ;;
  972. ;;;###autoload
  973. (cl-defun helm-read-file-name
  974. (prompt
  975. &key
  976. (name "Read File Name")
  977. (initial-input default-directory)
  978. (buffer "*Helm file completions*")
  979. test
  980. noret
  981. (case-fold helm-file-name-case-fold-search)
  982. preselect
  983. history
  984. must-match
  985. (fuzzy t)
  986. default
  987. marked-candidates
  988. (candidate-number-limit helm-ff-candidate-number-limit)
  989. nomark
  990. (alistp t)
  991. (persistent-action-if 'helm-find-files-persistent-action-if)
  992. (persistent-help "Hit1 Expand Candidate, Hit2 or (C-u) Find file")
  993. (mode-line helm-read-file-name-mode-line-string))
  994. "Read a file name with helm completion.
  995. It is helm `read-file-name' emulation.
  996. Argument PROMPT is the default prompt to use.
  997. Keys description:
  998. - NAME: Source name, default to \"Read File Name\".
  999. - INITIAL-INPUT: Where to start read file name, default to `default-directory'.
  1000. - BUFFER: `helm-buffer' name default to \"*Helm Completions*\".
  1001. - TEST: A predicate called with one arg 'candidate'.
  1002. - NORET: Allow disabling helm-ff-RET (have no effect if helm-ff-RET
  1003. isn't bound to RET).
  1004. - CASE-FOLD: Same as `helm-case-fold-search'.
  1005. - PRESELECT: helm preselection.
  1006. - HISTORY: Display HISTORY in a special source.
  1007. - MUST-MATCH: Can be 'confirm, nil, or t.
  1008. - FUZZY: Enable fuzzy matching when non-nil (Enabled by default).
  1009. - MARKED-CANDIDATES: When non--nil return a list of marked candidates.
  1010. - NOMARK: When non--nil don't allow marking candidates.
  1011. - ALISTP: Don't use `all-completions' in history (take effect only on history).
  1012. - PERSISTENT-ACTION-IF: a persistent if action function.
  1013. - PERSISTENT-HELP: persistent help message.
  1014. - MODE-LINE: A mode line message, default is `helm-read-file-name-mode-line-string'."
  1015. (require 'tramp)
  1016. (when (get-buffer helm-action-buffer)
  1017. (kill-buffer helm-action-buffer))
  1018. (mapc (lambda (hook)
  1019. (add-hook 'helm-after-update-hook hook))
  1020. '(helm-ff-move-to-first-real-candidate
  1021. helm-ff-update-when-only-one-matched
  1022. helm-ff-auto-expand-to-home-or-root))
  1023. (let* ((action-fn `(("Sole action (Identity)"
  1024. . (lambda (candidate)
  1025. (if ,marked-candidates
  1026. (helm-marked-candidates :with-wildcard t)
  1027. (identity candidate))))))
  1028. ;; Be sure we don't erase the underlying minibuffer if some.
  1029. (helm-ff-auto-update-initial-value
  1030. (and helm-ff-auto-update-initial-value
  1031. (not (minibuffer-window-active-p (minibuffer-window)))))
  1032. helm-follow-mode-persistent
  1033. (helm-ff-fuzzy-matching
  1034. (and fuzzy
  1035. (not (memq helm-mm-matching-method '(multi1 multi3p)))))
  1036. (hist (and history (helm-comp-read-get-candidates
  1037. history nil nil alistp)))
  1038. (minibuffer-completion-confirm must-match)
  1039. (helm-ff--RET-disabled noret)
  1040. (minibuffer-completion-predicate test)
  1041. (minibuffer-completing-file-name t)
  1042. (helm--completing-file-name t)
  1043. (helm-read-file-name-mode-line-string
  1044. (replace-regexp-in-string "helm-maybe-exit-minibuffer"
  1045. "helm-confirm-and-exit-minibuffer"
  1046. helm-read-file-name-mode-line-string))
  1047. (src-list
  1048. (list
  1049. ;; History source.
  1050. (helm-build-sync-source (format "%s History" name)
  1051. :header-name (lambda (name)
  1052. (concat name (substitute-command-keys
  1053. helm-find-files-doc-header)))
  1054. :mode-line mode-line
  1055. :candidates hist
  1056. :nohighlight t
  1057. :fuzzy-match fuzzy
  1058. :persistent-action-if persistent-action-if
  1059. :persistent-help persistent-help
  1060. :keymap helm-read-file-map
  1061. :must-match must-match
  1062. :nomark nomark
  1063. :action action-fn)
  1064. ;; Other source.
  1065. (helm-build-sync-source name
  1066. :header-name (lambda (name)
  1067. (concat name (substitute-command-keys
  1068. helm-find-files-doc-header)))
  1069. :init (lambda ()
  1070. (setq helm-ff-auto-update-flag
  1071. helm-ff-auto-update-initial-value)
  1072. (setq helm-ff--auto-update-state
  1073. helm-ff-auto-update-flag))
  1074. :mode-line mode-line
  1075. :help-message 'helm-read-file-name-help-message
  1076. :nohighlight t
  1077. :candidates
  1078. (lambda ()
  1079. (append (and (not (file-exists-p helm-pattern))
  1080. (not (helm-ff--invalid-tramp-name-p helm-pattern))
  1081. (list helm-pattern))
  1082. (if test
  1083. (cl-loop with hn = (helm-ff--tramp-hostnames)
  1084. for i in (helm-find-files-get-candidates
  1085. must-match)
  1086. when (or (member i hn) ; A tramp host
  1087. (funcall test i)) ; Test ok
  1088. collect i)
  1089. (helm-find-files-get-candidates must-match))))
  1090. :filtered-candidate-transformer 'helm-ff-sort-candidates
  1091. :filter-one-by-one 'helm-ff-filter-candidate-one-by-one
  1092. :persistent-action-if persistent-action-if
  1093. :persistent-help persistent-help
  1094. :volatile t
  1095. :keymap helm-read-file-map
  1096. :must-match must-match
  1097. :cleanup 'helm-find-files-cleanup
  1098. :nomark nomark
  1099. :action action-fn)))
  1100. ;; Helm result.
  1101. (result (helm
  1102. :sources (if helm-mode-reverse-history
  1103. (reverse src-list) src-list)
  1104. :input (expand-file-name initial-input)
  1105. :prompt prompt
  1106. :candidate-number-limit candidate-number-limit
  1107. :resume 'noresume
  1108. :case-fold-search case-fold
  1109. :default default
  1110. :buffer buffer
  1111. :full-frame nil
  1112. :preselect preselect)))
  1113. (or
  1114. (cond ((and result (stringp result)
  1115. (string= result "") ""))
  1116. ((and result
  1117. (stringp result)
  1118. (file-equal-p result initial-input)
  1119. default)
  1120. (if (listp default) (car default) default))
  1121. ((and result (listp result))
  1122. (mapcar #'expand-file-name result))
  1123. ((and result (file-directory-p result))
  1124. (file-name-as-directory (expand-file-name result)))
  1125. (result (expand-file-name result)))
  1126. (helm-mode--keyboard-quit))))
  1127. (defun helm-mode--default-filename (fname dir initial)
  1128. (unless dir (setq dir default-directory))
  1129. (unless (file-name-absolute-p dir)
  1130. (setq dir (expand-file-name dir)))
  1131. (unless (or fname (consp fname))
  1132. (setq fname (expand-file-name
  1133. (or initial buffer-file-name dir)
  1134. dir)))
  1135. (if (and fname (consp fname))
  1136. (setq fname (cl-loop for f in fname
  1137. collect (expand-file-name f dir)))
  1138. (if (file-name-absolute-p fname)
  1139. fname (expand-file-name fname dir))))
  1140. (cl-defun helm--generic-read-file-name
  1141. (prompt &optional dir default-filename mustmatch initial predicate)
  1142. "Generic helm replacement of `read-file-name'.
  1143. Don't use it directly, use instead `helm-read-file-name' in your programs."
  1144. (let* ((init (or initial dir default-directory))
  1145. (current-command (or (helm-this-command) this-command))
  1146. (str-command (helm-symbol-name current-command))
  1147. (helm--file-completion-sources
  1148. (cons str-command
  1149. (remove str-command helm--file-completion-sources)))
  1150. (buf-name (format "*helm-mode-%s*" str-command))
  1151. (entry (assq current-command
  1152. helm-completing-read-handlers-alist))
  1153. (def-com (cdr-safe entry))
  1154. (str-defcom (and def-com (helm-symbol-name def-com)))
  1155. ;; Don't modify the original args list for emacs generic functions.
  1156. (def-args (list prompt dir default-filename mustmatch initial predicate))
  1157. ;; Append the two extra args needed to set the buffer and source name
  1158. ;; in helm specialized functions.
  1159. (any-args (append def-args (list str-command buf-name)))
  1160. (reading-directory (eq predicate 'file-directory-p))
  1161. helm-completion-mode-start-message ; Be quiet
  1162. helm-completion-mode-quit-message ; Same here
  1163. fname)
  1164. ;; Build `default-filename' with `dir'+`initial' when
  1165. ;; `default-filename' is not specified.
  1166. ;; See `read-file-name' docstring for more infos.
  1167. (setq default-filename (helm-mode--default-filename
  1168. default-filename dir initial))
  1169. ;; Some functions that normally call `completing-read' can switch
  1170. ;; brutally to `read-file-name' (e.g find-tag), in this case
  1171. ;; the helm specialized function will fail because it is build
  1172. ;; for `completing-read', so set it to 'incompatible to be sure
  1173. ;; we switch to `helm-read-file-name' and don't try to call it
  1174. ;; with wrong number of args.
  1175. (when (eq def-com 'ido)
  1176. (setq def-com 'ido-read-file-name))
  1177. (when (and def-com (> (length (help-function-arglist def-com)) 8))
  1178. (setq def-com 'incompatible))
  1179. (unless (or (not entry) def-com)
  1180. (cl-return-from helm--generic-read-file-name
  1181. (unwind-protect
  1182. (progn
  1183. (helm-mode -1)
  1184. (apply read-file-name-function def-args))
  1185. (helm-mode 1))))
  1186. ;; If we use now `read-file-name' we MUST turn off `helm-mode'
  1187. ;; to avoid infinite recursion and CRASH. It will be reenabled on exit.
  1188. (when (or (eq def-com 'read-file-name)
  1189. (eq def-com 'ido-read-file-name)
  1190. (and (stringp str-defcom)
  1191. (not (string-match "^helm" str-defcom))))
  1192. (helm-mode -1))
  1193. (unwind-protect
  1194. (setq fname
  1195. (cond (;; A specialized function exists, run it
  1196. ;; with the two extra args specific to helm.
  1197. ;; Note that the helm handler should ensure
  1198. ;; :initial-input is not nil i.e. Use init
  1199. ;; which fallback to default-directory instead
  1200. ;; of INITIAL.
  1201. (and def-com helm-mode
  1202. (not (eq def-com 'ido-read-file-name))
  1203. (not (eq def-com 'incompatible)))
  1204. (apply def-com any-args))
  1205. (;; Def-com value is `ido-read-file-name'
  1206. ;; run it with default args.
  1207. (and def-com (eq def-com 'ido-read-file-name))
  1208. (ido-mode 1)
  1209. (apply def-com def-args))
  1210. (;; Def-com value is `read-file-name'
  1211. ;; run it with default args.
  1212. (eq def-com 'read-file-name)
  1213. (apply def-com def-args))
  1214. (t ; Fall back to classic `helm-read-file-name'.
  1215. (helm-read-file-name
  1216. prompt
  1217. :name str-command
  1218. :buffer buf-name
  1219. :default default-filename
  1220. ;; Helm handlers should always have a non nil INITIAL arg.
  1221. :initial-input (expand-file-name init dir)
  1222. :alistp nil
  1223. :must-match mustmatch
  1224. :test predicate
  1225. :noret reading-directory))))
  1226. (and ido-mode (ido-mode -1))
  1227. (helm-mode 1)
  1228. ;; Same comment as in `helm--completing-read-default'.
  1229. (setq this-command current-command))
  1230. (if (and
  1231. ;; Using `read-directory-name'.
  1232. reading-directory
  1233. ;; `file-name-as-directory' return "./" when FNAME is
  1234. ;; empty string.
  1235. (not (string= fname "")))
  1236. (file-name-as-directory fname) fname)))
  1237. ;; Read file name handler with history (issue #1652)
  1238. (defun helm-read-file-name-handler-1 (prompt dir default-filename
  1239. mustmatch initial predicate
  1240. name buffer)
  1241. "A `read-file-name' handler with history.
  1242. Can be added to `helm-completing-read-handlers-alist' for functions
  1243. that need a `read-file-name' function with directory history.
  1244. The `helm-find-files' history `helm-ff-history' is used here."
  1245. (let ((helm-always-two-windows t)
  1246. (helm-split-window-default-side
  1247. (if (eq helm-split-window-default-side 'same)
  1248. 'below helm-split-window-default-side))
  1249. helm-split-window-inside-p
  1250. helm-reuse-last-window-split-state
  1251. ;; Helm handlers should always have a non nil INITIAL arg.
  1252. (init (or initial dir default-directory)))
  1253. (helm-read-file-name
  1254. prompt
  1255. :name name
  1256. :history helm-ff-history
  1257. :buffer buffer
  1258. :default default-filename
  1259. :initial-input (expand-file-name init dir)
  1260. :alistp nil
  1261. :must-match mustmatch
  1262. :test predicate)))
  1263. ;;; Completion in region and Helm style
  1264. ;;
  1265. (defun helm-mode--advice-lisp--local-variables (old--fn &rest args)
  1266. (ignore-errors
  1267. (apply old--fn args)))
  1268. (defvar helm-completion--sorting-done nil
  1269. "Flag that notify the FCT if sorting have been done in completion function.")
  1270. (defun helm-completion-in-region-sort-fn (candidates _source)
  1271. "Default sort function for completion-in-region."
  1272. (if helm-completion--sorting-done
  1273. candidates
  1274. (sort candidates 'helm-generic-sort-fn)))
  1275. (defun helm-mode--completion-in-region-initial-input (str)
  1276. "Highlight prefix in helm and helm-fuzzy `helm-completion-styles'."
  1277. (if (memq helm-completion-style '(helm helm-fuzzy))
  1278. (propertize str 'read-only t 'face 'helm-mode-prefix 'rear-nonsticky t)
  1279. str))
  1280. (defun helm-completion-in-region--initial-filter (comps afun file-comp-p)
  1281. "Add annotations at end of candidates and filter out dot files."
  1282. (if file-comp-p
  1283. ;; Filter out dot files in file completion.
  1284. (cl-loop for f in comps unless
  1285. (string-match "\\`\\.\\{1,2\\}/\\'" f)
  1286. collect f)
  1287. (if afun
  1288. ;; Add annotation at end of
  1289. ;; candidate if needed, e.g. foo<f>, this happen when
  1290. ;; completing against a quoted symbol.
  1291. (mapcar (lambda (s)
  1292. (let ((ann (funcall afun s)))
  1293. (if ann
  1294. (cons
  1295. (concat
  1296. s
  1297. (propertize
  1298. " " 'display
  1299. (propertize
  1300. ann
  1301. 'face 'completions-annotations)))
  1302. s)
  1303. s)))
  1304. comps)
  1305. comps)))
  1306. ;; Helm multi matching style
  1307. (defun helm-completion-try-completion (string table pred point)
  1308. "The try completion function for `completing-styles-alist'.
  1309. Actually do nothing."
  1310. ;; AFAIU the try completion function is here to handle single
  1311. ;; element completion, in this case it throw this element without
  1312. ;; popping up *completions* buffer. If that's the case we don't need
  1313. ;; this because helm already handle this with
  1314. ;; `helm-execute-action-at-once-if-one', so returning unconditionaly
  1315. ;; nil should be fine.
  1316. (ignore string table pred point))
  1317. (defun helm-completion-all-completions (string table pred point)
  1318. "The all completions function for `completing-styles-alist'."
  1319. ;; FIXME: No need to bind all these value.
  1320. (cl-multiple-value-bind (all _pattern prefix _suffix _carbounds)
  1321. (helm-completion--multi-all-completions string table pred point)
  1322. (when all (nconc all (length prefix)))))
  1323. (defun helm-completion--multi-all-completions-1 (string collection &optional predicate)
  1324. "Allow `all-completions' multi matching on its candidates."
  1325. (all-completions "" collection (lambda (x &optional _y)
  1326. ;; Second arg _y is needed when
  1327. ;; COLLECTION is a hash-table issue
  1328. ;; #2231 (C-x 8 RET).
  1329. ;; Elements of collection may be
  1330. ;; lists or alists, in this case consider the
  1331. ;; car of element issue #2219 (org-refile).
  1332. (let ((elm (if (listp x) (car x) x)))
  1333. (if predicate
  1334. (and (funcall predicate elm)
  1335. (helm-mm-match (helm-stringify elm) string))
  1336. (helm-mm-match (helm-stringify elm) string))))))
  1337. (defun helm-completion--multi-all-completions (string table pred point)
  1338. "Collect completions from TABLE for helm completion style."
  1339. (let* ((beforepoint (substring string 0 point))
  1340. (afterpoint (substring string point))
  1341. (bounds (completion-boundaries beforepoint table pred afterpoint))
  1342. (prefix (substring beforepoint 0 (car bounds)))
  1343. (suffix (substring afterpoint (cdr bounds)))
  1344. (all (helm-completion--multi-all-completions-1 string table pred)))
  1345. (list all string prefix suffix point)))
  1346. ;; The adjust-metadata functions run only in emacs-27, they are NOT
  1347. ;; used otherwise.
  1348. (defun helm-completion--adjust-metadata (metadata)
  1349. (if (memq helm-completion-style '(helm helm-fuzzy))
  1350. metadata
  1351. (let ((compose-helm-sort-fn
  1352. (lambda (candidates)
  1353. (sort candidates #'helm-generic-sort-fn))))
  1354. `(metadata
  1355. (display-sort-function
  1356. . ,compose-helm-sort-fn)
  1357. (cycle-sort-function
  1358. . ,compose-helm-sort-fn)
  1359. ,@(cdr metadata)))))
  1360. (put 'helm 'completion--adjust-metadata 'helm-completion--adjust-metadata)
  1361. ;; Helm-flex style.
  1362. (defun helm-flex-completion-try-completion (string table pred point)
  1363. "The try completion function for `completing-styles-alist'.
  1364. Actually do nothing."
  1365. ;; AFAIU the try completion function is here to handle single
  1366. ;; element completion, in this case it throw this element without
  1367. ;; popping up *completions* buffer. If that's the case we don't need
  1368. ;; this because helm already handle this with
  1369. ;; `helm-execute-action-at-once-if-one', so returning unconditionaly
  1370. ;; nil should be fine.
  1371. (ignore string table pred point))
  1372. (defun helm-flex-completion-all-completions (string table pred point)
  1373. "The all completions function for `completing-styles-alist'."
  1374. ;; FIXME: No need to bind all these value.
  1375. (cl-multiple-value-bind (all pattern prefix _suffix _carbounds)
  1376. (helm-completion--flex-all-completions string table pred point)
  1377. (let ((regexp (completion-pcm--pattern->regex pattern 'group)))
  1378. (when all (nconc (helm-flex-add-score-as-prop all regexp)
  1379. (length prefix))))))
  1380. (defun helm-flex-add-score-as-prop (candidates regexp)
  1381. (cl-loop for cand in candidates
  1382. collect (helm-flex--style-score cand regexp)))
  1383. (defun helm-completion--flex-all-completions-1 (_string collection &optional predicate)
  1384. "Allow `all-completions' multi matching on its candidates."
  1385. (all-completions "" collection (lambda (x &optional _y)
  1386. ;; Elements of collection may be
  1387. ;; lists, in this case consider the
  1388. ;; car of element #2219.
  1389. (let ((elm (if (listp x) (car x) x)))
  1390. (if predicate
  1391. (and (funcall predicate elm)
  1392. (helm-flex-style-match (helm-stringify elm)))
  1393. (helm-flex-style-match (helm-stringify elm)))))))
  1394. (defun helm-completion--flex-transform-pattern (pattern)
  1395. ;; "fob" => '(prefix "f" any "o" any "b" any point)
  1396. (cl-loop for p in pattern
  1397. if (stringp p) nconc
  1398. (cl-loop for str across p
  1399. nconc (list (string str) 'any))
  1400. else nconc (list p)))
  1401. (defun helm-completion--flex-all-completions (string table pred point)
  1402. "Collect completions from TABLE for helm completion style."
  1403. (let* ((beforepoint (substring string 0 point))
  1404. (afterpoint (substring string point))
  1405. (bounds (completion-boundaries beforepoint table pred afterpoint))
  1406. (prefix (substring beforepoint 0 (car bounds)))
  1407. (suffix (substring afterpoint (cdr bounds)))
  1408. (basic-pattern (completion-basic--pattern
  1409. beforepoint afterpoint bounds))
  1410. (pattern (if (not (stringp (car basic-pattern)))
  1411. basic-pattern
  1412. (cons 'prefix basic-pattern)))
  1413. (pattern (helm-completion--flex-transform-pattern pattern))
  1414. (all (helm-completion--flex-all-completions-1 string table pred)))
  1415. (list all pattern prefix suffix point)))
  1416. ;; Completion-in-region-function
  1417. (defun helm--completion-in-region (start end collection &optional predicate)
  1418. "Helm replacement of `completion--in-region'."
  1419. (cl-declare (special require-match prompt))
  1420. (advice-add
  1421. 'lisp--local-variables
  1422. :around #'helm-mode--advice-lisp--local-variables)
  1423. (let ((old--helm-completion-style helm-completion-style))
  1424. (helm-aif (cdr (assq major-mode helm-completion-styles-alist))
  1425. (customize-set-variable 'helm-completion-style
  1426. (if (cdr-safe it) (car it) it)))
  1427. (unwind-protect
  1428. (let* ((enable-recursive-minibuffers t)
  1429. (completion-flex-nospace t)
  1430. (completion-styles (helm--prepare-completion-styles))
  1431. (input (buffer-substring-no-properties start end))
  1432. ;; Always start with prefix to allow completing without
  1433. ;; the need of inserting a space after cursor or
  1434. ;; relaying on crap old completion-styles emacs22 which
  1435. ;; add suffix after prefix. e.g. def|else.
  1436. (initial-input (buffer-substring-no-properties start (point)))
  1437. (prefix (and (eq helm-completion-style 'emacs) initial-input))
  1438. (point (point))
  1439. (current-command (or (helm-this-command) this-command))
  1440. (crm (eq current-command 'crm-complete))
  1441. (str-command (helm-symbol-name current-command))
  1442. (buf-name (format "*helm-mode-%s*" str-command))
  1443. (require-match (or (and (boundp 'require-match) require-match)
  1444. minibuffer-completion-confirm
  1445. ;; If prompt have not been propagated here, that's
  1446. ;; probably mean we have no prompt and we are in
  1447. ;; completion-at-point or friend, so use a non--nil
  1448. ;; value for require-match.
  1449. (not (boundp 'prompt))))
  1450. (metadata (completion-metadata input collection predicate))
  1451. ;; `completion-extra-properties' is let-bounded in `completion-at-point'.
  1452. ;; `afun' is a closure to call against each string in `data'.
  1453. ;; it provide the annotation info for each string.
  1454. ;; e.g "foo" => "foo <f>" where foo is a function.
  1455. ;; See Issue #407.
  1456. (afun (or (plist-get completion-extra-properties :annotation-function)
  1457. (completion-metadata-get metadata 'annotation-function)))
  1458. (init-space-suffix (unless (or (memq helm-completion-style '(helm-fuzzy emacs))
  1459. (string-suffix-p " " input)
  1460. (string= input ""))
  1461. " "))
  1462. (file-comp-p (or (eq (completion-metadata-get metadata 'category) 'file)
  1463. (helm-mode--in-file-completion-p)
  1464. ;; Assume that when `afun' and `predicate' are null
  1465. ;; we are in filename completion.
  1466. (and (null afun) (null predicate))))
  1467. ;; `completion-all-completions' store the base-size in the last `cdr',
  1468. ;; so data looks like this: '(a b c d . 0) and (last data) == (d . 0).
  1469. base-size
  1470. (compfn (lambda (str _predicate _action)
  1471. (let* ((comps
  1472. (completion-all-completions
  1473. str ; This is helm-pattern
  1474. collection
  1475. predicate
  1476. ;; Use prefix length at first call to
  1477. ;; allow styles matching
  1478. ;; "prefix*suffix" to kick in.
  1479. (length (or prefix str))
  1480. metadata))
  1481. (last-data (last comps))
  1482. (bs (helm-aif (cdr last-data)
  1483. (prog1 it
  1484. ;; Remove the last element of
  1485. ;; comps by side-effect.
  1486. (setcdr last-data nil))
  1487. 0))
  1488. ;; Helm syle sort fn is added to
  1489. ;; metadata only in emacs-27, so in
  1490. ;; emacs-26 use helm-generic-sort-fn
  1491. ;; which handle both helm and
  1492. ;; helm-flex styles. When
  1493. ;; helm-completion-style is helm or
  1494. ;; helm-fuzzy, sorting will be done
  1495. ;; later in FCT.
  1496. (sort-fn
  1497. (and (eq helm-completion-style 'emacs)
  1498. (or
  1499. ;; Emacs-27
  1500. (completion-metadata-get
  1501. metadata 'display-sort-function)
  1502. ;; Emacs-26
  1503. (lambda (candidates)
  1504. (sort candidates #'helm-generic-sort-fn)))))
  1505. all)
  1506. ;; Reset prefix to allow using length of
  1507. ;; helm-pattern on next calls (this avoid
  1508. ;; args-out-of-range error).
  1509. (and prefix (setq prefix nil))
  1510. ;; base-size needs to be set only once at
  1511. ;; first call.
  1512. (unless base-size (setq base-size bs))
  1513. (setq helm-completion--sorting-done (and sort-fn t))
  1514. (setq all (copy-sequence comps))
  1515. ;; Fall back to string-lessp sorting when
  1516. ;; str is too small as specialized
  1517. ;; sorting may be too slow (flex).
  1518. (when (and sort-fn (<= (length str) 1))
  1519. (setq sort-fn (lambda (all) (sort all #'string-lessp))))
  1520. (helm-completion-in-region--initial-filter
  1521. (if sort-fn (funcall sort-fn all) all)
  1522. afun file-comp-p))))
  1523. (data (if (memq helm-completion-style '(helm helm-fuzzy))
  1524. (funcall compfn input nil nil)
  1525. compfn))
  1526. (result (if (stringp data)
  1527. data
  1528. (helm-comp-read
  1529. ;; Completion-at-point and friends have no prompt.
  1530. (or (and (boundp 'prompt) prompt) "Pattern: ")
  1531. data
  1532. :name str-command
  1533. :nomark (null crm)
  1534. :marked-candidates crm
  1535. :initial-input
  1536. (cond ((and file-comp-p
  1537. (not (string-match "/\\'" initial-input)))
  1538. (concat (helm-mode--completion-in-region-initial-input
  1539. (if (memq helm-completion-style '(helm helm-fuzzy))
  1540. (helm-basename initial-input)
  1541. initial-input))
  1542. init-space-suffix))
  1543. ((string-match "/\\'" initial-input)
  1544. (and (eq helm-completion-style 'emacs) initial-input))
  1545. ((or (null require-match)
  1546. (stringp require-match))
  1547. (helm-mode--completion-in-region-initial-input initial-input))
  1548. (t (concat (helm-mode--completion-in-region-initial-input initial-input)
  1549. init-space-suffix)))
  1550. :buffer buf-name
  1551. :fc-transformer
  1552. ;; Ensure sort fn is at the end.
  1553. (append '(helm-cr-default-transformer)
  1554. (and helm-completion-in-region-default-sort-fn
  1555. (list helm-completion-in-region-default-sort-fn)))
  1556. :match-dynamic (eq helm-completion-style 'emacs)
  1557. :fuzzy (eq helm-completion-style 'helm-fuzzy)
  1558. :exec-when-only-one t
  1559. :quit-when-no-cand
  1560. (lambda ()
  1561. ;; Delay message to overwrite "Quit".
  1562. (run-with-timer
  1563. 0.01 nil
  1564. (lambda ()
  1565. (message "[No matches]")))
  1566. t) ; exit minibuffer immediately.
  1567. :must-match require-match))))
  1568. (helm-completion-in-region--insert-result result start point end base-size))
  1569. (customize-set-variable 'helm-completion-style old--helm-completion-style)
  1570. (setq helm-completion--sorting-done nil)
  1571. (advice-remove 'lisp--local-variables
  1572. #'helm-mode--advice-lisp--local-variables))))
  1573. (defun helm-completion-in-region--insert-result (result start point end base-size)
  1574. (cond ((stringp result)
  1575. (choose-completion-string
  1576. result (current-buffer)
  1577. (list (+ start base-size) point)
  1578. completion-list-insert-choice-function)
  1579. (when helm-completion-mark-suffix
  1580. (run-with-idle-timer 0.01 nil
  1581. (lambda ()
  1582. (helm-aand
  1583. (+ (- (point) point) end)
  1584. (and (> it (point)) it)
  1585. (push-mark it t t))))))
  1586. ((consp result) ; crm.
  1587. (let ((beg (+ start base-size))
  1588. (sep ","))
  1589. ;; Try to find a default separator.
  1590. (save-excursion
  1591. (goto-char beg)
  1592. (when (looking-back crm-separator (1- (point)))
  1593. (setq sep (match-string 0))))
  1594. (funcall completion-list-insert-choice-function
  1595. beg end (mapconcat 'identity result sep))))
  1596. (t nil)))
  1597. (defun helm-mode--in-file-completion-p ()
  1598. (with-helm-current-buffer
  1599. (run-hook-with-args-until-success 'file-name-at-point-functions)))
  1600. (defun helm-mode--disable-ido-maybe (&optional from-hook)
  1601. (when (and (boundp 'ido-everywhere) ido-everywhere)
  1602. (remove-function read-file-name-function #'ido-read-file-name)
  1603. (remove-function read-buffer-function #'ido-read-buffer)
  1604. (setq ido-everywhere nil)
  1605. (if from-hook
  1606. (user-error "Unable to turn on Ido-everywhere while Helm-mode is enabled")
  1607. (user-error "Helm-mode enabled (Ido-everywhere is incompatible with Helm-mode, disabling it)"))))
  1608. (defun helm-mode--ido-everywhere-hook ()
  1609. ;; Called only when user calls directly ido-everywhere
  1610. ;; and helm-mode is enabled.
  1611. (when helm-mode
  1612. (helm-mode--disable-ido-maybe t)))
  1613. ;;;###autoload
  1614. (define-minor-mode helm-mode
  1615. "Toggle generic helm completion.
  1616. All functions in Emacs that use `completing-read',
  1617. `read-file-name', `completion-in-region' and friends will use helm
  1618. interface when this mode is turned on.
  1619. However you can modify this behavior for functions of your choice
  1620. with `helm-completing-read-handlers-alist'.
  1621. Called with a positive arg, turn on unconditionally, with a
  1622. negative arg turn off.
  1623. You can toggle it with M-x `helm-mode'.
  1624. About `ido-mode':
  1625. DO NOT enable `ido-everywhere' when using `helm-mode' and instead of
  1626. using `ido-mode', add the commands where you want to use ido to
  1627. `helm-completing-read-handlers-alist' with `ido' as value.
  1628. Note: This mode is incompatible with Emacs23."
  1629. :group 'helm-mode
  1630. :global t
  1631. :lighter helm-completion-mode-string
  1632. (cl-assert (boundp 'completing-read-function) nil
  1633. "`helm-mode' not available, upgrade to Emacs-24")
  1634. (if helm-mode
  1635. (progn
  1636. (add-function :override completing-read-function
  1637. #'helm--completing-read-default)
  1638. (add-function :override read-file-name-function
  1639. #'helm--generic-read-file-name)
  1640. (add-function :override read-buffer-function
  1641. #'helm--generic-read-buffer)
  1642. (add-function :override completion-in-region-function
  1643. #'helm--completion-in-region)
  1644. ;; If user have enabled ido-everywhere BEFORE enabling
  1645. ;; helm-mode disable it and warn user about its
  1646. ;; incompatibility with helm-mode (issue #2085).
  1647. (helm-mode--disable-ido-maybe)
  1648. ;; If ido-everywhere is not enabled yet anticipate and
  1649. ;; disable it if user attempt to enable it while helm-mode
  1650. ;; is running (issue #2085).
  1651. (add-hook 'ido-everywhere-hook #'helm-mode--ido-everywhere-hook)
  1652. (when (fboundp 'ffap-read-file-or-url-internal)
  1653. ;; `ffap-read-file-or-url-internal' have been removed in
  1654. ;; emacs-27 and `ffap-read-file-or-url' is fixed, so no need
  1655. ;; to advice it.
  1656. (advice-add 'ffap-read-file-or-url :override #'helm-advice--ffap-read-file-or-url)))
  1657. (progn
  1658. (remove-function completing-read-function #'helm--completing-read-default)
  1659. (remove-function read-file-name-function #'helm--generic-read-file-name)
  1660. (remove-function read-buffer-function #'helm--generic-read-buffer)
  1661. (remove-function completion-in-region-function #'helm--completion-in-region)
  1662. (remove-hook 'ido-everywhere-hook #'helm-mode--ido-everywhere-hook)
  1663. (when (fboundp 'ffap-read-file-or-url-internal)
  1664. (advice-remove 'ffap-read-file-or-url #'helm-advice--ffap-read-file-or-url)))))
  1665. (provide 'helm-mode)
  1666. ;; Local Variables:
  1667. ;; byte-compile-warnings: (not obsolete)
  1668. ;; coding: utf-8
  1669. ;; indent-tabs-mode: nil
  1670. ;; End:
  1671. ;;; helm-mode.el ends here