From ae0d9fbb0976613ccd6fb9781d77ab1fde026c99 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sat, 27 Feb 2016 16:05:35 +0100 Subject: [PATCH] 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. --- lisp/org-footnote.el | 19 +++++++++++++++++-- testing/lisp/test-org-footnote.el | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lisp/org-footnote.el b/lisp/org-footnote.el index 58f2063a5..b090a4544 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 e08c7fe98..253f4ee54 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))))) -- 2.11.4.GIT