branch: externals/ellama
commit 4e1db4a155eaaeb66aa2604e27599f49b778bff4
Author: Sergey Kostyaev <[email protected]>
Commit: Sergey Kostyaev <[email protected]>
Add info node quote context elements
---
ellama.el | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/test-ellama.el | 26 ++++++++++++++++++++
2 files changed, 93 insertions(+)
diff --git a/ellama.el b/ellama.el
index e2a3895312..cdfff19790 100644
--- a/ellama.el
+++ b/ellama.el
@@ -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."
@@ -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
diff --git a/tests/test-ellama.el b/tests/test-ellama.el
index dfb0683aba..f208983da1 100644
--- a/tests/test-ellama.el
+++ b/tests/test-ellama.el
@@ -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")
@@ -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