Add optional region support to aimenu

This change modifies the function aimenu-get-buffer-with-line-numbers to accept optional start and end arguments, allowing it to add line numbers to a specified region instead of the entire buffer. Additionally, it adjusts usage in aimenu-show-outline to handle active region if present.
This commit is contained in:
Jeremy Dormitzer 2024-08-15 00:04:09 -04:00
parent e2580c7abe
commit d2b7817dc1

View File

@ -48,12 +48,15 @@
"Return a hash for the given string STR using SHA-256." "Return a hash for the given string STR using SHA-256."
(secure-hash 'sha256 str)) (secure-hash 'sha256 str))
(defun aimenu-get-buffer-with-line-numbers (buffer) (defun aimenu-get-buffer-with-line-numbers (buffer &optional start end)
"Return the contents of BUFFER with line numbers added to each line." "Return the contents of BUFFER with line numbers added to each line."
(with-temp-buffer (with-temp-buffer
(insert-buffer-substring buffer) (insert-buffer-substring buffer start end)
(goto-char (point-min)) (goto-char (point-min))
(let ((line-number 1)) (let ((line-number (1+ (or (when start
(with-current-buffer buffer
(line-number-at-pos start)))
0))))
(while (not (eobp)) (while (not (eobp))
(insert (format "%d: " line-number)) (insert (format "%d: " line-number))
(forward-line 1) (forward-line 1)
@ -110,7 +113,10 @@ If ARG is non-nil, prompt for an instruction for generating the outline."
(let* ((instruction (if arg (let* ((instruction (if arg
(read-string "Instruction: ") (read-string "Instruction: ")
nil)) nil))
(buffer-contents (aimenu-get-buffer-with-line-numbers (current-buffer))) (buffer-contents
(apply #'aimenu-get-buffer-with-line-numbers (current-buffer)
(when (region-active-p)
(list (region-beginning) (region-end)))))
(prompt (if instruction (prompt (if instruction
(concat buffer-contents "\n\nInstruction: " instruction) (concat buffer-contents "\n\nInstruction: " instruction)
buffer-contents)) buffer-contents))