diff options
author | Nicolas Goaziou <n.goaziou@gmail.com> | 2012-07-31 20:49:11 +0200 |
---|---|---|
committer | Nicolas Goaziou <n.goaziou@gmail.com> | 2012-07-31 20:49:11 +0200 |
commit | 783fd91e669992d9828e558771f683645ac92544 (patch) | |
tree | f159c8b04410ba8c2bfc57ea42ca7351233ea72a | |
parent | 64a30f6ecb2edf11922503bddc7dc540c7a17cd1 (diff) | |
download | org-mode-783fd91e669992d9828e558771f683645ac92544.tar.gz |
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:
-rw-r--r-- | lisp/org.el | 183 | ||||
-rw-r--r-- | testing/lisp/test-org.el | 17 |
2 files changed, 111 insertions, 89 deletions
diff --git a/lisp/org.el b/lisp/org.el index 7efb97f..c79b87d 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -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." diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 9747491..230d3e8 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -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. |