This is the org-fill-buffer command, done generically for people who want to fill or unfill the entire buffer, as is required when alternating between hard newline filling and visual line mode filling.
See attached patch for docstring and commit message.
From 706d5d71cdf1ed2528664bdaf714aad6bd15af6c Mon Sep 17 00:00:00 2001 From: Psionik K <73710933+psioni...@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:15:26 +0900 Subject: [PATCH] org.el: introducing org-fill-buffer * lisp/org.el: (org-fill-buffer) this command walks the tree and will call fill-paragraph on every paragraph or plain-list element, enabling the user to quickly cycle between hard newlines or visual-line-mode. They can also adjust the fill, such as after removing indentation. --- lisp/org.el | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lisp/org.el b/lisp/org.el index 57379c26..1becc394 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -21901,6 +21901,38 @@ modified." (org-do-remove-indentation)))))))) (funcall unindent-tree (org-element-contents parse-tree)))) +(defun org-fill-buffer () + "Fill all paragraph and plain-list elements. +This command can be used to add hard newlines or to remove them. +To add hard newlines for text filling, set `fill-column' to the +desired with. To remove hard newlines, such as to prepare the +contents for `visual-line-mode', set `fill-column' to +`most-positive-fixnum'." + (interactive) + (unless (and (eq major-mode 'org-mode)) + (user-error "Cannot fill a buffer not in Org mode")) + (letrec ((parse-tree (org-element-parse-buffer 'greater-element nil 'defer)) + (fill-tree + (lambda (contents) + (dolist (element (reverse contents)) + (let ((type (org-element-type element))) + (if (member type '(headline section)) + (funcall fill-tree (org-element-contents element)) + (save-excursion + (save-restriction + (narrow-to-region + (org-element-begin element) + (org-element-end element)) + (goto-char (point-min)) + (pcase type + (`paragraph (fill-paragraph)) + (`plain-list + (mapc (lambda (i) + (goto-char (car i)) + (fill-paragraph)) + (reverse (org-list-struct))))))))))))) + (funcall fill-tree (org-element-contents parse-tree)))) + (defun org-make-options-regexp (kwds &optional extra) "Make a regular expression for keyword lines. KWDS is a list of keywords, as strings. Optional argument EXTRA, -- 2.42.0