From f6f7a62d8076580e8794b18cee20ba86dd95a0e6 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 6 Oct 2023 12:39:19 -0700 Subject: [PATCH] Wait for user to stop typing to trigger search from khoj.el in Emacs - Improves user experience by aligning idle time with search latency to avoid display jitter (to render results) while user is typing - Makes the idle time configurable Closes #480 --- src/interface/emacs/khoj.el | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 2f8360f2c..e690b4809 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -87,6 +87,12 @@ :group 'khoj :type 'integer) +(defcustom khoj-search-on-idle-time 0.3 + "Idle time (in seconds) to wait before triggering search." + :group 'khoj + :type 'number) + + (defcustom khoj-default-content-type "org" "The default content type to perform search on." :group 'khoj @@ -115,6 +121,9 @@ (defvar khoj--content-type "org" "The type of content to perform search on.") +(defvar khoj--search-on-idle-timer nil + "Idle timer to trigger incremental search.") + (declare-function org-element-property "org-mode" (PROPERTY ELEMENT)) (declare-function org-element-type "org-mode" (ELEMENT)) (declare-function markdown-mode "markdown-mode" ()) @@ -920,6 +929,9 @@ RECEIVE-DATE is the message receive date." (message "khoj.el: Teardown Incremental Search") ;; unset khoj minibuffer window (setq khoj--minibuffer-window nil) + (when (and khoj--search-on-idle-timer + (timerp khoj--search-on-idle-timer)) + (cancel-timer khoj--search-on-idle-timer)) ;; delete open connections to khoj server (khoj--delete-open-network-connections-to-server) ;; remove hooks for khoj incremental query and self @@ -942,8 +954,10 @@ RECEIVE-DATE is the message receive date." ;; set current (mini-)buffer entered as khoj minibuffer ;; used to query khoj API only when user in khoj minibuffer (setq khoj--minibuffer-window (current-buffer)) - (add-hook 'post-command-hook #'khoj--incremental-search) ; do khoj incremental search after every user action - (add-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search)) ; teardown khoj incremental search on minibuffer exit + ; do khoj incremental search after idle time + (setq khoj--search-on-idle-timer (run-with-idle-timer khoj-search-on-idle-time t #'khoj--incremental-search)) + ; teardown khoj incremental search on minibuffer exit + (add-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search)) (read-string khoj--query-prompt))))