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.
200 ;; And leave TO before that one.
202 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
204 (not (and use-hard-newlines
205 (get-text-property (1- (point)) 'hard))))
206 (delete-backward-char 1)
211 ;; If there was no newline, and there is text in the paragraph, then
213 (if (and (not oneleft) (> to from-plus-indent))
215 (goto-char from-plus-indent))
217 (if (not (> to (point)))
218 nil ; There is no paragraph, only whitespace: exit now.
220 (or justify (setq justify (current-justification)))
222 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
223 (let ((fill-prefix fill-prefix))
224 ;; Figure out how this paragraph is indented, if desired.
225 (if (and adaptive-fill-mode
226 (or (null fill-prefix) (string= fill-prefix "")))
227 (setq fill-prefix (fill-context-prefix from to)))
232 (narrow-to-region (point) to)
234 (if (not justify) ; filling disabled: just check indentation
238 (if (and (not (eolp))
239 (< (current-indentation) (current-left-margin)))
240 (indent-to-left-margin))
243 (if use-hard-newlines
244 (remove-text-properties from (point-max) '(hard nil)))
245 ;; Make sure first line is indented (at least) to left margin...
246 (if (or (memq justify '(right center))
247 (< (current-indentation) (current-left-margin)))
248 (indent-to-left-margin))
249 ;; Delete the fill prefix from every line except the first.
250 ;; The first line may not even have a fill prefix.
252 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
254 (regexp-quote fill-prefix)
258 (if (>= (+ (current-left-margin) (length fill-prefix))
259 (current-fill-column))
260 (error "fill-prefix too long for specified width"))
264 (if (looking-at fpre)
265 (delete-region (point) (match-end 0)))
268 (and (looking-at fpre) (goto-char (match-end 0)))
269 (setq from (point)))))
270 ;; Remove indentation from lines other than the first.
271 (beginning-of-line 2)
272 (indent-region (point) (point-max) 0)
275 ;; FROM, and point, are now before the text to fill,
276 ;; but after any fill prefix on the first line.
278 ;; Make sure sentences ending at end of line get an extra space.
279 ;; loses on split abbrevs ("Mr.\nSmith")
280 (while (re-search-forward "[.?!][])}\"']*$" nil t)
281 (or (eobp) (insert-and-inherit ?\ )))
283 (skip-chars-forward " \t")
284 ;; Then change all newlines to spaces.
285 (subst-char-in-region from (point-max) ?\n ?\ )
286 (if (and nosqueeze (not (eq justify 'full)))
288 (canonically-space-region (point) (point-max))
289 (goto-char (point-max))
290 (delete-horizontal-space)
291 (insert-and-inherit " "))
292 (goto-char (point-min))
294 ;; This is the actual filling loop.
295 (let ((prefixcol 0) linebeg)
297 (setq linebeg (point))
298 (move-to-column (1+ (current-fill-column)))
300 (or nosqueeze (delete-horizontal-space))
301 ;; Move back to start of word.
302 (skip-chars-backward "^ \n" linebeg)
303 ;; Don't break after a period followed by just one space.
304 ;; Move back to the previous place to break.
305 ;; The reason is that if a period ends up at the end of a line,
306 ;; further fills will assume it ends a sentence.
307 ;; If we now know it does not end a sentence,
308 ;; avoid putting it at the end of the line.
309 (if sentence-end-double-space
310 (while (and (> (point) (+ linebeg 2))
311 (eq (preceding-char) ?\ )
312 (not (eq (following-char) ?\ ))
313 (eq (char-after (- (point) 2)) ?\.))
315 (skip-chars-backward "^ \n" linebeg)))
316 ;; If the left margin and fill prefix by themselves
317 ;; pass the fill-column, keep at least one word.
318 ;; This handles ALL BUT the first line of the paragraph.
319 (if (if (zerop prefixcol)
321 (skip-chars-backward " \t" linebeg)
323 (>= prefixcol (current-column)))
324 ;; Ok, skip at least one word.
325 ;; Meanwhile, don't stop at a period followed by one space.
327 (move-to-column prefixcol)
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 " \t")
336 (skip-chars-forward "^ \n\t")
338 ;; Normally, move back over the single space between the words.
340 ;; If the left margin and fill prefix by themselves
341 ;; pass the fill-column, keep at least one word.
342 ;; This handles the first line of the paragraph.
343 (if (and (zerop prefixcol)
344 (let ((fill-point (point)) nchars)
346 (move-to-left-margin)
347 (setq nchars (- fill-point (point)))
350 (< nchars (length fill-prefix))
351 (string= (buffer-substring (point) fill-point)
352 (substring fill-prefix 0 nchars)))))))
353 ;; Ok, skip at least one word. But
354 ;; don't stop at a period followed by just one space.
356 (while (and (not (eobp))
359 sentence-end-double-space
360 (save-excursion (forward-char -1)
361 (and (looking-at "\\. ")
362 (not (looking-at "\\. ")))))))
363 (skip-chars-forward " \t")
364 (skip-chars-forward "^ \t\n")
366 ;; Check again to see if we got to the end of the paragraph.
368 (or nosqueeze (delete-horizontal-space))
369 ;; Replace whitespace here with one newline, then indent to left
371 (skip-chars-backward " \t")
373 ;; Give newline the properties of the space(s) it replaces
374 (set-text-properties (1- (point)) (point)
375 (text-properties-at (point)))
376 (indent-to-left-margin)
377 ;; Insert the fill prefix after indentation.
378 ;; Set prefixcol so whitespace in the prefix won't get lost.
379 (and fill-prefix (not (equal fill-prefix ""))
381 (insert-and-inherit fill-prefix)
382 (setq prefixcol (current-column))))))
383 ;; Justify the line just ended, if desired.
386 (justify-current-line justify t t)
388 (justify-current-line justify nil t)
389 (forward-line 1))))))
390 ;; Leave point after final newline.
391 (goto-char (point-max)))
394 (defun fill-paragraph (arg)
395 "Fill paragraph at or after point. Prefix arg means justify as well.
396 If `sentence-end-double-space' is non-nil, then period followed by one
397 space does not end a sentence, so don't break a line there.
399 If `fill-paragraph-function' is non-nil, we call it (passing our
400 argument to it), and if it returns non-nil, we simply return its value."
401 (interactive (list (if current-prefix-arg 'full)))
402 (or (and fill-paragraph-function
403 (let ((function fill-paragraph-function)
404 fill-paragraph-function)
405 (funcall function arg)))
406 (let ((before (point)))
409 (or (bolp) (newline 1))
411 (beg (progn (backward-paragraph) (point))))
413 (if use-hard-newlines
414 ;; Can't use fill-region-as-paragraph, since this paragraph may
415 ;; still contain hard newlines. See fill-region.
416 (fill-region beg end arg)
417 (fill-region-as-paragraph beg end arg)))))))
419 (defun fill-region (from to &optional justify nosqueeze to-eop)
420 "Fill each of the paragraphs in the region.
421 Prefix arg (non-nil third arg, if called from program) means justify as well.
423 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
424 whitespace other than line breaks untouched, and fifth arg TO-EOP
425 non-nil means to keep filling to the end of the paragraph (or next
426 hard newline, if `use-hard-newlines' is on).
428 If `sentence-end-double-space' is non-nil, then period followed by one
429 space does not end a sentence, so don't break a line there."
430 (interactive (list (region-beginning) (region-end)
431 (if current-prefix-arg 'full)))
434 (goto-char (max from to))
436 (progn (skip-chars-backward "\n")
437 (forward-paragraph)))
439 (goto-char (setq beg (min from to)))
441 (narrow-to-region (point) end)
443 (let ((initial (point))
445 ;; If using hard newlines, break at every one for filling
446 ;; purposes rather than using paragraph breaks.
447 (if use-hard-newlines
449 (while (and (setq end (text-property-any (point) (point-max)
451 (not (= ?\n (char-after end)))
452 (not (= end (point-max))))
453 (goto-char (1+ end)))
454 (setq end (if end (min (point-max) (1+ end)) (point-max)))
456 (forward-paragraph 1)
458 (forward-paragraph -1))
461 (if (>= (point) initial)
462 (fill-region-as-paragraph (point) end justify nosqueeze)
463 (goto-char end)))))))
466 (defconst default-justification 'left
467 "*Method of justifying text not otherwise specified.
468 Possible values are `left', `right', `full', `center', or `none'.
469 The requested kind of justification is done whenever lines are filled.
470 The `justification' text-property can locally override this variable.
471 This variable automatically becomes buffer-local when set in any fashion.")
472 (make-variable-buffer-local 'default-justification)
474 (defun current-justification ()
475 "How should we justify this line?
476 This returns the value of the text-property `justification',
477 or the variable `default-justification' if there is no text-property.
478 However, it returns nil rather than `none' to mean \"don't justify\"."
479 (let ((j (or (get-text-property
480 ;; Make sure we're looking at paragraph body.
481 (save-excursion (skip-chars-forward " \t")
482 (if (and (eobp) (not (bobp)))
483 (1- (point)) (point)))
485 default-justification)))
490 (defun set-justification (begin end value &optional whole-par)
491 "Set the region's justification style.
492 The kind of justification to use is prompted for.
493 If the mark is not active, this command operates on the current paragraph.
494 If the mark is active, the region is used. However, if the beginning and end
495 of the region are not at paragraph breaks, they are moved to the beginning and
496 end of the paragraphs they are in.
497 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
500 When calling from a program, operates just on region between BEGIN and END,
501 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
502 extended to include entire paragraphs as in the interactive command."
503 (interactive (list (if mark-active (region-beginning) (point))
504 (if mark-active (region-end) (point))
505 (let ((s (completing-read
506 "Set justification to: "
507 '(("left") ("right") ("full")
510 (if (equal s "") (error ""))
516 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
517 (paragraph-ignore-fill-prefix (if use-hard-newlines t
518 paragraph-ignore-fill-prefix)))
520 (while (and (bolp) (not (eobp))) (forward-char 1))
524 (skip-chars-backward " \t\n" begin)
528 (narrow-to-region (point-min) end)
529 (unjustify-region begin (point-max))
530 (put-text-property begin (point-max) 'justification value)
531 (fill-region begin (point-max) nil t))))
533 (defun set-justification-none (b e)
534 "Disable automatic filling for paragraphs in the region.
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 'none t))
540 (defun set-justification-left (b e)
541 "Make paragraphs in the region left-justified.
542 This is usually the default, but see the variable `default-justification'.
543 If the mark is not active, this applies to the current paragraph."
544 (interactive (list (if mark-active (region-beginning) (point))
545 (if mark-active (region-end) (point))))
546 (set-justification b e 'left t))
548 (defun set-justification-right (b e)
549 "Make paragraphs in the region right-justified:
550 Flush at the right margin and ragged on the left.
551 If the mark is not active, this applies to the current paragraph."
552 (interactive (list (if mark-active (region-beginning) (point))
553 (if mark-active (region-end) (point))))
554 (set-justification b e 'right t))
556 (defun set-justification-full (b e)
557 "Make paragraphs in the region fully justified:
558 This makes lines flush on both margins by inserting spaces between words.
559 If the mark is not active, this applies to the current paragraph."
560 (interactive (list (if mark-active (region-beginning) (point))
561 (if mark-active (region-end) (point))))
562 (set-justification b e 'full t))
564 (defun set-justification-center (b e)
565 "Make paragraphs in the region centered.
566 If the mark is not active, this applies to the current paragraph."
567 (interactive (list (if mark-active (region-beginning) (point))
568 (if mark-active (region-end) (point))))
569 (set-justification b e 'center t))
571 ;; A line has up to six parts:
574 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
576 ;; "Indent-1" is the left-margin indentation; normally it ends at column
577 ;; given by the `current-left-margin' function.
578 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
579 ;; "Indent-2" is added to justify a line if the `current-justification' is
580 ;; `center' or `right'. In `left' and `full' justification regions, any
581 ;; whitespace there is part of the line's text, and should not be changed.
582 ;; Trailing whitespace is not counted as part of the line length when
583 ;; center- or right-justifying.
585 ;; All parts of the line are optional, although the final newline can
586 ;; only be missing on the last line of the buffer.
588 (defun justify-current-line (&optional how eop nosqueeze)
589 "Do some kind of justification on this line.
590 Normally does full justification: adds spaces to the line to make it end at
591 the column given by `current-fill-column'.
592 Optional first argument HOW specifies alternate type of justification:
593 it can be `left', `right', `full', `center', or `none'.
594 If HOW is t, will justify however the `current-justification' function says to.
595 If HOW is nil or missing, full justification is done by default.
596 Second arg EOP non-nil means that this is the last line of the paragraph, so
597 it will not be stretched by full justification.
598 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
599 otherwise it is made canonical."
601 (if (eq t how) (setq how (or (current-justification) 'none))
602 (if (null how) (setq how 'full)
603 (or (memq how '(none left right center))
605 (or (memq how '(none left)) ; No action required for these.
606 (let ((fc (current-fill-column))
608 fp-end ; point at end of fill prefix
609 beg ; point at beginning of line's text
610 end ; point at end of line's text
611 indent ; column of `beg'
612 endcol ; column of `end'
613 ncols) ; new indent point or offset
615 ;; Check if this is the last line of the paragraph.
616 (if (and use-hard-newlines (null eop)
617 (get-text-property (point) 'hard))
619 (skip-chars-backward " \t")
620 ;; Quick exit if it appears to be properly justified already
621 ;; or there is no text.
623 (and (memq how '(full right))
624 (= (current-column) fc)))
628 (skip-chars-forward " \t")
629 ;; Skip over fill-prefix.
631 (not (string-equal fill-prefix ""))
634 (point) (min (point-max) (+ (length fill-prefix)
636 (forward-char (length fill-prefix))
637 (if (and adaptive-fill-mode
638 (looking-at adaptive-fill-regexp))
639 (goto-char (match-end 0))))
640 (setq fp-end (point))
641 (skip-chars-forward " \t")
642 ;; This is beginning of the line's text.
643 (setq indent (current-column))
646 (setq endcol (current-column))
648 ;; HOW can't be null or left--we would have exited already
649 (cond ((eq 'right how)
650 (setq ncols (- fc endcol))
652 ;; Need to remove some indentation
654 (progn (goto-char fp-end)
655 (if (< (current-column) (+ indent ncols))
656 (move-to-column (+ indent ncols) t))
658 (progn (move-to-column indent) (point)))
661 (indent-to (+ indent ncols))
662 ;; If point was at beginning of text, keep it there.
664 (move-marker pos (point)))))
667 ;; Figure out how much indentation is needed
668 (setq ncols (+ (current-left-margin)
669 (/ (- fc (current-left-margin) ;avail. space
670 (- endcol indent)) ;text width
673 ;; Have too much indentation - remove some
675 (progn (goto-char fp-end)
676 (if (< (current-column) ncols)
677 (move-to-column ncols t))
679 (progn (move-to-column indent) (point)))
680 ;; Have too little - add some
683 ;; If point was at beginning of text, keep it there.
685 (move-marker pos (point)))))
688 ;; Insert extra spaces between words to justify line
690 (narrow-to-region beg end)
692 (canonically-space-region beg end))
693 (goto-char (point-max))
694 (setq ncols (- fc endcol))
695 ;; Ncols is number of additional spaces needed
698 (search-backward " " nil t))
700 (let ((nmove (+ 3 (random 3))))
702 (or (search-backward " " nil t)
704 (goto-char (point-max))
705 (search-backward " ")))
706 (skip-chars-backward " ")
707 (setq nmove (1- nmove))))
708 (insert-and-inherit " ")
709 (skip-chars-backward " ")
710 (setq ncols (1- ncols)))))))
711 (t (error "Unknown justification value"))))
713 (move-marker pos nil)))
716 (defun unjustify-current-line ()
717 "Remove justification whitespace from current line.
718 If the line is centered or right-justified, this function removes any
719 indentation past the left margin. If the line is full-justified, it removes
720 extra spaces between words. It does nothing in other justification modes."
721 (let ((justify (current-justification)))
722 (cond ((eq 'left justify) nil)
723 ((eq nil justify) nil)
724 ((eq 'full justify) ; full justify: remove extra spaces
725 (beginning-of-line-text)
726 (canonically-space-region
727 (point) (save-excursion (end-of-line) (point))))
728 ((memq justify '(center right))
730 (move-to-left-margin nil t)
731 ;; Position ourselves after any fill-prefix.
733 (not (string-equal fill-prefix ""))
736 (point) (min (point-max) (+ (length fill-prefix)
738 (forward-char (length fill-prefix)))
739 (delete-region (point) (progn (skip-chars-forward " \t")
742 (defun unjustify-region (&optional begin end)
743 "Remove justification whitespace from region.
744 For centered or right-justified regions, this function removes any indentation
745 past the left margin from each line. For full-justified lines, it removes
746 extra spaces between words. It does nothing in other justification modes.
747 Arguments BEGIN and END are optional; default is the whole buffer."
750 (if end (narrow-to-region (point-min) end))
751 (goto-char (or begin (point-min)))
753 (unjustify-current-line)
757 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
758 "Fill paragraphs within the region, allowing varying indentation within each.
759 This command divides the region into \"paragraphs\",
760 only at paragraph-separator lines, then fills each paragraph
761 using as the fill prefix the smallest indentation of any line
764 When calling from a program, pass range to fill as first two arguments.
766 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
767 JUSTIFY to justify paragraphs (prefix arg),
768 MAIL-FLAG for a mail message, i. e. don't fill header lines."
769 (interactive (list (region-beginning) (region-end)
770 (if current-prefix-arg 'full)))
771 (let ((fill-individual-varying-indent t))
772 (fill-individual-paragraphs min max justifyp mailp)))
774 (defun fill-individual-paragraphs (min max &optional justify mailp)
775 "Fill paragraphs of uniform indentation within the region.
776 This command divides the region into \"paragraphs\",
777 treating every change in indentation level as a paragraph boundary,
778 then fills each paragraph using its indentation level as the fill prefix.
780 When calling from a program, pass range to fill as first two arguments.
782 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
783 JUSTIFY to justify paragraphs (prefix arg),
784 MAIL-FLAG for a mail message, i. e. don't fill header lines."
785 (interactive (list (region-beginning) (region-end)
786 (if current-prefix-arg 'full)))
791 (narrow-to-region (point) max)
793 (while (and (not (eobp))
794 (or (looking-at "[ \t]*[^ \t\n]+:")
795 (looking-at "[ \t]*$")))
796 (if (looking-at "[ \t]*[^ \t\n]+:")
797 (search-forward "\n\n" nil 'move)
799 (narrow-to-region (point) max)
800 ;; Loop over paragraphs.
801 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
802 (move-to-left-margin)
803 (let ((start (point))
804 fill-prefix fill-prefix-regexp)
805 ;; Find end of paragraph, and compute the smallest fill-prefix
806 ;; that fits all the lines in this paragraph.
808 ;; Update the fill-prefix on the first line
809 ;; and whenever the prefix good so far is too long.
810 (if (not (and fill-prefix
811 (looking-at fill-prefix-regexp)))
813 (if (and adaptive-fill-mode adaptive-fill-regexp
814 (looking-at adaptive-fill-regexp))
818 (save-excursion (skip-chars-forward " \t")
820 fill-prefix-regexp (regexp-quote fill-prefix)))
822 (move-to-left-margin)
823 ;; Now stop the loop if end of paragraph.
825 (if fill-individual-varying-indent
826 ;; If this line is a separator line, with or
827 ;; without prefix, end the paragraph.
829 (not (looking-at paragraph-separate))
831 (not (and (looking-at fill-prefix-regexp)
832 (progn (forward-char (length fill-prefix))
833 (looking-at paragraph-separate))))))
834 ;; If this line has more or less indent
835 ;; than the fill prefix wants, end the paragraph.
836 (and (looking-at fill-prefix-regexp)
838 (not (progn (forward-char (length fill-prefix))
839 (or (looking-at paragraph-separate)
840 (looking-at paragraph-start))))))))))
841 ;; Fill this paragraph, but don't add a newline at the end.
842 (let ((had-newline (bolp)))
843 (fill-region-as-paragraph start (point) justify)
844 (or had-newline (delete-char -1))))))))
846 ;;; fill.el ends here