branch: elpa/aidermacs commit 2f96105e419a956d52e977c211a6b04c4722e308 Author: Kang Tu <tni...@gmail.com> Commit: GitHub <nore...@github.com>
Several small changes (#8) * add aider-send-line-under-cursor * feat: add Aider mode with key binding for .aider files * add scratch.aider, for batch / reproducible of aider command * make switch buffer shortcut for aider-mode * feat: add aider-send-paragraph function and key binding in aider-mode * reorder function, change short-cut to fit ess key binding * feat: add function to batch add Dired marked files to Aider buffer * feat: add interactive function to run find-name-dired from Git repository root * update menu * refactor: Rename parameter in aider--send-command for clarity and update calls * update place to invoke aider--send-command. remove dup --------- Co-authored-by: Kang Tu <kang...@apple.com> --- aider.el | 64 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/aider.el b/aider.el index eae1110589..730b5b1d3a 100644 --- a/aider.el +++ b/aider.el @@ -50,24 +50,24 @@ This function can be customized or redefined by the user." ["Aider process" ("a" "Run Aider" aider-run-aider) ("z" "Switch to Aider Buffer" aider-switch-to-buffer) - ("l" "Clear Aider" aider-clear) ;; Menu item for clear command - ("s" "Reset Aider" aider-reset) ;; Menu item for reset command + ("l" "Clear Aider" aider-clear) + ("s" "Reset Aider" aider-reset) ] ["Add file to aider" ("f" "Add Current File" aider-add-current-file) ("F" "Find Files in the Git Repo" aider-repo-find-name-dired) ("b" "Batch Add Dired Marked Files" aider-batch-add-dired-marked-files) + ("R" "Open Git Repo Root Dired" aider-git-repo-root-dired) ] ["Code change" ("c" "Code Change" aider-code-change) ("r" "Region Code Refactor" aider-region-refactor) - ("u" "Undo Last Change" aider-undo-last-change) ;; Menu item for undo last change + ("u" "Undo Last Change" aider-undo-last-change) ] ["Discussion" ("q" "Ask Question" aider-ask-question) ("t" "Architect Discussion" aider-architect-discussion) - ("d" "Debug Exception" aider-debug-exception) ;; Menu item for debug command - ("Q" "Ask Question Under Cursor" aider-ask-question-under-cursor) + ("d" "Debug Exception" aider-debug-exception) ] ["Other" ("g" "General Command" aider-general-command) @@ -122,18 +122,15 @@ If not in a git repository, an error is raised." (defun aider-clear () "Send the command \"/clear\" to the Aider buffer." (interactive) - (aider--send-command "/clear" t)) + (aider--send-command "/clear")) (defun aider-reset () "Send the command \"/reset\" to the Aider buffer." (interactive) - (aider--send-command "/reset" t)) + (aider--send-command "/reset")) ;; Shared helper function to send commands to corresponding aider buffer -(defun aider--send-command (command &optional not-switch-to-buffer) - "Send COMMAND to the corresponding aider comint buffer after performing necessary checks. -COMMAND should be a string representing the command to send. -SWITCH-TO-BUFFER determines whether to switch to the Aider buffer after sending the command. Default is t." +(defun aider--send-command (command &optional switch-to-buffer) "Send COMMAND to the corresponding aider comint buffer after performing necessary checks. COMMAND should be a string representing the command to send." ;; Check if the corresponding aider buffer exists @@ -149,7 +146,7 @@ COMMAND should be a string representing the command to send." (comint-send-string aider-buffer command) ;; Provide feedback to the user (message "Sent command to aider buffer: %s" (string-trim command)) - (when (not not-switch-to-buffer) + (when switch-to-buffer (aider-switch-to-buffer))) (message "No active process found in buffer %s." (aider-buffer-name)))) (message "Buffer %s does not exist. Please start 'aider' first." (aider-buffer-name)))) @@ -163,7 +160,7 @@ COMMAND should be a string representing the command to send." (message "Current buffer is not associated with a file.") (let ((command (format "/add %s" (expand-file-name buffer-file-name)))) ;; Use the shared helper function to send the command - (aider--send-command command t)))) + (aider--send-command command)))) ;; Function to send a custom command to corresponding aider buffer (defun aider-general-command () @@ -172,7 +169,7 @@ COMMAND should be a string representing the command to send." (let ((command (aider-read-string "Enter command to send to aider: "))) ;; Use the shared helper function to send the command (aider-add-current-file) - (aider--send-command command))) + (aider--send-command command t))) ;; New function to get command from user and send it prefixed with "/code " (defun aider-code-change () @@ -193,7 +190,7 @@ COMMAND should be a string representing the command to send." "Prompt the user for a command and send it to the corresponding aider comint buffer prefixed with \"/help \"." (interactive) (let ((command (aider-read-string "Enter help question: "))) - (aider--send-command (concat "/help " command)))) + (aider-send-command-with-prefix "/help " command))) ;; New function to get command from user and send it prefixed with "/architect " (defun aider-architect-discussion () @@ -208,13 +205,13 @@ COMMAND should be a string representing the command to send." replacing all newline characters except for the one at the end." (interactive) (let ((command (aider-plain-read-string "Enter exception, can be multiple lines: "))) - (aider--send-command (concat "/ask Investigate the following exception, with current added files as context: " command)))) + (aider--send-command (concat "/ask Investigate the following exception, with current added files as context: " command) t))) ;; Modified function to get command from user and send it based on selected region (defun aider-undo-last-change () "Undo the last change made by Aider." (interactive) - (aider--send-command "/undo" t)) + (aider--send-command "/undo")) (defun aider-region-refactor-generate-command (region-text function-name user-command) "Generate the command string based on REGION-TEXT, FUNCTION-NAME, and USER-COMMAND." @@ -234,21 +231,15 @@ The command will be formatted as \"/architect \" followed by the user command an (function-name (which-function)) (user-command (aider-read-string "Enter your refactor instruction: ")) (command (aider-region-refactor-generate-command region-text function-name user-command))) - (aider--send-command command)) - (aider-add-current-file) + (aider-add-current-file) + (aider--send-command command t) + ) (message "No region selected."))) (defun aider-send-command-with-prefix (prefix command) "Send COMMAND to the Aider buffer prefixed with PREFIX." (aider-add-current-file) - (aider--send-command (concat prefix command))) - -;; New function to send "ask <line under cursor>" to the Aider buffer -(defun aider-ask-question-under-cursor () - "Send the command \"ask <line under cursor>\" to the Aider buffer." - (interactive) - (let ((line (thing-at-point 'line t))) - (aider--send-command (concat "/ask " (string-trim line))))) + (aider--send-command (concat prefix command) t)) ;;; functions for dired related @@ -259,7 +250,7 @@ The command will be formatted as \"/architect \" followed by the user command an (let ((files (dired-get-marked-files))) (if files (let ((command (concat "/add " (mapconcat 'expand-file-name files " ")))) - (aider--send-command command)) + (aider--send-command command t)) (message "No files marked in Dired.")))) ;; New function to run `find-name-dired` from the Git repository root directory @@ -272,14 +263,23 @@ The command will be formatted as \"/architect \" followed by the user command an (message "Not in a git repository") (find-name-dired repo-path pattern)))) -;;; functions for .aider file +(defun aider-git-repo-root-dired () + "Open a Dired buffer at the root of the current Git repository." + (interactive) + (let ((git-repo-path (shell-command-to-string "git rev-parse --show-toplevel"))) + (if (string-match-p "fatal" git-repo-path) + (message "The current buffer is not in a Git repository.") + (let ((repo-path (string-trim git-repo-path))) + (dired-other-window repo-path))))) + +;;; functions for .aider file related ;; New function to send "<line under cursor>" to the Aider buffer (defun aider-send-line-under-cursor () "Send the command \"ask <line under cursor>\" to the Aider buffer." (interactive) (let ((line (thing-at-point 'line t))) - (aider--send-command (string-trim line)))) + (aider--send-command (string-trim line) t))) ;;; New function to send the current paragraph to the Aider buffer (defun aider-send-paragraph () @@ -292,7 +292,7 @@ The command will be formatted as \"/architect \" followed by the user command an (save-excursion (forward-paragraph) (point))))) - (aider--send-command (string-trim paragraph)))) + (aider--send-command (string-trim paragraph) t))) (defun aider-mode-setup () "Setup key bindings for Aider mode." @@ -304,7 +304,7 @@ The command will be formatted as \"/architect \" followed by the user command an (define-derived-mode aider-mode fundamental-mode "Aider" "Major mode for editing Aider files." ;; Add any additional setup for aider-mode here -) + ) (add-to-list 'auto-mode-alist '("\\.aider\\'" . aider-mode))