Skip to content

Commit

Permalink
ivy.el: Add a small refresh delay for dynamic collections.
Browse files Browse the repository at this point in the history
This is probably a good idea performance-wise since it limits the number
of collection updates when the user is typing. However, the main reason
for this change is to work around an Emacs freeze on Windows when the
dynamic collection is generated by an external command (see for example
this bug in helm-ag which also affects counsel-ag and similar commands:
emacsorphanage/helm-ag#188).

Related to abo-abo#1218 (but only applies for dynamic collections, where the
difference is really noticeable).
Helps with abo-abo#1198 and abo-abo#786.
  • Loading branch information
jeberger committed Oct 11, 2017
1 parent ff61b44 commit 36d7e83
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ customizations apply to the current completion session."
(delete item
(cdr (symbol-value hist))))))))
(ivy-state-current ivy-last)))
(remove-hook 'post-command-hook #'ivy--exhibit)
(remove-hook 'post-command-hook #'ivy--queue-exhibit)
(when (eq ivy-display-function 'ivy-display-function-overlay)
(ivy-overlay-cleanup))
(when (setq unwind (ivy-state-unwind ivy-last))
Expand Down Expand Up @@ -2339,7 +2339,7 @@ tries to ensure that it does not change depending on the number of candidates."
(if ivy-add-newline-after-prompt
1
0))))
(add-hook 'post-command-hook #'ivy--exhibit nil t)
(add-hook 'post-command-hook #'ivy--queue-exhibit nil t)
;; show completions with empty input
(ivy--exhibit))

Expand Down Expand Up @@ -2555,10 +2555,31 @@ Possible choices are 'ivy-magic-slash-non-match-cd-selected,
Otherwise, ~/ will move home."
:type 'boolean)

(defcustom ivy-dynamic-exhibit-delay-ms 500
"Delay in ms before dynamic collections are refreshed"
:type 'integer)

(defvar ivy--exhibit-timer nil)

(defun ivy--queue-exhibit ()
"Insert Ivy completions display, possibly after a timeout for
dynamic collections.
Should be run via minibuffer `post-command-hook'."
(if (and (> ivy-dynamic-exhibit-delay-ms 0)
(ivy-state-dynamic-collection ivy-last))
(progn
(when ivy--exhibit-timer (cancel-timer ivy--exhibit-timer))
(setq ivy--exhibit-timer
(run-with-timer
(/ ivy-dynamic-exhibit-delay-ms 1000.0)
nil
'ivy--exhibit)))
(ivy--exhibit)))

(defun ivy--exhibit ()
"Insert Ivy completions display.
Should be run via minibuffer `post-command-hook'."
(when (memq 'ivy--exhibit post-command-hook)
(when (memq 'ivy--queue-exhibit post-command-hook)
(let ((inhibit-field-text-motion nil))
(constrain-to-field nil (point-max)))
(setq ivy-text (ivy--input))
Expand Down

0 comments on commit 36d7e83

Please sign in to comment.