2022-07-19 14:26:16 +00:00
|
|
|
;;; khoj.el --- Natural 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-21 16:22:24 +00:00
|
|
|
;; Version: 1.0
|
|
|
|
;; 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-19 14:26:16 +00:00
|
|
|
;; This package provides natural language search on org-mode notes,
|
2022-07-21 16:22:24 +00:00
|
|
|
;; 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.
|
|
|
|
;; 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
|
|
|
|
"Width of rendered images returned by Khoj"
|
|
|
|
:group 'khoj
|
2021-09-10 08:01:23 +00:00
|
|
|
:type 'integer)
|
|
|
|
|
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
|
|
|
|
(lambda (args) (format "%s" (cdr (assoc 'entry args))))
|
|
|
|
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
|
2021-09-29 20:30:15 +00:00
|
|
|
(format "* %s\n%s"
|
|
|
|
query
|
2021-08-16 08:27:46 +00:00
|
|
|
(mapcar
|
2022-07-20 16:33:27 +00:00
|
|
|
(lambda (args) (format "%s" (cdr (assoc 'entry args))))
|
2021-08-16 08:27:46 +00:00
|
|
|
json-response))))
|
|
|
|
|
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")
|
2021-08-16 08:27:46 +00:00
|
|
|
((equal file-extension "bean") "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-19 14:26:16 +00:00
|
|
|
(defun khoj--construct-api-query (query search-type)
|
2021-08-16 08:27:46 +00:00
|
|
|
(let ((encoded-query (url-hexify-string query)))
|
2022-07-19 14:26:16 +00:00
|
|
|
(format "%s/search?q=%s&t=%s" khoj--server-url encoded-query search-type)))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
2022-07-26 22:48:27 +00:00
|
|
|
|
2022-07-26 23:05:00 +00:00
|
|
|
;; Incremental Search on Khoj
|
2022-07-26 22:48:27 +00:00
|
|
|
(defun remove-khoj ()
|
|
|
|
(remove-hook 'after-change-functions #'query-khoj))
|
|
|
|
|
2022-07-26 23:05:00 +00:00
|
|
|
(defun khoj-incremental ()
|
|
|
|
(interactive)
|
|
|
|
(let* ((default-type (khoj--buffer-name-to-search-type (buffer-name)))
|
|
|
|
(search-type (completing-read "Type: " '("org" "markdown" "ledger" "music" "image") nil t default-type))
|
|
|
|
(buff (get-buffer-create (format "*Khoj (t:%s)*" search-type))))
|
|
|
|
(switch-to-buffer buff)
|
|
|
|
(minibuffer-with-setup-hook
|
|
|
|
(lambda ()
|
|
|
|
(add-hook 'after-change-functions #'query-khoj)
|
|
|
|
(add-hook 'minibuffer-exit-hook #'remove-khoj))
|
|
|
|
(read-string "Query: "))))
|
|
|
|
|
|
|
|
(defun query-khoj (beg end len)
|
|
|
|
(let* ((query (minibuffer-contents-no-properties))
|
|
|
|
(search-type "org")
|
|
|
|
(buff (get-buffer-create (format "*Khoj (t:%s)*" search-type))))
|
|
|
|
;; get json response from api
|
|
|
|
(with-current-buffer buff
|
|
|
|
(let ((url (khoj--construct-api-query query search-type))
|
|
|
|
(inhibit-read-only t))
|
|
|
|
(erase-buffer)
|
|
|
|
(url-insert-file-contents url)))
|
|
|
|
;; render json response into formatted entries
|
|
|
|
(with-current-buffer buff
|
|
|
|
(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))))
|
|
|
|
(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))))
|
|
|
|
(read-only-mode t))))
|
2022-07-26 22:48:27 +00:00
|
|
|
|
2021-09-10 05:10:37 +00:00
|
|
|
;;;###autoload
|
2022-07-19 14:26:16 +00:00
|
|
|
(defun khoj (query)
|
2022-07-21 16:22:24 +00:00
|
|
|
"Search your content naturally using the Khoj API"
|
2021-08-16 08:27:46 +00:00
|
|
|
(interactive "sQuery: ")
|
2022-07-19 14:26:16 +00:00
|
|
|
(let* ((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-19 14:26:16 +00:00
|
|
|
(url (khoj--construct-api-query query search-type))
|
|
|
|
(buff (get-buffer-create (format "*Khoj (q:%s t:%s)*" query search-type))))
|
2021-08-16 08:27:46 +00:00
|
|
|
;; get json response from api
|
|
|
|
(with-current-buffer buff
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(erase-buffer)
|
|
|
|
(url-insert-file-contents url)))
|
2022-07-21 16:22:24 +00:00
|
|
|
;; render json response into formatted entries
|
2021-08-16 08:27:46 +00:00
|
|
|
(with-current-buffer buff
|
|
|
|
(let ((inhibit-read-only t)
|
|
|
|
(json-response (json-parse-buffer :object-type 'alist)))
|
|
|
|
(erase-buffer)
|
|
|
|
(insert
|
2022-07-21 17:57:57 +00:00
|
|
|
(cond ((or (equal search-type "org") (equal search-type "music")) (khoj--extract-entries-as-org json-response query))
|
2022-07-21 16:22:24 +00:00
|
|
|
((equal search-type "markdown") (khoj--extract-entries-as-markdown json-response query))
|
2022-07-19 14:26:16 +00:00
|
|
|
((equal search-type "ledger") (khoj--extract-entries-as-ledger json-response query))
|
|
|
|
((equal search-type "image") (khoj--extract-entries-as-images json-response query))
|
2022-07-16 15:31:49 +00:00
|
|
|
(t (format "%s" json-response))))
|
2022-07-21 17:57:57 +00:00
|
|
|
(cond ((equal search-type "org") (org-mode))
|
2022-07-21 16:22:24 +00:00
|
|
|
((equal search-type "markdown") (markdown-mode))
|
2022-02-26 22:33:10 +00:00
|
|
|
((equal search-type "ledger") (beancount-mode))
|
2021-09-10 08:01:23 +00:00
|
|
|
((equal search-type "music") (progn (org-mode)
|
|
|
|
(org-music-mode)))
|
2022-07-16 15:31:49 +00:00
|
|
|
((equal search-type "image") (progn (shr-render-region (point-min) (point-max))
|
|
|
|
(goto-char (point-min))))
|
|
|
|
(t (fundamental-mode))))
|
2021-08-16 08:27:46 +00:00
|
|
|
(read-only-mode t))
|
|
|
|
(switch-to-buffer buff)))
|
|
|
|
|
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
|