branch: elpa/aidermacs commit 2c837d33546a03f197b5d28f5c748fa593b40401 Author: Kang Tu <tni...@gmail.com> Commit: GitHub <nore...@github.com>
Feat: Improve aider-minor-mode (#86) * feat: Update aider-send-region to handle inactive region by marking paragraph * refactor: Rename function to aider-send-region-or-block and update key binding * refactor: Improve `aider-send-region-or-block` function with cursor preservation and mark deactivation * refactor: Simplify keybindings for sending regions in Aider minor mode * refactor: Rename and improve `aider-send-line-under-cursor` function * feat: Add Ctrl+Enter keybinding for sending line or region in aider-minor-mode * docs: Update README.org with accurate aider-minor-mode key bindings and descriptions * feat: Add auto-enable aider-minor-mode for aider-related files * feat: Enhance aider-minor-mode activation for specific file conditions * refactor: Simplify file hook condition for aider minor mode * feat: Add configurable aider prompt file name with custom variable * refactor: Reorganize and clean up code structure in aider.el * feat: Add aider-open-prompt-file function and menu option * fix: Open prompt file in other window to preserve current buffer * docs: improve documentation and fix cli behavior * docs(readme): replace aider-minor-mode with aider prompt file * docs: add aider prompt file screenshot * docs: clarify usage of aider prompt file * docs: improve documentation on prompt file usage and update example image * refactor: Rename `aider-send-region-or-block` to `aider-send-block-or-region` * docs: update aider prompt file command reference description --- README.org | 33 +++++++------------ aider.el | 86 ++++++++++++++++++++++++++++++++++++++------------ aider_prompt_file.png | Bin 0 -> 123619 bytes 3 files changed, 78 insertions(+), 41 deletions(-) diff --git a/README.org b/README.org index 074c348cf8..e81ccfa1a6 100644 --- a/README.org +++ b/README.org @@ -88,7 +88,7 @@ The aider prefix is "A". *** Helm Support -Helm enables fuzzy searching functionality for command history prompts. +Helm enables fuzzy searching functionality for command history prompts. Since it is very possible that we use prompt written before, it could potentially save lots of time typing. You can have helm-based completion with run the following code, after install helm library: @@ -97,22 +97,13 @@ You can have helm-based completion with run the following code, after install he :straight (:host github :repo "tninja/aider.el" :files ("aider.el" "aider-helm.el"))) #+END_SRC -*** Aider script interactive mode: aider-minor-mode +*** Aider prompt file -- If you prefer writing Aider commands in a separate file and sending them to an Aider session (similar to working with Python or R scripts and sending code blocks to a REPL), you might want to try aider-minor-mode. It provides the following key bindings: - - C-c C-n: If region is active, send selected region line by line; otherwise, send current line - - C-c C-c: Send current region line by line to aider session - - C-c C-r: Send current region as a single block to aider session - - Enable aider-minor-mode for your editing buffer - - To automatically enable aider-minor-mode for any file with "aider" in its filename: +- Use C-c a p to open the repo specific prompt file. You can use this file to organize tasks, and write prompt and send them to the Aider session. multi-line prompts are supported. -#+BEGIN_SRC emacs-lisp - (add-hook 'find-file-hook - (lambda () - (when (and (buffer-file-name) - (string-match-p "aider" (buffer-file-name))) - (aider-minor-mode 1)))) -#+END_SRC +- The following example shows C-c C-c key pressed when cursor is on the prompt. + +[[file:./aider_prompt_file.png]] * Most used features (integrated into the aider menu) @@ -160,12 +151,6 @@ When being called with the universal argument (`C-u`), a prompt will offer the u *** And More: You can add your own Elisp functions to support your specific use cases. Feel free to ask Aider/`aider.el` to help you create them. -* FAQ - -- How to enter multi-line prompts in aider session buffer? - - aider itself support that, [[https://aider.chat/docs/usage/commands.html#entering-multi-line-chat-messages][doc]]. I used the tag way and it works well for me. - - use a separate file for aider script to modify your multi-line prompts (you don't want them to go away right? especially if it is not perfect and need to be modified), and use aider-minor-mode to send the multi-line prompts to aider buffer. - * My personal development experience using aider.el - Here I just share my personal experience. You might have different / better way to use aider.el. @@ -226,6 +211,12 @@ You can add your own Elisp functions to support your specific use cases. Feel fr * [[./examples][Example Application Written with aider.el]] +* FAQ + +- How to enter multi-line prompts in aider session buffer? + - aider itself support that, [[https://aider.chat/docs/usage/commands.html#entering-multi-line-chat-messages][doc]]. I used the tag way and it works well for me. + - use aider prompt file (C-c a p) to write multi-line prompts (we don't want them to go away right? especially if it is not perfect and need to be modified, and we might want to reuse it sometime later). + * Other Emacs AI coding tool - Inspired by, and Thanks to: diff --git a/aider.el b/aider.el index 069fc4f024..41ee8da037 100644 --- a/aider.el +++ b/aider.el @@ -22,14 +22,6 @@ :prefix "aider-" :group 'convenience) -(defvar aider-read-string-history nil - "History list for aider read string inputs.") -(if (bound-and-true-p savehist-loaded) - (add-to-list 'savehist-additional-variables 'aider-read-string-history) - (add-hook 'savehist-mode-hook - (lambda () - (add-to-list 'savehist-additional-variables 'aider-read-string-history)))) - (defcustom aider-program "aider" "The name or path of the aider program." :type 'string @@ -58,6 +50,20 @@ Also based on aider LLM benchmark: https://aider.chat/docs/leaderboards/" :type '(repeat string) :group 'aider) +(defcustom aider-prompt-file-name ".aider.prompt.org" + "File name that will automatically enable aider-minor-mode when opened. +This is the file name without path." + :type 'string + :group 'aider) + +(defvar aider-read-string-history nil + "History list for aider read string inputs.") +(if (bound-and-true-p savehist-loaded) + (add-to-list 'savehist-additional-variables 'aider-read-string-history) + (add-hook 'savehist-mode-hook + (lambda () + (add-to-list 'savehist-additional-variables 'aider-read-string-history)))) + (defface aider-command-separator '((((type graphic)) :strike-through t :extend t) (((type tty)) :inherit font-lock-comment-face :underline t :extend t)) @@ -166,6 +172,7 @@ Affects the system message too.") ["Other" ("g" "General Command" aider-general-command) ("Q" "Ask General Question" aider-general-question) + ("p" "Open Prompt File" aider-open-prompt-file) ("h" "Help" aider-help) ] ]) @@ -726,14 +733,15 @@ Otherwise implement TODOs for the entire current file." ;; New function to send "<line under cursor>" or region line by line to the Aider buffer ;;;###autoload -(defun aider-send-line-under-cursor () - "If region is active, send the selected region line by line to the Aider buffer. -Otherwise, send the line under cursor to the Aider buffer." +(defun aider-send-line-or-region () + "Send text to the Aider buffer. +If region is active, send the selected region line by line. +Otherwise, send the line under cursor." (interactive) (if (region-active-p) (aider-send-region-by-line) (let ((line (thing-at-point 'line t))) - (aider--send-command (string-trim line) nil)))) + (aider--send-command (string-trim line) t)))) ;;; New function to send the current selected region line by line to the Aider buffer ;;;###autoload @@ -749,26 +757,56 @@ If no region is selected, show a message." (region-end)))) (mapc (lambda (line) (unless (string-empty-p line) - (aider--send-command line nil))) + (aider--send-command line t))) (split-string region-text "\n" t))) (message "No region selected."))) ;;;###autoload -(defun aider-send-region () - "Send the current active region text as a whole block to aider session." +(defun aider-send-block-or-region () + "Send the current active region text or, if no region is active, send the current paragraph content to the aider session. +When sending paragraph content, preserve cursor position and deactivate mark afterwards." (interactive) (if (region-active-p) (let ((region-text (buffer-substring-no-properties (region-beginning) (region-end)))) (unless (string-empty-p region-text) (aider--send-command region-text t))) - (message "No region selected."))) + (save-excursion ; preserve cursor position + (let ((region-text + (progn + (mark-paragraph) ; mark paragraph + (buffer-substring-no-properties (region-beginning) (region-end))))) + (unless (string-empty-p region-text) + (aider--send-command region-text t)) + (deactivate-mark))))) ; deactivate mark after sending + +;;;###autoload +(defun aider-open-prompt-file () + "Open aider prompt file under git repo root. +If file doesn't exist, create it with command binding help and sample prompt." + (interactive) + (let* ((git-root (magit-toplevel)) + (prompt-file (when git-root + (expand-file-name aider-prompt-file-name git-root)))) + (if prompt-file + (progn + (find-file-other-window prompt-file) + (unless (file-exists-p prompt-file) + ;; Insert initial content for new file + (insert "# Aider Prompt File - Command Reference:\n") + (insert "# C-c C-n or C-<return>: Send current line or selected region line by line\n") + (insert "# C-c C-c: Send current block or selected region as a whole\n") + (insert "# C-c C-z: Switch to aider buffer\n\n") + (insert "* Sample task:\n\n") + (insert "/ask what this repo is about?\n") + (save-buffer))) + (message "Not in a git repository")))) ;; Define the keymap for Aider Minor Mode (defvar aider-minor-mode-map (let ((map (make-sparse-keymap))) - (define-key map (kbd "C-c C-n") 'aider-send-line-under-cursor) - (define-key map (kbd "C-c C-c") 'aider-send-region-by-line) - (define-key map (kbd "C-c C-r") 'aider-send-region) + (define-key map (kbd "C-c C-n") 'aider-send-line-or-region) + (define-key map (kbd "C-<return>") 'aider-send-line-or-region) + (define-key map (kbd "C-c C-c") 'aider-send-block-or-region) (define-key map (kbd "C-c C-z") 'aider-switch-to-buffer) map) "Keymap for Aider Minor Mode.") @@ -778,7 +816,15 @@ If no region is selected, show a message." (define-minor-mode aider-minor-mode "Minor mode for Aider with keybindings." :lighter " Aider" - :keymap aider-minor-mode-map) + :keymap aider-minor-mode-map + :override t) + +(add-hook 'find-file-hook + (lambda () + (when (and (buffer-file-name) + (or (string-match-p "aider" (buffer-file-name)) + (string= aider-prompt-file-name (file-name-nondirectory (buffer-file-name))))) + (aider-minor-mode 1)))) (when (featurep 'doom) (require 'aider-doom)) diff --git a/aider_prompt_file.png b/aider_prompt_file.png new file mode 100644 index 0000000000..9015f2b415 Binary files /dev/null and b/aider_prompt_file.png differ