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, or nil if there is none.
45 If the function returns nil, then `fill-paragraph' does its normal work.")
47 (defun set-fill-prefix ()
48 "Set the fill prefix to the current line up to point.
49 Filling expects lines to start with the fill prefix and
50 reinserts the fill prefix in each resulting line."
52 (setq fill-prefix (buffer-substring
53 (save-excursion (move-to-left-margin) (point))
55 (if (equal fill-prefix "")
56 (setq fill-prefix nil))
58 (message "fill-prefix: \"%s\"" fill-prefix)
59 (message "fill-prefix cancelled")))
61 (defconst adaptive-fill-mode t
62 "*Non-nil means determine a paragraph's fill prefix from its text.")
64 (defconst adaptive-fill-regexp "[ \t]*\\([#;>*]+ +\\)?"
65 "*Regexp to match text at start of line that constitutes indentation.
66 If Adaptive Fill mode is enabled, whatever text matches this pattern
67 on the second line of a paragraph is used as the standard indentation
68 for the paragraph. If the paragraph has just one line, the indentation
69 is taken from that line.")
71 (defvar adaptive-fill-function nil
72 "*Function to call to choose a fill prefix for a paragraph.
73 This function is used when `adaptive-fill-regexp' does not match.")
75 (defun current-fill-column ()
76 "Return the fill-column to use for this line.
77 The fill-column to use for a buffer is stored in the variable `fill-column',
78 but can be locally modified by the `right-margin' text property, which is
79 subtracted from `fill-column'.
81 The fill column to use for a line is the first column at which the column
82 number equals or exceeds the local fill-column - right-margin difference."
85 (let* ((here (progn (beginning-of-line) (point)))
87 (eol (progn (end-of-line) (point)))
88 margin fill-col change col)
89 ;; Look separately at each region of line with a different right-margin.
90 (while (and (setq margin (get-text-property here 'right-margin)
91 fill-col (- fill-column (or margin 0))
92 change (text-property-not-all
93 here eol 'right-margin margin))
94 (progn (goto-char (1- change))
95 (setq col (current-column))
99 (max here-col fill-col)))))
101 (defun canonically-space-region (beg end)
102 "Remove extra spaces between words in region.
103 Puts one space between words in region; two between sentences.
104 Remove indentation from each line."
108 ;; Nuke tabs; they get screwed up in a fill.
109 ;; This is quick, but loses when a tab follows the end of a sentence.
110 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
112 (subst-char-in-region beg end ?\t ?\ )
113 (while (and (< (point) end)
114 (re-search-forward " *" end t))
116 (+ (match-beginning 0)
117 ;; Determine number of spaces to leave:
119 (skip-chars-backward " ]})\"'")
120 (cond ((and sentence-end-double-space
121 (memq (preceding-char) '(?. ?? ?!))) 2)
122 ((and colon-double-space
123 (= (preceding-char) ?:)) 2)
124 ((char-equal (preceding-char) ?\n) 0)
127 ;; Make sure sentences ending at end of line get an extra space.
128 ;; loses on split abbrevs ("Mr.\nSmith")
130 (while (and (< (point) end)
131 (re-search-forward "[.?!][])}\"']*$" end t))
132 (insert-and-inherit ? ))))
134 (defun fill-context-prefix (from to &optional first-line-regexp)
135 "Compute a fill prefix from the text between FROM and TO.
136 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
137 If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
138 first line, insist it must match FIRST-LINE-REGEXP."
141 (if (eolp) (forward-line 1))
142 ;; Move to the second line unless there is just one.
143 (let ((firstline (point))
144 ;; Non-nil if we are on the second line.
149 (goto-char firstline)
151 (move-to-left-margin)
152 (let ((start (point))
153 (eol (save-excursion (end-of-line) (point))))
155 (if (not (looking-at paragraph-start))
156 (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
157 (buffer-substring-no-properties start (match-end 0)))
158 (adaptive-fill-function (funcall adaptive-fill-function)))))
161 (null first-line-regexp)
162 (string-match first-line-regexp result))
165 (defun fill-region-as-paragraph (from to &optional justify nosqueeze)
166 "Fill the region as one paragraph.
167 It removes any paragraph breaks in the region and extra newlines at the end,
168 indents and fills lines between the margins given by the
169 `current-left-margin' and `current-fill-column' functions.
170 It leaves point at the beginning of the line following the paragraph.
172 Normally performs justification according to the `current-justification'
173 function, but with a prefix arg, does full justification instead.
175 From a program, optional third arg JUSTIFY can specify any type of
176 justification, and fourth arg NOSQUEEZE non-nil means not to make spaces
177 between words canonical before filling.
179 If `sentence-end-double-space' is non-nil, then period followed by one
180 space does not end a sentence, so don't break a line there."
181 (interactive (list (region-beginning) (region-end)
182 (if current-prefix-arg 'full)))
183 ;; Arrange for undoing the fill to restore point.
184 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
185 (setq buffer-undo-list (cons (point) buffer-undo-list)))
187 ;; Make sure "to" is the endpoint.
188 (goto-char (min from to))
189 (setq to (max from to))
190 ;; Ignore blank lines at beginning of region.
191 (skip-chars-forward " \t\n")
193 (let ((from-plus-indent (point))
199 ;; Delete all but one soft newline at end of region.
201 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
203 (not (and use-hard-newlines
204 (get-text-property (1- (point)) 'hard))))
205 (delete-backward-char 1)
210 ;; If there was no newline, and there is text in the paragraph, then
212 (if (and (not oneleft) (> to from-plus-indent))
214 (goto-char from-plus-indent))
216 (if (not (> to (point)))
217 nil ; There is no paragraph, only whitespace: exit now.
219 (or justify (setq justify (current-justification)))
221 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
222 (let ((fill-prefix fill-prefix))
223 ;; Figure out how this paragraph is indented, if desired.
224 (if (and adaptive-fill-mode
225 (or (null fill-prefix) (string= fill-prefix "")))
226 (setq fill-prefix (fill-context-prefix from to)))
231 (narrow-to-region (point) to)
233 (if (not justify) ; filling disabled: just check indentation
237 (if (and (not (eolp))
238 (< (current-indentation) (current-left-margin)))
239 (indent-to-left-margin))
242 (if use-hard-newlines
243 (remove-text-properties from (point-max) '(hard nil)))
244 ;; Make sure first line is indented (at least) to left margin...
245 (if (or (memq justify '(right center))
246 (< (current-indentation) (current-left-margin)))
247 (indent-to-left-margin))
248 ;; Delete the fill prefix from every line except the first.
249 ;; The first line may not even have a fill prefix.
251 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
253 (regexp-quote fill-prefix)
257 (if (>= (+ (current-left-margin) (length fill-prefix))
258 (current-fill-column))
259 (error "fill-prefix too long for specified width"))
263 (if (looking-at fpre)
264 (delete-region (point) (match-end 0)))
267 (and (looking-at fpre) (goto-char (match-end 0)))
268 (setq from (point)))))
269 ;; Remove indentation from lines other than the first.
270 (beginning-of-line 2)
271 (indent-region (point) (point-max) 0)
274 ;; FROM, and point, are now before the text to fill,
275 ;; but after any fill prefix on the first line.
277 ;; Make sure sentences ending at end of line get an extra space.
278 ;; loses on split abbrevs ("Mr.\nSmith")
279 (while (re-search-forward "[.?!][])}\"']*$" nil t)
280 (or (eobp) (insert-and-inherit ?\ )))
282 (skip-chars-forward " \t")
283 ;; Then change all newlines to spaces.
284 (subst-char-in-region from (point-max) ?\n ?\ )
285 (if (and nosqueeze (not (eq justify 'full)))
287 (canonically-space-region (point) (point-max))
288 (goto-char (point-max))
289 (delete-horizontal-space)
290 (insert-and-inherit " "))
291 (goto-char (point-min))
293 ;; This is the actual filling loop.
294 (let ((prefixcol 0) linebeg)
296 (setq linebeg (point))
297 (move-to-column (1+ (current-fill-column)))
299 (or nosqueeze (delete-horizontal-space))
300 ;; Move back to start of word.
301 (skip-chars-backward "^ \n" linebeg)
302 ;; Don't break after a period followed by just one space.
303 ;; Move back to the previous place to break.
304 ;; The reason is that if a period ends up at the end of a line,
305 ;; further fills will assume it ends a sentence.
306 ;; If we now know it does not end a sentence,
307 ;; avoid putting it at the end of the line.
308 (if sentence-end-double-space
309 (while (and (> (point) (+ linebeg 2))
310 (eq (preceding-char) ?\ )
311 (not (eq (following-char) ?\ ))
312 (eq (char-after (- (point) 2)) ?\.))
314 (skip-chars-backward "^ \n" linebeg)))
315 ;; If the left margin and fill prefix by themselves
316 ;; pass the fill-column, keep at least one word.
317 ;; This handles ALL BUT the first line of the paragraph.
318 (if (if (zerop prefixcol)
320 (skip-chars-backward " \t" linebeg)
322 (>= prefixcol (current-column)))
323 ;; Ok, skip at least one word.
324 ;; Meanwhile, don't stop at a period followed by one space.
326 (move-to-column prefixcol)
327 (while (and (not (eobp))
330 sentence-end-double-space
331 (save-excursion (forward-char -1)
332 (and (looking-at "\\. ")
333 (not (looking-at "\\. ")))))))
334 (skip-chars-forward " \t")
335 (skip-chars-forward "^ \n\t")
337 ;; Normally, move back over the single space between the words.
339 ;; If the left margin and fill prefix by themselves
340 ;; pass the fill-column, keep at least one word.
341 ;; This handles the first line of the paragraph.
342 (if (and (zerop prefixcol)
343 (let ((fill-point (point)) nchars)
345 (move-to-left-margin)
346 (setq nchars (- fill-point (point)))
349 (< nchars (length fill-prefix))
350 (string= (buffer-substring (point) fill-point)
351 (substring fill-prefix 0 nchars)))))))
352 ;; Ok, skip at least one word. But
353 ;; don't stop at a period followed by just one space.
355 (while (and (not (eobp))
358 sentence-end-double-space
359 (save-excursion (forward-char -1)
360 (and (looking-at "\\. ")
361 (not (looking-at "\\. ")))))))
362 (skip-chars-forward " \t")
363 (skip-chars-forward "^ \t\n")
365 ;; Replace whitespace here with one newline, then indent to left
367 (skip-chars-backward " \t")
369 ;; Give newline the properties of the space(s) it replaces
370 (set-text-properties (1- (point)) (point)
371 (text-properties-at (point)))
372 (indent-to-left-margin)
373 ;; Insert the fill prefix after indentation.
374 ;; Set prefixcol so whitespace in the prefix won't get lost.
375 (and fill-prefix (not (equal fill-prefix ""))
377 (insert-and-inherit fill-prefix)
378 (setq prefixcol (current-column)))))
379 ;; Justify the line just ended, if desired.
382 (justify-current-line justify t t)
384 (justify-current-line justify nil t)
385 (forward-line 1))))))
386 ;; Leave point after final newline.
387 (goto-char (point-max)))
390 (defun fill-paragraph (arg)
391 "Fill paragraph at or after point. Prefix arg means justify as well.
392 If `sentence-end-double-space' is non-nil, then period followed by one
393 space does not end a sentence, so don't break a line there.
395 If `fill-paragraph-function' is non-nil, we call it (passing our
396 argument to it), and if it returns non-nil, we simply return its value."
397 (interactive (list (if current-prefix-arg 'full)))
398 (or (and fill-paragraph-function
399 (let ((function fill-paragraph-function)
400 fill-paragraph-function)
401 (funcall function arg)))
402 (let ((before (point)))
405 (or (bolp) (newline 1))
407 (beg (progn (backward-paragraph) (point))))
409 (if use-hard-newlines
410 ;; Can't use fill-region-as-paragraph, since this paragraph may
411 ;; still contain hard newlines. See fill-region.
412 (fill-region beg end arg)
413 (fill-region-as-paragraph beg end arg)))))))
415 (defun fill-region (from to &optional justify nosqueeze to-eop)
416 "Fill each of the paragraphs in the region.
417 Prefix arg (non-nil third arg, if called from program) means justify as well.
419 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
420 whitespace other than line breaks untouched, and fifth arg TO-EOP
421 non-nil means to keep filling to the end of the paragraph (or next
422 hard newline, if `use-hard-newlines' is on).
424 If `sentence-end-double-space' is non-nil, then period followed by one
425 space does not end a sentence, so don't break a line there."
426 (interactive (list (region-beginning) (region-end)
427 (if current-prefix-arg 'full)))
430 (goto-char (max from to))
432 (progn (skip-chars-backward "\n")
433 (forward-paragraph)))
435 (goto-char (setq beg (min from to)))
437 (narrow-to-region (point) end)
439 (let ((initial (point))
441 ;; If using hard newlines, break at every one for filling
442 ;; purposes rather than using paragraph breaks.
443 (if use-hard-newlines
445 (while (and (setq end (text-property-any (point) (point-max)
447 (not (= ?\n (char-after end)))
448 (not (= end (point-max))))
449 (goto-char (1+ end)))
450 (setq end (if end (min (point-max) (1+ end)) (point-max)))
452 (forward-paragraph 1)
454 (forward-paragraph -1))
457 (if (>= (point) initial)
458 (fill-region-as-paragraph (point) end justify nosqueeze)
459 (goto-char end)))))))
462 (defconst default-justification 'left
463 "*Method of justifying text not otherwise specified.
464 Possible values are `left', `right', `full', `center', or `none'.
465 The requested kind of justification is done whenever lines are filled.
466 The `justification' text-property can locally override this variable.
467 This variable automatically becomes buffer-local when set in any fashion.")
468 (make-variable-buffer-local 'default-justification)
470 (defun current-justification ()
471 "How should we justify this line?
472 This returns the value of the text-property `justification',
473 or the variable `default-justification' if there is no text-property.
474 However, it returns nil rather than `none' to mean \"don't justify\"."
475 (let ((j (or (get-text-property
476 ;; Make sure we're looking at paragraph body.
477 (save-excursion (skip-chars-forward " \t")
478 (if (and (eobp) (not (bobp)))
479 (1- (point)) (point)))
481 default-justification)))
486 (defun set-justification (begin end value &optional whole-par)
487 "Set the region's justification style.
488 The kind of justification to use is prompted for.
489 If the mark is not active, this command operates on the current paragraph.
490 If the mark is active, the region is used. However, if the beginning and end
491 of the region are not at paragraph breaks, they are moved to the beginning and
492 end of the paragraphs they are in.
493 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
496 When calling from a program, operates just on region between BEGIN and END,
497 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
498 extended to include entire paragraphs as in the interactive command."
499 (interactive (list (if mark-active (region-beginning) (point))
500 (if mark-active (region-end) (point))
501 (let ((s (completing-read
502 "Set justification to: "
503 '(("left") ("right") ("full")
506 (if (equal s "") (error ""))
512 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
513 (paragraph-ignore-fill-prefix (if use-hard-newlines t
514 paragraph-ignore-fill-prefix)))
516 (while (and (bolp) (not (eobp))) (forward-char 1))
520 (skip-chars-backward " \t\n" begin)
524 (narrow-to-region (point-min) end)
525 (unjustify-region begin (point-max))
526 (put-text-property begin (point-max) 'justification value)
527 (fill-region begin (point-max) nil t))))
529 (defun set-justification-none (b e)
530 "Disable automatic filling for paragraphs in the region.
531 If the mark is not active, this applies to the current paragraph."
532 (interactive (list (if mark-active (region-beginning) (point))
533 (if mark-active (region-end) (point))))
534 (set-justification b e 'none t))
536 (defun set-justification-left (b e)
537 "Make paragraphs in the region left-justified.
538 This is usually the default, but see the variable `default-justification'.
539 If the mark is not active, this applies to the current paragraph."
540 (interactive (list (if mark-active (region-beginning) (point))
541 (if mark-active (region-end) (point))))
542 (set-justification b e 'left t))
544 (defun set-justification-right (b e)
545 "Make paragraphs in the region right-justified:
546 Flush at the right margin and ragged on the left.
547 If the mark is not active, this applies to the current paragraph."
548 (interactive (list (if mark-active (region-beginning) (point))
549 (if mark-active (region-end) (point))))
550 (set-justification b e 'right t))
552 (defun set-justification-full (b e)
553 "Make paragraphs in the region fully justified:
554 This makes lines flush on both margins by inserting spaces between words.
555 If the mark is not active, this applies to the current paragraph."
556 (interactive (list (if mark-active (region-beginning) (point))
557 (if mark-active (region-end) (point))))
558 (set-justification b e 'full t))
560 (defun set-justification-center (b e)
561 "Make paragraphs in the region centered.
562 If the mark is not active, this applies to the current paragraph."
563 (interactive (list (if mark-active (region-beginning) (point))
564 (if mark-active (region-end) (point))))
565 (set-justification b e 'center t))
567 ;; A line has up to six parts:
570 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
572 ;; "Indent-1" is the left-margin indentation; normally it ends at column
573 ;; given by the `current-left-margin' function.
574 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
575 ;; "Indent-2" is added to justify a line if the `current-justification' is
576 ;; `center' or `right'. In `left' and `full' justification regions, any
577 ;; whitespace there is part of the line's text, and should not be changed.
578 ;; Trailing whitespace is not counted as part of the line length when
579 ;; center- or right-justifying.
581 ;; All parts of the line are optional, although the final newline can
582 ;; only be missing on the last line of the buffer.
584 (defun justify-current-line (&optional how eop nosqueeze)
585 "Do some kind of justification on this line.
586 Normally does full justification: adds spaces to the line to make it end at
587 the column given by `current-fill-column'.
588 Optional first argument HOW specifies alternate type of justification:
589 it can be `left', `right', `full', `center', or `none'.
590 If HOW is t, will justify however the `current-justification' function says to.
591 If HOW is nil or missing, full justification is done by default.
592 Second arg EOP non-nil means that this is the last line of the paragraph, so
593 it will not be stretched by full justification.
594 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
595 otherwise it is made canonical."
597 (if (eq t how) (setq how (or (current-justification) 'none))
598 (if (null how) (setq how 'full)
599 (or (memq how '(none left right center))
601 (or (memq how '(none left)) ; No action required for these.
602 (let ((fc (current-fill-column))
604 fp-end ; point at end of fill prefix
605 beg ; point at beginning of line's text
606 end ; point at end of line's text
607 indent ; column of `beg'
608 endcol ; column of `end'
609 ncols) ; new indent point or offset
611 ;; Check if this is the last line of the paragraph.
612 (if (and use-hard-newlines (null eop)
613 (get-text-property (point) 'hard))
615 (skip-chars-backward " \t")
616 ;; Quick exit if it appears to be properly justified already
617 ;; or there is no text.
619 (and (memq how '(full right))
620 (= (current-column) fc)))
624 (skip-chars-forward " \t")
625 ;; Skip over fill-prefix.
627 (not (string-equal fill-prefix ""))
630 (point) (min (point-max) (+ (length fill-prefix)
632 (forward-char (length fill-prefix))
633 (if (and adaptive-fill-mode
634 (looking-at adaptive-fill-regexp))
635 (goto-char (match-end 0))))
636 (setq fp-end (point))
637 (skip-chars-forward " \t")
638 ;; This is beginning of the line's text.
639 (setq indent (current-column))
642 (setq endcol (current-column))
644 ;; HOW can't be null or left--we would have exited already
645 (cond ((eq 'right how)
646 (setq ncols (- fc endcol))
648 ;; Need to remove some indentation
650 (progn (goto-char fp-end)
651 (if (< (current-column) (+ indent ncols))
652 (move-to-column (+ indent ncols) t))
654 (progn (move-to-column indent) (point)))
657 (indent-to (+ indent ncols))
658 ;; If point was at beginning of text, keep it there.
660 (move-marker pos (point)))))
663 ;; Figure out how much indentation is needed
664 (setq ncols (+ (current-left-margin)
665 (/ (- fc (current-left-margin) ;avail. space
666 (- endcol indent)) ;text width
669 ;; Have too much indentation - remove some
671 (progn (goto-char fp-end)
672 (if (< (current-column) ncols)
673 (move-to-column ncols t))
675 (progn (move-to-column indent) (point)))
676 ;; Have too little - add some
679 ;; If point was at beginning of text, keep it there.
681 (move-marker pos (point)))))
684 ;; Insert extra spaces between words to justify line
686 (narrow-to-region beg end)
688 (canonically-space-region beg end))
689 (goto-char (point-max))
690 (setq ncols (- fc endcol))
691 ;; Ncols is number of additional spaces needed
694 (search-backward " " nil t))
696 (let ((nmove (+ 3 (random 3))))
698 (or (search-backward " " nil t)
700 (goto-char (point-max))
701 (search-backward " ")))
702 (skip-chars-backward " ")
703 (setq nmove (1- nmove))))
704 (insert-and-inherit " ")
705 (skip-chars-backward " ")
706 (setq ncols (1- ncols)))))))
707 (t (error "Unknown justification value"))))
709 (move-marker pos nil)))
712 (defun unjustify-current-line ()
713 "Remove justification whitespace from current line.
714 If the line is centered or right-justified, this function removes any
715 indentation past the left margin. If the line is full-jusitified, it removes
716 extra spaces between words. It does nothing in other justification modes."
717 (let ((justify (current-justification)))
718 (cond ((eq 'left justify) nil)
719 ((eq nil justify) nil)
720 ((eq 'full justify) ; full justify: remove extra spaces
721 (beginning-of-line-text)
722 (canonically-space-region
723 (point) (save-excursion (end-of-line) (point))))
724 ((memq justify '(center right))
726 (move-to-left-margin nil t)
727 ;; Position ourselves after any fill-prefix.
729 (not (string-equal fill-prefix ""))
732 (point) (min (point-max) (+ (length fill-prefix)
734 (forward-char (length fill-prefix)))
735 (delete-region (point) (progn (skip-chars-forward " \t")
738 (defun unjustify-region (&optional begin end)
739 "Remove justification whitespace from region.
740 For centered or right-justified regions, this function removes any indentation
741 past the left margin from each line. For full-jusitified lines, it removes
742 extra spaces between words. It does nothing in other justification modes.
743 Arguments BEGIN and END are optional; default is the whole buffer."
746 (if end (narrow-to-region (point-min) end))
747 (goto-char (or begin (point-min)))
749 (unjustify-current-line)
753 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
754 "Fill paragraphs within the region, allowing varying indentation within each.
755 This command divides the region into \"paragraphs\",
756 only at paragraph-separator lines, then fills each paragraph
757 using as the fill prefix the smallest indentation of any line
760 When calling from a program, pass range to fill as first two arguments.
762 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
763 JUSTIFY to justify paragraphs (prefix arg),
764 MAIL-FLAG for a mail message, i. e. don't fill header lines."
765 (interactive (list (region-beginning) (region-end)
766 (if current-prefix-arg 'full)))
767 (let ((fill-individual-varying-indent t))
768 (fill-individual-paragraphs min max justifyp mailp)))
770 (defun fill-individual-paragraphs (min max &optional justify mailp)
771 "Fill paragraphs of uniform indentation within the region.
772 This command divides the region into \"paragraphs\",
773 treating every change in indentation level as a paragraph boundary,
774 then fills each paragraph using its indentation level as the fill prefix.
776 When calling from a program, pass range to fill as first two arguments.
778 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
779 JUSTIFY to justify paragraphs (prefix arg),
780 MAIL-FLAG for a mail message, i. e. don't fill header lines."
781 (interactive (list (region-beginning) (region-end)
782 (if current-prefix-arg 'full)))
787 (narrow-to-region (point) max)
789 (while (and (not (eobp))
790 (or (looking-at "[ \t]*[^ \t\n]+:")
791 (looking-at "[ \t]*$")))
792 (if (looking-at "[ \t]*[^ \t\n]+:")
793 (search-forward "\n\n" nil 'move)
795 (narrow-to-region (point) max)
796 ;; Loop over paragraphs.
797 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
798 (move-to-left-margin)
799 (let ((start (point))
800 fill-prefix fill-prefix-regexp)
801 ;; Find end of paragraph, and compute the smallest fill-prefix
802 ;; that fits all the lines in this paragraph.
804 ;; Update the fill-prefix on the first line
805 ;; and whenever the prefix good so far is too long.
806 (if (not (and fill-prefix
807 (looking-at fill-prefix-regexp)))
809 (if (and adaptive-fill-mode adaptive-fill-regexp
810 (looking-at adaptive-fill-regexp))
814 (save-excursion (skip-chars-forward " \t")
816 fill-prefix-regexp (regexp-quote fill-prefix)))
818 (move-to-left-margin)
819 ;; Now stop the loop if end of paragraph.
821 (if fill-individual-varying-indent
822 ;; If this line is a separator line, with or
823 ;; without prefix, end the paragraph.
825 (not (looking-at paragraph-separate))
827 (not (and (looking-at fill-prefix-regexp)
828 (progn (forward-char (length fill-prefix))
829 (looking-at paragraph-separate))))))
830 ;; If this line has more or less indent
831 ;; than the fill prefix wants, end the paragraph.
832 (and (looking-at fill-prefix-regexp)
834 (not (progn (forward-char (length fill-prefix))
835 (or (looking-at paragraph-separate)
836 (looking-at paragraph-start))))))))))
837 ;; Fill this paragraph, but don't add a newline at the end.
838 (let ((had-newline (bolp)))
839 (fill-region-as-paragraph start (point) justify)
840 (or had-newline (delete-char -1))))))))
842 ;;; fill.el ends here