I'm trying to fill programmatically an Org buffer (especially the elements quote-block
, paragraph
and item
). Note that all following attempts contains a (org-cycle '(64))
because they all don't work when headings are not fully expanded (not sure why).
First attempt
(defun test/org-fill-whole-buffer-1 ()
(interactive)
(org-cycle '(64))
(mark-whole-buffer)
(org-fill-paragraph nil (list (point-min) (point-max))))
It works partially:
- items right above a quote block are not filled;
- items after multiple line breaks are not filled.
And per the documentation, mark-whole-buffer
should not be called programmatically.
Second attempt
(defun test/org-fill-whole-buffer-2 ()
(interactive)
(org-cycle '(64))
(save-excursion
(org-element-map (org-element-parse-buffer 'element)
'paragraph
(lambda (elt)
(when-let* ((target-point (org-element-property :contents-begin elt)))
(goto-char target-point)
(org-fill-paragraph nil t))))))
This obviously doesn't work (or works randomly) because once the first element is filled, the position of the subsequent ones is not correct anymore.
Third attempt
Improved version of test/org-fill-whole-buffer-2
: fill elements in reverse order of their appearances. Now the elements positions are correct as they are not altered by previous org-fill-paragraph
calls.
(defun test/org-fill-whole-buffer-3 (types)
"Fill Org elements of TYPES in the whole Org buffer."
(interactive
(list 'paragraph))
(org-cycle '(64))
(save-excursion
(mapc (lambda (n)
(goto-char n)
(org-fill-paragraph nil t))
(reverse
(org-element-map (org-element-parse-buffer 'element)
types
(lambda (elt)
(when-let* ((target-point (org-element-property :contents-begin elt)))
target-point)))))))
However, there are still some issues I can't really understand.
For instance, assuming that all nodes are folded, and that I call
(test/org-fill-whole-buffer-3 'paragraph)
, I obtain this: But then if IM-x undo-tree-undo
, fold headings, and call again(test/org-fill-whole-buffer-3 'paragraph)
, I obtain this:The function seems to be also "point-dependent", meaning that if I
goto-char
onto a problematic item (see point 1) before calling(test/org-fill-whole-buffer-3 'paragraph)
, then that item would fill properly at the first time as the second picture showed (but not other problematic items).Interestingly,
test/org-fill-whole-buffer-1
doesn't have the issue of point 1, but have issue with some items. So, one may think that calling(test/org-fill-whole-buffer-1)
and(test/org-fill-whole-buffer-3 'item)
successively would work as they complete each other. Nope, that doesn't work for a mysterious reason.
For these reasons, I had trouble to make a minimal reproducible example to illustrate issues I have with test/org-fill-whole-buffer-3
. But the essence of this question is still "how to fill a whole Org buffer programmatically, included Org elements quote-block
, paragraph
and item
?", so any brand-new solution is welcomed.
EDIT:
Here are pastebin links of my real world Org buffer. Special attention should be made on the two items containing org-dict-tlfi
links (see the pictures above). The expected result is as shown in the second picture, which could be achieved interactively with M-q
(org-fill-paragraph
) at the beginning of the unfilled item.
- gist.github.com
- pastebin.ubuntu.com (requires an account to download)
Org mode version: 9.4.4
C-e
then only gets me to after the first paren after "... ses ergots". I think something in that item confuses the parser. Can you isolate the problems you have found and cut-and-paste the items/paragraphs into a separate file? I'd like to try them all and see if there is any commonality.