Use post-command-hook to only query on user input

- Hooking into after-change-functions results in system logs triggering query
This commit is contained in:
Debanjum Singh Solanky 2022-07-27 05:41:22 +04:00
parent 0d49398954
commit bfcb962cbe

View file

@ -153,20 +153,19 @@
(read-only-mode t))) (read-only-mode t)))
;; Incremental Search on Khoj ;; Incremental Search on Khoj
(defun khoj--incremental-query (beg end len) (defun khoj--incremental-query ()
(let* ((in-khoj-prompt (equal (minibuffer-prompt) khoj--query-prompt)) (let* ((search-type "org")
(search-type "org")
(buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type))) (buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type)))
(query (minibuffer-contents-no-properties)) (query (minibuffer-contents-no-properties))
(query-url (khoj--construct-api-query query search-type))) (query-url (khoj--construct-api-query query search-type)))
(khoj--query-api-and-render-results (khoj--query-api-and-render-results
query query
search-type search-type
query-url query-url
buffer-name))) buffer-name)))
(defun khoj--remove-incremental-query () (defun khoj--remove-incremental-query ()
(remove-hook 'after-change-functions #'khoj--incremental-query) (remove-hook 'post-command-hook #'khoj--incremental-query)
(remove-hook 'minibuffer-exit-hook #'khoj--remove-incremental-query)) (remove-hook 'minibuffer-exit-hook #'khoj--remove-incremental-query))
;;;###autoload ;;;###autoload
@ -179,8 +178,8 @@
(switch-to-buffer buffer-name) (switch-to-buffer buffer-name)
(minibuffer-with-setup-hook (minibuffer-with-setup-hook
(lambda () (lambda ()
(add-hook 'after-change-functions #'khoj--incremental-query) (add-hook 'post-command-hook #'khoj--incremental-query nil 'local)
(add-hook 'minibuffer-exit-hook #'khoj--remove-incremental-query)) (add-hook 'minibuffer-exit-hook #'khoj--remove-incremental-query nil 'local))
(read-string khoj--query-prompt)))) (read-string khoj--query-prompt))))
;;;###autoload ;;;###autoload