-When SORT is t, refer to `ivy-sort-functions-alist' for sorting.
-
-ACTION is a lambda to call after a result was selected. It should
-take a single argument, usually a string.
-
-UNWIND is a lambda to call before exiting.
-
-RE-BUILDER is a lambda that transforms text into a regex.
-
-MATCHER can completely override matching.
-
-DYNAMIC-COLLECTION is a function to call to update the list of
-candidates with each input."
- (unless initial-input
- (setq initial-input (cdr (assoc this-command
- ivy-initial-inputs-alist))))
- (setq ivy-last
- (make-ivy-state
- :prompt prompt
- :collection collection
- :predicate predicate
- :require-match require-match
- :initial-input initial-input
- :history history
- :preselect preselect
- :keymap keymap
- :update-fn update-fn
- :sort sort
- :action action
- :window (selected-window)
- :unwind unwind
- :re-builder re-builder
- :matcher matcher
- :dynamic-collection dynamic-collection))
- (setq ivy--directory nil)
- (setq ivy--regex-function
- (or re-builder
- (and (functionp collection)
- (cdr (assoc collection ivy-re-builders-alist)))
- (cdr (assoc t ivy-re-builders-alist))
- 'ivy--regex))
- (setq ivy--subexps 0)
- (setq ivy--regexp-quote 'regexp-quote)
- (setq ivy--old-text "")
- (setq ivy-text "")
- (setq ivy-calling nil)
- (let (coll sort-fn)
- (cond ((eq collection 'Info-read-node-name-1)
- (if (equal Info-current-file "dir")
- (setq coll
- (mapcar (lambda (x) (format "(%s)" x))
- (cl-delete-duplicates
- (all-completions "(" collection predicate)
- :test #'equal)))
- (setq coll (all-completions "" collection predicate))))
- ((eq collection 'read-file-name-internal)
- (setq ivy--directory default-directory)
- (require 'dired)
- (setq coll
- (ivy--sorted-files default-directory))
- (when initial-input
- (unless (or require-match
- (equal initial-input default-directory)
- (equal initial-input ""))
- (setq coll (cons initial-input coll)))
- (setq initial-input nil)))
- ((eq collection 'internal-complete-buffer)
- (setq coll (ivy--buffer-list "" ivy-use-virtual-buffers)))
- ((or (functionp collection)
- (vectorp collection)
- (listp (car collection)))
- (setq coll (all-completions "" collection predicate)))
- ((hash-table-p collection)
- (error "Hash table as a collection unsupported"))
- (t
- (setq coll collection)))
- (when sort
- (if (and (functionp collection)
- (setq sort-fn (assoc collection ivy-sort-functions-alist)))
- (when (and (setq sort-fn (cdr sort-fn))
- (not (eq collection 'read-file-name-internal)))
- (setq coll (cl-sort coll sort-fn)))
- (unless (eq history 'org-refile-history)
- (if (and (setq sort-fn (cdr (assoc t ivy-sort-functions-alist)))
- (<= (length coll) ivy-sort-max-size))
- (setq coll (cl-sort (copy-sequence coll) sort-fn))))))
- (when preselect
- (unless (or (and require-match
- (not (eq collection 'internal-complete-buffer)))
- (let ((re (format "\\`%s" (regexp-quote preselect))))
- (cl-find-if (lambda (x) (string-match re x))
- coll)))
- (setq coll (cons preselect coll))))
- (setq ivy--index (or
- (and dynamic-collection
- ivy--index)
- (and preselect
- (ivy--preselect-index
- coll initial-input preselect))
- 0))
- (setq ivy--old-re nil)
- (setq ivy--old-cands nil)
- (setq ivy--all-candidates coll)
+When SORT is t, use `ivy-sort-functions-alist' for sorting.
+
+ACTION is a lambda function to call after selecting a result. It
+takes a single string argument.
+
+UNWIND is a lambda function to call before exiting.
+
+RE-BUILDER is a lambda function to call to transform text into a
+regex pattern.
+
+MATCHER is to override matching.
+
+DYNAMIC-COLLECTION is a boolean to specify if the list of
+candidates is updated after each input by calling COLLECTION.
+
+CALLER is a symbol to uniquely identify the caller to `ivy-read'.
+It is used, along with COLLECTION, to determine which
+customizations apply to the current completion session."
+ (let ((extra-actions (plist-get ivy--actions-list this-command)))
+ (when extra-actions
+ (setq action
+ (if (functionp action)
+ `(1
+ ("o" ,action "default")
+ ,@extra-actions)
+ (delete-dups (append action extra-actions))))))
+ (let ((recursive-ivy-last (and (active-minibuffer-window) ivy-last)))
+ (setq ivy-last
+ (make-ivy-state
+ :prompt prompt
+ :collection collection
+ :predicate predicate
+ :require-match require-match
+ :initial-input initial-input
+ :history history
+ :preselect preselect
+ :keymap keymap
+ :update-fn update-fn
+ :sort sort
+ :action action
+ :window (selected-window)
+ :buffer (current-buffer)
+ :unwind unwind
+ :re-builder re-builder
+ :matcher matcher
+ :dynamic-collection dynamic-collection
+ :caller caller))
+ (ivy--reset-state ivy-last)
+ (prog1
+ (unwind-protect
+ (minibuffer-with-setup-hook
+ #'ivy--minibuffer-setup
+ (let* ((hist (or history 'ivy-history))
+ (minibuffer-completion-table collection)
+ (minibuffer-completion-predicate predicate)
+ (resize-mini-windows (cond
+ ((display-graphic-p) nil)
+ ((null resize-mini-windows) 'grow-only)
+ (t resize-mini-windows)))
+ (res (read-from-minibuffer
+ prompt
+ (ivy-state-initial-input ivy-last)
+ (make-composed-keymap keymap ivy-minibuffer-map)
+ nil
+ hist)))
+ (when (eq ivy-exit 'done)
+ (let ((item (if ivy--directory
+ ivy--current
+ ivy-text)))
+ (unless (equal item "")
+ (set hist (cons (propertize item 'ivy-index ivy--index)
+ (delete item
+ (cdr (symbol-value hist)))))))
+ res)))
+ (remove-hook 'post-command-hook #'ivy--exhibit)
+ (when (setq unwind (ivy-state-unwind ivy-last))
+ (funcall unwind)))
+ (ivy-call)
+ (when recursive-ivy-last
+ (ivy--reset-state (setq ivy-last recursive-ivy-last))))))
+
+(defun ivy--reset-state (state)
+ "Reset the ivy to STATE.
+This is useful for recursive `ivy-read'."
+ (let ((prompt (or (ivy-state-prompt state) ""))
+ (collection (ivy-state-collection state))
+ (predicate (ivy-state-predicate state))
+ (history (ivy-state-history state))
+ (preselect (ivy-state-preselect state))
+ (sort (ivy-state-sort state))
+ (re-builder (ivy-state-re-builder state))
+ (dynamic-collection (ivy-state-dynamic-collection state))
+ (initial-input (ivy-state-initial-input state))
+ (require-match (ivy-state-require-match state))
+ (caller (ivy-state-caller state)))
+ (unless initial-input
+ (setq initial-input (cdr (assoc this-command
+ ivy-initial-inputs-alist))))
+ (setq ivy--directory nil)
+ (setq ivy-case-fold-search 'auto)
+ (setq ivy--regex-function
+ (or re-builder
+ (and (functionp collection)
+ (cdr (assoc collection ivy-re-builders-alist)))
+ (and caller
+ (cdr (assoc caller ivy-re-builders-alist)))
+ (cdr (assoc t ivy-re-builders-alist))
+ 'ivy--regex))
+ (setq ivy--subexps 0)
+ (setq ivy--regexp-quote 'regexp-quote)
+ (setq ivy--old-text "")
+ (setq ivy--full-length nil)
+ (setq ivy-text "")
+ (setq ivy-calling nil)
+ (let (coll sort-fn)
+ (cond ((eq collection 'Info-read-node-name-1)
+ (if (equal Info-current-file "dir")
+ (setq coll
+ (mapcar (lambda (x) (format "(%s)" x))
+ (cl-delete-duplicates
+ (all-completions "(" collection predicate)
+ :test #'equal)))
+ (setq coll (all-completions "" collection predicate))))
+ ((eq collection 'read-file-name-internal)
+ (setq ivy--directory default-directory)
+ (require 'dired)
+ (when preselect
+ (let ((preselect-directory (file-name-directory preselect)))
+ (unless (or (null preselect-directory)
+ (string= preselect-directory
+ default-directory))
+ (setq ivy--directory preselect-directory))
+ (setf
+ (ivy-state-preselect state)
+ (setq preselect (file-name-nondirectory preselect)))))
+ (setq coll (ivy--sorted-files ivy--directory))
+ (when initial-input
+ (unless (or require-match
+ (equal initial-input default-directory)
+ (equal initial-input ""))
+ (setq coll (cons initial-input coll)))
+ (setq initial-input nil)))
+ ((eq collection 'internal-complete-buffer)
+ (setq coll (ivy--buffer-list "" ivy-use-virtual-buffers)))
+ ((or (functionp collection)
+ (byte-code-function-p collection)
+ (vectorp collection)
+ (and (consp collection) (listp (car collection)))
+ (hash-table-p collection))
+ (setq coll (all-completions "" collection predicate)))
+ (t
+ (setq coll collection)))
+ (when sort
+ (if (and (functionp collection)
+ (setq sort-fn (assoc collection ivy-sort-functions-alist)))
+ (when (and (setq sort-fn (cdr sort-fn))
+ (not (eq collection 'read-file-name-internal)))
+ (setq coll (cl-sort coll sort-fn)))
+ (unless (eq history 'org-refile-history)
+ (if (and (setq sort-fn (cdr (assoc t ivy-sort-functions-alist)))
+ (<= (length coll) ivy-sort-max-size))
+ (setq coll (cl-sort (copy-sequence coll) sort-fn))))))
+ (when preselect
+ (unless (or (and require-match
+ (not (eq collection 'internal-complete-buffer)))
+ dynamic-collection
+ (let ((re (regexp-quote preselect)))
+ (cl-find-if (lambda (x) (string-match re x))
+ coll)))
+ (setq coll (cons preselect coll))))
+ (setq ivy--old-re nil)
+ (setq ivy--old-cands nil)
+ (when (integerp preselect)
+ (setq ivy--old-re "")
+ (setq ivy--index preselect))
+ (when initial-input
+ ;; Needed for anchor to work
+ (setq ivy--old-cands coll)
+ (setq ivy--old-cands (ivy--filter initial-input coll)))
+ (setq ivy--all-candidates coll)
+ (unless (integerp preselect)
+ (setq ivy--index (or
+ (and dynamic-collection
+ ivy--index)
+ (and preselect
+ (ivy--preselect-index
+ preselect
+ (if initial-input
+ ivy--old-cands
+ coll)))
+ 0))))