From 656c32d075d939aa69bc315bc91515930680377c Mon Sep 17 00:00:00 2001 Message-Id: <656c32d075d939aa69bc315bc91515930680377c.1679926405.git.yantar92@posteo.net> From: Ihor Radchenko 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 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