Personal emacs config
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.

5295 lines
195 KiB

5 years ago
  1. ;;; ivy.el --- Incremental Vertical completYon -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2015-2020 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
  4. ;; URL: https://github.com/abo-abo/swiper
  5. ;; Version: 0.13.0
  6. ;; Package-Requires: ((emacs "24.5"))
  7. ;; Keywords: matching
  8. ;; This file is part of GNU Emacs.
  9. ;; This file is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; For a full copy of the GNU General Public License
  18. ;; see <https://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This package provides `ivy-read' as an alternative to
  21. ;; `completing-read' and similar functions.
  22. ;;
  23. ;; There's no intricate code to determine the best candidate.
  24. ;; Instead, the user can navigate to it with `ivy-next-line' and
  25. ;; `ivy-previous-line'.
  26. ;;
  27. ;; The matching is done by splitting the input text by spaces and
  28. ;; re-building it into a regex.
  29. ;; So "for example" is transformed into "\\(for\\).*\\(example\\)".
  30. ;;; Code:
  31. (require 'colir)
  32. (require 'ivy-overlay)
  33. (require 'ivy-faces)
  34. (require 'cl-lib)
  35. (require 'ring)
  36. (eval-when-compile
  37. (require 'subr-x))
  38. ;;* Customization
  39. (defgroup ivy nil
  40. "Incremental vertical completion."
  41. :group 'convenience)
  42. (defcustom ivy-height 10
  43. "Number of lines for the minibuffer window.
  44. See also `ivy-height-alist'."
  45. :type 'integer)
  46. (defcustom ivy-count-format "%-4d "
  47. "The style to use for displaying the current candidate count for `ivy-read'.
  48. Set this to \"\" to suppress the count visibility.
  49. Set this to \"(%d/%d) \" to display both the index and the count."
  50. :type '(choice
  51. (const :tag "Count disabled" "")
  52. (const :tag "Count matches" "%-4d ")
  53. (const :tag "Count matches and show current match" "(%d/%d) ")
  54. string))
  55. (defcustom ivy-pre-prompt-function nil
  56. "When non-nil, add strings before the `ivy-read' prompt."
  57. :type '(choice
  58. (const :tag "Do nothing" nil)
  59. (function :tag "Custom function")))
  60. (defcustom ivy-add-newline-after-prompt nil
  61. "When non-nil, add a newline after the `ivy-read' prompt."
  62. :type 'boolean)
  63. (defcustom ivy-wrap nil
  64. "When non-nil, wrap around after the first and the last candidate."
  65. :type 'boolean)
  66. (defcustom ivy-display-style 'fancy
  67. "The style for formatting the minibuffer.
  68. By default, the matched strings are copied as is.
  69. The fancy display style highlights matching parts of the regexp,
  70. a behavior similar to `swiper'."
  71. :type '(choice
  72. (const :tag "Plain" nil)
  73. (const :tag "Fancy" fancy)))
  74. (defcustom ivy-on-del-error-function #'abort-recursive-edit
  75. "Function to call when deletion fails during completion.
  76. The usual reason for `ivy-backward-delete-char' to fail is when
  77. there is no text left to delete, i.e., when it is called at the
  78. beginning of the minibuffer.
  79. The default setting provides a quick exit from completion.
  80. Another common option is `ignore', which does nothing."
  81. :type '(choice
  82. (const :tag "Exit completion" abort-recursive-edit)
  83. (const :tag "Do nothing" ignore)
  84. (function :tag "Custom function")))
  85. (defcustom ivy-extra-directories '("../" "./")
  86. "Add this to the front of the list when completing file names.
  87. Only \"./\" and \"../\" apply here. They appear in reverse order."
  88. :type '(repeat :tag "Dirs"
  89. (choice
  90. (const :tag "Parent Directory" "../")
  91. (const :tag "Current Directory" "./"))))
  92. (defcustom ivy-use-virtual-buffers nil
  93. "When non-nil, add recent files and/or bookmarks to `ivy-switch-buffer'.
  94. The value `recentf' includes only recent files to the virtual
  95. buffers list, whereas the value `bookmarks' does the same for
  96. bookmarks. Any other non-nil value includes both."
  97. :type '(choice
  98. (const :tag "Don't use virtual buffers" nil)
  99. (const :tag "Recent files" recentf)
  100. (const :tag "Bookmarks" bookmarks)
  101. (const :tag "All virtual buffers" t)))
  102. (defvar ivy--display-function nil
  103. "The display-function is used in current.")
  104. (defvar ivy-display-functions-props
  105. '((ivy-display-function-overlay :cleanup ivy-overlay-cleanup))
  106. "Map Ivy display functions to their property lists.
  107. Examples of properties include associated `:cleanup' functions.")
  108. (defcustom ivy-display-functions-alist
  109. '((ivy-completion-in-region . ivy-display-function-overlay)
  110. (t . nil))
  111. "An alist for customizing where to display the candidates.
  112. Each key is a caller symbol. When the value is nil (the default),
  113. the candidates are shown in the minibuffer. Otherwise, the value
  114. is a function which takes a string argument comprising the
  115. current matching candidates and displays it somewhere.
  116. See also `https://github.com/abo-abo/swiper/wiki/ivy-display-function'."
  117. :type '(alist
  118. :key-type symbol
  119. :value-type (choice
  120. (const :tag "Minibuffer" nil)
  121. (const :tag "LV" ivy-display-function-lv)
  122. (const :tag "Popup" ivy-display-function-popup)
  123. (const :tag "Overlay" ivy-display-function-overlay)
  124. (function :tag "Custom function"))))
  125. (defvar ivy-completing-read-dynamic-collection nil
  126. "Run `ivy-completing-read' with `:dynamic-collection t`.")
  127. (defcustom ivy-completing-read-handlers-alist
  128. '((tmm-menubar . completing-read-default)
  129. (tmm-shortcut . completing-read-default)
  130. (bbdb-create . ivy-completing-read-with-empty-string-def)
  131. (auto-insert . ivy-completing-read-with-empty-string-def)
  132. (Info-on-current-buffer . ivy-completing-read-with-empty-string-def)
  133. (Info-follow-reference . ivy-completing-read-with-empty-string-def)
  134. (Info-menu . ivy-completing-read-with-empty-string-def)
  135. (Info-index . ivy-completing-read-with-empty-string-def)
  136. (Info-virtual-index . ivy-completing-read-with-empty-string-def)
  137. (info-display-manual . ivy-completing-read-with-empty-string-def))
  138. "An alist of handlers to replace `completing-read' in `ivy-mode'."
  139. :type '(alist :key-type symbol :value-type function))
  140. (defcustom ivy-height-alist nil
  141. "An alist to customize `ivy-height'.
  142. It is a list of (CALLER . HEIGHT). CALLER is a caller of
  143. `ivy-read' and HEIGHT is the number of lines displayed.
  144. HEIGHT can also be a function that returns the number of lines."
  145. :type '(alist
  146. :key-type function
  147. :value-type (choice integer function)))
  148. (defvar ivy-completing-read-ignore-handlers-depth -1
  149. "Used to avoid infinite recursion.
  150. If `(minibuffer-depth)' equals this, `ivy-completing-read' will
  151. act as if `ivy-completing-read-handlers-alist' is empty.")
  152. (defvar ivy-highlight-grep-commands nil
  153. "List of grep-like commands.")
  154. (defvar ivy--actions-list nil
  155. "A list of extra actions per command.")
  156. (defun ivy-set-actions (cmd actions)
  157. "Set CMD extra exit points to ACTIONS."
  158. (setq ivy--actions-list
  159. (plist-put ivy--actions-list cmd actions)))
  160. (defun ivy-add-actions (cmd actions)
  161. "Add extra exit points ACTIONS to CMD.
  162. Existing exit points of CMD are overwritten by those in
  163. ACTIONS that have the same key."
  164. (setq ivy--actions-list
  165. (plist-put ivy--actions-list cmd
  166. (cl-delete-duplicates
  167. (append (plist-get ivy--actions-list cmd) actions)
  168. :key #'car :test #'equal))))
  169. (defun ivy--compute-extra-actions (action caller)
  170. "Add extra actions to ACTION based on CALLER."
  171. (let* ((extra-actions (cl-delete-duplicates
  172. (append (plist-get ivy--actions-list t)
  173. (plist-get ivy--actions-list this-command)
  174. (plist-get ivy--actions-list caller))
  175. :key #'car :test #'equal))
  176. (override-default (assoc "o" extra-actions)))
  177. (cond (override-default
  178. (cons 1 (cons override-default
  179. (cl-delete "o" extra-actions
  180. :key #'car :test #'equal))))
  181. ((not extra-actions)
  182. action)
  183. ((functionp action)
  184. `(1
  185. ("o" ,action "default")
  186. ,@extra-actions))
  187. ((null action)
  188. `(1
  189. ("o" identity "default")
  190. ,@extra-actions))
  191. (t
  192. (cons (car action)
  193. (cl-delete-duplicates (cdr (append action extra-actions))
  194. :key #'car :test #'equal :from-end t))))))
  195. (defvar ivy--prompts-list nil)
  196. (defun ivy-set-prompt (caller prompt-fn)
  197. "Associate CALLER with PROMPT-FN.
  198. PROMPT-FN is a function of no arguments that returns a prompt string."
  199. (setq ivy--prompts-list
  200. (plist-put ivy--prompts-list caller prompt-fn)))
  201. (defvar ivy--display-transformers-alist nil
  202. "A list of str->str transformers per command.")
  203. (defun ivy-set-display-transformer (cmd transformer)
  204. "Set CMD a displayed candidate TRANSFORMER.
  205. It's a lambda that takes a string one of the candidates in the
  206. collection and returns a string for display, the same candidate
  207. plus some extra information.
  208. This lambda is called only on the `ivy-height' candidates that
  209. are about to be displayed, not on the whole collection."
  210. (declare (obsolete "Use `ivy-configure' :display-transformer-fn" "<2020-05-20 Wed>"))
  211. (ivy--alist-set 'ivy--display-transformers-alist cmd transformer))
  212. (defvar ivy--sources-list nil
  213. "A list of extra sources per command.")
  214. (defun ivy-set-sources (cmd sources)
  215. "Attach to CMD a list of extra SOURCES.
  216. Each static source is a function that takes no argument and
  217. returns a list of strings.
  218. The (original-source) determines the position of the original
  219. dynamic source.
  220. Extra dynamic sources aren't supported yet.
  221. Example:
  222. (defun small-recentf ()
  223. (cl-subseq recentf-list 0 20))
  224. (ivy-set-sources
  225. 'counsel-locate
  226. '((small-recentf)
  227. (original-source)))"
  228. (setq ivy--sources-list
  229. (plist-put ivy--sources-list cmd sources)))
  230. (defun ivy--compute-extra-candidates (caller)
  231. (let ((extra-sources (or (plist-get ivy--sources-list caller)
  232. '((original-source))))
  233. (result nil))
  234. (dolist (source extra-sources)
  235. (cond ((equal source '(original-source))
  236. (push source result))
  237. ((null (cdr source))
  238. (push (list (car source) (funcall (car source))) result))))
  239. result))
  240. (defvar ivy-current-prefix-arg nil
  241. "Prefix arg to pass to actions.
  242. This is a global variable that is set by ivy functions for use in
  243. action functions.")
  244. ;;* Keymap
  245. (require 'delsel)
  246. (defun ivy-define-key (keymap key def)
  247. "Forward to (`define-key' KEYMAP KEY DEF).
  248. Remove DEF from `counsel-M-x' list."
  249. (put def 'no-counsel-M-x t)
  250. (define-key keymap key def))
  251. (defvar ivy-minibuffer-map
  252. (let ((map (make-sparse-keymap)))
  253. (ivy-define-key map (kbd "C-m") 'ivy-done)
  254. (define-key map [down-mouse-1] 'ignore)
  255. (ivy-define-key map [mouse-1] 'ivy-mouse-done)
  256. (ivy-define-key map [mouse-3] 'ivy-mouse-dispatching-done)
  257. (ivy-define-key map (kbd "C-M-m") 'ivy-call)
  258. (ivy-define-key map (kbd "C-j") 'ivy-alt-done)
  259. (ivy-define-key map (kbd "C-M-j") 'ivy-immediate-done)
  260. (ivy-define-key map (kbd "TAB") 'ivy-partial-or-done)
  261. (ivy-define-key map [remap next-line] 'ivy-next-line)
  262. (ivy-define-key map [remap previous-line] 'ivy-previous-line)
  263. (ivy-define-key map (kbd "C-r") 'ivy-reverse-i-search)
  264. (define-key map (kbd "SPC") 'self-insert-command)
  265. (ivy-define-key map [remap delete-backward-char] 'ivy-backward-delete-char)
  266. (ivy-define-key map [remap backward-delete-char-untabify] 'ivy-backward-delete-char)
  267. (ivy-define-key map [remap backward-kill-word] 'ivy-backward-kill-word)
  268. (ivy-define-key map [remap delete-char] 'ivy-delete-char)
  269. (ivy-define-key map [remap forward-char] 'ivy-forward-char)
  270. (ivy-define-key map (kbd "<right>") 'ivy-forward-char)
  271. (ivy-define-key map [remap kill-word] 'ivy-kill-word)
  272. (ivy-define-key map [remap beginning-of-buffer] 'ivy-beginning-of-buffer)
  273. (ivy-define-key map [remap end-of-buffer] 'ivy-end-of-buffer)
  274. (ivy-define-key map (kbd "M-n") 'ivy-next-history-element)
  275. (ivy-define-key map (kbd "M-p") 'ivy-previous-history-element)
  276. (define-key map (kbd "C-g") 'minibuffer-keyboard-quit)
  277. (ivy-define-key map [remap scroll-up-command] 'ivy-scroll-up-command)
  278. (ivy-define-key map [remap scroll-down-command] 'ivy-scroll-down-command)
  279. (ivy-define-key map (kbd "<next>") 'ivy-scroll-up-command)
  280. (ivy-define-key map (kbd "<prior>") 'ivy-scroll-down-command)
  281. (ivy-define-key map (kbd "C-v") 'ivy-scroll-up-command)
  282. (ivy-define-key map (kbd "M-v") 'ivy-scroll-down-command)
  283. (ivy-define-key map (kbd "C-M-n") 'ivy-next-line-and-call)
  284. (ivy-define-key map (kbd "C-M-p") 'ivy-previous-line-and-call)
  285. (ivy-define-key map (kbd "M-a") 'ivy-toggle-marks)
  286. (ivy-define-key map (kbd "M-r") 'ivy-toggle-regexp-quote)
  287. (ivy-define-key map (kbd "M-j") 'ivy-yank-word)
  288. (ivy-define-key map (kbd "M-i") 'ivy-insert-current)
  289. (ivy-define-key map (kbd "C-M-y") 'ivy-insert-current-full)
  290. (ivy-define-key map (kbd "C-o") 'hydra-ivy/body)
  291. (ivy-define-key map (kbd "M-o") 'ivy-dispatching-done)
  292. (ivy-define-key map (kbd "C-M-o") 'ivy-dispatching-call)
  293. (ivy-define-key map [remap kill-line] 'ivy-kill-line)
  294. (ivy-define-key map [remap kill-whole-line] 'ivy-kill-whole-line)
  295. (ivy-define-key map (kbd "S-SPC") 'ivy-restrict-to-matches)
  296. (ivy-define-key map [remap kill-ring-save] 'ivy-kill-ring-save)
  297. (ivy-define-key map (kbd "C-M-a") 'ivy-read-action)
  298. (ivy-define-key map (kbd "C-c C-o") 'ivy-occur)
  299. (ivy-define-key map (kbd "C-c C-a") 'ivy-toggle-ignore)
  300. (ivy-define-key map (kbd "C-c C-s") 'ivy-rotate-sort)
  301. (ivy-define-key map [remap describe-mode] 'ivy-help)
  302. (ivy-define-key map "$" 'ivy-magic-read-file-env)
  303. map)
  304. "Keymap used in the minibuffer.")
  305. (autoload 'hydra-ivy/body "ivy-hydra" "" t)
  306. (autoload 'ivy-hydra-read-action "ivy-hydra" "" t)
  307. (defvar ivy-mode-map
  308. (let ((map (make-sparse-keymap)))
  309. (ivy-define-key map [remap switch-to-buffer] 'ivy-switch-buffer)
  310. (ivy-define-key map [remap switch-to-buffer-other-window] 'ivy-switch-buffer-other-window)
  311. map)
  312. "Keymap for `ivy-mode'.")
  313. ;;* Globals
  314. (cl-defstruct ivy-state
  315. prompt collection
  316. predicate require-match initial-input
  317. history preselect keymap update-fn sort
  318. ;; The frame in which `ivy-read' was called
  319. frame
  320. ;; The window in which `ivy-read' was called
  321. window
  322. ;; The buffer in which `ivy-read' was called
  323. buffer
  324. ;; The value of `ivy-text' to be used by `ivy-occur'
  325. text
  326. action
  327. unwind
  328. re-builder
  329. matcher
  330. ;; When this is non-nil, call it for each input change to get new candidates
  331. dynamic-collection
  332. ;; A lambda that transforms candidates only for display
  333. display-transformer-fn
  334. directory
  335. caller
  336. current
  337. def
  338. ignore
  339. multi-action
  340. extra-props)
  341. (defvar ivy-last (make-ivy-state)
  342. "The last parameters passed to `ivy-read'.
  343. This should eventually become a stack so that you could use
  344. `ivy-read' recursively.")
  345. (defvar ivy--sessions nil
  346. "Alist mapping session symbols to `ivy-state' objects.")
  347. (defvar ivy-recursive-last nil)
  348. (defvar ivy-recursive-restore t
  349. "When non-nil, restore the above state when exiting the minibuffer.
  350. This variable is let-bound to nil by functions that take care of
  351. the restoring themselves.")
  352. (defsubst ivy-set-action (action)
  353. "Set the current `ivy-last' field to ACTION."
  354. (setf (ivy-state-action ivy-last) action))
  355. (defvar inhibit-message)
  356. (defvar ffap-machine-p-known)
  357. (defun ivy-thing-at-point ()
  358. "Return a string that corresponds to the current thing at point."
  359. (substring-no-properties
  360. (cond
  361. ((use-region-p)
  362. (let* ((beg (region-beginning))
  363. (end (region-end))
  364. (eol (save-excursion (goto-char beg) (line-end-position))))
  365. (buffer-substring-no-properties beg (min end eol))))
  366. ((thing-at-point 'url))
  367. ((and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
  368. (let ((inhibit-message t)
  369. (ffap-machine-p-known 'reject))
  370. (run-hook-with-args-until-success 'file-name-at-point-functions))))
  371. ((let ((s (thing-at-point 'symbol)))
  372. (and (stringp s)
  373. (if (string-match "\\`[`']?\\(.*?\\)'?\\'" s)
  374. (match-string 1 s)
  375. s))))
  376. ((looking-at "(+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
  377. (match-string-no-properties 1))
  378. (t
  379. ""))))
  380. (defvar ivy-history nil
  381. "History list of candidates entered in the minibuffer.
  382. Maximum length of the history list is determined by the value
  383. of `history-length'.")
  384. (defvar ivy--directory nil
  385. "Current directory when completing file names.")
  386. (defvar ivy--directory-hist nil
  387. "Store the history of directories.
  388. This allows RET to reverse consecutive DEL.")
  389. (defvar ivy--length 0
  390. "Store the amount of viable candidates.")
  391. (defvar ivy-text ""
  392. "Store the user's string as it is typed in.")
  393. (defvar ivy-regex ""
  394. "Store the regex value that corresponds to `ivy-text'.")
  395. (defvar ivy--regex-function 'ivy--regex
  396. "Current function for building a regex.")
  397. (defun ivy-set-text (str)
  398. "Set `ivy-text' to STR."
  399. (setq ivy-text str)
  400. (setq ivy-regex (funcall ivy--regex-function ivy-text)))
  401. (defvar ivy--index 0
  402. "Store the index of the current candidate.")
  403. (defvar ivy--window-index 0
  404. "Store the index of the current candidate in the minibuffer window.
  405. This means it's between 0 and `ivy-height'.")
  406. (defvar ivy-exit nil
  407. "Store `done' if the completion was successfully selected.
  408. Otherwise, store nil.")
  409. (defvar ivy--all-candidates nil
  410. "Store the candidates passed to `ivy-read'.")
  411. (defvar ivy--extra-candidates '((original-source))
  412. "Store candidates added by the extra sources.
  413. This is an internal-use alist. Each key is a function name, or
  414. original-source (which represents where the current dynamic
  415. candidates should go).
  416. Each value is an evaluation of the function, in case of static
  417. sources. These values will subsequently be filtered on `ivy-text'.
  418. This variable is set by `ivy-read' and used by `ivy--set-candidates'.")
  419. (defcustom ivy-use-ignore-default t
  420. "The default policy for user-configured candidate filtering."
  421. :type '(choice
  422. (const :tag "Ignore ignored always" always)
  423. (const :tag "Ignore ignored when others exist" t)
  424. (const :tag "Don't ignore" nil)))
  425. (defvar ivy-use-ignore t
  426. "Store policy for user-configured candidate filtering.
  427. This may be changed dynamically by `ivy-toggle-ignore'.
  428. Use `ivy-use-ignore-default' for a permanent configuration.")
  429. (defvar ivy--default nil
  430. "Default initial input.")
  431. (defvar ivy--prompt nil
  432. "Store the format-style prompt.
  433. When non-nil, it should contain at least one %d.")
  434. (defvar ivy--prompt-extra ""
  435. "Temporary modifications to the prompt.")
  436. (defvar ivy--old-re nil
  437. "Store the old regexp.
  438. Either a string or a list for `ivy-re-match'.")
  439. (defvar ivy--old-cands nil
  440. "Store the candidates matched by `ivy--old-re'.")
  441. (defvar ivy--highlight-function 'ivy--highlight-default
  442. "Current function for formatting the candidates.")
  443. (defvar ivy--subexps 0
  444. "Number of groups in the current `ivy--regex'.")
  445. (defvar ivy--full-length nil
  446. "The total amount of candidates when :dynamic-collection is non-nil.")
  447. (defvar ivy--old-text ""
  448. "Store old `ivy-text' for dynamic completion.")
  449. (defvar ivy--trying-to-resume-dynamic-collection nil
  450. "Non-nil if resuming from a dynamic collection.
  451. When non-nil, ivy will wait until the first chunk of asynchronous
  452. candidates has been received before selecting the last
  453. preselected candidate.")
  454. (defun ivy--set-index-dynamic-collection ()
  455. (when ivy--trying-to-resume-dynamic-collection
  456. (let ((preselect-index
  457. (ivy--preselect-index (ivy-state-preselect ivy-last) ivy--all-candidates)))
  458. (when preselect-index
  459. (ivy-set-index preselect-index)))
  460. (setq ivy--trying-to-resume-dynamic-collection nil)))
  461. (defcustom ivy-case-fold-search-default
  462. (if search-upper-case
  463. 'auto
  464. case-fold-search)
  465. "The default value for `case-fold-search' in Ivy operations.
  466. The special value `auto' means case folding is performed so long
  467. as the entire input string comprises lower-case characters. This
  468. corresponds to the default behaviour of most Emacs search
  469. functionality, e.g. as seen in `isearch'."
  470. :link '(info-link "(emacs)Lax Search")
  471. :type '(choice
  472. (const :tag "Auto" auto)
  473. (const :tag "Always" t)
  474. (const :tag "Never" nil)))
  475. (defvar ivy-case-fold-search ivy-case-fold-search-default
  476. "Store the current overriding `case-fold-search'.")
  477. (defcustom ivy-more-chars-alist
  478. '((t . 3))
  479. "Map commands to their minimum required input length.
  480. That is the number of characters prompted for before fetching
  481. candidates. The special key t is used as a fallback."
  482. :type '(alist :key-type symbol :value-type integer))
  483. (defun ivy-more-chars ()
  484. "Return two fake candidates prompting for at least N input.
  485. N is obtained from `ivy-more-chars-alist'."
  486. (let ((diff (- (ivy-alist-setting ivy-more-chars-alist)
  487. (length ivy-text))))
  488. (when (> diff 0)
  489. (list "" (format "%d chars more" diff)))))
  490. (defun ivy--case-fold-p (string)
  491. "Return nil if STRING should be matched case-sensitively."
  492. (if (eq ivy-case-fold-search 'auto)
  493. (string= string (downcase string))
  494. ivy-case-fold-search))
  495. (defun ivy--case-fold-string= (s1 s2)
  496. "Like `string=', but obeys `case-fold-search'."
  497. (eq t (compare-strings s1 nil nil s2 nil nil case-fold-search)))
  498. (defmacro ivy-quit-and-run (&rest body)
  499. "Quit the minibuffer and run BODY afterwards."
  500. (declare (indent 0))
  501. `(progn
  502. (put 'quit 'error-message "")
  503. (run-at-time nil nil
  504. (lambda ()
  505. (put 'quit 'error-message "Quit")
  506. (with-demoted-errors "Error: %S"
  507. ,@body)))
  508. (abort-recursive-edit)))
  509. (defun ivy-exit-with-action (action &optional exit-code)
  510. "Quit the minibuffer and call ACTION afterwards."
  511. (ivy-set-action
  512. `(lambda (x)
  513. (funcall ',action x)
  514. (ivy-set-action ',(ivy-state-action ivy-last))))
  515. (setq ivy-exit (or exit-code 'done))
  516. (exit-minibuffer))
  517. (defmacro with-ivy-window (&rest body)
  518. "Execute BODY in the window from which `ivy-read' was called."
  519. (declare (indent 0)
  520. (debug t))
  521. `(with-selected-window (ivy--get-window ivy-last)
  522. ,@body))
  523. (defun ivy--expand-file-name (text)
  524. (cond
  525. ((eq (ivy-state-history ivy-last) 'grep-files-history)
  526. text)
  527. (ivy--directory
  528. (if (and (string-match-p "^/" text) (file-remote-p ivy--directory))
  529. (let ((parts (split-string ivy--directory ":")))
  530. (concat (nth 0 parts) ":" (nth 1 parts) ":" text))
  531. (expand-file-name text ivy--directory)))
  532. (t
  533. text)))
  534. (defun ivy--done (text)
  535. "Insert TEXT and exit minibuffer."
  536. (if (member (ivy-state-prompt ivy-last) '("Create directory: " "Make directory: "))
  537. (ivy-immediate-done)
  538. (when (stringp text)
  539. (insert
  540. (setf (ivy-state-current ivy-last)
  541. (ivy--expand-file-name text))))
  542. (setq ivy-exit 'done)
  543. (exit-minibuffer)))
  544. (defcustom ivy-use-selectable-prompt nil
  545. "When non-nil, make the prompt line selectable like a candidate.
  546. The prompt line can be selected by calling `ivy-previous-line' when the first
  547. regular candidate is selected. Both actions `ivy-done' and `ivy-alt-done',
  548. when called on a selected prompt, are forwarded to `ivy-immediate-done', which
  549. results to the same as calling `ivy-immediate-done' explicitly when a regular
  550. candidate is selected.
  551. Note that if `ivy-wrap' is set to t, calling `ivy-previous-line' when the
  552. prompt is selected wraps around to the last candidate, while calling
  553. `ivy-next-line' on the last candidate wraps around to the first
  554. candidate, not the prompt."
  555. :type 'boolean)
  556. (defvar ivy--use-selectable-prompt nil
  557. "Store the effective `ivy-use-selectable-prompt' for current session.")
  558. (defun ivy--prompt-selectable-p ()
  559. "Return t if the prompt line is selectable."
  560. (and ivy-use-selectable-prompt
  561. (or (memq (ivy-state-require-match ivy-last)
  562. '(nil confirm confirm-after-completion))
  563. ;; :require-match is t, but "" is in the collection
  564. (let ((coll (ivy-state-collection ivy-last)))
  565. (and (listp coll)
  566. (if (consp (car coll))
  567. (member '("") coll)
  568. (member "" coll)))))))
  569. (defun ivy--prompt-selected-p ()
  570. "Return t if the prompt line is selected."
  571. (and ivy--use-selectable-prompt
  572. (= ivy--index -1)))
  573. ;;* Commands
  574. (defun ivy-done ()
  575. "Exit the minibuffer with the selected candidate."
  576. (interactive)
  577. (if (ivy--prompt-selected-p)
  578. (ivy-immediate-done)
  579. (setq ivy-current-prefix-arg current-prefix-arg)
  580. (delete-minibuffer-contents)
  581. (cond ((and (= ivy--length 0)
  582. (eq this-command 'ivy-dispatching-done))
  583. (ivy--done ivy-text))
  584. ((or (> ivy--length 0)
  585. ;; the action from `ivy-dispatching-done' may not need a
  586. ;; candidate at all
  587. (eq this-command 'ivy-dispatching-done))
  588. (ivy--done (ivy-state-current ivy-last)))
  589. ((and (memq (ivy-state-collection ivy-last)
  590. '(read-file-name-internal internal-complete-buffer))
  591. (eq confirm-nonexistent-file-or-buffer t)
  592. (not (string= " (confirm)" ivy--prompt-extra)))
  593. (setq ivy--prompt-extra " (confirm)")
  594. (insert ivy-text)
  595. (ivy--exhibit))
  596. ((memq (ivy-state-require-match ivy-last)
  597. '(nil confirm confirm-after-completion))
  598. (ivy--done ivy-text))
  599. (t
  600. (setq ivy--prompt-extra " (match required)")
  601. (insert ivy-text)
  602. (ivy--exhibit)))))
  603. (defvar ivy-mouse-1-tooltip
  604. "Exit the minibuffer with the selected candidate."
  605. "The doc visible in the tooltip for mouse-1 binding in the minibuffer.")
  606. (defvar ivy-mouse-3-tooltip
  607. "Display alternative actions."
  608. "The doc visible in the tooltip for mouse-3 binding in the minibuffer.")
  609. (defun ivy-mouse-offset (event)
  610. "Compute the offset between the candidate at point and the selected one."
  611. (if event
  612. (let* ((line-number-at-point
  613. (max 2
  614. (line-number-at-pos (posn-point (event-start event)))))
  615. (line-number-candidate ;; convert to 0 based index
  616. (- line-number-at-point 2))
  617. (offset
  618. (- line-number-candidate
  619. ivy--window-index)))
  620. offset)
  621. nil))
  622. (defun ivy-mouse-done (event)
  623. (interactive "@e")
  624. (let ((offset (ivy-mouse-offset event)))
  625. (when offset
  626. (ivy-next-line offset)
  627. (ivy--exhibit)
  628. (ivy-alt-done))))
  629. (defun ivy-mouse-dispatching-done (event)
  630. (interactive "@e")
  631. (let ((offset (ivy-mouse-offset event)))
  632. (when offset
  633. (ivy-next-line offset)
  634. (ivy--exhibit)
  635. (ivy-dispatching-done))))
  636. (defcustom ivy-read-action-format-function 'ivy-read-action-format-default
  637. "Function used to transform the actions list into a docstring."
  638. :type '(radio
  639. (function-item ivy-read-action-format-default)
  640. (function-item ivy-read-action-format-columns)))
  641. (defun ivy-read-action-format-default (actions)
  642. "Create a docstring from ACTIONS.
  643. ACTIONS is a list. Each list item is a list of 3 items:
  644. key (a string), cmd and doc (a string)."
  645. (format "%s\n%s\n"
  646. (if (eq this-command 'ivy-read-action)
  647. "Select action: "
  648. (ivy-state-current ivy-last))
  649. (mapconcat
  650. (lambda (x)
  651. (format "%s: %s"
  652. (propertize
  653. (car x)
  654. 'face 'ivy-action)
  655. (nth 2 x)))
  656. actions
  657. "\n")))
  658. (defun ivy-read-action-format-columns (actions)
  659. "Create a docstring from ACTIONS, using several columns if needed to preserve `ivy-height'.
  660. ACTIONS is a list. Each list item is a list of 3 items: key (a
  661. string), cmd and doc (a string)."
  662. (let ((length (length actions))
  663. (i 0)
  664. (max-rows (- ivy-height 1))
  665. rows cols col lwidth rwidth)
  666. (while (< i length)
  667. (setq col (cl-subseq actions i (min length (cl-incf i max-rows))))
  668. (setq lwidth (apply 'max (mapcar (lambda (x)
  669. (length (nth 0 x)))
  670. col)))
  671. (setq rwidth (apply 'max (mapcar (lambda (x)
  672. (length (nth 2 x)))
  673. col)))
  674. (setq col (mapcar (lambda (x)
  675. (format (format "%%%ds: %%-%ds" lwidth rwidth)
  676. (propertize (car x) 'face 'ivy-action)
  677. (nth 2 x)))
  678. col))
  679. (cond
  680. ((null rows)
  681. (setq rows (length col)))
  682. ((< (length col) rows)
  683. (setq col (append col (make-list (- rows (length col)) "")))))
  684. (push col cols))
  685. (format "%s\n%s\n"
  686. (if (eq this-command 'ivy-read-action)
  687. "Select action: "
  688. (ivy-state-current ivy-last))
  689. (mapconcat 'identity
  690. (apply 'cl-mapcar
  691. (lambda (&rest args)
  692. (mapconcat 'identity args " | "))
  693. (nreverse cols))
  694. "\n"))))
  695. (defcustom ivy-read-action-function #'ivy-read-action-by-key
  696. "Function used to read an action."
  697. :type '(radio
  698. (function-item ivy-read-action-by-key)
  699. (function-item ivy-read-action-ivy)
  700. (function-item ivy-hydra-read-action)))
  701. (defun ivy-read-action ()
  702. "Change the action to one of the available ones.
  703. Return nil for `minibuffer-keyboard-quit' or wrong key during the
  704. selection, non-nil otherwise."
  705. (interactive)
  706. (let ((actions (ivy-state-action ivy-last)))
  707. (if (not (ivy--actionp actions))
  708. t
  709. (let ((ivy--directory ivy--directory))
  710. (funcall ivy-read-action-function actions)))))
  711. (defvar set-message-function)
  712. (defun ivy-read-action-by-key (actions)
  713. (let* ((set-message-function nil)
  714. (hint (funcall ivy-read-action-format-function (cdr actions)))
  715. (resize-mini-windows t)
  716. (key "")
  717. action-idx)
  718. (while (and (setq action-idx (cl-position-if
  719. (lambda (x)
  720. (string-prefix-p key (car x)))
  721. (cdr actions)))
  722. (not (string= key (car (nth action-idx (cdr actions))))))
  723. (setq key (concat key (key-description (vector (read-key hint))))))
  724. (ivy-shrink-after-dispatching)
  725. (cond ((member key '("ESC" "C-g" "M-o"))
  726. nil)
  727. ((null action-idx)
  728. (message "%s is not bound" key)
  729. nil)
  730. (t
  731. (message "")
  732. (setcar actions (1+ action-idx))
  733. (ivy-set-action actions)))))
  734. (defvar ivy-marked-candidates nil
  735. "List of marked candidates.
  736. Use `ivy-mark' to populate this.
  737. When this list is non-nil at the end of the session, the action
  738. will be called for each element of this list.")
  739. (defun ivy-read-action-ivy (actions)
  740. "Select an action from ACTIONS using Ivy."
  741. (let ((enable-recursive-minibuffers t))
  742. (if (and (> (minibuffer-depth) 1)
  743. (eq (ivy-state-caller ivy-last) 'ivy-read-action-ivy))
  744. (minibuffer-keyboard-quit)
  745. (let ((ivy-marked-candidates ivy-marked-candidates))
  746. (ivy-read (format "action (%s): " (ivy-state-current ivy-last))
  747. (cl-mapcar
  748. (lambda (a i) (cons (format "[%s] %s" (nth 0 a) (nth 2 a)) i))
  749. (cdr actions) (number-sequence 1 (length (cdr actions))))
  750. :action (lambda (a)
  751. (setcar actions (cdr a))
  752. (ivy-set-action actions))
  753. :caller 'ivy-read-action-ivy)))))
  754. (defun ivy-shrink-after-dispatching ()
  755. "Shrink the window after dispatching when action list is too large."
  756. (when (window-minibuffer-p)
  757. (window-resize nil (- ivy-height (window-height)))))
  758. (defun ivy-dispatching-done ()
  759. "Select one of the available actions and call `ivy-done'."
  760. (interactive)
  761. (let ((ivy-exit 'ivy-dispatching-done))
  762. (when (ivy-read-action)
  763. (ivy-done)))
  764. (ivy-shrink-after-dispatching))
  765. (defun ivy-dispatching-call ()
  766. "Select one of the available actions and call `ivy-call'."
  767. (interactive)
  768. (setq ivy-current-prefix-arg current-prefix-arg)
  769. (let ((actions (copy-sequence (ivy-state-action ivy-last)))
  770. (old-ivy-text ivy-text))
  771. (unwind-protect
  772. (when (ivy-read-action)
  773. (ivy-set-text old-ivy-text)
  774. (ivy-call))
  775. (ivy-set-action actions)))
  776. (ivy-shrink-after-dispatching))
  777. (defun ivy-build-tramp-name (x)
  778. "Reconstruct X into a path.
  779. Is is a cons cell, related to `tramp-get-completion-function'."
  780. (let ((user (car x))
  781. (domain (cadr x)))
  782. (if user
  783. (concat user "@" domain)
  784. domain)))
  785. (declare-function Info-find-node "info")
  786. (declare-function Info-read-node-name-1 "info")
  787. (declare-function tramp-get-completion-function "tramp")
  788. (defcustom ivy-alt-done-functions-alist nil
  789. "Customize what `ivy-alt-done' does per-collection."
  790. :type '(alist :key-type symbol :value-type function))
  791. (defun ivy--completing-fname-p ()
  792. (eq 'file (cdr (assoc
  793. 'category
  794. (ignore-errors
  795. (funcall (ivy-state-collection ivy-last) ivy-text nil 'metadata))))))
  796. (defun ivy-alt-done (&optional arg)
  797. "Exit the minibuffer with the selected candidate.
  798. When ARG is t, exit with current text, ignoring the candidates.
  799. When the current candidate during file name completion is a
  800. directory, continue completion from within that directory instead
  801. of exiting. This function is otherwise like `ivy-done'."
  802. (interactive "P")
  803. (setq ivy-current-prefix-arg current-prefix-arg)
  804. (let (alt-done-fn)
  805. (cond ((or arg (ivy--prompt-selected-p))
  806. (ivy-immediate-done))
  807. ((setq alt-done-fn (ivy-alist-setting ivy-alt-done-functions-alist))
  808. (funcall alt-done-fn))
  809. ((ivy--completing-fname-p)
  810. (ivy--directory-done))
  811. (t
  812. (ivy-done)))))
  813. (defun ivy--info-alt-done ()
  814. (if (member (ivy-state-current ivy-last) '("(./)" "(../)"))
  815. (ivy-quit-and-run
  816. (ivy-read "Go to file: " #'read-file-name-internal
  817. :action (lambda (x)
  818. (Info-find-node
  819. (expand-file-name x ivy--directory)
  820. "Top"))))
  821. (ivy-done)))
  822. (defvar ivy-auto-select-single-candidate nil
  823. "When non-nil, auto-select the candidate if it is the only one.
  824. When t, it is the same as if the user were prompted and selected the candidate
  825. by calling the default action. This variable has no use unless the collection
  826. contains a single candidate.")
  827. (defun ivy--directory-enter ()
  828. (let (dir)
  829. (when (and
  830. (> ivy--length 0)
  831. (not (string= (ivy-state-current ivy-last) "./"))
  832. (setq dir (ivy-expand-file-if-directory (ivy-state-current ivy-last))))
  833. (ivy--cd dir)
  834. (ivy--exhibit))))
  835. (defun ivy--handle-directory (input)
  836. "Detect the next directory based on special values of INPUT."
  837. (cond ((string= input "/")
  838. "/")
  839. ((string= input "/sudo::")
  840. (concat input ivy--directory))))
  841. (defun ivy--tramp-candidates ()
  842. (let ((method (match-string 1 ivy-text))
  843. (user (match-string 2 ivy-text))
  844. (rest (match-string 3 ivy-text))
  845. res)
  846. (dolist (x (tramp-get-completion-function method))
  847. (setq res (append res (funcall (car x) (cadr x)))))
  848. (setq res (delq nil res))
  849. (when user
  850. (dolist (x res)
  851. (setcar x user)))
  852. (setq res (delete-dups res))
  853. (let* ((old-ivy-last ivy-last)
  854. (enable-recursive-minibuffers t)
  855. (host (let ((ivy-auto-select-single-candidate nil))
  856. (ivy-read "user@host: "
  857. (mapcar #'ivy-build-tramp-name res)
  858. :initial-input rest))))
  859. (setq ivy-last old-ivy-last)
  860. (when host
  861. (setq ivy--directory "/")
  862. (ivy--cd (concat "/" method ":" host ":/"))))))
  863. (defun ivy--directory-done ()
  864. "Handle exit from the minibuffer when completing file names."
  865. (let ((dir (ivy--handle-directory ivy-text)))
  866. (cond ((equal (ivy-state-current ivy-last) (ivy-state-def ivy-last))
  867. (ivy-done))
  868. ((and (ivy-state-require-match ivy-last)
  869. (equal ivy-text "")
  870. (null ivy--old-cands))
  871. (ivy-immediate-done))
  872. (dir
  873. (let ((inhibit-message t))
  874. (ivy--cd dir)))
  875. ((ivy--directory-enter))
  876. ((unless (string= ivy-text "")
  877. ;; Obsolete since 26.1 and removed in 28.1.
  878. (defvar tramp-completion-mode)
  879. (with-no-warnings
  880. (let* ((tramp-completion-mode t)
  881. (file (expand-file-name
  882. (if (> ivy--length 0) (ivy-state-current ivy-last) ivy-text)
  883. ivy--directory)))
  884. (when (ignore-errors (file-exists-p file))
  885. (if (file-directory-p file)
  886. (ivy--cd (file-name-as-directory file))
  887. (ivy-done))
  888. ivy-text)))))
  889. ((or (and (equal ivy--directory "/")
  890. (string-match-p "\\`[^/]+:.*:.*\\'" ivy-text))
  891. (string-match-p "\\`/[^/]+:.*:.*\\'" ivy-text))
  892. (ivy-done))
  893. ((ivy--tramp-prefix-p)
  894. (ivy--tramp-candidates))
  895. (t
  896. (ivy-done)))))
  897. (defun ivy--tramp-prefix-p ()
  898. (or (and (equal ivy--directory "/")
  899. (cond ((string-match
  900. "\\`\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
  901. ivy-text)
  902. (save-match-data
  903. (ivy-set-text (ivy-state-current ivy-last))))
  904. ((string-match
  905. "\\`\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
  906. (ivy-state-current ivy-last))
  907. (save-match-data
  908. (ivy-set-text (ivy-state-current ivy-last))))))
  909. (string-match
  910. "\\`/\\([^/]+?\\):\\(?:\\(.*\\)@\\)?\\(.*\\)\\'"
  911. ivy-text)))
  912. (defun ivy-expand-file-if-directory (file-name)
  913. "Expand FILE-NAME as directory.
  914. When this directory doesn't exist, return nil."
  915. (when (stringp file-name)
  916. (let ((full-name
  917. ;; Ignore host name must not match method "ssh"
  918. (ignore-errors
  919. (file-name-as-directory
  920. (expand-file-name file-name ivy--directory)))))
  921. (when (and full-name (file-directory-p full-name))
  922. full-name))))
  923. (defcustom ivy-tab-space nil
  924. "When non-nil, `ivy-partial-or-done' should insert a space."
  925. :type 'boolean)
  926. (defun ivy-partial-or-done ()
  927. "Complete the minibuffer text as much as possible.
  928. If the text hasn't changed as a result, forward to `ivy-alt-done'."
  929. (interactive)
  930. (cond
  931. ((and (numberp completion-cycle-threshold)
  932. (< (length ivy--all-candidates) completion-cycle-threshold))
  933. (let ((ivy-wrap t))
  934. (ivy-next-line)))
  935. ((and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
  936. (or (and (equal ivy--directory "/")
  937. (string-match-p "\\`[^/]+:.*\\'" ivy-text))
  938. (= (string-to-char ivy-text) ?/)))
  939. (let ((default-directory ivy--directory)
  940. dir)
  941. (minibuffer-complete)
  942. (ivy-set-text (ivy--input))
  943. (when (setq dir (ivy-expand-file-if-directory ivy-text))
  944. (ivy--cd dir))))
  945. (t
  946. (or (ivy-partial)
  947. (when (or (eq this-command last-command)
  948. (eq ivy--length 1))
  949. (ivy-alt-done))))))
  950. (defun ivy--partial-cd-for-single-directory ()
  951. (when (and
  952. (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
  953. (= 1 (length
  954. (ivy--re-filter
  955. (funcall ivy--regex-function
  956. (concat "^" (string-remove-prefix "^" ivy-text)))
  957. ivy--all-candidates)))
  958. (let ((default-directory ivy--directory))
  959. (file-directory-p (ivy-state-current ivy-last))))
  960. (ivy--directory-done)))
  961. (defun ivy-partial ()
  962. "Complete the minibuffer text as much as possible."
  963. (interactive)
  964. (let* ((parts (or (ivy--split-spaces ivy-text) (list "")))
  965. (tail (last parts))
  966. (postfix (car tail))
  967. (case-fold-search (ivy--case-fold-p ivy-text))
  968. (completion-ignore-case case-fold-search)
  969. (new (try-completion (string-remove-prefix "^" postfix)
  970. (if (ivy-state-dynamic-collection ivy-last)
  971. ivy--all-candidates
  972. (mapcar (lambda (str)
  973. (let ((i (string-match-p postfix str)))
  974. (and i (substring str i))))
  975. ivy--old-cands)))))
  976. (cond ((eq new t) nil)
  977. ((string= new ivy-text) nil)
  978. ((string= (car tail) (car (ivy--split-spaces new))) nil)
  979. (new
  980. (delete-region (minibuffer-prompt-end) (point-max))
  981. (setcar tail
  982. (if (= (string-to-char postfix) ?^)
  983. (concat "^" new)
  984. new))
  985. (ivy-set-text
  986. (concat
  987. (mapconcat #'identity parts " ")
  988. (and ivy-tab-space (not (= (length ivy--old-cands) 1)) " ")))
  989. (insert ivy-text)
  990. (ivy--partial-cd-for-single-directory)
  991. t))))
  992. (defvar ivy-completion-beg nil
  993. "Completion bounds start.")
  994. (defvar ivy-completion-end nil
  995. "Completion bounds end.")
  996. (defun ivy-immediate-done ()
  997. "Exit the minibuffer with current input instead of current candidate."
  998. (interactive)
  999. (delete-minibuffer-contents)
  1000. (setf (ivy-state-current ivy-last)
  1001. (cond ((or (not ivy--directory)
  1002. (eq (ivy-state-history ivy-last) 'grep-files-history))
  1003. ivy-text)
  1004. ((and (string= ivy-text "")
  1005. (eq (ivy-state-collection ivy-last)
  1006. #'read-file-name-internal))
  1007. (if (ivy-state-def ivy-last)
  1008. (if (and
  1009. (file-exists-p (ivy-state-def ivy-last))
  1010. (/= (length ivy--directory)
  1011. (1+ (length (expand-file-name (ivy-state-def ivy-last))))))
  1012. ivy--directory
  1013. (copy-sequence (ivy-state-def ivy-last)))
  1014. ivy--directory))
  1015. (t
  1016. (expand-file-name ivy-text ivy--directory))))
  1017. (insert (ivy-state-current ivy-last))
  1018. (setq ivy-completion-beg ivy-completion-end)
  1019. (setq ivy-exit 'done)
  1020. (exit-minibuffer))
  1021. (defun ivy--restore-session (&optional session)
  1022. "Resume a recorded completion SESSION, if any exists."
  1023. (when ivy--sessions
  1024. (unless session
  1025. (setq session (intern
  1026. (let ((ivy-last ivy-last)
  1027. ivy--all-candidates
  1028. ivy-text)
  1029. (ivy-read "Choose ivy session: "
  1030. ivy--sessions
  1031. :require-match t)))))
  1032. (setq ivy-last (or (cdr (assq session ivy--sessions))
  1033. ivy-last)))
  1034. (let ((data (plist-get (ivy-state-extra-props ivy-last) :ivy-data)))
  1035. (when data
  1036. (setq ivy--all-candidates (plist-get data :all-candidates))
  1037. (setq ivy-text (plist-get data :text)))))
  1038. ;;;###autoload
  1039. (defun ivy-resume (&optional session)
  1040. "Resume the last completion session, or SESSION if non-nil.
  1041. With a prefix arg, try to restore a recorded completion session,
  1042. if one exists."
  1043. (interactive)
  1044. (when (or current-prefix-arg session)
  1045. (ivy--restore-session session))
  1046. (if (or (null (ivy-state-action ivy-last))
  1047. (eq (ivy--get-action ivy-last) #'identity))
  1048. (user-error "The last session isn't compatible with `ivy-resume'")
  1049. (when (memq (ivy-state-caller ivy-last)
  1050. '(swiper
  1051. swiper-isearch swiper-backward
  1052. swiper-isearch-backward
  1053. counsel-grep))
  1054. (switch-to-buffer (ivy-state-buffer ivy-last)))
  1055. (with-current-buffer (ivy-state-buffer ivy-last)
  1056. (let ((default-directory (ivy-state-directory ivy-last))
  1057. (ivy-use-ignore-default (ivy-state-ignore ivy-last)))
  1058. (ivy-read
  1059. (ivy-state-prompt ivy-last)
  1060. (ivy-state-collection ivy-last)
  1061. :predicate (ivy-state-predicate ivy-last)
  1062. :require-match (ivy-state-require-match ivy-last)
  1063. :initial-input ivy-text
  1064. :history (ivy-state-history ivy-last)
  1065. :preselect (ivy-state-current ivy-last)
  1066. :keymap (ivy-state-keymap ivy-last)
  1067. :update-fn (ivy-state-update-fn ivy-last)
  1068. :sort (ivy-state-sort ivy-last)
  1069. :action (ivy-state-action ivy-last)
  1070. :unwind (ivy-state-unwind ivy-last)
  1071. :re-builder (ivy-state-re-builder ivy-last)
  1072. :matcher (ivy-state-matcher ivy-last)
  1073. :dynamic-collection (ivy-state-dynamic-collection ivy-last)
  1074. :extra-props (ivy-state-extra-props ivy-last)
  1075. :caller (ivy-state-caller ivy-last))))))
  1076. (defvar-local ivy-calling nil
  1077. "When non-nil, call the current action when `ivy--index' changes.")
  1078. (defun ivy-set-index (index)
  1079. "Set `ivy--index' to INDEX."
  1080. (setq ivy--index index)
  1081. (when ivy-calling
  1082. (ivy--exhibit)
  1083. (ivy-call)))
  1084. (defun ivy-beginning-of-buffer ()
  1085. "Select the first completion candidate."
  1086. (interactive)
  1087. (ivy-set-index 0))
  1088. (defun ivy-end-of-buffer ()
  1089. "Select the last completion candidate."
  1090. (interactive)
  1091. (ivy-set-index (1- ivy--length)))
  1092. (defun ivy-scroll-up-command ()
  1093. "Scroll the candidates upward by the minibuffer height."
  1094. (interactive)
  1095. (ivy-set-index (min (1- (+ ivy--index ivy-height))
  1096. (1- ivy--length))))
  1097. (defun ivy-scroll-down-command ()
  1098. "Scroll the candidates downward by the minibuffer height."
  1099. (interactive)
  1100. (ivy-set-index (max (1+ (- ivy--index ivy-height))
  1101. 0)))
  1102. (defun ivy-next-line (&optional arg)
  1103. "Move cursor vertically down ARG candidates."
  1104. (interactive "p")
  1105. (setq arg (or arg 1))
  1106. (let ((index (+ ivy--index arg)))
  1107. (if (> index (1- ivy--length))
  1108. (if ivy-wrap
  1109. (ivy-beginning-of-buffer)
  1110. (ivy-set-index (1- ivy--length)))
  1111. (ivy-set-index index))))
  1112. (defun ivy-next-line-or-history (&optional arg)
  1113. "Move cursor vertically down ARG candidates.
  1114. If the input is empty, select the previous history element instead."
  1115. (interactive "p")
  1116. (let ((orig-index ivy--index))
  1117. (ivy-next-line arg)
  1118. (when (and (string= ivy-text "") (= ivy--index orig-index))
  1119. (ivy-previous-history-element 1))))
  1120. (defun ivy-previous-line (&optional arg)
  1121. "Move cursor vertically up ARG candidates."
  1122. (interactive "p")
  1123. (setq arg (or arg 1))
  1124. (let ((index (- ivy--index arg))
  1125. (min-index (if ivy--use-selectable-prompt -1 0)))
  1126. (if (< index min-index)
  1127. (if ivy-wrap
  1128. (ivy-end-of-buffer)
  1129. (ivy-set-index min-index))
  1130. (ivy-set-index index))))
  1131. (defun ivy-previous-line-or-history (arg)
  1132. "Move cursor vertically up ARG candidates.
  1133. If the input is empty, select the previous history element instead."
  1134. (interactive "p")
  1135. (let ((orig-index ivy--index))
  1136. (ivy-previous-line arg)
  1137. (when (and (string= ivy-text "") (= ivy--index orig-index))
  1138. (ivy-previous-history-element 1))))
  1139. (defun ivy-toggle-calling ()
  1140. "Flip `ivy-calling'."
  1141. (interactive)
  1142. (when (setq ivy-calling (not ivy-calling))
  1143. (ivy-call)))
  1144. (defun ivy-toggle-ignore ()
  1145. "Toggle user-configured candidate filtering."
  1146. (interactive)
  1147. (setq ivy-use-ignore
  1148. (if ivy-use-ignore
  1149. nil
  1150. (or ivy-use-ignore-default t)))
  1151. (setf (ivy-state-ignore ivy-last) ivy-use-ignore)
  1152. ;; invalidate cache
  1153. (setq ivy--old-cands nil))
  1154. (defun ivy--get-action (state)
  1155. "Get the action function from STATE."
  1156. (let ((action (ivy-state-action state)))
  1157. (when action
  1158. (if (functionp action)
  1159. action
  1160. (cadr (nth (car action) action))))))
  1161. (defun ivy--get-multi-action (state)
  1162. "Get the multi-action function from STATE."
  1163. (let* ((action (ivy-state-action state))
  1164. (multi-action
  1165. (and (listp action)
  1166. (not (eq (car action) 'lambda))
  1167. (nth 3 (nth (car action) action)))))
  1168. (if multi-action
  1169. multi-action
  1170. (when (eq (car action) 1)
  1171. (ivy-state-multi-action state)))))
  1172. (defun ivy--get-window (state)
  1173. "Get the window from STATE."
  1174. (if (ivy-state-p state)
  1175. (let ((window (ivy-state-window state)))
  1176. (if (window-live-p window)
  1177. window
  1178. (next-window)))
  1179. (selected-window)))
  1180. (defun ivy--actionp (x)
  1181. "Return non-nil when X is a list of actions."
  1182. (and (consp x) (not (memq (car x) '(closure lambda)))))
  1183. (defcustom ivy-action-wrap nil
  1184. "When non-nil, `ivy-next-action' and `ivy-prev-action' wrap."
  1185. :type 'boolean)
  1186. (defun ivy-next-action ()
  1187. "When the current action is a list, scroll it forwards."
  1188. (interactive)
  1189. (let ((action (ivy-state-action ivy-last)))
  1190. (when (ivy--actionp action)
  1191. (let ((len (1- (length action)))
  1192. (idx (car action)))
  1193. (if (>= idx len)
  1194. (when ivy-action-wrap
  1195. (setf (car action) 1))
  1196. (cl-incf (car action)))))))
  1197. (defun ivy-prev-action ()
  1198. "When the current action is a list, scroll it backwards."
  1199. (interactive)
  1200. (let ((action (ivy-state-action ivy-last)))
  1201. (when (ivy--actionp action)
  1202. (if (<= (car action) 1)
  1203. (when ivy-action-wrap
  1204. (setf (car action) (1- (length action))))
  1205. (cl-decf (car action))))))
  1206. (defun ivy-action-name ()
  1207. "Return the name associated with the current action."
  1208. (let ((action (ivy-state-action ivy-last)))
  1209. (if (ivy--actionp action)
  1210. (format "[%d/%d] %s"
  1211. (car action)
  1212. (1- (length action))
  1213. (nth 2 (nth (car action) action)))
  1214. "[1/1] default")))
  1215. (defvar ivy-inhibit-action nil
  1216. "When non-nil, `ivy-call' does nothing.
  1217. Example use:
  1218. (let* ((ivy-inhibit-action t)
  1219. (str (ivy-switch-buffer)))
  1220. ;; do whatever with str - the corresponding buffer will not be opened
  1221. )")
  1222. (defun ivy-recursive-restore ()
  1223. "Restore the above state when exiting the minibuffer.
  1224. See variable `ivy-recursive-restore' for further information."
  1225. (when (and ivy-recursive-last
  1226. ivy-recursive-restore
  1227. (not (eq ivy-last ivy-recursive-last)))
  1228. (ivy--reset-state (setq ivy-last ivy-recursive-last))))
  1229. (defvar ivy-mark-prefix ">"
  1230. "Prefix used by `ivy-mark'.")
  1231. (defun ivy--call-marked (action)
  1232. (let* ((prefix-len (length ivy-mark-prefix))
  1233. (marked-candidates
  1234. (mapcar
  1235. (lambda (s)
  1236. (let ((cand (substring s prefix-len)))
  1237. (if ivy--directory
  1238. (expand-file-name cand ivy--directory)
  1239. cand)))
  1240. ivy-marked-candidates))
  1241. (multi-action (ivy--get-multi-action ivy-last)))
  1242. (if multi-action
  1243. (let ((default-directory (ivy-state-directory ivy-last)))
  1244. (funcall multi-action (mapcar #'ivy--call-cand marked-candidates)))
  1245. (dolist (c marked-candidates)
  1246. (let ((default-directory (ivy-state-directory ivy-last)))
  1247. (funcall action (ivy--call-cand c)))))))
  1248. (defun ivy--call-cand (current)
  1249. (let ((collection (ivy-state-collection ivy-last)))
  1250. (cond
  1251. ;; Alist type.
  1252. ((and (consp (car-safe collection))
  1253. ;; Previously, the cdr of the selected
  1254. ;; candidate would be returned. Now, the
  1255. ;; whole candidate is returned.
  1256. (let ((idx (get-text-property 0 'idx current)))
  1257. (if idx
  1258. (progn
  1259. (ivy--remove-props current 'idx)
  1260. (nth idx collection))
  1261. (assoc current collection)))))
  1262. (ivy--directory
  1263. (expand-file-name current ivy--directory))
  1264. ((equal current "")
  1265. ivy-text)
  1266. (t
  1267. current))))
  1268. (defun ivy-call ()
  1269. "Call the current action without exiting completion."
  1270. (interactive)
  1271. ;; Testing with `ivy-with' seems to call `ivy-call' again,
  1272. ;; in which case `this-command' is nil; so check for this.
  1273. (unless (memq this-command '(nil
  1274. ivy-done
  1275. ivy-alt-done
  1276. ivy-dispatching-done))
  1277. (setq ivy-current-prefix-arg current-prefix-arg))
  1278. (let* ((action
  1279. (if (functionp ivy-inhibit-action)
  1280. ivy-inhibit-action
  1281. (and (not ivy-inhibit-action)
  1282. (ivy--get-action ivy-last))))
  1283. (current (ivy-state-current ivy-last))
  1284. (x (ivy--call-cand current))
  1285. (res
  1286. (cond
  1287. ((null action)
  1288. current)
  1289. (t
  1290. (select-window (ivy--get-window ivy-last))
  1291. (set-buffer (ivy-state-buffer ivy-last))
  1292. (prog1 (unwind-protect
  1293. (if ivy-marked-candidates
  1294. (ivy--call-marked action)
  1295. (funcall action x))
  1296. (ivy-recursive-restore))
  1297. (unless (or (eq ivy-exit 'done)
  1298. (minibuffer-window-active-p (selected-window))
  1299. (null (active-minibuffer-window)))
  1300. (select-window (active-minibuffer-window))))))))
  1301. (if ivy-inhibit-action
  1302. res
  1303. current)))
  1304. (defun ivy-call-and-recenter ()
  1305. "Call action and recenter window according to the selected candidate."
  1306. (interactive)
  1307. (ivy-call)
  1308. (with-ivy-window
  1309. (recenter-top-bottom)))
  1310. (defun ivy-next-line-and-call (&optional arg)
  1311. "Move cursor vertically down ARG candidates.
  1312. Call the permanent action if possible."
  1313. (interactive "p")
  1314. (ivy-next-line arg)
  1315. (ivy--exhibit)
  1316. (ivy-call))
  1317. (defun ivy-previous-line-and-call (&optional arg)
  1318. "Move cursor vertically up ARG candidates.
  1319. Call the permanent action if possible."
  1320. (interactive "p")
  1321. (ivy-previous-line arg)
  1322. (ivy--exhibit)
  1323. (ivy-call))
  1324. (defun ivy-previous-history-element (arg)
  1325. "Forward to `previous-history-element' with ARG."
  1326. (interactive "p")
  1327. (previous-history-element arg)
  1328. (ivy--cd-maybe)
  1329. (move-end-of-line 1)
  1330. (ivy--maybe-scroll-history))
  1331. (defun ivy--insert-symbol-boundaries ()
  1332. (undo-boundary)
  1333. (beginning-of-line)
  1334. (insert "\\_<")
  1335. (end-of-line)
  1336. (insert "\\_>"))
  1337. (defun ivy-next-history-element (arg)
  1338. "Forward to `next-history-element' with ARG."
  1339. (interactive "p")
  1340. (if (and (= minibuffer-history-position 0)
  1341. (equal ivy-text ""))
  1342. (progn
  1343. (when minibuffer-default
  1344. (setq ivy--default (car minibuffer-default)))
  1345. (insert ivy--default)
  1346. (when (and (with-ivy-window (derived-mode-p 'prog-mode))
  1347. (eq (ivy-state-caller ivy-last) 'swiper)
  1348. (not (file-exists-p ivy--default))
  1349. (not (ivy-ffap-url-p ivy--default))
  1350. (not (ivy-state-dynamic-collection ivy-last))
  1351. (> (point) (minibuffer-prompt-end)))
  1352. (ivy--insert-symbol-boundaries)))
  1353. (next-history-element arg))
  1354. (ivy--cd-maybe)
  1355. (move-end-of-line 1)
  1356. (ivy--maybe-scroll-history))
  1357. (defvar ivy-ffap-url-functions nil
  1358. "List of functions that check if the point is on a URL.")
  1359. (defun ivy--cd-maybe ()
  1360. "Check if the current input points to a different directory.
  1361. If so, move to that directory, while keeping only the file name."
  1362. (when ivy--directory
  1363. (let ((input (ivy--input))
  1364. url)
  1365. (if (setq url (or (ivy-ffap-url-p input)
  1366. (with-ivy-window
  1367. (cl-reduce
  1368. (lambda (a b)
  1369. (or a (funcall b)))
  1370. ivy-ffap-url-functions
  1371. :initial-value nil))))
  1372. (ivy-exit-with-action
  1373. (lambda (_)
  1374. (ivy-ffap-url-fetcher url))
  1375. 'no-update-history)
  1376. (setq input (expand-file-name input))
  1377. (let ((file (file-name-nondirectory input))
  1378. (dir (expand-file-name (file-name-directory input))))
  1379. (if (string= dir ivy--directory)
  1380. (progn
  1381. (delete-minibuffer-contents)
  1382. (insert file))
  1383. (ivy--cd dir)
  1384. (insert file)))))))
  1385. (defun ivy--maybe-scroll-history ()
  1386. "If the selected history element has an index, scroll there."
  1387. (let ((idx (ignore-errors
  1388. (get-text-property
  1389. (minibuffer-prompt-end)
  1390. 'ivy-index))))
  1391. (when idx
  1392. (ivy--exhibit)
  1393. (ivy-set-index idx))))
  1394. (declare-function tramp-get-completion-methods "tramp")
  1395. (defun ivy--cd (dir)
  1396. "When completing file names, move to directory DIR."
  1397. (if (ivy--completing-fname-p)
  1398. (progn
  1399. (push dir ivy--directory-hist)
  1400. (setq ivy--old-cands nil)
  1401. (setq ivy--old-re nil)
  1402. (ivy-set-index 0)
  1403. (setq ivy--all-candidates
  1404. (append
  1405. (ivy--sorted-files (setq ivy--directory dir))
  1406. (when (and (string= dir "/") (featurep 'tramp))
  1407. (sort
  1408. (mapcar
  1409. (lambda (s) (substring s 1))
  1410. (tramp-get-completion-methods ""))
  1411. #'string<))))
  1412. (ivy-set-text "")
  1413. (setf (ivy-state-directory ivy-last) dir)
  1414. (delete-minibuffer-contents))
  1415. (error "Unexpected")))
  1416. (defun ivy--parent-dir (filename)
  1417. "Return parent directory of absolute FILENAME."
  1418. (file-name-directory (directory-file-name filename)))
  1419. (defun ivy-backward-delete-char ()
  1420. "Forward to `delete-backward-char'.
  1421. Call `ivy-on-del-error-function' if an error occurs, usually when
  1422. there is no more text to delete at the beginning of the
  1423. minibuffer."
  1424. (interactive)
  1425. (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
  1426. (progn
  1427. (ivy--cd (ivy--parent-dir (expand-file-name ivy--directory)))
  1428. (ivy--exhibit))
  1429. (setq prefix-arg current-prefix-arg)
  1430. (condition-case nil
  1431. (call-interactively #'delete-backward-char)
  1432. (error
  1433. (when ivy-on-del-error-function
  1434. (funcall ivy-on-del-error-function))))))
  1435. (defun ivy-delete-char (arg)
  1436. "Forward to `delete-char' ARG."
  1437. (interactive "p")
  1438. (unless (eolp)
  1439. (delete-char arg)))
  1440. (defun ivy-forward-char (arg)
  1441. "Forward to `forward-char' ARG."
  1442. (interactive "p")
  1443. (unless (eolp)
  1444. (forward-char arg)))
  1445. (defun ivy-kill-word (arg)
  1446. "Forward to `kill-word' ARG."
  1447. (interactive "p")
  1448. (unless (eolp)
  1449. (kill-word arg)))
  1450. (defun ivy-kill-line ()
  1451. "Forward to `kill-line'."
  1452. (interactive)
  1453. (if (eolp)
  1454. (kill-region (minibuffer-prompt-end) (point))
  1455. (kill-line)))
  1456. (defun ivy-kill-whole-line ()
  1457. "Forward to `kill-whole-line'."
  1458. (interactive)
  1459. (kill-region (minibuffer-prompt-end) (line-end-position)))
  1460. (defun ivy-backward-kill-word ()
  1461. "Forward to `backward-kill-word'."
  1462. (interactive)
  1463. (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
  1464. (progn
  1465. (ivy--cd (ivy--parent-dir (expand-file-name ivy--directory)))
  1466. (ivy--exhibit))
  1467. (ignore-errors
  1468. (let ((pt (point))
  1469. (last-command (if (eq last-command 'ivy-backward-kill-word)
  1470. 'kill-region
  1471. last-command)))
  1472. (forward-word -1)
  1473. (kill-region pt (point))))))
  1474. (defvar ivy--regexp-quote #'regexp-quote
  1475. "Store the regexp quoting state.")
  1476. (defun ivy-toggle-regexp-quote ()
  1477. "Toggle the regexp quoting."
  1478. (interactive)
  1479. (setq ivy--old-re nil)
  1480. (cl-rotatef ivy--regex-function ivy--regexp-quote)
  1481. (setq ivy--old-text "")
  1482. (setq ivy-regex (funcall ivy--regex-function ivy-text)))
  1483. (defcustom ivy-format-functions-alist
  1484. '((t . ivy-format-function-default))
  1485. "An alist of functions that transform the list of candidates into a string.
  1486. This string is inserted into the minibuffer."
  1487. :type '(alist
  1488. :key-type symbol
  1489. :value-type
  1490. (choice
  1491. (const :tag "Default" ivy-format-function-default)
  1492. (const :tag "Arrow prefix" ivy-format-function-arrow)
  1493. (const :tag "Full line" ivy-format-function-line)
  1494. (function :tag "Custom function"))))
  1495. (defun ivy-sort-file-function-default (x y)
  1496. "Compare two files X and Y.
  1497. Prioritize directories."
  1498. (if (get-text-property 0 'dirp x)
  1499. (if (get-text-property 0 'dirp y)
  1500. (string< (directory-file-name x) (directory-file-name y))
  1501. t)
  1502. (if (get-text-property 0 'dirp y)
  1503. nil
  1504. (string< x y))))
  1505. (defun ivy-string< (x y)
  1506. "Like `string<', but operate on CARs when given cons cells."
  1507. (string< (if (consp x) (car x) x)
  1508. (if (consp y) (car y) y)))
  1509. (define-obsolete-function-alias 'ivy-sort-file-function-using-ido
  1510. 'ido-file-extension-lessp "<2019-10-12 Sat>")
  1511. (defcustom ivy-sort-functions-alist
  1512. '((t . ivy-string<))
  1513. "An alist of sorting functions for each collection function.
  1514. Interactive functions that call completion fit in here as well.
  1515. Nil means no sorting, which is useful to turn off the sorting for
  1516. functions that have candidates in the natural buffer order, like
  1517. `org-refile' or `Man-goto-section'.
  1518. A list can be used to associate multiple sorting functions with a
  1519. collection. The car of the list is the current sort
  1520. function. This list can be rotated with `ivy-rotate-sort'.
  1521. The entry associated with t is used for all fall-through cases.
  1522. See also `ivy-sort-max-size'."
  1523. :type
  1524. '(alist
  1525. :key-type (choice
  1526. (const :tag "Fall-through" t)
  1527. (symbol :tag "Collection"))
  1528. :value-type (choice
  1529. (const :tag "Plain sort" ivy-string<)
  1530. (const :tag "File sort" ivy-sort-file-function-default)
  1531. (const :tag "File sort using Ido" ido-file-extension-lessp)
  1532. (const :tag "No sort" nil)
  1533. (function :tag "Custom function")
  1534. (repeat (function :tag "Custom function")))))
  1535. (defun ivy--sort-function (collection)
  1536. "Retrieve sort function for COLLECTION from `ivy-sort-functions-alist'."
  1537. (let ((entry (cdr (or (assq collection ivy-sort-functions-alist)
  1538. (assq (ivy-state-caller ivy-last) ivy-sort-functions-alist)
  1539. (assq t ivy-sort-functions-alist)))))
  1540. (and (or (functionp entry)
  1541. (functionp (setq entry (car-safe entry))))
  1542. entry)))
  1543. (defun ivy-rotate-sort ()
  1544. "Rotate through sorting functions available for current collection.
  1545. This only has an effect if multiple sorting functions are
  1546. specified for the current collection in
  1547. `ivy-sort-functions-alist'."
  1548. (interactive)
  1549. (let ((cell (or (assq (ivy-state-collection ivy-last) ivy-sort-functions-alist)
  1550. (assq (ivy-state-caller ivy-last) ivy-sort-functions-alist)
  1551. (assq t ivy-sort-functions-alist))))
  1552. (when (consp (cdr cell))
  1553. (setcdr cell (nconc (cddr cell) (list (cadr cell))))
  1554. (ivy--reset-state ivy-last))))
  1555. (defcustom ivy-index-functions-alist
  1556. '((t . ivy-recompute-index-zero))
  1557. "An alist of index recomputing functions for each collection function.
  1558. When the input changes, the appropriate function returns an
  1559. integer - the index of the matched candidate that should be
  1560. selected."
  1561. :type '(alist :key-type symbol :value-type function))
  1562. (defvar ivy-re-builders-alist
  1563. '((t . ivy--regex-plus))
  1564. "An alist of regex building functions for each collection function.
  1565. Each key is (in order of priority):
  1566. 1. The actual collection function, e.g. `read-file-name-internal'.
  1567. 2. The symbol passed by :caller into `ivy-read'.
  1568. 3. `this-command'.
  1569. 4. t.
  1570. Each value is a function that should take a string and return a
  1571. valid regex or a regex sequence (see below).
  1572. Possible choices: `ivy--regex', `regexp-quote',
  1573. `ivy--regex-plus', `ivy--regex-fuzzy', `ivy--regex-ignore-order'.
  1574. If a function returns a list, it should format like this:
  1575. '((\"matching-regexp\" . t) (\"non-matching-regexp\") ...).
  1576. The matches will be filtered in a sequence, you can mix the
  1577. regexps that should match and that should not match as you
  1578. like.")
  1579. (defvar ivy-highlight-functions-alist
  1580. '((ivy--regex-ignore-order . ivy--highlight-ignore-order)
  1581. (ivy--regex-fuzzy . ivy--highlight-fuzzy)
  1582. (ivy--regex-plus . ivy--highlight-default))
  1583. "An alist of highlighting functions for each regex builder function.")
  1584. (defcustom ivy-initial-inputs-alist
  1585. '((org-refile . "^")
  1586. (org-agenda-refile . "^")
  1587. (org-capture-refile . "^")
  1588. (Man-completion-table . "^")
  1589. (woman . "^"))
  1590. "An alist associating commands with their initial input.
  1591. Each cdr is either a string or a function called in the context
  1592. of a call to `ivy-read'."
  1593. :type '(alist
  1594. :key-type (symbol)
  1595. :value-type (choice (string) (function))))
  1596. (defcustom ivy-hooks-alist nil
  1597. "An alist associating commands to setup functions.
  1598. Examples: `toggle-input-method', (lambda () (insert \"^\")), etc.
  1599. May supersede `ivy-initial-inputs-alist'."
  1600. :type '(alist :key-type symbol :value-type function))
  1601. (defvar ivy--occurs-list nil
  1602. "A list of custom occur generators per command.")
  1603. (defun ivy-set-occur (cmd occur)
  1604. "Assign CMD a custom OCCUR function."
  1605. (setq ivy--occurs-list
  1606. (plist-put ivy--occurs-list cmd occur)))
  1607. (defcustom ivy-update-fns-alist nil
  1608. "An alist associating commands to their :update-fn values."
  1609. :type '(alist
  1610. :key-type symbol
  1611. :value-type
  1612. (radio
  1613. (const :tag "Off" nil)
  1614. (const :tag "Call action on change" auto))))
  1615. (defcustom ivy-unwind-fns-alist nil
  1616. "An alist associating commands to their :unwind values."
  1617. :type '(alist :key-type symbol :value-type function))
  1618. (defcustom ivy-init-fns-alist nil
  1619. "An alist associating commands to their :init values.
  1620. An :init is a function with no arguments.
  1621. `ivy-read' calls it to initialize."
  1622. :type '(alist :key-type symbol :value-type function))
  1623. (defun ivy--alist-set (alist-sym key val)
  1624. (let ((curr-val (symbol-value alist-sym))
  1625. (customized-val (get alist-sym 'customized-value))
  1626. (default-val (eval (car (get alist-sym 'standard-value)))))
  1627. ;; when the value was set by `customize-set-variable', don't touch it
  1628. (unless customized-val
  1629. ;; only works if the value wasn't customized by the user
  1630. (when (or (null default-val) (equal curr-val default-val))
  1631. (let ((cell (assoc key curr-val)))
  1632. (if cell
  1633. (setcdr cell val)
  1634. (set alist-sym (cons (cons key val)
  1635. (symbol-value alist-sym)))))
  1636. (when default-val
  1637. (put alist-sym 'standard-value
  1638. (list (list 'quote (symbol-value alist-sym)))))))))
  1639. (declare-function counsel-set-async-exit-code "counsel")
  1640. (defvar ivy--parents-alist nil
  1641. "Configure parent caller for child caller.
  1642. The child caller inherits and can override the settings of the parent.")
  1643. (cl-defun ivy-configure (caller
  1644. &key
  1645. parent
  1646. initial-input
  1647. height
  1648. occur
  1649. update-fn
  1650. init-fn
  1651. unwind-fn
  1652. index-fn
  1653. sort-fn
  1654. format-fn
  1655. display-fn
  1656. display-transformer-fn
  1657. alt-done-fn
  1658. more-chars
  1659. grep-p
  1660. exit-codes)
  1661. "Configure `ivy-read' params for CALLER."
  1662. (declare (indent 1))
  1663. (when parent
  1664. (ivy--alist-set 'ivy--parents-alist caller parent))
  1665. (when initial-input
  1666. (ivy--alist-set 'ivy-initial-inputs-alist caller initial-input))
  1667. (when height
  1668. (ivy--alist-set 'ivy-height-alist caller height))
  1669. (when occur
  1670. (ivy-set-occur caller occur))
  1671. (when update-fn
  1672. (ivy--alist-set 'ivy-update-fns-alist caller update-fn))
  1673. (when unwind-fn
  1674. (ivy--alist-set 'ivy-unwind-fns-alist caller unwind-fn))
  1675. (when init-fn
  1676. (ivy--alist-set 'ivy-init-fns-alist caller init-fn))
  1677. (when index-fn
  1678. (ivy--alist-set 'ivy-index-functions-alist caller index-fn))
  1679. (when sort-fn
  1680. (ivy--alist-set 'ivy-sort-functions-alist caller sort-fn))
  1681. (when format-fn
  1682. (ivy--alist-set 'ivy-format-functions-alist caller format-fn))
  1683. (when display-fn
  1684. (ivy--alist-set 'ivy-display-functions-alist caller display-fn))
  1685. (when display-transformer-fn
  1686. (ivy--alist-set 'ivy--display-transformers-alist caller display-transformer-fn))
  1687. (when alt-done-fn
  1688. (ivy--alist-set 'ivy-alt-done-functions-alist caller alt-done-fn))
  1689. (when more-chars
  1690. (ivy--alist-set 'ivy-more-chars-alist caller more-chars))
  1691. (when grep-p
  1692. (cl-pushnew caller ivy-highlight-grep-commands))
  1693. (when exit-codes
  1694. (let (code msg)
  1695. (while (and (setq code (pop exit-codes))
  1696. (setq msg (pop exit-codes)))
  1697. (counsel-set-async-exit-code caller code msg)))))
  1698. (defcustom ivy-sort-max-size 30000
  1699. "Sorting won't be done for collections larger than this."
  1700. :type 'integer)
  1701. (defalias 'ivy--dirname-p
  1702. ;; Added in Emacs 25.1.
  1703. (if (fboundp 'directory-name-p)
  1704. #'directory-name-p
  1705. (lambda (name)
  1706. "Return non-nil if NAME ends with a directory separator."
  1707. (string-suffix-p "/" name))))
  1708. (defun ivy--sorted-files (dir)
  1709. "Return the list of files in DIR.
  1710. Directories come first."
  1711. (let* ((default-directory dir)
  1712. (seq (condition-case nil
  1713. (mapcar (lambda (s) (replace-regexp-in-string "\\$\\$" "$" s))
  1714. (all-completions "" #'read-file-name-internal
  1715. (ivy-state-predicate ivy-last)))
  1716. (error
  1717. (directory-files dir))))
  1718. sort-fn)
  1719. (setq seq (delete "./" (delete "../" seq)))
  1720. (when (eq (setq sort-fn (ivy--sort-function #'read-file-name-internal))
  1721. #'ivy-sort-file-function-default)
  1722. (setq seq (mapcar (lambda (x)
  1723. (propertize x 'dirp (ivy--dirname-p x)))
  1724. seq)))
  1725. (when sort-fn
  1726. (setq seq (sort seq sort-fn)))
  1727. (dolist (dir ivy-extra-directories)
  1728. (push dir seq))
  1729. (if (string= dir "/")
  1730. (cl-remove-if (lambda (s) (string-match ":$" s)) (delete "../" seq))
  1731. seq)))
  1732. (defun ivy-alist-setting (alist &optional key)
  1733. "Return the value associated with KEY in ALIST, using `assq'.
  1734. KEY defaults to the last caller of `ivy-read'; if no entry is
  1735. found, it falls back to the key t."
  1736. (let ((caller (or key (ivy-state-caller ivy-last))))
  1737. (or
  1738. (and caller (cdr (assq caller alist)))
  1739. (let ((parent (cdr (assq caller ivy--parents-alist))))
  1740. (when parent
  1741. (ivy-alist-setting alist parent)))
  1742. (cdr (assq t alist)))))
  1743. (defun ivy--height (caller)
  1744. (let ((v (or (ivy-alist-setting ivy-height-alist caller)
  1745. ivy-height)))
  1746. (if (integerp v)
  1747. v
  1748. (if (functionp v)
  1749. (funcall v caller)
  1750. (error "Unexpected value: %S" v)))))
  1751. (defun ivy--remove-props (str &rest props)
  1752. "Return STR with text PROPS destructively removed."
  1753. (ignore-errors
  1754. (remove-list-of-text-properties 0 (length str) props str))
  1755. str)
  1756. (defun ivy--update-prompt (prompt)
  1757. (cond ((equal prompt "Keyword, C-h: ")
  1758. ;; auto-insert.el
  1759. "Keyword (C-M-j to end): ")
  1760. (t
  1761. ;; misearch.el
  1762. (replace-regexp-in-string "RET to end" "C-M-j to end" prompt))))
  1763. ;;** Entry Point
  1764. ;;;###autoload
  1765. (cl-defun ivy-read (prompt collection
  1766. &key
  1767. predicate require-match initial-input
  1768. history preselect def keymap update-fn sort
  1769. action multi-action
  1770. unwind re-builder matcher
  1771. dynamic-collection
  1772. extra-props
  1773. caller)
  1774. "Read a string in the minibuffer, with completion.
  1775. PROMPT is a string, normally ending in a colon and a space.
  1776. `ivy-count-format' is prepended to PROMPT during completion.
  1777. COLLECTION is either a list of strings, a function, an alist, or
  1778. a hash table, supplied for `minibuffer-completion-table'.
  1779. PREDICATE is applied to filter out the COLLECTION immediately.
  1780. This argument is for compatibility with `completing-read'.
  1781. When REQUIRE-MATCH is non-nil, only members of COLLECTION can be
  1782. selected.
  1783. If INITIAL-INPUT is non-nil, then insert that input in the
  1784. minibuffer initially.
  1785. HISTORY is a name of a variable to hold the completion session
  1786. history.
  1787. KEYMAP is composed with `ivy-minibuffer-map'.
  1788. PRESELECT, when non-nil, determines which one of the candidates
  1789. matching INITIAL-INPUT to select initially. An integer stands
  1790. for the position of the desired candidate in the collection,
  1791. counting from zero. Otherwise, use the first occurrence of
  1792. PRESELECT in the collection. Comparison is first done with
  1793. `equal'. If that fails, and when applicable, match PRESELECT as
  1794. a regular expression.
  1795. DEF is for compatibility with `completing-read'.
  1796. UPDATE-FN is called each time the candidate list is re-displayed.
  1797. When SORT is non-nil, `ivy-sort-functions-alist' determines how
  1798. to sort candidates before displaying them.
  1799. ACTION is a function to call after selecting a candidate.
  1800. It takes one argument, the selected candidate. If COLLECTION is
  1801. an alist, the argument is a cons cell, otherwise it's a string.
  1802. MULTI-ACTION, when non-nil, is called instead of ACTION when
  1803. there are marked candidates. It takes the list of candidates as
  1804. its only argument. When it's nil, ACTION is called on each marked
  1805. candidate.
  1806. UNWIND is a function of no arguments to call before exiting.
  1807. RE-BUILDER is a function transforming input text into a regex
  1808. pattern.
  1809. MATCHER is a function which can override how candidates are
  1810. filtered based on user input. It takes a regex pattern and a
  1811. list of candidates, and returns the list of matching candidates.
  1812. DYNAMIC-COLLECTION is a boolean specifying whether the list of
  1813. candidates is updated after each input by calling COLLECTION.
  1814. EXTRA-PROPS is a plist that can be used to store
  1815. collection-specific session-specific data.
  1816. CALLER is a symbol to uniquely identify the caller to `ivy-read'.
  1817. It is used, along with COLLECTION, to determine which
  1818. customizations apply to the current completion session."
  1819. (let ((init-fn (ivy-alist-setting ivy-init-fns-alist caller)))
  1820. (when init-fn
  1821. (funcall init-fn)))
  1822. ;; get un-stuck from an existing `read-key' overriding minibuffer keys
  1823. (when (equal overriding-local-map '(keymap))
  1824. (keyboard-quit))
  1825. (setq caller (or caller this-command))
  1826. (let* ((ivy-recursive-last (and (active-minibuffer-window) ivy-last))
  1827. (ivy--display-function
  1828. (when (or ivy-recursive-last
  1829. (not (window-minibuffer-p)))
  1830. (ivy-alist-setting ivy-display-functions-alist caller))))
  1831. (setq update-fn (or update-fn (ivy-alist-setting ivy-update-fns-alist caller)))
  1832. (setq unwind (or unwind (ivy-alist-setting ivy-unwind-fns-alist caller)))
  1833. (setq ivy-last
  1834. (make-ivy-state
  1835. :prompt (ivy--update-prompt prompt)
  1836. :collection collection
  1837. :predicate predicate
  1838. :require-match require-match
  1839. :initial-input initial-input
  1840. :history history
  1841. :preselect preselect
  1842. :keymap keymap
  1843. :update-fn (if (eq update-fn 'auto)
  1844. (lambda ()
  1845. (with-ivy-window
  1846. (funcall
  1847. (ivy--get-action ivy-last)
  1848. (if (consp (car-safe (ivy-state-collection ivy-last)))
  1849. (assoc (ivy-state-current ivy-last)
  1850. (ivy-state-collection ivy-last))
  1851. (ivy-state-current ivy-last)))))
  1852. update-fn)
  1853. :sort sort
  1854. :action (ivy--compute-extra-actions action caller)
  1855. :multi-action multi-action
  1856. :frame (selected-frame)
  1857. :window (selected-window)
  1858. :buffer (current-buffer)
  1859. :unwind unwind
  1860. :re-builder re-builder
  1861. :matcher matcher
  1862. :dynamic-collection dynamic-collection
  1863. :display-transformer-fn (ivy-alist-setting ivy--display-transformers-alist caller)
  1864. :directory default-directory
  1865. :extra-props extra-props
  1866. :caller caller
  1867. :def def))
  1868. (ivy--reset-state ivy-last)
  1869. (unwind-protect
  1870. (minibuffer-with-setup-hook
  1871. #'ivy--minibuffer-setup
  1872. (let* ((hist (or history 'ivy-history))
  1873. (minibuffer-completion-table collection)
  1874. (minibuffer-completion-predicate predicate)
  1875. (ivy-height (ivy--height caller))
  1876. (resize-mini-windows (unless (display-graphic-p)
  1877. 'grow-only)))
  1878. (if (and ivy-auto-select-single-candidate
  1879. ivy--all-candidates
  1880. (null (cdr ivy--all-candidates)))
  1881. (progn
  1882. (setf (ivy-state-current ivy-last)
  1883. (car ivy--all-candidates))
  1884. (setq ivy-exit 'done))
  1885. (condition-case err
  1886. (read-from-minibuffer
  1887. prompt
  1888. (ivy-state-initial-input ivy-last)
  1889. (make-composed-keymap keymap ivy-minibuffer-map)
  1890. nil
  1891. hist)
  1892. (error
  1893. (unless (equal err '(error "Selecting deleted buffer"))
  1894. (signal (car err) (cdr err))))))
  1895. (when (eq ivy-exit 'done)
  1896. (ivy--update-history hist))))
  1897. (let ((session (or (plist-get extra-props :session)
  1898. (unless (or (minibufferp)
  1899. (null (ivy-state-action ivy-last))
  1900. (eq (ivy--get-action ivy-last) #'identity))
  1901. caller))))
  1902. (when session
  1903. (setf (ivy-state-extra-props ivy-last)
  1904. (plist-put extra-props :ivy-data `(:all-candidates ,ivy--all-candidates
  1905. :text ,ivy-text)))
  1906. (ivy--alist-set 'ivy--sessions session ivy-last)))
  1907. (ivy--cleanup))
  1908. (ivy-call)))
  1909. (defun ivy--update-history (hist)
  1910. (let ((item
  1911. (if (or (string= ivy-text "")
  1912. (eq
  1913. (plist-get (ivy-state-extra-props ivy-last) :caller)
  1914. 'ivy-completing-read)
  1915. (eq (ivy-state-history ivy-last) 'file-name-history))
  1916. (ivy-state-current ivy-last)
  1917. ivy-text)))
  1918. (cond ((equal item ""))
  1919. ((stringp item)
  1920. (set hist (cons (propertize item 'ivy-index ivy--index)
  1921. (delete item (symbol-value hist))))))))
  1922. (defun ivy--cleanup ()
  1923. ;; Fixes a bug in ESS, #1660
  1924. (put 'post-command-hook 'permanent-local nil)
  1925. (remove-hook 'post-command-hook #'ivy--queue-exhibit)
  1926. (remove-hook 'window-size-change-functions #'ivy--window-size-changed)
  1927. (let ((cleanup (ivy--display-function-prop :cleanup))
  1928. (unwind (ivy-state-unwind ivy-last)))
  1929. (when (functionp cleanup)
  1930. (funcall cleanup))
  1931. (when unwind
  1932. (funcall unwind)))
  1933. (ivy--pulse-cleanup)
  1934. (unless (eq ivy-exit 'done)
  1935. (ivy-recursive-restore)))
  1936. (defun ivy--display-function-prop (prop)
  1937. "Return PROP associated with current `ivy--display-function'."
  1938. (plist-get (cdr (assq ivy--display-function
  1939. ivy-display-functions-props))
  1940. prop))
  1941. (defvar Info-complete-menu-buffer)
  1942. (defun ivy--reset-state (state)
  1943. "Reset the ivy to STATE.
  1944. This is useful for recursive `ivy-read'."
  1945. (setq ivy-marked-candidates nil)
  1946. (unless (equal (selected-frame) (ivy-state-frame state))
  1947. (select-window (active-minibuffer-window)))
  1948. (let* ((prompt (or (ivy-state-prompt state) ""))
  1949. (collection (ivy-state-collection state))
  1950. (predicate (ivy-state-predicate state))
  1951. (history (ivy-state-history state))
  1952. (preselect (ivy-state-preselect state))
  1953. (re-builder (ivy-state-re-builder state))
  1954. (dynamic-collection (ivy-state-dynamic-collection state))
  1955. (require-match (ivy-state-require-match state))
  1956. (caller (or (ivy-state-caller state) this-command))
  1957. (sort (or (ivy-state-sort state) (assoc caller ivy-sort-functions-alist)))
  1958. (initial-input
  1959. (or (ivy-state-initial-input state)
  1960. (let ((init (ivy-alist-setting ivy-initial-inputs-alist caller)))
  1961. (if (functionp init) (funcall init) init))))
  1962. (def (ivy-state-def state)))
  1963. (when (and (eq caller 'swiper-isearch) (buffer-modified-p))
  1964. (setq preselect nil))
  1965. (setq ivy--extra-candidates (ivy--compute-extra-candidates caller))
  1966. (setq ivy--directory nil)
  1967. (setq ivy--directory-hist (list default-directory))
  1968. (setq ivy-case-fold-search ivy-case-fold-search-default)
  1969. (setf (ivy-state-re-builder ivy-last)
  1970. (setq ivy--regex-function
  1971. (or re-builder
  1972. (and (functionp collection)
  1973. (cdr (assq collection ivy-re-builders-alist)))
  1974. (ivy-alist-setting ivy-re-builders-alist)
  1975. #'ivy--regex)))
  1976. (setq ivy--subexps 0)
  1977. (setq ivy--regexp-quote #'regexp-quote)
  1978. (setq ivy--old-text "")
  1979. (setq ivy--full-length nil)
  1980. (ivy-set-text (or initial-input ""))
  1981. (setq ivy--index 0)
  1982. (setq ivy-calling nil)
  1983. (setq ivy-use-ignore ivy-use-ignore-default)
  1984. (setf (ivy-state-ignore state) ivy-use-ignore)
  1985. (setq ivy--highlight-function
  1986. (or (cdr (assq (ivy-alist-setting ivy-re-builders-alist)
  1987. ivy-highlight-functions-alist))
  1988. #'ivy--highlight-default))
  1989. (let ((ivy-recursive-restore nil)
  1990. coll sort-fn)
  1991. (cond ((eq collection #'Info-read-node-name-1)
  1992. (setq coll
  1993. (if (equal (bound-and-true-p Info-current-file) "dir")
  1994. (mapcar (lambda (x) (format "(%s)" x))
  1995. (delete-dups
  1996. (all-completions "(" collection predicate)))
  1997. (all-completions "" collection predicate))))
  1998. ((memq collection '(read-file-name-internal ffap-read-file-or-url-internal))
  1999. (require 'tramp)
  2000. (when (and (equal def initial-input)
  2001. (member "./" ivy-extra-directories))
  2002. (setq def nil))
  2003. (setq ivy--directory default-directory)
  2004. (when (and initial-input
  2005. (not (equal initial-input "")))
  2006. (cond ((file-directory-p initial-input)
  2007. (when (equal (file-name-nondirectory initial-input) "")
  2008. (setf (ivy-state-preselect state) (setq preselect nil))
  2009. (setq def nil))
  2010. (setq ivy--directory (file-name-as-directory initial-input))
  2011. (setq initial-input nil)
  2012. (when preselect
  2013. (let ((preselect-directory
  2014. (file-name-directory preselect)))
  2015. (when (and preselect-directory
  2016. (not (equal
  2017. (expand-file-name
  2018. preselect-directory)
  2019. (expand-file-name ivy--directory))))
  2020. (setf (ivy-state-preselect state)
  2021. (setq preselect nil))))))
  2022. ((ignore-errors
  2023. (file-exists-p (file-name-directory initial-input)))
  2024. (setq ivy--directory (file-name-directory initial-input))
  2025. (setf (ivy-state-preselect state)
  2026. (file-name-nondirectory initial-input)))))
  2027. (require 'dired)
  2028. (when preselect
  2029. (let ((preselect-directory (ivy--parent-dir preselect)))
  2030. (when (and preselect-directory
  2031. (not (string= preselect-directory
  2032. default-directory)))
  2033. (setq ivy--directory preselect-directory))
  2034. (setq preselect (file-relative-name preselect
  2035. preselect-directory))
  2036. (setf (ivy-state-preselect state) preselect)))
  2037. (setq sort nil)
  2038. (setq coll (ivy--sorted-files ivy--directory))
  2039. (when initial-input
  2040. (unless (or require-match
  2041. (equal initial-input default-directory)
  2042. (equal initial-input ""))
  2043. (setq coll (cons initial-input coll)))
  2044. (setq initial-input (file-name-nondirectory initial-input))))
  2045. ((eq collection #'internal-complete-buffer)
  2046. (setq coll (ivy--buffer-list
  2047. ""
  2048. (and ivy-use-virtual-buffers
  2049. (member caller '(ivy-switch-buffer
  2050. ivy-switch-buffer-other-window
  2051. counsel-switch-buffer)))
  2052. predicate)))
  2053. (dynamic-collection
  2054. (setq coll (if (and (eq this-command 'ivy-resume) (not (buffer-modified-p)))
  2055. ivy--all-candidates
  2056. (ivy--dynamic-collection-cands (or initial-input "")))))
  2057. ((consp (car-safe collection))
  2058. (setq collection (cl-remove-if-not predicate collection))
  2059. (when (and sort (setq sort-fn (ivy--sort-function caller)))
  2060. (setq collection (sort (copy-sequence collection) sort-fn))
  2061. (setq sort nil))
  2062. (setf (ivy-state-collection ivy-last) collection)
  2063. (setq coll (let ((i -1))
  2064. (mapcar (lambda (x)
  2065. (propertize x 'idx (cl-incf i)))
  2066. (all-completions "" collection)))))
  2067. ((or (functionp collection)
  2068. (byte-code-function-p collection)
  2069. (vectorp collection)
  2070. (hash-table-p collection)
  2071. (and (listp collection) (symbolp (car collection))))
  2072. (let ((Info-complete-menu-buffer
  2073. ;; FIXME: This is a temporary workaround for issue #1803.
  2074. (or (bound-and-true-p Info-complete-menu-buffer)
  2075. (ivy-state-buffer state))))
  2076. (setq coll (all-completions "" collection predicate))))
  2077. (t
  2078. (setq coll (all-completions "" collection predicate))))
  2079. (unless (ivy-state-dynamic-collection ivy-last)
  2080. (setq coll (delete "" coll)))
  2081. (when (and sort
  2082. (or (functionp collection)
  2083. (not (eq history 'org-refile-history)))
  2084. (setq sort-fn (ivy--sort-function
  2085. (if (functionp collection) collection caller)))
  2086. (listp coll)
  2087. (null (nthcdr ivy-sort-max-size coll)))
  2088. (setq coll (sort (copy-sequence coll) sort-fn)))
  2089. (when def
  2090. (cond ((stringp (car-safe def))
  2091. (setq coll
  2092. (delete-dups
  2093. (append def coll))))
  2094. ((and (stringp def) (not (member def coll)))
  2095. (push def coll))))
  2096. (setq coll (ivy--set-candidates coll))
  2097. (setq ivy--old-re nil)
  2098. (setq ivy--old-cands nil)
  2099. (when initial-input
  2100. ;; Needed for anchor to work
  2101. (setq ivy--old-cands coll)
  2102. (setq ivy--old-cands (ivy--filter initial-input coll)))
  2103. (unless (setq ivy--trying-to-resume-dynamic-collection
  2104. (and preselect dynamic-collection))
  2105. (when (integerp preselect)
  2106. (setq ivy--old-re "")
  2107. (ivy-set-index preselect)))
  2108. (setq ivy--all-candidates coll)
  2109. (unless (integerp preselect)
  2110. (ivy-set-index (or
  2111. (and dynamic-collection
  2112. ivy--index)
  2113. (and preselect
  2114. (ivy--preselect-index
  2115. preselect
  2116. (if initial-input
  2117. ivy--old-cands
  2118. coll)))
  2119. 0))))
  2120. (setq ivy-exit nil)
  2121. (setq ivy--default
  2122. (if (region-active-p)
  2123. (buffer-substring (region-beginning) (region-end))
  2124. (ivy-thing-at-point)))
  2125. (setq ivy--prompt (ivy-add-prompt-count (ivy--quote-format-string prompt)))
  2126. (setq ivy--use-selectable-prompt (ivy--prompt-selectable-p))
  2127. (setf (ivy-state-initial-input ivy-last) initial-input)))
  2128. (defun ivy-add-prompt-count (prompt)
  2129. "Add count information to PROMPT."
  2130. (cond ((null ivy-count-format)
  2131. (error "`ivy-count-format' must not be nil; set it to \"\" instead"))
  2132. ((string-match "%d.*\\(%d\\)" ivy-count-format)
  2133. (let* ((w
  2134. (if (listp ivy--all-candidates)
  2135. (1+ (floor (log (max 1 (length ivy--all-candidates)) 10)))
  2136. 1))
  2137. (s (replace-match (format "%%-%dd" w) t t ivy-count-format 1)))
  2138. (string-match "%d" s)
  2139. (concat (replace-match (format "%%%dd" w) t t s)
  2140. prompt)))
  2141. ((string-match-p "%.*d" ivy-count-format)
  2142. (concat ivy-count-format prompt))
  2143. (t
  2144. prompt)))
  2145. (defun ivy--quote-format-string (str)
  2146. "Make STR suitable for `format' with no extra arguments."
  2147. (replace-regexp-in-string "%" "%%" str t t))
  2148. ;;;###autoload
  2149. (defun ivy-completing-read (prompt collection
  2150. &optional predicate require-match initial-input
  2151. history def inherit-input-method)
  2152. "Read a string in the minibuffer, with completion.
  2153. This interface conforms to `completing-read' and can be used for
  2154. `completing-read-function'.
  2155. PROMPT is a string that normally ends in a colon and a space.
  2156. COLLECTION is either a list of strings, an alist, an obarray, or a hash table.
  2157. PREDICATE limits completion to a subset of COLLECTION.
  2158. REQUIRE-MATCH is a boolean value or a symbol. See `completing-read'.
  2159. INITIAL-INPUT is a string inserted into the minibuffer initially.
  2160. HISTORY is a list of previously selected inputs.
  2161. DEF is the default value.
  2162. INHERIT-INPUT-METHOD is currently ignored."
  2163. (let ((handler
  2164. (and (< ivy-completing-read-ignore-handlers-depth (minibuffer-depth))
  2165. (assq this-command ivy-completing-read-handlers-alist))))
  2166. (if handler
  2167. (let ((completion-in-region-function #'completion--in-region)
  2168. (ivy-completing-read-ignore-handlers-depth (1+ (minibuffer-depth))))
  2169. (funcall (cdr handler)
  2170. prompt collection
  2171. predicate require-match
  2172. initial-input history
  2173. def inherit-input-method))
  2174. ;; See the doc of `completing-read'.
  2175. (when (consp history)
  2176. (when (numberp (cdr history))
  2177. (setq initial-input (nth (1- (cdr history))
  2178. (symbol-value (car history)))))
  2179. (setq history (car history)))
  2180. (when (consp def)
  2181. (setq def (car def)))
  2182. (let ((str (ivy-read
  2183. prompt collection
  2184. :predicate predicate
  2185. :require-match (when (and collection require-match)
  2186. require-match)
  2187. :initial-input (cond ((consp initial-input)
  2188. (car initial-input))
  2189. ((and (stringp initial-input)
  2190. (not (eq collection #'read-file-name-internal))
  2191. (string-match-p "\\+" initial-input))
  2192. (replace-regexp-in-string
  2193. "\\+" "\\\\+" initial-input))
  2194. (t
  2195. initial-input))
  2196. :preselect def
  2197. :def def
  2198. :history history
  2199. :keymap nil
  2200. :dynamic-collection ivy-completing-read-dynamic-collection
  2201. :extra-props '(:caller ivy-completing-read)
  2202. :caller (if (and collection (symbolp collection))
  2203. collection
  2204. this-command))))
  2205. (if (string= str "")
  2206. ;; For `completing-read' compat, return the first element of
  2207. ;; DEFAULT, if it is a list; "", if DEFAULT is nil; or DEFAULT.
  2208. (or def "")
  2209. str)))))
  2210. (defun ivy-completing-read-with-empty-string-def
  2211. (prompt collection
  2212. &optional predicate require-match initial-input
  2213. history def inherit-input-method)
  2214. "Same as `ivy-completing-read' but with different handling of DEF.
  2215. Specifically, if DEF is nil, it is treated the same as if DEF was
  2216. the empty string. This mimics the behavior of
  2217. `completing-read-default'. This function can therefore be used in
  2218. place of `ivy-completing-read' for commands that rely on this
  2219. behavior."
  2220. (ivy-completing-read
  2221. prompt collection predicate require-match initial-input
  2222. history (or def "") inherit-input-method))
  2223. (declare-function mc/all-fake-cursors "ext:multiple-cursors-core")
  2224. (defun ivy-completion-in-region-action (str)
  2225. "Insert STR, erasing the previous one.
  2226. The previous string is between `ivy-completion-beg' and `ivy-completion-end'."
  2227. (when (consp str)
  2228. (setq str (cdr str)))
  2229. (when (stringp str)
  2230. (let ((fake-cursors (and (require 'multiple-cursors-core nil t)
  2231. (mc/all-fake-cursors)))
  2232. (pt (point))
  2233. (beg ivy-completion-beg)
  2234. (end ivy-completion-end))
  2235. (when beg
  2236. (delete-region beg end))
  2237. (setq ivy-completion-beg (point))
  2238. (insert (substring-no-properties str))
  2239. (completion--done str 'exact)
  2240. (setq ivy-completion-end (point))
  2241. (save-excursion
  2242. (dolist (cursor fake-cursors)
  2243. (goto-char (overlay-start cursor))
  2244. (delete-region (+ (point) (- beg pt))
  2245. (+ (point) (- end pt)))
  2246. (insert (substring-no-properties str))
  2247. ;; manually move the fake cursor
  2248. (move-overlay cursor (point) (1+ (point)))
  2249. (set-marker (overlay-get cursor 'point) (point))
  2250. (set-marker (overlay-get cursor 'mark) (point)))))))
  2251. (defun ivy-completion-common-length (str)
  2252. "Return the amount of characters that match in STR.
  2253. `completion-all-completions' computes this and returns the result
  2254. via text properties.
  2255. The first non-matching part is propertized:
  2256. - either with: (face (completions-first-difference))
  2257. - or: (font-lock-face completions-first-difference)."
  2258. (let ((char-property-alias-alist '((face font-lock-face)))
  2259. (i (1- (length str))))
  2260. (catch 'done
  2261. (while (>= i 0)
  2262. (when (equal (get-text-property i 'face str)
  2263. '(completions-first-difference))
  2264. (throw 'done i))
  2265. (cl-decf i))
  2266. (throw 'done (length str)))))
  2267. (defun ivy-completion-in-region (start end collection &optional predicate)
  2268. "An Ivy function suitable for `completion-in-region-function'.
  2269. The function completes the text between START and END using COLLECTION.
  2270. PREDICATE (a function called with no arguments) says when to exit.
  2271. See `completion-in-region' for further information."
  2272. (let* ((enable-recursive-minibuffers t)
  2273. (str (buffer-substring-no-properties start end))
  2274. (completion-ignore-case (ivy--case-fold-p str))
  2275. (comps
  2276. (completion-all-completions str collection predicate (- end start))))
  2277. (cond ((null comps)
  2278. (message "No matches"))
  2279. ((progn
  2280. (nconc comps nil)
  2281. (and (null (cdr comps))
  2282. (string= str (car comps))))
  2283. (message "Sole match"))
  2284. (t
  2285. (when (eq collection 'crm--collection-fn)
  2286. (setq comps (delete-dups comps)))
  2287. (let* ((len (ivy-completion-common-length (car comps)))
  2288. (initial (cond ((= len 0)
  2289. "")
  2290. ((let ((str-len (length str)))
  2291. (when (> len str-len)
  2292. (setq len str-len)
  2293. str)))
  2294. (t
  2295. (substring str (- len))))))
  2296. (delete-region (- end len) end)
  2297. (setq ivy-completion-beg (- end len))
  2298. (setq ivy-completion-end ivy-completion-beg)
  2299. (if (null (cdr comps))
  2300. (progn
  2301. (unless (minibuffer-window-active-p (selected-window))
  2302. (setf (ivy-state-window ivy-last) (selected-window)))
  2303. (ivy-completion-in-region-action
  2304. (substring-no-properties (car comps))))
  2305. (dolist (s comps)
  2306. ;; Remove face `completions-first-difference'.
  2307. (ivy--remove-props s 'face))
  2308. (setq ivy--old-re nil)
  2309. (unless (ivy--filter initial comps)
  2310. (setq initial nil)
  2311. (setq predicate nil)
  2312. (setq collection comps))
  2313. (unless (derived-mode-p #'emacs-lisp-mode)
  2314. (setq collection comps)
  2315. (setq predicate nil))
  2316. (ivy-read (format "(%s): " str) collection
  2317. :predicate predicate
  2318. :initial-input (concat
  2319. (and (derived-mode-p #'emacs-lisp-mode)
  2320. "^")
  2321. initial)
  2322. :action #'ivy-completion-in-region-action
  2323. :unwind (lambda ()
  2324. (unless (eq ivy-exit 'done)
  2325. (goto-char ivy-completion-beg)
  2326. (when initial
  2327. (insert initial))))
  2328. :caller 'ivy-completion-in-region)))
  2329. ;; Return value should be non-nil on valid completion;
  2330. ;; see `completion-in-region'.
  2331. t))))
  2332. (defun ivy-completion-in-region-prompt ()
  2333. "Prompt function for `ivy-completion-in-region'.
  2334. See `ivy-set-prompt'."
  2335. (and (window-minibuffer-p (ivy-state-window ivy-last))
  2336. (ivy-add-prompt-count (ivy-state-prompt ivy-last))))
  2337. (ivy-set-prompt #'ivy-completion-in-region #'ivy-completion-in-region-prompt)
  2338. (defcustom ivy-do-completion-in-region t
  2339. "When non-nil `ivy-mode' will set `completion-in-region-function'."
  2340. :type 'boolean)
  2341. (defvar ivy--old-crf nil
  2342. "Store previous value of `completing-read-function'.")
  2343. (defvar ivy--old-cirf nil
  2344. "Store previous value of `completion-in-region-function'.")
  2345. ;;;###autoload
  2346. (define-minor-mode ivy-mode
  2347. "Toggle Ivy mode on or off.
  2348. Turn Ivy mode on if ARG is positive, off otherwise.
  2349. Turning on Ivy mode sets `completing-read-function' to
  2350. `ivy-completing-read'.
  2351. Global bindings:
  2352. \\{ivy-mode-map}
  2353. Minibuffer bindings:
  2354. \\{ivy-minibuffer-map}"
  2355. :group 'ivy
  2356. :global t
  2357. :keymap ivy-mode-map
  2358. :lighter " ivy"
  2359. (if ivy-mode
  2360. (progn
  2361. (unless (eq completing-read-function #'ivy-completing-read)
  2362. (setq ivy--old-crf completing-read-function)
  2363. (setq completing-read-function #'ivy-completing-read))
  2364. (when ivy-do-completion-in-region
  2365. (unless (eq completion-in-region-function #'ivy-completion-in-region)
  2366. (setq ivy--old-cirf completion-in-region-function)
  2367. (setq completion-in-region-function #'ivy-completion-in-region))))
  2368. (when (eq completing-read-function #'ivy-completing-read)
  2369. (setq completing-read-function (or ivy--old-crf
  2370. #'completing-read-default))
  2371. (setq ivy--old-crf nil))
  2372. (when (eq completion-in-region-function #'ivy-completion-in-region)
  2373. (setq completion-in-region-function (or ivy--old-cirf
  2374. #'completion--in-region))
  2375. (setq ivy--old-cirf nil))))
  2376. (defun ivy--preselect-index (preselect candidates)
  2377. "Return the index of PRESELECT in CANDIDATES."
  2378. (or (cond ((integerp preselect)
  2379. (if (integerp (car candidates))
  2380. (cl-position preselect candidates)
  2381. preselect))
  2382. ((cl-position preselect candidates :test #'equal))
  2383. ((ivy--regex-p preselect)
  2384. (cl-position preselect candidates :test #'string-match-p)))
  2385. 0))
  2386. ;;* Implementation
  2387. ;;** Regex
  2388. (defun ivy-re-match (re-seq str)
  2389. "Return non-nil if RE-SEQ is matched by STR.
  2390. RE-SEQ is a list of (RE . MATCH-P).
  2391. RE is a regular expression.
  2392. MATCH-P is t when RE should match STR and nil when RE should not
  2393. match STR.
  2394. Each element of RE-SEQ must match for the function to return true.
  2395. This concept is used to generalize regular expressions for
  2396. `ivy--regex-plus' and `ivy--regex-ignore-order'."
  2397. (let ((res t)
  2398. re)
  2399. (while (and res (setq re (pop re-seq)))
  2400. (setq res
  2401. (if (cdr re)
  2402. (string-match-p (car re) str)
  2403. (not (string-match-p (car re) str)))))
  2404. res))
  2405. (defvar ivy--regex-hash
  2406. (make-hash-table :test #'equal)
  2407. "Store pre-computed regex.")
  2408. (defvar ivy--input-garbage nil)
  2409. (defun ivy--split (str)
  2410. "Split STR into list of substrings bounded by spaces.
  2411. Single spaces act as splitting points. Consecutive spaces
  2412. \"quote\" their preceding spaces, i.e., guard them from being
  2413. split. This allows the literal interpretation of N spaces by
  2414. inputting N+1 spaces. Any substring not constituting a valid
  2415. regexp is passed to `regexp-quote'."
  2416. (let ((len (length str))
  2417. (i 0)
  2418. (start 0)
  2419. (res nil)
  2420. match-len
  2421. end
  2422. c)
  2423. (catch 'break
  2424. (while (< i len)
  2425. (setq c (aref str i))
  2426. (cond ((= ?\[ c)
  2427. (if (setq end (ivy--match-regex-brackets
  2428. (substring str i)))
  2429. (cl-incf i end)
  2430. (setq ivy--input-garbage (substring str i))
  2431. (throw 'break nil)))
  2432. ((= ?\\ c)
  2433. (if (and (< (1+ i) len) (= ?\( (aref str (1+ i))))
  2434. (progn
  2435. (when (> i start)
  2436. (push (substring str start i) res))
  2437. (if (eq (string-match "\\\\([^\0]*?\\\\)" str i) i)
  2438. (progn
  2439. (push (match-string 0 str) res)
  2440. (setq i (match-end 0))
  2441. (setq start i))
  2442. (setq ivy--input-garbage (substring str i))
  2443. (throw 'break nil)))
  2444. (cl-incf i)))
  2445. ((= ?\ c)
  2446. (string-match " +" str i)
  2447. (setq match-len (- (match-end 0) (match-beginning 0)))
  2448. (if (= match-len 1)
  2449. (progn
  2450. (when (> i start)
  2451. (push (substring str start i) res))
  2452. (setq start (1+ i)))
  2453. (setq str (replace-match
  2454. (make-string (1- match-len) ?\ )
  2455. nil nil str))
  2456. (setq len (length str))
  2457. (cl-incf i (1- match-len)))
  2458. (cl-incf i))
  2459. (t
  2460. (cl-incf i)))))
  2461. (when (< start i)
  2462. (push (substring str start) res))
  2463. (mapcar #'ivy--regex-or-literal (nreverse res))))
  2464. (defun ivy--match-regex-brackets (str)
  2465. (let ((len (length str))
  2466. (i 1)
  2467. (open-count 1)
  2468. c)
  2469. (while (and (< i len)
  2470. (> open-count 0))
  2471. (setq c (aref str i))
  2472. (cond ((= c ?\[)
  2473. (cl-incf open-count))
  2474. ((= c ?\])
  2475. (cl-decf open-count)))
  2476. (cl-incf i))
  2477. (when (= open-count 0)
  2478. (if (eq (string-match "[+*?]" str i) i)
  2479. (match-end 0)
  2480. i))))
  2481. (defun ivy--trim-trailing-re (regex)
  2482. "Trim incomplete REGEX.
  2483. If REGEX ends with \\|, trim it, since then it matches an empty string."
  2484. (if (string-match "\\`\\(.*\\)[\\]|\\'" regex)
  2485. (match-string 1 regex)
  2486. regex))
  2487. (defun ivy--regex (str &optional greedy)
  2488. "Re-build regex pattern from STR in case it has a space.
  2489. When GREEDY is non-nil, join words in a greedy way."
  2490. (let ((hashed (unless greedy
  2491. (gethash str ivy--regex-hash))))
  2492. (if hashed
  2493. (progn
  2494. (setq ivy--subexps (car hashed))
  2495. (cdr hashed))
  2496. (when (string-match-p "\\(?:[^\\]\\|^\\)\\\\\\'" str)
  2497. (setq str (substring str 0 -1)))
  2498. (setq str (ivy--trim-trailing-re str))
  2499. (cdr (puthash str
  2500. (let ((subs (ivy--split str)))
  2501. (if (= (length subs) 1)
  2502. (cons
  2503. (setq ivy--subexps 0)
  2504. (if (string-match-p "\\`\\.[^.]" (car subs))
  2505. (concat "\\." (substring (car subs) 1))
  2506. (car subs)))
  2507. (cons
  2508. (setq ivy--subexps (length subs))
  2509. (replace-regexp-in-string
  2510. "\\.\\*\\??\\\\( "
  2511. "\\( "
  2512. (mapconcat
  2513. (lambda (x)
  2514. (if (string-match-p "\\`\\\\([^?][^\0]*\\\\)\\'" x)
  2515. x
  2516. (format "\\(%s\\)" x)))
  2517. subs
  2518. (if greedy ".*" ".*?"))
  2519. nil t))))
  2520. ivy--regex-hash)))))
  2521. (defun ivy--regex-p (object)
  2522. "Return OBJECT if it is a valid regular expression, else nil."
  2523. (ignore-errors (string-match-p object "") object))
  2524. (defun ivy--regex-or-literal (str)
  2525. "If STR isn't a legal regexp, escape it."
  2526. (or (ivy--regex-p str) (regexp-quote str)))
  2527. (defun ivy--split-negation (str)
  2528. "Split STR into text before and after ! delimiter.
  2529. Do not split if the delimiter is escaped as \\!.
  2530. Assumes there is at most one un-escaped delimiter and discards
  2531. text after delimiter if it is empty. Modifies match data."
  2532. (unless (string= str "")
  2533. (let ((delim "\\(?:\\`\\|[^\\]\\)\\(!\\)"))
  2534. (mapcar (lambda (split)
  2535. ;; Store "\!" as "!".
  2536. (replace-regexp-in-string "\\\\!" "!" split t t))
  2537. (if (string-match delim str)
  2538. ;; Ignore everything past first un-escaped ! rather than
  2539. ;; crashing. We can't warn or error because the minibuffer is
  2540. ;; already active.
  2541. (let* ((i (match-beginning 1))
  2542. (j (and (string-match delim str (1+ i))
  2543. (match-beginning 1)))
  2544. (neg (substring str (1+ i) j)))
  2545. (cons (substring str 0 i)
  2546. (and (not (string= neg ""))
  2547. (list neg))))
  2548. (list str))))))
  2549. (defun ivy--split-spaces (str)
  2550. "Split STR on spaces, unless they're preceded by \\.
  2551. No un-escaped spaces are left in the output. Any substring not
  2552. constituting a valid regexp is passed to `regexp-quote'."
  2553. (when str
  2554. (let ((i 0) ; End of last search.
  2555. (j 0) ; End of last delimiter.
  2556. parts)
  2557. (while (string-match "\\(\\\\ \\)\\| +" str i)
  2558. (setq i (match-end 0))
  2559. (if (not (match-beginning 1))
  2560. ;; Un-escaped space(s).
  2561. (let ((delim (match-beginning 0)))
  2562. (when (< j delim)
  2563. (push (substring str j delim) parts))
  2564. (setq j i))
  2565. ;; Store "\ " as " ".
  2566. (setq str (replace-match " " t t str 1))
  2567. (setq i (1- i))))
  2568. (when (< j (length str))
  2569. (push (substring str j) parts))
  2570. (mapcar #'ivy--regex-or-literal (nreverse parts)))))
  2571. (defun ivy--regex-ignore-order (str)
  2572. "Re-build regex from STR by splitting at spaces and using ! for negation.
  2573. Examples:
  2574. foo -> matches \"foo\"
  2575. foo bar -> matches if both \"foo\" and \"bar\" match (any order)
  2576. foo !bar -> matches if \"foo\" matches and \"bar\" does not match
  2577. foo !bar baz -> matches if \"foo\" matches and neither \"bar\" nor \"baz\" match
  2578. foo[a-z] -> matches \"foo[a-z]\"
  2579. Escaping examples:
  2580. foo\\!bar -> matches \"foo!bar\"
  2581. foo\\ bar -> matches \"foo bar\"
  2582. Returns a list suitable for `ivy-re-match'."
  2583. (setq str (ivy--trim-trailing-re str))
  2584. (let* (regex-parts
  2585. (raw-parts (ivy--split-negation str)))
  2586. (dolist (part (ivy--split-spaces (car raw-parts)))
  2587. (push (cons part t) regex-parts))
  2588. (when (cdr raw-parts)
  2589. (dolist (part (ivy--split-spaces (cadr raw-parts)))
  2590. (push (cons part nil) regex-parts)))
  2591. (if regex-parts (nreverse regex-parts)
  2592. "")))
  2593. (defun ivy--regex-plus (str)
  2594. "Build a regex sequence from STR.
  2595. Spaces are wild card characters, everything before \"!\" should
  2596. match. Everything after \"!\" should not match."
  2597. (let ((parts (ivy--split-negation str)))
  2598. (cl-case (length parts)
  2599. (0
  2600. "")
  2601. (1
  2602. (if (= (aref str 0) ?!)
  2603. (list (cons "" t)
  2604. (list (ivy--regex (car parts))))
  2605. (ivy--regex (car parts))))
  2606. (2
  2607. (cons
  2608. (cons (ivy--regex (car parts)) t)
  2609. (mapcar #'list (split-string (cadr parts) " " t))))
  2610. (t (error "Unexpected: use only one !")))))
  2611. (defun ivy--regex-fuzzy (str)
  2612. "Build a regex sequence from STR.
  2613. Insert .* between each char."
  2614. (setq str (ivy--trim-trailing-re str))
  2615. (if (string-match "\\`\\(\\^?\\)\\(.*?\\)\\(\\$?\\)\\'" str)
  2616. (prog1
  2617. (concat (match-string 1 str)
  2618. (let ((lst (string-to-list (match-string 2 str))))
  2619. (apply #'concat
  2620. (cl-mapcar
  2621. #'concat
  2622. (cons "" (cdr (mapcar (lambda (c) (format "[^%c\n]*" c))
  2623. lst)))
  2624. (mapcar (lambda (x) (format "\\(%s\\)" (regexp-quote (char-to-string x))))
  2625. lst))))
  2626. (match-string 3 str))
  2627. (setq ivy--subexps (length (match-string 2 str))))
  2628. str))
  2629. (defcustom ivy-fixed-height-minibuffer nil
  2630. "When non nil, fix the height of the minibuffer during ivy completion.
  2631. This effectively sets the minimum height at this level to `ivy-height' and
  2632. tries to ensure that it does not change depending on the number of candidates."
  2633. :type 'boolean)
  2634. ;;** Rest
  2635. (defcustom ivy-truncate-lines t
  2636. "Minibuffer setting for `truncate-lines'."
  2637. :type 'boolean)
  2638. (defun ivy--minibuffer-setup ()
  2639. "Setup ivy completion in the minibuffer."
  2640. (setq-local mwheel-scroll-up-function 'ivy-next-line)
  2641. (setq-local mwheel-scroll-down-function 'ivy-previous-line)
  2642. (setq-local completion-show-inline-help nil)
  2643. (setq-local line-spacing nil)
  2644. (setq-local minibuffer-default-add-function
  2645. (lambda ()
  2646. (list ivy--default)))
  2647. (setq-local inhibit-field-text-motion nil)
  2648. (setq truncate-lines ivy-truncate-lines)
  2649. (setq-local max-mini-window-height ivy-height)
  2650. (let ((height (cond ((and ivy-fixed-height-minibuffer
  2651. (not (eq (ivy-state-caller ivy-last)
  2652. #'ivy-completion-in-region)))
  2653. (+ ivy-height (if ivy-add-newline-after-prompt 1 0)))
  2654. (ivy-add-newline-after-prompt 2))))
  2655. (when height
  2656. (set-window-text-height nil height)))
  2657. (add-hook 'post-command-hook #'ivy--queue-exhibit nil t)
  2658. (add-hook 'window-size-change-functions #'ivy--window-size-changed nil t)
  2659. (let ((hook (ivy-alist-setting ivy-hooks-alist)))
  2660. (when (functionp hook)
  2661. (funcall hook))))
  2662. (defun ivy--input ()
  2663. "Return the current minibuffer input."
  2664. ;; assume one-line minibuffer input
  2665. (save-excursion
  2666. (goto-char (minibuffer-prompt-end))
  2667. (let ((inhibit-field-text-motion t))
  2668. (buffer-substring-no-properties
  2669. (point)
  2670. (line-end-position)))))
  2671. (defun ivy--minibuffer-cleanup ()
  2672. "Delete the displayed completion candidates."
  2673. (save-excursion
  2674. (goto-char (minibuffer-prompt-end))
  2675. (delete-region (line-end-position) (point-max))))
  2676. (defun ivy-cleanup-string (str)
  2677. "Destructively remove unwanted text properties from STR."
  2678. (ivy--remove-props str 'field))
  2679. (defvar ivy-set-prompt-text-properties-function
  2680. #'ivy-set-prompt-text-properties-default
  2681. "Function to set the text properties of the default ivy prompt.
  2682. Called with two arguments, PROMPT and PROPS, where PROMPT is the
  2683. string to be propertized and PROPS is a plist of default text
  2684. properties that may be applied to PROMPT. The function should
  2685. return the propertized PROMPT, which may be modified in-place.")
  2686. (defun ivy-set-prompt-text-properties-default (prompt props)
  2687. "Propertize (confirm) and (match required) parts of PROMPT.
  2688. PROPS is a plist of default text properties to apply to these
  2689. parts beyond their respective faces `ivy-confirm-face' and
  2690. `ivy-match-required-face'."
  2691. (dolist (pair '(("confirm" . ivy-confirm-face)
  2692. ("match required" . ivy-match-required-face)))
  2693. (let ((i (string-match-p (car pair) prompt)))
  2694. (when i
  2695. (add-text-properties i (+ i (length (car pair)))
  2696. `(face ,(cdr pair) ,@props)
  2697. prompt))))
  2698. prompt)
  2699. (defun ivy-prompt ()
  2700. "Return the current prompt."
  2701. (let* ((caller (ivy-state-caller ivy-last))
  2702. (fn (plist-get ivy--prompts-list caller)))
  2703. (if fn
  2704. (condition-case err
  2705. (funcall fn)
  2706. (wrong-number-of-arguments
  2707. (lwarn 'ivy :error "%s
  2708. Prompt function set via `ivy-set-prompt' for caller `%s'
  2709. should take no arguments."
  2710. (error-message-string err)
  2711. caller)
  2712. ;; Old behavior.
  2713. (funcall fn (ivy-state-prompt ivy-last))))
  2714. ivy--prompt)))
  2715. (defun ivy--insert-prompt ()
  2716. "Update the prompt according to `ivy--prompt'."
  2717. (when (setq ivy--prompt (ivy-prompt))
  2718. (unless (memq this-command '(ivy-done ivy-alt-done ivy-partial-or-done
  2719. counsel-find-symbol))
  2720. (setq ivy--prompt-extra ""))
  2721. (let (head tail)
  2722. (if (string-match "\\(.*?\\)\\(:? ?\\)\\'" ivy--prompt)
  2723. (progn
  2724. (setq head (match-string 1 ivy--prompt))
  2725. (setq tail (match-string 2 ivy--prompt)))
  2726. (setq head ivy--prompt)
  2727. (setq tail ""))
  2728. (let ((inhibit-read-only t)
  2729. (std-props '(front-sticky t rear-nonsticky t field t read-only t))
  2730. (n-str
  2731. (concat
  2732. (if (and (bound-and-true-p minibuffer-depth-indicate-mode)
  2733. (> (minibuffer-depth) 1))
  2734. (format "[%d] " (minibuffer-depth))
  2735. "")
  2736. (concat
  2737. (if (string-match "%d.*%d" ivy-count-format)
  2738. (format head
  2739. (1+ ivy--index)
  2740. (or (and (ivy-state-dynamic-collection ivy-last)
  2741. ivy--full-length)
  2742. ivy--length))
  2743. (format head
  2744. (or (and (ivy-state-dynamic-collection ivy-last)
  2745. ivy--full-length)
  2746. ivy--length)))
  2747. ivy--prompt-extra
  2748. tail)))
  2749. (d-str (if ivy--directory
  2750. (abbreviate-file-name ivy--directory)
  2751. "")))
  2752. (save-excursion
  2753. (goto-char (point-min))
  2754. (delete-region (point-min) (minibuffer-prompt-end))
  2755. (let ((len-n (length n-str))
  2756. (len-d (length d-str))
  2757. (ww (window-width)))
  2758. (setq n-str
  2759. (cond ((> (+ len-n len-d) ww)
  2760. (concat n-str "\n" d-str "\n"))
  2761. ((> (+ len-n len-d (length ivy-text)) ww)
  2762. (concat n-str d-str "\n"))
  2763. (t
  2764. (concat n-str d-str)))))
  2765. (when ivy-pre-prompt-function
  2766. (setq n-str (concat (funcall ivy-pre-prompt-function) n-str)))
  2767. (when ivy-add-newline-after-prompt
  2768. (setq n-str (concat n-str "\n")))
  2769. (let ((regex (format "\\([^\n]\\{%d\\}\\)[^\n]" (window-width))))
  2770. (while (string-match regex n-str)
  2771. (setq n-str (replace-match
  2772. (concat (match-string 1 n-str) "\n")
  2773. nil t n-str 1))))
  2774. (set-text-properties 0 (length n-str)
  2775. `(face minibuffer-prompt ,@std-props)
  2776. n-str)
  2777. (setq n-str (funcall ivy-set-prompt-text-properties-function
  2778. n-str std-props))
  2779. (insert n-str))
  2780. ;; Mark prompt as selected if the user moves there or it is the only
  2781. ;; option left. Since the user input stays put, we have to manually
  2782. ;; remove the face as well.
  2783. (when ivy--use-selectable-prompt
  2784. (if (= ivy--index -1)
  2785. (add-face-text-property
  2786. (minibuffer-prompt-end) (line-end-position) 'ivy-prompt-match)
  2787. (remove-list-of-text-properties
  2788. (minibuffer-prompt-end) (line-end-position) '(face))))
  2789. ;; get out of the prompt area
  2790. (constrain-to-field nil (point-max))))))
  2791. (defun ivy--sort-maybe (collection)
  2792. "Sort COLLECTION if needed."
  2793. (let ((sort (ivy-state-sort ivy-last)))
  2794. (if (and sort
  2795. (or (functionp sort)
  2796. (functionp (setq sort (ivy--sort-function
  2797. (ivy-state-collection ivy-last))))))
  2798. (sort (copy-sequence collection) sort)
  2799. collection)))
  2800. (defcustom ivy-magic-slash-non-match-action 'ivy-magic-slash-non-match-cd-selected
  2801. "Action to take when a slash is added to the end of a non existing directory.
  2802. Possible choices are 'ivy-magic-slash-non-match-cd-selected,
  2803. 'ivy-magic-slash-non-match-create, or nil"
  2804. :type '(choice
  2805. (const :tag "Use currently selected directory"
  2806. ivy-magic-slash-non-match-cd-selected)
  2807. (const :tag "Create and use new directory"
  2808. ivy-magic-slash-non-match-create)
  2809. (const :tag "Do nothing"
  2810. nil)))
  2811. (defun ivy--create-and-cd (dir)
  2812. "When completing file names, create directory DIR and move there."
  2813. (make-directory dir)
  2814. (ivy--cd dir))
  2815. (defun ivy--magic-file-doubleslash-directory ()
  2816. "Return an appropriate directory for when two slashes are entered."
  2817. (let (remote)
  2818. (cond
  2819. ;; Windows
  2820. ;; ((string-match "\\`[[:alpha:]]:/" ivy--directory)
  2821. ;; (match-string 0 ivy--directory))
  2822. ;; Remote root if on remote
  2823. ((setq remote (file-remote-p ivy--directory))
  2824. (concat remote "/"))
  2825. ;; Local root
  2826. (t
  2827. "/"))))
  2828. (defun ivy--magic-file-slash ()
  2829. "Handle slash when completing file names."
  2830. (when (or (and (eq this-command #'self-insert-command)
  2831. (eolp))
  2832. (eq this-command #'ivy-partial-or-done))
  2833. (let ((canonical (expand-file-name ivy-text ivy--directory))
  2834. (magic (not (string= ivy-text "/"))))
  2835. (cond ((member ivy-text ivy--all-candidates)
  2836. (ivy--cd canonical))
  2837. ((and (eq system-type 'windows-nt) (string= ivy-text "//")))
  2838. ((string-suffix-p "//" ivy-text)
  2839. (ivy--cd
  2840. (ivy--magic-file-doubleslash-directory)))
  2841. ((string-match-p "\\`/ssh:" ivy-text)
  2842. (ivy--cd (file-name-directory ivy-text)))
  2843. ((string-match "[[:alpha:]]:/\\'" ivy-text)
  2844. (let ((drive-root (match-string 0 ivy-text)))
  2845. (when (file-exists-p drive-root)
  2846. (ivy--cd drive-root))))
  2847. ((and magic (file-directory-p canonical))
  2848. (ivy--cd canonical))
  2849. ((let ((default-directory ivy--directory))
  2850. (and (or (> ivy--index 0)
  2851. (= ivy--length 1)
  2852. magic)
  2853. (not (ivy--prompt-selected-p))
  2854. (not (equal (ivy-state-current ivy-last) ""))
  2855. (file-directory-p (ivy-state-current ivy-last))
  2856. (or (eq ivy-magic-slash-non-match-action
  2857. 'ivy-magic-slash-non-match-cd-selected)
  2858. (eq this-command #'ivy-partial-or-done))))
  2859. (ivy--cd
  2860. (expand-file-name (ivy-state-current ivy-last) ivy--directory)))
  2861. ((and (eq ivy-magic-slash-non-match-action
  2862. 'ivy-magic-slash-non-match-create)
  2863. magic)
  2864. (ivy--create-and-cd canonical))))))
  2865. (defun ivy-magic-read-file-env ()
  2866. "If reading filename, jump to environment variable location."
  2867. (interactive)
  2868. (if (and ivy--directory
  2869. (equal ivy-text ""))
  2870. (let* ((cands (cl-loop for pair in process-environment
  2871. for (var val) = (split-string pair "=" t)
  2872. if (and val (not (equal "" val)))
  2873. if (file-exists-p
  2874. (if (file-name-absolute-p val)
  2875. val
  2876. (setq val
  2877. (expand-file-name val ivy--directory))))
  2878. collect (cons var val)))
  2879. (enable-recursive-minibuffers t)
  2880. (x (ivy-read "Env: " cands))
  2881. (path (cdr (assoc x cands))))
  2882. (insert (if (file-accessible-directory-p path)
  2883. (file-name-as-directory path)
  2884. path))
  2885. (ivy--cd-maybe))
  2886. (insert last-input-event)))
  2887. (defun ivy-make-magic-action (caller key)
  2888. "Return a command that does the equivalent of `ivy-read-action' and KEY.
  2889. This happens only when the input is empty.
  2890. The intention is to bind the result to keys that are typically
  2891. bound to `self-insert-command'."
  2892. (let* ((alist (assoc key
  2893. (plist-get
  2894. ivy--actions-list
  2895. caller)))
  2896. (doc (format "%s (`%S')"
  2897. (nth 2 alist)
  2898. (nth 1 alist))))
  2899. `(lambda (&optional arg)
  2900. ,doc
  2901. (interactive "p")
  2902. (if (string= "" ivy-text)
  2903. (execute-kbd-macro
  2904. (kbd ,(concat "M-o " key)))
  2905. (self-insert-command arg)))))
  2906. (defcustom ivy-magic-tilde t
  2907. "When non-nil, ~ will move home when selecting files.
  2908. Otherwise, ~/ will move home."
  2909. :type 'boolean)
  2910. (defcustom ivy-dynamic-exhibit-delay-ms 0
  2911. "Delay in ms before dynamic collections are refreshed"
  2912. :type 'integer)
  2913. (defvar ivy--exhibit-timer nil)
  2914. (defun ivy--queue-exhibit ()
  2915. "Insert Ivy completions display, possibly after a timeout for
  2916. dynamic collections.
  2917. Should be run via minibuffer `post-command-hook'."
  2918. (if (and (> ivy-dynamic-exhibit-delay-ms 0)
  2919. (ivy-state-dynamic-collection ivy-last))
  2920. (progn
  2921. (when ivy--exhibit-timer (cancel-timer ivy--exhibit-timer))
  2922. (setq ivy--exhibit-timer
  2923. (run-with-timer
  2924. (/ ivy-dynamic-exhibit-delay-ms 1000.0)
  2925. nil
  2926. 'ivy--exhibit)))
  2927. (ivy--exhibit)))
  2928. (defalias 'ivy--file-local-name
  2929. (if (fboundp 'file-local-name)
  2930. #'file-local-name
  2931. (lambda (file)
  2932. (or (file-remote-p file 'localname) file)))
  2933. "Compatibility shim for `file-local-name'.
  2934. The function was added in Emacs 26.1.")
  2935. (defun ivy--magic-tilde-directory (dir)
  2936. "Return an appropriate home for DIR for when ~ or ~/ are entered."
  2937. (file-name-as-directory
  2938. (expand-file-name
  2939. (let* ((home (expand-file-name (concat (file-remote-p dir) "~/")))
  2940. (dir-path (ivy--file-local-name dir))
  2941. (home-path (ivy--file-local-name home)))
  2942. (if (string= dir-path home-path)
  2943. "~"
  2944. home)))))
  2945. (defun ivy-update-candidates (cands)
  2946. (ivy--insert-minibuffer
  2947. (ivy--format
  2948. (setq ivy--all-candidates cands))))
  2949. (defun ivy--exhibit ()
  2950. "Insert Ivy completions display.
  2951. Should be run via minibuffer `post-command-hook'."
  2952. (when (memq 'ivy--queue-exhibit post-command-hook)
  2953. (let ((inhibit-field-text-motion nil))
  2954. (constrain-to-field nil (point-max)))
  2955. (ivy-set-text (ivy--input))
  2956. (let ((new-minibuffer (ivy--update-minibuffer)))
  2957. (when new-minibuffer
  2958. (ivy--insert-minibuffer new-minibuffer)))
  2959. t))
  2960. (defun ivy--dynamic-collection-cands (input)
  2961. (let ((coll (funcall (ivy-state-collection ivy-last) input)))
  2962. (if (listp coll)
  2963. (mapcar (lambda (x) (if (consp x) (car x) x)) coll)
  2964. coll)))
  2965. (defun ivy--update-minibuffer ()
  2966. (prog1
  2967. (if (ivy-state-dynamic-collection ivy-last)
  2968. ;; while-no-input would cause annoying
  2969. ;; "Waiting for process to die...done" message interruptions
  2970. (let ((inhibit-message t)
  2971. coll in-progress)
  2972. (unless (or (equal ivy--old-text ivy-text)
  2973. (eq this-command 'ivy-resume))
  2974. (while-no-input
  2975. (setq coll (ivy--dynamic-collection-cands ivy-text))
  2976. (when (eq coll 0)
  2977. (setq coll nil)
  2978. (setq ivy--old-re nil)
  2979. (setq in-progress t))
  2980. (setq ivy--all-candidates (ivy--sort-maybe coll))))
  2981. (when (eq ivy--all-candidates 0)
  2982. (setq ivy--all-candidates nil)
  2983. (setq ivy--old-re nil)
  2984. (setq in-progress t))
  2985. (when (or ivy--all-candidates
  2986. (and (not (get-process " *counsel*"))
  2987. (not in-progress)))
  2988. (ivy--set-index-dynamic-collection)
  2989. (ivy--format ivy--all-candidates)))
  2990. (cond (ivy--directory
  2991. (cond ((or (string= "~/" ivy-text)
  2992. (and (string= "~" ivy-text)
  2993. ivy-magic-tilde))
  2994. (ivy--cd (ivy--magic-tilde-directory ivy--directory)))
  2995. ((string-match "/\\'" ivy-text)
  2996. (ivy--magic-file-slash))))
  2997. ((eq (ivy-state-collection ivy-last) #'internal-complete-buffer)
  2998. (when (or (and (string-match "\\` " ivy-text)
  2999. (not (string-match "\\` " ivy--old-text)))
  3000. (and (string-match "\\` " ivy--old-text)
  3001. (not (string-match "\\` " ivy-text))))
  3002. (setq ivy--all-candidates
  3003. (if (= (string-to-char ivy-text) ?\s)
  3004. (ivy--buffer-list " ")
  3005. (ivy--buffer-list "" ivy-use-virtual-buffers)))
  3006. (setq ivy--old-re nil))))
  3007. (with-current-buffer (ivy-state-buffer ivy-last)
  3008. (ivy--format
  3009. (ivy--filter ivy-text ivy--all-candidates))))
  3010. (setq ivy--old-text ivy-text)))
  3011. (defun ivy-display-function-fallback (str)
  3012. (let ((buffer-undo-list t))
  3013. (save-excursion
  3014. (forward-line 1)
  3015. (insert str))))
  3016. (defun ivy--insert-minibuffer (text)
  3017. "Insert TEXT into minibuffer with appropriate cleanup."
  3018. (let ((resize-mini-windows nil)
  3019. (update-fn (ivy-state-update-fn ivy-last))
  3020. (old-mark (marker-position (mark-marker)))
  3021. (win (active-minibuffer-window))
  3022. deactivate-mark)
  3023. (when win
  3024. (with-selected-window win
  3025. (ivy--minibuffer-cleanup)
  3026. (when update-fn
  3027. (funcall update-fn))
  3028. (ivy--insert-prompt)
  3029. ;; Do nothing if while-no-input was aborted.
  3030. (when (stringp text)
  3031. (if ivy--display-function
  3032. (funcall ivy--display-function text)
  3033. (ivy-display-function-fallback text)))
  3034. (ivy--resize-minibuffer-to-fit)
  3035. ;; prevent region growing due to text remove/add
  3036. (when (region-active-p)
  3037. (set-mark old-mark))))))
  3038. (defvar ivy-auto-shrink-minibuffer nil
  3039. "When non-nil and the height < `ivy-height', auto-shrink the minibuffer.")
  3040. (make-obsolete-variable 'ivy-auto-shrink-minibuffer
  3041. 'ivy-auto-shrink-minibuffer-alist
  3042. "<2020-04-28 Tue>")
  3043. (defcustom ivy-auto-shrink-minibuffer-alist nil
  3044. "An alist to configure auto-shrinking of the minibuffer.
  3045. Each key is a caller symbol. When the value is non-nil, and the
  3046. height < `ivy-height', auto-shrink the minibuffer."
  3047. :type '(alist
  3048. :key-type symbol
  3049. :value-type boolean))
  3050. (defun ivy--do-shrink-window ()
  3051. (let ((h (save-excursion
  3052. (goto-char (minibuffer-prompt-end))
  3053. (let ((inhibit-field-text-motion t))
  3054. (line-number-at-pos)))))
  3055. (shrink-window (-
  3056. (/ (window-body-height nil t)
  3057. (frame-char-height))
  3058. ivy--length h))))
  3059. (defun ivy--resize-minibuffer-to-fit ()
  3060. "Resize the minibuffer window size to fit the text in the minibuffer."
  3061. (unless (or (frame-root-window-p (minibuffer-window))
  3062. (memq this-command '(ivy-read-action
  3063. ivy-dispatching-done
  3064. ivy-dispatching-call)))
  3065. (with-selected-window (minibuffer-window)
  3066. (if (fboundp 'window-text-pixel-size)
  3067. (let ((text-height (cdr (window-text-pixel-size)))
  3068. (body-height (window-body-height nil t)))
  3069. (cond ((> text-height body-height)
  3070. ;; Note: the size increment needs to be at least
  3071. ;; frame-char-height, otherwise resizing won't do
  3072. ;; anything.
  3073. (let ((delta (max (- text-height body-height)
  3074. (frame-char-height))))
  3075. (window-resize nil delta nil t t)))
  3076. ((and (or ivy-auto-shrink-minibuffer
  3077. (ivy-alist-setting
  3078. ivy-auto-shrink-minibuffer-alist))
  3079. (< ivy--length ivy-height))
  3080. (ivy--do-shrink-window))))
  3081. (let ((text-height (count-screen-lines))
  3082. (body-height (window-body-height)))
  3083. (when (> text-height body-height)
  3084. (window-resize nil (- text-height body-height) nil t)))))))
  3085. (defun ivy--window-size-changed (&rest _)
  3086. "Resize ivy window to fit with current frame's size."
  3087. (when ivy-mode
  3088. (ivy--resize-minibuffer-to-fit)))
  3089. (defun ivy--add-face (str face)
  3090. "Propertize STR with FACE."
  3091. (let ((len (length str)))
  3092. (condition-case nil
  3093. (progn
  3094. (colir-blend-face-background 0 len face str)
  3095. (let ((foreground (face-foreground face)))
  3096. (when foreground
  3097. (add-face-text-property
  3098. 0 len (list :foreground foreground) nil str))))
  3099. (error
  3100. (ignore-errors
  3101. (font-lock-append-text-property 0 len 'face face str)))))
  3102. str)
  3103. (declare-function flx-make-string-cache "ext:flx")
  3104. (declare-function flx-score "ext:flx")
  3105. (defvar ivy--flx-cache nil)
  3106. (with-eval-after-load 'flx
  3107. (setq ivy--flx-cache (flx-make-string-cache)))
  3108. (defun ivy-toggle-case-fold ()
  3109. "Toggle `case-fold-search' for Ivy operations.
  3110. Instead of modifying `case-fold-search' directly, this command
  3111. toggles `ivy-case-fold-search', which can take on more values
  3112. than the former, between nil and either `auto' or t. See
  3113. `ivy-case-fold-search-default' for the meaning of these values.
  3114. In any Ivy completion session, the case folding starts with
  3115. `ivy-case-fold-search-default'."
  3116. (interactive)
  3117. (setq ivy-case-fold-search
  3118. (and (not ivy-case-fold-search)
  3119. (or ivy-case-fold-search-default 'auto)))
  3120. ;; Reset cache so that the candidate list updates.
  3121. (setq ivy--old-re nil))
  3122. (defun ivy--re-filter (re candidates &optional mkpred)
  3123. "Return all RE matching CANDIDATES.
  3124. RE is a list of cons cells, with a regexp car and a boolean cdr.
  3125. When the cdr is t, the car must match.
  3126. Otherwise, the car must not match."
  3127. (if (equal re "")
  3128. candidates
  3129. (ignore-errors
  3130. (dolist (re (if (stringp re) (list (cons re t)) re))
  3131. (let* ((re-str (car re))
  3132. (pred
  3133. (if mkpred
  3134. (funcall mkpred re-str)
  3135. (lambda (x) (string-match-p re-str x)))))
  3136. (setq candidates
  3137. (cl-remove nil candidates
  3138. (if (cdr re) :if-not :if)
  3139. pred))))
  3140. candidates)))
  3141. (defun ivy--filter (name candidates)
  3142. "Return all items that match NAME in CANDIDATES.
  3143. CANDIDATES are assumed to be static."
  3144. (let ((re (funcall ivy--regex-function name)))
  3145. (if (and
  3146. ivy--old-re
  3147. ivy--old-cands
  3148. (equal re ivy--old-re))
  3149. ;; quick caching for "C-n", "C-p" etc.
  3150. ivy--old-cands
  3151. (let* ((re-str (ivy-re-to-str re))
  3152. (matcher (ivy-state-matcher ivy-last))
  3153. (case-fold-search (ivy--case-fold-p name))
  3154. (cands (cond
  3155. (matcher
  3156. (funcall matcher re candidates))
  3157. ((and ivy--old-re
  3158. (stringp re)
  3159. (stringp ivy--old-re)
  3160. (not (string-match-p "\\\\" ivy--old-re))
  3161. (not (equal ivy--old-re ""))
  3162. (memq (cl-search
  3163. (if (string-match-p "\\\\)\\'" ivy--old-re)
  3164. (substring ivy--old-re 0 -2)
  3165. ivy--old-re)
  3166. re)
  3167. '(0 2))
  3168. ivy--old-cands
  3169. (ivy--re-filter re ivy--old-cands)))
  3170. (t
  3171. (ivy--re-filter re candidates)))))
  3172. (if (memq (cdr (assq (ivy-state-caller ivy-last)
  3173. ivy-index-functions-alist))
  3174. '(ivy-recompute-index-swiper
  3175. ivy-recompute-index-swiper-async
  3176. ivy-recompute-index-swiper-async-backward
  3177. ivy-recompute-index-swiper-backward))
  3178. (progn
  3179. (ivy--recompute-index re-str cands)
  3180. (setq ivy--old-cands (ivy--sort name cands)))
  3181. (setq ivy--old-cands (ivy--sort name cands))
  3182. (ivy--recompute-index re-str ivy--old-cands))
  3183. (setq ivy--old-re re)
  3184. ivy--old-cands))))
  3185. (defun ivy--set-candidates (x)
  3186. "Update `ivy--all-candidates' with X."
  3187. (let (res
  3188. ;; (ivy--recompute-index-inhibit t)
  3189. )
  3190. (dolist (source ivy--extra-candidates)
  3191. (if (equal source '(original-source))
  3192. (if (null res)
  3193. (setq res x)
  3194. (setq res (append x res)))
  3195. (setq ivy--old-re nil)
  3196. (setq res (append
  3197. (ivy--filter ivy-text (cadr source))
  3198. res))))
  3199. (setq ivy--all-candidates
  3200. (if (cdr ivy--extra-candidates)
  3201. (delete-dups res)
  3202. res))))
  3203. (defun ivy--shorter-matches-first (_name cands)
  3204. "Sort CANDS according to their length."
  3205. (if (nthcdr ivy-sort-max-size cands)
  3206. cands
  3207. (cl-sort (copy-sequence cands) #'< :key #'length)))
  3208. (defcustom ivy-sort-matches-functions-alist
  3209. '((t . nil)
  3210. (ivy-completion-in-region . ivy--shorter-matches-first)
  3211. (ivy-switch-buffer . ivy-sort-function-buffer))
  3212. "An alist of functions for sorting matching candidates.
  3213. Unlike `ivy-sort-functions-alist', which is used to sort the
  3214. whole collection only once, this alist of functions are used to
  3215. sort only matching candidates after each change in input.
  3216. The alist KEY is either a collection function or t to match
  3217. previously unmatched collection functions.
  3218. The alist VAL is a sorting function with the signature of
  3219. `ivy--prefix-sort'."
  3220. :type '(alist
  3221. :key-type (choice
  3222. (const :tag "Fall-through" t)
  3223. (symbol :tag "Collection"))
  3224. :value-type
  3225. (choice
  3226. (const :tag "Don't sort" nil)
  3227. (const :tag "Put prefix matches ahead" ivy--prefix-sort)
  3228. (function :tag "Custom sort function"))))
  3229. (defun ivy--sort-files-by-date (_name candidates)
  3230. "Re-sort CANDIDATES according to file modification date."
  3231. (let ((default-directory ivy--directory))
  3232. (sort (copy-sequence candidates) #'file-newer-than-file-p)))
  3233. (defvar ivy--flx-featurep (require 'flx nil 'noerror))
  3234. (defun ivy--sort (name candidates)
  3235. "Re-sort candidates by NAME.
  3236. All CANDIDATES are assumed to match NAME."
  3237. (let (fun)
  3238. (cond ((setq fun (ivy-alist-setting ivy-sort-matches-functions-alist))
  3239. (funcall fun name candidates))
  3240. ((and ivy--flx-featurep
  3241. (eq ivy--regex-function 'ivy--regex-fuzzy))
  3242. (ivy--flx-sort name candidates))
  3243. (t
  3244. candidates))))
  3245. (defun ivy--prefix-sort (name candidates)
  3246. "Re-sort candidates by NAME.
  3247. All CANDIDATES are assumed to match NAME.
  3248. Prefix matches to NAME are put ahead of the list."
  3249. (if (or (string= name "")
  3250. (= (aref name 0) ?^))
  3251. candidates
  3252. (let ((re-prefix (concat "\\`" (funcall ivy--regex-function name)))
  3253. res-prefix
  3254. res-noprefix)
  3255. (dolist (s candidates)
  3256. (if (string-match-p re-prefix s)
  3257. (push s res-prefix)
  3258. (push s res-noprefix)))
  3259. (nconc
  3260. (nreverse res-prefix)
  3261. (nreverse res-noprefix)))))
  3262. (defvar ivy--virtual-buffers nil
  3263. "Store the virtual buffers alist.")
  3264. (defun ivy-re-to-str (re)
  3265. "Transform RE to a string.
  3266. Functions like `ivy--regex-ignore-order' return a cons list.
  3267. This function extracts a string from the cons list."
  3268. (if (consp re) (caar re) re))
  3269. (defun ivy-sort-function-buffer (name candidates)
  3270. "Re-sort candidates by NAME.
  3271. CANDIDATES is a list of buffer names each containing NAME.
  3272. Sort open buffers before virtual buffers, and prefix matches
  3273. before substring matches."
  3274. (if (or (string= name "")
  3275. (= (aref name 0) ?^))
  3276. candidates
  3277. (let* ((base-re (ivy-re-to-str (funcall ivy--regex-function name)))
  3278. (re-star-prefix (concat "\\`\\*" base-re))
  3279. (re-prefix (concat "\\`" base-re))
  3280. res-prefix
  3281. res-noprefix
  3282. res-virtual-prefix
  3283. res-virtual-noprefix)
  3284. (dolist (s candidates)
  3285. (cond
  3286. ((and (assoc s ivy--virtual-buffers)
  3287. (or (string-match-p re-star-prefix s)
  3288. (string-match-p re-prefix s)))
  3289. (push s res-virtual-prefix))
  3290. ((assoc s ivy--virtual-buffers)
  3291. (push s res-virtual-noprefix))
  3292. ((or (string-match-p re-star-prefix s)
  3293. (string-match-p re-prefix s))
  3294. (push s res-prefix))
  3295. (t
  3296. (push s res-noprefix))))
  3297. (nconc
  3298. (nreverse res-prefix)
  3299. (nreverse res-noprefix)
  3300. (nreverse res-virtual-prefix)
  3301. (nreverse res-virtual-noprefix)))))
  3302. (defvar ivy-flx-limit 200
  3303. "Used to conditionally turn off flx sorting.
  3304. When the amount of matching candidates exceeds this limit, then
  3305. no sorting is done.")
  3306. (defvar ivy--recompute-index-inhibit nil
  3307. "When non-nil, `ivy--recompute-index' is a no-op.")
  3308. (defun ivy--recompute-index (re-str cands)
  3309. "Recompute index of selected candidate matching RE-STR.
  3310. CANDS are the current candidates."
  3311. (let ((caller (ivy-state-caller ivy-last))
  3312. (func (or (ivy-alist-setting ivy-index-functions-alist)
  3313. #'ivy-recompute-index-zero))
  3314. (case-fold-search (ivy--case-fold-p re-str))
  3315. (preselect (ivy-state-preselect ivy-last))
  3316. (current (ivy-state-current ivy-last))
  3317. (empty (string= re-str "")))
  3318. (unless (or (memq this-command '(ivy-resume ivy-partial-or-done))
  3319. ivy--recompute-index-inhibit)
  3320. (let ((index (cond
  3321. ((or empty (string= re-str "^"))
  3322. (ivy--preselect-index preselect cands))
  3323. ((and (> (length cands) 10000) (eq func #'ivy-recompute-index-zero))
  3324. 0)
  3325. ((cl-position (string-remove-prefix "^" re-str)
  3326. cands
  3327. :test #'ivy--case-fold-string=))
  3328. ((and (ivy--completing-fname-p)
  3329. (cl-position (concat re-str "/")
  3330. cands
  3331. :test #'ivy--case-fold-string=)))
  3332. ((and (eq caller 'ivy-switch-buffer)
  3333. (not empty))
  3334. (or (cl-position current cands :test #'string=)
  3335. 0))
  3336. ((and (not empty)
  3337. (not (eq caller 'swiper))
  3338. (not (and ivy--flx-featurep
  3339. (eq ivy--regex-function 'ivy--regex-fuzzy)
  3340. ;; Limit to configured number of candidates
  3341. (null (nthcdr ivy-flx-limit cands))))
  3342. ;; If there was a preselected candidate, don't try to
  3343. ;; keep it selected even if the regexp still matches it.
  3344. ;; See issue #1563. See also `ivy--preselect-index',
  3345. ;; which this logic roughly mirrors.
  3346. (not (or
  3347. (and (integerp preselect)
  3348. (= ivy--index preselect))
  3349. (equal current preselect)
  3350. (and (ivy--regex-p preselect)
  3351. (stringp current)
  3352. (string-match-p preselect current))))
  3353. ivy--old-cands
  3354. (cl-position current cands :test #'equal)))
  3355. ((funcall func re-str cands))
  3356. (t 0))))
  3357. (ivy-set-index index)))))
  3358. (defun ivy-recompute-index-swiper (_re-str cands)
  3359. "Recompute index of selected candidate when using `swiper'.
  3360. CANDS are the current candidates."
  3361. (condition-case nil
  3362. (let ((tail (nthcdr ivy--index ivy--old-cands))
  3363. idx)
  3364. (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
  3365. (progn
  3366. (while (and tail (null idx))
  3367. ;; Compare with eq to handle equal duplicates in cands
  3368. (setq idx (cl-position (pop tail) cands)))
  3369. (or
  3370. idx
  3371. (1- (length cands))))
  3372. (if ivy--old-cands
  3373. ivy--index
  3374. ;; already in ivy-state-buffer
  3375. (let ((n (line-number-at-pos))
  3376. (res 0)
  3377. (i 0))
  3378. (dolist (c cands)
  3379. (when (eq n (get-text-property 0 'swiper-line-number c))
  3380. (setq res i))
  3381. (cl-incf i))
  3382. res))))
  3383. (error 0)))
  3384. (defun ivy-recompute-index-swiper-backward (re-str cands)
  3385. "Recompute index of selected candidate when using `swiper-backward'.
  3386. CANDS are the current candidates."
  3387. (let ((idx (ivy-recompute-index-swiper re-str cands)))
  3388. (if (or (= idx -1)
  3389. (<= (get-text-property 0 'swiper-line-number (nth idx cands))
  3390. (line-number-at-pos)))
  3391. idx
  3392. (- idx 1))))
  3393. (defun ivy-recompute-index-swiper-async (_re-str cands)
  3394. "Recompute index of selected candidate when using `swiper' asynchronously.
  3395. CANDS are the current candidates."
  3396. (if (null ivy--old-cands)
  3397. (let ((ln (with-ivy-window
  3398. (line-number-at-pos))))
  3399. (or
  3400. ;; closest to current line going forwards
  3401. (cl-position-if (lambda (x)
  3402. (>= (string-to-number x) ln))
  3403. cands)
  3404. ;; closest to current line going backwards
  3405. (1- (length cands))))
  3406. (let ((tail (nthcdr ivy--index ivy--old-cands))
  3407. idx)
  3408. (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
  3409. (progn
  3410. (while (and tail (null idx))
  3411. ;; Compare with `equal', since the collection is re-created
  3412. ;; each time with `split-string'
  3413. (setq idx (cl-position (pop tail) cands :test #'equal)))
  3414. (or idx 0))
  3415. ivy--index))))
  3416. (defun ivy-recompute-index-swiper-async-backward (re-str cands)
  3417. "Recompute index of selected candidate when using `swiper-backward'
  3418. asynchronously. CANDS are the current candidates."
  3419. (if (= (length cands) 0)
  3420. 0
  3421. (let ((idx (ivy-recompute-index-swiper-async re-str cands)))
  3422. (if
  3423. (<= (string-to-number (nth idx cands))
  3424. (with-ivy-window (line-number-at-pos)))
  3425. idx
  3426. (- idx 1)))))
  3427. (defun ivy-recompute-index-zero (_re-str _cands)
  3428. "Recompute index of selected candidate.
  3429. This function serves as a fallback when nothing else is available."
  3430. 0)
  3431. (defcustom ivy-minibuffer-faces
  3432. '(ivy-minibuffer-match-face-1
  3433. ivy-minibuffer-match-face-2
  3434. ivy-minibuffer-match-face-3
  3435. ivy-minibuffer-match-face-4)
  3436. "List of `ivy' faces for minibuffer group matches."
  3437. :type '(repeat :tag "Faces"
  3438. (choice
  3439. (const ivy-minibuffer-match-face-1)
  3440. (const ivy-minibuffer-match-face-2)
  3441. (const ivy-minibuffer-match-face-3)
  3442. (const ivy-minibuffer-match-face-4)
  3443. (face :tag "Other face"))))
  3444. (defun ivy--minibuffer-face (n)
  3445. "Return Nth face from `ivy-minibuffer-faces'.
  3446. N wraps around, but skips the first element of the list."
  3447. (let ((tail (cdr ivy-minibuffer-faces)))
  3448. (nth (mod (+ n 2) (length tail)) tail)))
  3449. (defun ivy--flx-propertize (x)
  3450. "X is (cons (flx-score STR ...) STR)."
  3451. (let ((str (copy-sequence (cdr x)))
  3452. (i 0)
  3453. (last-j -2))
  3454. (dolist (j (cdar x))
  3455. (unless (eq j (1+ last-j))
  3456. (cl-incf i))
  3457. (setq last-j j)
  3458. (add-face-text-property j (1+ j) (ivy--minibuffer-face i) nil str))
  3459. str))
  3460. (defun ivy--flx-sort (name cands)
  3461. "Sort according to closeness to string NAME the string list CANDS."
  3462. (condition-case nil
  3463. (let* ((bolp (= (string-to-char name) ?^))
  3464. ;; An optimized regex for fuzzy matching
  3465. ;; "abc" → "^[^a]*a[^b]*b[^c]*c"
  3466. (fuzzy-regex (concat "\\`"
  3467. (and bolp (regexp-quote (substring name 1 2)))
  3468. (mapconcat
  3469. (lambda (x)
  3470. (setq x (char-to-string x))
  3471. (concat "[^" x "]*" (regexp-quote x)))
  3472. (if bolp (substring name 2) name)
  3473. "")))
  3474. ;; Strip off the leading "^" for flx matching
  3475. (flx-name (if bolp (substring name 1) name))
  3476. cands-left
  3477. cands-to-sort)
  3478. ;; Filter out non-matching candidates
  3479. (dolist (cand cands)
  3480. (when (string-match-p fuzzy-regex cand)
  3481. (push cand cands-left)))
  3482. ;; pre-sort the candidates by length before partitioning
  3483. (setq cands-left (cl-sort cands-left #'< :key #'length))
  3484. ;; partition the candidates into sorted and unsorted groups
  3485. (dotimes (_ (min (length cands-left) ivy-flx-limit))
  3486. (push (pop cands-left) cands-to-sort))
  3487. (nconc
  3488. ;; Compute all of the flx scores in one pass and sort
  3489. (mapcar #'car
  3490. (sort (mapcar
  3491. (lambda (cand)
  3492. (cons cand
  3493. (car (flx-score cand flx-name ivy--flx-cache))))
  3494. cands-to-sort)
  3495. (lambda (c1 c2)
  3496. ;; Break ties by length
  3497. (if (/= (cdr c1) (cdr c2))
  3498. (> (cdr c1)
  3499. (cdr c2))
  3500. (< (length (car c1))
  3501. (length (car c2)))))))
  3502. ;; Add the unsorted candidates
  3503. cands-left))
  3504. (error cands)))
  3505. (defun ivy--truncate-string (str width)
  3506. "Truncate STR to WIDTH."
  3507. (truncate-string-to-width str width nil nil t))
  3508. (defun ivy--format-function-generic (selected-fn other-fn cands separator)
  3509. "Transform candidates into a string for minibuffer.
  3510. SELECTED-FN is called for the selected candidate, OTHER-FN for the others.
  3511. Both functions take one string argument each. CANDS is a list of candidates
  3512. and SEPARATOR is used to join them."
  3513. (let ((i -1))
  3514. (mapconcat
  3515. (lambda (str)
  3516. (let ((curr (eq (cl-incf i) ivy--window-index)))
  3517. (if curr
  3518. (funcall selected-fn str)
  3519. (funcall other-fn str))))
  3520. cands
  3521. separator)))
  3522. (defun ivy-format-function-default (cands)
  3523. "Transform CANDS into a string for minibuffer."
  3524. (ivy--format-function-generic
  3525. (lambda (str)
  3526. (ivy--add-face str 'ivy-current-match))
  3527. #'identity
  3528. cands
  3529. "\n"))
  3530. (defun ivy-format-function-arrow (cands)
  3531. "Transform CANDS into a string for minibuffer."
  3532. (ivy--format-function-generic
  3533. (lambda (str)
  3534. (concat "> " (ivy--add-face str 'ivy-current-match)))
  3535. (lambda (str)
  3536. (concat " " str))
  3537. cands
  3538. "\n"))
  3539. (defun ivy-format-function-line (cands)
  3540. "Transform CANDS into a string for minibuffer.
  3541. Note that since Emacs 27, `ivy-current-match' needs to have :extend t attribute.
  3542. It has it by default, but the current theme also needs to set it."
  3543. (ivy--format-function-generic
  3544. (lambda (str)
  3545. (ivy--add-face (concat str "\n") 'ivy-current-match))
  3546. (lambda (str)
  3547. (concat str "\n"))
  3548. cands
  3549. ""))
  3550. (defun ivy--highlight-ignore-order (str)
  3551. "Highlight STR, using the ignore-order method."
  3552. (when (consp ivy--old-re)
  3553. (let ((i 1))
  3554. (dolist (re ivy--old-re)
  3555. (when (string-match (car re) str)
  3556. (add-face-text-property
  3557. (match-beginning 0) (match-end 0)
  3558. (ivy--minibuffer-face i)
  3559. nil str))
  3560. (cl-incf i))))
  3561. str)
  3562. (defun ivy--highlight-fuzzy (str)
  3563. "Highlight STR, using the fuzzy method."
  3564. (if (and ivy--flx-featurep
  3565. (eq (ivy-alist-setting ivy-re-builders-alist) 'ivy--regex-fuzzy))
  3566. (let ((flx-name (string-remove-prefix "^" ivy-text)))
  3567. (ivy--flx-propertize
  3568. (cons (flx-score str flx-name ivy--flx-cache) str)))
  3569. (ivy--highlight-default str)))
  3570. (defcustom ivy-use-group-face-if-no-groups t
  3571. "If t, and the expression has no subgroups, highlight whole match as a group.
  3572. It will then use the second face (first of the \"group\" faces)
  3573. of `ivy-minibuffer-faces'. Otherwise, always use the first face
  3574. in this case."
  3575. :type 'boolean)
  3576. (defun ivy--highlight-default (str)
  3577. "Highlight STR, using the default method."
  3578. (unless ivy--old-re
  3579. (setq ivy--old-re ivy-regex))
  3580. (let ((regexps
  3581. (if (listp ivy--old-re)
  3582. (mapcar #'car (cl-remove-if-not #'cdr ivy--old-re))
  3583. (list ivy--old-re)))
  3584. start)
  3585. (dolist (re regexps)
  3586. (ignore-errors
  3587. (while (and (string-match re str start)
  3588. (> (- (match-end 0) (match-beginning 0)) 0))
  3589. (setq start (match-end 0))
  3590. (let ((i 0)
  3591. (n 0)
  3592. prev)
  3593. (while (<= i ivy--subexps)
  3594. (let ((beg (match-beginning i))
  3595. (end (match-end i)))
  3596. (when (and beg end)
  3597. (unless (or (and prev (= prev beg))
  3598. (zerop i))
  3599. (cl-incf n))
  3600. (let ((face
  3601. (cond ((and ivy-use-group-face-if-no-groups
  3602. (zerop ivy--subexps))
  3603. (cadr ivy-minibuffer-faces))
  3604. ((zerop i)
  3605. (car ivy-minibuffer-faces))
  3606. (t
  3607. (ivy--minibuffer-face n)))))
  3608. (add-face-text-property beg end face nil str))
  3609. (unless (zerop i)
  3610. (setq prev end))))
  3611. (cl-incf i)))))))
  3612. str)
  3613. (defun ivy--format-minibuffer-line (str)
  3614. "Format line STR for use in minibuffer."
  3615. (let* ((str (ivy-cleanup-string (copy-sequence str)))
  3616. (str (if (eq ivy-display-style 'fancy)
  3617. (if (memq (ivy-state-caller ivy-last)
  3618. ivy-highlight-grep-commands)
  3619. (let* ((start (if (string-match "\\`[^:]+:\\(?:[^:]+:\\)?" str)
  3620. (match-end 0) 0))
  3621. (file (substring str 0 start))
  3622. (match (substring str start)))
  3623. (concat file (funcall ivy--highlight-function match)))
  3624. (funcall ivy--highlight-function str))
  3625. str))
  3626. (olen (length str))
  3627. (annot (plist-get completion-extra-properties :annotation-function)))
  3628. (add-text-properties
  3629. 0 olen
  3630. '(mouse-face
  3631. ivy-minibuffer-match-highlight
  3632. help-echo
  3633. (format
  3634. (if tooltip-mode
  3635. "mouse-1: %s\nmouse-3: %s"
  3636. "mouse-1: %s mouse-3: %s")
  3637. ivy-mouse-1-tooltip ivy-mouse-3-tooltip))
  3638. str)
  3639. (when annot
  3640. (setq str (concat str (funcall annot str)))
  3641. (add-face-text-property
  3642. olen (length str) 'ivy-completions-annotations nil str))
  3643. str))
  3644. (defun ivy-read-file-transformer (str)
  3645. "Transform candidate STR when reading files."
  3646. (if (ivy--dirname-p str)
  3647. (propertize str 'face 'ivy-subdir)
  3648. str))
  3649. (defun ivy--minibuffer-index-bounds (idx len wnd-len)
  3650. (let* ((half-height (/ wnd-len 2))
  3651. (start (max 0
  3652. (min (- idx half-height)
  3653. (- len (1- wnd-len)))))
  3654. (end (min (+ start (1- wnd-len)) len)))
  3655. (list start end (- idx start))))
  3656. (defun ivy--format (cands)
  3657. "Return a string for CANDS suitable for display in the minibuffer.
  3658. CANDS is a list of candidates that :display-transformer can turn into strings."
  3659. (setq ivy--length (length cands))
  3660. (when (>= ivy--index ivy--length)
  3661. (ivy-set-index (max (1- ivy--length) 0)))
  3662. (if (null cands)
  3663. (setf (ivy-state-current ivy-last) "")
  3664. (let ((cur (nth ivy--index cands)))
  3665. (setf (ivy-state-current ivy-last) (if (stringp cur)
  3666. (copy-sequence cur)
  3667. cur)))
  3668. (let* ((bnd (ivy--minibuffer-index-bounds
  3669. ivy--index ivy--length ivy-height))
  3670. (wnd-cands (cl-subseq cands (car bnd) (cadr bnd)))
  3671. (case-fold-search (ivy--case-fold-p ivy-text))
  3672. transformer-fn)
  3673. (setq ivy--window-index (nth 2 bnd))
  3674. (when (setq transformer-fn (ivy-state-display-transformer-fn ivy-last))
  3675. (with-ivy-window
  3676. (with-current-buffer (ivy-state-buffer ivy-last)
  3677. (setq wnd-cands (mapcar transformer-fn wnd-cands)))))
  3678. (ivy--wnd-cands-to-str wnd-cands))))
  3679. (defun ivy--wnd-cands-to-str (wnd-cands)
  3680. (let ((str (concat "\n"
  3681. (funcall (ivy-alist-setting ivy-format-functions-alist)
  3682. (condition-case nil
  3683. (mapcar
  3684. #'ivy--format-minibuffer-line
  3685. wnd-cands)
  3686. (error wnd-cands))))))
  3687. (put-text-property 0 (length str) 'read-only nil str)
  3688. str))
  3689. (defvar recentf-list)
  3690. (defvar bookmark-alist)
  3691. (defcustom ivy-virtual-abbreviate 'name
  3692. "The mode of abbreviation for virtual buffer names."
  3693. :type '(choice
  3694. (const :tag "Only name" name)
  3695. (const :tag "Abbreviated path" abbreviate)
  3696. (const :tag "Full path" full)
  3697. ;; eventually, uniquify
  3698. ))
  3699. (declare-function bookmark-maybe-load-default-file "bookmark")
  3700. (declare-function bookmark-get-filename "bookmark")
  3701. (defun ivy--virtual-buffers ()
  3702. "Adapted from `ido-add-virtual-buffers-to-list'."
  3703. (require 'bookmark)
  3704. (unless recentf-mode
  3705. (recentf-mode 1))
  3706. (bookmark-maybe-load-default-file)
  3707. (let* ((vb-bkm (delete " - no file -"
  3708. (delq nil (mapcar #'bookmark-get-filename
  3709. bookmark-alist))))
  3710. (vb-list (cond ((eq ivy-use-virtual-buffers 'recentf)
  3711. recentf-list)
  3712. ((eq ivy-use-virtual-buffers 'bookmarks)
  3713. vb-bkm)
  3714. (ivy-use-virtual-buffers
  3715. (append recentf-list vb-bkm))
  3716. (t nil)))
  3717. virtual-buffers)
  3718. (dolist (head vb-list)
  3719. (let* ((file-name (if (stringp head)
  3720. head
  3721. (cdr head)))
  3722. (name (cond ((eq ivy-virtual-abbreviate 'name)
  3723. (file-name-nondirectory file-name))
  3724. ((eq ivy-virtual-abbreviate 'abbreviate)
  3725. (abbreviate-file-name file-name))
  3726. (t
  3727. (expand-file-name file-name)))))
  3728. (when (equal name "")
  3729. (setq name
  3730. (if (consp head)
  3731. (car head)
  3732. (file-name-nondirectory (directory-file-name file-name)))))
  3733. (unless (or (equal name "")
  3734. (get-file-buffer file-name)
  3735. (assoc name virtual-buffers))
  3736. (push (cons (copy-sequence name) file-name) virtual-buffers))))
  3737. (when virtual-buffers
  3738. (dolist (comp virtual-buffers)
  3739. (put-text-property 0 (length (car comp))
  3740. 'face 'ivy-virtual
  3741. (car comp)))
  3742. (setq ivy--virtual-buffers (nreverse virtual-buffers))
  3743. (mapcar #'car ivy--virtual-buffers))))
  3744. (defcustom ivy-ignore-buffers '("\\` " "\\`\\*tramp/")
  3745. "List of regexps or functions matching buffer names to ignore."
  3746. :type '(repeat (choice regexp function)))
  3747. (defvar ivy-switch-buffer-faces-alist '((dired-mode . ivy-subdir)
  3748. (org-mode . ivy-org))
  3749. "Store face customizations for `ivy-switch-buffer'.
  3750. Each KEY is `major-mode', each VALUE is a face name.")
  3751. (defun ivy--buffer-list (str &optional virtual predicate)
  3752. "Return the buffers that match STR.
  3753. If VIRTUAL is non-nil, add virtual buffers.
  3754. If optional argument PREDICATE is non-nil, use it to test each
  3755. possible match. See `all-completions' for further information."
  3756. (delete-dups
  3757. (nconc
  3758. (all-completions str #'internal-complete-buffer predicate)
  3759. (and virtual
  3760. (ivy--virtual-buffers)))))
  3761. (defvar ivy-views (and nil
  3762. `(("ivy + *scratch* {}"
  3763. (vert
  3764. (file ,(expand-file-name "ivy.el"))
  3765. (buffer "*scratch*")))
  3766. ("swiper + *scratch* {}"
  3767. (horz
  3768. (file ,(expand-file-name "swiper.el"))
  3769. (buffer "*scratch*")))))
  3770. "Store window configurations selectable by `ivy-switch-buffer'.
  3771. The default value is given as an example.
  3772. Each element is a list of (NAME VIEW). NAME is a string, it's
  3773. recommended to end it with a distinctive snippet e.g. \"{}\" so
  3774. that it's easy to distinguish the window configurations.
  3775. VIEW is either a TREE or a window-configuration (see
  3776. `ivy--get-view-config').
  3777. TREE is a nested list with the following valid cars:
  3778. - vert: split the window vertically
  3779. - horz: split the window horizontally
  3780. - file: open the specified file
  3781. - buffer: open the specified buffer
  3782. TREE can be nested multiple times to have multiple window splits.")
  3783. (defun ivy-default-view-name ()
  3784. "Return default name for new view."
  3785. (let* ((default-view-name
  3786. (concat "{} "
  3787. (mapconcat #'identity
  3788. (sort
  3789. (mapcar (lambda (w)
  3790. (let* ((b (window-buffer w))
  3791. (f (buffer-file-name b)))
  3792. (if f
  3793. (file-name-nondirectory f)
  3794. (buffer-name b))))
  3795. (window-list))
  3796. #'string-lessp)
  3797. " ")))
  3798. (view-name-re (concat "\\`"
  3799. (regexp-quote default-view-name)
  3800. " \\([0-9]+\\)"))
  3801. old-view)
  3802. (cond ((setq old-view
  3803. (cl-find-if
  3804. (lambda (x)
  3805. (string-match view-name-re (car x)))
  3806. ivy-views))
  3807. (format "%s %d"
  3808. default-view-name
  3809. (1+ (string-to-number
  3810. (match-string 1 (car old-view))))))
  3811. ((assoc default-view-name ivy-views)
  3812. (concat default-view-name " 1"))
  3813. (t
  3814. default-view-name))))
  3815. (defun ivy--get-view-config ()
  3816. "Get `current-window-configuration' for `ivy-views'."
  3817. (dolist (w (window-list))
  3818. (set-window-parameter w 'ivy-view-data
  3819. (with-current-buffer (window-buffer w)
  3820. (cond (buffer-file-name
  3821. (list 'file buffer-file-name (point)))
  3822. ((eq major-mode 'dired-mode)
  3823. (list 'file default-directory (point)))
  3824. (t
  3825. (list 'buffer (buffer-name) (point)))))))
  3826. (let ((window-persistent-parameters
  3827. (append window-persistent-parameters
  3828. (list (cons 'ivy-view-data t)))))
  3829. (current-window-configuration)))
  3830. (defun ivy-push-view (&optional arg)
  3831. "Push the current window tree on `ivy-views'.
  3832. When ARG is non-nil, replace a selected item on `ivy-views'.
  3833. Currently, the split configuration (i.e. horizontal or vertical)
  3834. and point positions are saved, but the split positions aren't.
  3835. Use `ivy-pop-view' to delete any item from `ivy-views'."
  3836. (interactive "P")
  3837. (let* ((view (ivy--get-view-config))
  3838. (view-name
  3839. (if arg
  3840. (ivy-read "Update view: " ivy-views)
  3841. (ivy-read "Name view: " nil
  3842. :initial-input (ivy-default-view-name)))))
  3843. (when view-name
  3844. (let ((x (assoc view-name ivy-views)))
  3845. (if x
  3846. (setcdr x (list view))
  3847. (push (list view-name view) ivy-views))))))
  3848. (defun ivy-pop-view-action (view)
  3849. "Delete VIEW from `ivy-views'."
  3850. (setq ivy-views (delete view ivy-views))
  3851. (setq ivy--all-candidates
  3852. (delete (car view) ivy--all-candidates))
  3853. (setq ivy--old-cands nil))
  3854. (defun ivy-pop-view ()
  3855. "Delete a view to delete from `ivy-views'."
  3856. (interactive)
  3857. (ivy-read "Pop view: " ivy-views
  3858. :preselect (caar ivy-views)
  3859. :action #'ivy-pop-view-action
  3860. :caller 'ivy-pop-view))
  3861. (defun ivy-source-views ()
  3862. "Return the name of the views saved in `ivy-views'."
  3863. (mapcar #'car ivy-views))
  3864. (ivy-set-sources
  3865. 'ivy-switch-buffer
  3866. '((original-source)
  3867. (ivy-source-views)))
  3868. (defun ivy-set-view-recur (view)
  3869. "Set VIEW recursively."
  3870. (cond ((window-configuration-p view)
  3871. (set-window-configuration view)
  3872. (dolist (w (window-list))
  3873. (with-selected-window w
  3874. (ivy-set-view-recur
  3875. (window-parameter w 'ivy-view-data)))))
  3876. ((eq (car view) 'vert)
  3877. (let* ((wnd1 (selected-window))
  3878. (wnd2 (split-window-vertically))
  3879. (views (cdr view))
  3880. (v (pop views))
  3881. (temp-wnd))
  3882. (with-selected-window wnd1
  3883. (ivy-set-view-recur v))
  3884. (while (setq v (pop views))
  3885. (with-selected-window wnd2
  3886. (when views
  3887. (setq temp-wnd (split-window-vertically)))
  3888. (ivy-set-view-recur v)
  3889. (when views
  3890. (setq wnd2 temp-wnd))))))
  3891. ((eq (car view) 'horz)
  3892. (let* ((wnd1 (selected-window))
  3893. (wnd2 (split-window-horizontally))
  3894. (views (cdr view))
  3895. (v (pop views))
  3896. (temp-wnd))
  3897. (with-selected-window wnd1
  3898. (ivy-set-view-recur v))
  3899. (while (setq v (pop views))
  3900. (with-selected-window wnd2
  3901. (when views
  3902. (setq temp-wnd (split-window-horizontally)))
  3903. (ivy-set-view-recur v)
  3904. (when views
  3905. (setq wnd2 temp-wnd))))))
  3906. ((eq (car view) 'file)
  3907. (let* ((name (nth 1 view))
  3908. (virtual (assoc name ivy--virtual-buffers))
  3909. buffer)
  3910. (cond ((setq buffer (get-buffer name))
  3911. (switch-to-buffer buffer nil 'force-same-window))
  3912. (virtual
  3913. (find-file (cdr virtual)))
  3914. ((file-exists-p name)
  3915. (find-file name))))
  3916. (when (and (> (length view) 2)
  3917. (numberp (nth 2 view)))
  3918. (goto-char (nth 2 view))))
  3919. ((eq (car view) 'buffer)
  3920. (switch-to-buffer (nth 1 view))
  3921. (when (and (> (length view) 2)
  3922. (numberp (nth 2 view)))
  3923. (goto-char (nth 2 view))))
  3924. ((eq (car view) 'sexp)
  3925. (eval (nth 1 view)))))
  3926. (defun ivy--switch-buffer-action (buffer)
  3927. "Switch to BUFFER.
  3928. BUFFER may be a string or nil."
  3929. (if (zerop (length buffer))
  3930. (switch-to-buffer
  3931. ivy-text nil 'force-same-window)
  3932. (let ((virtual (assoc buffer ivy--virtual-buffers))
  3933. (view (assoc buffer ivy-views)))
  3934. (cond ((and virtual
  3935. (not (get-buffer buffer)))
  3936. (find-file (cdr virtual)))
  3937. (view
  3938. (delete-other-windows)
  3939. (let (
  3940. ;; silence "Directory has changed on disk"
  3941. (inhibit-message t))
  3942. (ivy-set-view-recur (cadr view))))
  3943. (t
  3944. (switch-to-buffer
  3945. buffer nil 'force-same-window))))))
  3946. (defun ivy--switch-buffer-other-window-action (buffer)
  3947. "Switch to BUFFER in other window.
  3948. BUFFER may be a string or nil."
  3949. (if (zerop (length buffer))
  3950. (switch-to-buffer-other-window ivy-text)
  3951. (let ((virtual (assoc buffer ivy--virtual-buffers)))
  3952. (if (and virtual
  3953. (not (get-buffer buffer)))
  3954. (find-file-other-window (cdr virtual))
  3955. (switch-to-buffer-other-window buffer)))))
  3956. (defun ivy--rename-buffer-action (buffer)
  3957. "Rename BUFFER."
  3958. (let ((new-name (read-string "Rename buffer (to new name): ")))
  3959. (with-current-buffer buffer
  3960. (rename-buffer new-name))))
  3961. (defun ivy--find-file-action (buffer)
  3962. "Find file from BUFFER's directory."
  3963. (let* ((virtual (assoc buffer ivy--virtual-buffers))
  3964. (default-directory (if virtual
  3965. (file-name-directory (cdr virtual))
  3966. (buffer-local-value 'default-directory
  3967. (or (get-buffer buffer)
  3968. (current-buffer))))))
  3969. (call-interactively (if (functionp 'counsel-find-file)
  3970. #'counsel-find-file
  3971. #'find-file))))
  3972. (defun ivy--kill-buffer-or-virtual (buffer)
  3973. (if (get-buffer buffer)
  3974. (kill-buffer buffer)
  3975. (setq recentf-list (delete
  3976. (cdr (assoc buffer ivy--virtual-buffers))
  3977. recentf-list))))
  3978. (defun ivy--kill-current-candidate ()
  3979. (setf (ivy-state-preselect ivy-last) ivy--index)
  3980. (setq ivy--old-re nil)
  3981. (setq ivy--all-candidates (delete (ivy-state-current ivy-last) ivy--all-candidates))
  3982. (let ((ivy--recompute-index-inhibit t))
  3983. (ivy--exhibit)))
  3984. (defun ivy--kill-current-candidate-buffer ()
  3985. (setf (ivy-state-preselect ivy-last) ivy--index)
  3986. (setq ivy--old-re nil)
  3987. (setq ivy--all-candidates (ivy--buffer-list "" ivy-use-virtual-buffers nil))
  3988. (let ((ivy--recompute-index-inhibit t))
  3989. (ivy--exhibit)))
  3990. (defun ivy--kill-buffer-action (buffer)
  3991. "Kill BUFFER."
  3992. (ivy--kill-buffer-or-virtual buffer)
  3993. (unless (buffer-live-p (ivy-state-buffer ivy-last))
  3994. (setf (ivy-state-buffer ivy-last)
  3995. (with-ivy-window (current-buffer))))
  3996. (ivy--kill-current-candidate-buffer))
  3997. (defvar ivy-switch-buffer-map
  3998. (let ((map (make-sparse-keymap)))
  3999. (ivy-define-key map (kbd "C-k") 'ivy-switch-buffer-kill)
  4000. map))
  4001. (defun ivy-switch-buffer-kill ()
  4002. "When at end-of-line, kill the current buffer in `ivy-switch-buffer'.
  4003. Otherwise, forward to `ivy-kill-line'."
  4004. (interactive)
  4005. (if (not (eolp))
  4006. (ivy-kill-line)
  4007. (ivy--kill-buffer-action
  4008. (ivy-state-current ivy-last))))
  4009. (ivy-set-actions
  4010. 'ivy-switch-buffer
  4011. '(("f"
  4012. ivy--find-file-action
  4013. "find file")
  4014. ("j"
  4015. ivy--switch-buffer-other-window-action
  4016. "other window")
  4017. ("k"
  4018. ivy--kill-buffer-action
  4019. "kill")
  4020. ("r"
  4021. ivy--rename-buffer-action
  4022. "rename")))
  4023. (ivy-set-actions
  4024. t
  4025. '(("i" ivy--action-insert "insert")
  4026. ("w" ivy--action-copy "copy")))
  4027. (defun ivy--trim-grep-line-number (x)
  4028. (if (string-match ":[0-9]+:" x)
  4029. (substring x (match-end 0))
  4030. x))
  4031. (defun ivy--action-insert (x)
  4032. (insert
  4033. (if (stringp x)
  4034. (ivy--trim-grep-line-number x)
  4035. x (car x))))
  4036. (defun ivy--action-copy (x)
  4037. (kill-new
  4038. (if (stringp x)
  4039. (ivy--trim-grep-line-number x)
  4040. (car x))))
  4041. (defun ivy--switch-buffer-matcher (regexp candidates)
  4042. "Return REGEXP matching CANDIDATES.
  4043. Skip buffers that match `ivy-ignore-buffers'."
  4044. (if (string-match-p "^:" ivy-text)
  4045. (delete-dups
  4046. (cl-remove-if-not
  4047. (lambda (s)
  4048. (let ((b (get-buffer s)))
  4049. (and b
  4050. (string-match-p regexp (buffer-local-value 'default-directory b))
  4051. (not (string-match-p "^\\*" s)))))
  4052. candidates))
  4053. (let ((res (ivy--re-filter regexp candidates)))
  4054. (if (or (null ivy-use-ignore)
  4055. (null ivy-ignore-buffers))
  4056. res
  4057. (or (cl-remove-if
  4058. (lambda (buf)
  4059. (cl-find-if
  4060. (lambda (f-or-r)
  4061. (if (functionp f-or-r)
  4062. (funcall f-or-r buf)
  4063. (string-match-p f-or-r buf)))
  4064. ivy-ignore-buffers))
  4065. res)
  4066. (and (eq ivy-use-ignore t)
  4067. res))))))
  4068. (defun ivy-append-face (str face)
  4069. "Append to STR the property FACE."
  4070. (when face
  4071. (setq str (copy-sequence str))
  4072. (add-face-text-property 0 (length str) face t str))
  4073. str)
  4074. (defun ivy--remote-buffer-p (buffer)
  4075. "Return non-nil if BUFFER object is visiting a remote file.
  4076. If that is the case, value is a string identifying the remote
  4077. connection."
  4078. (let ((dir (buffer-local-value 'default-directory buffer)))
  4079. (ignore-errors (file-remote-p dir))))
  4080. (defun ivy-switch-buffer-transformer (str)
  4081. "Transform candidate STR when switching buffers."
  4082. (let ((buf (get-buffer str)))
  4083. (cond ((not buf) str)
  4084. ((let ((remote (ivy--remote-buffer-p buf)))
  4085. (when remote
  4086. (format "%s (%s)" (ivy-append-face str 'ivy-remote) remote))))
  4087. ((not (verify-visited-file-modtime buf))
  4088. (ivy-append-face str 'ivy-modified-outside-buffer))
  4089. ((buffer-modified-p buf)
  4090. (ivy-append-face str 'ivy-modified-buffer))
  4091. (t
  4092. (let* ((mode (buffer-local-value 'major-mode buf))
  4093. (face (cdr (assq mode ivy-switch-buffer-faces-alist))))
  4094. (ivy-append-face str face))))))
  4095. (defun ivy-switch-buffer-occur (cands)
  4096. "Occur function for `ivy-switch-buffer' using `ibuffer'.
  4097. CANDS are the candidates to be displayed."
  4098. (unless cands
  4099. (setq cands (all-completions ivy-text #'internal-complete-buffer)))
  4100. (ibuffer
  4101. nil (buffer-name)
  4102. `((or ,@(cl-mapcan
  4103. (lambda (cand)
  4104. (unless (eq (get-text-property 0 'face cand) 'ivy-virtual)
  4105. `((name . ,(format "\\_<%s\\_>" (regexp-quote cand))))))
  4106. cands)))))
  4107. ;;;###autoload
  4108. (defun ivy-switch-buffer ()
  4109. "Switch to another buffer."
  4110. (interactive)
  4111. (ivy-read "Switch to buffer: " #'internal-complete-buffer
  4112. :keymap ivy-switch-buffer-map
  4113. :preselect (buffer-name (other-buffer (current-buffer)))
  4114. :action #'ivy--switch-buffer-action
  4115. :matcher #'ivy--switch-buffer-matcher
  4116. :caller 'ivy-switch-buffer))
  4117. (ivy-configure 'ivy-switch-buffer
  4118. :parent 'internal-complete-buffer
  4119. :occur #'ivy-switch-buffer-occur)
  4120. ;;;###autoload
  4121. (defun ivy-switch-view ()
  4122. "Switch to one of the window views stored by `ivy-push-view'."
  4123. (interactive)
  4124. (let ((ivy-initial-inputs-alist
  4125. '((ivy-switch-buffer . "{}"))))
  4126. (ivy-switch-buffer)))
  4127. ;;;###autoload
  4128. (defun ivy-switch-buffer-other-window ()
  4129. "Switch to another buffer in another window."
  4130. (interactive)
  4131. (ivy-read "Switch to buffer in other window: " #'internal-complete-buffer
  4132. :matcher #'ivy--switch-buffer-matcher
  4133. :preselect (buffer-name (other-buffer (current-buffer)))
  4134. :action #'ivy--switch-buffer-other-window-action
  4135. :keymap ivy-switch-buffer-map
  4136. :caller 'ivy-switch-buffer-other-window))
  4137. (ivy-configure 'ivy-switch-buffer-other-window
  4138. :parent 'ivy-switch-buffer)
  4139. (defun ivy--yank-handle-case-fold (text)
  4140. (if (and (> (length ivy-text) 0)
  4141. (string= (downcase ivy-text) ivy-text))
  4142. (downcase text)
  4143. text))
  4144. (defun ivy--yank-by (fn &rest args)
  4145. "Pull buffer text from current line into search string.
  4146. The region to extract is determined by the respective values of
  4147. point before and after applying FN to ARGS."
  4148. (let (text)
  4149. (with-ivy-window
  4150. (let ((beg (point))
  4151. (bol (line-beginning-position))
  4152. (eol (line-end-position))
  4153. end)
  4154. (unwind-protect
  4155. (progn (apply fn args)
  4156. (setq end (goto-char (max bol (min (point) eol))))
  4157. (setq text (buffer-substring-no-properties beg end))
  4158. (ivy--pulse-region beg end))
  4159. (unless text
  4160. (goto-char beg)))))
  4161. (when text
  4162. (insert (replace-regexp-in-string
  4163. " +" " "
  4164. (ivy--yank-handle-case-fold text)
  4165. t t)))))
  4166. (defun ivy-yank-word (&optional arg)
  4167. "Pull next word from buffer into search string.
  4168. If optional ARG is non-nil, pull in the next ARG
  4169. words (previous if ARG is negative)."
  4170. (interactive "p")
  4171. (ivy--yank-by #'forward-word arg))
  4172. (defun ivy-yank-symbol (&optional arg)
  4173. "Pull next symbol from buffer into search string.
  4174. If optional ARG is non-nil, pull in the next ARG
  4175. symbols (previous if ARG is negative)."
  4176. (interactive "p")
  4177. (ivy--yank-by #'forward-symbol (or arg 1)))
  4178. (defun ivy-yank-char (&optional arg)
  4179. "Pull next character from buffer into search string.
  4180. If optional ARG is non-nil, pull in the next ARG
  4181. characters (previous if ARG is negative)."
  4182. (interactive "p")
  4183. (ivy--yank-by #'forward-char arg))
  4184. (defvar ivy--pulse-overlay nil
  4185. "Overlay used to highlight yanked word.")
  4186. (defvar ivy--pulse-timer nil
  4187. "Timer used to dispose of `ivy--pulse-overlay'.")
  4188. (defcustom ivy-pulse-delay 0.5
  4189. "Number of seconds to display `ivy-yanked-word' highlight.
  4190. When nil, disable highlighting."
  4191. :type '(choice
  4192. (number :tag "Delay in seconds")
  4193. (const :tag "Disable" nil)))
  4194. (defun ivy--pulse-region (start end)
  4195. "Temporarily highlight text between START and END.
  4196. The \"pulse\" duration is determined by `ivy-pulse-delay'."
  4197. (when ivy-pulse-delay
  4198. (if ivy--pulse-overlay
  4199. (let ((ostart (overlay-start ivy--pulse-overlay))
  4200. (oend (overlay-end ivy--pulse-overlay)))
  4201. (when (< end start)
  4202. (cl-rotatef start end))
  4203. ;; Extend the existing overlay's region to include START..END,
  4204. ;; but only if the two regions are contiguous.
  4205. (move-overlay ivy--pulse-overlay
  4206. (if (= start oend) ostart start)
  4207. (if (= end ostart) oend end)))
  4208. (setq ivy--pulse-overlay (make-overlay start end))
  4209. (overlay-put ivy--pulse-overlay 'face 'ivy-yanked-word))
  4210. (when ivy--pulse-timer
  4211. (cancel-timer ivy--pulse-timer))
  4212. (setq ivy--pulse-timer
  4213. (run-at-time ivy-pulse-delay nil #'ivy--pulse-cleanup))))
  4214. (defun ivy--pulse-cleanup ()
  4215. "Cancel `ivy--pulse-timer' and delete `ivy--pulse-overlay'."
  4216. (when ivy--pulse-timer
  4217. (cancel-timer ivy--pulse-timer)
  4218. (setq ivy--pulse-timer nil))
  4219. (when ivy--pulse-overlay
  4220. (delete-overlay ivy--pulse-overlay)
  4221. (setq ivy--pulse-overlay nil)))
  4222. (defun ivy-kill-ring-save ()
  4223. "Store the current candidates into the kill ring.
  4224. If the region is active, forward to `kill-ring-save' instead."
  4225. (interactive)
  4226. (if (region-active-p)
  4227. (call-interactively 'kill-ring-save)
  4228. (kill-new
  4229. (mapconcat
  4230. #'identity
  4231. ivy--old-cands
  4232. "\n"))))
  4233. (defun ivy-insert-current ()
  4234. "Make the current candidate into current input.
  4235. Don't finish completion."
  4236. (interactive)
  4237. (delete-minibuffer-contents)
  4238. (let ((end (and ivy--directory
  4239. (ivy--dirname-p (ivy-state-current ivy-last))
  4240. -1)))
  4241. (insert (substring-no-properties
  4242. (ivy-state-current ivy-last) 0 end))))
  4243. (defun ivy-insert-current-full ()
  4244. "Insert the full Yank the current directory into the minibuffer."
  4245. (interactive)
  4246. (insert ivy--directory))
  4247. (defcustom ivy-preferred-re-builders
  4248. '((ivy--regex-plus . "ivy")
  4249. (ivy--regex-ignore-order . "order")
  4250. (ivy--regex-fuzzy . "fuzzy"))
  4251. "Alist of preferred re-builders with display names.
  4252. This list can be rotated with `ivy-rotate-preferred-builders'."
  4253. :type '(alist :key-type function :value-type string))
  4254. (defun ivy-rotate-preferred-builders ()
  4255. "Switch to the next re builder in `ivy-preferred-re-builders'."
  4256. (interactive)
  4257. (when ivy-preferred-re-builders
  4258. (setq ivy--old-re nil)
  4259. (setq ivy--regex-function
  4260. (let ((cell (assq ivy--regex-function ivy-preferred-re-builders)))
  4261. (car (or (cadr (memq cell ivy-preferred-re-builders))
  4262. (car ivy-preferred-re-builders)))))))
  4263. (defun ivy-toggle-fuzzy ()
  4264. "Toggle the re builder between `ivy--regex-fuzzy' and `ivy--regex-plus'."
  4265. (interactive)
  4266. (setq ivy--old-re nil)
  4267. (if (eq ivy--regex-function 'ivy--regex-fuzzy)
  4268. (setq ivy--regex-function 'ivy--regex-plus)
  4269. (setq ivy--regex-function 'ivy--regex-fuzzy)))
  4270. (defun ivy--label-and-delete-dups (entries)
  4271. "Label ENTRIES with history indices."
  4272. (let ((ht (make-hash-table :test 'equal))
  4273. (idx 0)
  4274. entry
  4275. accum)
  4276. (while (setq entry (pop entries))
  4277. (unless (gethash entry ht)
  4278. (puthash entry t ht)
  4279. (push `(,entry . ,idx) accum))
  4280. (cl-incf idx))
  4281. (nreverse accum)))
  4282. (defvar ivy--reverse-i-search-symbol nil
  4283. "Store the history symbol.")
  4284. (defun ivy-reverse-i-search-kill ()
  4285. "Remove the current item from history"
  4286. (interactive)
  4287. (if (not (eolp))
  4288. (ivy-kill-line)
  4289. (let ((current (ivy-state-current ivy-last)))
  4290. (if (symbolp ivy--reverse-i-search-symbol)
  4291. (set
  4292. ivy--reverse-i-search-symbol
  4293. (delete current (symbol-value ivy--reverse-i-search-symbol)))
  4294. (ring-remove
  4295. ivy--reverse-i-search-symbol
  4296. (ring-member ivy--reverse-i-search-symbol (ivy-state-current ivy-last)))))
  4297. (ivy--kill-current-candidate)))
  4298. (defvar ivy-reverse-i-search-map
  4299. (let ((map (make-sparse-keymap)))
  4300. (ivy-define-key map (kbd "C-k") 'ivy-reverse-i-search-kill)
  4301. map))
  4302. (defun ivy-history-contents (history)
  4303. "Copy contents of HISTORY.
  4304. A copy is necessary so that we don't clobber any string attributes.
  4305. Also set `ivy--reverse-i-search-symbol' to HISTORY."
  4306. (setq ivy--reverse-i-search-symbol history)
  4307. (cond ((symbolp history)
  4308. (ivy--label-and-delete-dups
  4309. (copy-sequence (symbol-value history))))
  4310. ((ring-p history)
  4311. (ivy--label-and-delete-dups
  4312. (when (> (ring-size history) 0)
  4313. (ring-elements history))))
  4314. ((sequencep history)
  4315. (ivy--label-and-delete-dups
  4316. (copy-sequence history)))
  4317. (t
  4318. (error "Expected a symbol, ring, or sequence: %S" history))))
  4319. (defun ivy-reverse-i-search ()
  4320. "Enter a recursive `ivy-read' session using the current history.
  4321. The selected history element will be inserted into the minibuffer.
  4322. \\<ivy-reverse-i-search-map>
  4323. You can also delete an element from history with \\[ivy-reverse-i-search-kill]."
  4324. (interactive)
  4325. (cond
  4326. ((= (minibuffer-depth) 0)
  4327. (user-error
  4328. "This command is intended to be called from within `ivy-read'"))
  4329. ;; don't recur
  4330. ((and (> (minibuffer-depth) 1)
  4331. (eq (ivy-state-caller ivy-last) 'ivy-reverse-i-search)))
  4332. (t
  4333. (let ((enable-recursive-minibuffers t)
  4334. (old-last ivy-last))
  4335. (ivy-read "Reverse-i-search: "
  4336. (ivy-history-contents (ivy-state-history ivy-last))
  4337. :keymap ivy-reverse-i-search-map
  4338. :action (lambda (x)
  4339. (ivy--reset-state
  4340. (setq ivy-last old-last))
  4341. (delete-minibuffer-contents)
  4342. (insert (substring-no-properties (car x)))
  4343. (ivy--cd-maybe))
  4344. :caller 'ivy-reverse-i-search)))))
  4345. (defun ivy-restrict-to-matches ()
  4346. "Restrict candidates to current input and erase input."
  4347. (interactive)
  4348. (delete-minibuffer-contents)
  4349. (if (ivy-state-dynamic-collection ivy-last)
  4350. (progn
  4351. (setf (ivy-state-dynamic-collection ivy-last) nil)
  4352. (setf (ivy-state-collection ivy-last)
  4353. (setq ivy--all-candidates ivy--old-cands)))
  4354. (setq ivy--all-candidates
  4355. (ivy--filter ivy-text ivy--all-candidates))))
  4356. ;;* Occur
  4357. (defvar-local ivy-occur-last nil
  4358. "Buffer-local value of `ivy-last'.
  4359. Can't re-use `ivy-last' because using e.g. `swiper' in the same
  4360. buffer would modify `ivy-last'.")
  4361. (defvar ivy-occur-mode-map
  4362. (let ((map (make-sparse-keymap)))
  4363. (ivy-define-key map [mouse-1] 'ivy-occur-click)
  4364. (ivy-define-key map (kbd "RET") 'ivy-occur-press-and-switch)
  4365. (ivy-define-key map (kbd "j") 'ivy-occur-next-line)
  4366. (ivy-define-key map (kbd "k") 'ivy-occur-previous-line)
  4367. (define-key map (kbd "h") 'backward-char)
  4368. (define-key map (kbd "l") 'forward-char)
  4369. (ivy-define-key map (kbd "f") 'ivy-occur-press)
  4370. (ivy-define-key map (kbd "g") 'ivy-occur-revert-buffer)
  4371. (ivy-define-key map (kbd "a") 'ivy-occur-read-action)
  4372. (ivy-define-key map (kbd "o") 'ivy-occur-dispatch)
  4373. (ivy-define-key map (kbd "c") 'ivy-occur-toggle-calling)
  4374. (define-key map (kbd "q") 'quit-window)
  4375. (define-key map (kbd "R") 'read-only-mode)
  4376. (ivy-define-key map (kbd "C-d") 'ivy-occur-delete-candidate)
  4377. map)
  4378. "Keymap for Ivy Occur mode.")
  4379. (defun ivy-occur-toggle-calling ()
  4380. "Toggle `ivy-calling'."
  4381. (interactive)
  4382. (if (setq ivy-calling (not ivy-calling))
  4383. (progn
  4384. (setq mode-name "Ivy-Occur [calling]")
  4385. (ivy-occur-press))
  4386. (setq mode-name "Ivy-Occur"))
  4387. (force-mode-line-update))
  4388. (defun ivy--find-occur-buffer ()
  4389. (let ((cb (current-buffer)))
  4390. (cl-find-if
  4391. (lambda (b)
  4392. (with-current-buffer b
  4393. (and (eq major-mode 'ivy-occur-grep-mode)
  4394. (equal cb (ivy-state-buffer ivy-occur-last)))))
  4395. (buffer-list))))
  4396. (defun ivy--select-occur-buffer ()
  4397. (let* ((ob (ivy--find-occur-buffer))
  4398. (ow (cl-find-if (lambda (w) (equal ob (window-buffer w)))
  4399. (window-list))))
  4400. (if ow
  4401. (select-window ow)
  4402. (pop-to-buffer ob))))
  4403. (defun ivy-occur-next-line (&optional arg)
  4404. "Move the cursor down ARG lines.
  4405. When `ivy-calling' isn't nil, call `ivy-occur-press'."
  4406. (interactive "p")
  4407. (let ((offset (cond ((derived-mode-p 'ivy-occur-grep-mode) 5)
  4408. ((derived-mode-p 'ivy-occur-mode) 2))))
  4409. (if offset
  4410. (progn
  4411. (if (< (line-number-at-pos) offset)
  4412. (progn
  4413. (goto-char (point-min))
  4414. (forward-line (1- offset)))
  4415. (forward-line arg)
  4416. (when (eolp)
  4417. (forward-line -1)))
  4418. (when ivy-calling
  4419. (ivy-occur-press)))
  4420. (ivy--select-occur-buffer)
  4421. (ivy-occur-next-line arg)
  4422. (ivy-occur-press-and-switch))))
  4423. (defun ivy-occur-previous-line (&optional arg)
  4424. "Move the cursor up ARG lines.
  4425. When `ivy-calling' isn't nil, call `ivy-occur-press'."
  4426. (interactive "p")
  4427. (let ((offset (cond ((derived-mode-p 'ivy-occur-grep-mode) 5)
  4428. ((derived-mode-p 'ivy-occur-mode) 2))))
  4429. (if offset
  4430. (progn
  4431. (forward-line (- arg))
  4432. (when (< (line-number-at-pos) offset)
  4433. (goto-char (point-min))
  4434. (forward-line (1- offset)))
  4435. (when ivy-calling
  4436. (ivy-occur-press)))
  4437. (ivy--select-occur-buffer)
  4438. (ivy-occur-previous-line arg)
  4439. (ivy-occur-press-and-switch))))
  4440. (defun ivy-occur-next-error (n &optional reset)
  4441. "A `next-error-function' for `ivy-occur-mode'."
  4442. (interactive "p")
  4443. (when reset
  4444. (goto-char (point-min)))
  4445. (setq n (or n 1))
  4446. (let ((ivy-calling t))
  4447. (cond ((< n 0) (ivy-occur-previous-line (- n)))
  4448. (t (ivy-occur-next-line n))))
  4449. ;; The window's point overrides the buffer's point every time it's redisplayed
  4450. (dolist (window (get-buffer-window-list nil nil t))
  4451. (set-window-point window (point))))
  4452. (define-derived-mode ivy-occur-mode fundamental-mode "Ivy-Occur"
  4453. "Major mode for output from \\[ivy-occur].
  4454. \\{ivy-occur-mode-map}"
  4455. (setq-local view-read-only nil))
  4456. (defvar ivy-occur-grep-mode-map
  4457. (let ((map (copy-keymap ivy-occur-mode-map)))
  4458. (ivy-define-key map (kbd "C-x C-q") 'ivy-wgrep-change-to-wgrep-mode)
  4459. (ivy-define-key map "w" 'ivy-wgrep-change-to-wgrep-mode)
  4460. map)
  4461. "Keymap for Ivy Occur Grep mode.")
  4462. (defun ivy-occur-delete-candidate ()
  4463. (interactive)
  4464. (let ((inhibit-read-only t))
  4465. (delete-region (line-beginning-position)
  4466. (1+ (line-end-position)))))
  4467. (define-derived-mode ivy-occur-grep-mode grep-mode "Ivy-Occur"
  4468. "Major mode for output from \\[ivy-occur].
  4469. \\{ivy-occur-grep-mode-map}"
  4470. (setq-local view-read-only nil)
  4471. (when (fboundp 'wgrep-setup)
  4472. (wgrep-setup)))
  4473. (defun ivy--starts-with-dotslash (str)
  4474. (string-match-p "\\`\\.[/\\]" str))
  4475. (defun ivy--occur-insert-lines (cands)
  4476. "Insert CANDS into `ivy-occur' buffer."
  4477. (font-lock-mode -1)
  4478. (dolist (cand cands)
  4479. (setq cand
  4480. (if (string-match "\\`\\(.*:[0-9]+:\\)\\(.*\\)\\'" cand)
  4481. (let ((file-and-line (match-string 1 cand))
  4482. (grep-line (match-string 2 cand)))
  4483. (concat
  4484. (propertize file-and-line 'face 'ivy-grep-info)
  4485. (ivy--highlight-fuzzy grep-line)))
  4486. (ivy--highlight-fuzzy (copy-sequence cand))))
  4487. (add-text-properties
  4488. 0 (length cand)
  4489. '(mouse-face
  4490. highlight
  4491. help-echo "mouse-1: call ivy-action")
  4492. cand)
  4493. (insert (if (ivy--starts-with-dotslash cand) "" " ")
  4494. cand ?\n)))
  4495. (defun ivy--occur-default (cands)
  4496. "Insert CANDS into the current occur buffer."
  4497. (unless cands
  4498. (let ((coll (ivy-state-collection ivy-last)))
  4499. (when (arrayp coll)
  4500. (setq coll (all-completions "" coll (ivy-state-predicate ivy-last))))
  4501. (setq cands (ivy--filter (ivy-state-text ivy-last) coll))))
  4502. (ivy-occur-mode)
  4503. (insert (format "%d candidates:\n" (length cands)))
  4504. (ivy--occur-insert-lines cands)
  4505. (read-only-mode))
  4506. (defun ivy-occur ()
  4507. "Stop completion and put the current candidates into a new buffer.
  4508. The new buffer remembers current action(s).
  4509. While in the *ivy-occur* buffer, selecting a candidate with RET or
  4510. a mouse click will call the appropriate action for that candidate.
  4511. There is no limit on the number of *ivy-occur* buffers."
  4512. (interactive)
  4513. (if (not (window-minibuffer-p))
  4514. (user-error "No completion session is active")
  4515. (let* ((caller (ivy-state-caller ivy-last))
  4516. (occur-fn (or (plist-get ivy--occurs-list caller)
  4517. #'ivy--occur-default))
  4518. (buffer
  4519. (generate-new-buffer
  4520. (format "*ivy-occur%s \"%s\"*"
  4521. (if caller
  4522. (concat " " (prin1-to-string caller))
  4523. "")
  4524. ivy-text))))
  4525. (with-current-buffer buffer
  4526. (funcall occur-fn ivy--old-cands)
  4527. (setf (ivy-state-text ivy-last) ivy-text)
  4528. (setq ivy-occur-last ivy-last))
  4529. (ivy-exit-with-action
  4530. (lambda (_)
  4531. (pop-to-buffer buffer)
  4532. (setq next-error-last-buffer buffer)
  4533. (setq-local next-error-function #'ivy-occur-next-error))))))
  4534. (defun ivy-occur-revert-buffer ()
  4535. "Refresh the buffer making it up-to date with the collection.
  4536. Currently only works for `swiper'. In that specific case, the
  4537. *ivy-occur* buffer becomes nearly useless as the original buffer
  4538. is updated, since the line numbers no longer match.
  4539. Calling this function is as if you called `ivy-occur' on the
  4540. updated original buffer."
  4541. (interactive)
  4542. (let ((caller (ivy-state-caller ivy-occur-last))
  4543. (ivy-last ivy-occur-last))
  4544. (let ((inhibit-read-only t)
  4545. (line (line-number-at-pos)))
  4546. (erase-buffer)
  4547. (funcall (or (plist-get ivy--occurs-list caller)
  4548. #'ivy--occur-default) nil)
  4549. (goto-char (point-min))
  4550. (forward-line (1- line)))
  4551. (setq ivy-occur-last ivy-last)))
  4552. (declare-function wgrep-change-to-wgrep-mode "ext:wgrep")
  4553. (defun ivy-wgrep-change-to-wgrep-mode ()
  4554. "Forward to `wgrep-change-to-wgrep-mode'."
  4555. (interactive)
  4556. (if (require 'wgrep nil 'noerror)
  4557. (wgrep-change-to-wgrep-mode)
  4558. (error "Package wgrep isn't installed")))
  4559. (defun ivy-occur-read-action ()
  4560. "Select one of the available actions as the current one."
  4561. (interactive)
  4562. (let ((ivy-last ivy-occur-last))
  4563. (ivy-read-action)))
  4564. (defun ivy-occur-dispatch ()
  4565. "Call one of the available actions on the current item."
  4566. (interactive)
  4567. (let* ((state-action (ivy-state-action ivy-occur-last))
  4568. (actions (if (symbolp state-action)
  4569. state-action
  4570. (copy-sequence state-action))))
  4571. (unwind-protect
  4572. (progn
  4573. (ivy-occur-read-action)
  4574. (ivy-occur-press))
  4575. (setf (ivy-state-action ivy-occur-last) actions))))
  4576. (defun ivy-occur-click (event)
  4577. "Execute action for the current candidate.
  4578. EVENT gives the mouse position."
  4579. (interactive "e")
  4580. (let ((window (posn-window (event-end event)))
  4581. (pos (posn-point (event-end event))))
  4582. (with-current-buffer (window-buffer window)
  4583. (goto-char pos)
  4584. (ivy-occur-press))))
  4585. (declare-function swiper--cleanup "swiper")
  4586. (declare-function swiper--add-overlays "swiper")
  4587. (defvar ivy-occur-timer nil)
  4588. (defun ivy--occur-press-update-window ()
  4589. (cond
  4590. ((memq (ivy-state-caller ivy-occur-last)
  4591. (append '(swiper swiper-isearch) ivy-highlight-grep-commands))
  4592. (let ((window (ivy-state-window ivy-occur-last))
  4593. (buffer (ivy-state-buffer ivy-occur-last)))
  4594. (when (buffer-live-p buffer)
  4595. (cond ((or (not (window-live-p window))
  4596. (equal window (selected-window)))
  4597. (save-selected-window
  4598. (setf (ivy-state-window ivy-occur-last)
  4599. (display-buffer buffer))))
  4600. ((not (equal (window-buffer window) buffer))
  4601. (with-selected-window window
  4602. (switch-to-buffer buffer)))))))
  4603. ((memq (ivy-state-caller ivy-occur-last)
  4604. '(counsel-describe-function
  4605. counsel-describe-variable
  4606. counsel-describe-symbol))
  4607. (setf (ivy-state-window ivy-occur-last)
  4608. (selected-window))
  4609. (selected-window))))
  4610. (defun ivy--occur-press-buffer ()
  4611. (let ((buffer (ivy-state-buffer ivy-last)))
  4612. (if (buffer-live-p buffer)
  4613. buffer
  4614. (current-buffer))))
  4615. (defun ivy-occur-press ()
  4616. "Execute action for the current candidate."
  4617. (interactive)
  4618. (ivy--occur-press-update-window)
  4619. (when (save-excursion
  4620. (beginning-of-line)
  4621. (looking-at "\\(?:.[/\\]\\| \\)\\(.*\\)$"))
  4622. (let* ((ivy-last ivy-occur-last)
  4623. (ivy-text (ivy-state-text ivy-last))
  4624. (str (buffer-substring
  4625. (match-beginning 1)
  4626. (match-end 1)))
  4627. (offset (or (get-text-property 0 'offset str) 0))
  4628. (coll (ivy-state-collection ivy-last))
  4629. (action (ivy--get-action ivy-last))
  4630. (ivy-exit 'done))
  4631. (with-ivy-window
  4632. (with-current-buffer (ivy--occur-press-buffer)
  4633. (save-restriction
  4634. (widen)
  4635. (funcall action
  4636. (if (and (consp coll)
  4637. (consp (car coll)))
  4638. (assoc str coll)
  4639. (substring str offset)))))
  4640. (if (memq (ivy-state-caller ivy-last)
  4641. (append '(swiper swiper-isearch) ivy-highlight-grep-commands))
  4642. (with-current-buffer (window-buffer (selected-window))
  4643. (swiper--cleanup)
  4644. (swiper--add-overlays
  4645. (ivy--regex ivy-text)
  4646. (line-beginning-position)
  4647. (line-end-position)
  4648. (selected-window))
  4649. (when (timerp ivy-occur-timer)
  4650. (cancel-timer ivy-occur-timer))
  4651. (setq ivy-occur-timer
  4652. (run-at-time 1.0 nil 'swiper--cleanup))))))))
  4653. (defun ivy-occur-press-and-switch ()
  4654. "Execute action for the current candidate and switch window."
  4655. (interactive)
  4656. (ivy-occur-press)
  4657. (select-window (ivy--get-window ivy-occur-last)))
  4658. (defun ivy--marked-p ()
  4659. (member (ivy-state-current ivy-last) ivy-marked-candidates))
  4660. (defun ivy--unmark (cand)
  4661. (setcar (member cand ivy--all-candidates)
  4662. (setcar (member cand ivy--old-cands)
  4663. (substring cand (length ivy-mark-prefix))))
  4664. (setq ivy-marked-candidates
  4665. (delete cand ivy-marked-candidates)))
  4666. (defun ivy--mark (cand)
  4667. (let ((marked-cand (concat ivy-mark-prefix cand)))
  4668. (setcar (member cand ivy--all-candidates)
  4669. (setcar (member cand ivy--old-cands) marked-cand))
  4670. (setq ivy-marked-candidates
  4671. (append ivy-marked-candidates (list marked-cand)))))
  4672. (defun ivy-mark ()
  4673. "Mark the selected candidate and move to the next one.
  4674. In `ivy-call', :action will be called in turn for all marked
  4675. candidates.
  4676. However, if :multi-action was supplied to `ivy-read', then it
  4677. will be called with `ivy-marked-candidates'. This way, it can
  4678. make decisions based on the whole marked list."
  4679. (interactive)
  4680. (unless (ivy--marked-p)
  4681. (ivy--mark (ivy-state-current ivy-last)))
  4682. (ivy-next-line))
  4683. (defun ivy-unmark ()
  4684. "Unmark the selected candidate and move to the next one."
  4685. (interactive)
  4686. (when (ivy--marked-p)
  4687. (ivy--unmark (ivy-state-current ivy-last)))
  4688. (ivy-next-line))
  4689. (defun ivy-unmark-backward ()
  4690. "Move to the previous candidate and unmark it."
  4691. (interactive)
  4692. (ivy-previous-line)
  4693. (ivy--exhibit)
  4694. (when (ivy--marked-p)
  4695. (ivy--unmark (ivy-state-current ivy-last))))
  4696. (defun ivy-toggle-marks ()
  4697. "Toggle mark for all narrowed candidates."
  4698. (interactive)
  4699. (dolist (cand ivy--old-cands)
  4700. (if (member cand ivy-marked-candidates)
  4701. (ivy--unmark cand)
  4702. (ivy--mark cand))))
  4703. (defconst ivy-help-file (let ((default-directory
  4704. (if load-file-name
  4705. (file-name-directory load-file-name)
  4706. default-directory)))
  4707. (if (file-exists-p "ivy-help.org")
  4708. (expand-file-name "ivy-help.org")
  4709. (if (file-exists-p "doc/ivy-help.org")
  4710. (expand-file-name "doc/ivy-help.org"))))
  4711. "The file for `ivy-help'.")
  4712. (defvar org-hide-emphasis-markers)
  4713. (defun ivy-help ()
  4714. "Help for `ivy'."
  4715. (interactive)
  4716. (let ((buf (get-buffer "*Ivy Help*"))
  4717. (inhibit-read-only t))
  4718. (unless buf
  4719. (setq buf (get-buffer-create "*Ivy Help*"))
  4720. (cl-letf (((symbol-function #'help-buffer) (lambda () buf)))
  4721. (describe-mode))
  4722. (with-current-buffer buf
  4723. (goto-char (point-min))
  4724. (insert "* describe-mode\n")
  4725. (goto-char (point-min))
  4726. (insert-file-contents ivy-help-file)
  4727. (org-mode)
  4728. (setq-local org-hide-emphasis-markers t)
  4729. (view-mode)
  4730. (goto-char (point-min))
  4731. (let ((inhibit-message t))
  4732. (org-cycle '(64)))))
  4733. (if (eq this-command 'ivy-help)
  4734. (switch-to-buffer buf)
  4735. (with-ivy-window
  4736. (pop-to-buffer buf)))
  4737. (view-mode)
  4738. (goto-char (point-min))))
  4739. (declare-function ffap-url-p "ffap")
  4740. (defvar ffap-url-fetcher)
  4741. (defun ivy-ffap-url-p (string)
  4742. "Forward to `ffap-url-p'."
  4743. (require 'ffap)
  4744. (ffap-url-p string))
  4745. (defun ivy-ffap-url-fetcher (url)
  4746. "Calls `ffap-url-fetcher'."
  4747. (require 'ffap)
  4748. (funcall ffap-url-fetcher url))
  4749. (ivy-configure 'read-file-name-internal
  4750. :sort-fn #'ivy-sort-file-function-default
  4751. :display-transformer-fn #'ivy-read-file-transformer
  4752. :alt-done-fn #'ivy--directory-done)
  4753. (ivy-configure 'internal-complete-buffer
  4754. :display-transformer-fn #'ivy-switch-buffer-transformer)
  4755. (ivy-configure 'Info-read-node-name-1
  4756. :alt-done-fn #'ivy--info-alt-done)
  4757. (provide 'ivy)
  4758. ;;; ivy.el ends here