branch: externals/ellama
commit 90196c304c7a05a61894fa65d4b70760c0fda8a5
Merge: 7b602f5dc1 118ea80b6c
Author: Sergey Kostyaev <s-kosty...@users.noreply.github.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #214 from s-kostyaev/improve-adding-selection-to-context
    
    Add buffer quote context elements and improve adding selection to context
---
 NEWS.org             |  3 +++
 ellama.el            | 51 ++++++++++++++++++++++++++++++++++++++++++++++++---
 tests/test-ellama.el | 11 +++++++++++
 3 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index 3cbb042e31..359fbeb715 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -1,3 +1,6 @@
+* Version 1.1.1
+- Improve ~ellama-add-selection~.
+- Add buffer quote context element.
 * Version 1.1.0
 - Enhancing interaction with reasoning models. Thinking tags within
   session buffers will be collapsed by default after generation.
diff --git a/ellama.el b/ellama.el
index db6defbd37..762c758c77 100644
--- a/ellama.el
+++ b/ellama.el
@@ -6,7 +6,7 @@
 ;; URL: http://github.com/s-kostyaev/ellama
 ;; Keywords: help local tools
 ;; Package-Requires: ((emacs "28.1") (llm "0.22.0") (spinner "1.7.4") 
(transient "0.7") (compat "29.1") (posframe "1.4.0"))
-;; Version: 1.1.0
+;; Version: 1.1.1
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;; Created: 8th Oct 2023
 
@@ -1134,6 +1134,46 @@ If EPHEMERAL non nil new session will not be associated 
with any file."
   (with-slots (name) element
     (format "[[elisp:(display-buffer \"%s\")][%s]]" name name)))
 
+;; Buffer quote context elements
+
+(defclass ellama-context-element-buffer-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-buffer-quote))
+  "Extract the content of the context ELEMENT."
+  (oref element content))
+
+(cl-defmethod ellama-context-element-display
+  ((element ellama-context-element-buffer-quote))
+  "Display the context ELEMENT."
+  (oref element name))
+
+(cl-defmethod ellama-context-element-format
+  ((element ellama-context-element-buffer-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 "[%s](%s):\n%s\n\n"
+               name name
+               (ellama--md-quote content))
+      (format "[%s](%s):\n```emacs-lisp\n(display-buffer \"%s\")"
+             name name (ellama--quote-buffer content)))))
+
+(cl-defmethod ellama-context-element-format
+  ((element ellama-context-element-buffer-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"
+               name name (ellama--org-quote content))
+      (format "[[%s][%s]] [[elisp:(display-buffer \"%s\")][show]]"
+             name name (ellama--quote-buffer content)))))
+
 ;; File context element
 
 (defclass ellama-context-element-file (ellama-context-element)
@@ -1457,11 +1497,16 @@ If EPHEMERAL non nil new session will not be associated 
with any file."
 
 ;;;###autoload
 (defun ellama-context-add-selection ()
-  "Add file to context."
+  "Add active region to context."
   (interactive)
   (if (region-active-p)
       (let* ((content (buffer-substring-no-properties (region-beginning) 
(region-end)))
-             (element (ellama-context-element-text :content content)))
+            (file-name (buffer-file-name))
+            (buffer-name (buffer-name (current-buffer)))
+             (element (if file-name
+                         (ellama-context-element-file-quote :path file-name
+                                                            :content content)
+                       (ellama-context-element-buffer-quote :name buffer-name 
:content content))))
         (ellama-context-element-add element))
     (warn "No active region")))
 
diff --git a/tests/test-ellama.el b/tests/test-ellama.el
index 0944050965..818b439980 100644
--- a/tests/test-ellama.el
+++ b/tests/test-ellama.el
@@ -235,6 +235,17 @@
   (let ((element (ellama-context-element-file-quote :path "/path/to/file" 
:content "123")))
     (should (equal "file" (ellama-context-element-display element)))))
 
+(ert-deftest test-ellama-context-element-extract-buffer-quote ()
+  (with-temp-buffer
+    (insert "123")
+    (let ((element (ellama-context-element-buffer-quote :name (buffer-name) 
:content "123")))
+      (should (equal "123" (ellama-context-element-extract element))))))
+
+(ert-deftest test-ellama-context-element-display-buffer-quote ()
+  (with-temp-buffer
+    (let ((element (ellama-context-element-buffer-quote :name (buffer-name) 
:content "123")))
+      (should (equal (buffer-name) (ellama-context-element-display 
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

Reply via email to