Browse Source

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.
Nicolas Goaziou 4 years ago
parent
commit
ae0d9fbb09
2 changed files with 36 additions and 2 deletions
  1. 17 2
      lisp/org-footnote.el
  2. 19 0
      testing/lisp/test-org-footnote.el

+ 17 - 2
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)))))
 

+ 19 - 0
testing/lisp/test-org-footnote.el

@@ -432,6 +432,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)))))
 
@@ -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)))))