org-footnote: don't insert footnote section without footnotes

* lisp/org-footnote.el (org-footnote-normalize): footnote section
  should be inserted only if there are footnotes to insert.
This commit is contained in:
Nicolas Goaziou 2011-07-16 10:43:46 +02:00
parent 86418aad12
commit 6d9e90454f
1 changed files with 27 additions and 26 deletions

View File

@ -632,10 +632,8 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
inlinep) ref-table))) inlinep) ref-table)))
;; Remove definition of non-inlined footnotes. ;; Remove definition of non-inlined footnotes.
(unless inlinep (org-footnote-delete-definitions lbl)))) (unless inlinep (org-footnote-delete-definitions lbl))))
;; 2. Find and remove the footnote section, if any. If we are ;; 2. Find and remove the footnote section, if any. Also
;; exporting, insert it again at end of buffer. In a non ;; determine where footnotes shall be inserted (INS-POINT).
;; org-mode file, insert instead
;; `org-footnote-tag-for-non-org-mode-files'.
(goto-char (point-min)) (goto-char (point-min))
(cond (cond
((org-mode-p) ((org-mode-p)
@ -644,37 +642,31 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
(concat "^\\*[ \t]+" (regexp-quote org-footnote-section) (concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
"[ \t]*$") "[ \t]*$")
nil t)) nil t))
(if export-props (progn
(replace-match "") (setq ins-point (match-beginning 0))
(org-back-to-heading t) (delete-region (match-beginning 0) (org-end-of-subtree t)))
(forward-line 1) (setq ins-point (point-max))))
(setq ins-point (point))
(delete-region (point) (org-end-of-subtree t)))
(goto-char (point-max))
(unless export-props
(when org-footnote-section
(or (bolp) (insert "\n"))
(insert "* " org-footnote-section "\n")
(setq ins-point (point))))))
(t (t
(if (re-search-forward (when (re-search-forward
(concat "^" (concat "^"
(regexp-quote org-footnote-tag-for-non-org-mode-files) (regexp-quote org-footnote-tag-for-non-org-mode-files)
"[ \t]*$") "[ \t]*$")
nil t) nil t)
(replace-match "")) (replace-match ""))
;; In message-mode, ensure footnotes are inserted before the ;; In message-mode, ensure footnotes are inserted before the
;; signature. ;; signature.
(let ((pt-max (let ((pt-max
(or (and (derived-mode-p 'message-mode) (or (and (derived-mode-p 'message-mode)
(save-excursion (save-excursion
(goto-char (point-max)) (goto-char (point-max))
(re-search-backward message-signature-separator nil t))) (re-search-backward
message-signature-separator nil t)
(1- (point))))
(point-max)))) (point-max))))
(goto-char pt-max) (goto-char pt-max)
(skip-chars-backward " \t\n\r") (skip-chars-backward " \t\n\r")
(forward-line)
(delete-region (point) pt-max)) (delete-region (point) pt-max))
(insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n")
(setq ins-point (point)))) (setq ins-point (point))))
;; 3. Clean-up REF-TABLE. ;; 3. Clean-up REF-TABLE.
(setq ref-table (setq ref-table
@ -701,11 +693,20 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
ins-point ins-point
(point-max))) (point-max)))
(cond (cond
((not ref-table)) ; no footnote: exit ;; No footnote: exit.
;; Cases when footnotes should be inserted together in one place. ((not ref-table))
;; Cases when footnotes should be inserted in one place.
((or (not (org-mode-p)) ((or (not (org-mode-p))
org-footnote-section org-footnote-section
(not sort-only)) (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" (insert "\n"
(mapconcat (lambda (x) (format "[%s] %s" (mapconcat (lambda (x) (format "[%s] %s"
(nth (if sort-only 0 1) x) (nth 2 x))) (nth (if sort-only 0 1) x) (nth 2 x)))