(fill-individual-paragraphs): Avoid infinite loop
[bpt/emacs.git] / lisp / textmodes / fill.el
1 ;;; fill.el --- fill commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Keywords: wp
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; All the commands for filling text. These are documented in the Emacs
26 ;; manual.
27
28 ;;; Code:
29
30 (defconst fill-individual-varying-indent nil
31 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
32 Non-nil means changing indent doesn't end a paragraph.
33 That mode can handle paragraphs with extra indentation on the first line,
34 but it requires separator lines between paragraphs.
35 A value of nil means that any change in indentation starts a new paragraph.")
36
37 (defconst sentence-end-double-space t
38 "*Non-nil means a single space does not end a sentence.")
39
40 (defun set-fill-prefix ()
41 "Set the fill prefix to the current line up to point.
42 Filling expects lines to start with the fill prefix and
43 reinserts the fill prefix in each resulting line."
44 (interactive)
45 (setq fill-prefix (buffer-substring
46 (save-excursion (beginning-of-line) (point))
47 (point)))
48 (if (equal fill-prefix "")
49 (setq fill-prefix nil))
50 (if fill-prefix
51 (message "fill-prefix: \"%s\"" fill-prefix)
52 (message "fill-prefix cancelled")))
53
54 (defconst adaptive-fill-mode t
55 "*Non-nil means determine a paragraph's fill prefix from its text.")
56
57 (defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
58 "*Regexp to match text at start of line that constitutes indentation.
59 If Adaptive Fill mode is enabled, whatever text matches this pattern
60 on the second line of a paragraph is used as the standard indentation
61 for the paragraph.")
62
63 (defun fill-region-as-paragraph (from to &optional justify-flag)
64 "Fill region as one paragraph: break lines to fit `fill-column'.
65 Prefix arg means justify too.
66 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 From program, pass args FROM, TO and JUSTIFY-FLAG."
69 (interactive "r\nP")
70 ;; Arrange for undoing the fill to restore point.
71 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
72 (setq buffer-undo-list (cons (point) buffer-undo-list)))
73 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
74 (let ((fill-prefix fill-prefix))
75 ;; Figure out how this paragraph is indented, if desired.
76 (if (and adaptive-fill-mode
77 (or (null fill-prefix) (string= fill-prefix "")))
78 (save-excursion
79 (goto-char (min from to))
80 (if (eolp) (forward-line 1))
81 (forward-line 1)
82 (if (< (point) (max from to))
83 (let ((start (point)))
84 (re-search-forward adaptive-fill-regexp)
85 (setq fill-prefix (buffer-substring start (point))))
86 (goto-char (min from to))
87 (if (eolp) (forward-line 1))
88 ;; If paragraph has only one line, don't assume in general
89 ;; that additional lines would have the same starting
90 ;; decoration. Assume no indentation.
91 ;; But if left-margin is nonzero, we can assume ordinary
92 ;; lines do have indentation.
93 (if (> left-margin 0)
94 (progn
95 (re-search-forward adaptive-fill-regexp)
96 (setq fill-prefix (make-string (current-column) ?\ ))))
97 )))
98
99 (save-restriction
100 (goto-char (max from to))
101 ;; If specified region ends before a newline,
102 ;; include that newline.
103 (if (and (eolp) (not (eobp)) (not (bolp)))
104 (forward-char 1))
105 (narrow-to-region (min from to) (point))
106 (goto-char (point-min))
107 (skip-chars-forward "\n")
108 (narrow-to-region (point) (point-max))
109 (setq from (point))
110 (goto-char (point-max))
111 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
112 (regexp-quote fill-prefix))))
113 ;; Delete the fill prefix from every line except the first.
114 ;; The first line may not even have a fill prefix.
115 (and fpre
116 (progn
117 (if (>= (length fill-prefix) fill-column)
118 (error "fill-prefix too long for specified width"))
119 (goto-char (point-min))
120 (forward-line 1)
121 (while (not (eobp))
122 (if (looking-at fpre)
123 (delete-region (point) (match-end 0)))
124 (forward-line 1))
125 (goto-char (point-min))
126 (and (looking-at fpre) (forward-char (length fill-prefix)))
127 (setq from (point)))))
128 ;; from is now before the text to fill,
129 ;; but after any fill prefix on the first line.
130
131 ;; Make sure sentences ending at end of line get an extra space.
132 ;; loses on split abbrevs ("Mr.\nSmith")
133 (goto-char from)
134 (while (re-search-forward "[.?!][])}\"']*$" nil t)
135 (insert ? ))
136
137 ;; Then change all newlines to spaces.
138 (subst-char-in-region from (point-max) ?\n ?\ )
139
140 ;; Flush excess spaces, except in the paragraph indentation.
141 (goto-char from)
142 (skip-chars-forward " \t")
143 ;; Nuke tabs while we're at it; they get screwed up in a fill.
144 ;; This is quick, but loses when a tab follows the end of a sentence.
145 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
146 ;; Blame the typist.
147 (subst-char-in-region (point) (point-max) ?\t ?\ )
148 (while (re-search-forward " *" nil t)
149 (delete-region
150 (+ (match-beginning 0)
151 (if (and sentence-end-double-space
152 (save-excursion
153 (skip-chars-backward " ]})\"'")
154 (memq (preceding-char) '(?. ?? ?!))))
155 2 1))
156 (match-end 0)))
157 (goto-char (point-max))
158 (delete-horizontal-space)
159 (insert " ")
160 (goto-char (point-min))
161
162 ;; This is the actual filling loop.
163 (let ((prefixcol 0) linebeg)
164 (while (not (eobp))
165 (setq linebeg (point))
166 (move-to-column (1+ fill-column))
167 (if (eobp)
168 nil
169 ;; Move back to start of word.
170 (skip-chars-backward "^ \n" linebeg)
171 ;; Don't break after a period followed by just one space.
172 ;; Move back to the previous place to break.
173 ;; The reason is that if a period ends up at the end of a line,
174 ;; further fills will assume it ends a sentence.
175 ;; If we now know it does not end a sentence,
176 ;; avoid putting it at the end of the line.
177 (if sentence-end-double-space
178 (while (and (> (point) (+ linebeg 2))
179 (eq (preceding-char) ?\ )
180 (not (eq (following-char) ?\ ))
181 (eq (char-after (- (point) 2)) ?\.))
182 (forward-char -2)
183 (skip-chars-backward "^ \n" linebeg)))
184 (if (if (zerop prefixcol)
185 (save-excursion
186 (skip-chars-backward " " linebeg)
187 (bolp))
188 (>= prefixcol (current-column)))
189 ;; Keep at least one word even if fill prefix exceeds margin.
190 ;; This handles all but the first line of the paragraph.
191 ;; Meanwhile, don't stop at a period followed by one space.
192 (let ((first t))
193 (move-to-column prefixcol)
194 (while (and (not (eobp))
195 (or first
196 (and (not (bobp))
197 sentence-end-double-space
198 (save-excursion (forward-char -1)
199 (and (looking-at "\\. ")
200 (not (looking-at "\\. ")))))))
201 (skip-chars-forward " ")
202 (skip-chars-forward "^ \n")
203 (setq first nil)))
204 ;; Normally, move back over the single space between the words.
205 (forward-char -1))
206 (if (and fill-prefix (zerop prefixcol)
207 (< (- (point) (point-min)) (length fill-prefix))
208 (string= (buffer-substring (point-min) (point))
209 (substring fill-prefix 0 (- (point) (point-min)))))
210 ;; Keep at least one word even if fill prefix exceeds margin.
211 ;; This handles the first line of the paragraph.
212 ;; Don't stop at a period followed by just one space.
213 (let ((first t))
214 (while (and (not (eobp))
215 (or first
216 (and (not (bobp))
217 sentence-end-double-space
218 (save-excursion (forward-char -1)
219 (and (looking-at "\\. ")
220 (not (looking-at "\\. ")))))))
221 (skip-chars-forward " ")
222 (skip-chars-forward "^ \n")
223 (setq first nil)))))
224 ;; Replace all whitespace here with one newline.
225 ;; Insert before deleting, so we don't forget which side of
226 ;; the whitespace point or markers used to be on.
227 (skip-chars-backward " ")
228 (insert ?\n)
229 (delete-horizontal-space)
230 ;; Insert the fill prefix at start of each line.
231 ;; Set prefixcol so whitespace in the prefix won't get lost.
232 (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
233 (progn
234 (insert fill-prefix)
235 (setq prefixcol (current-column))))
236 ;; Justify the line just ended, if desired.
237 (and justify-flag (not (eobp))
238 (progn
239 (forward-line -1)
240 (justify-current-line)
241 (forward-line 1))))))))
242
243 (defun fill-paragraph (arg)
244 "Fill paragraph at or after point. Prefix arg means justify as well.
245 If `sentence-end-double-space' is non-nil, then period followed by one
246 space does not end a sentence, so don't break a line there."
247 (interactive "P")
248 (let ((before (point)))
249 (save-excursion
250 (forward-paragraph)
251 (or (bolp) (newline 1))
252 (let ((end (point))
253 (beg (progn (backward-paragraph) (point))))
254 (goto-char before)
255 (fill-region-as-paragraph beg end arg)))))
256
257 (defun fill-region (from to &optional justify-flag)
258 "Fill each of the paragraphs in the region.
259 Prefix arg (non-nil third arg, if called from program) means justify as well.
260 If `sentence-end-double-space' is non-nil, then period followed by one
261 space does not end a sentence, so don't break a line there."
262 (interactive "r\nP")
263 (save-restriction
264 (goto-char (max from to))
265 ;; If specified region ends before a newline,
266 ;; include that newline.
267 (if (and (eolp) (not (eobp)) (not (bolp)))
268 (forward-char 1))
269 (narrow-to-region (min from to) (point))
270 (goto-char (point-min))
271 (while (not (eobp))
272 (let ((initial (point))
273 (end (progn
274 (forward-paragraph 1) (point))))
275 (forward-paragraph -1)
276 (if (>= (point) initial)
277 (fill-region-as-paragraph (point) end justify-flag)
278 (goto-char end))))))
279
280 (defun justify-current-line ()
281 "Add spaces to line point is in, so it ends at `fill-column'."
282 (interactive)
283 (save-excursion
284 (save-restriction
285 (let (ncols beg indent end)
286 (beginning-of-line)
287 (forward-char (length fill-prefix))
288 (skip-chars-forward " \t")
289 (setq indent (current-column))
290 (setq beg (point))
291 (end-of-line)
292 (narrow-to-region beg (point))
293 (setq end (point))
294 (skip-chars-backward " \t")
295 (delete-char (- end (point)))
296 (goto-char beg)
297 (while (re-search-forward " *" nil t)
298 (delete-region
299 (+ (match-beginning 0)
300 (if (save-excursion
301 (skip-chars-backward " ])\"'")
302 (memq (preceding-char) '(?. ?? ?!)))
303 2 1))
304 (match-end 0)))
305 (goto-char beg)
306 (while (re-search-forward "[.?!][])\"']*\n" nil t)
307 (forward-char -1)
308 (insert-and-inherit ? ))
309 (goto-char (point-max))
310 ;; Note that the buffer bounds start after the indentation,
311 ;; so the columns counted by INDENT don't appear in (current-column).
312 (setq ncols (- fill-column (current-column) indent))
313 (if (search-backward " " nil t)
314 (while (> ncols 0)
315 (let ((nmove (+ 3 (random 3))))
316 (while (> nmove 0)
317 (or (search-backward " " nil t)
318 (progn
319 (goto-char (point-max))
320 (search-backward " ")))
321 (skip-chars-backward " ")
322 (setq nmove (1- nmove))))
323 (insert-and-inherit " ")
324 (skip-chars-backward " ")
325 (setq ncols (1- ncols)))))))
326 nil)
327
328 \f
329 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
330 "Fill paragraphs within the region, allowing varying indentation within each.
331 This command divides the region into \"paragraphs\",
332 only at paragraph-separator lines, then fills each paragraph
333 using as the fill prefix the smallest indentation of any line
334 in the paragraph.
335
336 When calling from a program, pass range to fill as first two arguments.
337
338 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
339 JUSTIFY-FLAG to justify paragraphs (prefix arg),
340 MAIL-FLAG for a mail message, i. e. don't fill header lines."
341 (interactive "r\nP")
342 (let ((fill-individual-varying-indent t))
343 (fill-individual-paragraphs min max justifyp mailp)))
344
345 (defun fill-individual-paragraphs (min max &optional justifyp mailp)
346 "Fill paragraphs of uniform indentation within the region.
347 This command divides the region into \"paragraphs\",
348 treating every change in indentation level as a paragraph boundary,
349 then fills each paragraph using its indentation level as the fill prefix.
350
351 When calling from a program, pass range to fill as first two arguments.
352
353 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
354 JUSTIFY-FLAG to justify paragraphs (prefix arg),
355 MAIL-FLAG for a mail message, i. e. don't fill header lines."
356 (interactive "r\nP")
357 (save-restriction
358 (save-excursion
359 (goto-char min)
360 (beginning-of-line)
361 (narrow-to-region (point) max)
362 (if mailp
363 (while (and (not (eobp))
364 (or (looking-at "[ \t]*[^ \t\n]*:")
365 (looking-at "[ \t]*$")))
366 (if (looking-at "[ \t]*[^ \t\n]*:")
367 (search-forward "\n\n" nil 'move)
368 (forward-line 1))))
369 (narrow-to-region (point) max)
370 ;; Loop over paragraphs.
371 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
372 (beginning-of-line)
373 (let ((start (point))
374 fill-prefix fill-prefix-regexp)
375 ;; Find end of paragraph, and compute the smallest fill-prefix
376 ;; that fits all the lines in this paragraph.
377 (while (progn
378 ;; Update the fill-prefix on the first line
379 ;; and whenever the prefix good so far is too long.
380 (if (not (and fill-prefix
381 (looking-at fill-prefix-regexp)))
382 (setq fill-prefix
383 (buffer-substring (point)
384 (save-excursion (skip-chars-forward " \t") (point)))
385 fill-prefix-regexp
386 (regexp-quote fill-prefix)))
387 (forward-line 1)
388 ;; Now stop the loop if end of paragraph.
389 (and (not (eobp))
390 (if fill-individual-varying-indent
391 ;; If this line is a separator line, with or
392 ;; without prefix, end the paragraph.
393 (and
394 (not (looking-at paragraph-separate))
395 (save-excursion
396 (not (and (looking-at fill-prefix-regexp)
397 (progn (forward-char (length fill-prefix))
398 (looking-at paragraph-separate))))))
399 ;; If this line has more or less indent
400 ;; than the fill prefix wants, end the paragraph.
401 (and (looking-at fill-prefix-regexp)
402 (save-excursion
403 (not (progn (forward-char (length fill-prefix))
404 (or (looking-at paragraph-separate)
405 (looking-at paragraph-start))))))))))
406 ;; Fill this paragraph, but don't add a newline at the end.
407 (let ((had-newline (bolp)))
408 (fill-region-as-paragraph start (point) justifyp)
409 (or had-newline (delete-char -1))))))))
410
411 ;;; fill.el ends here