Skip to content

Commit 52f1902

Browse files
psionic-kkarthink
authored andcommitted
gptel-org: combine conditions in gptel-org--create-prompt
* gptel-org.el (gptel-org--create-prompt): The warning and fallback to parsing the entire buffer can happen in the conditionals. This change is primarily structural, non-behavioral.
1 parent dc16ec5 commit 52f1902

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

gptel-org.el

+46-46
Original file line numberDiff line numberDiff line change
@@ -186,53 +186,53 @@ value of `gptel-org-branching-context', which see."
186186
(when topic-start
187187
;; narrow to GPTEL_TOPIC property scope
188188
(narrow-to-region topic-start prompt-end))
189-
(if gptel-org-branching-context
189+
(if (and gptel-org-branching-context
190+
(or (fboundp 'org-element-lineage-map)
191+
(prog1 nil
192+
(display-warning
193+
'(gptel org)
194+
"Using `gptel-org-branching-context' requires Org version 9.7 or higher, it will be ignored."))))
190195
;; Create prompt from direct ancestors of point
191-
(if (fboundp 'org-element-lineage-map)
192-
(save-excursion
193-
(let* ((org-buf (current-buffer))
194-
(start-bounds (gptel-org--element-lineage-map
195-
(org-element-at-point) #'gptel-org--element-begin
196-
'(headline org-data) 'with-self))
197-
(end-bounds
198-
(cl-loop
199-
for (pos . rest) on (cdr start-bounds)
200-
while
201-
(and (>= pos (point-min)) ;respect narrowing
202-
(goto-char pos)
203-
;; org-element-lineage always returns an extra
204-
;; (org-data) element at point 1. If there is also a
205-
;; heading here, it is either a false positive or we
206-
;; would be double counting it. So we reject this node
207-
;; when also at a heading.
208-
(not (and (eq pos 1) (org-at-heading-p)
209-
;; Skip if at the last element of start-bounds,
210-
;; since we captured this heading already (#476)
211-
(null rest))))
212-
do (outline-next-heading)
213-
collect (point) into ends
214-
finally return (cons prompt-end ends))))
215-
(with-temp-buffer
216-
(setq-local gptel-backend (buffer-local-value 'gptel-backend org-buf)
217-
gptel--system-message
218-
(buffer-local-value 'gptel--system-message org-buf)
219-
gptel-model (buffer-local-value 'gptel-model org-buf)
220-
gptel-mode (buffer-local-value 'gptel-mode org-buf)
221-
gptel-track-response
222-
(buffer-local-value 'gptel-track-response org-buf)
223-
gptel-track-media
224-
(buffer-local-value 'gptel-track-media org-buf))
225-
(cl-loop for start in start-bounds
226-
for end in end-bounds
227-
do (insert-buffer-substring org-buf start end)
228-
(goto-char (point-min)))
229-
(goto-char (point-max))
230-
(let ((major-mode 'org-mode))
231-
(gptel--parse-buffer gptel-backend max-entries)))))
232-
(display-warning
233-
'(gptel org)
234-
"Using `gptel-org-branching-context' requires Org version 9.7 or higher, it will be ignored.")
235-
(gptel--parse-buffer gptel-backend max-entries))
196+
(save-excursion
197+
(let* ((org-buf (current-buffer))
198+
(start-bounds (gptel-org--element-lineage-map
199+
(org-element-at-point) #'gptel-org--element-begin
200+
'(headline org-data) 'with-self))
201+
(end-bounds
202+
(cl-loop
203+
for (pos . rest) on (cdr start-bounds)
204+
while
205+
(and (>= pos (point-min)) ;respect narrowing
206+
(goto-char pos)
207+
;; org-element-lineage always returns an extra
208+
;; (org-data) element at point 1. If there is also a
209+
;; heading here, it is either a false positive or we
210+
;; would be double counting it. So we reject this node
211+
;; when also at a heading.
212+
(not (and (eq pos 1) (org-at-heading-p)
213+
;; Skip if at the last element of start-bounds,
214+
;; since we captured this heading already (#476)
215+
(null rest))))
216+
do (outline-next-heading)
217+
collect (point) into ends
218+
finally return (cons prompt-end ends))))
219+
(with-temp-buffer
220+
(setq-local gptel-backend (buffer-local-value 'gptel-backend org-buf)
221+
gptel--system-message
222+
(buffer-local-value 'gptel--system-message org-buf)
223+
gptel-model (buffer-local-value 'gptel-model org-buf)
224+
gptel-mode (buffer-local-value 'gptel-mode org-buf)
225+
gptel-track-response
226+
(buffer-local-value 'gptel-track-response org-buf)
227+
gptel-track-media
228+
(buffer-local-value 'gptel-track-media org-buf))
229+
(cl-loop for start in start-bounds
230+
for end in end-bounds
231+
do (insert-buffer-substring org-buf start end)
232+
(goto-char (point-min)))
233+
(goto-char (point-max))
234+
(let ((major-mode 'org-mode))
235+
(gptel--parse-buffer gptel-backend max-entries)))))
236236
;; Create prompt the usual way
237237
(gptel--parse-buffer gptel-backend max-entries))))
238238

0 commit comments

Comments
 (0)