2

I copied and paste text in an org file and formatted it as a list:

- Funded pensions :: As explained more fully in Chapter 3, funded pensions are paid on behalf of its members.

How can I have the list formatted as:

- Funded pensions :: As explained more fully in Chapter 3, funded pensions are
      paid on behalf of its members.

Another simple example where fill-region-paragraphsdoes not work is:

* first header

- The primary objective of pensions is /economic security in old age/, achieved
through consumption smoothing, insurance, poverty relief, and redistribution.
- The primary objective of pension design is to /optimize old-age security/, includ-
ing the cost of providing it.
  • You can fill one single item with M-q. But, I believe that you want to fill a region with items. – Tobias Feb 24 '16 at 17:01
  • I tried with another example. I can't automatically indent nor using M-q, nor fill-region-as-paragraph or fill-region-paragraphs. I have to manually delete the first line break. – sbac Feb 24 '16 at 17:31
  • You should post such an example. – Tobias Feb 24 '16 at 20:14
  • Please see my edited post. – sbac Feb 24 '16 at 23:46
  • Your "other simple example" is another case. There, you do not want fill-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 command org-adjust-region. So, my recommendation for this case is to try org-adjust-region with prefix arg. – Tobias Feb 25 '16 at 4:41
4

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.
  • I used instead fill-region-as-paragraph with good results with only one exception: in that paragraph, I had first to delete manually the first line break. – sbac Feb 24 '16 at 17:05
  • Please see my edited post. – sbac Feb 25 '16 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.