]> code.delx.au - gnu-emacs/blob - lisp/textmodes/fill.el
991ca8a19331b6ff213a5689f5a480efc7d01f7d
[gnu-emacs] / lisp / textmodes / fill.el
1 ;;; fill.el --- fill commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995 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 (defconst colon-double-space nil
41 "*Non-nil means put two spaces after a colon when filling.")
42
43 (defvar fill-paragraph-function nil
44 "Mode-specific function to fill a paragraph.")
45
46 (defun set-fill-prefix ()
47 "Set the fill prefix to the current line up to point.
48 Filling expects lines to start with the fill prefix and
49 reinserts the fill prefix in each resulting line."
50 (interactive)
51 (setq fill-prefix (buffer-substring
52 (save-excursion (move-to-left-margin) (point))
53 (point)))
54 (if (equal fill-prefix "")
55 (setq fill-prefix nil))
56 (if fill-prefix
57 (message "fill-prefix: \"%s\"" fill-prefix)
58 (message "fill-prefix cancelled")))
59
60 (defconst adaptive-fill-mode t
61 "*Non-nil means determine a paragraph's fill prefix from its text.")
62
63 (defconst adaptive-fill-regexp "[ \t]*\\([#;>*]+ +\\)?"
64 "*Regexp to match text at start of line that constitutes indentation.
65 If Adaptive Fill mode is enabled, whatever text matches this pattern
66 on the second line of a paragraph is used as the standard indentation
67 for the paragraph. If the paragraph has just one line, the indentation
68 is taken from that line.")
69
70 (defun adaptive-fill-function nil
71 "*Function to call to choose a fill prefix for a paragraph.
72 This function is used when `adaptive-fill-regexp' does not match.")
73
74 (defun current-fill-column ()
75 "Return the fill-column to use for this line.
76 The fill-column to use for a buffer is stored in the variable `fill-column',
77 but can be locally modified by the `right-margin' text property, which is
78 subtracted from `fill-column'.
79
80 The fill column to use for a line is the first column at which the column
81 number equals or exceeds the local fill-column - right-margin difference."
82 (save-excursion
83 (if fill-column
84 (let* ((here (progn (beginning-of-line) (point)))
85 (here-col 0)
86 (eol (progn (end-of-line) (point)))
87 margin fill-col change col)
88 ;; Look separately at each region of line with a different right-margin.
89 (while (and (setq margin (get-text-property here 'right-margin)
90 fill-col (- fill-column (or margin 0))
91 change (text-property-not-all
92 here eol 'right-margin margin))
93 (progn (goto-char (1- change))
94 (setq col (current-column))
95 (< col fill-col)))
96 (setq here change
97 here-col col))
98 (max here-col fill-col)))))
99
100 (defun canonically-space-region (beg end)
101 "Remove extra spaces between words in region.
102 Puts one space between words in region; two between sentences.
103 Remove indentation from each line."
104 (interactive "r")
105 (save-excursion
106 (goto-char beg)
107 ;; Nuke tabs; they get screwed up in a fill.
108 ;; This is quick, but loses when a tab follows the end of a sentence.
109 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
110 ;; Blame the typist.
111 (subst-char-in-region beg end ?\t ?\ )
112 (while (and (< (point) end)
113 (re-search-forward " *" end t))
114 (delete-region
115 (+ (match-beginning 0)
116 ;; Determine number of spaces to leave:
117 (save-excursion
118 (skip-chars-backward " ]})\"'")
119 (cond ((and sentence-end-double-space
120 (memq (preceding-char) '(?. ?? ?!))) 2)
121 ((and colon-double-space
122 (= (preceding-char) ?:)) 2)
123 ((char-equal (preceding-char) ?\n) 0)
124 (t 1))))
125 (match-end 0)))
126 ;; Make sure sentences ending at end of line get an extra space.
127 ;; loses on split abbrevs ("Mr.\nSmith")
128 (goto-char beg)
129 (while (and (< (point) end)
130 (re-search-forward "[.?!][])}\"']*$" end t))
131 (insert-and-inherit ? ))))
132
133 (defun fill-region-as-paragraph (from to &optional justify nosqueeze)
134 "Fill the region as one paragraph.
135 Removes any paragraph breaks in the region and extra newlines at the end,
136 indents and fills lines between the margins given by the
137 `current-left-margin' and `current-fill-column' functions.
138
139 Normally performs justification according to the `current-justification'
140 function, but with a prefix arg, does full justification instead.
141
142 From a program, optional third arg JUSTIFY can specify any type of
143 justification, and fourth arg NOSQUEEZE non-nil means not to make spaces
144 between words canonical before filling.
145
146 If `sentence-end-double-space' is non-nil, then period followed by one
147 space does not end a sentence, so don't break a line there."
148 (interactive (list (region-beginning) (region-end)
149 (if current-prefix-arg 'full)))
150 ;; Arrange for undoing the fill to restore point.
151 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
152 (setq buffer-undo-list (cons (point) buffer-undo-list)))
153
154 ;; Make sure "to" is the endpoint. Make sure that we end up there.
155 (goto-char (min from to))
156 (setq to (max from to))
157 (setq from (point))
158
159 ;; Delete all but one soft newline at end of region.
160 (goto-char to)
161 (let ((oneleft nil))
162 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
163 (if (and oneleft
164 (not (and use-hard-newlines
165 (get-text-property (1- (point)) 'hard))))
166 (delete-backward-char 1)
167 (backward-char 1)
168 (setq oneleft t)))
169 ;; If there was no newline, create one.
170 (if (and (not oneleft) (> (point) from))
171 (save-excursion (newline))))
172 (setq to (point))
173
174 ;; Ignore blank lines at beginning of region.
175 (goto-char from)
176 (skip-chars-forward " \t\n")
177 (beginning-of-line)
178 (setq from (point))
179
180 (if (>= from to)
181 nil ; There is no paragraph at all.
182
183 (or justify (setq justify (current-justification)))
184
185 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
186 (let ((fill-prefix fill-prefix))
187 ;; Figure out how this paragraph is indented, if desired.
188 (if (and adaptive-fill-mode
189 (or (null fill-prefix) (string= fill-prefix "")))
190 (save-excursion
191 (goto-char from)
192 (if (eolp) (forward-line 1))
193 ;; Move to the second line unless there is just one.
194 (let ((firstline (point)))
195 (forward-line 1)
196 (if (>= (point) to)
197 (goto-char firstline)))
198 (move-to-left-margin)
199 (let ((start (point))
200 (eol (save-excursion (end-of-line) (point)))
201 temp)
202 (if (not (looking-at paragraph-start))
203 (cond ((re-search-forward adaptive-fill-regexp nil t)
204 (setq fill-prefix
205 (buffer-substring-no-properties start (point))))
206 ((setq temp (funcall adaptive-fill-function))
207 (setq fill-prefix temp)))))))
208
209 (save-restriction
210 (goto-char from)
211 (beginning-of-line)
212 (narrow-to-region (point) to)
213
214 (if (not justify) ; filling disabled: just check indentation
215 (progn
216 (goto-char from)
217 (while (not (eobp))
218 (if (and (not (eolp))
219 (< (current-indentation) (current-left-margin)))
220 (indent-to-left-margin))
221 (forward-line 1)))
222
223 (if use-hard-newlines
224 (remove-text-properties from (point-max) '(hard nil)))
225 ;; Make sure first line is indented (at least) to left margin...
226 (if (or (memq justify '(right center))
227 (< (current-indentation) (current-left-margin)))
228 (indent-to-left-margin))
229 ;; Delete the fill prefix from every line except the first.
230 ;; The first line may not even have a fill prefix.
231 (goto-char from)
232 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
233 (concat "[ \t]*"
234 (regexp-quote fill-prefix)
235 "[ \t]*"))))
236 (and fpre
237 (progn
238 (if (>= (+ (current-left-margin) (length fill-prefix))
239 (current-fill-column))
240 (error "fill-prefix too long for specified width"))
241 (goto-char from)
242 (forward-line 1)
243 (while (not (eobp))
244 (if (looking-at fpre)
245 (delete-region (point) (match-end 0)))
246 (forward-line 1))
247 (goto-char from)
248 (and (looking-at fpre) (goto-char (match-end 0)))
249 (setq from (point)))))
250 ;; Remove indentation from lines other than the first.
251 (beginning-of-line 2)
252 (indent-region (point) (point-max) 0)
253 (goto-char from)
254
255 ;; FROM, and point, are now before the text to fill,
256 ;; but after any fill prefix on the first line.
257
258 ;; Make sure sentences ending at end of line get an extra space.
259 ;; loses on split abbrevs ("Mr.\nSmith")
260 (while (re-search-forward "[.?!][])}\"']*$" nil t)
261 (insert-and-inherit ? ))
262 (goto-char from)
263 (skip-chars-forward " \t")
264 ;; Then change all newlines to spaces.
265 (subst-char-in-region from (point-max) ?\n ?\ )
266 (if (and nosqueeze (not (eq justify 'full)))
267 nil
268 (canonically-space-region (point) (point-max))
269 (goto-char (point-max))
270 (delete-horizontal-space)
271 (insert-and-inherit " "))
272 (goto-char (point-min))
273
274 ;; This is the actual filling loop.
275 (let ((prefixcol 0) linebeg)
276 (while (not (eobp))
277 (setq linebeg (point))
278 (move-to-column (1+ (current-fill-column)))
279 (if (eobp)
280 (or nosqueeze (delete-horizontal-space))
281 ;; Move back to start of word.
282 (skip-chars-backward "^ \n" linebeg)
283 ;; Don't break after a period followed by just one space.
284 ;; Move back to the previous place to break.
285 ;; The reason is that if a period ends up at the end of a line,
286 ;; further fills will assume it ends a sentence.
287 ;; If we now know it does not end a sentence,
288 ;; avoid putting it at the end of the line.
289 (if sentence-end-double-space
290 (while (and (> (point) (+ linebeg 2))
291 (eq (preceding-char) ?\ )
292 (not (eq (following-char) ?\ ))
293 (eq (char-after (- (point) 2)) ?\.))
294 (forward-char -2)
295 (skip-chars-backward "^ \n" linebeg)))
296 (if (if (zerop prefixcol)
297 (save-excursion
298 (skip-chars-backward " " linebeg)
299 (bolp))
300 (>= prefixcol (current-column)))
301 ;; Keep at least one word even if fill prefix exceeds margin.
302 ;; This handles all but the first line of the paragraph.
303 ;; Meanwhile, don't stop at a period followed by one space.
304 (let ((first t))
305 (move-to-column prefixcol)
306 (while (and (not (eobp))
307 (or first
308 (and (not (bobp))
309 sentence-end-double-space
310 (save-excursion (forward-char -1)
311 (and (looking-at "\\. ")
312 (not (looking-at "\\. ")))))))
313 (skip-chars-forward " ")
314 (skip-chars-forward "^ \n")
315 (setq first nil)))
316 ;; Normally, move back over the single space between the words.
317 (forward-char -1))
318 (if (and fill-prefix (zerop prefixcol)
319 (< (- (point) (point-min)) (length fill-prefix))
320 (string= (buffer-substring (point-min) (point))
321 (substring fill-prefix 0 (- (point) (point-min)))))
322 ;; Keep at least one word even if fill prefix exceeds margin.
323 ;; This handles the first line of the paragraph.
324 ;; Don't stop at a period followed by just one space.
325 (let ((first t))
326 (while (and (not (eobp))
327 (or first
328 (and (not (bobp))
329 sentence-end-double-space
330 (save-excursion (forward-char -1)
331 (and (looking-at "\\. ")
332 (not (looking-at "\\. ")))))))
333 (skip-chars-forward " ")
334 (skip-chars-forward "^ \n")
335 (setq first nil))))
336 ;; Replace whitespace here with one newline, then indent to left
337 ;; margin.
338 (skip-chars-backward " ")
339 (insert ?\n)
340 ;; Give newline the properties of the space(s) it replaces
341 (set-text-properties (1- (point)) (point)
342 (text-properties-at (point)))
343 (indent-to-left-margin)
344 ;; Insert the fill prefix after indentation.
345 ;; Set prefixcol so whitespace in the prefix won't get lost.
346 (and fill-prefix (not (equal fill-prefix ""))
347 (progn
348 (insert-and-inherit fill-prefix)
349 (setq prefixcol (current-column)))))
350 ;; Justify the line just ended, if desired.
351 (if justify
352 (if (eobp)
353 (justify-current-line justify t t)
354 (forward-line -1)
355 (justify-current-line justify nil t)
356 (forward-line 1))))))
357 ;; Leave point after final newline.
358 (goto-char (point-max)))
359 (forward-char 1))))
360
361 (defun fill-paragraph (arg)
362 "Fill paragraph at or after point. Prefix arg means justify as well.
363 If `sentence-end-double-space' is non-nil, then period followed by one
364 space does not end a sentence, so don't break a line there.
365
366 If `fill-paragraph-function' is non-nil, we call it (passing our
367 argument to it), and if it returns non-nil, we simply return its value."
368 (interactive (list (if current-prefix-arg 'full)))
369 (or (and fill-paragraph-function
370 (let ((function fill-paragraph-function)
371 fill-paragraph-function)
372 (funcall function arg)))
373 (let ((before (point)))
374 (save-excursion
375 (forward-paragraph)
376 (or (bolp) (newline 1))
377 (let ((end (point))
378 (beg (progn (backward-paragraph) (point))))
379 (goto-char before)
380 (if use-hard-newlines
381 ;; Can't use fill-region-as-paragraph, since this paragraph may
382 ;; still contain hard newlines. See fill-region.
383 (fill-region beg end arg)
384 (fill-region-as-paragraph beg end arg)))))))
385
386 (defun fill-region (from to &optional justify nosqueeze to-eop)
387 "Fill each of the paragraphs in the region.
388 Prefix arg (non-nil third arg, if called from program) means justify as well.
389
390 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
391 whitespace other than line breaks untouched, and fifth arg TO-EOP
392 non-nil means to keep filling to the end of the paragraph (or next
393 hard newline, if `use-hard-newlines' is on).
394
395 If `sentence-end-double-space' is non-nil, then period followed by one
396 space does not end a sentence, so don't break a line there."
397 (interactive (list (region-beginning) (region-end)
398 (if current-prefix-arg 'full)))
399 (let (end beg)
400 (save-restriction
401 (goto-char (max from to))
402 (if to-eop
403 (progn (skip-chars-backward "\n")
404 (forward-paragraph)))
405 (setq end (point))
406 (goto-char (setq beg (min from to)))
407 (beginning-of-line)
408 (narrow-to-region (point) end)
409 (while (not (eobp))
410 (let ((initial (point))
411 end)
412 ;; If using hard newlines, break at every one for filling
413 ;; purposes rather than using paragraph breaks.
414 (if use-hard-newlines
415 (progn
416 (while (and (setq end (text-property-any (point) (point-max)
417 'hard t))
418 (not (= ?\n (char-after end)))
419 (not (= end (point-max))))
420 (goto-char (1+ end)))
421 (setq end (if end (min (point-max) (1+ end)) (point-max)))
422 (goto-char initial))
423 (forward-paragraph 1)
424 (setq end (point))
425 (forward-paragraph -1))
426 (if (< (point) beg)
427 (goto-char beg))
428 (if (>= (point) initial)
429 (fill-region-as-paragraph (point) end justify nosqueeze)
430 (goto-char end)))))))
431
432 \f
433 (defconst default-justification 'left
434 "*Method of justifying text not otherwise specified.
435 Possible values are `left', `right', `full', `center', or `none'.
436 The requested kind of justification is done whenever lines are filled.
437 The `justification' text-property can locally override this variable.
438 This variable automatically becomes buffer-local when set in any fashion.")
439 (make-variable-buffer-local 'default-justification)
440
441 (defun current-justification ()
442 "How should we justify this line?
443 This returns the value of the text-property `justification',
444 or the variable `default-justification' if there is no text-property.
445 However, it returns nil rather than `none' to mean \"don't justify\"."
446 (let ((j (or (get-text-property
447 ;; Make sure we're looking at paragraph body.
448 (save-excursion (skip-chars-forward " \t")
449 (if (and (eobp) (not (bobp)))
450 (1- (point)) (point)))
451 'justification)
452 default-justification)))
453 (if (eq 'none j)
454 nil
455 j)))
456
457 (defun set-justification (begin end value &optional whole-par)
458 "Set the region's justification style.
459 The kind of justification to use is prompted for.
460 If the mark is not active, this command operates on the current paragraph.
461 If the mark is active, the region is used. However, if the beginning and end
462 of the region are not at paragraph breaks, they are moved to the beginning and
463 end of the paragraphs they are in.
464 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
465 breaks.
466
467 When calling from a program, operates just on region between BEGIN and END,
468 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
469 extended to include entire paragraphs as in the interactive command."
470 (interactive (list (if mark-active (region-beginning) (point))
471 (if mark-active (region-end) (point))
472 (let ((s (completing-read
473 "Set justification to: "
474 '(("left") ("right") ("full")
475 ("center") ("none"))
476 nil t)))
477 (if (equal s "") (error ""))
478 (intern s))
479 t))
480 (save-excursion
481 (save-restriction
482 (if whole-par
483 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
484 (paragraph-ignore-fill-prefix (if use-hard-newlines t
485 paragraph-ignore-fill-prefix)))
486 (goto-char begin)
487 (while (and (bolp) (not (eobp))) (forward-char 1))
488 (backward-paragraph)
489 (setq begin (point))
490 (goto-char end)
491 (skip-chars-backward " \t\n" begin)
492 (forward-paragraph)
493 (setq end (point))))
494
495 (narrow-to-region (point-min) end)
496 (unjustify-region begin (point-max))
497 (put-text-property begin (point-max) 'justification value)
498 (fill-region begin (point-max) nil t))))
499
500 (defun set-justification-none (b e)
501 "Disable automatic filling for paragraphs in the region.
502 If the mark is not active, this applies to the current paragraph."
503 (interactive (list (if mark-active (region-beginning) (point))
504 (if mark-active (region-end) (point))))
505 (set-justification b e 'none t))
506
507 (defun set-justification-left (b e)
508 "Make paragraphs in the region left-justified.
509 This is usually the default, but see the variable `default-justification'.
510 If the mark is not active, this applies to the current paragraph."
511 (interactive (list (if mark-active (region-beginning) (point))
512 (if mark-active (region-end) (point))))
513 (set-justification b e 'left t))
514
515 (defun set-justification-right (b e)
516 "Make paragraphs in the region right-justified:
517 Flush at the right margin and ragged on the left.
518 If the mark is not active, this applies to the current paragraph."
519 (interactive (list (if mark-active (region-beginning) (point))
520 (if mark-active (region-end) (point))))
521 (set-justification b e 'right t))
522
523 (defun set-justification-full (b e)
524 "Make paragraphs in the region fully justified:
525 This makes lines flush on both margins by inserting spaces between words.
526 If the mark is not active, this applies to the current paragraph."
527 (interactive (list (if mark-active (region-beginning) (point))
528 (if mark-active (region-end) (point))))
529 (set-justification b e 'full t))
530
531 (defun set-justification-center (b e)
532 "Make paragraphs in the region centered.
533 If the mark is not active, this applies to the current paragraph."
534 (interactive (list (if mark-active (region-beginning) (point))
535 (if mark-active (region-end) (point))))
536 (set-justification b e 'center t))
537
538 ;; A line has up to six parts:
539 ;;
540 ;; >>> hello.
541 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
542 ;;
543 ;; "Indent-1" is the left-margin indentation; normally it ends at column
544 ;; given by the `current-left-margin' function.
545 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
546 ;; "Indent-2" is added to justify a line if the `current-justification' is
547 ;; `center' or `right'. In `left' and `full' justification regions, any
548 ;; whitespace there is part of the line's text, and should not be changed.
549 ;; Trailing whitespace is not counted as part of the line length when
550 ;; center- or right-justifying.
551 ;;
552 ;; All parts of the line are optional, although the final newline can
553 ;; only be missing on the last line of the buffer.
554
555 (defun justify-current-line (&optional how eop nosqueeze)
556 "Do some kind of justification on this line.
557 Normally does full justification: adds spaces to the line to make it end at
558 the column given by `current-fill-column'.
559 Optional first argument HOW specifies alternate type of justification:
560 it can be `left', `right', `full', `center', or `none'.
561 If HOW is t, will justify however the `current-justification' function says to.
562 If HOW is nil or missing, full justification is done by default.
563 Second arg EOP non-nil means that this is the last line of the paragraph, so
564 it will not be stretched by full justification.
565 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
566 otherwise it is made canonical."
567 (interactive)
568 (if (eq t how) (setq how (or (current-justification) 'none)))
569 (if (null how) (setq how 'full))
570 (or (memq how '(none left)) ; No action required for these.
571 (let ((fc (current-fill-column))
572 (pos (point-marker))
573 fp-end ; point at end of fill prefix
574 beg ; point at beginning of line's text
575 end ; point at end of line's text
576 indent ; column of `beg'
577 endcol ; column of `end'
578 ncols) ; new indent point or offset
579 (end-of-line)
580 ;; Check if this is the last line of the paragraph.
581 (if (and use-hard-newlines (null eop)
582 (get-text-property (point) 'hard))
583 (setq eop t))
584 (skip-chars-backward " \t")
585 ;; Quick exit if it appears to be properly justified already
586 ;; or there is no text.
587 (if (or (bolp)
588 (and (memq how '(full right))
589 (= (current-column) fc)))
590 nil
591 (setq end (point))
592 (beginning-of-line)
593 (skip-chars-forward " \t")
594 ;; Skip over fill-prefix.
595 (if (and fill-prefix
596 (not (string-equal fill-prefix ""))
597 (equal fill-prefix
598 (buffer-substring
599 (point) (min (point-max) (+ (length fill-prefix)
600 (point))))))
601 (forward-char (length fill-prefix))
602 (if (and adaptive-fill-mode
603 (looking-at adaptive-fill-regexp))
604 (goto-char (match-end 0))))
605 (setq fp-end (point))
606 (skip-chars-forward " \t")
607 ;; This is beginning of the line's text.
608 (setq indent (current-column))
609 (setq beg (point))
610 (goto-char end)
611 (setq endcol (current-column))
612
613 ;; HOW can't be null or left--we would have exited already
614 (cond ((eq 'right how)
615 (setq ncols (- fc endcol))
616 (if (< ncols 0)
617 ;; Need to remove some indentation
618 (delete-region
619 (progn (goto-char fp-end)
620 (if (< (current-column) (+ indent ncols))
621 (move-to-column (+ indent ncols) t))
622 (point))
623 (progn (move-to-column indent) (point)))
624 ;; Need to add some
625 (goto-char beg)
626 (indent-to (+ indent ncols))
627 ;; If point was at beginning of text, keep it there.
628 (if (= beg pos)
629 (move-marker pos (point)))))
630
631 ((eq 'center how)
632 ;; Figure out how much indentation is needed
633 (setq ncols (+ (current-left-margin)
634 (/ (- fc (current-left-margin) ;avail. space
635 (- endcol indent)) ;text width
636 2)))
637 (if (< ncols indent)
638 ;; Have too much indentation - remove some
639 (delete-region
640 (progn (goto-char fp-end)
641 (if (< (current-column) ncols)
642 (move-to-column ncols t))
643 (point))
644 (progn (move-to-column indent) (point)))
645 ;; Have too little - add some
646 (goto-char beg)
647 (indent-to ncols)
648 ;; If point was at beginning of text, keep it there.
649 (if (= beg pos)
650 (move-marker pos (point)))))
651
652 ((eq 'full how)
653 ;; Insert extra spaces between words to justify line
654 (save-restriction
655 (narrow-to-region beg end)
656 (or nosqueeze
657 (canonically-space-region beg end))
658 (goto-char (point-max))
659 (setq ncols (- fc endcol))
660 ;; Ncols is number of additional spaces needed
661 (if (> ncols 0)
662 (if (and (not eop)
663 (search-backward " " nil t))
664 (while (> ncols 0)
665 (let ((nmove (+ 3 (random 3))))
666 (while (> nmove 0)
667 (or (search-backward " " nil t)
668 (progn
669 (goto-char (point-max))
670 (search-backward " ")))
671 (skip-chars-backward " ")
672 (setq nmove (1- nmove))))
673 (insert-and-inherit " ")
674 (skip-chars-backward " ")
675 (setq ncols (1- ncols)))))))
676 (t (error "Unknown justification value"))))
677 (goto-char pos)
678 (move-marker pos nil)))
679 nil)
680
681 (defun unjustify-current-line ()
682 "Remove justification whitespace from current line.
683 If the line is centered or right-justified, this function removes any
684 indentation past the left margin. If the line is full-jusitified, it removes
685 extra spaces between words. It does nothing in other justification modes."
686 (let ((justify (current-justification)))
687 (cond ((eq 'left justify) nil)
688 ((eq nil justify) nil)
689 ((eq 'full justify) ; full justify: remove extra spaces
690 (beginning-of-line-text)
691 (canonically-space-region
692 (point) (save-excursion (end-of-line) (point))))
693 ((memq justify '(center right))
694 (save-excursion
695 (move-to-left-margin nil t)
696 ;; Position ourselves after any fill-prefix.
697 (if (and fill-prefix
698 (not (string-equal fill-prefix ""))
699 (equal fill-prefix
700 (buffer-substring
701 (point) (min (point-max) (+ (length fill-prefix)
702 (point))))))
703 (forward-char (length fill-prefix)))
704 (delete-region (point) (progn (skip-chars-forward " \t")
705 (point))))))))
706
707 (defun unjustify-region (&optional begin end)
708 "Remove justification whitespace from region.
709 For centered or right-justified regions, this function removes any indentation
710 past the left margin from each line. For full-jusitified lines, it removes
711 extra spaces between words. It does nothing in other justification modes.
712 Arguments BEGIN and END are optional; default is the whole buffer."
713 (save-excursion
714 (save-restriction
715 (if end (narrow-to-region (point-min) end))
716 (goto-char (or begin (point-min)))
717 (while (not (eobp))
718 (unjustify-current-line)
719 (forward-line 1)))))
720
721 \f
722 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
723 "Fill paragraphs within the region, allowing varying indentation within each.
724 This command divides the region into \"paragraphs\",
725 only at paragraph-separator lines, then fills each paragraph
726 using as the fill prefix the smallest indentation of any line
727 in the paragraph.
728
729 When calling from a program, pass range to fill as first two arguments.
730
731 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
732 JUSTIFY to justify paragraphs (prefix arg),
733 MAIL-FLAG for a mail message, i. e. don't fill header lines."
734 (interactive (list (region-beginning) (region-end)
735 (if current-prefix-arg 'full)))
736 (let ((fill-individual-varying-indent t))
737 (fill-individual-paragraphs min max justifyp mailp)))
738
739 (defun fill-individual-paragraphs (min max &optional justify mailp)
740 "Fill paragraphs of uniform indentation within the region.
741 This command divides the region into \"paragraphs\",
742 treating every change in indentation level as a paragraph boundary,
743 then fills each paragraph using its indentation level as the fill prefix.
744
745 When calling from a program, pass range to fill as first two arguments.
746
747 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
748 JUSTIFY to justify paragraphs (prefix arg),
749 MAIL-FLAG for a mail message, i. e. don't fill header lines."
750 (interactive (list (region-beginning) (region-end)
751 (if current-prefix-arg 'full)))
752 (save-restriction
753 (save-excursion
754 (goto-char min)
755 (beginning-of-line)
756 (narrow-to-region (point) max)
757 (if mailp
758 (while (and (not (eobp))
759 (or (looking-at "[ \t]*[^ \t\n]*:")
760 (looking-at "[ \t]*$")))
761 (if (looking-at "[ \t]*[^ \t\n]*:")
762 (search-forward "\n\n" nil 'move)
763 (forward-line 1))))
764 (narrow-to-region (point) max)
765 ;; Loop over paragraphs.
766 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
767 (beginning-of-line)
768 (let ((start (point))
769 fill-prefix fill-prefix-regexp)
770 ;; Find end of paragraph, and compute the smallest fill-prefix
771 ;; that fits all the lines in this paragraph.
772 (while (progn
773 ;; Update the fill-prefix on the first line
774 ;; and whenever the prefix good so far is too long.
775 (if (not (and fill-prefix
776 (looking-at fill-prefix-regexp)))
777 (setq fill-prefix
778 (if (and adaptive-fill-mode adaptive-fill-regexp
779 (looking-at (concat "\\(" adaptive-fill-regexp "\\)")))
780 (match-string 1)
781 (buffer-substring (point)
782 (save-excursion (skip-chars-forward " \t") (point))))
783 fill-prefix-regexp
784 (regexp-quote fill-prefix)))
785 (forward-line 1)
786 ;; Now stop the loop if end of paragraph.
787 (and (not (eobp))
788 (if fill-individual-varying-indent
789 ;; If this line is a separator line, with or
790 ;; without prefix, end the paragraph.
791 (and
792 (not (looking-at paragraph-separate))
793 (save-excursion
794 (not (and (looking-at fill-prefix-regexp)
795 (progn (forward-char (length fill-prefix))
796 (looking-at paragraph-separate))))))
797 ;; If this line has more or less indent
798 ;; than the fill prefix wants, end the paragraph.
799 (and (looking-at fill-prefix-regexp)
800 (save-excursion
801 (not (progn (forward-char (length fill-prefix))
802 (or (looking-at paragraph-separate)
803 (looking-at paragraph-start))))))))))
804 ;; Fill this paragraph, but don't add a newline at the end.
805 (let ((had-newline (bolp)))
806 (fill-region-as-paragraph start (point) justify)
807 (or had-newline (delete-char -1))))))))
808
809 ;;; fill.el ends here