From 36d7e83cc1f9c3d18f9e8c5b4a0612bb8bbb5822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20M=2E=20Berger?= Date: Wed, 11 Oct 2017 10:15:35 +0200 Subject: [PATCH] ivy.el: Add a small refresh delay for dynamic collections. 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: https://github.com/syohex/emacs-helm-ag/issues/188). Related to #1218 (but only applies for dynamic collections, where the difference is really noticeable). Helps with #1198 and #786. --- ivy.el | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/ivy.el b/ivy.el index d3ea381d..cb164076 100644 --- a/ivy.el +++ b/ivy.el @@ -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)) @@ -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)) @@ -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))