From 0a51c630761ae18a7b57fd346ff08f869a041a6b Mon Sep 17 00:00:00 2001 From: Chris Barrett Date: Wed, 5 Oct 2022 22:46:04 +1300 Subject: [PATCH] Adapt `delete-blank-lines` to fix corner cases in blank line handling --- lisp/org-format.el | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lisp/org-format.el b/lisp/org-format.el index c319880..49b08c7 100644 --- a/lisp/org-format.el +++ b/lisp/org-format.el @@ -73,6 +73,19 @@ Only applies to level-1 headings in the document." (let ((tags (org-get-tags))) (seq-contains-p tags org-archive-tag)))) +(defun org-format--delete-blank-lines () + "Modified version of `delete-blank-lines'." + (beginning-of-line) + (when (looking-at "[ \t]*$") + (delete-region (point) + (if (re-search-backward "[^ \t\n]" nil t) + (progn (forward-line 1) (point)) + (point-min)))) + ;; Handle the special case where point is followed by newline and eob. + ;; Delete the line, leaving point at eob. + (when (looking-at "^[ \t]*\n\\'") + (delete-region (point) (point-max)))) + (defun org-format-all-headings () "Ensure that blank lines exist between headings and their contents." (interactive) @@ -100,7 +113,7 @@ Only applies to level-1 headings in the document." (unless (and (fboundp 'org-transclusion-within-transclusion-p) (org-transclusion-within-transclusion-p)) (forward-line 1) - (delete-blank-lines) + (org-format--delete-blank-lines) (org-format--ensure-empty-lines org-format-blank-lines-before-meta) (org-end-of-meta-data t) (org-format--ensure-empty-lines org-format-blank-lines-before-content))) @@ -110,7 +123,7 @@ Only applies to level-1 headings in the document." (org-with-wide-buffer ;; Clean up trailing whitespace. (goto-char (point-max)) - (delete-blank-lines) + (org-format--delete-blank-lines) ;; Format transcluded headings as if they were really there. (goto-char (point-min))