Function: org-footnote-sort

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

Signature

(org-footnote-sort)

Documentation

Rearrange footnote definitions in the current buffer.

Sort footnote definitions so they match order of footnote references. Also relocate definitions at the end of their relative section or within a single footnote section, according to org-footnote-section. Inline definitions are ignored.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-footnote.el.gz
(defun org-footnote-sort ()
  "Rearrange footnote definitions in the current buffer.
Sort footnote definitions so they match order of footnote
references.  Also relocate definitions at the end of their
relative section or within a single footnote section, according
to `org-footnote-section'.  Inline definitions are ignored."
  (let ((references (org-footnote--collect-references)))
    (org-preserve-local-variables
     (let ((definitions (org-footnote--collect-definitions 'delete)))
       (org-with-wide-buffer
	(org-footnote--clear-footnote-section)
	;; Insert footnote definitions at the appropriate location,
	;; separated by a blank line.  Each definition is inserted
	;; only once throughout the buffer.
	(let (inserted)
	  (dolist (cell references)
	    (let ((label (car cell))
		  (nested (not (nth 2 cell)))
		  (inline (nth 3 cell)))
	      (unless (or (member label inserted) inline)
		(push label inserted)
		(unless (or org-footnote-section nested)
		  ;; If `org-footnote-section' is non-nil, or
		  ;; reference is nested, point is already at the
		  ;; correct position.  Otherwise, move at the
		  ;; appropriate location within the section
		  ;; containing the reference.
		  (goto-char (nth 1 cell))
		  (org-footnote--goto-local-insertion-point))
		(insert "\n"
			(or (cdr (assoc label definitions))
			    (format "[fn:%s] DEFINITION NOT FOUND." label))
			"\n"))))
	  ;; Insert un-referenced footnote definitions at the end.
          ;; Combine all insertions into one to create a single cache
          ;; update call.
          (org-combine-change-calls (point) (point)
	    (pcase-dolist (`(,label . ,definition) definitions)
	      (unless (member label inserted)
                (insert "\n" definition "\n"))))))))))