Skip to content

Commit

Permalink
org.el: Fix the filling of regions containing lists
Browse files Browse the repository at this point in the history
* lisp/org.el (org-setup-filling): Set fill-forward-paragraph-function.
(org--single-lines-list-is-paragraph): New internal variable.  Whether
a list with single lines items should be considered a single
paragraph.
(org--paragraph-at-point): use org--single-lines-list-is-paragraph.
(org-fill-paragraph): When an active region contains a list ensure
every item get filled.
* testing/lisp/test-org.el (test-org/fill-paragraph):
(test-org/fill-region): Test behaviour of fill-paragraph and
fill-region with an active region containing a list.

When filling paragraphs in a region, do not treat a list with single
lines items as a single paragraph.
  • Loading branch information
sebmiq committed Jul 4, 2022
1 parent 0ed0dea commit 4e42500
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lisp/org.el
Original file line number Diff line number Diff line change
Expand Up @@ -18709,6 +18709,8 @@ assumed to be significant there."
;; `org-setup-filling' installs filling and auto-filling related
;; variables during `org-mode' initialization.

(defvar org--single-lines-list-is-paragraph) ; defined later

(defun org-setup-filling ()
(require 'org-element)
;; Prevent auto-fill from inserting unwanted new items.
Expand All @@ -18722,6 +18724,10 @@ assumed to be significant there."
(setq-local paragraph-start paragraph-ending)
(setq-local paragraph-separate paragraph-ending))
(setq-local fill-paragraph-function 'org-fill-paragraph)
(setq-local fill-forward-paragraph-function
(lambda (&optional arg)
(let ((org--single-lines-list-is-paragraph nil))
(org-forward-paragraph arg))))
(setq-local auto-fill-inhibit-regexp nil)
(setq-local adaptive-fill-function 'org-adaptive-fill-function)
(setq-local normal-auto-fill-function 'org-auto-fill-function)
Expand Down Expand Up @@ -18951,9 +18957,11 @@ filling the current element."
(progn
(goto-char (region-end))
(skip-chars-backward " \t\n")
(while (> (point) start)
(org-fill-element justify)
(org-backward-paragraph)))
(let ((org--single-lines-list-is-paragraph nil))
(while (> (point) start)
(org-fill-element justify)
(org-backward-paragraph)
(skip-chars-backward " \t\n"))))
(goto-char origin)
(set-marker origin nil))))
(t
Expand Down Expand Up @@ -20301,6 +20309,9 @@ It also provides the following special moves for convenience:
;; Return moves left.
arg))

(defvar org--single-lines-list-is-paragraph t
"Treat plain lists with single line items as a whole paragraph")

(defun org--paragraph-at-point ()
"Return paragraph, or equivalent, element at point.

Expand Down Expand Up @@ -20362,7 +20373,7 @@ Function may return a real element, or a pseudo-element with type
(while (memq (org-element-type (org-element-property :parent l))
'(item plain-list))
(setq l (org-element-property :parent l)))
(and l
(and l org--single-lines-list-is-paragraph
(org-with-point-at (org-element-property :post-affiliated l)
(forward-line (length (org-element-property :structure l)))
(= (point) (org-element-property :contents-end l)))
Expand Down
41 changes: 41 additions & 0 deletions testing/lisp/test-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,49 @@
(push-mark (point) t t)
(goto-char (point-max))
(call-interactively #'org-fill-paragraph)
(buffer-string)))))
;; Fill every list item in a region
(should
(equal "\n- 2345678\n 9\n- 2345678\n 9"
(org-test-with-temp-text "\n- 2345678 9\n- 2345678 9"
(let ((fill-column 10))
(transient-mark-mode 1)
(push-mark (point-min) t t)
(goto-char (point-max))
(call-interactively #'org-fill-paragraph)
(buffer-string)))))
(should
(equal "\n- 2345678\n 9\n- 2345678"
(org-test-with-temp-text "\n- 2345678 9\n- 2345678"
(let ((fill-column 10))
(transient-mark-mode 1)
(push-mark (point-min) t t)
(goto-char (point-max))
(call-interactively #'org-fill-paragraph)
(buffer-string))))))

(ert-deftest test-org/fill-region ()
"Test `fill-region' behaviour."
;; fill-region should fill every item of a list
(should
(equal "\n- 2345678\n 9\n- 2345678\n 9"
(org-test-with-temp-text "\n- 2345678 9\n- 2345678 9"
(let ((fill-column 10))
(transient-mark-mode 1)
(push-mark (point-min) t t)
(goto-char (point-max))
(call-interactively #'fill-region)
(buffer-string)))))
(should
(equal "\n- 1 2\n- 1 2"
(org-test-with-temp-text "\n- 1\n 2\n- 1\n 2"
(let ((fill-column 10))
(transient-mark-mode 1)
(push-mark (point-min) t t)
(goto-char (point-max))
(call-interactively #'fill-region)
(buffer-string))))) )

(ert-deftest test-org/auto-fill-function ()
"Test auto-filling features."
;; Auto fill paragraph.
Expand Down

0 comments on commit 4e42500

Please sign in to comment.