From 0d642627adaf6f85572a4552cd9f857b652d51c4 Mon Sep 17 00:00:00 2001 From: Vignesh Shankar Date: Fri, 26 Sep 2025 11:17:34 -0700 Subject: [PATCH 1/4] feat: add region detection for aidermacs-direct-change function Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- aidermacs.el | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/aidermacs.el b/aidermacs.el index 87812ab..5d5f4f2 100644 --- a/aidermacs.el +++ b/aidermacs.el @@ -730,9 +730,40 @@ Use highlighted region as context unless IGNORE-CONTEXT is set to non-nil." (unless (string-empty-p user-command) (concat ": " user-command))))) +(defun aidermacs--detect-code-change-region () + "Select the underlying region based on indentation (prog-mode) or paragraph. +Returns a cons cell (START . END) of the detected region, or nil if no region found." + (save-excursion + (if (derived-mode-p 'prog-mode) + ;; In programming modes, select based on indentation + (let ((start (point)) + (end (point)) + (current-indent (current-indentation))) + ;; TODO: Use built-in functions for indentation-based selection + ;; Consider using `beginning-of-defun', `end-of-defun', or similar + ;; For now, return current line as fallback + (beginning-of-line) + (setq start (point)) + (end-of-line) + (setq end (point)) + (cons start end)) + ;; In non-programming modes, select paragraph + (let ((start (point)) + (end (point))) + (backward-paragraph) + (setq start (point)) + (forward-paragraph) + (setq end (point)) + (cons start end))))) + (defun aidermacs-direct-change () - "Prompt the user for an input and send it to aidermacs prefixed with \"/code \"." + "Prompt the user for an input and send it to aidermacs prefixed with \"/code \". +If no region is active, automatically detect and select a relevant region." (interactive) + (unless (use-region-p) + (when-let ((region (aidermacs--detect-code-change-region))) + (goto-char (car region)) + (set-mark (cdr region)))) (when-let* ((command (aidermacs--form-prompt "/code" "Make this change" "will edit file"))) (aidermacs--ensure-current-file-tracked) (aidermacs--send-command command))) From 425abe0be1abe7b1331bfa5fbdb175b326273d66 Mon Sep 17 00:00:00 2001 From: Vignesh Shankar Date: Fri, 26 Sep 2025 11:26:14 -0700 Subject: [PATCH 2/4] refactor: simplify region detection to use paragraph boundaries Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- aidermacs.el | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/aidermacs.el b/aidermacs.el index 5d5f4f2..bffb532 100644 --- a/aidermacs.el +++ b/aidermacs.el @@ -731,30 +731,16 @@ Use highlighted region as context unless IGNORE-CONTEXT is set to non-nil." (concat ": " user-command))))) (defun aidermacs--detect-code-change-region () - "Select the underlying region based on indentation (prog-mode) or paragraph. + "Select the underlying region based on paragraph boundaries. Returns a cons cell (START . END) of the detected region, or nil if no region found." (save-excursion - (if (derived-mode-p 'prog-mode) - ;; In programming modes, select based on indentation - (let ((start (point)) - (end (point)) - (current-indent (current-indentation))) - ;; TODO: Use built-in functions for indentation-based selection - ;; Consider using `beginning-of-defun', `end-of-defun', or similar - ;; For now, return current line as fallback - (beginning-of-line) - (setq start (point)) - (end-of-line) - (setq end (point)) - (cons start end)) - ;; In non-programming modes, select paragraph - (let ((start (point)) - (end (point))) - (backward-paragraph) - (setq start (point)) - (forward-paragraph) - (setq end (point)) - (cons start end))))) + (let ((start (point)) + (end (point))) + (backward-paragraph) + (setq start (point)) + (forward-paragraph) + (setq end (point)) + (cons start end)))) (defun aidermacs-direct-change () "Prompt the user for an input and send it to aidermacs prefixed with \"/code \". From 400be59dae87518d28305377e048b764dca14430 Mon Sep 17 00:00:00 2001 From: Vignesh Shankar Date: Fri, 26 Sep 2025 11:27:18 -0700 Subject: [PATCH 3/4] refactor: extract code change region selection into helper function Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- aidermacs.el | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/aidermacs.el b/aidermacs.el index bffb532..5cdcc21 100644 --- a/aidermacs.el +++ b/aidermacs.el @@ -742,14 +742,18 @@ Returns a cons cell (START . END) of the detected region, or nil if no region fo (setq end (point)) (cons start end)))) +(defun aidermacs--set-code-change-region () + "Set the region based on detected code change boundaries if no region is active." + (unless (use-region-p) + (when-let ((region (aidermacs--detect-code-change-region))) + (goto-char (car region)) + (set-mark (cdr region))))) + (defun aidermacs-direct-change () "Prompt the user for an input and send it to aidermacs prefixed with \"/code \". If no region is active, automatically detect and select a relevant region." (interactive) - (unless (use-region-p) - (when-let ((region (aidermacs--detect-code-change-region))) - (goto-char (car region)) - (set-mark (cdr region)))) + (aidermacs--set-code-change-region) (when-let* ((command (aidermacs--form-prompt "/code" "Make this change" "will edit file"))) (aidermacs--ensure-current-file-tracked) (aidermacs--send-command command))) From ad9c0b5cbb8a08db71ce32d8da5835b8559ed4e8 Mon Sep 17 00:00:00 2001 From: Vignesh Shankar Date: Fri, 26 Sep 2025 11:29:45 -0700 Subject: [PATCH 4/4] Use it for the code funtions --- aidermacs.el | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/aidermacs.el b/aidermacs.el index 5cdcc21..35477e5 100644 --- a/aidermacs.el +++ b/aidermacs.el @@ -67,10 +67,10 @@ nil / 'code – start in code mode (no --chat-mode arg) 'architect – start in architect mode (--chat-mode architect + model flags) 'help – start in help mode (--chat-mode help)." :type '(choice (const :tag "Code (default)" nil) - (const :tag "Code" code) - (const :tag "Ask" ask) - (const :tag "Architect" architect) - (const :tag "Help" help))) + (const :tag "Code" code) + (const :tag "Ask" ask) + (const :tag "Architect" architect) + (const :tag "Help" help))) ;; DEPRECATED – will disappear in a future release (defcustom aidermacs-use-architect-mode nil @@ -455,8 +455,8 @@ This is useful for working in monorepos where you want to limit aider's scope." This is useful for working in complex monorepos with nested subprojects." (interactive (list (read-file-name "Choose a directory: " nil nil t nil 'file-directory-p))) - (let ((aidermacs-subtree-only t) - (default-directory directory)) + (let ((aidermacs-subtree-only t) + (default-directory directory)) (aidermacs-run))) (defun aidermacs--command-may-edit-files (command) @@ -764,6 +764,7 @@ If a region is active, include the region text in the question. If cursor is inside a function, include the function name as context. If called from the aidermacs buffer, use general question instead." (interactive) + (aidermacs--set-code-change-region) (when-let* ((command (aidermacs--form-prompt "/ask" "Propose a solution" "won't edit file"))) (aidermacs--ensure-current-file-tracked) (aidermacs--send-command command))) @@ -773,6 +774,7 @@ If called from the aidermacs buffer, use general question instead." If region is active, inspect that region. If point is in a function, inspect that function." (interactive) + (aidermacs--set-code-change-region) (when-let* ((command (aidermacs--form-prompt "/architect" "Design a solution" "confirm before edit"))) (aidermacs--ensure-current-file-tracked) (aidermacs--send-command command)))