Function: org-footnote--collect-references

org-footnote--collect-references is a byte-compiled function defined in org-footnote.el.gz.

Signature

(org-footnote--collect-references &optional ANONYMOUS)

Documentation

Collect all labeled footnote references in current buffer.

Return an alist where associations follow the pattern

  (LABEL MARKER TOP-LEVEL SIZE)

with

  LABEL the label of the of the definition,
  MARKER a marker pointing to its beginning,
  TOP-LEVEL a boolean, nil when the footnote is contained within
            another one,
  SIZE the length of the inline definition, in characters,
            or nil for non-inline references.

When optional ANONYMOUS is non-nil, also collect anonymous references. In such cases, LABEL is nil.

References are sorted according to a deep-reading order.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-footnote.el.gz
(defun org-footnote--collect-references (&optional anonymous)
  "Collect all labeled footnote references in current buffer.

Return an alist where associations follow the pattern

  (LABEL MARKER TOP-LEVEL SIZE)

with

  LABEL     the label of the of the definition,
  MARKER    a marker pointing to its beginning,
  TOP-LEVEL a boolean, nil when the footnote is contained within
            another one,
  SIZE      the length of the inline definition, in characters,
            or nil for non-inline references.

When optional ANONYMOUS is non-nil, also collect anonymous
references.  In such cases, LABEL is nil.

References are sorted according to a deep-reading order."
  (org-with-wide-buffer
   (goto-char (point-min))
   (let ((regexp (if anonymous org-footnote-re "\\[fn:[-_[:word:]]+[]:]"))
	 references nested)
     (save-excursion
       (while (re-search-forward regexp nil t)
	 ;; Ignore definitions.
	 (unless (and (eq (char-before) ?\])
		      (= (line-beginning-position) (match-beginning 0)))
	   ;; Ensure point is within the reference before parsing it.
	   (backward-char)
	   (let ((object (org-element-context)))
	     (when (org-element-type-p object 'footnote-reference)
	       (let* ((label (org-element-property :label object))
		      (begin (org-element-property :begin object))
		      (size
		       (and (eq (org-element-property :type object) 'inline)
			    (- (org-element-property :contents-end object)
			       (org-element-property :contents-begin object)))))
		 (let ((d (org-element-lineage object 'footnote-definition)))
		   (push (list label (copy-marker begin) (not d) size)
			 references)
		   (when d
		     ;; Nested references are stored in alist NESTED.
		     ;; Associations there follow the pattern
		     ;;
		     ;;   (DEFINITION-LABEL . REFERENCES)
		     (let* ((def-label (org-element-property :label d))
			    (labels (assoc def-label nested)))
		       (if labels (push label (cdr labels))
			 (push (list def-label label) nested)))))))))))
     ;; Sort the list of references.  Nested footnotes have priority
     ;; over top-level ones.
     (letrec ((ordered nil)
	      (add-reference
	       (lambda (ref allow-nested)
		 (when (or allow-nested (nth 2 ref))
		   (push ref ordered)
		   (dolist (r (mapcar (lambda (l) (assoc l references))
				      (reverse
				       (cdr (assoc (nth 0 ref) nested)))))
		     (funcall add-reference r t))))))
       (dolist (r (reverse references) (nreverse ordered))
	 (funcall add-reference r nil))))))