1717(declare-function ai-code-read-string " ai-code-input" )
1818(declare-function ai-code--insert-prompt " ai-code-prompt-mode" )
1919(declare-function ai-code--get-clipboard-text " ai-code-interface" )
20+ (declare-function ai-code-call-gptel-sync " ai-code-prompt-mode" )
2021
2122;;;### autoload
2223(defun ai-code-ask-question (arg )
@@ -324,7 +325,6 @@ Explain what this function does, its parameters, return value, algorithm, and it
324325 (when final-prompt
325326 (ai-code--insert-prompt final-prompt)))))
326327
327-
328328(defun ai-code--explain-file ()
329329 " Explain the current file."
330330 (let ((file-name (or buffer-file-name " current buffer" )))
@@ -334,6 +334,85 @@ Explain what this function does, its parameters, return value, algorithm, and it
334334 (when final-prompt
335335 (ai-code--insert-prompt final-prompt)))))
336336
337+ ;;;### autoload
338+ (defcustom ai-code-notes-file-name " .ai.code.notes.org"
339+ " Default note file name relative to the project root.
340+ This value is used by `ai-code-take-notes' when suggesting where to store notes."
341+ :type 'string
342+ :group 'ai-code )
343+
344+ ;;;### autoload
345+ (defcustom ai-code-notes-use-gptel-headline nil
346+ " Whether to use GPTel to generate headline for notes.
347+ If non-nil, call `ai-code-call-gptel-sync` to generate a smart default
348+ headline based on the selected content. Otherwise, prompt with empty default."
349+ :type 'boolean
350+ :group 'ai-code )
351+
352+ ;;;### autoload
353+ (defun ai-code-take-notes ()
354+ " Take notes from selected region and save to a note file.
355+ When there is a selected region, ask for note file path (default is
356+ .ai.code.notes.org in the git root) and section title. Add the section
357+ title as a headline at the end of the note file, and put the selected
358+ region as content of that section."
359+ (interactive )
360+ (unless (region-active-p )
361+ (user-error " No region selected. Please select the text you want to save as notes" ))
362+ (let* ((region-text (buffer-substring-no-properties (region-beginning ) (region-end )))
363+ (git-root (condition-case nil
364+ (magit-toplevel)
365+ (error nil )))
366+ (default-note-file (if git-root
367+ (expand-file-name ai-code-notes-file-name git-root)
368+ (expand-file-name ai-code-notes-file-name default-directory)))
369+ (note-file (read-file-name
370+ " Note file: "
371+ (file-name-directory default-note-file)
372+ default-note-file
373+ nil
374+ (file-name-nondirectory default-note-file)))
375+ (default-title (when ai-code-notes-use-gptel-headline
376+ (condition-case err
377+ (string-trim
378+ (ai-code-call-gptel-sync
379+ (format " Generate a concise headline (max 10 words) for this note content. Only return the headline text without quotes or extra formatting:\n\n %s "
380+ (if (> (length region-text) 500 )
381+ (substring region-text 0 500 )
382+ region-text))))
383+ (error
384+ (message " GPTel headline generation failed: %s " (error-message-string err))
385+ " " ))))
386+ (section-title (ai-code-read-string " Section title: " (or default-title " " ))))
387+ (when (string-empty-p section-title)
388+ (user-error " Section title cannot be empty" ))
389+ ; ; Create note file directory if it doesn't exist
390+ (let ((note-dir (file-name-directory note-file)))
391+ (unless (file-exists-p note-dir)
392+ (make-directory note-dir t )))
393+ ; ; Append section to note file
394+ (with-current-buffer (find-file-noselect note-file)
395+ (save-excursion
396+ (goto-char (point-max ))
397+ ; ; Add newline before new section if file is not empty
398+ (unless (bobp )
399+ (insert " \n\n " ))
400+ ; ; Insert headline
401+ (insert " * " section-title " \n " )
402+ ; ; Insert timestamp
403+ (org-insert-time-stamp (current-time ) t nil )
404+ (insert " \n\n " )
405+ ; ; Insert region content
406+ (insert region-text)
407+ (insert " \n " ))
408+ (save-buffer ))
409+ ; ; Open note file in other window and scroll to bottom
410+ (let ((note-buffer (find-file-other-window note-file)))
411+ (with-selected-window (get-buffer-window note-buffer)
412+ (goto-char (point-max ))
413+ (recenter -1 )))
414+ (message " Notes added to %s under section: %s " note-file section-title)))
415+
337416(provide 'ai-code-discussion )
338417
339418; ;; ai-code-discussion.el ends here
0 commit comments