When at an item or a footnote definition, fill first paragraph instead

* lisp/org.el (org-fill-paragraph): When at an item or a footnote
  definition, fill first paragraph instead.
* testing/lisp/test-org.el:
This commit is contained in:
Nicolas Goaziou 2012-07-31 20:49:11 +02:00
parent 64a30f6ecb
commit 783fd91e66
2 changed files with 111 additions and 89 deletions

View File

@ -20892,94 +20892,103 @@ If JUSTIFY is non-nil (interactively, with prefix argument),
justify as well. If `sentence-end-double-space' is non-nil, then
period followed by one space does not end a sentence, so don't
break a line there. The variable `fill-column' controls the
width for filling."
(let ((element (org-element-at-point)))
;; First check if point is in a blank line at the beginning of the
;; buffer. In that case, ignore filling.
(if (< (point) (org-element-property :begin element)) t
(case (org-element-type element)
;; Align Org tables, leave table.el tables as-is.
(table-row (org-table-align) t)
(table
(when (eq (org-element-property :type element) 'org) (org-table-align))
t)
;; Elements that may contain `line-break' type objects.
((paragraph verse-block)
(let ((beg (org-element-property :contents-begin element))
(end (org-element-property :contents-end element))
(type (org-element-type element)))
;; Do nothing if point is at an affiliated keyword or at
;; verse block markers.
(if (or (< (point) beg)
(and (eq type 'verse-block) (>= (point) end)))
t
;; At a verse block, first narrow to current "paragraph"
;; and set current element to that paragraph.
(save-restriction
(when (eq type 'verse-block)
(narrow-to-region beg end)
width for filling.
For convenience, when point is at a plain list, an item or
a footnote definition, try to fill the first paragraph within."
(save-excursion
;; Move to end of line in order to get the first paragraph within
;; a plain list or a footnote definition.
(end-of-line)
(let ((element (org-element-at-point)))
;; First check if point is in a blank line at the beginning of the
;; buffer. In that case, ignore filling.
(if (< (point) (org-element-property :begin element)) t
(case (org-element-type element)
;; Align Org tables, leave table.el tables as-is.
(table-row (org-table-align) t)
(table
(when (eq (org-element-property :type element) 'org)
(org-table-align))
t)
;; Elements that may contain `line-break' type objects.
((paragraph verse-block)
(let ((beg (org-element-property :contents-begin element))
(end (org-element-property :contents-end element))
(type (org-element-type element)))
;; Do nothing if point is at an affiliated keyword or at
;; verse block markers.
(if (or (< (point) beg)
(and (eq type 'verse-block) (>= (point) end)))
t
;; At a verse block, first narrow to current "paragraph"
;; and set current element to that paragraph.
(save-restriction
(when (eq type 'verse-block)
(narrow-to-region beg end)
(save-excursion
(let ((bol-pos (point-at-bol)))
(re-search-backward
org-element-paragraph-separate nil 'm)
(unless (or (bobp) (= (point-at-bol) bol-pos))
(forward-line))
(setq element (org-element-paragraph-parser end)
beg (org-element-property :contents-begin element)
end (org-element-property
:contents-end element)))))
;; Fill paragraph, taking line breaks into consideration.
;; For that, slice the paragraph using line breaks as
;; separators, and fill the parts in reverse order to
;; avoid messing with markers.
(save-excursion
(end-of-line)
(let ((bol-pos (point-at-bol)))
(re-search-backward org-element-paragraph-separate nil 'm)
(unless (or (bobp) (= (point-at-bol) bol-pos))
(forward-line))
(setq element (org-element-paragraph-parser end)
beg (org-element-property :contents-begin element)
end (org-element-property :contents-end element)))))
;; Fill paragraph, taking line breaks into consideration.
;; For that, slice the paragraph using line breaks as
;; separators, and fill the parts in reverse order to
;; avoid messing with markers.
(save-excursion
(goto-char end)
(mapc
(lambda (pos)
(let ((fill-prefix (org-fill-context-prefix pos)))
(fill-region-as-paragraph pos (point) justify))
(goto-char pos))
;; Find the list of ending positions for line breaks
;; in the current paragraph. Add paragraph beginning
;; to include first slice.
(nreverse
(cons beg
(org-element-map
(org-element--parse-objects
beg end nil org-element-all-objects)
'line-break
(lambda (lb) (org-element-property :end lb))))))))
t)))
;; Contents of `comment-block' type elements should be filled as
;; plain text.
(comment-block
(let ((fill-prefix (org-fill-context-prefix (point))))
(save-excursion
(fill-region-as-paragraph
(progn
(goto-char (org-element-property :begin element))
(while (looking-at org-element--affiliated-re) (forward-line))
(forward-line)
(point))
(progn
(goto-char (org-element-property :end element))
(skip-chars-backward " \r\t\n")
(line-beginning-position))
justify))) t)
;; Fill comments, indented or not.
(comment
(let ((fill-prefix (org-fill-context-prefix (point))))
(save-excursion
(fill-region-as-paragraph
(progn
(goto-char (org-element-property :begin element))
(while (looking-at org-element--affiliated-re) (forward-line))
(point))
(progn
(goto-char (org-element-property :end element))
(skip-chars-backward " \r\t\n")
(line-end-position))))))
;; Ignore every other element.
(otherwise t)))))
(goto-char end)
(mapc
(lambda (pos)
(let ((fill-prefix (org-fill-context-prefix pos)))
(fill-region-as-paragraph pos (point) justify))
(goto-char pos))
;; Find the list of ending positions for line breaks
;; in the current paragraph. Add paragraph beginning
;; to include first slice.
(nreverse
(cons beg
(org-element-map
(org-element--parse-objects
beg end nil org-element-all-objects)
'line-break
(lambda (lb) (org-element-property :end lb))))))))
t)))
;; Contents of `comment-block' type elements should be filled as
;; plain text.
(comment-block
(let ((fill-prefix (org-fill-context-prefix (point))))
(save-excursion
(fill-region-as-paragraph
(progn
(goto-char (org-element-property :begin element))
(while (looking-at org-element--affiliated-re) (forward-line))
(forward-line)
(point))
(progn
(goto-char (org-element-property :end element))
(skip-chars-backward " \r\t\n")
(line-beginning-position))
justify))) t)
;; Fill comments, indented or not.
(comment
(let ((fill-prefix (org-fill-context-prefix (point))))
(save-excursion
(fill-region-as-paragraph
(progn
(goto-char (org-element-property :begin element))
(while (looking-at org-element--affiliated-re) (forward-line))
(point))
(progn
(goto-char (org-element-property :end element))
(skip-chars-backward " \r\t\n")
(line-end-position))))))
;; Ignore every other element.
(otherwise t))))))
(defun org-auto-fill-function ()
"Auto-fill function."

View File

@ -143,8 +143,7 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/"
(let ((fill-column 20))
(org-fill-paragraph)
(should (equal (buffer-string) "some \\\\\nlong text"))))
;; Special case: fill correctly a paragraph when point is at its
;; very end.
;; Correctly fill a paragraph when point is at its very end.
(should
(equal "A B"
(org-test-with-temp-text "A\nB"
@ -152,6 +151,20 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/"
(goto-char (point-max))
(org-fill-paragraph)
(buffer-string)))))
;; Special case: Fill first paragraph when point is at an item or
;; a plain-list or a footnote reference.
(should
(equal "- A B"
(org-test-with-temp-text "- A\n B"
(let ((fill-column 20))
(org-fill-paragraph)
(buffer-string)))))
(should
(equal "[fn:1] A B"
(org-test-with-temp-text "[fn:1] A\nB"
(let ((fill-column 20))
(org-fill-paragraph)
(buffer-string)))))
;; At a verse block, fill paragraph at point, also preserving line
;; breaks. Though, do nothing when point is at the block
;; boundaries.