فهرست منبع

org-footnote: Fix normalization when no footnote section is defined

* lisp/org-footnote.el (org-footnote-normalize): Ensure footnote
  definition will be inserted at the end of the section corresponding
  to to its first reference.
Nicolas Goaziou 14 سال پیش
والد
کامیت
b032a6d51b
1فایلهای تغییر یافته به همراه32 افزوده شده و 17 حذف شده
  1. 32 17
      lisp/org-footnote.el

+ 32 - 17
lisp/org-footnote.el

@@ -657,6 +657,7 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
       (goto-char (point-min))
       (while (setq ref (org-footnote-get-next-reference))
 	(let* ((lbl (car ref))
+	       (pos (nth 1 ref))
 	       ;; When footnote isn't anonymous, check if it's label
 	       ;; (REF) is already stored in REF-TABLE.  In that case,
 	       ;; extract number used to identify it (MARKER).  If
@@ -684,7 +685,8 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
 		 org-footnote-fill-after-inline-note-extraction
 		 (org-fill-paragraph)))
 	  ;; Add label (REF), identifier (MARKER), definition (DEF)
-	  ;; and type (INLINEP) to REF-TABLE if data was unknown.
+	  ;; type (INLINEP) and position (POS) to REF-TABLE if data
+	  ;; was unknown.
 	  (unless a
 	    (let ((def (or (nth 3 ref)	; inline
 			   (and export-props
@@ -702,27 +704,31 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
 				      '(:todo-keywords t :tags t :priority t))))
 				(org-export-preprocess-string def parameters))
 			    def)
-			  inlinep) ref-table)))))
+			  inlinep pos) ref-table)))))
       ;; 2. Find and remove the footnote section, if any.  Also
       ;;    determine where footnotes shall be inserted (INS-POINT).
-      (goto-char (point-min))
       (cond
-       ((and org-footnote-section
-	     (eq major-mode 'org-mode)
-	     (re-search-forward
+       ((and org-footnote-section (eq major-mode 'org-mode))
+	(goto-char (point-min))
+	(if (re-search-forward
 	      (concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
-		      "[ \t]*$")
-	      nil t))
-	(delete-region (match-beginning 0) (org-end-of-subtree t)))
-       ((eq major-mode 'org-mode)
+		      "[ \t]*$") nil t)
+	    (delete-region (match-beginning 0) (org-end-of-subtree t)))
+	;; A new footnote section is inserted by default at the end of
+	;; the buffer.
 	(goto-char (point-max))
 	(unless (bolp) (newline)))
+       ;; No footnote section set: Footnotes will be added before next
+       ;; headline.
+       ((eq major-mode 'org-mode)
+	(org-with-limited-levels (outline-next-heading)))
        (t
 	;; Remove any left-over tag in the buffer, if one is set up.
 	(when org-footnote-tag-for-non-org-mode-files
 	  (let ((tag (concat "^" (regexp-quote
 				  org-footnote-tag-for-non-org-mode-files)
 			     "[ \t]*$")))
+	    (goto-char (point-min))
 	    (while (re-search-forward tag nil t)
 	      (replace-match "")
 	      (delete-region (point) (progn (forward-line) (point))))))
@@ -769,8 +775,7 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
        ((not ref-table))
        ;; Cases when footnotes should be inserted in one place.
        ((or (not (eq major-mode 'org-mode))
-	    org-footnote-section
-	    (not sort-only))
+	    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,
@@ -795,14 +800,24 @@ Additional note on `org-footnote-insert-pos-for-preprocessor':
 	  (insert "* " org-footnote-section "\n")))
 	(set-marker ins-point nil)
 	;; Insert the footnotes, separated by a blank line.
-	(insert (mapconcat (lambda (x) (format "\n[%s] %s"
-					  (nth (if sort-only 0 1) x) (nth 2 x)))
-			   ref-table "\n"))
+	(insert
+	 (mapconcat
+	  (lambda (x)
+	    (format "\n[%s] %s" (nth (if sort-only 0 1) x) (nth 2 x)))
+	  ref-table "\n"))
 	(unless (eobp) (insert "\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)))
+	(when export-props (setq org-export-footnotes-seen ref-table)))
+       ;; Each footnote definition has to be inserted at the end of
+       ;; the section where its first reference belongs.
+       ((not sort-only)
+	(mapc
+	 (lambda (x)
+	   (goto-char (nth 4 x))
+	   (org-footnote-goto-local-insertion-point)
+	   (insert (format "\n[%s] %s\n" (nth 1 x) (nth 2 x))))
+	 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