branch: elpa/aidermacs commit e5495ff36280fec8b37dd4e7734757680d05caac Author: Mingde (Matthew) Zeng <matthew...@posteo.net> Commit: Mingde (Matthew) Zeng <matthew...@posteo.net>
Re-introduce aidermacs--comint-output-filter, simplify callback mechanism We need this to handle diff more generally, related to https://github.com/MatthewZMD/aidermacs/pull/21#issuecomment-2660983638 --- aidermacs-backend-comint.el | 19 ++++++++++++++++--- aidermacs-backend-vterm.el | 10 +--------- aidermacs-backends.el | 14 +++++++++++++- aidermacs-models.el | 4 ++-- aidermacs.el | 4 ++-- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/aidermacs-backend-comint.el b/aidermacs-backend-comint.el index 2965fa612e..7eff85e147 100644 --- a/aidermacs-backend-comint.el +++ b/aidermacs-backend-comint.el @@ -65,7 +65,6 @@ This allows for multi-line input without sending the command." "Face for search/replace block content." :group 'aidermacs) - (defvar aidermacs-font-lock-keywords '(("^\x2500+\n?" 0 '(face aidermacs-command-separator) t) ("^\x2500+" 0 '(face nil display (space :width 2))) @@ -97,6 +96,19 @@ This allows for multi-line input without sending the command." This variable holds the actual marker text (e.g., <<<<<<< SEARCH, =======, etc.) that was matched at the start of the current syntax block.") +(defvar-local aidermacs--comint-output-temp "" + "Temporary output variable storing the raw output string.") + +(defun aidermacs--comint-output-filter (output) + "Accumulate OUTPUT string until a prompt is detected, then store it." + (when (and (aidermacs--is-aidermacs-buffer-p) (not (string-empty-p output))) + (setq aidermacs--comint-output-temp + (concat aidermacs--comint-output-temp (substring-no-properties output))) + ;; Check if the output contains a prompt + (when (string-match-p "\n[^[:space:]]*>[[:space:]]$" aidermacs--comint-output-temp) + (aidermacs--store-output aidermacs--comint-output-temp) + (setq aidermacs--comint-output-temp "")))) + (defun aidermacs-reset-font-lock-state () "Reset font lock state to default for processing a new source block." (setq aidermacs--syntax-block-delimiter nil @@ -239,7 +251,7 @@ _OUTPUT is the text to be processed." (cdr (cl-assoc-if (lambda (re) (string-match re file)) auto-mode-alist)))) 'fundamental-mode)) -(defun aidermacs-kill-buffer () +(defun aidermacs-kill-comint-syntax-fontify-buffer () "Clean up the fontify buffer." (when (bufferp aidermacs--syntax-work-buffer) (kill-buffer aidermacs--syntax-work-buffer))) @@ -264,8 +276,9 @@ BUFFER-NAME is the name for the aidermacs buffer." (setq-local comint-input-sender 'aidermacs-input-sender) (setq aidermacs--syntax-work-buffer (get-buffer-create (concat " *aidermacs-syntax" buffer-name))) - (add-hook 'kill-buffer-hook #'aidermacs-kill-buffer nil t) + (add-hook 'kill-buffer-hook #'aidermacs-kill-comint-syntax-fontify-buffer nil t) (add-hook 'comint-output-filter-functions #'aidermacs-fontify-blocks 100 t) + (add-hook 'comint-output-filter-functions #'aidermacs--comint-output-filter) (let ((local-map (make-sparse-keymap))) (set-keymap-parent local-map comint-mode-map) (define-key local-map (kbd aidermacs-comint-multiline-newline-key) #'comint-accumulate) diff --git a/aidermacs-backend-vterm.el b/aidermacs-backend-vterm.el index 70bfb832b6..7697d4426c 100644 --- a/aidermacs-backend-vterm.el +++ b/aidermacs-backend-vterm.el @@ -55,14 +55,6 @@ :type 'string :group 'aidermacs) -(defun aidermacs--is-aidermacs-vterm-buffer-p (&optional buffer) - "Check if BUFFER is an aidermacs vterm buffer. -If BUFFER is nil, check the current buffer. -Returns non-nil if the buffer name matches the aidermacs buffer pattern." - (let ((buf (or buffer (current-buffer)))) - (and (derived-mode-p 'vterm-mode) - (string-match-p "^\\*aidermacs:" (buffer-name buf))))) - (defun aidermacs--vterm-check-finish-sequence-repeated (proc orig-filter start-point expected) "Check for the finish sequence in PROC's buffer. PROC is the process to check. ORIG-FILTER is the original process filter. @@ -109,7 +101,7 @@ pattern to match. If the finish sequence is detected, store the output via ORIG-FUN is the original function being advised. ARGS are its arguments. This sets a temporary process filter that checks for the finish sequence after each output chunk, reducing the need for timers." - (if (aidermacs--is-aidermacs-vterm-buffer-p) + (if (aidermacs--is-aidermacs-buffer-p) (let* ((start-point (condition-case nil (vterm--get-prompt-point) (error (point-min)))) diff --git a/aidermacs-backends.el b/aidermacs-backends.el index 0e7fc3bf87..2302a161b3 100644 --- a/aidermacs-backends.el +++ b/aidermacs-backends.el @@ -96,7 +96,7 @@ If there's a callback function, call it with the output." (unless aidermacs--in-callback (when aidermacs--current-callback (let ((aidermacs--in-callback t)) - (funcall aidermacs--current-callback output) + (funcall aidermacs--current-callback) (setq aidermacs--current-callback nil))))) ;; Backend dispatcher functions @@ -111,6 +111,18 @@ BUFFER-NAME is the name for the aidermacs buffer." (t (aidermacs-run-comint program args buffer-name)))) +(defun aidermacs--is-aidermacs-buffer-p (&optional buffer) + "Check if BUFFER is any type of aidermacs buffer. +If BUFFER is nil, check the current buffer. +Returns non-nil if the buffer name matches the aidermacs buffer pattern +and is using either comint or vterm mode." + (let ((buf (or buffer (current-buffer)))) + (with-current-buffer buf + (and (string-match-p "^\\*aidermacs:" (buffer-name buf)) + (or (derived-mode-p 'comint-mode) + (and (fboundp 'vterm-mode) + (derived-mode-p 'vterm-mode))))))) + (defun aidermacs--send-command-backend (buffer command) "Send command to buffer using the appropriate backend. BUFFER is the target buffer. COMMAND is the text to send." diff --git a/aidermacs-models.el b/aidermacs-models.el index 0078a57a43..888cf02b9f 100644 --- a/aidermacs-models.el +++ b/aidermacs-models.el @@ -134,12 +134,12 @@ This is a private function used internally." This fetches models from various API providers and caches them." (aidermacs--send-command-redirect "/models /" - (lambda (output) + (lambda () (let* ((supported-models (seq-filter (lambda (line) (string-prefix-p "- " line)) - (split-string output "\n" t))) + (split-string aidermacs--current-output "\n" t))) (models nil)) (setq supported-models (mapcar (lambda (line) diff --git a/aidermacs.el b/aidermacs.el index 69652f92ce..a11d8a9336 100644 --- a/aidermacs.el +++ b/aidermacs.el @@ -296,8 +296,8 @@ This is useful for working in monorepos where you want to limit aider's scope." "Get list of files in current session and call CALLBACK with the result." (aidermacs--send-command-redirect "/ls" - (lambda (output) - (let ((files (aidermacs--parse-ls-output output))) + (lambda () + (let ((files (aidermacs--parse-ls-output aidermacs--current-output))) (funcall callback files))))) (defun aidermacs--send-command (command &optional switch-to-buffer use-existing)