mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-27 17:35:07 +01:00
Use Transient Menu to Improve Khoj.el Interface
-5f446b1
Convert `khoj' entry point method to transient.el menu for richer configuration -9d64a00
Allow updating khoj content index from within `khoj.el'
This commit is contained in:
commit
b58dd82141
1 changed files with 115 additions and 52 deletions
|
@ -6,7 +6,7 @@
|
|||
;; Description: Natural, Incremental Search for your Second Brain
|
||||
;; Keywords: search, org-mode, outlines, markdown, beancount, ledger, image
|
||||
;; Version: 0.2.2
|
||||
;; Package-Requires: ((emacs "27.1"))
|
||||
;; Package-Requires: ((emacs "27.1") (transient "0.3.0"))
|
||||
;; URL: https://github.com/debanjum/khoj/tree/master/src/interface/emacs
|
||||
|
||||
;; This file is NOT part of GNU Emacs.
|
||||
|
@ -48,6 +48,12 @@
|
|||
|
||||
(require 'url)
|
||||
(require 'json)
|
||||
(require 'transient)
|
||||
|
||||
|
||||
;; -------------------------
|
||||
;; Khoj Static Configuration
|
||||
;; -------------------------
|
||||
|
||||
(defcustom khoj-server-url "http://localhost:8000"
|
||||
"Location of Khoj API server."
|
||||
|
@ -69,24 +75,30 @@
|
|||
:group 'khoj
|
||||
:type 'integer)
|
||||
|
||||
(defcustom khoj-default-search-type "org"
|
||||
(defcustom khoj-default-content-type "org"
|
||||
"The default content type to perform search on."
|
||||
:group 'khoj
|
||||
:type '(choice (const "org")
|
||||
(const "markdown")
|
||||
(const "ledger")
|
||||
(const "image")
|
||||
(const "music")))
|
||||
|
||||
|
||||
;; --------------------------
|
||||
;; Khoj Dynamic Configuration
|
||||
;; --------------------------
|
||||
|
||||
(defvar khoj--minibuffer-window nil
|
||||
"Minibuffer window being used by user to enter query.")
|
||||
"Minibuffer window used to enter query.")
|
||||
|
||||
(defconst khoj--query-prompt "🦅Khoj: "
|
||||
"Query prompt shown to user in the minibuffer.")
|
||||
"Query prompt shown in the minibuffer.")
|
||||
|
||||
(defconst khoj--buffer-name "*🦅Khoj*"
|
||||
"Name of buffer to show results from Khoj.")
|
||||
|
||||
(defvar khoj--search-type "org"
|
||||
(defvar khoj--content-type "org"
|
||||
"The type of content to perform search on.")
|
||||
|
||||
(declare-function beancount-mode "beancount" ())
|
||||
|
@ -96,29 +108,29 @@
|
|||
NO-PAGING FILTER))
|
||||
|
||||
(defun khoj--keybindings-info-message ()
|
||||
"Show available khoj keybindings in-context, when user invokes Khoj."
|
||||
"Show available khoj keybindings in-context, when khoj invoked."
|
||||
(let ((enabled-content-types (khoj--get-enabled-content-types)))
|
||||
(concat
|
||||
"
|
||||
Set Search Type
|
||||
Set Content Type
|
||||
-------------------------\n"
|
||||
(when (member 'markdown enabled-content-types)
|
||||
"C-x m | markdown\n")
|
||||
"C-x m | markdown\n")
|
||||
(when (member 'org enabled-content-types)
|
||||
"C-x o | org-mode\n")
|
||||
(when (member 'ledger enabled-content-types)
|
||||
"C-x l | ledger\n")
|
||||
(when (member 'image enabled-content-types)
|
||||
"C-x i | images\n")
|
||||
"C-x i | image\n")
|
||||
(when (member 'music enabled-content-types)
|
||||
"C-x M | music\n"))))
|
||||
|
||||
(defvar khoj--rerank nil "Track when re-rank of results triggered.")
|
||||
(defun khoj--search-markdown () "Set search-type to `markdown'." (interactive) (setq khoj--search-type "markdown"))
|
||||
(defun khoj--search-org () "Set search-type to `org-mode'." (interactive) (setq khoj--search-type "org"))
|
||||
(defun khoj--search-ledger () "Set search-type to `ledger'." (interactive) (setq khoj--search-type "ledger"))
|
||||
(defun khoj--search-images () "Set search-type to image." (interactive) (setq khoj--search-type "image"))
|
||||
(defun khoj--search-music () "Set search-type to music." (interactive) (setq khoj--search-type "music"))
|
||||
(defun khoj--search-markdown () "Set content-type to `markdown'." (interactive) (setq khoj--content-type "markdown"))
|
||||
(defun khoj--search-org () "Set content-type to `org-mode'." (interactive) (setq khoj--content-type "org"))
|
||||
(defun khoj--search-ledger () "Set content-type to `ledger'." (interactive) (setq khoj--content-type "ledger"))
|
||||
(defun khoj--search-images () "Set content-type to image." (interactive) (setq khoj--content-type "image"))
|
||||
(defun khoj--search-music () "Set content-type to music." (interactive) (setq khoj--content-type "music"))
|
||||
(defun khoj--improve-rank () "Use cross-encoder to rerank search results." (interactive) (khoj--incremental-search t))
|
||||
(defun khoj--make-search-keymap (&optional existing-keymap)
|
||||
"Setup keymap to configure Khoj search. Build of EXISTING-KEYMAP when passed."
|
||||
|
@ -148,6 +160,11 @@ Use `which-key` if available, else display simple message in echo area"
|
|||
nil t t))
|
||||
(message "%s" (khoj--keybindings-info-message))))
|
||||
|
||||
|
||||
;; -----------------------------------------------
|
||||
;; Extract and Render Entries of each Content Type
|
||||
;; -----------------------------------------------
|
||||
|
||||
(defun khoj--extract-entries-as-markdown (json-response query)
|
||||
"Convert JSON-RESPONSE, QUERY from API to markdown entries."
|
||||
;; remove leading (, ) or SPC from extracted entries string
|
||||
|
@ -219,8 +236,8 @@ Use `which-key` if available, else display simple message in echo area"
|
|||
(format "%s\n\n" (cdr (assoc 'entry args))))
|
||||
json-response)))))
|
||||
|
||||
(defun khoj--buffer-name-to-search-type (buffer-name)
|
||||
"Infer search type based on BUFFER-NAME."
|
||||
(defun khoj--buffer-name-to-content-type (buffer-name)
|
||||
"Infer content type based on BUFFER-NAME."
|
||||
(let ((enabled-content-types (khoj--get-enabled-content-types))
|
||||
(file-extension (file-name-extension buffer-name)))
|
||||
(cond
|
||||
|
@ -228,7 +245,12 @@ Use `which-key` if available, else display simple message in echo area"
|
|||
((and (member 'ledger enabled-content-types) (or (equal file-extension "bean") (equal file-extension "beancount"))) "ledger")
|
||||
((and (member 'org enabled-content-types) (equal file-extension "org")) "org")
|
||||
((and (member 'markdown enabled-content-types) (or (equal file-extension "markdown") (equal file-extension "md"))) "markdown")
|
||||
(t khoj-default-search-type))))
|
||||
(t khoj-default-content-type))))
|
||||
|
||||
|
||||
;; --------------
|
||||
;; Query Khoj API
|
||||
;; --------------
|
||||
|
||||
(defun khoj--get-enabled-content-types ()
|
||||
"Get content types enabled for search from API."
|
||||
|
@ -246,14 +268,14 @@ Use `which-key` if available, else display simple message in echo area"
|
|||
(lambda (a) (not (eq (cdr a) :null)))
|
||||
content-type))))))
|
||||
|
||||
(defun khoj--construct-api-query (query search-type &optional rerank)
|
||||
"Construct API Query from QUERY, SEARCH-TYPE and (optional) RERANK params."
|
||||
(defun khoj--construct-api-query (query content-type &optional rerank)
|
||||
"Construct API Query from QUERY, CONTENT-TYPE and (optional) RERANK params."
|
||||
(let ((rerank (or rerank "false"))
|
||||
(encoded-query (url-hexify-string query)))
|
||||
(format "%s/api/search?q=%s&t=%s&r=%s&n=%s" khoj-server-url encoded-query search-type rerank khoj-results-count)))
|
||||
(format "%s/api/search?q=%s&t=%s&r=%s&n=%s" khoj-server-url encoded-query content-type rerank khoj-results-count)))
|
||||
|
||||
(defun khoj--query-api-and-render-results (query search-type query-url buffer-name)
|
||||
"Query Khoj API using QUERY, SEARCH-TYPE, QUERY-URL.
|
||||
(defun khoj--query-api-and-render-results (query content-type query-url buffer-name)
|
||||
"Query Khoj API using QUERY, CONTENT-TYPE, QUERY-URL.
|
||||
Render results in BUFFER-NAME."
|
||||
;; get json response from api
|
||||
(with-current-buffer buffer-name
|
||||
|
@ -267,28 +289,32 @@ Render results in BUFFER-NAME."
|
|||
(json-response (json-parse-buffer :object-type 'alist)))
|
||||
(erase-buffer)
|
||||
(insert
|
||||
(cond ((or (equal search-type "org") (equal search-type "music")) (khoj--extract-entries-as-org json-response query))
|
||||
((equal search-type "markdown") (khoj--extract-entries-as-markdown json-response query))
|
||||
((equal search-type "ledger") (khoj--extract-entries-as-ledger json-response query))
|
||||
((equal search-type "image") (khoj--extract-entries-as-images json-response query))
|
||||
(cond ((or (equal content-type "org") (equal content-type "music")) (khoj--extract-entries-as-org json-response query))
|
||||
((equal content-type "markdown") (khoj--extract-entries-as-markdown json-response query))
|
||||
((equal content-type "ledger") (khoj--extract-entries-as-ledger json-response query))
|
||||
((equal content-type "image") (khoj--extract-entries-as-images json-response query))
|
||||
(t (format "%s" json-response))))
|
||||
(cond ((equal search-type "org") (org-mode))
|
||||
((equal search-type "markdown") (markdown-mode))
|
||||
((equal search-type "ledger") (beancount-mode))
|
||||
((equal search-type "music") (progn (org-mode)
|
||||
(cond ((equal content-type "org") (org-mode))
|
||||
((equal content-type "markdown") (markdown-mode))
|
||||
((equal content-type "ledger") (beancount-mode))
|
||||
((equal content-type "music") (progn (org-mode)
|
||||
(org-music-mode)))
|
||||
((equal search-type "image") (progn (shr-render-region (point-min) (point-max))
|
||||
((equal content-type "image") (progn (shr-render-region (point-min) (point-max))
|
||||
(goto-char (point-min))))
|
||||
(t (fundamental-mode))))
|
||||
(read-only-mode t)))
|
||||
|
||||
|
||||
;; ------------------
|
||||
;; Incremental Search
|
||||
;; ------------------
|
||||
|
||||
(defun khoj--incremental-search (&optional rerank)
|
||||
"Perform Incremental Search on Khoj. Allow optional RERANK of results."
|
||||
(let* ((rerank-str (cond (rerank "true") (t "false")))
|
||||
(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-url (khoj--construct-api-query query khoj--content-type rerank-str)))
|
||||
;; Query khoj API only when user in khoj minibuffer and non-empty query
|
||||
;; Prevents querying if
|
||||
;; 1. user hasn't started typing query
|
||||
|
@ -309,7 +335,7 @@ Render results in BUFFER-NAME."
|
|||
(message "Khoj: Rerank Results"))
|
||||
(khoj--query-api-and-render-results
|
||||
query
|
||||
khoj--search-type
|
||||
khoj--content-type
|
||||
query-url
|
||||
khoj-buffer-name))))))
|
||||
|
||||
|
@ -332,14 +358,10 @@ Render results in BUFFER-NAME."
|
|||
(remove-hook 'post-command-hook #'khoj--incremental-search)
|
||||
(remove-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search))
|
||||
|
||||
|
||||
;;;###autoload
|
||||
(defun khoj ()
|
||||
(defun khoj-incremental ()
|
||||
"Natural, Incremental Search for your personal notes, transactions and music."
|
||||
(interactive)
|
||||
(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))))
|
||||
;; switch to khoj results buffer
|
||||
(switch-to-buffer khoj-buffer-name)
|
||||
;; open and setup minibuffer for incremental search
|
||||
|
@ -356,21 +378,62 @@ Render results in BUFFER-NAME."
|
|||
(add-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search)) ; teardown khoj incremental search on minibuffer exit
|
||||
(read-string khoj--query-prompt))))
|
||||
|
||||
|
||||
;; ---------
|
||||
;; Khoj Menu
|
||||
;; ---------
|
||||
|
||||
(transient-define-argument khoj--content-type-switch ()
|
||||
:class 'transient-switches
|
||||
:argument-format "--content-type=%s"
|
||||
:argument-regexp ".+"
|
||||
;; set content type to last used or based on current buffer or to default
|
||||
:init-value (lambda (obj) (oset obj value (format "--content-type=%s" (or khoj--content-type (khoj--buffer-name-to-content-type (buffer-name))))))
|
||||
;; dynamically set choices to content types enabled on khoj backend
|
||||
:choices (mapcar #'symbol-name (khoj--get-enabled-content-types)))
|
||||
|
||||
(transient-define-suffix khoj--search-command (&optional args)
|
||||
(interactive (list (transient-args transient-current-command)))
|
||||
(progn
|
||||
;; set content type to last used or based on current buffer or to default
|
||||
(setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
|
||||
;; set results count to last used or to default
|
||||
(setq khoj-results-count (or (transient-arg-value "--results-count=" args) khoj-results-count))
|
||||
;; trigger incremental search
|
||||
(call-interactively #'khoj-incremental)))
|
||||
|
||||
(transient-define-suffix khoj--update-command (&optional args)
|
||||
"Call khoj API to update index of specified content type."
|
||||
(interactive (list (transient-args transient-current-command)))
|
||||
(let* ((force-update (if (member "--force-update" args) "true" "false"))
|
||||
(content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
|
||||
(update-url (format "%s/api/update?t=%s&force=%s" khoj-server-url content-type force-update))
|
||||
(url-request-method "GET"))
|
||||
(url-retrieve update-url (lambda (_) (message "Khoj %s index %supdated!" content-type (if (member "--force-update" args) "force " ""))))))
|
||||
|
||||
(transient-define-prefix khoj-menu ()
|
||||
"Create Khoj Menu to Configure and Execute Commands."
|
||||
[["Configure General"
|
||||
("t" "Content Type" khoj--content-type-switch)]
|
||||
["Configure Search"
|
||||
("n" "Results Count" "--results-count=" :init-value (lambda (obj) (oset obj value (format "%s" khoj-results-count))))]
|
||||
["Configure Update"
|
||||
("-f" "Force Update" "--force-update")]]
|
||||
[["Act"
|
||||
("s" "Search" khoj--search-command)
|
||||
("u" "Update" khoj--update-command)
|
||||
("q" "Quit" transient-quit-one)]])
|
||||
|
||||
|
||||
;; ----------
|
||||
;; Entrypoint
|
||||
;; ----------
|
||||
|
||||
;;;###autoload
|
||||
(defun khoj-simple (query)
|
||||
"Natural Search for QUERY on your personal notes, transactions, music and images."
|
||||
(interactive "s🦅Khoj: ")
|
||||
(let* ((rerank "true")
|
||||
(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 "*%s (q:%s t:%s)*" khoj--buffer-name query search-type))))
|
||||
(khoj--query-api-and-render-results
|
||||
query
|
||||
search-type
|
||||
query-url
|
||||
buffer-name)
|
||||
(switch-to-buffer buffer-name)))
|
||||
(defun khoj ()
|
||||
"Natural, Incremental Search for your personal notes, transactions and images."
|
||||
(interactive)
|
||||
(khoj-menu))
|
||||
|
||||
(provide 'khoj)
|
||||
|
||||
|
|
Loading…
Reference in a new issue