Skip to content

Commit

Permalink
Wait for user to stop typing to trigger search from khoj.el in Emacs
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
debanjum committed Oct 6, 2023
1 parent 5c4f0d4 commit f6f7a62
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/interface/emacs/khoj.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" ())
Expand Down Expand Up @@ -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
Expand All @@ -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))))


Expand Down

0 comments on commit f6f7a62

Please sign in to comment.