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 (defcustom 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."
40 (defcustom sentence-end-double-space t
41 "*Non-nil means a single space does not end a sentence."
45 (defcustom colon-double-space nil
46 "*Non-nil means put two spaces after a colon when filling."
50 (defvar fill-paragraph-function nil
51 "Mode-specific function to fill a paragraph, or nil if there is none.
52 If the function returns nil, then `fill-paragraph' does its normal work.")
54 (defvar enable-kinsoku t
55 "*Non-nil means enable `kinsoku' processing on filling paragraph.
56 `Kinsoku' processing is to prohibit specific characters to be placed
57 at beginning or end of line. See the documentation of kinsoku for
60 (defun set-fill-prefix ()
61 "Set the fill prefix to the current line up to point.
62 Filling expects lines to start with the fill prefix and
63 reinserts the fill prefix in each resulting line."
65 (setq fill-prefix (buffer-substring
66 (save-excursion (move-to-left-margin) (point))
68 (if (equal fill-prefix "")
69 (setq fill-prefix nil))
71 (message "fill-prefix: \"%s\"" fill-prefix)
72 (message "fill-prefix cancelled")))
74 (defcustom adaptive-fill-mode t
75 "*Non-nil means determine a paragraph's fill prefix from its text."
79 (defcustom adaptive-fill-regexp "[ \t]*\\([-|#;>*]+ *\\|(?[0-9]+[.)] *\\)*"
80 "*Regexp to match text at start of line that constitutes indentation.
81 If Adaptive Fill mode is enabled, a prefix matching this pattern
82 on the first and second lines of a paragraph is used as the
83 standard indentation for the whole paragraph.
85 If the paragraph has just one line, the indentation is taken from that
86 line, but in that case `adaptive-fill-first-line-regexp' also plays
91 (defcustom adaptive-fill-first-line-regexp "\\`[ \t]*$`//'"
92 "*Regexp specifying whether to set fill prefix from a one-line paragraph.
93 When a paragraph has just one line, then after `adaptive-fill-regexp'
94 finds the prefix at the beginning of the line, if it doesn't
95 match this regexp, it is replaced with whitespace.
97 By default, this regexp matches sequences of just spaces and tabs.
99 However, we never use a prefix from a one-line paragraph
100 if it would act as a paragraph-starter on the second line."
104 (defcustom adaptive-fill-function nil
105 "*Function to call to choose a fill prefix for a paragraph.
106 This function is used when `adaptive-fill-regexp' does not match."
110 (defun current-fill-column ()
111 "Return the fill-column to use for this line.
112 The fill-column to use for a buffer is stored in the variable `fill-column',
113 but can be locally modified by the `right-margin' text property, which is
114 subtracted from `fill-column'.
116 The fill column to use for a line is the first column at which the column
117 number equals or exceeds the local fill-column - right-margin difference."
120 (let* ((here (progn (beginning-of-line) (point)))
122 (eol (progn (end-of-line) (point)))
123 margin fill-col change col)
124 ;; Look separately at each region of line with a different right-margin.
125 (while (and (setq margin (get-text-property here 'right-margin)
126 fill-col (- fill-column (or margin 0))
127 change (text-property-not-all
128 here eol 'right-margin margin))
129 (progn (goto-char (1- change))
130 (setq col (current-column))
134 (max here-col fill-col)))))
136 (defun canonically-space-region (beg end)
137 "Remove extra spaces between words in region.
138 Leave one space between words, two at end of sentences or after colons
139 (depending on values of `sentence-end-double-space' and `colon-double-space').
140 Remove indentation from each line."
144 ;; Nuke tabs; they get screwed up in a fill.
145 ;; This is quick, but loses when a tab follows the end of a sentence.
146 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
148 (subst-char-in-region beg end ?\t ?\ )
149 (while (and (< (point) end)
150 (re-search-forward " *" end t))
152 (+ (match-beginning 0)
153 ;; Determine number of spaces to leave:
155 (skip-chars-backward " ]})\"'")
156 (cond ((and sentence-end-double-space
157 (memq (preceding-char) '(?. ?? ?!))) 2)
158 ((and colon-double-space
159 (= (preceding-char) ?:)) 2)
160 ((char-equal (preceding-char) ?\n) 0)
163 ;; Make sure sentences ending at end of line get an extra space.
164 ;; loses on split abbrevs ("Mr.\nSmith")
166 (while (and (< (point) end)
167 (re-search-forward "[.?!][])}\"']*$" end t))
168 ;; We insert before markers in case a caller such as
169 ;; do-auto-fill has done a save-excursion with point at the end
170 ;; of the line and wants it to stay at the end of the line.
171 (insert-before-markers-and-inherit ? ))))
173 (defun fill-context-prefix (from to &optional first-line-regexp)
174 "Compute a fill prefix from the text between FROM and TO.
175 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'
176 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
177 we reject a prefix based on a one-line paragraph if that prefix would
178 act as a paragraph-separator."
179 (or first-line-regexp
180 (setq first-line-regexp adaptive-fill-first-line-regexp))
183 (if (eolp) (forward-line 1))
184 ;; Move to the second line unless there is just one.
185 (let ((firstline (point))
186 ;; Non-nil if we are on the second line.
191 (goto-char firstline)
193 (move-to-left-margin)
194 (let ((start (point)))
196 (if (not (looking-at paragraph-start))
197 (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
198 (buffer-substring-no-properties start (match-end 0)))
199 (adaptive-fill-function (funcall adaptive-fill-function)))))
201 ;; If we get a fill prefix from the second line,
202 ;; make sure it's on the first line too.
206 (if (looking-at (regexp-quote result))
208 ;; If we get a fill prefix from a one-line paragraph,
209 ;; maybe change it to whitespace,
210 ;; and check that it isn't a paragraph starter.
213 ;; If RESULT comes from the first line,
214 ;; see if it seems reasonable to use for all lines.
215 ;; If not, replace it with whitespace.
216 (or (and first-line-regexp
217 (string-match first-line-regexp result))
218 (and comment-start-skip
219 (string-match comment-start-skip result))
220 (setq result (make-string (string-width result) ?\ )))
221 ;; But either way, reject it if it indicates
222 ;; the start of a paragraph.
223 (if (not (eq 0 (string-match paragraph-start result)))
226 (defun fill-region-as-paragraph (from to &optional justify
227 nosqueeze squeeze-after)
228 "Fill the region as one paragraph.
229 It removes any paragraph breaks in the region and extra newlines at the end,
230 indents and fills lines between the margins given by the
231 `current-left-margin' and `current-fill-column' functions.
232 It leaves point at the beginning of the line following the paragraph.
234 Normally performs justification according to the `current-justification'
235 function, but with a prefix arg, does full justification instead.
237 From a program, optional third arg JUSTIFY can specify any type of
238 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
239 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
240 means don't canonicalize spaces before that position.
242 If `sentence-end-double-space' is non-nil, then period followed by one
243 space does not end a sentence, so don't break a line there."
244 (interactive (list (region-beginning) (region-end)
245 (if current-prefix-arg 'full)))
246 ;; Arrange for undoing the fill to restore point.
247 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
248 (setq buffer-undo-list (cons (point) buffer-undo-list)))
250 ;; Make sure "to" is the endpoint.
251 (goto-char (min from to))
252 (setq to (max from to))
253 ;; Ignore blank lines at beginning of region.
254 (skip-chars-forward " \t\n")
256 (let ((from-plus-indent (point))
262 ;; Delete all but one soft newline at end of region.
263 ;; And leave TO before that one.
265 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
267 (not (and use-hard-newlines
268 (get-text-property (1- (point)) 'hard))))
269 (delete-backward-char 1)
274 ;; If there was no newline, and there is text in the paragraph, then
276 (if (and (not oneleft) (> to from-plus-indent))
278 (goto-char from-plus-indent))
280 (if (not (> to (point)))
281 nil ; There is no paragraph, only whitespace: exit now.
283 (or justify (setq justify (current-justification)))
285 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
286 (let ((fill-prefix fill-prefix))
287 ;; Figure out how this paragraph is indented, if desired.
288 (if (and adaptive-fill-mode
289 (or (null fill-prefix) (string= fill-prefix "")))
290 (setq fill-prefix (fill-context-prefix from to)))
295 (narrow-to-region (point) to)
297 (if (not justify) ; filling disabled: just check indentation
301 (if (and (not (eolp))
302 (< (current-indentation) (current-left-margin)))
303 (indent-to-left-margin))
306 (if use-hard-newlines
307 (remove-text-properties from (point-max) '(hard nil)))
308 ;; Make sure first line is indented (at least) to left margin...
309 (if (or (memq justify '(right center))
310 (< (current-indentation) (current-left-margin)))
311 (indent-to-left-margin))
312 ;; Delete the fill prefix from every line except the first.
313 ;; The first line may not even have a fill prefix.
315 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
317 (regexp-quote fill-prefix)
321 (if (>= (+ (current-left-margin) (length fill-prefix))
322 (current-fill-column))
323 (error "fill-prefix too long for specified width"))
327 (if (looking-at fpre)
328 (delete-region (point) (match-end 0)))
331 (if (looking-at fpre)
332 (goto-char (match-end 0)))
333 (setq from (point)))))
334 ;; Remove indentation from lines other than the first.
335 (beginning-of-line 2)
336 (indent-region (point) (point-max) 0)
339 ;; FROM, and point, are now before the text to fill,
340 ;; but after any fill prefix on the first line.
342 ;; Make sure sentences ending at end of line get an extra space.
343 ;; loses on split abbrevs ("Mr.\nSmith")
344 (while (re-search-forward "[.?!][])}\"']*$" nil t)
345 (or (eobp) (insert-and-inherit ?\ )))
347 ;; The character category `|' means that we can break a line
348 ;; at the character. Since we don't need a space between
349 ;; them, delete all newlines between them ...
350 (while (re-search-forward "\\c|\n\\|\n\\c|" nil t)
353 (if (= (char-before (match-beginning 0)) ?\ )
354 ;; ... except when there is end of sentence. The
355 ;; variable `sentence-end-double-space' is handled
358 (delete-region (match-beginning 0) (1+ (match-beginning 0))))))
360 (skip-chars-forward " \t")
361 ;; Then change all newlines to spaces.
362 (subst-char-in-region from (point-max) ?\n ?\ )
363 (if (and nosqueeze (not (eq justify 'full)))
365 (canonically-space-region (or squeeze-after (point)) (point-max))
366 (goto-char (point-max))
367 (delete-horizontal-space)
368 (insert-and-inherit " "))
369 (goto-char (point-min))
371 ;; This is the actual filling loop.
372 (let ((prefixcol 0) linebeg)
374 (setq linebeg (point))
375 (move-to-column (1+ (current-fill-column)))
377 (or nosqueeze (delete-horizontal-space))
378 ;; Move back to the point where we can break the line
379 ;; at. We break the line between word or after/before
380 ;; the character which has character category `|'. We
381 ;; search space, \c| followed by a character, or \c|
382 ;; following a character. If not found, place
383 ;; the point at linebeg.
384 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
385 ;; In case of space, we place the point at next to
386 ;; the point where the break occurs acutually,
387 ;; because we don't want to change the following
388 ;; logic of original Emacs. In case of \c|, the
389 ;; point is at the place where the break occurs.
391 ;; Don't break after a period followed by just one space.
392 ;; Move back to the previous place to break.
393 ;; The reason is that if a period ends up at the end of a line,
394 ;; further fills will assume it ends a sentence.
395 ;; If we now know it does not end a sentence,
396 ;; avoid putting it at the end of the line.
397 (if sentence-end-double-space
398 (while (and (> (point) (+ linebeg 2))
399 (eq (preceding-char) ?\ )
400 (not (eq (following-char) ?\ ))
401 (eq (char-after (- (point) 2)) ?\.))
403 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
405 ;; If the left margin and fill prefix by themselves
406 ;; pass the fill-column. or if they are zero
407 ;; but we have no room for even one word,
408 ;; keep at least one word or a character which has
409 ;; category `|'anyway .
410 ;; This handles ALL BUT the first line of the paragraph.
411 (if (if (zerop prefixcol)
413 (skip-chars-backward " \t" linebeg)
415 (>= prefixcol (current-column)))
416 ;; Ok, skip at least one word or one \c| character.
417 ;; Meanwhile, don't stop at a period followed by one space.
419 (move-to-column prefixcol)
420 (while (and (not (eobp))
423 sentence-end-double-space
424 (save-excursion (forward-char -1)
425 (and (looking-at "\\. ")
426 (not (looking-at "\\. ")))))))
427 (skip-chars-forward " \t")
428 ;; Skip one \c| character or one word.
429 (if (looking-at "$\\|\\c|\\|[^ \t\n]+")
430 (goto-char (match-end 0)))
432 ;; Normally, move back over the single space between the words.
433 (if (= (preceding-char) ?\ ) (forward-char -1))
434 ;; Do KINSOKU processing.
435 (if (and enable-multibyte-characters enable-kinsoku)
438 ;; If the left margin and fill prefix by themselves
439 ;; pass the fill-column, keep at least one word.
440 ;; This handles the first line of the paragraph.
441 (if (and (zerop prefixcol)
442 (let ((fill-point (point)) nchars)
444 (move-to-left-margin)
445 (setq nchars (- fill-point (point)))
448 (< nchars (length fill-prefix))
449 (string= (buffer-substring (point) fill-point)
450 (substring fill-prefix 0 nchars)))))))
451 ;; Ok, skip at least one word. But
452 ;; don't stop at a period followed by just one space.
454 (while (and (not (eobp))
457 sentence-end-double-space
458 (save-excursion (forward-char -1)
459 (and (looking-at "\\. ")
460 (not (looking-at "\\. ")))))))
461 (skip-chars-forward " \t")
462 ;; Skip one \c| character or one word.
463 (if (looking-at "$\\|\\c|\\|[^ \t\n]+")
464 (goto-char (match-end 0)))
466 ;; Check again to see if we got to the end of the paragraph.
467 (if (save-excursion (skip-chars-forward " \t") (eobp))
468 (or nosqueeze (delete-horizontal-space))
469 ;; Replace whitespace here with one newline, then indent to left
471 (skip-chars-backward " \t")
472 (if (and (= (following-char) ?\ )
473 (or (aref (char-category-set (preceding-char)) ?|)
474 (looking-at "[ \t]+\\c|")))
475 ;; We need one space at end of line so that
476 ;; further filling won't delete it. NOTE: We
477 ;; intentionally leave this one space to
478 ;; distingush the case that user wants to put
479 ;; space between \c| characters.
482 ;; Give newline the properties of the space(s) it replaces
483 (set-text-properties (1- (point)) (point)
484 (text-properties-at (point)))
485 (indent-to-left-margin)
486 ;; Insert the fill prefix after indentation.
487 ;; Set prefixcol so whitespace in the prefix won't get lost.
488 (and fill-prefix (not (equal fill-prefix ""))
490 (insert-and-inherit fill-prefix)
491 (setq prefixcol (current-column))))))
492 ;; Justify the line just ended, if desired.
495 (justify-current-line justify t t)
497 (justify-current-line justify nil t)
498 (forward-line 1))))))
499 ;; Leave point after final newline.
500 (goto-char (point-max)))
503 (defun fill-paragraph (arg)
504 "Fill paragraph at or after point. Prefix arg means justify as well.
505 If `sentence-end-double-space' is non-nil, then period followed by one
506 space does not end a sentence, so don't break a line there.
508 If `fill-paragraph-function' is non-nil, we call it (passing our
509 argument to it), and if it returns non-nil, we simply return its value."
510 (interactive (list (if current-prefix-arg 'full)))
511 (or (and fill-paragraph-function
512 (let ((function fill-paragraph-function)
513 fill-paragraph-function)
514 (funcall function arg)))
515 (let ((before (point)))
518 (or (bolp) (newline 1))
520 (beg (progn (backward-paragraph) (point))))
522 (if use-hard-newlines
523 ;; Can't use fill-region-as-paragraph, since this paragraph may
524 ;; still contain hard newlines. See fill-region.
525 (fill-region beg end arg)
526 (fill-region-as-paragraph beg end arg)))))))
528 (defun fill-region (from to &optional justify nosqueeze to-eop)
529 "Fill each of the paragraphs in the region.
530 Prefix arg (non-nil third arg, if called from program) means justify as well.
532 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
533 whitespace other than line breaks untouched, and fifth arg TO-EOP
534 non-nil means to keep filling to the end of the paragraph (or next
535 hard newline, if `use-hard-newlines' is on).
537 If `sentence-end-double-space' is non-nil, then period followed by one
538 space does not end a sentence, so don't break a line there."
539 (interactive (list (region-beginning) (region-end)
540 (if current-prefix-arg 'full)))
543 (goto-char (max from to))
545 (progn (skip-chars-backward "\n")
546 (forward-paragraph)))
548 (goto-char (setq beg (min from to)))
550 (narrow-to-region (point) end)
552 (let ((initial (point))
554 ;; If using hard newlines, break at every one for filling
555 ;; purposes rather than using paragraph breaks.
556 (if use-hard-newlines
558 (while (and (setq end (text-property-any (point) (point-max)
560 (not (= ?\n (char-after end)))
561 (not (= end (point-max))))
562 (goto-char (1+ end)))
563 (setq end (if end (min (point-max) (1+ end)) (point-max)))
565 (forward-paragraph 1)
567 (forward-paragraph -1))
570 (if (>= (point) initial)
571 (fill-region-as-paragraph (point) end justify nosqueeze)
572 (goto-char end)))))))
575 (defcustom default-justification 'left
576 "*Method of justifying text not otherwise specified.
577 Possible values are `left', `right', `full', `center', or `none'.
578 The requested kind of justification is done whenever lines are filled.
579 The `justification' text-property can locally override this variable.
580 This variable automatically becomes buffer-local when set in any fashion."
581 :type '(choice (const left)
587 (make-variable-buffer-local 'default-justification)
589 (defun current-justification ()
590 "How should we justify this line?
591 This returns the value of the text-property `justification',
592 or the variable `default-justification' if there is no text-property.
593 However, it returns nil rather than `none' to mean \"don't justify\"."
594 (let ((j (or (get-text-property
595 ;; Make sure we're looking at paragraph body.
596 (save-excursion (skip-chars-forward " \t")
597 (if (and (eobp) (not (bobp)))
598 (1- (point)) (point)))
600 default-justification)))
605 (defun set-justification (begin end value &optional whole-par)
606 "Set the region's justification style.
607 The kind of justification to use is prompted for.
608 If the mark is not active, this command operates on the current paragraph.
609 If the mark is active, the region is used. However, if the beginning and end
610 of the region are not at paragraph breaks, they are moved to the beginning and
611 end of the paragraphs they are in.
612 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
615 When calling from a program, operates just on region between BEGIN and END,
616 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
617 extended to include entire paragraphs as in the interactive command."
618 (interactive (list (if mark-active (region-beginning) (point))
619 (if mark-active (region-end) (point))
620 (let ((s (completing-read
621 "Set justification to: "
622 '(("left") ("right") ("full")
625 (if (equal s "") (error ""))
631 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
632 (paragraph-ignore-fill-prefix (if use-hard-newlines t
633 paragraph-ignore-fill-prefix)))
635 (while (and (bolp) (not (eobp))) (forward-char 1))
639 (skip-chars-backward " \t\n" begin)
643 (narrow-to-region (point-min) end)
644 (unjustify-region begin (point-max))
645 (put-text-property begin (point-max) 'justification value)
646 (fill-region begin (point-max) nil t))))
648 (defun set-justification-none (b e)
649 "Disable automatic filling for paragraphs in the region.
650 If the mark is not active, this applies to the current paragraph."
651 (interactive (list (if mark-active (region-beginning) (point))
652 (if mark-active (region-end) (point))))
653 (set-justification b e 'none t))
655 (defun set-justification-left (b e)
656 "Make paragraphs in the region left-justified.
657 This is usually the default, but see the variable `default-justification'.
658 If the mark is not active, this applies to the current paragraph."
659 (interactive (list (if mark-active (region-beginning) (point))
660 (if mark-active (region-end) (point))))
661 (set-justification b e 'left t))
663 (defun set-justification-right (b e)
664 "Make paragraphs in the region right-justified:
665 Flush at the right margin and ragged on the left.
666 If the mark is not active, this applies to the current paragraph."
667 (interactive (list (if mark-active (region-beginning) (point))
668 (if mark-active (region-end) (point))))
669 (set-justification b e 'right t))
671 (defun set-justification-full (b e)
672 "Make paragraphs in the region fully justified:
673 This makes lines flush on both margins by inserting spaces between words.
674 If the mark is not active, this applies to the current paragraph."
675 (interactive (list (if mark-active (region-beginning) (point))
676 (if mark-active (region-end) (point))))
677 (set-justification b e 'full t))
679 (defun set-justification-center (b e)
680 "Make paragraphs in the region centered.
681 If the mark is not active, this applies to the current paragraph."
682 (interactive (list (if mark-active (region-beginning) (point))
683 (if mark-active (region-end) (point))))
684 (set-justification b e 'center t))
686 ;; A line has up to six parts:
689 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
691 ;; "Indent-1" is the left-margin indentation; normally it ends at column
692 ;; given by the `current-left-margin' function.
693 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
694 ;; "Indent-2" is added to justify a line if the `current-justification' is
695 ;; `center' or `right'. In `left' and `full' justification regions, any
696 ;; whitespace there is part of the line's text, and should not be changed.
697 ;; Trailing whitespace is not counted as part of the line length when
698 ;; center- or right-justifying.
700 ;; All parts of the line are optional, although the final newline can
701 ;; only be missing on the last line of the buffer.
703 (defun justify-current-line (&optional how eop nosqueeze)
704 "Do some kind of justification on this line.
705 Normally does full justification: adds spaces to the line to make it end at
706 the column given by `current-fill-column'.
707 Optional first argument HOW specifies alternate type of justification:
708 it can be `left', `right', `full', `center', or `none'.
709 If HOW is t, will justify however the `current-justification' function says to.
710 If HOW is nil or missing, full justification is done by default.
711 Second arg EOP non-nil means that this is the last line of the paragraph, so
712 it will not be stretched by full justification.
713 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
714 otherwise it is made canonical."
716 (if (eq t how) (setq how (or (current-justification) 'none))
717 (if (null how) (setq how 'full)
718 (or (memq how '(none left right center))
720 (or (memq how '(none left)) ; No action required for these.
721 (let ((fc (current-fill-column))
723 fp-end ; point at end of fill prefix
724 beg ; point at beginning of line's text
725 end ; point at end of line's text
726 indent ; column of `beg'
727 endcol ; column of `end'
728 ncols) ; new indent point or offset
730 ;; Check if this is the last line of the paragraph.
731 (if (and use-hard-newlines (null eop)
732 (get-text-property (point) 'hard))
734 (skip-chars-backward " \t")
735 ;; Quick exit if it appears to be properly justified already
736 ;; or there is no text.
738 (and (memq how '(full right))
739 (= (current-column) fc)))
743 (skip-chars-forward " \t")
744 ;; Skip over fill-prefix.
746 (not (string-equal fill-prefix ""))
749 (point) (min (point-max) (+ (length fill-prefix)
751 (forward-char (length fill-prefix))
752 (if (and adaptive-fill-mode
753 (looking-at adaptive-fill-regexp))
754 (goto-char (match-end 0))))
755 (setq fp-end (point))
756 (skip-chars-forward " \t")
757 ;; This is beginning of the line's text.
758 (setq indent (current-column))
761 (setq endcol (current-column))
763 ;; HOW can't be null or left--we would have exited already
764 (cond ((eq 'right how)
765 (setq ncols (- fc endcol))
767 ;; Need to remove some indentation
769 (progn (goto-char fp-end)
770 (if (< (current-column) (+ indent ncols))
771 (move-to-column (+ indent ncols) t))
773 (progn (move-to-column indent) (point)))
776 (indent-to (+ indent ncols))
777 ;; If point was at beginning of text, keep it there.
779 (move-marker pos (point)))))
782 ;; Figure out how much indentation is needed
783 (setq ncols (+ (current-left-margin)
784 (/ (- fc (current-left-margin) ;avail. space
785 (- endcol indent)) ;text width
788 ;; Have too much indentation - remove some
790 (progn (goto-char fp-end)
791 (if (< (current-column) ncols)
792 (move-to-column ncols t))
794 (progn (move-to-column indent) (point)))
795 ;; Have too little - add some
798 ;; If point was at beginning of text, keep it there.
800 (move-marker pos (point)))))
803 ;; Insert extra spaces between words to justify line
805 (narrow-to-region beg end)
807 (canonically-space-region beg end))
808 (goto-char (point-max))
809 (setq ncols (- fc endcol))
810 ;; Ncols is number of additional spaces needed
813 (search-backward " " nil t))
815 (let ((nmove (+ 3 (random 3))))
817 (or (search-backward " " nil t)
819 (goto-char (point-max))
820 (search-backward " ")))
821 (skip-chars-backward " ")
822 (setq nmove (1- nmove))))
823 (insert-and-inherit " ")
824 (skip-chars-backward " ")
825 (setq ncols (1- ncols)))))))
826 (t (error "Unknown justification value"))))
828 (move-marker pos nil)))
831 (defun unjustify-current-line ()
832 "Remove justification whitespace from current line.
833 If the line is centered or right-justified, this function removes any
834 indentation past the left margin. If the line is full-justified, it removes
835 extra spaces between words. It does nothing in other justification modes."
836 (let ((justify (current-justification)))
837 (cond ((eq 'left justify) nil)
838 ((eq nil justify) nil)
839 ((eq 'full justify) ; full justify: remove extra spaces
840 (beginning-of-line-text)
841 (canonically-space-region
842 (point) (save-excursion (end-of-line) (point))))
843 ((memq justify '(center right))
845 (move-to-left-margin nil t)
846 ;; Position ourselves after any fill-prefix.
848 (not (string-equal fill-prefix ""))
851 (point) (min (point-max) (+ (length fill-prefix)
853 (forward-char (length fill-prefix)))
854 (delete-region (point) (progn (skip-chars-forward " \t")
857 (defun unjustify-region (&optional begin end)
858 "Remove justification whitespace from region.
859 For centered or right-justified regions, this function removes any indentation
860 past the left margin from each line. For full-justified lines, it removes
861 extra spaces between words. It does nothing in other justification modes.
862 Arguments BEGIN and END are optional; default is the whole buffer."
865 (if end (narrow-to-region (point-min) end))
866 (goto-char (or begin (point-min)))
868 (unjustify-current-line)
872 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
873 "Fill paragraphs within the region, allowing varying indentation within each.
874 This command divides the region into \"paragraphs\",
875 only at paragraph-separator lines, then fills each paragraph
876 using as the fill prefix the smallest indentation of any line
879 When calling from a program, pass range to fill as first two arguments.
881 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
882 JUSTIFY to justify paragraphs (prefix arg),
883 MAIL-FLAG for a mail message, i. e. don't fill header lines."
884 (interactive (list (region-beginning) (region-end)
885 (if current-prefix-arg 'full)))
886 (let ((fill-individual-varying-indent t))
887 (fill-individual-paragraphs min max justifyp mailp)))
889 (defun fill-individual-paragraphs (min max &optional justify mailp)
890 "Fill paragraphs of uniform indentation within the region.
891 This command divides the region into \"paragraphs\",
892 treating every change in indentation level as a paragraph boundary,
893 then fills each paragraph using its indentation level as the fill prefix.
895 When calling from a program, pass range to fill as first two arguments.
897 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
898 JUSTIFY to justify paragraphs (prefix arg),
899 MAIL-FLAG for a mail message, i. e. don't fill header lines."
900 (interactive (list (region-beginning) (region-end)
901 (if current-prefix-arg 'full)))
906 (narrow-to-region (point) max)
908 (while (and (not (eobp))
909 (or (looking-at "[ \t]*[^ \t\n]+:")
910 (looking-at "[ \t]*$")))
911 (if (looking-at "[ \t]*[^ \t\n]+:")
912 (search-forward "\n\n" nil 'move)
914 (narrow-to-region (point) max)
915 ;; Loop over paragraphs.
916 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
917 (move-to-left-margin)
918 (let ((start (point))
919 fill-prefix fill-prefix-regexp)
920 ;; Find end of paragraph, and compute the smallest fill-prefix
921 ;; that fits all the lines in this paragraph.
923 ;; Update the fill-prefix on the first line
924 ;; and whenever the prefix good so far is too long.
925 (if (not (and fill-prefix
926 (looking-at fill-prefix-regexp)))
928 (if (and adaptive-fill-mode adaptive-fill-regexp
929 (looking-at adaptive-fill-regexp))
933 (save-excursion (skip-chars-forward " \t")
935 fill-prefix-regexp (regexp-quote fill-prefix)))
938 ;; If forward-line went past a newline,
939 ;; move further to the left margin.
940 (move-to-left-margin))
941 ;; Now stop the loop if end of paragraph.
943 (if fill-individual-varying-indent
944 ;; If this line is a separator line, with or
945 ;; without prefix, end the paragraph.
947 (not (looking-at paragraph-separate))
949 (not (and (looking-at fill-prefix-regexp)
950 (progn (forward-char (length fill-prefix))
951 (looking-at paragraph-separate))))))
952 ;; If this line has more or less indent
953 ;; than the fill prefix wants, end the paragraph.
954 (and (looking-at fill-prefix-regexp)
956 (not (progn (forward-char (length fill-prefix))
957 (or (looking-at paragraph-separate)
958 (looking-at paragraph-start))))))))))
959 ;; Fill this paragraph, but don't add a newline at the end.
960 (let ((had-newline (bolp)))
961 (fill-region-as-paragraph start (point) justify)
962 (or had-newline (delete-char -1))))))))
964 ;;; fill.el ends here