浏览代码

org-footnote: fix bug when inserting a footnote in a non-Org buffer

* lisp/org-footnote.el (org-footnote-create-definition): when skipping
  already written footnotes definition, the algorithme would assume
  each one was only one-line long.
Nicolas Goaziou 13 年之前
父节点
当前提交
3b5132bf8f
共有 1 个文件被更改,包括 23 次插入14 次删除
  1. 23 14
      lisp/org-footnote.el

+ 23 - 14
lisp/org-footnote.el

@@ -479,21 +479,30 @@ or new, let the user edit the definition of the footnote."
       (org-footnote-goto-local-insertion-point)
       (org-show-context 'link-search))
      (t
-      (let ((re (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$")))
-	(unless (re-search-forward re nil t)
-	  (let ((max (if (and (derived-mode-p 'message-mode)
-			      (re-search-forward message-signature-separator nil t))
-			 (progn (beginning-of-line) (point))
-		       (goto-char (point-max)))))
-	    (skip-chars-backward " \t\r\n")
-	    (delete-region (point) max)
-	    (insert "\n\n")
-	    (insert org-footnote-tag-for-non-org-mode-files "\n"))))
-	;; Skip existing footnotes
-      (while (re-search-forward "^[[:space:]]*\\[[^]]+\\] " nil t)
-	(forward-line))))
+      ;; In a non-Org file.  Search for footnote tag, or create it if
+      ;; necessary (at the end of buffer, or before a signature if in
+      ;; Message mode).  Set point after any definition already there.
+      (let ((tag (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$"))
+	    (max (save-excursion
+		   (if (and (derived-mode-p 'message-mode)
+			    (re-search-forward
+			     message-signature-separator nil t))
+		       (copy-marker (point-at-bol) t)
+		     (copy-marker (point-max) t)))))
+	(goto-char max)
+	(unless (re-search-backward tag nil t)
+	  (skip-chars-backward " \t\r\n")
+	  (delete-region (point) max)
+	  (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n"))
+	;; Skip existing footnotes.
+	(while (re-search-forward org-footnote-definition-re max t))
+	(let ((def (org-footnote-at-definition-p)))
+	  (when def (goto-char (nth 2 def))))
+	(set-marker max nil))))
+    ;; Insert footnote label, position point and notify user.
+    (unless (bolp) (insert "\n"))
     (insert "\n[" label "] \n")
-    (goto-char (1- (point)))
+    (backward-char)
     (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
 
 ;;;###autoload