branch: elpa/aidermacs commit da508bcc37ee450052648869f1d33272591a33e0 Author: joey <mazzare...@gmail.com> Commit: GitHub <nore...@github.com>
Doom integration (#15) * feat: Add Doom Emacs keybindings for Aider commands in doom.el * Implementation work and iteration * Update docs based on feedback * Update README.org Co-authored-by: Kang Tu <tni...@gmail.com> * update naming * cruft * better shortcuts * typo fix * checkpoing * much better, I think * update docs --------- Co-authored-by: joey (aider) <oh...@khtdr.com> Co-authored-by: Kang Tu <tni...@gmail.com> --- .gitignore | 1 + README.org | 27 ++++++++++++++++++- aider-doom.el | 60 +++++++++++++++++++++++++++++++++++++++++ helm-aider.el => aider-helm.el | 2 +- aider.el | 35 ++++++++++++++++++------ doom-menus.png | Bin 0 -> 169569 bytes 6 files changed, 115 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index b0ac3ed9c0..d5ac763886 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .aider* +.env diff --git a/README.org b/README.org index 0a21bf65f7..23e9e51980 100644 --- a/README.org +++ b/README.org @@ -30,6 +30,7 @@ * Installation +** Vanilla Emacs Installation - [[https://aider.chat/docs/install.html][Install aider]] - Install the dependency [[https://github.com/magit/transient][Transient]] using your package manager. - Install aider.el with the following code: @@ -44,9 +45,33 @@ (global-set-key (kbd "C-c a") 'aider-transient-menu)) #+END_SRC + +** Doom Installation and Configuration + +- Add the following code to your doom/packages.el + +#+BEGIN_SRC emacs-lisp +(package! aider :recipe (:host github :repo "tninja/aider.el" :files ("*.el"))) +#+END_SRC + +- Adjust and add the following code to your doom/config.el + +#+BEGIN_SRC emacs-lisp +(use-package aider + :config + (setq aider-args '("--model" "gpt-4o-mini"))) +#+END_SRC + +The aider prefix is "l". + +- Start and open the aider buffer: =[SPC] l o= +- Add the current file with =[SPC] l a c= + +[[file:./doom-menus.png]] + ** Optional -- Maybe you might want to try helm-aider.el. That file added support of command history and completion from helm. +- Maybe you might want to try aider-helm.el. That file added support of command history and completion from helm. - If you enjoy writing aider command in a separate file and send them to aider session, just like working on python or R script and send code block into REPL, you might want to try aider-minor-mode. It by default bind C-c C-n to send current line to aider session, and C-c C-c to send current region to aider session. diff --git a/aider-doom.el b/aider-doom.el new file mode 100644 index 0000000000..c4f65273b9 --- /dev/null +++ b/aider-doom.el @@ -0,0 +1,60 @@ +;;; aider-doom.el --- Description -*- lexical-binding: t; -*- +;; +;; This file is not part of GNU Emacs. +;; +;;; Commentary: +;; +;; Doom integration for Aider +;; +;;; Code: + +(defun aider-doom-setup-keys () + "Setup Aider keybindings if the current buffer is in a git repository." + (when (and (featurep 'doom-keybinds) + (vc-backend (or (buffer-file-name) default-directory))) + (map! :leader + (:prefix ("l" . "Aider") + (:prefix ("a" . "Add") + :desc "Current file" "c" #'aider-add-current-file + :desc "Files in window" "w" #'aider-add-files-in-current-window + :desc "Batch direct marked files" "b" #'aider-batch-add-dired-marked-files + :desc "Find files in repo" "g" #'aider-repo-find-name-dired + :desc "Open repo root" "d" #'aider-git-repo-root-dired) + + (:prefix ("b" . "Buffer") + :desc "Switch to Aider" "b" #'aider-switch-to-buffer + :desc "Clear Aider" "c" #'aider-clear) + + (:prefix ("s" . "Send") + :desc "File read-only" "f" #'aider-current-file-read-only + :desc "Line at cursor" "l" #'aider-send-line-under-cursor + :desc "Paragraph at cursor" "p" #'aider-send-paragraph) + + (:prefix ("c" . "Code") + :desc "Change" "c" #'aider-code-change + :desc "Refactor region" "r" #'aider-region-refactor + :desc "Undo change" "u" #'aider-undo-last-change) + + (:prefix ("d" . "Discuss") + :desc "Ask question" "a" #'aider-ask-question + :desc "Architecture" "d" #'aider-architect-discussion + :desc "Region explanation" "r" #'aider-region-explain + :desc "Exception debugggin" "e" #'aider-debug-exception) + + (:prefix ("z" . "Other") + :desc "General command" "c" #'aider-general-command + :desc "Help" "h" #'aider-help + :desc "Show last commit" "g" #'aider-magit-show-last-commit) + + + :desc "Open Aider" "o" #'aider-run-aider + :desc "Reset Aider" "r" #'aider-reset + :desc "Exit Aider" "x" #'aider-exit)))) + +;; Add the setup function to appropriate hooks +(add-hook 'find-file-hook #'aider-doom-setup-keys) +(add-hook 'dired-mode-hook #'aider-doom-setup-keys) +(add-hook 'after-change-major-mode-hook #'aider-doom-setup-keys) + +(provide 'aider-doom) +;;; aider-doom.el ends here diff --git a/helm-aider.el b/aider-helm.el similarity index 96% rename from helm-aider.el rename to aider-helm.el index 8fcc9c904c..4b8b998139 100644 --- a/helm-aider.el +++ b/aider-helm.el @@ -9,7 +9,7 @@ "History list for `aider-helm-read-string` inputs.") (defvar aider-helm-read-string-history-file - "~/.emacs.d/aider-helm-read-string-history.el" + (expand-file-name "aider-helm-read-string-history.el" user-emacs-directory) "File to save `aider-helm-read-string-history`.") (defvar aider-helm-read-string-history-max 1000 diff --git a/aider.el b/aider.el index 2e36a0d01d..7707aee1ae 100644 --- a/aider.el +++ b/aider.el @@ -1,7 +1,7 @@ ;;; aider.el --- Aider package for interactive conversation with aider -*- lexical-binding: t; -*- ;; Author: Kang Tu <tni...@gmail.com> -;; Version: 0.1.0 +;; Version: 0.2.0 ;; Package-Requires: ((emacs "25.1") (transient "0.3.0")) ;; Keywords: convenience, tools ;; URL: https://github.com/tninja/aider.el @@ -41,13 +41,13 @@ ("^\x2500+" 0 '(face nil display (space :width 2)))) "Font lock keywords for aider buffer.") + (defun aider-plain-read-string (prompt &optional initial-input) "Read a string from the user with PROMPT and optional INITIAL-INPUT. This function can be customized or redefined by the user." (let* ((input (read-string prompt initial-input)) (processed-input (replace-regexp-in-string "\n" "\\\\n" input))) - processed-input - )) + processed-input)) (defalias 'aider-read-string 'aider-plain-read-string) @@ -63,9 +63,11 @@ This function can be customized or redefined by the user." ("z" "Switch to Aider Buffer" aider-switch-to-buffer) ("l" "Clear Aider" aider-clear) ("s" "Reset Aider" aider-reset) + ("x" "Exit Aider" aider-exit) ] ["Add file to aider" ("f" "Add Current File" aider-add-current-file) + ("o" "Add Current File Read-Only" aider-current-file-read-only) ("w" "Add All Files in Current Window" aider-add-files-in-current-window) ("b" "Batch Add Dired Marked Files" aider-batch-add-dired-marked-files) ("F" "Find Files in the Git Repo" aider-repo-find-name-dired) @@ -144,6 +146,11 @@ If not in a git repository, an error is raised." (interactive) (aider--send-command "/reset")) +(defun aider-exit () + "Send the command \"/exit\" to the Aider buffer." + (interactive) + (aider--send-command "/exit")) + ;; Function to send large text (> 1024 chars) to the Aider buffer (defun aider--comint-send-large-string (buffer text) "Send large TEXT to the comint buffer in chunks of 1000 characters." @@ -191,6 +198,17 @@ COMMAND should be a string representing the command to send." ;; Use the shared helper function to send the command (aider--send-command command)))) +;; Function to send "/read <current buffer file full path>" to corresponding aider buffer +(defun aider-current-file-read-only () + "Send the command \"/read-only <current buffer file full path>\" to the corresponding aider comint buffer." + (interactive) + ;; Ensure the current buffer is associated with a file + (if (not buffer-file-name) + (message "Current buffer is not associated with a file.") + (let ((command (format "/read-only %s" (expand-file-name buffer-file-name)))) + ;; Use the shared helper function to send the command + (aider--send-command command)))) + ;; New function to add files in all buffers in current emacs window (defun aider-add-files-in-current-window () "Add files in all buffers in the current Emacs window to the Aider buffer." @@ -285,8 +303,7 @@ The command will be formatted as \"/architect \" followed by the user command an (user-command (aider-read-string "Enter your refactor instruction: ")) (command (aider-region-refactor-generate-command region-text function-name user-command))) (aider-add-current-file) - (aider--send-command command t) - ) + (aider--send-command command t)) (message "No region selected."))) ;; New function to explain the code in the selected region @@ -302,9 +319,8 @@ The command will be formatted as \"/ask \" followed by the text from the selecte (format "/ask in function %s, explain the following code block: %s" function-name processed-region-text) - (format "/ask explain the following code block: %s" - processed-region-text) - ))) + (format "/ask explain the following code block: %s" + processed-region-text)))) (aider-add-current-file) (aider--send-command command t)) (message "No region selected."))) @@ -381,6 +397,9 @@ The command will be formatted as \"/ask \" followed by the text from the selecte :lighter " Aider" :keymap aider-minor-mode-map) +(when (featurep 'doom) + (require 'aider-doom)) + (provide 'aider) ;;; aider.el ends here diff --git a/doom-menus.png b/doom-menus.png new file mode 100644 index 0000000000..70c496d3fa Binary files /dev/null and b/doom-menus.png differ