gptel-org: Include org-element-lineage-map with gptel (#294)

* gptel-org.el (gptel-org--element-lineage-map): Include
`org-element-lineage-map` for compatibility with older versions of Org (pre
9.7).
This commit is contained in:
Karthik Chikmagalur 2024-04-25 13:58:33 -07:00
parent 44feb1637f
commit a2b16c43b1

View file

@ -28,7 +28,6 @@
(require 'outline) (require 'outline)
(declare-function org-element-begin "org-element") (declare-function org-element-begin "org-element")
(declare-function org-element-lineage-map "org-element-ast")
;; Functions used for saving/restoring gptel state in Org buffers ;; Functions used for saving/restoring gptel state in Org buffers
(defvar org-entry-property-inherited-from) (defvar org-entry-property-inherited-from)
@ -42,6 +41,33 @@
(declare-function org-get-heading "org") (declare-function org-get-heading "org")
(declare-function org-at-heading-p "org") (declare-function org-at-heading-p "org")
;; Bundle `org-element-lineage-map' if it's not available (for Org 9.67 or older)
(eval-when-compile
(if (fboundp 'org-element-lineage-map)
(progn (declare-function org-element-lineage-map "org-element-ast")
(defalias 'gptel-org--element-lineage-map 'org-element-lineage-map))
(defun gptel-org--element-lineage-map (datum fun &optional types with-self first-match)
"Map FUN across ancestors of DATUM, from closest to furthest.
DATUM is an object or element. For TYPES, WITH-SELF and
FIRST-MATCH see `org-element-lineage-map'.
This function is provided for compatibility with older versions
of Org."
(declare (indent 2))
(setq fun (if (functionp fun) fun `(lambda (node) ,fun)))
(let ((up (if with-self datum (org-element-parent datum)))
acc rtn)
(catch :--first-match
(while up
(when (or (not types) (org-element-type-p up types))
(setq rtn (funcall fun up))
(if (and first-match rtn)
(throw :--first-match rtn)
(when rtn (push rtn acc))))
(setq up (org-element-parent up)))
(nreverse acc))))))
;;; User options ;;; User options
(defcustom gptel-org-branching-context nil (defcustom gptel-org-branching-context nil
@ -139,7 +165,7 @@ value of `gptel-org-branching-context', which see."
;; Create prompt from direct ancestors of point ;; Create prompt from direct ancestors of point
(save-excursion (save-excursion
(let* ((org-buf (current-buffer)) (let* ((org-buf (current-buffer))
(start-bounds (org-element-lineage-map (start-bounds (gptel-org--element-lineage-map
(org-element-at-point) #'org-element-begin (org-element-at-point) #'org-element-begin
'(headline org-data) 'with-self)) '(headline org-data) 'with-self))
(end-bounds (end-bounds