branch: externals/ellama
commit 80b84f869496572a7bd842ea1c53181baae8b90d
Author: Sergey Kostyaev <sskosty...@gmail.com>
Commit: Sergey Kostyaev <sskosty...@gmail.com>

    Add empty text check in filter processing
    
    Added a check to skip processing when input text is empty, preventing 
potential
    errors in subsequent operations like delete-char and insert. The existing 
logic
    is now wrapped within a condition that only executes when text is non-empty,
    ensuring the function handles empty input gracefully.
---
 ellama.el | 77 ++++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 39 insertions(+), 38 deletions(-)

diff --git a/ellama.el b/ellama.el
index b8b198fe05..5ba23084e0 100644
--- a/ellama.el
+++ b/ellama.el
@@ -1233,45 +1233,46 @@ FILTER is a function for text transformation."
           (safe-common-prefix ""))
       (lambda
        (text)
-       (with-current-buffer buffer
-         (save-excursion
-           (goto-char end-marker)
-           (let* ((filtered-text
-                   (funcall filter text))
-                  (use-hard-newlines t)
-                  (common-prefix (concat
-                                  safe-common-prefix
-                                  (ellama-max-common-prefix
-                                   (string-remove-prefix
-                                    safe-common-prefix
-                                    filtered-text)
-                                   (string-remove-prefix
+       (when (not (string-empty-p text))
+         (with-current-buffer buffer
+           (save-excursion
+             (goto-char end-marker)
+             (let* ((filtered-text
+                     (funcall filter text))
+                    (use-hard-newlines t)
+                    (common-prefix (concat
                                     safe-common-prefix
-                                    previous-filtered-text))))
-                  (wrong-chars-cnt (- (length previous-filtered-text)
-                                      (length common-prefix)))
-                  (delta (string-remove-prefix common-prefix filtered-text)))
-             (delete-char (- wrong-chars-cnt))
-             (when delta (insert (propertize delta 'hard t))
-                   (when (and
-                          ellama-fill-paragraphs
-                          (pcase ellama-fill-paragraphs
-                            ((cl-type function) (funcall 
ellama-fill-paragraphs))
-                            ((cl-type boolean) ellama-fill-paragraphs)
-                            ((cl-type list) (and (apply #'derived-mode-p
-                                                        
ellama-fill-paragraphs)))))
-                     (if (not (eq major-mode 'org-mode))
-                         (fill-paragraph)
-                       (when (not (save-excursion
-                                    (re-search-backward
-                                     "#\\+BEGIN_SRC"
-                                     beg-marker t)))
-                         (org-fill-paragraph))))
-                   (set-marker end-marker (point))
-                   (when (and ellama-auto-scroll (not ellama--stop-scroll))
-                     (ellama--scroll buffer end-marker))
-                   (setq safe-common-prefix (ellama--string-without-last-line 
common-prefix))
-                   (setq previous-filtered-text filtered-text)))))))))
+                                    (ellama-max-common-prefix
+                                     (string-remove-prefix
+                                      safe-common-prefix
+                                      filtered-text)
+                                     (string-remove-prefix
+                                      safe-common-prefix
+                                      previous-filtered-text))))
+                    (wrong-chars-cnt (- (length previous-filtered-text)
+                                        (length common-prefix)))
+                    (delta (string-remove-prefix common-prefix filtered-text)))
+               (delete-char (- wrong-chars-cnt))
+               (when delta (insert (propertize delta 'hard t))
+                     (when (and
+                            ellama-fill-paragraphs
+                            (pcase ellama-fill-paragraphs
+                              ((cl-type function) (funcall 
ellama-fill-paragraphs))
+                              ((cl-type boolean) ellama-fill-paragraphs)
+                              ((cl-type list) (and (apply #'derived-mode-p
+                                                          
ellama-fill-paragraphs)))))
+                       (if (not (eq major-mode 'org-mode))
+                           (fill-paragraph)
+                         (when (not (save-excursion
+                                      (re-search-backward
+                                       "#\\+BEGIN_SRC"
+                                       beg-marker t)))
+                           (org-fill-paragraph))))
+                     (set-marker end-marker (point))
+                     (when (and ellama-auto-scroll (not ellama--stop-scroll))
+                       (ellama--scroll buffer end-marker))
+                     (setq safe-common-prefix 
(ellama--string-without-last-line common-prefix))
+                     (setq previous-filtered-text filtered-text))))))))))
 
 (defun ellama--handle-partial (insert-text insert-reasoning reasoning-buffer)
   "Handle partial llm callback.

Reply via email to