emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Psionic K <psionik@positron.solutions>
To: Psionic K <psionik@positron.solutions>
Cc: Ihor Radchenko <yantar92@posteo.net>, emacs-orgmode@gnu.org
Subject: Re: org-(un)fill-buffer
Date: Wed, 10 Jan 2024 23:26:24 +0900	[thread overview]
Message-ID: <CADQMGASXJcFB7t+7oK+fuAS_C3bFeerikObyU9D5vh9vTYn8Ww@mail.gmail.com> (raw)
In-Reply-To: <CADQMGARjd2i_BrHsb-kH_Bk2NR2T4n5ghjzYvVGr+1tuUm-9QQ@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 256 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 503 bytes --]

[-- Attachment #2: 0001-org.el-introducing-org-fill-buffer.patch --]
[-- Type: text/x-patch, Size: 2452 bytes --]

From 706d5d71cdf1ed2528664bdaf714aad6bd15af6c Mon Sep 17 00:00:00 2001
From: Psionik K <73710933+psionic-k@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


  reply	other threads:[~2024-01-10 14:27 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10  9:40 org-(un)fill-buffer Psionic K
2024-01-10 12:34 ` org-(un)fill-buffer Ihor Radchenko
2024-01-10 14:04   ` org-(un)fill-buffer Psionic K
2024-01-10 14:26     ` Psionic K [this message]
2024-01-10 15:50       ` org-(un)fill-buffer Ihor Radchenko
2024-01-10 16:22         ` org-(un)fill-buffer Psionic K
2024-01-10 16:36           ` fill-region-as-paragraph does not respect fill-paragraph-function (was: org-(un)fill-buffer) Ihor Radchenko
2024-01-11  1:12           ` org-(un)fill-buffer Samuel Wales
2024-01-11  1:48             ` org-(un)fill-buffer Psionic K

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADQMGASXJcFB7t+7oK+fuAS_C3bFeerikObyU9D5vh9vTYn8Ww@mail.gmail.com \
    --to=psionik@positron.solutions \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).