Skip to content

Commit

Permalink
ivy.el (ivy--filter): allow filter to be interruptible with input.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiennq committed Jun 14, 2020
1 parent f8b1ab8 commit f0b0480
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -3318,7 +3318,7 @@ Should be run via minibuffer `post-command-hook'."
(setq ivy--old-re nil))))
(with-current-buffer (ivy-state-buffer ivy-last)
(ivy--format
(ivy--filter ivy-text ivy--all-candidates))))
(ivy--filter ivy-text ivy--all-candidates 'interruptible))))
(setq ivy--old-text ivy-text)))

(defun ivy-display-function-fallback (str)
Expand Down Expand Up @@ -3448,7 +3448,20 @@ In any Ivy completion session, the case folding starts with
;; Reset cache so that the candidate list updates.
(setq ivy--old-re nil))

(defun ivy--re-filter (re candidates &optional mkpred)
(defmacro ivy--while-no-input (&rest body)
"Wrap BODY in `while-no-input'.
Return the value if success running BODY."
(declare (debug t) (indent 0))
`(let ((ret (while-no-input ,@body)))
(unless (booleanp ret) ret)))

(defun ivy--re-filter (re candidates &optional mkpred interruptible)
"Like `ivy--re-filter-1' but interruptible by keyboard."
(if interruptible
(ivy--while-no-input (ivy--re-filter-1 re candidates mkpred))
(ivy--re-filter-1 re candidates mkpred)))

(defun ivy--re-filter-1 (re candidates &optional mkpred)
"Return all RE matching CANDIDATES.
RE is a list of cons cells, with a regexp car and a boolean cdr.
When the cdr is t, the car must match.
Expand All @@ -3468,9 +3481,10 @@ Otherwise, the car must not match."
pred))))
candidates)))

(defun ivy--filter (name candidates)
(defun ivy--filter (name candidates &optional interruptible)
"Return all items that match NAME in CANDIDATES.
CANDIDATES are assumed to be static."
CANDIDATES are assumed to be static.
When INTERRUPTIBLE, keyboard input can stop filtering."
(let ((re (funcall ivy--regex-function name)))
(if (and
ivy--old-re
Expand All @@ -3496,20 +3510,20 @@ CANDIDATES are assumed to be static."
re)
'(0 2))
ivy--old-cands
(ivy--re-filter re ivy--old-cands)))
(ivy--re-filter re ivy--old-cands nil interruptible)))
(t
(ivy--re-filter re candidates)))))
(ivy--re-filter re candidates nil interruptible)))))
(if (memq (cdr (assq (ivy-state-caller ivy-last)
ivy-index-functions-alist))
'(ivy-recompute-index-swiper
ivy-recompute-index-swiper-async
ivy-recompute-index-swiper-async-backward
ivy-recompute-index-swiper-backward))
(progn
(ivy--recompute-index re-str cands)
(ivy--recompute-index re-str cands interruptible)
(setq ivy--old-cands (ivy--sort name cands)))
(setq ivy--old-cands (ivy--sort name cands))
(ivy--recompute-index re-str ivy--old-cands))
(ivy--recompute-index re-str ivy--old-cands interruptible))
(setq ivy--old-re re)
ivy--old-cands))))

Expand Down Expand Up @@ -3653,7 +3667,14 @@ no sorting is done.")
(defvar ivy--recompute-index-inhibit nil
"When non-nil, `ivy--recompute-index' is a no-op.")

(defun ivy--recompute-index (re-str cands)

(defun ivy--recompute-index (re-str cands &optional interruptible)
"Like `ivy--recompute-index-1' but interruptible by keyboard."
(if interruptible
(ivy--while-no-input (ivy--recompute-index-1 re-str cands))
(ivy--recompute-index-1 re-str cands)))

(defun ivy--recompute-index-1 (re-str cands)
"Recompute index of selected candidate matching RE-STR.
CANDS are the current candidates."
(let ((caller (ivy-state-caller ivy-last))
Expand Down

0 comments on commit f0b0480

Please sign in to comment.