Browse Source

org-element: Forbid affiliated keywords for comments

* lisp/org-element.el (org-element--current-element): Forbid
affiliated keywords for comments.
* testing/lisp/test-org-element.el (test-org-element/comment-parser):
Tiny refactoring.

The motivation is to avoid distinguishing between top-level comments,
which cannot have affiliated keywords, and other comments, which
could.  Therefore, we keep a single type of comment.
Nicolas Goaziou 5 years ago
parent
commit
ee2a8eb241
2 changed files with 37 additions and 50 deletions
  1. 17 26
      lisp/org-element.el
  2. 20 24
      testing/lisp/test-org-element.el

+ 17 - 26
lisp/org-element.el

@@ -1806,13 +1806,10 @@ Return a list whose CAR is `clock' and CDR is a plist containing
 
 ;;;; Comment
 
-(defun org-element-comment-parser (limit affiliated)
+(defun org-element-comment-parser (limit)
   "Parse a comment.
 
-LIMIT bounds the search.  AFFILIATED is a list of which CAR is
-the buffer position at the beginning of the first affiliated
-keyword and CDR is a plist of affiliated keywords along with
-their value.
+LIMIT bounds the search.
 
 Return a list whose CAR is `comment' and CDR is a plist
 containing `:begin', `:end', `:value', `:post-blank',
@@ -1820,8 +1817,7 @@ containing `:begin', `:end', `:value', `:post-blank',
 
 Assume point is at comment beginning."
   (save-excursion
-    (let* ((begin (or (car affiliated) (point)))
-	   (post-affiliated (point))
+    (let* ((begin (point))
 	   (value (prog2 (looking-at "[ \t]*# ?")
 		      (buffer-substring-no-properties
 		       (match-end 0) (line-end-position))
@@ -1843,13 +1839,11 @@ Assume point is at comment beginning."
 		       (skip-chars-forward " \r\t\n" limit)
 		       (if (eobp) (point) (line-beginning-position)))))
       (list 'comment
-	    (nconc
-	     (list :begin begin
-		   :end end
-		   :value value
-		   :post-blank (count-lines com-end end)
-		   :post-affiliated post-affiliated)
-	     (cdr affiliated))))))
+	    (list :begin begin
+		  :end end
+		  :value value
+		  :post-blank (count-lines com-end end)
+		  :post-affiliated begin)))))
 
 (defun org-element-comment-interpreter (comment _)
   "Interpret COMMENT element as Org syntax.
@@ -3874,9 +3868,9 @@ element it has to parse."
 	(org-element-section-parser
 	 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
 	     limit)))
-       ;; Top-level comments.  Those cannot have affiliated keywords.
-       ((and (eq mode 'top-comment) (looking-at "#\\(?: \\|$\\)"))
-	(org-element-comment-parser limit nil))
+       ;; Comments.
+       ((looking-at "^[ \t]*#\\(?: \\|$\\)")
+	(org-element-comment-parser limit))
        ;; Planning.
        ((and (eq mode 'planning)
 	     (eq ?* (char-after (line-beginning-position 0)))
@@ -3898,7 +3892,7 @@ element it has to parse."
        ;; Clock.
        ((looking-at org-clock-line-re) (org-element-clock-parser limit))
        ;; Inlinetask.
-       ((org-at-heading-p)
+       ((looking-at "^\\*+ ")
 	(org-element-inlinetask-parser limit raw-secondary-p))
        ;; From there, elements can have affiliated keywords.
        (t (let ((affiliated (org-element--collect-affiliated-keywords
@@ -3920,13 +3914,10 @@ element it has to parse."
 	      (org-element-fixed-width-parser limit affiliated))
 	     ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
 	     ;; Keywords.
-	     ((looking-at "[ \t]*#")
+	     ((looking-at "[ \t]*#\\+")
 	      (goto-char (match-end 0))
 	      (cond
-	       ((looking-at "\\(?: \\|$\\)")
-		(beginning-of-line)
-		(org-element-comment-parser limit affiliated))
-	       ((looking-at "\\+BEGIN_\\(\\S-+\\)")
+	       ((looking-at "BEGIN_\\(\\S-+\\)")
 		(beginning-of-line)
 		(funcall (pcase (upcase (match-string 1))
 			   ("CENTER"  #'org-element-center-block-parser)
@@ -3939,13 +3930,13 @@ element it has to parse."
 			   (_         #'org-element-special-block-parser))
 			 limit
 			 affiliated))
-	       ((looking-at "\\+CALL:")
+	       ((looking-at "CALL:")
 		(beginning-of-line)
 		(org-element-babel-call-parser limit affiliated))
-	       ((looking-at "\\+BEGIN:? ")
+	       ((looking-at "BEGIN:? ")
 		(beginning-of-line)
 		(org-element-dynamic-block-parser limit affiliated))
-	       ((looking-at "\\+\\S-+:")
+	       ((looking-at "\\S-+:")
 		(beginning-of-line)
 		(org-element-keyword-parser limit affiliated))
 	       (t

+ 20 - 24
testing/lisp/test-org-element.el

@@ -568,42 +568,38 @@ Some other text
 (ert-deftest test-org-element/comment-parser ()
   "Test `comment' parser."
   ;; Regular comment.
-  (should
-   (org-test-with-temp-text "# Comment"
-     (org-element-map (org-element-parse-buffer) 'comment 'identity)))
+  (should (eq 'comment
+	      (org-test-with-temp-text "# Comment"
+		(org-element-type (org-element-at-point)))))
   ;; Inline comment.
-  (should
-   (org-test-with-temp-text "  # Comment"
-     (org-element-map (org-element-parse-buffer) 'comment 'identity)))
+  (should (eq 'comment
+	      (org-test-with-temp-text "  # Comment"
+		(org-element-type (org-element-at-point)))))
   ;; Preserve indentation.
   (should
-   (equal
-    (org-element-property
-     :value
-     (org-test-with-temp-text "# No blank\n#  One blank"
-       (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
-    "No blank\n One blank"))
+   (equal "No blank\n One blank"
+	  (org-element-property
+	   :value
+	   (org-test-with-temp-text "# No blank\n#  One blank"
+	     (org-element-at-point)))))
   ;; Comment with blank lines.
   (should
-   (equal
-    (org-element-property
-     :value
-     (org-test-with-temp-text "# First part\n# \n#\n# Second part"
-       (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
-    "First part\n\n\nSecond part"))
+   (equal "First part\n\n\nSecond part"
+	  (org-element-property
+	   :value
+	   (org-test-with-temp-text "# First part\n# \n#\n# Second part"
+	     (org-element-at-point)))))
   ;; Do not mix comments and keywords.
   (should
    (eq 1
        (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
 	 (length (org-element-map (org-element-parse-buffer) 'comment
-		   'identity)))))
+		   #'identity)))))
   (should
    (equal "comment"
-	  (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
-	    (org-element-property
-	     :value
-	     (org-element-map (org-element-parse-buffer) 'comment
-	       'identity nil t)))))
+	  (org-test-with-temp-text
+	      "#+key: value\n<point># comment\n#+key: value"
+	    (org-element-property :value (org-element-at-point)))))
   ;; Correctly handle non-empty blank lines at the end of buffer.
   (should
    (org-test-with-temp-text "# A\n "