From 1dc6c4bc9ce21be6d5c1a7d623cd917e5a1224e6 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 9 Nov 2016 23:25:53 +0100 Subject: [PATCH 1/2] org-element: Fix inlinetask parsing * lisp/org-element.el (org-element-headline-parser): Small refactoring. (org-element-inlinetask-parser): Add missing :pre-blank property. Small refactoring. Also fix regexp for task end. * testing/lisp/test-org-element.el (test-org-element/inlinetask-parser): Add test. --- lisp/org-element.el | 46 ++++++++++++++++++-------------- testing/lisp/test-org-element.el | 5 ++++ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 027eea448..51b1ca443 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1018,15 +1018,14 @@ Assume point is at beginning of the headline." (standard-props (org-element--get-node-properties)) (time-props (org-element--get-time-properties)) (end (min (save-excursion (org-end-of-subtree t t)) limit)) - (pos-after-head (progn (forward-line) (point))) (contents-begin (save-excursion + (forward-line) (skip-chars-forward " \r\t\n" end) (and (/= (point) end) (line-beginning-position)))) (contents-end (and contents-begin (progn (goto-char end) (skip-chars-backward " \r\t\n") - (forward-line) - (point))))) + (line-beginning-position 2))))) (let ((headline (list 'headline (nconc @@ -1035,7 +1034,7 @@ Assume point is at beginning of the headline." :end end :pre-blank (if (not contents-begin) 0 - (count-lines pos-after-head contents-begin)) + (1- (count-lines begin contents-begin))) :contents-begin contents-begin :contents-end contents-end :level level @@ -1043,9 +1042,10 @@ Assume point is at beginning of the headline." :tags tags :todo-keyword todo :todo-type todo-type - :post-blank (count-lines - (or contents-end pos-after-head) - end) + :post-blank + (if contents-end + (count-lines contents-end end) + (1- (count-lines begin end))) :footnote-section-p footnote-section-p :archivedp archivedp :commentedp commentedp @@ -1116,10 +1116,11 @@ CONTENTS is the contents of the element." "Parse an inline task. Return a list whose CAR is `inlinetask' and CDR is a plist -containing `:title', `:begin', `:end', `:contents-begin' and -`:contents-end', `:level', `:priority', `:raw-value', `:tags', -`:todo-keyword', `:todo-type', `:scheduled', `:deadline', -`:closed', `:post-blank' and `:post-affiliated' keywords. +containing `:title', `:begin', `:end', `:pre-blank', +`:contents-begin' and `:contents-end', `:level', `:priority', +`:raw-value', `:tags', `:todo-keyword', `:todo-type', +`:scheduled', `:deadline', `:closed', `:post-blank' and +`:post-affiliated' keywords. The plist also contains any property set in the property drawer, with its name in upper cases and colons added at the @@ -1157,18 +1158,20 @@ Assume point is at beginning of the inline task." (task-end (save-excursion (end-of-line) (and (re-search-forward org-outline-regexp-bol limit t) - (looking-at-p "END[ \t]*$") + (looking-at-p "[ \t]*END[ \t]*$") (line-beginning-position)))) (standard-props (and task-end (org-element--get-node-properties))) (time-props (and task-end (org-element--get-time-properties))) - (contents-begin (progn (forward-line) - (and task-end (< (point) task-end) (point)))) + (contents-begin (and task-end + (< (point) task-end) + (progn + (forward-line) + (skip-chars-forward " \t\n") + (line-beginning-position)))) (contents-end (and contents-begin task-end)) - (before-blank (if (not task-end) (point) - (goto-char task-end) - (forward-line) - (point))) - (end (progn (skip-chars-forward " \r\t\n" limit) + (end (progn (when task-end (goto-char task-end)) + (forward-line) + (skip-chars-forward " \r\t\n" limit) (if (eobp) (point) (line-beginning-position)))) (inlinetask (list 'inlinetask @@ -1176,6 +1179,9 @@ Assume point is at beginning of the inline task." (list :raw-value raw-value :begin begin :end end + :pre-blank + (if (not contents-begin) 0 + (1- (count-lines begin contents-begin))) :contents-begin contents-begin :contents-end contents-end :level level @@ -1183,7 +1189,7 @@ Assume point is at beginning of the inline task." :tags tags :todo-keyword todo :todo-type todo-type - :post-blank (count-lines before-blank end) + :post-blank (1- (count-lines (or task-end begin) end)) :post-affiliated begin) time-props standard-props)))) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index c348cc99c..c164de7b8 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -1269,6 +1269,11 @@ Some other text (org-test-with-temp-text "*************** Task\nTest\n*************** END" (org-element-type (org-element-at-point))))) + (should + (eq 'inlinetask + (org-test-with-temp-text + "*************** Task\nTest\n*************** END" + (org-element-type (org-element-at-point))))) ;; Degenerate inlinetask. (should (eq 'inlinetask From 9f98979ff0cf23c7e3115f5205e601351d339d10 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Thu, 10 Nov 2016 00:25:34 +0100 Subject: [PATCH 2/2] org-element: Prevent parsing meta-data after blank lines * lisp/org-element.el (org-element--current-element): Check that planning info and property drawers are correctly located before parsing them. * testing/lisp/test-org-element.el (test-org-element/planning-parser): (test-org-element/property-drawer-parser): Add tests. --- lisp/org-element.el | 6 +++++- testing/lisp/test-org-element.el | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 51b1ca443..b86244eb6 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -3845,10 +3845,14 @@ element it has to parse." (or (save-excursion (org-with-limited-levels (outline-next-heading))) limit))) ;; Planning. - ((and (eq mode 'planning) (looking-at org-planning-line-re)) + ((and (eq mode 'planning) + (eq ?* (char-after (line-beginning-position 0))) + (looking-at org-planning-line-re)) (org-element-planning-parser limit)) ;; Property drawer. ((and (memq mode '(planning property-drawer)) + (eq ?* (char-after (line-beginning-position + (if (eq mode 'planning) 0 -1)))) (looking-at org-property-drawer-re)) (org-element-property-drawer-parser limit)) ;; When not at bol, point is at the beginning of an item or diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index c164de7b8..2a507f8ea 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -1971,6 +1971,11 @@ Outside list" (eq 'planning (org-test-with-temp-text "* H\n# Comment\nDEADLINE: <2012-03-29 thu.>" + (org-element-type (org-element-at-point))))) + (should-not + (eq 'planning + (org-test-with-temp-text + "* H\n\nDEADLINE: <2012-03-29 thu.>" (org-element-type (org-element-at-point)))))) @@ -2013,6 +2018,11 @@ Outside list" (org-test-with-temp-text "* H\nParagraph\n:PROPERTIES:\nparagraph\n:END:" (org-element-type (org-element-at-point))))) + (should-not + (eq 'property-drawer + (org-test-with-temp-text + "* H\n\n:PROPERTIES:\n:prop: value\n:END:" + (org-element-type (org-element-at-point))))) ;; Handle non-empty blank line at the end of buffer. (should (org-test-with-temp-text "* H\n:PROPERTIES:\n:END:\n "