Simplify get-current-text functions for Find Similar in khoj.el

Use existing functions like `string-trim', `thing-at-point' and
remove unneeded code from the two functions
This commit is contained in:
Debanjum Singh Solanky
2023-01-23 18:41:58 -03:00
parent 07e9e4ecc3
commit 0d0bf3b5aa

View File

@@ -50,6 +50,7 @@
(require 'json) (require 'json)
(require 'transient) (require 'transient)
(require 'outline) (require 'outline)
(eval-when-compile (require 'subr-x)) ;; for string-trim before Emacs 28.2
;; ------------------------- ;; -------------------------
@@ -385,15 +386,7 @@ Use `which-key` if available, else display simple message in echo area"
(defun khoj--get-current-outline-entry-text () (defun khoj--get-current-outline-entry-text ()
"Get text under current outline section." "Get text under current outline section."
(with-current-buffer (current-buffer) (string-trim
;; jump to cursor in current buffer
(goto-char (point))
;; trim leading whitespaces from text
(replace-regexp-in-string
"^[ \t\n]*" ""
;; trim trailing whitespaces from text
(replace-regexp-in-string
"[ \t\n]*$" ""
;; get text of current outline entry ;; get text of current outline entry
(cond (cond
;; when at heading of entry ;; when at heading of entry
@@ -404,29 +397,21 @@ Use `which-key` if available, else display simple message in echo area"
;; when within entry ;; when within entry
(t (buffer-substring-no-properties (t (buffer-substring-no-properties
(save-excursion (outline-previous-heading) (point)) (save-excursion (outline-previous-heading) (point))
(save-excursion (outline-next-heading) (point))))))))) (save-excursion (outline-next-heading) (point)))))))
(defun khoj--get-current-paragraph-text () (defun khoj--get-current-paragraph-text ()
"Get text in current paragraph at point." "Get trimmed text in current paragraph at point.
(with-current-buffer (current-buffer) Paragraph only starts at first text after blank line."
;; jump to cursor in current buffer (string-trim
(goto-char (point))
;; trim leading whitespaces from text
(replace-regexp-in-string
"^[ \t\n]*" ""
;; trim trailing whitespaces from text
(replace-regexp-in-string
"[ \t\n]*$" ""
(cond (cond
;; when at beginning of a middle paragraph ;; when at end of a middle paragraph
((and (looking-at paragraph-start) (not (equal (point) (point-min)))) ((and (looking-at paragraph-start) (not (equal (point) (point-min))))
(buffer-substring-no-properties (buffer-substring-no-properties
(save-excursion (backward-paragraph) (point)) (save-excursion (backward-paragraph) (point))
(point))) (point)))
;; else ;; else
(t (buffer-substring-no-properties (t (thing-at-point 'paragraph t)))))
(save-excursion (backward-paragraph) (point))
(save-excursion (forward-paragraph) (point)))))))))
(defun khoj--find-similar (&optional content-type) (defun khoj--find-similar (&optional content-type)
"Find items of CONTENT-TYPE in khoj index similar to text surrounding point." "Find items of CONTENT-TYPE in khoj index similar to text surrounding point."