1 ;;; fill.el --- fill commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
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)
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.
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.
25 ;; All the commands for filling text. These are documented in the Emacs
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.")
37 (defconst sentence-end-double-space t
38 "*Non-nil means a single space does not end a sentence.")
40 (defconst colon-double-space nil
41 "*Non-nil means put two spaces after a colon when filling.")
43 (defvar fill-paragraph-function nil
44 "Mode-specific function to fill a paragraph.")
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."
51 (setq fill-prefix (buffer-substring
52 (save-excursion (move-to-left-margin) (point))
54 (if (equal fill-prefix "")
55 (setq fill-prefix nil))
57 (message "fill-prefix: \"%s\"" fill-prefix)
58 (message "fill-prefix cancelled")))
60 (defconst adaptive-fill-mode t
61 "*Non-nil means determine a paragraph's fill prefix from its text.")
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.")
70 (defvar 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.")
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'.
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."
84 (let* ((here (progn (beginning-of-line) (point)))
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))
98 (max here-col fill-col)))))
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."
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".
111 (subst-char-in-region beg end ?\t ?\ )
112 (while (and (< (point) end)
113 (re-search-forward " *" end t))
115 (+ (match-beginning 0)
116 ;; Determine number of spaces to leave:
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)
126 ;; Make sure sentences ending at end of line get an extra space.
127 ;; loses on split abbrevs ("Mr.\nSmith")
129 (while (and (< (point) end)
130 (re-search-forward "[.?!][])}\"']*$" end t))
131 (insert-and-inherit ? ))))
133 (defun fill-context-prefix (from to)
134 "Compute a fill prefix from the text between FROM and TO.
135 This uses the variables `adapive-fill-prefix' and `adaptive-fill-function'."
138 (if (eolp) (forward-line 1))
139 ;; Move to the second line unless there is just one.
140 (let ((firstline (point)))
143 (goto-char firstline)))
144 (move-to-left-margin)
145 (let ((start (point))
146 (eol (save-excursion (end-of-line) (point))))
147 (if (not (looking-at paragraph-start))
148 (cond ((looking-at adaptive-fill-regexp)
149 (buffer-substring-no-properties start (match-end 0)))
150 (t (funcall adaptive-fill-function)))))))
152 (defun fill-region-as-paragraph (from to &optional justify nosqueeze)
153 "Fill the region as one paragraph.
154 Removes any paragraph breaks in the region and extra newlines at the end,
155 indents and fills lines between the margins given by the
156 `current-left-margin' and `current-fill-column' functions.
158 Normally performs justification according to the `current-justification'
159 function, but with a prefix arg, does full justification instead.
161 From a program, optional third arg JUSTIFY can specify any type of
162 justification, and fourth arg NOSQUEEZE non-nil means not to make spaces
163 between words canonical before filling.
165 If `sentence-end-double-space' is non-nil, then period followed by one
166 space does not end a sentence, so don't break a line there."
167 (interactive (list (region-beginning) (region-end)
168 (if current-prefix-arg 'full)))
169 ;; Arrange for undoing the fill to restore point.
170 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
171 (setq buffer-undo-list (cons (point) buffer-undo-list)))
173 ;; Make sure "to" is the endpoint. Make sure that we end up there.
174 (goto-char (min from to))
175 (setq to (max from to))
178 ;; Delete all but one soft newline at end of region.
181 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
183 (not (and use-hard-newlines
184 (get-text-property (1- (point)) 'hard))))
185 (delete-backward-char 1)
188 ;; If there was no newline, create one.
189 (if (and (not oneleft) (> (point) from))
190 (save-excursion (newline))))
193 ;; Ignore blank lines at beginning of region.
195 (skip-chars-forward " \t\n")
200 nil ; There is no paragraph at all.
202 (or justify (setq justify (current-justification)))
204 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
205 (let ((fill-prefix fill-prefix))
206 ;; Figure out how this paragraph is indented, if desired.
207 (if (and adaptive-fill-mode
208 (or (null fill-prefix) (string= fill-prefix "")))
209 (setq fill-prefix (fill-context-prefix from to)))
214 (narrow-to-region (point) to)
216 (if (not justify) ; filling disabled: just check indentation
220 (if (and (not (eolp))
221 (< (current-indentation) (current-left-margin)))
222 (indent-to-left-margin))
225 (if use-hard-newlines
226 (remove-text-properties from (point-max) '(hard nil)))
227 ;; Make sure first line is indented (at least) to left margin...
228 (if (or (memq justify '(right center))
229 (< (current-indentation) (current-left-margin)))
230 (indent-to-left-margin))
231 ;; Delete the fill prefix from every line except the first.
232 ;; The first line may not even have a fill prefix.
234 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
236 (regexp-quote fill-prefix)
240 (if (>= (+ (current-left-margin) (length fill-prefix))
241 (current-fill-column))
242 (error "fill-prefix too long for specified width"))
246 (if (looking-at fpre)
247 (delete-region (point) (match-end 0)))
250 (and (looking-at fpre) (goto-char (match-end 0)))
251 (setq from (point)))))
252 ;; Remove indentation from lines other than the first.
253 (beginning-of-line 2)
254 (indent-region (point) (point-max) 0)
257 ;; FROM, and point, are now before the text to fill,
258 ;; but after any fill prefix on the first line.
260 ;; Make sure sentences ending at end of line get an extra space.
261 ;; loses on split abbrevs ("Mr.\nSmith")
262 (while (re-search-forward "[.?!][])}\"']*$" nil t)
263 (or (eobp) (insert-and-inherit ?\ )))
265 (skip-chars-forward " \t")
266 ;; Then change all newlines to spaces.
267 (subst-char-in-region from (point-max) ?\n ?\ )
268 (if (and nosqueeze (not (eq justify 'full)))
270 (canonically-space-region (point) (point-max))
271 (goto-char (point-max))
272 (delete-horizontal-space)
273 (insert-and-inherit " "))
274 (goto-char (point-min))
276 ;; This is the actual filling loop.
277 (let ((prefixcol 0) linebeg)
279 (setq linebeg (point))
280 (move-to-column (1+ (current-fill-column)))
282 (or nosqueeze (delete-horizontal-space))
283 ;; Move back to start of word.
284 (skip-chars-backward "^ \n" linebeg)
285 ;; Don't break after a period followed by just one space.
286 ;; Move back to the previous place to break.
287 ;; The reason is that if a period ends up at the end of a line,
288 ;; further fills will assume it ends a sentence.
289 ;; If we now know it does not end a sentence,
290 ;; avoid putting it at the end of the line.
291 (if sentence-end-double-space
292 (while (and (> (point) (+ linebeg 2))
293 (eq (preceding-char) ?\ )
294 (not (eq (following-char) ?\ ))
295 (eq (char-after (- (point) 2)) ?\.))
297 (skip-chars-backward "^ \n" linebeg)))
298 (if (if (zerop prefixcol)
300 (skip-chars-backward " " linebeg)
302 (>= prefixcol (current-column)))
303 ;; Keep at least one word even if fill prefix exceeds margin.
304 ;; This handles all but the first line of the paragraph.
305 ;; Meanwhile, don't stop at a period followed by one space.
307 (move-to-column prefixcol)
308 (while (and (not (eobp))
311 sentence-end-double-space
312 (save-excursion (forward-char -1)
313 (and (looking-at "\\. ")
314 (not (looking-at "\\. ")))))))
315 (skip-chars-forward " ")
316 (skip-chars-forward "^ \n")
318 ;; Normally, move back over the single space between the words.
320 (if (and fill-prefix (zerop prefixcol)
321 (< (- (point) (point-min)) (length fill-prefix))
322 (string= (buffer-substring (point-min) (point))
323 (substring fill-prefix 0 (- (point) (point-min)))))
324 ;; Keep at least one word even if fill prefix exceeds margin.
325 ;; This handles the first line of the paragraph.
326 ;; Don't stop at a period followed by just one space.
328 (while (and (not (eobp))
331 sentence-end-double-space
332 (save-excursion (forward-char -1)
333 (and (looking-at "\\. ")
334 (not (looking-at "\\. ")))))))
335 (skip-chars-forward " ")
336 (skip-chars-forward "^ \n")
338 ;; Replace whitespace here with one newline, then indent to left
340 (skip-chars-backward " ")
342 ;; Give newline the properties of the space(s) it replaces
343 (set-text-properties (1- (point)) (point)
344 (text-properties-at (point)))
345 (indent-to-left-margin)
346 ;; Insert the fill prefix after indentation.
347 ;; Set prefixcol so whitespace in the prefix won't get lost.
348 (and fill-prefix (not (equal fill-prefix ""))
350 (insert-and-inherit fill-prefix)
351 (setq prefixcol (current-column)))))
352 ;; Justify the line just ended, if desired.
355 (justify-current-line justify t t)
357 (justify-current-line justify nil t)
358 (forward-line 1))))))
359 ;; Leave point after final newline.
360 (goto-char (point-max)))
363 (defun fill-paragraph (arg)
364 "Fill paragraph at or after point. Prefix arg means justify as well.
365 If `sentence-end-double-space' is non-nil, then period followed by one
366 space does not end a sentence, so don't break a line there.
368 If `fill-paragraph-function' is non-nil, we call it (passing our
369 argument to it), and if it returns non-nil, we simply return its value."
370 (interactive (list (if current-prefix-arg 'full)))
371 (or (and fill-paragraph-function
372 (let ((function fill-paragraph-function)
373 fill-paragraph-function)
374 (funcall function arg)))
375 (let ((before (point)))
378 (or (bolp) (newline 1))
380 (beg (progn (backward-paragraph) (point))))
382 (if use-hard-newlines
383 ;; Can't use fill-region-as-paragraph, since this paragraph may
384 ;; still contain hard newlines. See fill-region.
385 (fill-region beg end arg)
386 (fill-region-as-paragraph beg end arg)))))))
388 (defun fill-region (from to &optional justify nosqueeze to-eop)
389 "Fill each of the paragraphs in the region.
390 Prefix arg (non-nil third arg, if called from program) means justify as well.
392 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
393 whitespace other than line breaks untouched, and fifth arg TO-EOP
394 non-nil means to keep filling to the end of the paragraph (or next
395 hard newline, if `use-hard-newlines' is on).
397 If `sentence-end-double-space' is non-nil, then period followed by one
398 space does not end a sentence, so don't break a line there."
399 (interactive (list (region-beginning) (region-end)
400 (if current-prefix-arg 'full)))
403 (goto-char (max from to))
405 (progn (skip-chars-backward "\n")
406 (forward-paragraph)))
408 (goto-char (setq beg (min from to)))
410 (narrow-to-region (point) end)
412 (let ((initial (point))
414 ;; If using hard newlines, break at every one for filling
415 ;; purposes rather than using paragraph breaks.
416 (if use-hard-newlines
418 (while (and (setq end (text-property-any (point) (point-max)
420 (not (= ?\n (char-after end)))
421 (not (= end (point-max))))
422 (goto-char (1+ end)))
423 (setq end (if end (min (point-max) (1+ end)) (point-max)))
425 (forward-paragraph 1)
427 (forward-paragraph -1))
430 (if (>= (point) initial)
431 (fill-region-as-paragraph (point) end justify nosqueeze)
432 (goto-char end)))))))
435 (defconst default-justification 'left
436 "*Method of justifying text not otherwise specified.
437 Possible values are `left', `right', `full', `center', or `none'.
438 The requested kind of justification is done whenever lines are filled.
439 The `justification' text-property can locally override this variable.
440 This variable automatically becomes buffer-local when set in any fashion.")
441 (make-variable-buffer-local 'default-justification)
443 (defun current-justification ()
444 "How should we justify this line?
445 This returns the value of the text-property `justification',
446 or the variable `default-justification' if there is no text-property.
447 However, it returns nil rather than `none' to mean \"don't justify\"."
448 (let ((j (or (get-text-property
449 ;; Make sure we're looking at paragraph body.
450 (save-excursion (skip-chars-forward " \t")
451 (if (and (eobp) (not (bobp)))
452 (1- (point)) (point)))
454 default-justification)))
459 (defun set-justification (begin end value &optional whole-par)
460 "Set the region's justification style.
461 The kind of justification to use is prompted for.
462 If the mark is not active, this command operates on the current paragraph.
463 If the mark is active, the region is used. However, if the beginning and end
464 of the region are not at paragraph breaks, they are moved to the beginning and
465 end of the paragraphs they are in.
466 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
469 When calling from a program, operates just on region between BEGIN and END,
470 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
471 extended to include entire paragraphs as in the interactive command."
472 (interactive (list (if mark-active (region-beginning) (point))
473 (if mark-active (region-end) (point))
474 (let ((s (completing-read
475 "Set justification to: "
476 '(("left") ("right") ("full")
479 (if (equal s "") (error ""))
485 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
486 (paragraph-ignore-fill-prefix (if use-hard-newlines t
487 paragraph-ignore-fill-prefix)))
489 (while (and (bolp) (not (eobp))) (forward-char 1))
493 (skip-chars-backward " \t\n" begin)
497 (narrow-to-region (point-min) end)
498 (unjustify-region begin (point-max))
499 (put-text-property begin (point-max) 'justification value)
500 (fill-region begin (point-max) nil t))))
502 (defun set-justification-none (b e)
503 "Disable automatic filling for paragraphs in the region.
504 If the mark is not active, this applies to the current paragraph."
505 (interactive (list (if mark-active (region-beginning) (point))
506 (if mark-active (region-end) (point))))
507 (set-justification b e 'none t))
509 (defun set-justification-left (b e)
510 "Make paragraphs in the region left-justified.
511 This is usually the default, but see the variable `default-justification'.
512 If the mark is not active, this applies to the current paragraph."
513 (interactive (list (if mark-active (region-beginning) (point))
514 (if mark-active (region-end) (point))))
515 (set-justification b e 'left t))
517 (defun set-justification-right (b e)
518 "Make paragraphs in the region right-justified:
519 Flush at the right margin and ragged on the left.
520 If the mark is not active, this applies to the current paragraph."
521 (interactive (list (if mark-active (region-beginning) (point))
522 (if mark-active (region-end) (point))))
523 (set-justification b e 'right t))
525 (defun set-justification-full (b e)
526 "Make paragraphs in the region fully justified:
527 This makes lines flush on both margins by inserting spaces between words.
528 If the mark is not active, this applies to the current paragraph."
529 (interactive (list (if mark-active (region-beginning) (point))
530 (if mark-active (region-end) (point))))
531 (set-justification b e 'full t))
533 (defun set-justification-center (b e)
534 "Make paragraphs in the region centered.
535 If the mark is not active, this applies to the current paragraph."
536 (interactive (list (if mark-active (region-beginning) (point))
537 (if mark-active (region-end) (point))))
538 (set-justification b e 'center t))
540 ;; A line has up to six parts:
543 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
545 ;; "Indent-1" is the left-margin indentation; normally it ends at column
546 ;; given by the `current-left-margin' function.
547 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
548 ;; "Indent-2" is added to justify a line if the `current-justification' is
549 ;; `center' or `right'. In `left' and `full' justification regions, any
550 ;; whitespace there is part of the line's text, and should not be changed.
551 ;; Trailing whitespace is not counted as part of the line length when
552 ;; center- or right-justifying.
554 ;; All parts of the line are optional, although the final newline can
555 ;; only be missing on the last line of the buffer.
557 (defun justify-current-line (&optional how eop nosqueeze)
558 "Do some kind of justification on this line.
559 Normally does full justification: adds spaces to the line to make it end at
560 the column given by `current-fill-column'.
561 Optional first argument HOW specifies alternate type of justification:
562 it can be `left', `right', `full', `center', or `none'.
563 If HOW is t, will justify however the `current-justification' function says to.
564 If HOW is nil or missing, full justification is done by default.
565 Second arg EOP non-nil means that this is the last line of the paragraph, so
566 it will not be stretched by full justification.
567 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
568 otherwise it is made canonical."
570 (if (eq t how) (setq how (or (current-justification) 'none))
571 (if (null how) (setq how 'full)
572 (or (memq how '(none left right center))
574 (or (memq how '(none left)) ; No action required for these.
575 (let ((fc (current-fill-column))
577 fp-end ; point at end of fill prefix
578 beg ; point at beginning of line's text
579 end ; point at end of line's text
580 indent ; column of `beg'
581 endcol ; column of `end'
582 ncols) ; new indent point or offset
584 ;; Check if this is the last line of the paragraph.
585 (if (and use-hard-newlines (null eop)
586 (get-text-property (point) 'hard))
588 (skip-chars-backward " \t")
589 ;; Quick exit if it appears to be properly justified already
590 ;; or there is no text.
592 (and (memq how '(full right))
593 (= (current-column) fc)))
597 (skip-chars-forward " \t")
598 ;; Skip over fill-prefix.
600 (not (string-equal fill-prefix ""))
603 (point) (min (point-max) (+ (length fill-prefix)
605 (forward-char (length fill-prefix))
606 (if (and adaptive-fill-mode
607 (looking-at adaptive-fill-regexp))
608 (goto-char (match-end 0))))
609 (setq fp-end (point))
610 (skip-chars-forward " \t")
611 ;; This is beginning of the line's text.
612 (setq indent (current-column))
615 (setq endcol (current-column))
617 ;; HOW can't be null or left--we would have exited already
618 (cond ((eq 'right how)
619 (setq ncols (- fc endcol))
621 ;; Need to remove some indentation
623 (progn (goto-char fp-end)
624 (if (< (current-column) (+ indent ncols))
625 (move-to-column (+ indent ncols) t))
627 (progn (move-to-column indent) (point)))
630 (indent-to (+ indent ncols))
631 ;; If point was at beginning of text, keep it there.
633 (move-marker pos (point)))))
636 ;; Figure out how much indentation is needed
637 (setq ncols (+ (current-left-margin)
638 (/ (- fc (current-left-margin) ;avail. space
639 (- endcol indent)) ;text width
642 ;; Have too much indentation - remove some
644 (progn (goto-char fp-end)
645 (if (< (current-column) ncols)
646 (move-to-column ncols t))
648 (progn (move-to-column indent) (point)))
649 ;; Have too little - add some
652 ;; If point was at beginning of text, keep it there.
654 (move-marker pos (point)))))
657 ;; Insert extra spaces between words to justify line
659 (narrow-to-region beg end)
661 (canonically-space-region beg end))
662 (goto-char (point-max))
663 (setq ncols (- fc endcol))
664 ;; Ncols is number of additional spaces needed
667 (search-backward " " nil t))
669 (let ((nmove (+ 3 (random 3))))
671 (or (search-backward " " nil t)
673 (goto-char (point-max))
674 (search-backward " ")))
675 (skip-chars-backward " ")
676 (setq nmove (1- nmove))))
677 (insert-and-inherit " ")
678 (skip-chars-backward " ")
679 (setq ncols (1- ncols)))))))
680 (t (error "Unknown justification value"))))
682 (move-marker pos nil)))
685 (defun unjustify-current-line ()
686 "Remove justification whitespace from current line.
687 If the line is centered or right-justified, this function removes any
688 indentation past the left margin. If the line is full-jusitified, it removes
689 extra spaces between words. It does nothing in other justification modes."
690 (let ((justify (current-justification)))
691 (cond ((eq 'left justify) nil)
692 ((eq nil justify) nil)
693 ((eq 'full justify) ; full justify: remove extra spaces
694 (beginning-of-line-text)
695 (canonically-space-region
696 (point) (save-excursion (end-of-line) (point))))
697 ((memq justify '(center right))
699 (move-to-left-margin nil t)
700 ;; Position ourselves after any fill-prefix.
702 (not (string-equal fill-prefix ""))
705 (point) (min (point-max) (+ (length fill-prefix)
707 (forward-char (length fill-prefix)))
708 (delete-region (point) (progn (skip-chars-forward " \t")
711 (defun unjustify-region (&optional begin end)
712 "Remove justification whitespace from region.
713 For centered or right-justified regions, this function removes any indentation
714 past the left margin from each line. For full-jusitified lines, it removes
715 extra spaces between words. It does nothing in other justification modes.
716 Arguments BEGIN and END are optional; default is the whole buffer."
719 (if end (narrow-to-region (point-min) end))
720 (goto-char (or begin (point-min)))
722 (unjustify-current-line)
726 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
727 "Fill paragraphs within the region, allowing varying indentation within each.
728 This command divides the region into \"paragraphs\",
729 only at paragraph-separator lines, then fills each paragraph
730 using as the fill prefix the smallest indentation of any line
733 When calling from a program, pass range to fill as first two arguments.
735 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
736 JUSTIFY to justify paragraphs (prefix arg),
737 MAIL-FLAG for a mail message, i. e. don't fill header lines."
738 (interactive (list (region-beginning) (region-end)
739 (if current-prefix-arg 'full)))
740 (let ((fill-individual-varying-indent t))
741 (fill-individual-paragraphs min max justifyp mailp)))
743 (defun fill-individual-paragraphs (min max &optional justify mailp)
744 "Fill paragraphs of uniform indentation within the region.
745 This command divides the region into \"paragraphs\",
746 treating every change in indentation level as a paragraph boundary,
747 then fills each paragraph using its indentation level as the fill prefix.
749 When calling from a program, pass range to fill as first two arguments.
751 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
752 JUSTIFY to justify paragraphs (prefix arg),
753 MAIL-FLAG for a mail message, i. e. don't fill header lines."
754 (interactive (list (region-beginning) (region-end)
755 (if current-prefix-arg 'full)))
760 (narrow-to-region (point) max)
762 (while (and (not (eobp))
763 (or (looking-at "[ \t]*[^ \t\n]+:")
764 (looking-at "[ \t]*$")))
765 (if (looking-at "[ \t]*[^ \t\n]+:")
766 (search-forward "\n\n" nil 'move)
768 (narrow-to-region (point) max)
769 ;; Loop over paragraphs.
770 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
771 (move-to-left-margin)
772 (let ((start (point))
773 fill-prefix fill-prefix-regexp)
774 ;; Find end of paragraph, and compute the smallest fill-prefix
775 ;; that fits all the lines in this paragraph.
777 ;; Update the fill-prefix on the first line
778 ;; and whenever the prefix good so far is too long.
779 (if (not (and fill-prefix
780 (looking-at fill-prefix-regexp)))
782 (if (and adaptive-fill-mode adaptive-fill-regexp
783 (looking-at adaptive-fill-regexp))
787 (save-excursion (skip-chars-forward " \t")
789 fill-prefix-regexp (regexp-quote fill-prefix)))
790 (move-to-left-margin 1)
791 ;; Now stop the loop if end of paragraph.
793 (if fill-individual-varying-indent
794 ;; If this line is a separator line, with or
795 ;; without prefix, end the paragraph.
797 (not (looking-at paragraph-separate))
799 (not (and (looking-at fill-prefix-regexp)
800 (progn (forward-char (length fill-prefix))
801 (looking-at paragraph-separate))))))
802 ;; If this line has more or less indent
803 ;; than the fill prefix wants, end the paragraph.
804 (and (looking-at fill-prefix-regexp)
806 (not (progn (forward-char (length fill-prefix))
807 (or (looking-at paragraph-separate)
808 (looking-at paragraph-start))))))))))
809 ;; Fill this paragraph, but don't add a newline at the end.
810 (let ((had-newline (bolp)))
811 (fill-region-as-paragraph start (point) justify)
812 (or had-newline (delete-char -1))))))))
814 ;;; fill.el ends here