Function: org-element-ast-map
org-element-ast-map is a byte-compiled function defined in
org-element-ast.el.gz.
Signature
(org-element-ast-map DATA TYPES FUN &optional IGNORE FIRST-MATCH NO-RECURSION WITH-PROPERTIES NO-SECONDARY NO-UNDEFER)
Documentation
Map a function on selected syntax nodes.
DATA is a syntax tree. TYPES is a symbol or list of symbols of node types. FUN is the function called on the matching nodes. It has to accept one argument: the node itself.
When TYPES is t, call FUN for all the node types.
FUN can also be a Lisp form. The form will be evaluated as function
with symbol node bound to the current node.
When optional argument IGNORE is non-nil, it should be a list holding nodes to be skipped. In that case, the listed nodes and their contents will be skipped.
When optional argument FIRST-MATCH is non-nil, stop at the first match for which FUN doesn't return nil, and return that value.
Optional argument NO-RECURSION is a symbol or a list of symbols
representing node types. org-element-map won't enter any recursive
element or object whose type belongs to that list. Though, FUN can
still be applied on them.
When optional argument WITH-PROPERTIES is non-nil, it should hold a list of property names. These properties will be treated as additional secondary properties.
When optional argument NO-SECONDARY is non-nil, do not recurse into secondary strings.
When optional argument NO-UNDEFER is non-nil, do not resolve deferred values.
FUN may also throw :org-element-skip signal. Then,
org-element-ast-map will not recurse into the current node.
Nil values returned from FUN do not appear in the results.
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-element-ast.el.gz
;;;; AST queries
(defun org-element-ast-map
( data types fun
&optional
ignore first-match no-recursion
with-properties no-secondary no-undefer)
"Map a function on selected syntax nodes.
DATA is a syntax tree. TYPES is a symbol or list of symbols of
node types. FUN is the function called on the matching nodes.
It has to accept one argument: the node itself.
When TYPES is t, call FUN for all the node types.
FUN can also be a Lisp form. The form will be evaluated as function
with symbol `node' bound to the current node.
When optional argument IGNORE is non-nil, it should be a list holding
nodes to be skipped. In that case, the listed nodes and their
contents will be skipped.
When optional argument FIRST-MATCH is non-nil, stop at the first
match for which FUN doesn't return nil, and return that value.
Optional argument NO-RECURSION is a symbol or a list of symbols
representing node types. `org-element-map' won't enter any recursive
element or object whose type belongs to that list. Though, FUN can
still be applied on them.
When optional argument WITH-PROPERTIES is non-nil, it should hold a list
of property names. These properties will be treated as additional
secondary properties.
When optional argument NO-SECONDARY is non-nil, do not recurse into
secondary strings.
When optional argument NO-UNDEFER is non-nil, do not resolve deferred
values.
FUN may also throw `:org-element-skip' signal. Then,
`org-element-ast-map' will not recurse into the current node.
Nil values returned from FUN do not appear in the results."
(declare (indent 2))
;; Ensure TYPES and NO-RECURSION are a list, even of one node.
(when types
(let* ((types (pcase types
((pred listp) types)
(`t t)
(_ (list types))))
(no-recursion (if (listp no-recursion) no-recursion
(list no-recursion)))
(fun (if (functionp fun) fun `(lambda (node) ,fun)))
--acc)
(letrec ((--walk-tree
(lambda (--data)
;; Recursively walk DATA. INFO, if non-nil, is a plist
;; holding contextual information.
(let ((--type (org-element-type --data t))
recurse)
(cond
((not --data))
((not --type))
;; Ignored node in an export context.
((and ignore (memq --data ignore)))
;; List of elements or objects.
((eq --type 'anonymous)
(mapc --walk-tree (org-element-contents --data)))
(t
;; Check if TYPE is matching among TYPES. If so,
;; apply FUN to --DATA and accumulate return value
;; into --ACC (or exit if FIRST-MATCH is non-nil).
(setq recurse t)
(when (or (eq types t) (memq --type types))
(let ((result
(catch :org-element-skip
(setq recurse nil)
(prog1 (funcall fun --data)
(setq recurse t)))))
(cond ((not result))
(first-match (throw :--map-first-match result))
(t (push result --acc)))))
;; Determine if a recursion into --DATA is possible.
(cond
;; No recursion requested.
((not recurse))
;; --TYPE is explicitly removed from recursion.
((memq --type no-recursion))
;; In any other case, map secondary, affiliated, and contents.
(t
(when with-properties
(dolist (p with-properties)
(funcall
--walk-tree
(if no-undefer
(org-element-property-raw p --data)
(org-element-property p --data)))))
(unless no-secondary
(dolist (p (org-element-property :secondary --data))
(funcall
--walk-tree
(if no-undefer
(org-element-property-raw p --data)
(org-element-property p --data)))))
(mapc --walk-tree (org-element-contents --data))))))))))
(catch :--map-first-match
(funcall --walk-tree data)
;; Return value in a proper order.
(nreverse --acc))))))