1 ;;; fill.el --- fill commands for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 1996 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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; All the commands for filling text. These are documented in the Emacs
31 (defvar fill-individual-varying-indent nil
32 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
33 Non-nil means changing indent doesn't end a paragraph.
34 That mode can handle paragraphs with extra indentation on the first line,
35 but it requires separator lines between paragraphs.
36 A value of nil means that any change in indentation starts a new paragraph.")
38 (defvar sentence-end-double-space t
39 "*Non-nil means a single space does not end a sentence.")
41 (defvar colon-double-space nil
42 "*Non-nil means put two spaces after a colon when filling.")
44 (defvar fill-paragraph-function nil
45 "Mode-specific function to fill a paragraph, or nil if there is none.
46 If the function returns nil, then `fill-paragraph' does its normal work.")
48 (defun set-fill-prefix ()
49 "Set the fill prefix to the current line up to point.
50 Filling expects lines to start with the fill prefix and
51 reinserts the fill prefix in each resulting line."
53 (setq fill-prefix (buffer-substring
54 (save-excursion (move-to-left-margin) (point))
56 (if (equal fill-prefix "")
57 (setq fill-prefix nil))
59 (message "fill-prefix: \"%s\"" fill-prefix)
60 (message "fill-prefix cancelled")))
62 (defvar adaptive-fill-mode t
63 "*Non-nil means determine a paragraph's fill prefix from its text.")
65 (defvar adaptive-fill-regexp "[ \t]*\\([#;>*]+ +\\)?"
66 "*Regexp to match text at start of line that constitutes indentation.
67 If Adaptive Fill mode is enabled, whatever text matches this pattern
68 on the second line of a paragraph is used as the standard indentation
69 for the paragraph. If the paragraph has just one line, the indentation
70 is taken from that line.")
72 (defvar adaptive-fill-function nil
73 "*Function to call to choose a fill prefix for a paragraph.
74 This function is used when `adaptive-fill-regexp' does not match.")
76 (defun current-fill-column ()
77 "Return the fill-column to use for this line.
78 The fill-column to use for a buffer is stored in the variable `fill-column',
79 but can be locally modified by the `right-margin' text property, which is
80 subtracted from `fill-column'.
82 The fill column to use for a line is the first column at which the column
83 number equals or exceeds the local fill-column - right-margin difference."
86 (let* ((here (progn (beginning-of-line) (point)))
88 (eol (progn (end-of-line) (point)))
89 margin fill-col change col)
90 ;; Look separately at each region of line with a different right-margin.
91 (while (and (setq margin (get-text-property here 'right-margin)
92 fill-col (- fill-column (or margin 0))
93 change (text-property-not-all
94 here eol 'right-margin margin))
95 (progn (goto-char (1- change))
96 (setq col (current-column))
100 (max here-col fill-col)))))
102 (defun canonically-space-region (beg end)
103 "Remove extra spaces between words in region.
104 Leave one space between words, two at end of sentences or after colons
105 (depending on values of `sentence-end-double-space' and `colon-double-space').
106 Remove indentation from each line."
110 ;; Nuke tabs; they get screwed up in a fill.
111 ;; This is quick, but loses when a tab follows the end of a sentence.
112 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
114 (subst-char-in-region beg end ?\t ?\ )
115 (while (and (< (point) end)
116 (re-search-forward " *" end t))
118 (+ (match-beginning 0)
119 ;; Determine number of spaces to leave:
121 (skip-chars-backward " ]})\"'")
122 (cond ((and sentence-end-double-space
123 (memq (preceding-char) '(?. ?? ?!))) 2)
124 ((and colon-double-space
125 (= (preceding-char) ?:)) 2)
126 ((char-equal (preceding-char) ?\n) 0)
129 ;; Make sure sentences ending at end of line get an extra space.
130 ;; loses on split abbrevs ("Mr.\nSmith")
132 (while (and (< (point) end)
133 (re-search-forward "[.?!][])}\"']*$" end t))
134 ;; We insert before markers in case a caller such as
135 ;; do-auto-fill has done a save-excursion with point at the end
136 ;; of the line and wants it to stay at the end of the line.
137 (insert-before-markers-and-inherit ? ))))
139 (defun fill-context-prefix (from to &optional first-line-regexp)
140 "Compute a fill prefix from the text between FROM and TO.
141 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
142 If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
143 first line, insist it must match FIRST-LINE-REGEXP."
146 (if (eolp) (forward-line 1))
147 ;; Move to the second line unless there is just one.
148 (let ((firstline (point))
149 ;; Non-nil if we are on the second line.
154 (goto-char firstline)
156 (move-to-left-margin)
157 (let ((start (point)))
159 (if (not (looking-at paragraph-start))
160 (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
161 (buffer-substring-no-properties start (match-end 0)))
162 (adaptive-fill-function (funcall adaptive-fill-function)))))
165 (null first-line-regexp)
166 (string-match first-line-regexp result))
169 (defun fill-region-as-paragraph (from to &optional justify
170 nosqueeze squeeze-after)
171 "Fill the region as one paragraph.
172 It removes any paragraph breaks in the region and extra newlines at the end,
173 indents and fills lines between the margins given by the
174 `current-left-margin' and `current-fill-column' functions.
175 It leaves point at the beginning of the line following the paragraph.
177 Normally performs justification according to the `current-justification'
178 function, but with a prefix arg, does full justification instead.
180 From a program, optional third arg JUSTIFY can specify any type of
181 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
182 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
183 means don't canonicalize spaces before that position.
185 If `sentence-end-double-space' is non-nil, then period followed by one
186 space does not end a sentence, so don't break a line there."
187 (interactive (list (region-beginning) (region-end)
188 (if current-prefix-arg 'full)))
189 ;; Arrange for undoing the fill to restore point.
190 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
191 (setq buffer-undo-list (cons (point) buffer-undo-list)))
193 ;; Make sure "to" is the endpoint.
194 (goto-char (min from to))
195 (setq to (max from to))
196 ;; Ignore blank lines at beginning of region.
197 (skip-chars-forward " \t\n")
199 (let ((from-plus-indent (point))
205 ;; Delete all but one soft newline at end of region.
206 ;; And leave TO before that one.
208 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
210 (not (and use-hard-newlines
211 (get-text-property (1- (point)) 'hard))))
212 (delete-backward-char 1)
217 ;; If there was no newline, and there is text in the paragraph, then
219 (if (and (not oneleft) (> to from-plus-indent))
221 (goto-char from-plus-indent))
223 (if (not (> to (point)))
224 nil ; There is no paragraph, only whitespace: exit now.
226 (or justify (setq justify (current-justification)))
228 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
229 (let ((fill-prefix fill-prefix))
230 ;; Figure out how this paragraph is indented, if desired.
231 (if (and adaptive-fill-mode
232 (or (null fill-prefix) (string= fill-prefix "")))
233 (setq fill-prefix (fill-context-prefix from to)))
238 (narrow-to-region (point) to)
240 (if (not justify) ; filling disabled: just check indentation
244 (if (and (not (eolp))
245 (< (current-indentation) (current-left-margin)))
246 (indent-to-left-margin))
249 (if use-hard-newlines
250 (remove-text-properties from (point-max) '(hard nil)))
251 ;; Make sure first line is indented (at least) to left margin...
252 (if (or (memq justify '(right center))
253 (< (current-indentation) (current-left-margin)))
254 (indent-to-left-margin))
255 ;; Delete the fill prefix from every line except the first.
256 ;; The first line may not even have a fill prefix.
258 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
260 (regexp-quote fill-prefix)
264 (if (>= (+ (current-left-margin) (length fill-prefix))
265 (current-fill-column))
266 (error "fill-prefix too long for specified width"))
270 (if (looking-at fpre)
271 (delete-region (point) (match-end 0)))
274 (if (looking-at fpre)
275 (goto-char (match-end 0)))
276 (setq from (point)))))
277 ;; Remove indentation from lines other than the first.
278 (beginning-of-line 2)
279 (indent-region (point) (point-max) 0)
282 ;; FROM, and point, are now before the text to fill,
283 ;; but after any fill prefix on the first line.
285 ;; Make sure sentences ending at end of line get an extra space.
286 ;; loses on split abbrevs ("Mr.\nSmith")
287 (while (re-search-forward "[.?!][])}\"']*$" nil t)
288 (or (eobp) (insert-and-inherit ?\ )))
290 (skip-chars-forward " \t")
291 ;; Then change all newlines to spaces.
292 (subst-char-in-region from (point-max) ?\n ?\ )
293 (if (and nosqueeze (not (eq justify 'full)))
295 (canonically-space-region (or squeeze-after (point)) (point-max))
296 (goto-char (point-max))
297 (delete-horizontal-space)
298 (insert-and-inherit " "))
299 (goto-char (point-min))
301 ;; This is the actual filling loop.
302 (let ((prefixcol 0) linebeg)
304 (setq linebeg (point))
305 (move-to-column (1+ (current-fill-column)))
307 (or nosqueeze (delete-horizontal-space))
308 ;; Move back to start of word.
309 (skip-chars-backward "^ \n" linebeg)
310 ;; Don't break after a period followed by just one space.
311 ;; Move back to the previous place to break.
312 ;; The reason is that if a period ends up at the end of a line,
313 ;; further fills will assume it ends a sentence.
314 ;; If we now know it does not end a sentence,
315 ;; avoid putting it at the end of the line.
316 (if sentence-end-double-space
317 (while (and (> (point) (+ linebeg 2))
318 (eq (preceding-char) ?\ )
319 (not (eq (following-char) ?\ ))
320 (eq (char-after (- (point) 2)) ?\.))
322 (skip-chars-backward "^ \n" linebeg)))
323 ;; If the left margin and fill prefix by themselves
324 ;; pass the fill-column. or if they are zero
325 ;; but we have no room for even one word,
326 ;; keep at least one word anyway.
327 ;; This handles ALL BUT the first line of the paragraph.
328 (if (if (zerop prefixcol)
330 (skip-chars-backward " \t" linebeg)
332 (>= prefixcol (current-column)))
333 ;; Ok, skip at least one word.
334 ;; Meanwhile, don't stop at a period followed by one space.
336 (move-to-column prefixcol)
337 (while (and (not (eobp))
340 sentence-end-double-space
341 (save-excursion (forward-char -1)
342 (and (looking-at "\\. ")
343 (not (looking-at "\\. ")))))))
344 (skip-chars-forward " \t")
345 (skip-chars-forward "^ \n\t")
347 ;; Normally, move back over the single space between the words.
349 ;; If the left margin and fill prefix by themselves
350 ;; pass the fill-column, keep at least one word.
351 ;; This handles the first line of the paragraph.
352 (if (and (zerop prefixcol)
353 (let ((fill-point (point)) nchars)
355 (move-to-left-margin)
356 (setq nchars (- fill-point (point)))
359 (< nchars (length fill-prefix))
360 (string= (buffer-substring (point) fill-point)
361 (substring fill-prefix 0 nchars)))))))
362 ;; Ok, skip at least one word. But
363 ;; don't stop at a period followed by just one space.
365 (while (and (not (eobp))
368 sentence-end-double-space
369 (save-excursion (forward-char -1)
370 (and (looking-at "\\. ")
371 (not (looking-at "\\. ")))))))
372 (skip-chars-forward " \t")
373 (skip-chars-forward "^ \t\n")
375 ;; Check again to see if we got to the end of the paragraph.
376 (if (save-excursion (skip-chars-forward " \t") (eobp))
377 (or nosqueeze (delete-horizontal-space))
378 ;; Replace whitespace here with one newline, then indent to left
380 (skip-chars-backward " \t")
382 ;; Give newline the properties of the space(s) it replaces
383 (set-text-properties (1- (point)) (point)
384 (text-properties-at (point)))
385 (indent-to-left-margin)
386 ;; Insert the fill prefix after indentation.
387 ;; Set prefixcol so whitespace in the prefix won't get lost.
388 (and fill-prefix (not (equal fill-prefix ""))
390 (insert-and-inherit fill-prefix)
391 (setq prefixcol (current-column))))))
392 ;; Justify the line just ended, if desired.
395 (justify-current-line justify t t)
397 (justify-current-line justify nil t)
398 (forward-line 1))))))
399 ;; Leave point after final newline.
400 (goto-char (point-max)))
403 (defun fill-paragraph (arg)
404 "Fill paragraph at or after point. Prefix arg means justify as well.
405 If `sentence-end-double-space' is non-nil, then period followed by one
406 space does not end a sentence, so don't break a line there.
408 If `fill-paragraph-function' is non-nil, we call it (passing our
409 argument to it), and if it returns non-nil, we simply return its value."
410 (interactive (list (if current-prefix-arg 'full)))
411 (or (and fill-paragraph-function
412 (let ((function fill-paragraph-function)
413 fill-paragraph-function)
414 (funcall function arg)))
415 (let ((before (point)))
418 (or (bolp) (newline 1))
420 (beg (progn (backward-paragraph) (point))))
422 (if use-hard-newlines
423 ;; Can't use fill-region-as-paragraph, since this paragraph may
424 ;; still contain hard newlines. See fill-region.
425 (fill-region beg end arg)
426 (fill-region-as-paragraph beg end arg)))))))
428 (defun fill-region (from to &optional justify nosqueeze to-eop)
429 "Fill each of the paragraphs in the region.
430 Prefix arg (non-nil third arg, if called from program) means justify as well.
432 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
433 whitespace other than line breaks untouched, and fifth arg TO-EOP
434 non-nil means to keep filling to the end of the paragraph (or next
435 hard newline, if `use-hard-newlines' is on).
437 If `sentence-end-double-space' is non-nil, then period followed by one
438 space does not end a sentence, so don't break a line there."
439 (interactive (list (region-beginning) (region-end)
440 (if current-prefix-arg 'full)))
443 (goto-char (max from to))
445 (progn (skip-chars-backward "\n")
446 (forward-paragraph)))
448 (goto-char (setq beg (min from to)))
450 (narrow-to-region (point) end)
452 (let ((initial (point))
454 ;; If using hard newlines, break at every one for filling
455 ;; purposes rather than using paragraph breaks.
456 (if use-hard-newlines
458 (while (and (setq end (text-property-any (point) (point-max)
460 (not (= ?\n (char-after end)))
461 (not (= end (point-max))))
462 (goto-char (1+ end)))
463 (setq end (if end (min (point-max) (1+ end)) (point-max)))
465 (forward-paragraph 1)
467 (forward-paragraph -1))
470 (if (>= (point) initial)
471 (fill-region-as-paragraph (point) end justify nosqueeze)
472 (goto-char end)))))))
475 (defvar default-justification 'left
476 "*Method of justifying text not otherwise specified.
477 Possible values are `left', `right', `full', `center', or `none'.
478 The requested kind of justification is done whenever lines are filled.
479 The `justification' text-property can locally override this variable.
480 This variable automatically becomes buffer-local when set in any fashion.")
481 (make-variable-buffer-local 'default-justification)
483 (defun current-justification ()
484 "How should we justify this line?
485 This returns the value of the text-property `justification',
486 or the variable `default-justification' if there is no text-property.
487 However, it returns nil rather than `none' to mean \"don't justify\"."
488 (let ((j (or (get-text-property
489 ;; Make sure we're looking at paragraph body.
490 (save-excursion (skip-chars-forward " \t")
491 (if (and (eobp) (not (bobp)))
492 (1- (point)) (point)))
494 default-justification)))
499 (defun set-justification (begin end value &optional whole-par)
500 "Set the region's justification style.
501 The kind of justification to use is prompted for.
502 If the mark is not active, this command operates on the current paragraph.
503 If the mark is active, the region is used. However, if the beginning and end
504 of the region are not at paragraph breaks, they are moved to the beginning and
505 end of the paragraphs they are in.
506 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
509 When calling from a program, operates just on region between BEGIN and END,
510 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
511 extended to include entire paragraphs as in the interactive command."
512 (interactive (list (if mark-active (region-beginning) (point))
513 (if mark-active (region-end) (point))
514 (let ((s (completing-read
515 "Set justification to: "
516 '(("left") ("right") ("full")
519 (if (equal s "") (error ""))
525 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
526 (paragraph-ignore-fill-prefix (if use-hard-newlines t
527 paragraph-ignore-fill-prefix)))
529 (while (and (bolp) (not (eobp))) (forward-char 1))
533 (skip-chars-backward " \t\n" begin)
537 (narrow-to-region (point-min) end)
538 (unjustify-region begin (point-max))
539 (put-text-property begin (point-max) 'justification value)
540 (fill-region begin (point-max) nil t))))
542 (defun set-justification-none (b e)
543 "Disable automatic filling for paragraphs in the region.
544 If the mark is not active, this applies to the current paragraph."
545 (interactive (list (if mark-active (region-beginning) (point))
546 (if mark-active (region-end) (point))))
547 (set-justification b e 'none t))
549 (defun set-justification-left (b e)
550 "Make paragraphs in the region left-justified.
551 This is usually the default, but see the variable `default-justification'.
552 If the mark is not active, this applies to the current paragraph."
553 (interactive (list (if mark-active (region-beginning) (point))
554 (if mark-active (region-end) (point))))
555 (set-justification b e 'left t))
557 (defun set-justification-right (b e)
558 "Make paragraphs in the region right-justified:
559 Flush at the right margin and ragged on the left.
560 If the mark is not active, this applies to the current paragraph."
561 (interactive (list (if mark-active (region-beginning) (point))
562 (if mark-active (region-end) (point))))
563 (set-justification b e 'right t))
565 (defun set-justification-full (b e)
566 "Make paragraphs in the region fully justified:
567 This makes lines flush on both margins by inserting spaces between words.
568 If the mark is not active, this applies to the current paragraph."
569 (interactive (list (if mark-active (region-beginning) (point))
570 (if mark-active (region-end) (point))))
571 (set-justification b e 'full t))
573 (defun set-justification-center (b e)
574 "Make paragraphs in the region centered.
575 If the mark is not active, this applies to the current paragraph."
576 (interactive (list (if mark-active (region-beginning) (point))
577 (if mark-active (region-end) (point))))
578 (set-justification b e 'center t))
580 ;; A line has up to six parts:
583 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
585 ;; "Indent-1" is the left-margin indentation; normally it ends at column
586 ;; given by the `current-left-margin' function.
587 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
588 ;; "Indent-2" is added to justify a line if the `current-justification' is
589 ;; `center' or `right'. In `left' and `full' justification regions, any
590 ;; whitespace there is part of the line's text, and should not be changed.
591 ;; Trailing whitespace is not counted as part of the line length when
592 ;; center- or right-justifying.
594 ;; All parts of the line are optional, although the final newline can
595 ;; only be missing on the last line of the buffer.
597 (defun justify-current-line (&optional how eop nosqueeze)
598 "Do some kind of justification on this line.
599 Normally does full justification: adds spaces to the line to make it end at
600 the column given by `current-fill-column'.
601 Optional first argument HOW specifies alternate type of justification:
602 it can be `left', `right', `full', `center', or `none'.
603 If HOW is t, will justify however the `current-justification' function says to.
604 If HOW is nil or missing, full justification is done by default.
605 Second arg EOP non-nil means that this is the last line of the paragraph, so
606 it will not be stretched by full justification.
607 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
608 otherwise it is made canonical."
610 (if (eq t how) (setq how (or (current-justification) 'none))
611 (if (null how) (setq how 'full)
612 (or (memq how '(none left right center))
614 (or (memq how '(none left)) ; No action required for these.
615 (let ((fc (current-fill-column))
617 fp-end ; point at end of fill prefix
618 beg ; point at beginning of line's text
619 end ; point at end of line's text
620 indent ; column of `beg'
621 endcol ; column of `end'
622 ncols) ; new indent point or offset
624 ;; Check if this is the last line of the paragraph.
625 (if (and use-hard-newlines (null eop)
626 (get-text-property (point) 'hard))
628 (skip-chars-backward " \t")
629 ;; Quick exit if it appears to be properly justified already
630 ;; or there is no text.
632 (and (memq how '(full right))
633 (= (current-column) fc)))
637 (skip-chars-forward " \t")
638 ;; Skip over fill-prefix.
640 (not (string-equal fill-prefix ""))
643 (point) (min (point-max) (+ (length fill-prefix)
645 (forward-char (length fill-prefix))
646 (if (and adaptive-fill-mode
647 (looking-at adaptive-fill-regexp))
648 (goto-char (match-end 0))))
649 (setq fp-end (point))
650 (skip-chars-forward " \t")
651 ;; This is beginning of the line's text.
652 (setq indent (current-column))
655 (setq endcol (current-column))
657 ;; HOW can't be null or left--we would have exited already
658 (cond ((eq 'right how)
659 (setq ncols (- fc endcol))
661 ;; Need to remove some indentation
663 (progn (goto-char fp-end)
664 (if (< (current-column) (+ indent ncols))
665 (move-to-column (+ indent ncols) t))
667 (progn (move-to-column indent) (point)))
670 (indent-to (+ indent ncols))
671 ;; If point was at beginning of text, keep it there.
673 (move-marker pos (point)))))
676 ;; Figure out how much indentation is needed
677 (setq ncols (+ (current-left-margin)
678 (/ (- fc (current-left-margin) ;avail. space
679 (- endcol indent)) ;text width
682 ;; Have too much indentation - remove some
684 (progn (goto-char fp-end)
685 (if (< (current-column) ncols)
686 (move-to-column ncols t))
688 (progn (move-to-column indent) (point)))
689 ;; Have too little - add some
692 ;; If point was at beginning of text, keep it there.
694 (move-marker pos (point)))))
697 ;; Insert extra spaces between words to justify line
699 (narrow-to-region beg end)
701 (canonically-space-region beg end))
702 (goto-char (point-max))
703 (setq ncols (- fc endcol))
704 ;; Ncols is number of additional spaces needed
707 (search-backward " " nil t))
709 (let ((nmove (+ 3 (random 3))))
711 (or (search-backward " " nil t)
713 (goto-char (point-max))
714 (search-backward " ")))
715 (skip-chars-backward " ")
716 (setq nmove (1- nmove))))
717 (insert-and-inherit " ")
718 (skip-chars-backward " ")
719 (setq ncols (1- ncols)))))))
720 (t (error "Unknown justification value"))))
722 (move-marker pos nil)))
725 (defun unjustify-current-line ()
726 "Remove justification whitespace from current line.
727 If the line is centered or right-justified, this function removes any
728 indentation past the left margin. If the line is full-justified, it removes
729 extra spaces between words. It does nothing in other justification modes."
730 (let ((justify (current-justification)))
731 (cond ((eq 'left justify) nil)
732 ((eq nil justify) nil)
733 ((eq 'full justify) ; full justify: remove extra spaces
734 (beginning-of-line-text)
735 (canonically-space-region
736 (point) (save-excursion (end-of-line) (point))))
737 ((memq justify '(center right))
739 (move-to-left-margin nil t)
740 ;; Position ourselves after any fill-prefix.
742 (not (string-equal fill-prefix ""))
745 (point) (min (point-max) (+ (length fill-prefix)
747 (forward-char (length fill-prefix)))
748 (delete-region (point) (progn (skip-chars-forward " \t")
751 (defun unjustify-region (&optional begin end)
752 "Remove justification whitespace from region.
753 For centered or right-justified regions, this function removes any indentation
754 past the left margin from each line. For full-justified lines, it removes
755 extra spaces between words. It does nothing in other justification modes.
756 Arguments BEGIN and END are optional; default is the whole buffer."
759 (if end (narrow-to-region (point-min) end))
760 (goto-char (or begin (point-min)))
762 (unjustify-current-line)
766 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
767 "Fill paragraphs within the region, allowing varying indentation within each.
768 This command divides the region into \"paragraphs\",
769 only at paragraph-separator lines, then fills each paragraph
770 using as the fill prefix the smallest indentation of any line
773 When calling from a program, pass range to fill as first two arguments.
775 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
776 JUSTIFY to justify paragraphs (prefix arg),
777 MAIL-FLAG for a mail message, i. e. don't fill header lines."
778 (interactive (list (region-beginning) (region-end)
779 (if current-prefix-arg 'full)))
780 (let ((fill-individual-varying-indent t))
781 (fill-individual-paragraphs min max justifyp mailp)))
783 (defun fill-individual-paragraphs (min max &optional justify mailp)
784 "Fill paragraphs of uniform indentation within the region.
785 This command divides the region into \"paragraphs\",
786 treating every change in indentation level as a paragraph boundary,
787 then fills each paragraph using its indentation level as the fill prefix.
789 When calling from a program, pass range to fill as first two arguments.
791 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
792 JUSTIFY to justify paragraphs (prefix arg),
793 MAIL-FLAG for a mail message, i. e. don't fill header lines."
794 (interactive (list (region-beginning) (region-end)
795 (if current-prefix-arg 'full)))
800 (narrow-to-region (point) max)
802 (while (and (not (eobp))
803 (or (looking-at "[ \t]*[^ \t\n]+:")
804 (looking-at "[ \t]*$")))
805 (if (looking-at "[ \t]*[^ \t\n]+:")
806 (search-forward "\n\n" nil 'move)
808 (narrow-to-region (point) max)
809 ;; Loop over paragraphs.
810 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
811 (move-to-left-margin)
812 (let ((start (point))
813 fill-prefix fill-prefix-regexp)
814 ;; Find end of paragraph, and compute the smallest fill-prefix
815 ;; that fits all the lines in this paragraph.
817 ;; Update the fill-prefix on the first line
818 ;; and whenever the prefix good so far is too long.
819 (if (not (and fill-prefix
820 (looking-at fill-prefix-regexp)))
822 (if (and adaptive-fill-mode adaptive-fill-regexp
823 (looking-at adaptive-fill-regexp))
827 (save-excursion (skip-chars-forward " \t")
829 fill-prefix-regexp (regexp-quote fill-prefix)))
832 ;; If forward-line went past a newline,
833 ;; move further to the left margin.
834 (move-to-left-margin))
835 ;; Now stop the loop if end of paragraph.
837 (if fill-individual-varying-indent
838 ;; If this line is a separator line, with or
839 ;; without prefix, end the paragraph.
841 (not (looking-at paragraph-separate))
843 (not (and (looking-at fill-prefix-regexp)
844 (progn (forward-char (length fill-prefix))
845 (looking-at paragraph-separate))))))
846 ;; If this line has more or less indent
847 ;; than the fill prefix wants, end the paragraph.
848 (and (looking-at fill-prefix-regexp)
850 (not (progn (forward-char (length fill-prefix))
851 (or (looking-at paragraph-separate)
852 (looking-at paragraph-start))))))))))
853 ;; Fill this paragraph, but don't add a newline at the end.
854 (let ((had-newline (bolp)))
855 (fill-region-as-paragraph start (point) justify)
856 (or had-newline (delete-char -1))))))))
858 ;;; fill.el ends here