- ((or (not (org-mode-p))
- org-footnote-section
- (not sort-only))
- ;; Insert again the section title.
- (cond
- ((not (org-mode-p))
- (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n"))
- ((and org-footnote-section (not export-props))
- (or (bolp) (insert "\n"))
- (insert "* " org-footnote-section "\n")))
- ;; Insert the footnotes.
- (insert "\n"
- (mapconcat (lambda (x) (format "[%s] %s"
- (nth (if sort-only 0 1) x) (nth 2 x)))
- ref-table "\n\n")
- "\n\n")
- ;; When exporting, add newly inserted markers along with their
- ;; associated definition to `org-export-footnotes-seen'.
- (when export-props
- (setq org-export-footnotes-seen ref-table)))
- ;; Else, insert each definition at the end of the section
- ;; containing their first reference. Happens only in Org files
- ;; with no special footnote section, and only when doing
- ;; sorting.
- (t (mapc 'org-insert-footnote-reference-near-definition
- ref-table))))))
-
-(defun org-insert-footnote-reference-near-definition (entry)
- "Find first reference of footnote ENTRY and insert the definition there.
-ENTRY is (fn-label num-mark definition)."
- (when (car entry)
- (goto-char (point-min))
- (let ((ref (org-footnote-get-next-reference (car entry))))
- (when ref
- (goto-char (nth 2 ref))
- (org-footnote-goto-local-insertion-point)
- (insert (format "\n[%s] %s\n" (car entry) (nth 2 entry)))))))
+ ((or (not (derived-mode-p 'org-mode)) org-footnote-section)
+ ;; Insert again the section title, if any. Ensure that title,
+ ;; or the subsequent footnotes, will be separated by a blank
+ ;; lines from the rest of the document. In an Org buffer,
+ ;; separate section with a blank line, unless explicitly
+ ;; stated in `org-blank-before-new-entry'.
+ (if (not (derived-mode-p 'org-mode))
+ (progn (skip-chars-backward " \t\n\r")
+ (delete-region (point) ins-point)
+ (unless (bolp) (newline))
+ (when org-footnote-tag-for-non-org-mode-files
+ (insert "\n" org-footnote-tag-for-non-org-mode-files "\n")))
+ (when (and (cdr (assq 'heading org-blank-before-new-entry))
+ (zerop (save-excursion (org-back-over-empty-lines))))
+ (insert "\n"))
+ (insert "* " org-footnote-section "\n"))
+ (set-marker ins-point nil)
+ ;; Insert the footnotes, separated by a blank line.
+ (insert
+ (mapconcat
+ (lambda (x)
+ ;; Clean markers.
+ (set-marker (nth 4 x) nil)
+ (format "\n[%s] %s" (nth (if sort-only 0 1) x) (nth 2 x)))
+ ref-table "\n"))
+ (unless (eobp) (insert "\n\n")))
+ ;; Each footnote definition has to be inserted at the end of
+ ;; the section where its first reference belongs.
+ (t
+ (mapc
+ (lambda (x)
+ (let ((pos (nth 4 x)))
+ (goto-char pos)
+ ;; Clean marker.
+ (set-marker pos nil))
+ (org-footnote-goto-local-insertion-point)
+ (insert (format "\n[%s] %s\n"
+ (if sort-only (car x) (nth 1 x))
+ (nth 2 x))))
+ ref-table))))))