branch: externals/org-transclusion
commit 76ef28480ee34b80b334f3fdbcf8b1c48177a337
Author: Noboru Ota <m...@nobiot.com>
Commit: Noboru Ota <m...@nobiot.com>

    refactor(org): content-org-buffer-or-element
    
    Add content-filter-org-functions.
---
 org-transclusion.el | 104 +++++++++++++++++++++++++++++++++-------------------
 1 file changed, 67 insertions(+), 37 deletions(-)

diff --git a/org-transclusion.el b/org-transclusion.el
index af7ff9c4a8..c5d66e70d3 100644
--- a/org-transclusion.el
+++ b/org-transclusion.el
@@ -942,7 +942,7 @@ inserted when more than one space is inserted between 
symbols."
 ;;-----------------------------------------------------------------------------
 ;;;; Add-at-point functions
 (defun org-transclusion-add-org-id (link plist)
-  "Return a list for Org-ID LINK object and PLIST.
+  "Return the payload list for Org-ID LINK object and PLIST.
 Return nil if not found."
   (when (string= "id" (org-element-property :type link))
     ;; when type is id, the value of path is the id
@@ -957,7 +957,7 @@ Return nil if not found."
         nil))))
 
 (defun org-transclusion-add-org-file (link plist)
-  "Return a list for Org file LINK object and PLIST.
+  "Return the payload list for Org file LINK object and PLIST.
 Return nil if not found."
   (and (string= "file" (org-element-property :type link))
        (org-transclusion-org-file-p (org-element-property :path link))
@@ -965,7 +965,7 @@ Return nil if not found."
                (org-transclusion-content-org-link link plist))))
 
 (defun org-transclusion-add-other-file (link plist)
-  "Return a list for non-Org file LINK object and PLIST.
+  "Return the payload list for non-Org file LINK object and PLIST.
 Return nil if not found."
   (and (string= "file" (org-element-property :type link))
        (append '(:tc-type "others-default")
@@ -1120,7 +1120,7 @@ INDENT is the number of current indentation of the 
#+transclude."
   "Return a list of payload from MARKER and PLIST.
 This function is intended to be used for Org-ID.  It delegates the
 work to
-`org-transclusion-content-org-buffer-or-element'."
+`org-transclusion-content-org-filtered'."
   (if (and marker (marker-buffer marker)
            (buffer-live-p (marker-buffer marker)))
       (progn
@@ -1128,9 +1128,9 @@ work to
           (org-with-wide-buffer
            (goto-char marker)
            (if (org-before-first-heading-p)
-               (org-transclusion-content-org-buffer-or-element
+               (org-transclusion-content-org-filtered
                 nil plist)
-             (org-transclusion-content-org-buffer-or-element
+             (org-transclusion-content-org-filtered
               'only-element plist)))))
     (message "Nothing done. Cannot find marker for the ID.")))
 
@@ -1138,7 +1138,7 @@ work to
   "Return a list of payload from Org LINK object and PLIST.
 This function is intended to be used for Org-ID.  It delegates the
 work to
-`org-transclusion-content-org-buffer-or-element'."
+`org-transclusion-content-org-filtered'."
   (save-excursion
     ;; First visit the buffer and go to the relevant element if
     ;; search-option is present.
@@ -1152,17 +1152,45 @@ work to
               org-link-search-must-match-exact-headline)))
       (with-current-buffer buf
         (org-with-wide-buffer
+<<<<<<< HEAD
          (org-transclusion-content-org-buffer-or-element
           (and search-option
                (progn
                  (org-link-search search-option)
                  t))
           plist))))))
+=======
+         (if search-option
+             (progn
+               (org-link-search search-option)
+               (org-transclusion-content-org-filtered
+                'only-element plist))
+           (org-transclusion-content-org-filtered
+            nil plist)))))))
+>>>>>>> 915600c (refactor content-org-filtered)
 
+(defvar org-transclusion-content-filter-org-functions '())
+
+(add-hook 'org-transclusion-content-filter-org-functions
+          #'org-transclusion-content-filter-org-only-contents-function)
+
+(add-hook 'org-transclusion-content-filter-org-functions
+          #'org-transclusion-content-filter-org-expand-links-function)
+
+<<<<<<< HEAD
 (defun org-transclusion-content-org-buffer-or-element (only-element plist)
   "Return a list of payload for transclusion.
 This function assumes the point is at the beginning of the org
 element to transclude.
+=======
+(make-obsolete 'org-transclusion-content-org-buffer-or-element
+               'org-transclusion-content-org-filtered "1.4.0")
+
+(defun org-transclusion-content-org-filtered (only-element plist)
+  "Return the playload list for transclusion.
+This function assumes the point is at the beginning of the org
+element to transclude.-
+>>>>>>> 915600c (refactor content-org-filtered)
 
 The payload is a plist that consists of the following properties:
 - :src-content
@@ -1182,51 +1210,47 @@ property controls the filter applied to the 
transclusion."
     (if (or (not el)(not type))
         (message "Nothing done")
       ;; For dedicated target, we want to get the parent paragraph,
-      ;; rather than the target itself
+      ;; rather than the target itself-
       (when (and (string= "target" type)
                  (string= "paragraph"
                           (org-element-type (org-element-property :parent 
el))))
         (setq el (org-element-property :parent el)))
       (let ((beg (org-element-property :begin el))
             (end (org-element-property :end el))
-            (only-contents (plist-get plist :only-contents))
-            (exclude-elements
-             (org-transclusion-keyword-plist-to-exclude-elements plist))
-            (expand-links (plist-get plist :expand-links))
             obj)
         (when only-element
           (narrow-to-region beg end))
         (setq obj (org-element-parse-buffer))
-        ;; Apply `org-transclusion-exclude-elements'
-        ;; Appending exclude-elements can duplicate symbols
-        ;; But that does not influence the output
-        (let ((org-transclusion-exclude-elements
-               (append exclude-elements org-transclusion-exclude-elements)))
-          (setq obj (org-element-map obj org-element-all-elements
-                      #'org-transclusion-content-filter-org-exclude-elements
-                      nil nil org-element-all-elements nil)))
-        ;; First section
-        (unless only-element ;only-element is nil when it is a first section
-          (setq obj (org-element-map obj org-element-all-elements
-                      #'org-transclusion-content-filter-org-first-section
-                      nil nil org-element-all-elements nil)))
-        ;; Only contents
-        (when only-contents
-          (setq obj (org-element-map obj org-element-all-elements
-                      #'org-transclusion-content-filter-org-only-contents
-                      nil nil '(section) nil)))
-
-        ;; Expand file names in all the links
-        (when expand-links
-          (org-element-map obj 'link
-            #'org-transclusion-content-filter-expand-links))
-
+        (setq obj (org-transclusion-content-org-filter only-element obj plist))
         (list :src-content (org-element-interpret-data obj)
               :src-buf (current-buffer)
               :src-beg (point-min)
               :src-end (point-max))))))
 
-(defun org-transclusion-content-filter-expand-links (link)
+(defun org-transclusion-content-org-filter (only-element obj plist)
+  ;; Apply `org-transclusion-exclude-elements'
+  ;; Appending exclude-elements can duplicate symbols
+  ;; But that does not influence the output
+  (let ((org-transclusion-exclude-elements
+         (append (org-transclusion-keyword-plist-to-exclude-elements plist)
+                 org-transclusion-exclude-elements)))
+    (setq obj (org-element-map obj org-element-all-elements
+                #'org-transclusion-content-filter-org-exclude-elements
+                nil nil org-element-all-elements nil)))
+  ;; First section
+  (unless only-element ;only-element is nil when it is a first section
+    (setq obj (org-element-map obj org-element-all-elements
+                #'org-transclusion-content-filter-org-first-section
+                nil nil org-element-all-elements nil)))
+  ;; Apply other filters
+  (run-hook-with-args 'org-transclusion-content-filter-org-functions obj plist)
+  obj)
+
+(defun org-transclusion-content-filter-org-expand-links-function (obj plist)
+  (when-let ((expand-links (plist-get plist :expand-links)))
+    (org-element-map obj 'link 
#'org-transclusion-content-filter-org-expand-links)))
+
+(defun org-transclusion-content-filter-org-expand-links (link)
   "Convert LINK to an absolute filename.
 LINK is assumed to be an Org element. This function does nothing
 to LINK if the link is already absolute.
@@ -1261,6 +1285,12 @@ is non-nil."
       nil
     data))
 
+(defun org-transclusion-content-filter-org-only-contents-function (obj plist)
+  (when-let ((only-contents (plist-get plist :only-contents)))
+    (org-element-map obj org-element-all-elements
+      #'org-transclusion-content-filter-org-only-contents
+      nil nil '(section) nil)))
+
 (defun org-transclusion-content-filter-org-only-contents (data)
   "Exclude headlines from DATA to include only contents."
   (if (eq (org-element-type data) 'headline)

Reply via email to