branch: externals/org
commit c9060ec7c43f846897abe288cc97aa11544e5390
Author: Ihor Radchenko <yanta...@posteo.net>
Commit: Ihor Radchenko <yanta...@posteo.net>

    org-element: Record blank lines at the beginning of document in org-data
    
    * lisp/org-element.el (org-element-org-data-parser): Record blank
    lines at the beginning in :pre-blank property.
    (org-element-org-data-interpreter): Honor :pre-blank.
    (org-element-interpret-data): Use ~org-element-org-data-interpreter~.
    * testing/lisp/test-org-element.el (test-org-element/org-data-interpreter):
    Add new test.
    * etc/ORG-NEWS (~org-element-org-data-parser~ now returns syntax node
    with ~:pre-blank~ property): Announce the change.
    
    Reported-by: chris <inkbottle...@gmail.com>
    Link: https://orgmode.org/list/5322763.GXAFRqVoOG@nixos
---
 etc/ORG-NEWS                     |  8 ++++++++
 lisp/org-element.el              | 16 +++++++---------
 testing/lisp/test-org-element.el | 11 +++++++++++
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 982bac4e93..980838392e 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -452,6 +452,14 @@ capture ~:tree-type~ options]], the internal variable
 undocumented helper function ~org-datetree-insert-line~.
 
 ** Miscellaneous
+*** ~org-element-org-data-parser~ now returns syntax node with ~:pre-blank~ 
property
+
+Previously, parsing ~org-data~ syntax node did not record information
+about blank lines at the beginning of the document.  Now, the number
+of blank lines is recorded in ~:pre-blank~ property.
+
+~org-element-org-data-interpreter~ takes into account this information.
+
 *** ~yank-media~ and DND handlers now honor the user option 
~org-file-link-type~
 
 When inserting file: links, ~yank-media~ and DND handlers now respect
diff --git a/lisp/org-element.el b/lisp/org-element.el
index dd7ee6b24a..56c03a0aa7 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -1583,8 +1583,8 @@ Alter DATA by side effect."
   "Parse org-data.
 
 Return a new syntax node of `org-data' type containing `:begin',
-`:contents-begin', `:contents-end', `:end', `:post-blank',
-`:post-affiliated', and `:path' properties."
+`:pre-blank', `:contents-begin', `:contents-end', `:end',
+`:post-blank', `:post-affiliated', and `:path' properties."
   (org-with-wide-buffer
    (let* ((begin 1)
           (contents-begin (progn
@@ -1615,6 +1615,7 @@ Return a new syntax node of `org-data' type containing 
`:begin',
             :end end
             :robust-begin robust-begin
             :robust-end robust-end
+            :pre-blank (count-lines begin contents-begin)
             ;; Trailing blank lines in org-data, headlines, and
             ;; sections belong to the containing elements.
             :post-blank 0
@@ -1624,10 +1625,12 @@ Return a new syntax node of `org-data' type containing 
`:begin',
             :buffer (current-buffer)
             :deferred org-element--get-global-node-properties)))))
 
-(defun org-element-org-data-interpreter (_ contents)
+(defun org-element-org-data-interpreter (org-data contents)
   "Interpret ORG-DATA element as Org syntax.
 CONTENTS is the contents of the element."
-  contents)
+  (concat
+   (make-string (or (org-element-property :pre-blank org-data) 0) ?\n)
+   contents))
 
 ;;;; Inlinetask
 
@@ -5477,11 +5480,6 @@ to interpret.  Return Org syntax as a string."
                        (mapconcat (lambda (obj) (funcall fun obj parent))
                                   data
                                   ""))
-                      ;; Full Org document.
-                      ((eq type 'org-data)
-                       (mapconcat (lambda (obj) (funcall fun obj parent))
-                                  (org-element-contents data)
-                                  ""))
                       ;; Plain text: return it.
                       ((stringp data) data)
                       ;; Element or object without contents.
diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el
index e36057f5b4..a491892e43 100644
--- a/testing/lisp/test-org-element.el
+++ b/testing/lisp/test-org-element.el
@@ -3501,6 +3501,17 @@ Outside list"
          (org-element-interpret-data
            '(section (:post-blank 1) (paragraph (:post-blank 1) "Foo"))))))
 
+(ert-deftest test-org-element/org-data-interpreter ()
+  "Test `org-element-org-data-interpreter'."
+  (should
+   (equal (org-test-parse-and-interpret "Test.\n")
+         "Test.\n"))
+  (should
+   (equal (org-test-parse-and-interpret "
+
+Test.\n")
+         "\n\nTest.\n")))
+
 (ert-deftest test-org-element/center-block-interpreter ()
   "Test center block interpreter."
   (should

Reply via email to