I'm trying to fill programmatically an Org buffer (especially the elements
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).
(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.
(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.
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
(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 I
M-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-charonto 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).
test/org-fill-whole-buffer-1doesn't have the issue of point 1, but have issue with some items. So, one may think that calling
(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
item ?", so any brand-new solution is welcomed.
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
org-fill-paragraph) at the beginning of the unfilled item.
Org mode version: 9.4.4