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.
42 This is relevant for filling. See also `sentence-end-without-period'
43 and `colon-double-space'.
45 If you change this, you should also change `sentence-end'. See Info
50 (defcustom colon-double-space nil
51 "*Non-nil means put two spaces after a colon when filling."
55 (defcustom sentence-end-without-period nil
56 "*Non-nil means a sentence will end without a period.
57 For example, a sentence in Thai text ends with double space but
62 (defvar fill-paragraph-function nil
63 "Mode-specific function to fill a paragraph, or nil if there is none.
64 If the function returns nil, then `fill-paragraph' does its normal work.")
66 (defvar enable-kinsoku t
67 "*Non-nil means enable \"kinsoku\" processing on filling paragraph.
68 Kinsoku processing is designed to prevent certain characters from being
69 placed at the beginning or end of a line by filling.
70 See the documentation of `kinsoku' for more information.")
72 (defun set-fill-prefix ()
73 "Set the fill prefix to the current line up to point.
74 Filling expects lines to start with the fill prefix and
75 reinserts the fill prefix in each resulting line."
77 (setq fill-prefix
(buffer-substring
78 (save-excursion (move-to-left-margin) (point))
80 (if (equal fill-prefix
"")
81 (setq fill-prefix nil
))
83 (message "fill-prefix: \"%s\"" fill-prefix
)
84 (message "fill-prefix cancelled")))
86 (defcustom adaptive-fill-mode t
87 "*Non-nil means determine a paragraph's fill prefix from its text."
91 (defcustom adaptive-fill-regexp
92 (purecopy "[ \t]*\\([-|#;>*]+ *\\|(?[0-9]+[.)] *\\)*")
93 "*Regexp to match text at start of line that constitutes indentation.
94 If Adaptive Fill mode is enabled, a prefix matching this pattern
95 on the first and second lines of a paragraph is used as the
96 standard indentation for the whole paragraph.
98 If the paragraph has just one line, the indentation is taken from that
99 line, but in that case `adaptive-fill-first-line-regexp' also plays
104 (defcustom adaptive-fill-first-line-regexp
"\\`[ \t]*\\'"
105 "*Regexp specifying whether to set fill prefix from a one-line paragraph.
106 When a paragraph has just one line, then after `adaptive-fill-regexp'
107 finds the prefix at the beginning of the line, if it doesn't
108 match this regexp, it is replaced with whitespace.
110 By default, this regexp matches sequences of just spaces and tabs.
112 However, we never use a prefix from a one-line paragraph
113 if it would act as a paragraph-starter on the second line."
117 (defcustom adaptive-fill-function nil
118 "*Function to call to choose a fill prefix for a paragraph.
119 This function is used when `adaptive-fill-regexp' does not match."
123 (defvar fill-indent-according-to-mode nil
124 "Whether or not filling should try to use the major mode's indentation.")
126 (defun current-fill-column ()
127 "Return the fill-column to use for this line.
128 The fill-column to use for a buffer is stored in the variable `fill-column',
129 but can be locally modified by the `right-margin' text property, which is
130 subtracted from `fill-column'.
132 The fill column to use for a line is the first column at which the column
133 number equals or exceeds the local fill-column - right-margin difference."
136 (let* ((here (progn (beginning-of-line) (point)))
138 (eol (progn (end-of-line) (point)))
139 margin fill-col change col
)
140 ;; Look separately at each region of line with a different
142 (while (and (setq margin
(get-text-property here
'right-margin
)
143 fill-col
(- fill-column
(or margin
0))
144 change
(text-property-not-all
145 here eol
'right-margin margin
))
146 (progn (goto-char (1- change
))
147 (setq col
(current-column))
151 (max here-col fill-col
)))))
153 (defun canonically-space-region (beg end
)
154 "Remove extra spaces between words in region.
155 Leave one space between words, two at end of sentences or after colons
156 \(depending on values of `sentence-end-double-space', `colon-double-space',
157 and `sentence-end-without-period').
158 Remove indentation from each line."
162 ;; Nuke tabs; they get screwed up in a fill.
163 ;; This is quick, but loses when a tab follows the end of a sentence.
164 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
166 (subst-char-in-region beg end ?
\t ?\
)
167 (while (and (< (point) end
)
168 (re-search-forward " *" end t
))
170 (+ (match-beginning 0)
171 ;; Determine number of spaces to leave:
173 (skip-chars-backward " ]})\"'")
174 (cond ((and sentence-end-double-space
175 (or (memq (preceding-char) '(?. ?? ?
!))
176 (and sentence-end-without-period
177 (= (char-syntax (preceding-char)) ?w
)))) 2)
178 ((and colon-double-space
179 (= (preceding-char) ?
:)) 2)
180 ((char-equal (preceding-char) ?
\n) 0)
183 ;; Make sure sentences ending at end of line get an extra space.
184 ;; loses on split abbrevs ("Mr.\nSmith")
186 (let ((eol-double-space-re (if colon-double-space
189 (while (and (< (point) end
)
190 (re-search-forward eol-double-space-re end t
))
191 ;; We insert before markers in case a caller such as
192 ;; do-auto-fill has done a save-excursion with point at the end
193 ;; of the line and wants it to stay at the end of the line.
194 (insert-before-markers-and-inherit ?
)))))
196 (defun fill-common-string-prefix (s1 s2
)
197 "Return the longest common prefix of strings S1 and S2, or nil if none."
198 (let ((cmp (compare-strings s1 nil nil s2 nil nil
)))
201 (setq cmp
(1- (abs cmp
)))
203 (substring s1
0 cmp
)))))
205 (defun fill-context-prefix (from to
&optional first-line-regexp
)
206 "Compute a fill prefix from the text between FROM and TO.
207 This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
208 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
209 we reject a prefix based on a one-line paragraph if that prefix would
210 act as a paragraph-separator."
211 (or first-line-regexp
212 (setq first-line-regexp adaptive-fill-first-line-regexp
))
215 (if (eolp) (forward-line 1))
216 ;; Move to the second line unless there is just one.
217 (let ((firstline (point))
219 ;; Non-nil if we are on the second line.
223 (move-to-left-margin)
225 (setq first-line-prefix
226 (cond ((looking-at paragraph-start
) nil
)
227 ((and adaptive-fill-regexp
(looking-at adaptive-fill-regexp
))
228 (buffer-substring-no-properties start
(match-end 0)))
229 (adaptive-fill-function (funcall adaptive-fill-function
))))
232 (goto-char firstline
)
234 (move-to-left-margin)
236 (setq second-line-prefix
237 (cond ((looking-at paragraph-start
) nil
)
238 ((and adaptive-fill-regexp
239 (looking-at adaptive-fill-regexp
))
240 (buffer-substring-no-properties start
(match-end 0)))
241 (adaptive-fill-function
242 (funcall adaptive-fill-function
)))))
244 ;; If we get a fill prefix from the second line,
245 ;; make sure it or something compatible is on the first line too.
246 (and second-line-prefix first-line-prefix
247 ;; If the first line has the second line prefix too, use it.
248 (if (or (string-match (concat "\\`"
249 (regexp-quote second-line-prefix
)
252 ;; If the second line prefix is whitespace, use it.
253 (string-match "\\`[ \t]+\\'" second-line-prefix
))
256 ;; If using the common prefix of first-line-prefix
257 ;; and second-line-prefix leads to problems, consider
258 ;; to restore the code below that's commented out,
259 ;; and document why a common prefix cannot be used.
261 ; ;; If the second line has the first line prefix,
262 ; ;; plus whitespace, use the part that the first line shares.
263 ; (if (string-match (concat "\\`"
264 ; (regexp-quote first-line-prefix)
266 ; second-line-prefix)
267 ; first-line-prefix)))
269 ;; Use the longest common substring of both prefixes,
271 (fill-common-string-prefix first-line-prefix
272 second-line-prefix
)))
273 ;; If we get a fill prefix from a one-line paragraph,
274 ;; maybe change it to whitespace,
275 ;; and check that it isn't a paragraph starter.
276 (if first-line-prefix
278 ;; If first-line-prefix comes from the first line,
279 ;; see if it seems reasonable to use for all lines.
280 ;; If not, replace it with whitespace.
281 (if (or (and first-line-regexp
282 (string-match first-line-regexp
284 (and comment-start-skip
285 (string-match comment-start-skip
288 (make-string (string-width first-line-prefix
) ?\
))))
289 ;; But either way, reject it if it indicates the start
290 ;; of a paragraph when text follows it.
291 (if (not (eq 0 (string-match paragraph-start
292 (concat result
"a"))))
295 (defvar fill-nobreak-predicate nil
296 "If non-nil, a predicate for recognizing places not to break a line.
297 The predicate is called with no arguments, with point at the place
298 to be tested. If it returns t, fill commands do not break the line there.")
300 ;; Put `fill-find-break-point-function' property to charsets which
301 ;; require special functions to find line breaking point.
302 (let ((alist '((katakana-jisx0201 . kinsoku
)
303 (chinese-gb2312 . kinsoku
)
304 (japanese-jisx0208 . kinsoku
)
305 (japanese-jisx0212 . kinsoku
)
306 (chinese-big5-1 . kinsoku
)
307 (chinese-big5-2 . kinsoku
))))
309 (put-charset-property (car (car alist
)) 'fill-find-break-point-function
311 (setq alist
(cdr alist
))))
313 (defun fill-find-break-point (limit)
314 "Move point to a proper line breaking position of the current line.
315 Don't move back past the buffer position LIMIT.
317 This function is called when we are going to break the current line
318 after or before a non-ascii character. If the charset of the
319 character has the property `fill-find-break-point-function', this
320 function calls the property value as a function with one arg LINEBEG.
321 If the charset has no such property, do nothing."
322 (let* ((ch (following-char))
323 (charset (char-charset ch
))
325 (if (eq charset
'ascii
)
326 (setq ch
(preceding-char)
327 charset
(char-charset ch
)))
328 (if (charsetp charset
)
330 (get-charset-property charset
'fill-find-break-point-function
)))
331 (if (and func
(fboundp func
))
332 (funcall func limit
))))
334 (defun fill-region-as-paragraph (from to
&optional justify
335 nosqueeze squeeze-after
)
336 "Fill the region as one paragraph.
337 It removes any paragraph breaks in the region and extra newlines at the end,
338 indents and fills lines between the margins given by the
339 `current-left-margin' and `current-fill-column' functions.
340 \(In most cases, the variable `fill-column' controls the width.)
341 It leaves point at the beginning of the line following the paragraph.
343 Normally performs justification according to the `current-justification'
344 function, but with a prefix arg, does full justification instead.
346 From a program, optional third arg JUSTIFY can specify any type of
347 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
348 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
349 means don't canonicalize spaces before that position.
351 Return the fill-prefix used for filling.
353 If `sentence-end-double-space' is non-nil, then period followed by one
354 space does not end a sentence, so don't break a line there."
356 (barf-if-buffer-read-only)
357 (list (region-beginning) (region-end)
358 (if current-prefix-arg
'full
))))
359 (unless (memq justify
'(t nil none full center left right
))
360 (setq justify
'full
))
361 ;; Arrange for undoing the fill to restore point.
362 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
363 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
365 ;; Make sure "to" is the endpoint.
366 (goto-char (min from to
))
367 (setq to
(max from to
))
368 ;; Ignore blank lines at beginning of region.
369 (skip-chars-forward " \t\n")
371 (let ((from-plus-indent (point))
377 ;; Delete all but one soft newline at end of region.
378 ;; And leave TO before that one.
380 (while (and (> (point) from
) (eq ?
\n (char-after (1- (point)))))
382 (not (and use-hard-newlines
383 (get-text-property (1- (point)) 'hard
))))
384 (delete-backward-char 1)
388 ;; ;; If there was no newline, and there is text in the paragraph, then
389 ;; ;; create a newline.
390 ;; (if (and (not oneleft) (> to from-plus-indent))
392 (goto-char from-plus-indent
))
394 (if (not (> to
(point)))
395 nil
; There is no paragraph, only whitespace: exit now.
397 (or justify
(setq justify
(current-justification)))
399 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
400 (let ((fill-prefix fill-prefix
))
401 ;; Figure out how this paragraph is indented, if desired.
402 (if (and adaptive-fill-mode
403 (or (null fill-prefix
) (string= fill-prefix
"")))
404 (setq fill-prefix
(fill-context-prefix from to
)))
409 (narrow-to-region (point) to
)
411 (if (not justify
) ; filling disabled: just check indentation
415 (if (and (not (eolp))
416 (< (current-indentation) (current-left-margin)))
417 (indent-to-left-margin))
420 (if use-hard-newlines
421 (remove-text-properties from
(point-max) '(hard nil
)))
422 ;; Make sure first line is indented (at least) to left margin...
423 (if (or (memq justify
'(right center
))
424 (< (current-indentation) (current-left-margin)))
425 (indent-to-left-margin))
426 ;; Delete the fill prefix from every line except the first.
427 ;; The first line may not even have a fill prefix.
429 (let ((fpre (and fill-prefix
(not (equal fill-prefix
""))
431 (regexp-quote fill-prefix
)
435 (if (>= (+ (current-left-margin) (length fill-prefix
))
436 (current-fill-column))
437 (error "fill-prefix too long for specified width"))
441 (if (looking-at fpre
)
442 (delete-region (point) (match-end 0)))
445 (if (looking-at fpre
)
446 (goto-char (match-end 0)))
447 (setq from
(point)))))
448 ;; Remove indentation from lines other than the first.
449 (beginning-of-line 2)
450 (indent-region (point) (point-max) 0)
453 ;; FROM, and point, are now before the text to fill,
454 ;; but after any fill prefix on the first line.
456 ;; Make sure sentences ending at end of line get an extra space.
457 ;; loses on split abbrevs ("Mr.\nSmith")
458 (let ((eol-double-space-re (if colon-double-space
461 (while (re-search-forward eol-double-space-re nil t
)
462 (or (eobp) (insert-and-inherit ?\
))))
465 (if enable-multibyte-characters
466 ;; Delete unnecessay newlines surrounded by words. The
467 ;; character category `|' means that we can break a line
468 ;; at the character. And, charset property
469 ;; `nospace-between-words' tells how to concatenate
470 ;; words. If the value is non-nil, never put spaces
471 ;; between words, thus delete a newline between them.
472 ;; If the value is nil, delete a newline only when a
473 ;; character preceding a newline has text property
474 ;; `nospace-between-words'.
475 (while (search-forward "\n" nil t
)
476 (let ((prev (char-before (match-beginning 0)))
477 (next (following-char)))
478 (if (and (or (aref (char-category-set next
) ?|
)
479 (aref (char-category-set prev
) ?|
))
480 (or (get-charset-property (char-charset prev
)
481 'nospace-between-words
)
482 (get-text-property (1- (match-beginning 0))
483 'nospace-between-words
)))
487 (skip-chars-forward " \t")
488 ;; Then change all newlines to spaces.
489 (subst-char-in-region from
(point-max) ?
\n ?\
)
490 (if (and nosqueeze
(not (eq justify
'full
)))
492 (canonically-space-region (or squeeze-after
(point)) (point-max))
493 (goto-char (point-max))
494 (delete-horizontal-space)
495 (insert-and-inherit " "))
496 (goto-char (point-min))
498 ;; This is the actual filling loop.
499 (let ((prefixcol 0) linebeg
)
501 (setq linebeg
(point))
502 (move-to-column (1+ (current-fill-column)))
504 (or nosqueeze
(delete-horizontal-space))
505 ;; Move back to the point where we can break the line
506 ;; at. We break the line between word or after/before
507 ;; the character which has character category `|'. We
508 ;; search space, \c| followed by a character, or \c|
509 ;; following a character. If not found, place
510 ;; the point at linebeg.
511 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg
0)
512 ;; In case of space, we place the point at next to
513 ;; the point where the break occurs acutually,
514 ;; because we don't want to change the following
515 ;; logic of original Emacs. In case of \c|, the
516 ;; point is at the place where the break occurs.
518 ;; Don't break after a period followed by just one space.
519 ;; Move back to the previous place to break.
520 ;; The reason is that if a period ends up at the end of a line,
521 ;; further fills will assume it ends a sentence.
522 ;; If we now know it does not end a sentence,
523 ;; avoid putting it at the end of the line.
524 (while (or (and sentence-end-double-space
525 (> (point) (+ linebeg
2))
526 (eq (preceding-char) ?\
)
527 (not (eq (following-char) ?\
))
528 (eq (char-after (- (point) 2)) ?\.
)
529 (progn (forward-char -
2) t
))
530 (and fill-nobreak-predicate
531 (funcall fill-nobreak-predicate
)
532 (goto-char (match-beginning 0))))
533 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg
0)
535 ;; If the left margin and fill prefix by themselves
536 ;; pass the fill-column. or if they are zero
537 ;; but we have no room for even one word,
538 ;; keep at least one word or a character which has
539 ;; category `|'anyway .
540 ;; This handles ALL BUT the first line of the paragraph.
541 (if (if (zerop prefixcol
)
543 (skip-chars-backward " \t" linebeg
)
545 (>= prefixcol
(current-column)))
546 ;; Ok, skip at least one word or one \c| character.
547 ;; Meanwhile, don't stop at a period followed by one space.
549 (move-to-column prefixcol
)
550 (while (and (not (eobp))
553 sentence-end-double-space
556 (and (looking-at "\\. ")
557 (not (looking-at "\\. ")))))
558 (and fill-nobreak-predicate
559 (funcall fill-nobreak-predicate
))))
560 ;; Find a breakable point while ignoring the
562 (skip-chars-forward " \t")
563 (if (looking-at "\\c|")
565 (let ((pos (save-excursion
566 (skip-chars-forward "^ \n\t")
568 (if (re-search-forward "\\c|" pos t
)
572 ;; Normally, move back over the single space between
574 (if (= (preceding-char) ?\
) (forward-char -
1))
576 (if enable-multibyte-characters
577 ;; If we are going to break the line after or
578 ;; before a non-ascii character, we may have to
579 ;; run a special function for the charset of the
580 ;; character to find the correct break point.
581 (if (not (and (eq (charset-after (1- (point))) 'ascii
)
582 (eq (charset-after (point)) 'ascii
)))
583 ;; Make sure we take SOMETHING after the
584 ;; fill prefix if any.
585 (fill-find-break-point
588 (move-to-column prefixcol
)
591 ;; If the left margin and fill prefix by themselves
592 ;; pass the fill-column, keep at least one word.
593 ;; This handles the first line of the paragraph.
594 (if (and (zerop prefixcol
)
595 (let ((fill-point (point)) nchars
)
597 (move-to-left-margin)
598 (setq nchars
(- fill-point
(point)))
601 (< nchars
(length fill-prefix
))
602 (string= (buffer-substring (point)
604 (substring fill-prefix
606 ;; Ok, skip at least one word. But
607 ;; don't stop at a period followed by just one space.
609 (while (and (not (eobp))
612 sentence-end-double-space
615 (and (looking-at "\\. ")
616 (not (looking-at "\\. ")))))
617 (and fill-nobreak-predicate
618 (funcall fill-nobreak-predicate
))))
619 ;; Find a breakable point while ignoring the
621 (skip-chars-forward " \t")
622 (if (looking-at "\\c|")
624 (let ((pos (save-excursion
625 (skip-chars-forward "^ \n\t")
627 (if (re-search-forward "\\c|" pos t
)
631 ;; Check again to see if we got to the end of the paragraph.
632 (if (save-excursion (skip-chars-forward " \t") (eobp))
633 (or nosqueeze
(delete-horizontal-space))
634 ;; Replace whitespace here with one newline, then
635 ;; indent to left margin.
636 (skip-chars-backward " \t")
637 (if (and (= (following-char) ?\
)
638 (or (aref (char-category-set (preceding-char)) ?|
)
639 (looking-at "[ \t]+\\c|")))
640 ;; We need one space at end of line so that
641 ;; further filling won't delete it. NOTE: We
642 ;; intentionally leave this one space to
643 ;; distingush the case that user wants to put
644 ;; space between \c| characters.
647 ;; Give newline the properties of the space(s) it replaces
648 (set-text-properties (1- (point)) (point)
649 (text-properties-at (point)))
651 (not fill-indent-according-to-mode
)
652 (memq indent-line-function
653 ;; Brain dead "indenting" functions.
654 '(indent-relative-maybe indent-relative
)))
655 (indent-to-left-margin)
656 (indent-according-to-mode))
657 ;; Insert the fill prefix after indentation.
658 ;; Set prefixcol so whitespace in the prefix won't get lost.
659 (and fill-prefix
(not (equal fill-prefix
""))
661 (insert-and-inherit fill-prefix
)
662 (setq prefixcol
(current-column))))))
663 ;; Justify the line just ended, if desired.
665 (if (save-excursion (skip-chars-forward " \t") (eobp))
667 (delete-horizontal-space)
668 (justify-current-line justify t t
))
670 (justify-current-line justify nil t
)
671 (forward-line 1))))))
672 ;; Leave point after final newline.
673 (goto-char (point-max)))
676 ;; Return the fill-prefix we used
679 (defsubst skip-line-prefix
(prefix)
680 "If point is inside the string PREFIX at the beginning of line, move past it."
682 (< (- (point) (line-beginning-position)) (length prefix
))
685 (looking-at (regexp-quote prefix
))))
686 (goto-char (match-end 0))))
688 (defun fill-paragraph (arg)
689 "Fill paragraph at or after point. Prefix arg means justify as well.
690 If `sentence-end-double-space' is non-nil, then period followed by one
691 space does not end a sentence, so don't break a line there.
692 the variable `fill-column' controls the width for filling.
694 If `fill-paragraph-function' is non-nil, we call it (passing our
695 argument to it), and if it returns non-nil, we simply return its value.
697 If `fill-paragraph-function' is nil, return the fill-prefix used for filling."
699 (barf-if-buffer-read-only)
700 (list (if current-prefix-arg
'full
))))
701 (or (and fill-paragraph-function
702 (let ((function fill-paragraph-function
)
703 fill-paragraph-function
)
704 (funcall function arg
)))
705 (let ((before (point))
706 ;; Fill prefix used for filling the paragraph
708 ;; If fill-paragraph is called recursively,
709 ;; don't give fill-paragraph-function a second chance.
710 fill-paragraph-function
)
713 (or (bolp) (newline 1))
715 (beg (progn (backward-paragraph) (point))))
718 (if use-hard-newlines
719 ;; Can't use fill-region-as-paragraph, since this
720 ;; paragraph may still contain hard newlines. See
722 (fill-region beg end arg
)
723 (fill-region-as-paragraph beg end arg
)))))
724 ;; See if point ended up inside the fill-prefix, and if so, move
726 (skip-line-prefix fill-pfx
)
729 (defun fill-region (from to
&optional justify nosqueeze to-eop
)
730 "Fill each of the paragraphs in the region.
731 A prefix arg means justify as well.
732 Ordinarily the variable `fill-column' controls the width.
734 Noninteractively, the third argument JUSTIFY specifies which
735 kind of justification to do: `full', `left', `right', `center',
736 or `none' (equivalent to nil). t means handle each paragraph
737 as specified by its text properties.
739 The fourth arg NOSQUEEZE non-nil means to leave
740 whitespace other than line breaks untouched, and fifth arg TO-EOP
741 non-nil means to keep filling to the end of the paragraph (or next
742 hard newline, if `use-hard-newlines' is on).
744 Return the fill-prefix used for filling the last paragraph.
746 If `sentence-end-double-space' is non-nil, then period followed by one
747 space does not end a sentence, so don't break a line there."
749 (barf-if-buffer-read-only)
750 (list (region-beginning) (region-end)
751 (if current-prefix-arg
'full
))))
752 (unless (memq justify
'(t nil none full center left right
))
753 (setq justify
'full
))
754 (let (end beg fill-pfx
)
756 (goto-char (max from to
))
758 (progn (skip-chars-backward "\n")
759 (forward-paragraph)))
761 (goto-char (setq beg
(min from to
)))
763 (narrow-to-region (point) end
)
765 (let ((initial (point))
767 ;; If using hard newlines, break at every one for filling
768 ;; purposes rather than using paragraph breaks.
769 (if use-hard-newlines
771 (while (and (setq end
(text-property-any (point) (point-max)
773 (not (= ?
\n (char-after end
)))
774 (not (= end
(point-max))))
775 (goto-char (1+ end
)))
776 (setq end
(if end
(min (point-max) (1+ end
)) (point-max)))
778 (forward-paragraph 1)
780 (forward-paragraph -
1))
783 (if (>= (point) initial
)
785 (fill-region-as-paragraph (point) end justify nosqueeze
))
790 (defcustom default-justification
'left
791 "*Method of justifying text not otherwise specified.
792 Possible values are `left', `right', `full', `center', or `none'.
793 The requested kind of justification is done whenever lines are filled.
794 The `justification' text-property can locally override this variable.
795 This variable automatically becomes buffer-local when set in any fashion."
796 :type
'(choice (const left
)
802 (make-variable-buffer-local 'default-justification
)
804 (defun current-justification ()
805 "How should we justify this line?
806 This returns the value of the text-property `justification',
807 or the variable `default-justification' if there is no text-property.
808 However, it returns nil rather than `none' to mean \"don't justify\"."
809 (let ((j (or (get-text-property
810 ;; Make sure we're looking at paragraph body.
811 (save-excursion (skip-chars-forward " \t")
812 (if (and (eobp) (not (bobp)))
813 (1- (point)) (point)))
815 default-justification
)))
820 (defun set-justification (begin end value
&optional whole-par
)
821 "Set the region's justification style.
822 The kind of justification to use is prompted for.
823 If the mark is not active, this command operates on the current paragraph.
824 If the mark is active, the region is used. However, if the beginning and end
825 of the region are not at paragraph breaks, they are moved to the beginning and
826 end of the paragraphs they are in.
827 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
830 When calling from a program, operates just on region between BEGIN and END,
831 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
832 extended to include entire paragraphs as in the interactive command."
833 (interactive (list (if mark-active
(region-beginning) (point))
834 (if mark-active
(region-end) (point))
835 (let ((s (completing-read
836 "Set justification to: "
837 '(("left") ("right") ("full")
840 (if (equal s
"") (error ""))
846 (let ((paragraph-start (if use-hard-newlines
"." paragraph-start
))
847 (paragraph-ignore-fill-prefix (if use-hard-newlines t
848 paragraph-ignore-fill-prefix
)))
850 (while (and (bolp) (not (eobp))) (forward-char 1))
854 (skip-chars-backward " \t\n" begin
)
858 (narrow-to-region (point-min) end
)
859 (unjustify-region begin
(point-max))
860 (put-text-property begin
(point-max) 'justification value
)
861 (fill-region begin
(point-max) nil t
))))
863 (defun set-justification-none (b e
)
864 "Disable automatic filling for paragraphs in the region.
865 If the mark is not active, this applies to the current paragraph."
866 (interactive (list (if mark-active
(region-beginning) (point))
867 (if mark-active
(region-end) (point))))
868 (set-justification b e
'none t
))
870 (defun set-justification-left (b e
)
871 "Make paragraphs in the region left-justified.
872 This is usually the default, but see the variable `default-justification'.
873 If the mark is not active, this applies to the current paragraph."
874 (interactive (list (if mark-active
(region-beginning) (point))
875 (if mark-active
(region-end) (point))))
876 (set-justification b e
'left t
))
878 (defun set-justification-right (b e
)
879 "Make paragraphs in the region right-justified:
880 Flush at the right margin and ragged on the left.
881 If the mark is not active, this applies to the current paragraph."
882 (interactive (list (if mark-active
(region-beginning) (point))
883 (if mark-active
(region-end) (point))))
884 (set-justification b e
'right t
))
886 (defun set-justification-full (b e
)
887 "Make paragraphs in the region fully justified:
888 This makes lines flush on both margins by inserting spaces between words.
889 If the mark is not active, this applies to the current paragraph."
890 (interactive (list (if mark-active
(region-beginning) (point))
891 (if mark-active
(region-end) (point))))
892 (set-justification b e
'full t
))
894 (defun set-justification-center (b e
)
895 "Make paragraphs in the region centered.
896 If the mark is not active, this applies to the current paragraph."
897 (interactive (list (if mark-active
(region-beginning) (point))
898 (if mark-active
(region-end) (point))))
899 (set-justification b e
'center t
))
901 ;; A line has up to six parts:
904 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
906 ;; "Indent-1" is the left-margin indentation; normally it ends at column
907 ;; given by the `current-left-margin' function.
908 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
909 ;; "Indent-2" is added to justify a line if the `current-justification' is
910 ;; `center' or `right'. In `left' and `full' justification regions, any
911 ;; whitespace there is part of the line's text, and should not be changed.
912 ;; Trailing whitespace is not counted as part of the line length when
913 ;; center- or right-justifying.
915 ;; All parts of the line are optional, although the final newline can
916 ;; only be missing on the last line of the buffer.
918 (defun justify-current-line (&optional how eop nosqueeze
)
919 "Do some kind of justification on this line.
920 Normally does full justification: adds spaces to the line to make it end at
921 the column given by `current-fill-column'.
922 Optional first argument HOW specifies alternate type of justification:
923 it can be `left', `right', `full', `center', or `none'.
924 If HOW is t, will justify however the `current-justification' function says to.
925 If HOW is nil or missing, full justification is done by default.
926 Second arg EOP non-nil means that this is the last line of the paragraph, so
927 it will not be stretched by full justification.
928 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
929 otherwise it is made canonical."
931 (if (eq t how
) (setq how
(or (current-justification) 'none
))
932 (if (null how
) (setq how
'full
)
933 (or (memq how
'(none left right center
))
935 (or (memq how
'(none left
)) ; No action required for these.
936 (let ((fc (current-fill-column))
938 fp-end
; point at end of fill prefix
939 beg
; point at beginning of line's text
940 end
; point at end of line's text
941 indent
; column of `beg'
942 endcol
; column of `end'
943 ncols
; new indent point or offset
944 (nspaces 0) ; number of spaces between words
945 ; in line (not space characters)
946 fracspace
; fractional amount of space to be
947 ; added between each words
948 (curr-fracspace 0) ; current fractional space amount
951 ;; Check if this is the last line of the paragraph.
952 (if (and use-hard-newlines
(null eop
)
953 (get-text-property (point) 'hard
))
955 (skip-chars-backward " \t")
956 ;; Quick exit if it appears to be properly justified already
957 ;; or there is no text.
959 (and (memq how
'(full right
))
960 (= (current-column) fc
)))
964 (skip-chars-forward " \t")
965 ;; Skip over fill-prefix.
967 (not (string-equal fill-prefix
""))
970 (point) (min (point-max) (+ (length fill-prefix
)
972 (forward-char (length fill-prefix
))
973 (if (and adaptive-fill-mode
974 (looking-at adaptive-fill-regexp
))
975 (goto-char (match-end 0))))
976 (setq fp-end
(point))
977 (skip-chars-forward " \t")
978 ;; This is beginning of the line's text.
979 (setq indent
(current-column))
982 (setq endcol
(current-column))
984 ;; HOW can't be null or left--we would have exited already
985 (cond ((eq 'right how
)
986 (setq ncols
(- fc endcol
))
988 ;; Need to remove some indentation
990 (progn (goto-char fp-end
)
991 (if (< (current-column) (+ indent ncols
))
992 (move-to-column (+ indent ncols
) t
))
994 (progn (move-to-column indent
) (point)))
997 (indent-to (+ indent ncols
))
998 ;; If point was at beginning of text, keep it there.
1000 (move-marker pos
(point)))))
1003 ;; Figure out how much indentation is needed
1004 (setq ncols
(+ (current-left-margin)
1005 (/ (- fc
(current-left-margin) ;avail. space
1006 (- endcol indent
)) ;text width
1008 (if (< ncols indent
)
1009 ;; Have too much indentation - remove some
1011 (progn (goto-char fp-end
)
1012 (if (< (current-column) ncols
)
1013 (move-to-column ncols t
))
1015 (progn (move-to-column indent
) (point)))
1016 ;; Have too little - add some
1019 ;; If point was at beginning of text, keep it there.
1021 (move-marker pos
(point)))))
1024 ;; Insert extra spaces between words to justify line
1026 (narrow-to-region beg end
)
1028 (canonically-space-region beg end
))
1029 (goto-char (point-max))
1030 ;; count word spaces in line
1031 (while (search-backward " " nil t
)
1032 (setq nspaces
(1+ nspaces
))
1033 (skip-chars-backward " "))
1034 (setq ncols
(- fc endcol
))
1035 ;; Ncols is number of additional space chars needed
1036 (if (and (> ncols
0) (> nspaces
0) (not eop
))
1038 (setq curr-fracspace
(+ ncols
(/ (1+ nspaces
) 2))
1041 (skip-chars-forward " ")
1043 (make-string (/ curr-fracspace nspaces
) ?\
))
1044 (search-forward " " nil t
)
1045 (setq count
(1- count
)
1047 (+ (% curr-fracspace nspaces
) ncols
)))))))
1048 (t (error "Unknown justification value"))))
1050 (move-marker pos nil
)))
1053 (defun unjustify-current-line ()
1054 "Remove justification whitespace from current line.
1055 If the line is centered or right-justified, this function removes any
1056 indentation past the left margin. If the line is full-justified, it removes
1057 extra spaces between words. It does nothing in other justification modes."
1058 (let ((justify (current-justification)))
1059 (cond ((eq 'left justify
) nil
)
1060 ((eq nil justify
) nil
)
1061 ((eq 'full justify
) ; full justify: remove extra spaces
1062 (beginning-of-line-text)
1063 (canonically-space-region (point) (line-end-position)))
1064 ((memq justify
'(center right
))
1066 (move-to-left-margin nil t
)
1067 ;; Position ourselves after any fill-prefix.
1068 (if (and fill-prefix
1069 (not (string-equal fill-prefix
""))
1072 (point) (min (point-max) (+ (length fill-prefix
)
1074 (forward-char (length fill-prefix
)))
1075 (delete-region (point) (progn (skip-chars-forward " \t")
1078 (defun unjustify-region (&optional begin end
)
1079 "Remove justification whitespace from region.
1080 For centered or right-justified regions, this function removes any indentation
1081 past the left margin from each line. For full-justified lines, it removes
1082 extra spaces between words. It does nothing in other justification modes.
1083 Arguments BEGIN and END are optional; default is the whole buffer."
1086 (if end
(narrow-to-region (point-min) end
))
1087 (goto-char (or begin
(point-min)))
1089 (unjustify-current-line)
1090 (forward-line 1)))))
1093 (defun fill-nonuniform-paragraphs (min max
&optional justifyp citation-regexp
)
1094 "Fill paragraphs within the region, allowing varying indentation within each.
1095 This command divides the region into \"paragraphs\",
1096 only at paragraph-separator lines, then fills each paragraph
1097 using as the fill prefix the smallest indentation of any line
1100 When calling from a program, pass range to fill as first two arguments.
1102 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1103 JUSTIFY to justify paragraphs (prefix arg),
1104 When filling a mail message, pass a regexp for CITATION-REGEXP
1105 which will match the prefix of a line which is a citation marker
1106 plus whitespace, but no other kind of prefix.
1107 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1109 (barf-if-buffer-read-only)
1110 (list (region-beginning) (region-end)
1111 (if current-prefix-arg
'full
))))
1112 (let ((fill-individual-varying-indent t
))
1113 (fill-individual-paragraphs min max justifyp citation-regexp
)))
1115 (defun fill-individual-paragraphs (min max
&optional justify citation-regexp
)
1116 "Fill paragraphs of uniform indentation within the region.
1117 This command divides the region into \"paragraphs\",
1118 treating every change in indentation level or prefix as a paragraph boundary,
1119 then fills each paragraph using its indentation level as the fill prefix.
1121 There is one special case where a change in indentation does not start
1122 a new paragraph. This is for text of this form:
1124 foo> This line with extra indentation starts
1125 foo> a paragraph that continues on more lines.
1127 These lines are filled together.
1129 When calling from a program, pass the range to fill
1130 as the first two arguments.
1132 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1133 JUSTIFY to justify paragraphs (prefix arg),
1134 When filling a mail message, pass a regexp for CITATION-REGEXP
1135 which will match the prefix of a line which is a citation marker
1136 plus whitespace, but no other kind of prefix.
1137 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1139 (barf-if-buffer-read-only)
1140 (list (region-beginning) (region-end)
1141 (if current-prefix-arg
'full
))))
1146 (narrow-to-region (point) max
)
1148 (while (and (not (eobp))
1149 (or (looking-at "[ \t]*[^ \t\n]+:")
1150 (looking-at "[ \t]*$")))
1151 (if (looking-at "[ \t]*[^ \t\n]+:")
1152 (search-forward "\n\n" nil
'move
)
1154 (narrow-to-region (point) max
)
1155 ;; Loop over paragraphs.
1156 (while (let ((here (point)))
1157 ;; Skip over all paragraph-separating lines
1158 ;; so as to not include them in any paragraph.
1159 (while (and (not (eobp))
1160 (progn (move-to-left-margin)
1162 (looking-at paragraph-separate
))))
1164 (skip-chars-forward " \t\n") (not (eobp)))
1165 (move-to-left-margin)
1166 (let ((start (point))
1167 fill-prefix fill-prefix-regexp
)
1168 ;; Find end of paragraph, and compute the smallest fill-prefix
1169 ;; that fits all the lines in this paragraph.
1171 ;; Update the fill-prefix on the first line
1172 ;; and whenever the prefix good so far is too long.
1173 (if (not (and fill-prefix
1174 (looking-at fill-prefix-regexp
)))
1176 (fill-individual-paragraphs-prefix
1178 fill-prefix-regexp
(regexp-quote fill-prefix
)))
1181 ;; If forward-line went past a newline,
1182 ;; move further to the left margin.
1183 (move-to-left-margin))
1184 ;; Now stop the loop if end of paragraph.
1186 (if fill-individual-varying-indent
1187 ;; If this line is a separator line, with or
1188 ;; without prefix, end the paragraph.
1190 (not (looking-at paragraph-separate
))
1192 (not (and (looking-at fill-prefix-regexp
)
1193 (progn (forward-char
1194 (length fill-prefix
))
1196 paragraph-separate
))))))
1197 ;; If this line has more or less indent
1198 ;; than the fill prefix wants, end the paragraph.
1199 (and (looking-at fill-prefix-regexp
)
1200 ;; If fill prefix is shorter than a new
1201 ;; fill prefix computed here, end paragraph.
1202 (let ((this-line-fill-prefix
1203 (fill-individual-paragraphs-prefix
1205 (>= (length fill-prefix
)
1206 (length this-line-fill-prefix
)))
1208 (not (progn (forward-char
1209 (length fill-prefix
))
1210 (or (looking-at "[ \t]")
1211 (looking-at paragraph-separate
)
1212 (looking-at paragraph-start
)))))
1213 (not (and (equal fill-prefix
"")
1215 (looking-at citation-regexp
))))))))
1216 ;; Fill this paragraph, but don't add a newline at the end.
1217 (let ((had-newline (bolp)))
1218 (fill-region-as-paragraph start
(point) justify
)
1219 (if (and (bolp) (not had-newline
))
1220 (delete-char -
1))))))))
1222 (defun fill-individual-paragraphs-prefix (citation-regexp)
1223 (or (let ((adaptive-fill-first-line-regexp "")
1224 just-one-line-prefix
1226 one-line-citation-part
1227 two-lines-citation-part
1228 adjusted-two-lines-citation-part
)
1229 (setq just-one-line-prefix
1230 (fill-context-prefix
1232 (line-beginning-position 2)))
1233 (setq two-lines-prefix
1234 (fill-context-prefix
1236 (line-beginning-position 2)))
1237 (when just-one-line-prefix
1238 (setq one-line-citation-part
1240 (fill-individual-paragraphs-citation just-one-line-prefix
1242 just-one-line-prefix
)))
1243 (when two-lines-prefix
1244 (setq two-lines-citation-part
1246 (fill-individual-paragraphs-citation two-lines-prefix
1248 just-one-line-prefix
))
1249 (or two-lines-citation-part
(setq two-lines-citation-part
""))
1250 (setq adjusted-two-lines-citation-part
1251 (substring two-lines-citation-part
0
1252 (string-match "[ \t]*\\'"
1253 two-lines-citation-part
))))
1254 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1255 ;; is the same as that of TWO-LINES-PREFIX,
1256 ;; except perhaps with longer whitespace.
1257 (if (and just-one-line-prefix
1259 (string-match (concat "\\`"
1261 adjusted-two-lines-citation-part
)
1263 one-line-citation-part
)
1264 (>= (string-width one-line-citation-part
)
1265 (string-width two-lines-citation-part
)))
1267 just-one-line-prefix
))
1270 (save-excursion (skip-chars-forward " \t")
1273 (defun fill-individual-paragraphs-citation (string citation-regexp
)
1274 (string-match citation-regexp
1276 (match-string 0 string
))
1278 ;;; fill.el ends here