org-export: Correctly handle predicate in `org-export-get-ordinal'

* contrib/lisp/org-export.el (org-export-get-ordinal): Correctly
  handle predicate in `org-export-get-ordinal'. Also allow to count
  more than one element type in the same sequence with optional
  argument TYPES.
This commit is contained in:
Nicolas Goaziou 2012-01-20 19:17:11 +01:00
parent 10f26fb805
commit 3dc734eaf2
1 changed files with 9 additions and 5 deletions

View File

@ -2395,12 +2395,17 @@ INFO is a plist holding export options."
;; `org-export-get-ordinal' associates a sequence number to any object ;; `org-export-get-ordinal' associates a sequence number to any object
;; or element. ;; or element.
(defun org-export-get-ordinal (element info &optional within-section predicate) (defun org-export-get-ordinal
(element info &optional types within-section predicate)
"Return ordinal number of an element or object. "Return ordinal number of an element or object.
ELEMENT is the element or object considered. INFO is the plist ELEMENT is the element or object considered. INFO is the plist
used as a communication channel. used as a communication channel.
Optional argument TYPES, when non-nil, is a list of element or
object types, as symbols, that should also be counted in.
Otherwise, only provided element's type is considered.
When optional argument WITHIN-SECTION is non-nil, narrow counting When optional argument WITHIN-SECTION is non-nil, narrow counting
to the section containing ELEMENT. to the section containing ELEMENT.
@ -2411,7 +2416,6 @@ This argument allows to count only a certain type of objects,
like inline images, which are a subset of links \(in that case, like inline images, which are a subset of links \(in that case,
`org-export-inline-image-p' might be an useful predicate\)." `org-export-inline-image-p' might be an useful predicate\)."
(let ((counter 0) (let ((counter 0)
(type (car element))
;; Determine if search should apply to current section, in ;; Determine if search should apply to current section, in
;; which case it should be retrieved first, or to full parse ;; which case it should be retrieved first, or to full parse
;; tree. As a special case, an element or object without ;; tree. As a special case, an element or object without
@ -2423,12 +2427,12 @@ like inline images, which are a subset of links \(in that case,
(plist-get info :parse-tree))))) (plist-get info :parse-tree)))))
;; Increment counter until ELEMENT is found again. ;; Increment counter until ELEMENT is found again.
(org-element-map (org-element-map
data type data (or types (car element))
(lambda (el local) (lambda (el local)
(cond (cond
((and (functionp predicate) (funcall predicate el)))
((equal element el) (1+ counter)) ((equal element el) (1+ counter))
(t (incf counter) nil))) ((not predicate) (incf counter) nil)
((funcall predicate el) (incf counter) nil)))
info 'first-match))) info 'first-match)))