Explorar el Código

org-element: Fix footnote definition parsing

* lisp/org-element.el (org-element--footnote-separator): New variable.
(org-element-footnote-definition-parser): Handle footnotes with
affiliated keywords.

* testing/lisp/test-org-element.el (test-org-element/footnote-definition-parser):
Add test.
Nicolas Goaziou hace 9 años
padre
commit
7d0104f449
Se han modificado 2 ficheros con 41 adiciones y 16 borrados
  1. 29 15
      lisp/org-element.el
  2. 12 1
      testing/lisp/test-org-element.el

+ 29 - 15
lisp/org-element.el

@@ -796,6 +796,12 @@ CONTENTS is the contents of the element."
 
 ;;;; Footnote Definition
 
+(defconst org-element--footnote-separator
+  (concat org-outline-regexp-bol "\\|"
+	  org-footnote-definition-re "\\|"
+	  "^\\([ \t]*\n\\)\\{2,\\}")
+  "Regexp used as a footnote definition separator.")
+
 (defun org-element-footnote-definition-parser (limit affiliated)
   "Parse a footnote definition.
 
@@ -814,21 +820,29 @@ Assume point is at the beginning of the footnote definition."
 			 (org-match-string-no-properties 1)))
 	   (begin (car affiliated))
 	   (post-affiliated (point))
-	   (ending (save-excursion
-		     (if (progn
-			   (end-of-line)
-			   (re-search-forward
-			    (concat org-outline-regexp-bol "\\|"
-				    org-footnote-definition-re "\\|"
-				    "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
-			 (match-beginning 0)
-		       (point))))
-	   (contents-begin (progn
-			     (search-forward "]")
-			     (skip-chars-forward " \r\t\n" ending)
-			     (cond ((= (point) ending) nil)
-				   ((= (line-beginning-position) begin) (point))
-				   (t (line-beginning-position)))))
+	   (ending
+	    (save-excursion
+	      (end-of-line)
+	      (cond
+	       ((not
+		 (re-search-forward org-element--footnote-separator limit t))
+		limit)
+	       ((eq (char-after (match-beginning 0)) ?\[)
+		;; At a new footnote definition, make sure we end
+		;; before any affiliated keyword above.
+		(forward-line -1)
+		(while (and (> (point) post-affiliated)
+			    (org-looking-at-p org-element--affiliated-re))
+		  (forward-line -1))
+		(line-beginning-position 2))
+	       (t (match-beginning 0)))))
+	   (contents-begin
+	    (progn
+	      (search-forward "]")
+	      (skip-chars-forward " \r\t\n" ending)
+	      (cond ((= (point) ending) nil)
+		    ((= (line-beginning-position) post-affiliated) (point))
+		    (t (line-beginning-position)))))
 	   (contents-end (and contents-begin ending))
 	   (end (progn (goto-char ending)
 		       (skip-chars-forward " \r\t\n" limit)

+ 12 - 1
testing/lisp/test-org-element.el

@@ -930,7 +930,18 @@ Some other text
   ;; Handle non-empty blank line at the end of buffer.
   (should
    (org-test-with-temp-text "[fn:1] Definition\n "
-     (= (org-element-property :end (org-element-at-point)) (point-max)))))
+     (= (org-element-property :end (org-element-at-point)) (point-max))))
+  ;; Footnote with attributes.
+  (should
+   (= 1
+      (org-test-with-temp-text "#+attr_latex: :offset 0in\n[fn:1] A footnote."
+	(length
+	 (org-element-map (org-element-parse-buffer) 'footnote-definition
+	   #'identity)))))
+  (should
+   (org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2"
+     (goto-char (org-element-property :end (org-element-at-point)))
+     (looking-at "#"))))
 
 
 ;;;; Footnotes Reference.