1616
1717(declare-function ai-code-read-string " ai-code-input" )
1818(declare-function ai-code--insert-prompt " ai-code-prompt-mode" )
19+ (declare-function ai-code--get-clipboard-text " ai-code-interface" )
1920
2021;;;### autoload
2122(defun ai-code-ask-question (arg )
2223 " Generate prompt to ask questions about specific code.
23- With a prefix argument (\M -x ), prompt for a question without adding any context.
24+ With a prefix argument (C-u ), append the clipboard contents as context.
2425If current buffer is a file, keep existing logic.
2526If current buffer is a dired buffer:
2627 - If there are files or directories marked, use them as context (use git repo relative path, start with @ character)
@@ -32,18 +33,17 @@ Inserts the prompt into the AI prompt file and optionally sends to AI.
3233
3334Argument ARG is the prefix argument."
3435 (interactive " P" )
35- (if arg
36- (let ((question (ai-code-read-string " Ask question (no context): " " " )))
37- (ai-code--insert-prompt question))
36+ (let ((clipboard-context (when arg (ai-code--get-clipboard-text))))
3837 (cond
3938 ; ; Handle dired buffer
4039 ((eq major-mode 'dired-mode )
41- (ai-code--ask-question-dired))
40+ (ai-code--ask-question-dired clipboard-context ))
4241 ; ; Handle regular file buffer
43- (t (ai-code--ask-question-file)))))
42+ (t (ai-code--ask-question-file clipboard-context )))))
4443
45- (defun ai-code--ask-question-dired ()
46- " Handle ask question for dired buffer."
44+ (defun ai-code--ask-question-dired (clipboard-context )
45+ " Handle ask question for dired buffer.
46+ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
4747 (let* ((all-marked (dired-get-marked-files ))
4848 (file-at-point (dired-get-filename nil t ))
4949 (truly-marked (remove file-at-point all-marked))
@@ -55,20 +55,32 @@ Argument ARG is the prefix argument."
5555 (git-relative-files (when context-files
5656 (ai-code--get-git-relative-paths context-files)))
5757 (files-context-string (when git-relative-files
58- (concat " \n Files:\n "
59- (mapconcat (lambda (f ) (concat " @" f))
58+ (concat " \n Files:\n "
59+ (mapconcat (lambda (f ) (concat " @" f))
6060 git-relative-files " \n " ))))
6161 (prompt-label (cond
62+ ((and clipboard-context
63+ (string-match-p " \\ S-" clipboard-context))
64+ (if has-marks
65+ " Question about marked files/directories (clipboard context): "
66+ (if file-at-point
67+ (format " Question about %s (clipboard context): " (file-name-nondirectory file-at-point))
68+ " General question about directory (clipboard context): " )))
6269 (has-marks " Question about marked files/directories: " )
6370 (file-at-point (format " Question about %s : " (file-name-nondirectory file-at-point)))
6471 (t " General question about directory: " )))
6572 (question (ai-code-read-string prompt-label " " ))
66- (final-prompt (concat question files-context-string
73+ (final-prompt (concat question
74+ files-context-string
75+ (when (and clipboard-context
76+ (string-match-p " \\ S-" clipboard-context))
77+ (concat " \n\n Clipboard context:\n " clipboard-context))
6778 " \n Note: This is a question only - please do not modify the code." )))
6879 (ai-code--insert-prompt final-prompt)))
6980
70- (defun ai-code--ask-question-file ()
71- " Handle ask question for regular file buffer."
81+ (defun ai-code--ask-question-file (clipboard-context )
82+ " Handle ask question for regular file buffer.
83+ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
7284 (let* ((file-extension (when buffer-file-name
7385 (file-name-extension buffer-file-name)))
7486 (is-diff-or-patch (and file-extension
@@ -80,6 +92,18 @@ Argument ARG is the prefix argument."
8092 (buffer-substring-no-properties (region-beginning ) (region-end ))))
8193 (prompt-label
8294 (cond
95+ ((and clipboard-context
96+ (string-match-p " \\ S-" clipboard-context))
97+ (cond
98+ (region-active
99+ (if function-name
100+ (format " Question about selected code in function %s (clipboard context): " function-name)
101+ " Question about selected code (clipboard context): " ))
102+ (function-name
103+ (format " Question about function %s (clipboard context): " function-name))
104+ (buffer-file-name
105+ (format " General question about %s (clipboard context): " (file-name-nondirectory buffer-file-name)))
106+ (t " General question (clipboard context): " )))
83107 (region-active
84108 (if function-name
85109 (format " Question about selected code in function %s : " function-name)
@@ -98,6 +122,9 @@ Argument ARG is the prefix argument."
98122 (when function-name
99123 (format " \n Function: %s " function-name))
100124 files-context-string
125+ (when (and clipboard-context
126+ (string-match-p " \\ S-" clipboard-context))
127+ (concat " \n\n Clipboard context:\n " clipboard-context))
101128 " \n Note: This is a question only - please do not modify the code." )))
102129 (ai-code--insert-prompt final-prompt)))
103130
0 commit comments