Ihor Radchenko <yanta...@posteo.net> writes:

> Max Nikulin <maniku...@gmail.com> writes:
>
>>>    [previous object <max(n1,n2) spaces>]
>>
>> Yes, you do.
>>
>> I expected some complications due to newline characters (not line break 
>> markup objects), but they are not included in :post-blank and 
>> represented as "\n" string objects.
>
> Newlines are tricky. They may or may not be significant.
> For example, in CJK paragraphs, newlines are to be stripped.
>
> I think that a reasonable thing to do could be not adding newlines if
> the previous object is a plain string ending with a newline.

I think that using max(n1,n2) is an overkill. There is no reason to
alter existing spaces before, if any.

I am attaching tentative patch that simply keeps spaces, but if and only
if the previous object does not end with whitespace (including newline).

>From 656c32d075d939aa69bc315bc91515930680377c Mon Sep 17 00:00:00 2001
Message-Id: <656c32d075d939aa69bc315bc91515930680377c.1679926405.git.yanta...@posteo.net>
From: Ihor Radchenko <yanta...@posteo.net>
Date: Mon, 27 Mar 2023 16:11:32 +0200
Subject: [PATCH] org-export--prune-tree: Ensure spaces when removing objects

* lisp/ox.el (org-export--prune-tree): If the removed object has
trailing spaces and previous object does not have, keep the trailing
spaces.
* etc/ORG-NEWS (Blank lines after removed objects are not retained
during export): Document the change.

Reported-by: Andrea Lazzarini <andrea.lazzari...@gmail.com>
Link: https://orgmode.org/list/87o7p7z9k3.fsf@localhost
---
 etc/ORG-NEWS | 21 +++++++++++++++++++++
 lisp/ox.el   | 19 ++++++++++++++++++-
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index ac233a986..caf140279 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -145,6 +145,27 @@ execution completes.  The new ~:async~ header allows users to continue
 editing with Emacs while a ~:session~ block executes.
 
 ** Miscellaneous
+*** Blank lines after removed objects are not retained during export
+
+When certain objects in Org document are to be excluded from export,
+spaces after these objects were previously removed as well.
+
+For example, if ~org-export-with-footnotes~ is set to nil, the footnote in 
+
+: Pellentesque dapibus suscipit ligula.[fn:1]  Donec posuere augue in quam.
+
+would be removed, leading to the following exported ASCII document
+
+: Pellentesque dapibus suscipit ligula.Donec posuere augue in quam.
+
+This is because spaces after footnote (and other markup) are
+considered a part of the preceding AST object in Org.
+
+Now, unless there is a whitespace before an object to be removed,
+spaces are preserved during export:
+
+: Pellentesque dapibus suscipit ligula.  Donec posuere augue in quam.
+
 *** Remove undocumented ~:target~ header parameter in ~ob-clojure~
 
 The ~:target~ header was only used internally to distinguish
diff --git a/lisp/ox.el b/lisp/ox.el
index f9fc9a99b..206f0536d 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -2752,7 +2752,24 @@ (defun org-export--prune-tree (data info)
 		(let ((type (org-element-type data)))
 		  (if (org-export--skip-p data info selected excluded)
 		      (if (memq type '(table-cell table-row)) (push data ignore)
-			(org-element-extract-element data))
+			(let ((post-blank (org-element-property :post-blank data)))
+			  (if (or (not post-blank) (zerop post-blank)
+				  (eq 'element (org-element-class data)))
+			      (org-element-extract-element data)
+			    ;; Keep spaces in place of removed
+			    ;; element, if necessary.
+			    ;; Example: "Foo.[10%] Bar" would become
+			    ;; "Foo.Bar" if we do not keep spaces.
+			    (let ((previous (org-export-get-previous-element data info)))
+			      (if (or (not previous)
+				      (pcase (org-element-type previous)
+					(`plain-text
+					 (string-match-p
+					  (rx  whitespace eos) previous))
+					(_ (org-element-property :post-blank previous))))
+				  ;; Previous object ends with whitespace already.
+				  (org-element-extract-element data)
+				(org-element-set-element data (make-string post-blank ?\s)))))))
 		    (if (and (eq type 'headline)
 			     (eq (plist-get info :with-archived-trees)
 				 'headline)
-- 
2.39.1

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to