1 ;;; fill.el --- fill commands for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 96, 97, 1999 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.
43 If you change this, you should also change `sentence-end'.
44 See Info node `Sentences'."
48 (defcustom colon-double-space nil
49 "*Non-nil means put two spaces after a colon when filling."
53 (defcustom sentence-end-without-period nil
54 "*Non-nil means a sentence will end without period.
55 For example, Thai text ends with double space but without period."
59 (defvar fill-paragraph-function nil
60 "Mode-specific function to fill a paragraph, or nil if there is none.
61 If the function returns nil, then `fill-paragraph' does its normal work.")
63 (defvar enable-kinsoku t
64 "*Non-nil means enable \"kinsoku\" processing on filling paragraph.
65 Kinsoku processing is designed to prevent certain characters from being
66 placed at the beginning or end of a line by filling.
67 See the documentation of `kinsoku' for more information.")
69 (defun set-fill-prefix ()
70 "Set the fill prefix to the current line up to point.
71 Filling expects lines to start with the fill prefix and
72 reinserts the fill prefix in each resulting line."
74 (setq fill-prefix (buffer-substring
75 (save-excursion (move-to-left-margin) (point))
77 (if (equal fill-prefix "")
78 (setq fill-prefix nil))
80 (message "fill-prefix: \"%s\"" fill-prefix)
81 (message "fill-prefix cancelled")))
83 (defcustom adaptive-fill-mode t
84 "*Non-nil means determine a paragraph's fill prefix from its text."
88 (defcustom adaptive-fill-regexp "[ \t]*\\([-|#;>*]+ *\\|(?[0-9]+[.)] *\\)*"
89 "*Regexp to match text at start of line that constitutes indentation.
90 If Adaptive Fill mode is enabled, a prefix matching this pattern
91 on the first and second lines of a paragraph is used as the
92 standard indentation for the whole paragraph.
94 If the paragraph has just one line, the indentation is taken from that
95 line, but in that case `adaptive-fill-first-line-regexp' also plays
100 (defcustom adaptive-fill-first-line-regexp "\\`[ \t]*\\'"
101 "*Regexp specifying whether to set fill prefix from a one-line paragraph.
102 When a paragraph has just one line, then after `adaptive-fill-regexp'
103 finds the prefix at the beginning of the line, if it doesn't
104 match this regexp, it is replaced with whitespace.
106 By default, this regexp matches sequences of just spaces and tabs.
108 However, we never use a prefix from a one-line paragraph
109 if it would act as a paragraph-starter on the second line."
113 (defcustom adaptive-fill-function nil
114 "*Function to call to choose a fill prefix for a paragraph.
115 This function is used when `adaptive-fill-regexp' does not match."
119 (defun current-fill-column ()
120 "Return the fill-column to use for this line.
121 The fill-column to use for a buffer is stored in the variable `fill-column',
122 but can be locally modified by the `right-margin' text property, which is
123 subtracted from `fill-column'.
125 The fill column to use for a line is the first column at which the column
126 number equals or exceeds the local fill-column - right-margin difference."
129 (let* ((here (progn (beginning-of-line) (point)))
131 (eol (progn (end-of-line) (point)))
132 margin fill-col change col)
133 ;; Look separately at each region of line with a different right-margin.
134 (while (and (setq margin (get-text-property here 'right-margin)
135 fill-col (- fill-column (or margin 0))
136 change (text-property-not-all
137 here eol 'right-margin margin))
138 (progn (goto-char (1- change))
139 (setq col (current-column))
143 (max here-col fill-col)))))
145 (defun canonically-space-region (beg end)
146 "Remove extra spaces between words in region.
147 Leave one space between words, two at end of sentences or after colons
148 \(depending on values of `sentence-end-double-space', `colon-double-space',
149 and `sentence-end-without-period').
150 Remove indentation from each line."
154 ;; Nuke tabs; they get screwed up in a fill.
155 ;; This is quick, but loses when a tab follows the end of a sentence.
156 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
158 (subst-char-in-region beg end ?\t ?\ )
159 (while (and (< (point) end)
160 (re-search-forward " *" end t))
162 (+ (match-beginning 0)
163 ;; Determine number of spaces to leave:
165 (skip-chars-backward " ]})\"'")
166 (cond ((and sentence-end-double-space
167 (or (memq (preceding-char) '(?. ?? ?!))
168 (and sentence-end-without-period
169 (= (char-syntax (preceding-char)) ?w)))) 2)
170 ((and colon-double-space
171 (= (preceding-char) ?:)) 2)
172 ((char-equal (preceding-char) ?\n) 0)
175 ;; Make sure sentences ending at end of line get an extra space.
176 ;; loses on split abbrevs ("Mr.\nSmith")
178 (let ((eol-double-space-re (if colon-double-space
181 (while (and (< (point) end)
182 (re-search-forward eol-double-space-re end t))
183 ;; We insert before markers in case a caller such as
184 ;; do-auto-fill has done a save-excursion with point at the end
185 ;; of the line and wants it to stay at the end of the line.
186 (insert-before-markers-and-inherit ? )))))
188 (defun fill-common-string-prefix (s1 s2)
189 "Return the longest common prefix of strings S1 and S2, or nil if none."
190 (let ((cmp (compare-strings s1 nil nil s2 nil nil)))
193 (setq cmp (1- (abs cmp)))
195 (substring s1 0 cmp)))))
197 (defun fill-context-prefix (from to &optional first-line-regexp)
198 "Compute a fill prefix from the text between FROM and TO.
199 This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
200 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
201 we reject a prefix based on a one-line paragraph if that prefix would
202 act as a paragraph-separator."
203 (or first-line-regexp
204 (setq first-line-regexp adaptive-fill-first-line-regexp))
207 (if (eolp) (forward-line 1))
208 ;; Move to the second line unless there is just one.
209 (let ((firstline (point))
211 ;; Non-nil if we are on the second line.
215 (move-to-left-margin)
217 (setq first-line-prefix
218 (cond ((looking-at paragraph-start) nil)
219 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
220 (buffer-substring-no-properties start (match-end 0)))
221 (adaptive-fill-function (funcall adaptive-fill-function))))
224 (goto-char firstline)
226 (move-to-left-margin)
228 (setq second-line-prefix
229 (cond ((looking-at paragraph-start) nil)
230 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
231 (buffer-substring-no-properties start (match-end 0)))
232 (adaptive-fill-function (funcall adaptive-fill-function)))))
234 ;; If we get a fill prefix from the second line,
235 ;; make sure it or something compatible is on the first line too.
236 (and second-line-prefix first-line-prefix
237 ;; If the first line has the second line prefix too, use it.
238 (if (or (string-match (concat "\\`"
239 (regexp-quote second-line-prefix)
242 ;; If the second line prefix is whitespace, use it.
243 (string-match "\\`[ \t]+\\'" second-line-prefix))
246 ;; If using the common prefix of first-line-prefix
247 ;; and second-line-prefix leads to problems, consider
248 ;; to restore the code below that's commented out,
249 ;; and document why a common prefix cannot be used.
251 ; ;; If the second line has the first line prefix,
252 ; ;; plus whitespace, use the part that the first line shares.
253 ; (if (string-match (concat "\\`"
254 ; (regexp-quote first-line-prefix)
256 ; second-line-prefix)
257 ; first-line-prefix)))
259 ;; Use the longest common substring of both prefixes,
261 (fill-common-string-prefix first-line-prefix
262 second-line-prefix)))
263 ;; If we get a fill prefix from a one-line paragraph,
264 ;; maybe change it to whitespace,
265 ;; and check that it isn't a paragraph starter.
266 (if first-line-prefix
268 ;; If first-line-prefix comes from the first line,
269 ;; see if it seems reasonable to use for all lines.
270 ;; If not, replace it with whitespace.
271 (if (or (and first-line-regexp
272 (string-match first-line-regexp
274 (and comment-start-skip
275 (string-match comment-start-skip
278 (make-string (string-width first-line-prefix) ?\ ))))
279 ;; But either way, reject it if it indicates the start
280 ;; of a paragraph when text follows it.
281 (if (not (eq 0 (string-match paragraph-start
282 (concat result "a"))))
285 (defvar fill-nobreak-predicate nil
286 "If non-nil, a predicate for recognizing places not to break a line.
287 The predicate is called with no arguments, with point at the place
288 to be tested. If it returns t, fill commands do not break the line there.")
290 ;; Put `fill-find-break-point-function' property to charsets which
291 ;; require special functions to find line breaking point.
292 (let ((alist '((katakana-jisx0201 . kinsoku)
293 (chinese-gb2312 . kinsoku)
294 (japanese-jisx0208 . kinsoku)
295 (japanese-jisx0212 . kinsoku)
296 (chinese-big5-1 . kinsoku)
297 (chinese-big5-2 . kinsoku))))
299 (put-charset-property (car (car alist)) 'fill-find-break-point-function
301 (setq alist (cdr alist))))
303 (defun fill-find-break-point (limit)
304 "Move point to a proper line breaking position of the current line.
305 Don't move back past the buffer position LIMIT.
307 This function is called when we are going to break the current line
308 after or before a non-ascii character. If the charset of the
309 character has the property `fill-find-break-point-function', this
310 function calls the property value as a function with one arg LINEBEG.
311 If the charset has no such property, do nothing."
312 (let* ((ch (following-char))
313 (charset (char-charset ch))
315 (if (eq charset 'ascii)
316 (setq ch (preceding-char)
317 charset (char-charset ch)))
318 (if (charsetp charset)
320 (get-charset-property charset 'fill-find-break-point-function)))
321 (if (and func (fboundp func))
322 (funcall func limit))))
324 (defun fill-region-as-paragraph (from to &optional justify
325 nosqueeze squeeze-after)
326 "Fill the region as one paragraph.
327 It removes any paragraph breaks in the region and extra newlines at the end,
328 indents and fills lines between the margins given by the
329 `current-left-margin' and `current-fill-column' functions.
330 \(In most cases, the variable `fill-column' controls the width.)
331 It leaves point at the beginning of the line following the paragraph.
333 Normally performs justification according to the `current-justification'
334 function, but with a prefix arg, does full justification instead.
336 From a program, optional third arg JUSTIFY can specify any type of
337 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
338 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
339 means don't canonicalize spaces before that position.
341 If `sentence-end-double-space' is non-nil, then period followed by one
342 space does not end a sentence, so don't break a line there."
344 (barf-if-buffer-read-only)
345 (list (region-beginning) (region-end)
346 (if current-prefix-arg 'full))))
347 (unless (memq justify '(t nil none full center left right))
348 (setq justify 'full))
349 ;; Arrange for undoing the fill to restore point.
350 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
351 (setq buffer-undo-list (cons (point) buffer-undo-list)))
353 ;; Make sure "to" is the endpoint.
354 (goto-char (min from to))
355 (setq to (max from to))
356 ;; Ignore blank lines at beginning of region.
357 (skip-chars-forward " \t\n")
359 (let ((from-plus-indent (point))
365 ;; Delete all but one soft newline at end of region.
366 ;; And leave TO before that one.
368 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
370 (not (and use-hard-newlines
371 (get-text-property (1- (point)) 'hard))))
372 (delete-backward-char 1)
376 ;;; ;; If there was no newline, and there is text in the paragraph, then
377 ;;; ;; create a newline.
378 ;;; (if (and (not oneleft) (> to from-plus-indent))
380 (goto-char from-plus-indent))
382 (if (not (> to (point)))
383 nil ; There is no paragraph, only whitespace: exit now.
385 (or justify (setq justify (current-justification)))
387 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
388 (let ((fill-prefix fill-prefix))
389 ;; Figure out how this paragraph is indented, if desired.
390 (if (and adaptive-fill-mode
391 (or (null fill-prefix) (string= fill-prefix "")))
392 (setq fill-prefix (fill-context-prefix from to)))
397 (narrow-to-region (point) to)
399 (if (not justify) ; filling disabled: just check indentation
403 (if (and (not (eolp))
404 (< (current-indentation) (current-left-margin)))
405 (indent-to-left-margin))
408 (if use-hard-newlines
409 (remove-text-properties from (point-max) '(hard nil)))
410 ;; Make sure first line is indented (at least) to left margin...
411 (if (or (memq justify '(right center))
412 (< (current-indentation) (current-left-margin)))
413 (indent-to-left-margin))
414 ;; Delete the fill prefix from every line except the first.
415 ;; The first line may not even have a fill prefix.
417 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
419 (regexp-quote fill-prefix)
423 (if (>= (+ (current-left-margin) (length fill-prefix))
424 (current-fill-column))
425 (error "fill-prefix too long for specified width"))
429 (if (looking-at fpre)
430 (delete-region (point) (match-end 0)))
433 (if (looking-at fpre)
434 (goto-char (match-end 0)))
435 (setq from (point)))))
436 ;; Remove indentation from lines other than the first.
437 (beginning-of-line 2)
438 (indent-region (point) (point-max) 0)
441 ;; FROM, and point, are now before the text to fill,
442 ;; but after any fill prefix on the first line.
444 ;; Make sure sentences ending at end of line get an extra space.
445 ;; loses on split abbrevs ("Mr.\nSmith")
446 (let ((eol-double-space-re (if colon-double-space
449 (while (re-search-forward eol-double-space-re nil t)
450 (or (eobp) (insert-and-inherit ?\ ))))
453 (if enable-multibyte-characters
454 ;; Delete unnecessay newlines surrounded by words. The
455 ;; character category `|' means that we can break a line
456 ;; at the character. And, charset property
457 ;; `nospace-between-words' tells how to concatenate
458 ;; words. If the value is non-nil, never put spaces
459 ;; between words, thus delete a newline between them.
460 ;; If the value is nil, delete a newline only when a
461 ;; character preceding a newline has text property
462 ;; `nospace-between-words'.
463 (while (search-forward "\n" nil t)
464 (let ((prev (char-before (match-beginning 0)))
465 (next (following-char)))
466 (if (and (or (aref (char-category-set next) ?|)
467 (aref (char-category-set prev) ?|))
468 (or (get-charset-property (char-charset prev)
469 'nospace-between-words)
470 (get-text-property (1- (match-beginning 0))
471 'nospace-between-words)))
475 (skip-chars-forward " \t")
476 ;; Then change all newlines to spaces.
477 (subst-char-in-region from (point-max) ?\n ?\ )
478 (if (and nosqueeze (not (eq justify 'full)))
480 (canonically-space-region (or squeeze-after (point)) (point-max))
481 (goto-char (point-max))
482 (delete-horizontal-space)
483 (insert-and-inherit " "))
484 (goto-char (point-min))
486 ;; This is the actual filling loop.
487 (let ((prefixcol 0) linebeg)
489 (setq linebeg (point))
490 (move-to-column (1+ (current-fill-column)))
492 (or nosqueeze (delete-horizontal-space))
493 ;; Move back to the point where we can break the line
494 ;; at. We break the line between word or after/before
495 ;; the character which has character category `|'. We
496 ;; search space, \c| followed by a character, or \c|
497 ;; following a character. If not found, place
498 ;; the point at linebeg.
499 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
500 ;; In case of space, we place the point at next to
501 ;; the point where the break occurs acutually,
502 ;; because we don't want to change the following
503 ;; logic of original Emacs. In case of \c|, the
504 ;; point is at the place where the break occurs.
506 ;; Don't break after a period followed by just one space.
507 ;; Move back to the previous place to break.
508 ;; The reason is that if a period ends up at the end of a line,
509 ;; further fills will assume it ends a sentence.
510 ;; If we now know it does not end a sentence,
511 ;; avoid putting it at the end of the line.
512 (while (or (and sentence-end-double-space
513 (> (point) (+ linebeg 2))
514 (eq (preceding-char) ?\ )
515 (not (eq (following-char) ?\ ))
516 (eq (char-after (- (point) 2)) ?\.)
517 (progn (forward-char -2) t))
518 (and fill-nobreak-predicate
519 (funcall fill-nobreak-predicate)
520 (goto-char (match-beginning 0))))
521 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
523 ;; If the left margin and fill prefix by themselves
524 ;; pass the fill-column. or if they are zero
525 ;; but we have no room for even one word,
526 ;; keep at least one word or a character which has
527 ;; category `|'anyway .
528 ;; This handles ALL BUT the first line of the paragraph.
529 (if (if (zerop prefixcol)
531 (skip-chars-backward " \t" linebeg)
533 (>= prefixcol (current-column)))
534 ;; Ok, skip at least one word or one \c| character.
535 ;; Meanwhile, don't stop at a period followed by one space.
537 (move-to-column prefixcol)
538 (while (and (not (eobp))
541 sentence-end-double-space
542 (save-excursion (forward-char -1)
543 (and (looking-at "\\. ")
544 (not (looking-at "\\. ")))))
545 (and fill-nobreak-predicate
546 (funcall fill-nobreak-predicate))))
547 ;; Find a breakable point while ignoring the
549 (skip-chars-forward " \t")
550 (if (looking-at "\\c|")
552 (let ((pos (save-excursion
553 (skip-chars-forward "^ \n\t")
555 (if (re-search-forward "\\c|" pos t)
559 ;; Normally, move back over the single space between the words.
560 (if (= (preceding-char) ?\ ) (forward-char -1))
562 (if enable-multibyte-characters
563 ;; If we are going to break the line after or
564 ;; before a non-ascii character, we may have to
565 ;; run a special function for the charset of the
566 ;; character to find the correct break point.
567 (if (not (and (eq (charset-after (1- (point))) 'ascii)
568 (eq (charset-after (point)) 'ascii)))
569 ;; Make sure we take SOMETHING after the
570 ;; fill prefix if any.
571 (fill-find-break-point
574 (move-to-column prefixcol)
577 ;; If the left margin and fill prefix by themselves
578 ;; pass the fill-column, keep at least one word.
579 ;; This handles the first line of the paragraph.
580 (if (and (zerop prefixcol)
581 (let ((fill-point (point)) nchars)
583 (move-to-left-margin)
584 (setq nchars (- fill-point (point)))
587 (< nchars (length fill-prefix))
588 (string= (buffer-substring (point) fill-point)
589 (substring fill-prefix 0 nchars)))))))
590 ;; Ok, skip at least one word. But
591 ;; don't stop at a period followed by just one space.
593 (while (and (not (eobp))
596 sentence-end-double-space
597 (save-excursion (forward-char -1)
598 (and (looking-at "\\. ")
599 (not (looking-at "\\. ")))))
600 (and fill-nobreak-predicate
601 (funcall fill-nobreak-predicate))))
602 ;; Find a breakable point while ignoring the
604 (skip-chars-forward " \t")
605 (if (looking-at "\\c|")
607 (let ((pos (save-excursion
608 (skip-chars-forward "^ \n\t")
610 (if (re-search-forward "\\c|" pos t)
614 ;; Check again to see if we got to the end of the paragraph.
615 (if (save-excursion (skip-chars-forward " \t") (eobp))
616 (or nosqueeze (delete-horizontal-space))
617 ;; Replace whitespace here with one newline, then indent to left
619 (skip-chars-backward " \t")
620 (if (and (= (following-char) ?\ )
621 (or (aref (char-category-set (preceding-char)) ?|)
622 (looking-at "[ \t]+\\c|")))
623 ;; We need one space at end of line so that
624 ;; further filling won't delete it. NOTE: We
625 ;; intentionally leave this one space to
626 ;; distingush the case that user wants to put
627 ;; space between \c| characters.
630 ;; Give newline the properties of the space(s) it replaces
631 (set-text-properties (1- (point)) (point)
632 (text-properties-at (point)))
633 (indent-to-left-margin)
634 ;; Insert the fill prefix after indentation.
635 ;; Set prefixcol so whitespace in the prefix won't get lost.
636 (and fill-prefix (not (equal fill-prefix ""))
638 (insert-and-inherit fill-prefix)
639 (setq prefixcol (current-column))))))
640 ;; Justify the line just ended, if desired.
642 (if (save-excursion (skip-chars-forward " \t") (eobp))
644 (delete-horizontal-space)
645 (justify-current-line justify t t))
647 (justify-current-line justify nil t)
648 (forward-line 1))))))
649 ;; Leave point after final newline.
650 (goto-char (point-max)))
654 (defun fill-paragraph (arg)
655 "Fill paragraph at or after point. Prefix arg means justify as well.
656 If `sentence-end-double-space' is non-nil, then period followed by one
657 space does not end a sentence, so don't break a line there.
658 the variable `fill-column' controls the width for filling.
660 If `fill-paragraph-function' is non-nil, we call it (passing our
661 argument to it), and if it returns non-nil, we simply return its value."
663 (barf-if-buffer-read-only)
664 (list (if current-prefix-arg 'full))))
665 (or (and fill-paragraph-function
666 (let ((function fill-paragraph-function)
667 fill-paragraph-function)
668 (funcall function arg)))
669 (let ((before (point))
670 ;; If fill-paragraph is called recursively,
671 ;; don't give fill-paragraph-function a second chance.
672 fill-paragraph-function)
675 (or (bolp) (newline 1))
677 (beg (progn (backward-paragraph) (point))))
679 (if use-hard-newlines
680 ;; Can't use fill-region-as-paragraph, since this paragraph may
681 ;; still contain hard newlines. See fill-region.
682 (fill-region beg end arg)
683 (fill-region-as-paragraph beg end arg)))))))
685 (defun fill-region (from to &optional justify nosqueeze to-eop)
686 "Fill each of the paragraphs in the region.
687 A prefix arg means justify as well.
688 Ordinarily the variable `fill-column' controls the width.
690 Noninteractively, the third argument JUSTIFY specifies which
691 kind of justification to do: `full', `left', `right', `center',
692 or `none' (equivalent to nil). t means handle each paragraph
693 as specified by its text properties.
695 The fourth arg NOSQUEEZE non-nil means to leave
696 whitespace other than line breaks untouched, and fifth arg TO-EOP
697 non-nil means to keep filling to the end of the paragraph (or next
698 hard newline, if `use-hard-newlines' is on).
700 If `sentence-end-double-space' is non-nil, then period followed by one
701 space does not end a sentence, so don't break a line there."
703 (barf-if-buffer-read-only)
704 (list (region-beginning) (region-end)
705 (if current-prefix-arg 'full))))
706 (unless (memq justify '(t nil none full center left right))
707 (setq justify 'full))
710 (goto-char (max from to))
712 (progn (skip-chars-backward "\n")
713 (forward-paragraph)))
715 (goto-char (setq beg (min from to)))
717 (narrow-to-region (point) end)
719 (let ((initial (point))
721 ;; If using hard newlines, break at every one for filling
722 ;; purposes rather than using paragraph breaks.
723 (if use-hard-newlines
725 (while (and (setq end (text-property-any (point) (point-max)
727 (not (= ?\n (char-after end)))
728 (not (= end (point-max))))
729 (goto-char (1+ end)))
730 (setq end (if end (min (point-max) (1+ end)) (point-max)))
732 (forward-paragraph 1)
734 (forward-paragraph -1))
737 (if (>= (point) initial)
738 (fill-region-as-paragraph (point) end justify nosqueeze)
739 (goto-char end)))))))
742 (defcustom default-justification 'left
743 "*Method of justifying text not otherwise specified.
744 Possible values are `left', `right', `full', `center', or `none'.
745 The requested kind of justification is done whenever lines are filled.
746 The `justification' text-property can locally override this variable.
747 This variable automatically becomes buffer-local when set in any fashion."
748 :type '(choice (const left)
754 (make-variable-buffer-local 'default-justification)
756 (defun current-justification ()
757 "How should we justify this line?
758 This returns the value of the text-property `justification',
759 or the variable `default-justification' if there is no text-property.
760 However, it returns nil rather than `none' to mean \"don't justify\"."
761 (let ((j (or (get-text-property
762 ;; Make sure we're looking at paragraph body.
763 (save-excursion (skip-chars-forward " \t")
764 (if (and (eobp) (not (bobp)))
765 (1- (point)) (point)))
767 default-justification)))
772 (defun set-justification (begin end value &optional whole-par)
773 "Set the region's justification style.
774 The kind of justification to use is prompted for.
775 If the mark is not active, this command operates on the current paragraph.
776 If the mark is active, the region is used. However, if the beginning and end
777 of the region are not at paragraph breaks, they are moved to the beginning and
778 end of the paragraphs they are in.
779 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
782 When calling from a program, operates just on region between BEGIN and END,
783 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
784 extended to include entire paragraphs as in the interactive command."
785 (interactive (list (if mark-active (region-beginning) (point))
786 (if mark-active (region-end) (point))
787 (let ((s (completing-read
788 "Set justification to: "
789 '(("left") ("right") ("full")
792 (if (equal s "") (error ""))
798 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
799 (paragraph-ignore-fill-prefix (if use-hard-newlines t
800 paragraph-ignore-fill-prefix)))
802 (while (and (bolp) (not (eobp))) (forward-char 1))
806 (skip-chars-backward " \t\n" begin)
810 (narrow-to-region (point-min) end)
811 (unjustify-region begin (point-max))
812 (put-text-property begin (point-max) 'justification value)
813 (fill-region begin (point-max) nil t))))
815 (defun set-justification-none (b e)
816 "Disable automatic filling for paragraphs in the region.
817 If the mark is not active, this applies to the current paragraph."
818 (interactive (list (if mark-active (region-beginning) (point))
819 (if mark-active (region-end) (point))))
820 (set-justification b e 'none t))
822 (defun set-justification-left (b e)
823 "Make paragraphs in the region left-justified.
824 This is usually the default, but see the variable `default-justification'.
825 If the mark is not active, this applies to the current paragraph."
826 (interactive (list (if mark-active (region-beginning) (point))
827 (if mark-active (region-end) (point))))
828 (set-justification b e 'left t))
830 (defun set-justification-right (b e)
831 "Make paragraphs in the region right-justified:
832 Flush at the right margin and ragged on the left.
833 If the mark is not active, this applies to the current paragraph."
834 (interactive (list (if mark-active (region-beginning) (point))
835 (if mark-active (region-end) (point))))
836 (set-justification b e 'right t))
838 (defun set-justification-full (b e)
839 "Make paragraphs in the region fully justified:
840 This makes lines flush on both margins by inserting spaces between words.
841 If the mark is not active, this applies to the current paragraph."
842 (interactive (list (if mark-active (region-beginning) (point))
843 (if mark-active (region-end) (point))))
844 (set-justification b e 'full t))
846 (defun set-justification-center (b e)
847 "Make paragraphs in the region centered.
848 If the mark is not active, this applies to the current paragraph."
849 (interactive (list (if mark-active (region-beginning) (point))
850 (if mark-active (region-end) (point))))
851 (set-justification b e 'center t))
853 ;; A line has up to six parts:
856 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
858 ;; "Indent-1" is the left-margin indentation; normally it ends at column
859 ;; given by the `current-left-margin' function.
860 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
861 ;; "Indent-2" is added to justify a line if the `current-justification' is
862 ;; `center' or `right'. In `left' and `full' justification regions, any
863 ;; whitespace there is part of the line's text, and should not be changed.
864 ;; Trailing whitespace is not counted as part of the line length when
865 ;; center- or right-justifying.
867 ;; All parts of the line are optional, although the final newline can
868 ;; only be missing on the last line of the buffer.
870 (defun justify-current-line (&optional how eop nosqueeze)
871 "Do some kind of justification on this line.
872 Normally does full justification: adds spaces to the line to make it end at
873 the column given by `current-fill-column'.
874 Optional first argument HOW specifies alternate type of justification:
875 it can be `left', `right', `full', `center', or `none'.
876 If HOW is t, will justify however the `current-justification' function says to.
877 If HOW is nil or missing, full justification is done by default.
878 Second arg EOP non-nil means that this is the last line of the paragraph, so
879 it will not be stretched by full justification.
880 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
881 otherwise it is made canonical."
883 (if (eq t how) (setq how (or (current-justification) 'none))
884 (if (null how) (setq how 'full)
885 (or (memq how '(none left right center))
887 (or (memq how '(none left)) ; No action required for these.
888 (let ((fc (current-fill-column))
890 fp-end ; point at end of fill prefix
891 beg ; point at beginning of line's text
892 end ; point at end of line's text
893 indent ; column of `beg'
894 endcol ; column of `end'
895 ncols ; new indent point or offset
896 (nspaces 0) ; number of spaces between words
897 ; in line (not space characters)
898 fracspace ; fractional amount of space to be
899 ; added between each words
900 (curr-fracspace 0) ; current fractional space amount
903 ;; Check if this is the last line of the paragraph.
904 (if (and use-hard-newlines (null eop)
905 (get-text-property (point) 'hard))
907 (skip-chars-backward " \t")
908 ;; Quick exit if it appears to be properly justified already
909 ;; or there is no text.
911 (and (memq how '(full right))
912 (= (current-column) fc)))
916 (skip-chars-forward " \t")
917 ;; Skip over fill-prefix.
919 (not (string-equal fill-prefix ""))
922 (point) (min (point-max) (+ (length fill-prefix)
924 (forward-char (length fill-prefix))
925 (if (and adaptive-fill-mode
926 (looking-at adaptive-fill-regexp))
927 (goto-char (match-end 0))))
928 (setq fp-end (point))
929 (skip-chars-forward " \t")
930 ;; This is beginning of the line's text.
931 (setq indent (current-column))
934 (setq endcol (current-column))
936 ;; HOW can't be null or left--we would have exited already
937 (cond ((eq 'right how)
938 (setq ncols (- fc endcol))
940 ;; Need to remove some indentation
942 (progn (goto-char fp-end)
943 (if (< (current-column) (+ indent ncols))
944 (move-to-column (+ indent ncols) t))
946 (progn (move-to-column indent) (point)))
949 (indent-to (+ indent ncols))
950 ;; If point was at beginning of text, keep it there.
952 (move-marker pos (point)))))
955 ;; Figure out how much indentation is needed
956 (setq ncols (+ (current-left-margin)
957 (/ (- fc (current-left-margin) ;avail. space
958 (- endcol indent)) ;text width
961 ;; Have too much indentation - remove some
963 (progn (goto-char fp-end)
964 (if (< (current-column) ncols)
965 (move-to-column ncols t))
967 (progn (move-to-column indent) (point)))
968 ;; Have too little - add some
971 ;; If point was at beginning of text, keep it there.
973 (move-marker pos (point)))))
976 ;; Insert extra spaces between words to justify line
978 (narrow-to-region beg end)
980 (canonically-space-region beg end))
981 (goto-char (point-max))
982 ;; count word spaces in line
983 (while (search-backward " " nil t)
984 (setq nspaces (1+ nspaces))
985 (skip-chars-backward " "))
986 (setq ncols (- fc endcol))
987 ;; Ncols is number of additional space chars needed
988 (if (and (> ncols 0) (> nspaces 0) (not eop))
990 (setq curr-fracspace (+ ncols (/ (1+ nspaces) 2))
993 (skip-chars-forward " ")
995 (make-string (/ curr-fracspace nspaces) ?\ ))
996 (search-forward " " nil t)
997 (setq count (1- count)
999 (+ (% curr-fracspace nspaces) ncols)))))))
1000 (t (error "Unknown justification value"))))
1002 (move-marker pos nil)))
1005 (defun unjustify-current-line ()
1006 "Remove justification whitespace from current line.
1007 If the line is centered or right-justified, this function removes any
1008 indentation past the left margin. If the line is full-justified, it removes
1009 extra spaces between words. It does nothing in other justification modes."
1010 (let ((justify (current-justification)))
1011 (cond ((eq 'left justify) nil)
1012 ((eq nil justify) nil)
1013 ((eq 'full justify) ; full justify: remove extra spaces
1014 (beginning-of-line-text)
1015 (canonically-space-region
1016 (point) (save-excursion (end-of-line) (point))))
1017 ((memq justify '(center right))
1019 (move-to-left-margin nil t)
1020 ;; Position ourselves after any fill-prefix.
1021 (if (and fill-prefix
1022 (not (string-equal fill-prefix ""))
1025 (point) (min (point-max) (+ (length fill-prefix)
1027 (forward-char (length fill-prefix)))
1028 (delete-region (point) (progn (skip-chars-forward " \t")
1031 (defun unjustify-region (&optional begin end)
1032 "Remove justification whitespace from region.
1033 For centered or right-justified regions, this function removes any indentation
1034 past the left margin from each line. For full-justified lines, it removes
1035 extra spaces between words. It does nothing in other justification modes.
1036 Arguments BEGIN and END are optional; default is the whole buffer."
1039 (if end (narrow-to-region (point-min) end))
1040 (goto-char (or begin (point-min)))
1042 (unjustify-current-line)
1043 (forward-line 1)))))
1046 (defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
1047 "Fill paragraphs within the region, allowing varying indentation within each.
1048 This command divides the region into \"paragraphs\",
1049 only at paragraph-separator lines, then fills each paragraph
1050 using as the fill prefix the smallest indentation of any line
1053 When calling from a program, pass range to fill as first two arguments.
1055 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1056 JUSTIFY to justify paragraphs (prefix arg),
1057 When filling a mail message, pass a regexp for CITATION-REGEXP
1058 which will match the prefix of a line which is a citation marker
1059 plus whitespace, but no other kind of prefix.
1060 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1062 (barf-if-buffer-read-only)
1063 (list (region-beginning) (region-end)
1064 (if current-prefix-arg 'full))))
1065 (let ((fill-individual-varying-indent t))
1066 (fill-individual-paragraphs min max justifyp citation-regexp)))
1068 (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
1069 "Fill paragraphs of uniform indentation within the region.
1070 This command divides the region into \"paragraphs\",
1071 treating every change in indentation level or prefix as a paragraph boundary,
1072 then fills each paragraph using its indentation level as the fill prefix.
1074 There is one special case where a change in indentation does not start
1075 a new paragraph. This is for text of this form:
1077 foo> This line with extra indentation starts
1078 foo> a paragraph that continues on more lines.
1080 These lines are filled together.
1082 When calling from a program, pass the range to fill
1083 as the first two arguments.
1085 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1086 JUSTIFY to justify paragraphs (prefix arg),
1087 When filling a mail message, pass a regexp for CITATION-REGEXP
1088 which will match the prefix of a line which is a citation marker
1089 plus whitespace, but no other kind of prefix.
1090 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1092 (barf-if-buffer-read-only)
1093 (list (region-beginning) (region-end)
1094 (if current-prefix-arg 'full))))
1099 (narrow-to-region (point) max)
1101 (while (and (not (eobp))
1102 (or (looking-at "[ \t]*[^ \t\n]+:")
1103 (looking-at "[ \t]*$")))
1104 (if (looking-at "[ \t]*[^ \t\n]+:")
1105 (search-forward "\n\n" nil 'move)
1107 (narrow-to-region (point) max)
1108 ;; Loop over paragraphs.
1109 (while (let ((here (point)))
1110 ;; Skip over all paragraph-separating lines
1111 ;; so as to not include them in any paragraph.
1112 (while (and (not (eobp))
1113 (progn (move-to-left-margin)
1115 (looking-at paragraph-separate))))
1117 (skip-chars-forward " \t\n") (not (eobp)))
1118 (move-to-left-margin)
1119 (let ((start (point))
1120 fill-prefix fill-prefix-regexp)
1121 ;; Find end of paragraph, and compute the smallest fill-prefix
1122 ;; that fits all the lines in this paragraph.
1124 ;; Update the fill-prefix on the first line
1125 ;; and whenever the prefix good so far is too long.
1126 (if (not (and fill-prefix
1127 (looking-at fill-prefix-regexp)))
1129 (fill-individual-paragraphs-prefix citation-regexp)
1130 fill-prefix-regexp (regexp-quote fill-prefix)))
1133 ;; If forward-line went past a newline,
1134 ;; move further to the left margin.
1135 (move-to-left-margin))
1136 ;; Now stop the loop if end of paragraph.
1138 (if fill-individual-varying-indent
1139 ;; If this line is a separator line, with or
1140 ;; without prefix, end the paragraph.
1142 (not (looking-at paragraph-separate))
1144 (not (and (looking-at fill-prefix-regexp)
1145 (progn (forward-char (length fill-prefix))
1146 (looking-at paragraph-separate))))))
1147 ;; If this line has more or less indent
1148 ;; than the fill prefix wants, end the paragraph.
1149 (and (looking-at fill-prefix-regexp)
1150 ;; If fill prefix is shorter than a new
1151 ;; fill prefix computed here, end paragraph.
1152 (let ((this-line-fill-prefix
1153 (fill-individual-paragraphs-prefix
1155 (>= (length fill-prefix)
1156 (length this-line-fill-prefix)))
1158 (not (progn (forward-char (length fill-prefix))
1159 (or (looking-at "[ \t]")
1160 (looking-at paragraph-separate)
1161 (looking-at paragraph-start)))))
1162 (not (and (equal fill-prefix "")
1164 (looking-at citation-regexp))))))))
1165 ;; Fill this paragraph, but don't add a newline at the end.
1166 (let ((had-newline (bolp)))
1167 (fill-region-as-paragraph start (point) justify)
1168 (if (and (bolp) (not had-newline))
1169 (delete-char -1))))))))
1171 (defun fill-individual-paragraphs-prefix (citation-regexp)
1172 (or (let ((adaptive-fill-first-line-regexp "")
1173 just-one-line-prefix
1175 one-line-citation-part
1176 two-lines-citation-part
1177 adjusted-two-lines-citation-part)
1178 (setq just-one-line-prefix
1179 (fill-context-prefix
1181 (save-excursion (forward-line 1)
1183 (setq two-lines-prefix
1184 (fill-context-prefix
1186 (save-excursion (forward-line 2)
1188 (when just-one-line-prefix
1189 (setq one-line-citation-part
1191 (fill-individual-paragraphs-citation just-one-line-prefix
1193 just-one-line-prefix)))
1194 (when two-lines-prefix
1195 (setq two-lines-citation-part
1197 (fill-individual-paragraphs-citation two-lines-prefix
1199 just-one-line-prefix))
1200 (or two-lines-citation-part (setq two-lines-citation-part ""))
1201 (setq adjusted-two-lines-citation-part
1202 (substring two-lines-citation-part 0
1203 (string-match "[ \t]*\\'"
1204 two-lines-citation-part))))
1205 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1206 ;; is the same as that of TWO-LINES-PREFIX,
1207 ;; except perhaps with longer whitespace.
1208 (if (and just-one-line-prefix
1210 (string-match (concat "\\`"
1211 (regexp-quote adjusted-two-lines-citation-part)
1213 one-line-citation-part)
1214 (>= (string-width one-line-citation-part)
1215 (string-width two-lines-citation-part)))
1217 just-one-line-prefix))
1220 (save-excursion (skip-chars-forward " \t")
1223 (defun fill-individual-paragraphs-citation (string citation-regexp)
1224 (string-match citation-regexp
1226 (match-string 0 string))
1228 ;;; fill.el ends here