From 0a5c6d067a8f23e681ee40434da61204035c0418 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 17:23:14 +0300 Subject: [PATCH 1/8] Do not prompt user to set search type before querying Khoj via Emacs - What - Default to last used search type, when no search type specified - Allow user to change search type before they enter query (and after they've called khoj), if they want - Why - Reduce time from intent to results by using reasonable defaults - Make interactions smoother, more intuitive --- src/interface/emacs/khoj.el | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 9f55a82d..6603681b 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -68,6 +68,15 @@ (defvar khoj--search-type "org" "The type of content to perform search on.") +(defvar khoj--search-keymap + (let ((kmap (make-sparse-keymap))) + (define-key kmap (kbd "C-x m") '(lambda () (interactive) (setq khoj--search-type "markdown"))) + (define-key kmap (kbd "C-x o") '(lambda () (interactive) (setq khoj--search-type "org"))) + (define-key kmap (kbd "C-x l") '(lambda () (interactive) (setq khoj--search-type "ledger"))) + (define-key kmap (kbd "C-x i") '(lambda () (interactive) (setq khoj--search-type "image"))) + kmap) + "Keymap to configure Khoj search") + (defun khoj--extract-entries-as-markdown (json-response query) "Convert json response from API to markdown entries" ;; remove leading (, ) or SPC from extracted entries string @@ -183,10 +192,9 @@ ;; Incremental Search on Khoj (defun khoj--incremental-search (&optional rerank) (let* ((rerank-str (cond (rerank "true") (t "false"))) - (search-type khoj--search-type) - (buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type))) + (buffer-name (get-buffer-create "*Khoj*")) (query (minibuffer-contents-no-properties)) - (query-url (khoj--construct-api-query query search-type rerank-str))) + (query-url (khoj--construct-api-query query khoj--search-type rerank-str))) ;; Query khoj API only when user in khoj minibuffer. ;; Prevents querying during recursive edits or with contents of other buffers user may jump to (when (and (active-minibuffer-window) (equal (current-buffer) khoj--minibuffer-window)) @@ -195,7 +203,7 @@ (message "[Khoj]: Rerank Results")) (khoj--query-api-and-render-results query - search-type + khoj--search-type query-url buffer-name))))) @@ -229,14 +237,15 @@ (defun khoj () "Natural, Incremental Search for your personal notes, transactions and music using Khoj" (interactive) - (let* ((default-type (khoj--buffer-name-to-search-type (buffer-name))) - (search-type (completing-read "Type: " '("org" "markdown" "ledger" "music") nil t default-type)) - (buffer-name (get-buffer-create (format "*Khoj (t:%s)*" search-type)))) - (setq khoj--search-type search-type) + (let* ((khoj-buffer-name (get-buffer-create "*Khoj*"))) + ;; set khoj search type to last used or based on current buffer + (setq khoj--search-type (or khoj--search-type (khoj--buffer-name-to-search-type (buffer-name)))) + ;; setup temporary keymap for khoj + (set-transient-map khoj--search-keymap t) ;; setup rerank to improve results once user idle for KHOJ--RERANK-AFTER-IDLE-TIME seconds (setq khoj--rerank-timer (run-with-idle-timer khoj--rerank-after-idle-time t 'khoj--incremental-search t)) ;; switch to khoj results buffer - (switch-to-buffer buffer-name) + (switch-to-buffer khoj-buffer-name) ;; open and setup minibuffer for incremental search (minibuffer-with-setup-hook (lambda () From cc9a395e0ac00934468b1995cb87ac5491e4ff27 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 17:31:46 +0300 Subject: [PATCH 2/8] Keep name of buffer for Khoj results in a variable --- src/interface/emacs/khoj.el | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 6603681b..33777556 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -65,6 +65,9 @@ (defconst khoj--query-prompt "🦅Khoj: " "Query prompt shown to user in the minibuffer.") +(defconst khoj--buffer-name "*🦅Khoj*" + "Name of buffer to show results from Khoj.") + (defvar khoj--search-type "org" "The type of content to perform search on.") @@ -192,7 +195,7 @@ ;; Incremental Search on Khoj (defun khoj--incremental-search (&optional rerank) (let* ((rerank-str (cond (rerank "true") (t "false"))) - (buffer-name (get-buffer-create "*Khoj*")) + (khoj-buffer-name (get-buffer-create khoj--buffer-name)) (query (minibuffer-contents-no-properties)) (query-url (khoj--construct-api-query query khoj--search-type rerank-str))) ;; Query khoj API only when user in khoj minibuffer. @@ -205,7 +208,7 @@ query khoj--search-type query-url - buffer-name))))) + khoj-buffer-name))))) (defun delete-open-network-connections-to-khoj () "Delete all network connections to khoj server" @@ -237,7 +240,7 @@ (defun khoj () "Natural, Incremental Search for your personal notes, transactions and music using Khoj" (interactive) - (let* ((khoj-buffer-name (get-buffer-create "*Khoj*"))) + (let* ((khoj-buffer-name (get-buffer-create khoj--buffer-name))) ;; set khoj search type to last used or based on current buffer (setq khoj--search-type (or khoj--search-type (khoj--buffer-name-to-search-type (buffer-name)))) ;; setup temporary keymap for khoj @@ -266,7 +269,7 @@ (default-type (khoj--buffer-name-to-search-type (buffer-name))) (search-type (completing-read "Type: " '("org" "markdown" "ledger" "music" "image") nil t default-type)) (query-url (khoj--construct-api-query query search-type rerank)) - (buffer-name (get-buffer-create (format "*Khoj (q:%s t:%s)*" query search-type)))) + (buffer-name (get-buffer-create (format "*%s (q:%s t:%s)*" khoj--buffer-name query search-type)))) (khoj--query-api-and-render-results query search-type From 19c4701f3f48c13a83f21ed3822ae8a6ad2db5d5 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 17:33:45 +0300 Subject: [PATCH 3/8] Default to ledger search from files with .beancount extensions --- src/interface/emacs/khoj.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 33777556..2c8ef502 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -153,7 +153,7 @@ (let ((file-extension (file-name-extension buffer-name))) (cond ((equal buffer-name "Music.org") "music") - ((equal file-extension "bean") "ledger") + ((or (equal file-extension "bean") (equal file-extension "beancount")) "ledger") ((equal file-extension "org") "org") ((or (equal file-extension "markdown") (equal file-extension "md")) "markdown") (t "org")))) From 48c33b93cc5cac6960d7af8356121e45897f7b60 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 18:45:06 +0300 Subject: [PATCH 4/8] Generalize khoj keymap to func that can update existing keybdings --- src/interface/emacs/khoj.el | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 2c8ef502..035cd703 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -71,14 +71,14 @@ (defvar khoj--search-type "org" "The type of content to perform search on.") -(defvar khoj--search-keymap - (let ((kmap (make-sparse-keymap))) +(defun khoj--make-search-keymap (&optional existing-keymap) + "Setup keymap to configure Khoj search" + (let ((kmap (or existing-keymap (make-sparse-keymap)))) (define-key kmap (kbd "C-x m") '(lambda () (interactive) (setq khoj--search-type "markdown"))) (define-key kmap (kbd "C-x o") '(lambda () (interactive) (setq khoj--search-type "org"))) (define-key kmap (kbd "C-x l") '(lambda () (interactive) (setq khoj--search-type "ledger"))) (define-key kmap (kbd "C-x i") '(lambda () (interactive) (setq khoj--search-type "image"))) - kmap) - "Keymap to configure Khoj search") + kmap)) (defun khoj--extract-entries-as-markdown (json-response query) "Convert json response from API to markdown entries" @@ -244,7 +244,7 @@ ;; set khoj search type to last used or based on current buffer (setq khoj--search-type (or khoj--search-type (khoj--buffer-name-to-search-type (buffer-name)))) ;; setup temporary keymap for khoj - (set-transient-map khoj--search-keymap t) + (set-transient-map (khoj--search-keymap) t) ;; setup rerank to improve results once user idle for KHOJ--RERANK-AFTER-IDLE-TIME seconds (setq khoj--rerank-timer (run-with-idle-timer khoj--rerank-after-idle-time t 'khoj--incremental-search t)) ;; switch to khoj results buffer From 48e4a983c5c41824f9f7a704cb6876a3b408a4eb Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 18:47:20 +0300 Subject: [PATCH 5/8] Allow switching search type in the middle of querying Khoj on Emacs - More generally, this allows configuring the khoj search anytime while in khoj minibuffer window - Earlier could only configure search type at the start of the search --- src/interface/emacs/khoj.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 035cd703..2c17050f 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -243,8 +243,6 @@ (let* ((khoj-buffer-name (get-buffer-create khoj--buffer-name))) ;; set khoj search type to last used or based on current buffer (setq khoj--search-type (or khoj--search-type (khoj--buffer-name-to-search-type (buffer-name)))) - ;; setup temporary keymap for khoj - (set-transient-map (khoj--search-keymap) t) ;; setup rerank to improve results once user idle for KHOJ--RERANK-AFTER-IDLE-TIME seconds (setq khoj--rerank-timer (run-with-idle-timer khoj--rerank-after-idle-time t 'khoj--incremental-search t)) ;; switch to khoj results buffer @@ -252,6 +250,8 @@ ;; open and setup minibuffer for incremental search (minibuffer-with-setup-hook (lambda () + ;; Add khoj keybindings for configuring search to minibuffer keybindings + (khoj--make-search-keymap minibuffer-local-map) ;; 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)) From 609cd6e8bbb8729e8129c3b87e9fc2ad974da21c Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 19:32:58 +0300 Subject: [PATCH 6/8] Show keybindings to set khoj search type in echo area to assist user --- src/interface/emacs/khoj.el | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 2c17050f..b2f2eeda 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -80,6 +80,16 @@ (define-key kmap (kbd "C-x i") '(lambda () (interactive) (setq khoj--search-type "image"))) kmap)) +(defvar khoj--keybindings-help-message + " + Set Search Type +------------------------- +C-x m | markdown +C-x o | org-mode +C-x l | ledger/beancount +C-x i | images +") + (defun khoj--extract-entries-as-markdown (json-response query) "Convert json response from API to markdown entries" ;; remove leading (, ) or SPC from extracted entries string @@ -252,6 +262,7 @@ (lambda () ;; Add khoj keybindings for configuring search to minibuffer keybindings (khoj--make-search-keymap minibuffer-local-map) + (message "%s" khoj--keybindings-help-message) ;; 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)) From 6a8b2a6936588c90a8ee53e99455997c6ad5b5e2 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 19:34:12 +0300 Subject: [PATCH 7/8] Do not run incremental search when query is empty --- src/interface/emacs/khoj.el | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index b2f2eeda..cb2fa03a 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -208,12 +208,15 @@ C-x i | images (khoj-buffer-name (get-buffer-create khoj--buffer-name)) (query (minibuffer-contents-no-properties)) (query-url (khoj--construct-api-query query khoj--search-type rerank-str))) - ;; Query khoj API only when user in khoj minibuffer. - ;; Prevents querying during recursive edits or with contents of other buffers user may jump to - (when (and (active-minibuffer-window) (equal (current-buffer) khoj--minibuffer-window)) + ;; Query khoj API only when user in khoj minibuffer and non-empty query + ;; Prevents querying if + ;; 1. user hasn't started typing query + ;; 2. during recursive edits + ;; 3. with contents of other buffers user may jump to + (when (and (not (equal query "")) (active-minibuffer-window) (equal (current-buffer) khoj--minibuffer-window)) (progn (when rerank - (message "[Khoj]: Rerank Results")) + (message "Khoj: Rerank Results")) (khoj--query-api-and-render-results query khoj--search-type From 9fa3345000899e06376efa386c2b91073458a5ad Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 5 Aug 2022 20:15:51 +0300 Subject: [PATCH 8/8] Show available Khoj keybindings to customize search using which-key Fallback to showing simple khoj keybindings info message in echo area when which-key not available --- src/interface/emacs/khoj.el | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index cb2fa03a..e0e30460 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -71,16 +71,7 @@ (defvar khoj--search-type "org" "The type of content to perform search on.") -(defun khoj--make-search-keymap (&optional existing-keymap) - "Setup keymap to configure Khoj search" - (let ((kmap (or existing-keymap (make-sparse-keymap)))) - (define-key kmap (kbd "C-x m") '(lambda () (interactive) (setq khoj--search-type "markdown"))) - (define-key kmap (kbd "C-x o") '(lambda () (interactive) (setq khoj--search-type "org"))) - (define-key kmap (kbd "C-x l") '(lambda () (interactive) (setq khoj--search-type "ledger"))) - (define-key kmap (kbd "C-x i") '(lambda () (interactive) (setq khoj--search-type "image"))) - kmap)) - -(defvar khoj--keybindings-help-message +(defvar khoj--keybindings-info-message " Set Search Type ------------------------- @@ -89,6 +80,28 @@ C-x o | org-mode C-x l | ledger/beancount C-x i | images ") +(defun khoj--search-markdown (interactive) (setq khoj--search-type "markdown")) +(defun khoj--search-org (interactive) (setq khoj--search-type "org")) +(defun khoj--search-ledger (interactive) (setq khoj--search-type "ledger")) +(defun khoj--search-images (interactive) (setq khoj--search-type "image")) +(defun khoj--make-search-keymap (&optional existing-keymap) + "Setup keymap to configure Khoj search" + (let ((kmap (or existing-keymap (make-sparse-keymap)))) + (define-key kmap (kbd "C-x m") #'khoj--search-markdown) + (define-key kmap (kbd "C-x o") #'khoj--search-org) + (define-key kmap (kbd "C-x l") #'khoj--search-ledger) + (define-key kmap (kbd "C-x i") #'khoj--search-images) + kmap)) +(defun khoj--display-keybinding-info () + "Display information on keybindings to customize khoj search. +Use `which-key` if available, else display simple message in echo area" + (if (fboundp 'which-key--create-buffer-and-show) + (which-key--create-buffer-and-show + (kbd "C-x") + (symbolp (khoj--make-search-keymap)) + '(lambda (binding) (string-prefix-p "khoj--" (cdr binding))) + "Khoj Bindings") + (message "%s" khoj--keybindings-info-message))) (defun khoj--extract-entries-as-markdown (json-response query) "Convert json response from API to markdown entries" @@ -265,7 +278,8 @@ C-x i | images (lambda () ;; Add khoj keybindings for configuring search to minibuffer keybindings (khoj--make-search-keymap minibuffer-local-map) - (message "%s" khoj--keybindings-help-message) + ;; Display information on keybindings to customize khoj search + (khoj--display-keybinding-info) ;; 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))