summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2016-02-27 16:05:35 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2016-02-27 16:05:35 +0100
commitae0d9fbb0976613ccd6fb9781d77ab1fde026c99 (patch)
tree7a44e64a1608acce2dd543c39809ceed47e71e67
parent2a0a37c1a727763e70f612d1829d0e3168b3595b (diff)
downloadorg-mode-ae0d9fbb0976613ccd6fb9781d77ab1fde026c99.tar.gz
org-footnote: Handle un-referenced definitions
* lisp/org-footnote.el (org-footnote-sort): (org-footnote-normalize): Insert un-referenced definitions upon sorting. * testing/lisp/test-org-footnote.el (test-org-footnote/sort): (test-org-footnote/normalize): Add tests.
-rw-r--r--lisp/org-footnote.el19
-rw-r--r--testing/lisp/test-org-footnote.el19
2 files changed, 36 insertions, 2 deletions
diff --git a/lisp/org-footnote.el b/lisp/org-footnote.el
index 58f2063..b090a45 100644
--- a/lisp/org-footnote.el
+++ b/lisp/org-footnote.el
@@ -817,7 +817,12 @@ to `org-footnote-section'. Inline definitions are ignored."
(insert "\n"
(or (cdr (assoc label definitions))
(format "[fn:%s] DEFINITION NOT FOUND." label))
- "\n")))))))
+ "\n"))))
+ ;; Insert un-referenced footnote definitions at the end.
+ (let ((unreferenced
+ (cl-remove-if (lambda (d) (member (car d) inserted))
+ definitions)))
+ (dolist (d unreferenced) (insert "\n" (cdr d) "\n"))))))
;; Clear dangling markers in the buffer.
(dolist (r references) (set-marker (nth 1 r) nil)))))
@@ -896,7 +901,17 @@ to `org-footnote-section'. Inline definitions are ignored."
(t
(replace-regexp-in-string
"\\`\\[fn:\\(.*?\\)\\]" new stored nil nil 1)))
- "\n"))))))))
+ "\n")))))
+ ;; Insert un-referenced footnote definitions at the end.
+ (let ((unreferenced
+ (cl-remove-if (lambda (d) (member (car d) inserted))
+ definitions)))
+ (dolist (d unreferenced)
+ (insert "\n"
+ (replace-regexp-in-string org-footnote-definition-re
+ (format "[fn:%d]" (incf n))
+ (cdr d))
+ "\n"))))))
;; Clear dangling markers.
(dolist (r references) (set-marker (nth 1 r) nil)))))
diff --git a/testing/lisp/test-org-footnote.el b/testing/lisp/test-org-footnote.el
index e08c7fe..253f4ee 100644
--- a/testing/lisp/test-org-footnote.el
+++ b/testing/lisp/test-org-footnote.el
@@ -433,6 +433,18 @@ Text[fn:1][fn:4]
\[fn:4] Def 4
"
(let ((org-footnote-section nil)) (org-footnote-sort))
+ (buffer-string))))
+ ;; Insert un-referenced definitions at the end.
+ (should
+ (equal
+ "Text[fn:9]
+
+\[fn:9] B
+
+\[fn:1] A
+"
+ (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
+ (let ((org-footnote-section nil)) (org-footnote-sort))
(buffer-string)))))
(ert-deftest test-org-footnote/renumber-fn:N ()
@@ -541,6 +553,13 @@ Text[fn:1][fn:4]
(let ((org-footnote-section nil)
(org-footnote-fill-after-inline-note-extraction t))
(org-footnote-normalize))
+ (buffer-string))))
+ ;; Insert un-referenced definitions at the end.
+ (should
+ (equal
+ "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
+ (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
+ (let ((org-footnote-section nil)) (org-footnote-normalize))
(buffer-string)))))