Skip to content

Commit

Permalink
Merge pull request #125 from s-kostyaev/add-info-node-quote
Browse files Browse the repository at this point in the history
Add info node quote context elements
  • Loading branch information
s-kostyaev authored Jun 9, 2024
2 parents 60909fe + 4e1db4a commit fc8c9dc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
67 changes: 67 additions & 0 deletions ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,51 @@ If EPHEMERAL non nil new session will not be associated with any file."
(format "[[%s][%s]]:\n#+BEGIN_QUOTE\n%s\n#+END_QUOTE\n" url name content)
(format "[[%s][%s]]" url name))))

;; Info node quote context elements

(defclass ellama-context-element-info-node-quote (ellama-context-element)
((name :initarg :name :type string)
(content :initarg :content :type string))
"A structure for holding information about a context element.")

(cl-defmethod ellama-context-element-extract
((element ellama-context-element-info-node-quote))
"Extract the content of the context ELEMENT."
(oref element content))

(cl-defmethod ellama-context-element-format
((element ellama-context-element-info-node-quote) (mode (eql 'markdown-mode)))
"Format the context ELEMENT for the major MODE."
(ignore mode)
(with-slots (name content) element
(if ellama-show-quotes
(format "```emacs-lisp\n(info \"%s\")\n```\n%s\n\n"
name
(ellama--md-quote content))
(format "```emacs-lisp\n(info \"%s\")\n```\n" name))))

(cl-defmethod ellama-context-element-format
((element ellama-context-element-info-node-quote) (mode (eql 'org-mode)))
"Format the context ELEMENT for the major MODE."
(ignore mode)
(with-slots (name content) element
(if ellama-show-quotes
(format "[[%s][%s]]:\n#+BEGIN_QUOTE\n%s\n#+END_QUOTE\n"
(replace-regexp-in-string
"(\\(.?*\\)) \\(.*\\)" "info:\\1#\\2" name)
(if (and ellama-chat-translation-enabled
(not ellama--current-session))
(ellama--translate-string name)
name)
content)
(format "[[%s][%s]]"
(replace-regexp-in-string
"(\\(.?*\\)) \\(.*\\)" "info:\\1#\\2" name)
(if (and ellama-chat-translation-enabled
(not ellama--current-session))
(ellama--translate-string name)
name)))))

;;;###autoload
(defun ellama-context-add-file ()
"Add file to context."
Expand Down Expand Up @@ -985,6 +1030,28 @@ If EPHEMERAL non nil new session will not be associated with any file."
(let ((element (ellama-context-element-info-node :name node)))
(ellama-context-element-add element)))

(defun ellama-context-add-info-node-quote-noninteractive (name content)
"Add webpage with NAME quote CONTENT to context."
(let ((element (ellama-context-element-info-node-quote
:name name :content content)))
(ellama-context-element-add element)))

;;;###autoload
(defun ellama-context-add-info-node-quote ()
"Add info node quote to context interactively."
(interactive)
(let ((name (Info-copy-current-node-name))
(content (if (region-active-p)
(buffer-substring-no-properties
(region-beginning)
(region-end))
(buffer-substring-no-properties
(point-min)
(point-max)))))
(if (not name)
(warn "should be called from `info' buffer")
(ellama-context-add-info-node-quote-noninteractive name content))))

(defun ellama-context-add-webpage-quote (name url content)
"Add webpage with NAME and URL quote CONTENT to context."
(let ((element (ellama-context-element-webpage-quote
Expand Down
26 changes: 26 additions & 0 deletions tests/test-ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@
"
(ellama-context-element-format element 'org-mode)))))

(ert-deftest test-ellama-context-element-format-info-node-quote-disabled-markdown ()
(let ((element (ellama-context-element-info-node-quote :name "(emacs)Top" :content "1\n\n2"))
(ellama-show-quotes nil))
(should (equal "```emacs-lisp\n(info \"(emacs)Top\")\n```\n" (ellama-context-element-format element 'markdown-mode)))))

(ert-deftest test-ellama-context-element-format-info-node-quote-enabled-markdown ()
(let ((element (ellama-context-element-info-node-quote :name "(emacs)Top" :content "1\n\n2"))
(ellama-show-quotes t))
(should (equal "```emacs-lisp\n(info \"(emacs)Top\")\n```\n> 1\n> \n> 2\n\n"
(ellama-context-element-format element 'markdown-mode)))))

(ert-deftest test-ellama-context-element-format-info-node-quote-disabled-org-mode ()
(let ((element (ellama-context-element-info-node-quote :name "(emacs)Top" :content "1\n\n2"))
(ellama-show-quotes nil))
(should (equal "[[(emacs)Top][(emacs)Top]]" (ellama-context-element-format element 'org-mode)))))

(ert-deftest test-ellama-context-element-format-info-node-quote-enabled-org-mode ()
(let ((element (ellama-context-element-info-node-quote :name "(emacs)Top" :content "1\n\n2"))
(ellama-show-quotes t))
(should (equal "[[(emacs)Top][(emacs)Top]]:\n#+BEGIN_QUOTE\n1\n\n2\n#+END_QUOTE\n"
(ellama-context-element-format element 'org-mode)))))

(ert-deftest test-ellama-context-element-extract-buffer ()
(with-temp-buffer
(insert "123")
Expand All @@ -143,6 +165,10 @@
(let ((element (ellama-context-element-webpage-quote :content "123")))
(should (equal "123" (ellama-context-element-extract element)))))

(ert-deftest test-ellama-context-element-extract-info-node-quote ()
(let ((element (ellama-context-element-info-node-quote :content "123")))
(should (equal "123" (ellama-context-element-extract element)))))

(ert-deftest test-ellama-md-to-org-code-simple ()
(let ((result (ellama--translate-markdown-to-org-filter "Here is your TikZ code for a blue rectangle:
```tex
Expand Down

0 comments on commit fc8c9dc

Please sign in to comment.