diff options
| author | Richard M. Stallman | 1995-01-13 08:44:26 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1995-01-13 08:44:26 +0000 |
| commit | a098333daefdaa5cb1a0ca8f225de5ffc40e6a89 (patch) | |
| tree | 425479f5df4fe9b9c1571fe32e6dcd1c39d65be9 | |
| parent | 28191e20b40f815347504ef92c4ca1a535d5ca80 (diff) | |
| download | emacs-a098333daefdaa5cb1a0ca8f225de5ffc40e6a89.tar.gz emacs-a098333daefdaa5cb1a0ca8f225de5ffc40e6a89.zip | |
(fill-region-as-paragraph): Don't modify newline
at end of paragraph, to make sure text-properties are preserved.
(fill-paragraph): Call fill-region, not fill-region-as-paragraph,
to avoid clobbering paragraph-internal hard-newlines.
(fill-region): If use-hard-newlines is on, divide into
"paragraphs" by breaking at every hard newline, temporarily
ignoring paragraph-start.
| -rw-r--r-- | lisp/textmodes/fill.el | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el index 72aea08324a..9880e8c5fca 100644 --- a/lisp/textmodes/fill.el +++ b/lisp/textmodes/fill.el | |||
| @@ -62,6 +62,7 @@ for the paragraph.") | |||
| 62 | 62 | ||
| 63 | (defun fill-region-as-paragraph (from to &optional justify-flag) | 63 | (defun fill-region-as-paragraph (from to &optional justify-flag) |
| 64 | "Fill region as one paragraph: break lines to fit `fill-column'. | 64 | "Fill region as one paragraph: break lines to fit `fill-column'. |
| 65 | Any paragraph breaks in the region will be removed. | ||
| 65 | Prefix arg means justify too. | 66 | Prefix arg means justify too. |
| 66 | If `sentence-end-double-space' is non-nil, then period followed by one | 67 | If `sentence-end-double-space' is non-nil, then period followed by one |
| 67 | space does not end a sentence, so don't break a line there. | 68 | space does not end a sentence, so don't break a line there. |
| @@ -97,23 +98,21 @@ From program, pass args FROM, TO and JUSTIFY-FLAG." | |||
| 97 | ))) | 98 | ))) |
| 98 | 99 | ||
| 99 | (save-restriction | 100 | (save-restriction |
| 100 | (let (end) | 101 | (let (beg) |
| 102 | (goto-char (min from to)) | ||
| 103 | (skip-chars-forward "\n") | ||
| 104 | (setq beg (point)) | ||
| 101 | (goto-char (max from to)) | 105 | (goto-char (max from to)) |
| 102 | ;; If specified region ends before a newline, | 106 | (skip-chars-backward "\n") |
| 103 | ;; include that newline. | 107 | (setq to (point) |
| 104 | (if (and (eolp) (not (eobp)) (not (bolp))) | 108 | from beg) |
| 105 | (forward-char 1)) | ||
| 106 | (setq end (point)) | ||
| 107 | (setq from (min from to)) | ||
| 108 | (goto-char from) | 109 | (goto-char from) |
| 109 | (beginning-of-line) | 110 | (beginning-of-line) |
| 110 | (narrow-to-region (point) end)) | 111 | (narrow-to-region (point) to)) |
| 111 | (skip-chars-forward "\n") | 112 | (if use-hard-newlines |
| 112 | (narrow-to-region (point) (point-max)) | 113 | (remove-text-properties from to '(hard nil))) |
| 113 | (if (> from (point)) | 114 | (if (> from (point)) |
| 114 | (goto-char from) | 115 | (goto-char from)) |
| 115 | (setq from (point))) | ||
| 116 | (goto-char (point-max)) | ||
| 117 | (let ((fpre (and fill-prefix (not (equal fill-prefix "")) | 116 | (let ((fpre (and fill-prefix (not (equal fill-prefix "")) |
| 118 | (regexp-quote fill-prefix)))) | 117 | (regexp-quote fill-prefix)))) |
| 119 | ;; Delete the fill prefix from every line except the first. | 118 | ;; Delete the fill prefix from every line except the first. |
| @@ -138,7 +137,7 @@ From program, pass args FROM, TO and JUSTIFY-FLAG." | |||
| 138 | ;; loses on split abbrevs ("Mr.\nSmith") | 137 | ;; loses on split abbrevs ("Mr.\nSmith") |
| 139 | (goto-char from) | 138 | (goto-char from) |
| 140 | (while (re-search-forward "[.?!][])}\"']*$" nil t) | 139 | (while (re-search-forward "[.?!][])}\"']*$" nil t) |
| 141 | (insert ? )) | 140 | (insert-and-inherit ? )) |
| 142 | 141 | ||
| 143 | ;; Then change all newlines to spaces. | 142 | ;; Then change all newlines to spaces. |
| 144 | (subst-char-in-region from (point-max) ?\n ?\ ) | 143 | (subst-char-in-region from (point-max) ?\n ?\ ) |
| @@ -162,7 +161,7 @@ From program, pass args FROM, TO and JUSTIFY-FLAG." | |||
| 162 | (match-end 0))) | 161 | (match-end 0))) |
| 163 | (goto-char (point-max)) | 162 | (goto-char (point-max)) |
| 164 | (delete-horizontal-space) | 163 | (delete-horizontal-space) |
| 165 | (insert " ") | 164 | (insert-and-inherit " ") |
| 166 | (goto-char (point-min)) | 165 | (goto-char (point-min)) |
| 167 | 166 | ||
| 168 | ;; This is the actual filling loop. | 167 | ;; This is the actual filling loop. |
| @@ -171,7 +170,7 @@ From program, pass args FROM, TO and JUSTIFY-FLAG." | |||
| 171 | (setq linebeg (point)) | 170 | (setq linebeg (point)) |
| 172 | (move-to-column (1+ fill-column)) | 171 | (move-to-column (1+ fill-column)) |
| 173 | (if (eobp) | 172 | (if (eobp) |
| 174 | nil | 173 | (delete-horizontal-space) |
| 175 | ;; Move back to start of word. | 174 | ;; Move back to start of word. |
| 176 | (skip-chars-backward "^ \n" linebeg) | 175 | (skip-chars-backward "^ \n" linebeg) |
| 177 | ;; Don't break after a period followed by just one space. | 176 | ;; Don't break after a period followed by just one space. |
| @@ -226,19 +225,22 @@ From program, pass args FROM, TO and JUSTIFY-FLAG." | |||
| 226 | (not (looking-at "\\. "))))))) | 225 | (not (looking-at "\\. "))))))) |
| 227 | (skip-chars-forward " ") | 226 | (skip-chars-forward " ") |
| 228 | (skip-chars-forward "^ \n") | 227 | (skip-chars-forward "^ \n") |
| 229 | (setq first nil))))) | 228 | (setq first nil)))) |
| 230 | ;; Replace all whitespace here with one newline. | 229 | ;; Replace all whitespace here with one newline. |
| 231 | ;; Insert before deleting, so we don't forget which side of | 230 | ;; Insert before deleting, so we don't forget which side of |
| 232 | ;; the whitespace point or markers used to be on. | 231 | ;; the whitespace point or markers used to be on. |
| 233 | (skip-chars-backward " ") | 232 | (skip-chars-backward " ") |
| 234 | (insert ?\n) | 233 | (insert ?\n) |
| 235 | (delete-horizontal-space) | 234 | ;; Give newline the properties of the space(s) it replaces |
| 236 | ;; Insert the fill prefix at start of each line. | 235 | (set-text-properties (1- (point)) (point) |
| 237 | ;; Set prefixcol so whitespace in the prefix won't get lost. | 236 | (text-properties-at (point))) |
| 238 | (and (not (eobp)) fill-prefix (not (equal fill-prefix "")) | 237 | (delete-horizontal-space) |
| 239 | (progn | 238 | ;; Insert the fill prefix at start of each line. |
| 240 | (insert fill-prefix) | 239 | ;; Set prefixcol so whitespace in the prefix won't get lost. |
| 241 | (setq prefixcol (current-column)))) | 240 | (and fill-prefix (not (equal fill-prefix "")) |
| 241 | (progn | ||
| 242 | (insert-and-inherit fill-prefix) | ||
| 243 | (setq prefixcol (current-column))))) | ||
| 242 | ;; Justify the line just ended, if desired. | 244 | ;; Justify the line just ended, if desired. |
| 243 | (and justify-flag (not (eobp)) | 245 | (and justify-flag (not (eobp)) |
| 244 | (progn | 246 | (progn |
| @@ -258,7 +260,11 @@ space does not end a sentence, so don't break a line there." | |||
| 258 | (let ((end (point)) | 260 | (let ((end (point)) |
| 259 | (beg (progn (backward-paragraph) (point)))) | 261 | (beg (progn (backward-paragraph) (point)))) |
| 260 | (goto-char before) | 262 | (goto-char before) |
| 261 | (fill-region-as-paragraph beg end arg))))) | 263 | (if use-hard-newlines |
| 264 | ;; Can't use fill-region-as-paragraph, since this paragraph may | ||
| 265 | ;; still contain hard newlines. See fill-region. | ||
| 266 | (fill-region beg end arg) | ||
| 267 | (fill-region-as-paragraph beg end arg)))))) | ||
| 262 | 268 | ||
| 263 | (defun fill-region (from to &optional justify-flag) | 269 | (defun fill-region (from to &optional justify-flag) |
| 264 | "Fill each of the paragraphs in the region. | 270 | "Fill each of the paragraphs in the region. |
| @@ -266,13 +272,12 @@ Prefix arg (non-nil third arg, if called from program) means justify as well. | |||
| 266 | If `sentence-end-double-space' is non-nil, then period followed by one | 272 | If `sentence-end-double-space' is non-nil, then period followed by one |
| 267 | space does not end a sentence, so don't break a line there." | 273 | space does not end a sentence, so don't break a line there." |
| 268 | (interactive "r\nP") | 274 | (interactive "r\nP") |
| 269 | (let (end beg) | 275 | ;; If using hard newlines, break at every one for filling purposes rather |
| 276 | ;; than breaking at normal paragraph breaks. | ||
| 277 | (let ((paragraph-start (if use-hard-newlines "^" paragraph-start)) | ||
| 278 | end beg) | ||
| 270 | (save-restriction | 279 | (save-restriction |
| 271 | (goto-char (max from to)) | 280 | (goto-char (max from to)) |
| 272 | ;; If specified region ends before a newline, | ||
| 273 | ;; include that newline. | ||
| 274 | (if (and (eolp) (not (eobp)) (not (bolp))) | ||
| 275 | (forward-char 1)) | ||
| 276 | (setq end (point)) | 281 | (setq end (point)) |
| 277 | (goto-char (setq beg (min from to))) | 282 | (goto-char (setq beg (min from to))) |
| 278 | (beginning-of-line) | 283 | (beginning-of-line) |