2022-07-27 14:18:17 +00:00
|
|
|
;;; khoj.el --- Natural, Incremental Search via Emacs
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
|
|
;; Copyright (C) 2021-2022 Debanjum Singh Solanky
|
|
|
|
|
|
|
|
;; Author: Debanjum Singh Solanky <debanjum@gmail.com>
|
2022-07-27 14:18:17 +00:00
|
|
|
;; Version: 2.0
|
2022-07-21 16:22:24 +00:00
|
|
|
;; Keywords: search, org-mode, outlines, markdown, image
|
2022-07-19 14:26:16 +00:00
|
|
|
;; URL: http://github.com/debanjum/khoj/interface/emacs
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License
|
|
|
|
;; as published by the Free Software Foundation; either version 3
|
|
|
|
;; of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2022-07-27 14:18:17 +00:00
|
|
|
;; This package provides a natural, incremental search interface to your
|
|
|
|
;; org-mode notes, markdown files, beancount transactions and images.
|
2022-07-19 14:26:16 +00:00
|
|
|
;; It is a wrapper that interfaces with transformer based ML models.
|
2022-07-27 14:18:17 +00:00
|
|
|
;; The models search capabilities are exposed via the Khoj HTTP API.
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'url)
|
|
|
|
(require 'json)
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(defcustom khoj--server-url "http://localhost:8000"
|
|
|
|
"Location of Khoj API server."
|
|
|
|
:group 'khoj
|
2021-08-16 08:27:46 +00:00
|
|
|
:type 'string)
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(defcustom khoj--image-width 156
|
2022-07-27 14:55:18 +00:00
|
|
|
"Width of rendered images returned by Khoj."
|
2022-07-19 14:26:16 +00:00
|
|
|
:group 'khoj
|
2021-09-10 08:01:23 +00:00
|
|
|
:type 'integer)
|
|
|
|
|
2022-07-27 02:58:36 +00:00
|
|
|
(defcustom khoj--rerank-after-idle-time 1.0
|
2022-07-27 14:55:18 +00:00
|
|
|
"Idle time (in seconds) to trigger cross-encoder to rerank incremental search results."
|
2022-07-27 02:58:36 +00:00
|
|
|
:group 'khoj
|
|
|
|
:type 'float)
|
|
|
|
|
2022-07-27 14:55:18 +00:00
|
|
|
(defcustom khoj--results-count 5
|
|
|
|
"Number of results to get from Khoj API for each query."
|
|
|
|
:group 'khoj
|
|
|
|
:type 'integer)
|
|
|
|
|
2022-07-27 02:58:36 +00:00
|
|
|
(defvar khoj--rerank-timer nil
|
|
|
|
"Idle timer to make cross-encoder re-rank incremental search results if user idle.")
|
|
|
|
|
2022-07-27 16:08:37 +00:00
|
|
|
(defvar khoj--minibuffer-window nil
|
|
|
|
"Minibuffer window being used by user to enter query.")
|
|
|
|
|
2022-07-29 13:50:29 +00:00
|
|
|
(defconst khoj--query-prompt "🦅Khoj: "
|
2022-07-27 00:14:14 +00:00
|
|
|
"Query prompt shown to user in the minibuffer.")
|
|
|
|
|
2022-08-05 14:31:46 +00:00
|
|
|
(defconst khoj--buffer-name "*🦅Khoj*"
|
|
|
|
"Name of buffer to show results from Khoj.")
|
|
|
|
|
2022-07-27 02:13:04 +00:00
|
|
|
(defvar khoj--search-type "org"
|
|
|
|
"The type of content to perform search on.")
|
|
|
|
|
2022-08-05 17:15:51 +00:00
|
|
|
(defvar khoj--keybindings-info-message
|
2022-08-05 16:32:58 +00:00
|
|
|
"
|
|
|
|
Set Search Type
|
|
|
|
-------------------------
|
|
|
|
C-x m | markdown
|
|
|
|
C-x o | org-mode
|
|
|
|
C-x l | ledger/beancount
|
|
|
|
C-x i | images
|
|
|
|
")
|
2022-08-06 12:19:01 +00:00
|
|
|
(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"))
|
2022-08-05 17:15:51 +00:00
|
|
|
(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"
|
2022-08-06 13:27:23 +00:00
|
|
|
(if (fboundp 'which-key-show-full-keymap)
|
|
|
|
(let ((khoj--keymap (khoj--make-search-keymap)))
|
|
|
|
(which-key-show-full-keymap 'khoj--keymap))
|
2022-08-05 17:15:51 +00:00
|
|
|
(message "%s" khoj--keybindings-info-message)))
|
2022-08-05 16:32:58 +00:00
|
|
|
|
2022-07-21 16:22:24 +00:00
|
|
|
(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
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^[\(\) ]" ""
|
|
|
|
;; extract entries from response as single string and convert to entries
|
|
|
|
(format "# %s\n%s"
|
|
|
|
query
|
|
|
|
(mapcar
|
2022-08-01 00:54:35 +00:00
|
|
|
(lambda (args)
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^\#+" "##"
|
|
|
|
(format "%s" (cdr (assoc 'entry args)))))
|
2022-07-21 16:22:24 +00:00
|
|
|
json-response))))
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(defun khoj--extract-entries-as-org (json-response query)
|
2021-08-16 08:27:46 +00:00
|
|
|
"Convert json response from API to org-mode entries"
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^[\(\) ]" ""
|
|
|
|
;; extract entries from response as single string and convert to entries
|
2022-08-05 02:27:57 +00:00
|
|
|
(format "#+STARTUP: showall hidestars inlineimages\n* %s\n%s"
|
2021-09-29 20:30:15 +00:00
|
|
|
query
|
2021-08-16 08:27:46 +00:00
|
|
|
(mapcar
|
2022-08-01 00:54:35 +00:00
|
|
|
(lambda (args)
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^\*+" "**"
|
|
|
|
(format "%s" (cdr (assoc 'entry args)))))
|
|
|
|
json-response))))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(defun khoj--extract-entries-as-images (json-response query)
|
2022-07-21 16:22:24 +00:00
|
|
|
"Convert json response from API to html with images"
|
2021-09-10 08:01:23 +00:00
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
(replace-regexp-in-string
|
2022-07-16 15:31:49 +00:00
|
|
|
"[\(\) ]$" ""
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^[\(\) ]" ""
|
|
|
|
;; extract entries from response as single string and convert to entries
|
|
|
|
(format "<html>\n<body>\n<h1>%s</h1>%s\n\n</body>\n</html>"
|
|
|
|
query
|
|
|
|
(mapcar
|
|
|
|
(lambda (args) (format
|
|
|
|
"\n\n<h2>Score: %s Meta: %s Image: %s</h2>\n\n<a href=\"%s%s\">\n<img src=\"%s%s?%s\" width=100 height=100>\n</a>"
|
|
|
|
(cdr (assoc 'score args))
|
|
|
|
(cdr (assoc 'metadata_score args))
|
|
|
|
(cdr (assoc 'image_score args))
|
2022-07-19 14:26:16 +00:00
|
|
|
khoj--server-url
|
2022-07-16 15:31:49 +00:00
|
|
|
(cdr (assoc 'entry args))
|
2022-07-19 14:26:16 +00:00
|
|
|
khoj--server-url
|
2022-07-16 15:31:49 +00:00
|
|
|
(cdr (assoc 'entry args))
|
|
|
|
(random 10000)))
|
|
|
|
json-response)))))
|
2021-09-10 08:01:23 +00:00
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(defun khoj--extract-entries-as-ledger (json-response query)
|
2021-08-16 08:27:46 +00:00
|
|
|
"Convert json response from API to ledger entries"
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
(replace-regexp-in-string
|
2022-02-26 22:33:10 +00:00
|
|
|
"[\(\) ]$" ""
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^[\(\) ]" ""
|
|
|
|
;; extract entries from response as single string and convert to entries
|
|
|
|
(format ";; %s\n\n%s\n"
|
|
|
|
query
|
|
|
|
(mapcar
|
|
|
|
(lambda (args)
|
2022-07-20 16:33:27 +00:00
|
|
|
(format "%s\n\n" (cdr (assoc 'entry args))))
|
2022-02-26 22:33:10 +00:00
|
|
|
json-response)))))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(defun khoj--buffer-name-to-search-type (buffer-name)
|
2021-08-16 08:27:46 +00:00
|
|
|
(let ((file-extension (file-name-extension buffer-name)))
|
|
|
|
(cond
|
2021-08-29 10:07:36 +00:00
|
|
|
((equal buffer-name "Music.org") "music")
|
2022-08-05 14:33:45 +00:00
|
|
|
((or (equal file-extension "bean") (equal file-extension "beancount")) "ledger")
|
2022-07-21 17:57:57 +00:00
|
|
|
((equal file-extension "org") "org")
|
2022-07-21 16:22:24 +00:00
|
|
|
((or (equal file-extension "markdown") (equal file-extension "md")) "markdown")
|
2022-07-21 17:57:57 +00:00
|
|
|
(t "org"))))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-27 02:58:36 +00:00
|
|
|
(defun khoj--construct-api-query (query search-type &optional rerank)
|
|
|
|
(let ((rerank (or rerank "false"))
|
2022-07-27 14:55:18 +00:00
|
|
|
(results-count (or khoj--results-count 5))
|
2022-07-27 02:58:36 +00:00
|
|
|
(encoded-query (url-hexify-string query)))
|
2022-07-27 14:55:18 +00:00
|
|
|
(format "%s/search?q=%s&t=%s&r=%s&n=%s" khoj--server-url encoded-query search-type rerank results-count)))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-27 00:14:14 +00:00
|
|
|
(defun khoj--query-api-and-render-results (query search-type query-url buffer-name)
|
|
|
|
;; get json response from api
|
|
|
|
(with-current-buffer buffer-name
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(erase-buffer)
|
|
|
|
(url-insert-file-contents query-url)))
|
|
|
|
;; render json response into formatted entries
|
|
|
|
(with-current-buffer buffer-name
|
|
|
|
(let ((inhibit-read-only t)
|
|
|
|
(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))
|
|
|
|
(t (format "%s" json-response))))
|
2022-07-26 23:05:00 +00:00
|
|
|
(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)
|
|
|
|
(org-music-mode)))
|
|
|
|
((equal search-type "image") (progn (shr-render-region (point-min) (point-max))
|
|
|
|
(goto-char (point-min))))
|
|
|
|
(t (fundamental-mode))))
|
2022-07-27 00:14:14 +00:00
|
|
|
(read-only-mode t)))
|
|
|
|
|
2022-07-27 16:08:37 +00:00
|
|
|
|
2022-07-27 00:14:14 +00:00
|
|
|
;; Incremental Search on Khoj
|
2022-07-27 16:08:37 +00:00
|
|
|
(defun khoj--incremental-search (&optional rerank)
|
|
|
|
(let* ((rerank-str (cond (rerank "true") (t "false")))
|
2022-08-05 14:31:46 +00:00
|
|
|
(khoj-buffer-name (get-buffer-create khoj--buffer-name))
|
2022-07-27 00:14:14 +00:00
|
|
|
(query (minibuffer-contents-no-properties))
|
2022-08-05 14:23:14 +00:00
|
|
|
(query-url (khoj--construct-api-query query khoj--search-type rerank-str)))
|
2022-08-05 16:34:12 +00:00
|
|
|
;; 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))
|
2022-07-27 23:37:16 +00:00
|
|
|
(progn
|
|
|
|
(when rerank
|
2022-08-05 16:34:12 +00:00
|
|
|
(message "Khoj: Rerank Results"))
|
2022-07-27 23:37:16 +00:00
|
|
|
(khoj--query-api-and-render-results
|
|
|
|
query
|
2022-08-05 14:23:14 +00:00
|
|
|
khoj--search-type
|
2022-07-27 23:37:16 +00:00
|
|
|
query-url
|
2022-08-05 14:31:46 +00:00
|
|
|
khoj-buffer-name)))))
|
2022-07-27 16:08:37 +00:00
|
|
|
|
2022-08-03 12:14:34 +00:00
|
|
|
(defun delete-open-network-connections-to-khoj ()
|
|
|
|
"Delete all network connections to khoj server"
|
|
|
|
(dolist (proc (process-list))
|
|
|
|
(let ((proc-buf (buffer-name (process-buffer proc)))
|
|
|
|
(khoj-network-proc-buf (string-join (split-string khoj--server-url "://") " ")))
|
|
|
|
(when (string-match (format "%s" khoj-network-proc-buf) proc-buf)
|
|
|
|
(delete-process proc)))))
|
|
|
|
|
2022-07-27 16:08:37 +00:00
|
|
|
(defun khoj--teardown-incremental-search ()
|
2022-08-03 12:14:34 +00:00
|
|
|
(message "[Khoj]: Teardown Incremental Search")
|
2022-07-27 23:37:16 +00:00
|
|
|
;; remove advice to rerank results on normal exit from minibuffer
|
|
|
|
(advice-remove 'exit-minibuffer #'khoj--minibuffer-exit-advice)
|
2022-07-27 16:08:37 +00:00
|
|
|
;; unset khoj minibuffer window
|
|
|
|
(setq khoj--minibuffer-window nil)
|
|
|
|
;; cancel rerank timer
|
|
|
|
(when (timerp khoj--rerank-timer)
|
|
|
|
(cancel-timer khoj--rerank-timer))
|
2022-08-03 12:14:34 +00:00
|
|
|
;; delete open connections to khoj
|
|
|
|
(delete-open-network-connections-to-khoj)
|
2022-07-27 16:08:37 +00:00
|
|
|
;; remove hooks for khoj incremental query and self
|
|
|
|
(remove-hook 'post-command-hook #'khoj--incremental-search)
|
|
|
|
(remove-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search))
|
2022-07-27 00:14:14 +00:00
|
|
|
|
2022-07-27 23:37:16 +00:00
|
|
|
(defun khoj--minibuffer-exit-advice (&rest _args)
|
|
|
|
(khoj--incremental-search t))
|
2022-07-27 00:14:14 +00:00
|
|
|
|
2022-08-06 13:27:23 +00:00
|
|
|
|
2022-07-27 00:14:14 +00:00
|
|
|
;;;###autoload
|
2022-07-27 14:18:17 +00:00
|
|
|
(defun khoj ()
|
2022-07-27 00:14:14 +00:00
|
|
|
"Natural, Incremental Search for your personal notes, transactions and music using Khoj"
|
|
|
|
(interactive)
|
2022-08-05 14:31:46 +00:00
|
|
|
(let* ((khoj-buffer-name (get-buffer-create khoj--buffer-name)))
|
2022-08-05 14:23:14 +00:00
|
|
|
;; 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))))
|
2022-07-27 16:08:37 +00:00
|
|
|
;; 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
|
2022-08-05 14:23:14 +00:00
|
|
|
(switch-to-buffer khoj-buffer-name)
|
2022-07-27 16:08:37 +00:00
|
|
|
;; open and setup minibuffer for incremental search
|
2022-07-27 00:14:14 +00:00
|
|
|
(minibuffer-with-setup-hook
|
|
|
|
(lambda ()
|
2022-08-05 15:47:20 +00:00
|
|
|
;; Add khoj keybindings for configuring search to minibuffer keybindings
|
|
|
|
(khoj--make-search-keymap minibuffer-local-map)
|
2022-08-05 17:15:51 +00:00
|
|
|
;; Display information on keybindings to customize khoj search
|
|
|
|
(khoj--display-keybinding-info)
|
2022-07-27 16:08:37 +00:00
|
|
|
;; 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))
|
2022-07-27 23:37:16 +00:00
|
|
|
;; rerank results on normal exit from minibuffer
|
|
|
|
(advice-add 'exit-minibuffer :before #'khoj--minibuffer-exit-advice)
|
2022-07-27 16:08:37 +00:00
|
|
|
(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
|
2022-07-27 00:14:14 +00:00
|
|
|
(read-string khoj--query-prompt))))
|
2022-07-26 22:48:27 +00:00
|
|
|
|
2021-09-10 05:10:37 +00:00
|
|
|
;;;###autoload
|
2022-07-27 14:18:17 +00:00
|
|
|
(defun khoj-simple (query)
|
2022-07-27 16:08:37 +00:00
|
|
|
"Natural Search for QUERY in your personal notes, transactions, music and images using Khoj"
|
2022-07-29 13:50:29 +00:00
|
|
|
(interactive "s🦅Khoj: ")
|
2022-07-27 14:18:17 +00:00
|
|
|
(let* ((rerank "true")
|
|
|
|
(default-type (khoj--buffer-name-to-search-type (buffer-name)))
|
2022-07-21 17:57:57 +00:00
|
|
|
(search-type (completing-read "Type: " '("org" "markdown" "ledger" "music" "image") nil t default-type))
|
2022-07-27 14:18:17 +00:00
|
|
|
(query-url (khoj--construct-api-query query search-type rerank))
|
2022-08-05 14:31:46 +00:00
|
|
|
(buffer-name (get-buffer-create (format "*%s (q:%s t:%s)*" khoj--buffer-name query search-type))))
|
2022-07-27 00:14:14 +00:00
|
|
|
(khoj--query-api-and-render-results
|
|
|
|
query
|
|
|
|
search-type
|
|
|
|
query-url
|
|
|
|
buffer-name)
|
|
|
|
(switch-to-buffer buffer-name)))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
(provide 'khoj)
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
;;; khoj.el ends here
|