Note, the problem with the original version of paragraph-fill
is that it calls fill-region
if the region is active. This merges all items into one filled item instead of several filled items.
There follows a command fill-region-paragraphs
that fills each paragraph with the non-interactive version of fill-paragraph
instead.
Thus the items in the region are filled separately.
In the code the command is bound to the key sequence C-c f. You can change this to your likings.
(defun fill-region-paragraphs (b e &optional justify)
"Fill region between b and e like `fill-paragraph' for each paragraph in region
instead of `fill-region' which is implied by the original version of `fill-paragraph'.
Justify when called with prefix arg."
(interactive "r\nP")
(save-excursion
(goto-char b)
(while (< (point) e)
(fill-paragraph justify)
(forward-paragraph)
)))
(global-set-key (kbd "C-c f") 'fill-region-paragraphs)
Below, there is an extented version of the code from the answer to another question.
I've added paragraph-fill
to it.
First select the items you want to be filled. Then press the prefix arg C-u and C-+ to get the formatting you want.
(defun org-adjust-region (b e)
"Re-adjust stuff in region according to the preceeding stuff."
(interactive "r") ;; current region
(save-excursion
(let ((e (set-marker (make-marker) e))
(_indent (lambda ()
(insert ?\n)
(backward-char)
(org-indent-line)
(delete-char 1)))
last-item-pos)
(goto-char b)
(beginning-of-line)
(while (< (point) e)
(indent-line-to 0)
(cond
((looking-at "[[:space:]]*$")) ;; ignore empty lines
((org-at-heading-p)) ;; just leave the zero-indent
((org-at-item-p)
(funcall _indent)
(let ((struct (org-list-struct))
(mark-active nil))
(ignore-errors (org-list-indent-item-generic -1 t struct)))
(setq last-item-pos (point))
(when current-prefix-arg
(fill-paragraph)))
((org-at-block-p)
(funcall _indent)
(goto-char (plist-get (cadr (org-element-special-block-parser e nil)) :contents-end))
(org-indent-line))
(t (funcall _indent)))
(forward-line))
(when last-item-pos
(goto-char last-item-pos)
(org-list-repair)
))))
(define-key org-mode-map (kbd "C-+") 'org-adjust-region)
In emacs -Q
25.1.50.2 I used the following org file test.org
for testing:
* first header
- Funded pensions :: As explained more fully in Chapter 3, funded pensions are paid on behalf of its members.
- Funded pensions :: As explained more fully in Chapter 3, funded pensions are paid on behalf of its members.
- Funded pensions :: As explained more fully in Chapter 3, funded pensions are paid on behalf of its members.
Selecting all items as region and calling fill-region-as-paragraph
resulted into:
* first header
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members. - Funded pensions :: As
explained more fully in Chapter 3, funded pensions are paid on behalf
of its members. - Funded pensions :: As explained more fully in
Chapter 3, funded pensions are paid on behalf of its members.
which is clearly not what you want.
Applying fill-region-paragraphs
results into the following text:
* first header
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members.
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members.
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members.
This text looks to me like what you want.
Running C-u C-+ even gets the indentation right:
* first header
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members.
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members.
- Funded pensions :: As explained more fully in Chapter 3, funded
pensions are paid on behalf of its members.
M-q
. But, I believe that you want to fill a region with items. – Tobias Feb 24 '16 at 17:01fill-region-as-paragraph
orfill-region-paragraphs
. I have to manually delete the first line break. – sbac Feb 24 '16 at 17:31fill-paragraph
alone. In each item the second line starts at the same column as the bullet of the item. The org-syntax says that this line does not belong to the item. This is exactly the reason why there is the commandorg-adjust-region
. So, my recommendation for this case is to tryorg-adjust-region
with prefix arg. – Tobias Feb 25 '16 at 4:41