]> code.delx.au - gnu-emacs/blob - lisp/textmodes/fill.el
(sentence-end-double-space): Doc fix.
[gnu-emacs] / lisp / textmodes / fill.el
1 ;;; fill.el --- fill commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 96, 1997 Free Software Foundation, Inc.
4
5 ;; Keywords: wp
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; All the commands for filling text. These are documented in the Emacs
27 ;; manual.
28
29 ;;; Code:
30
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."
37 :type 'boolean
38 :group 'fill)
39
40 (defcustom sentence-end-double-space t
41 "*Non-nil means a single space does not end a sentence.
42
43 If you change this, you should also change `sentence-end'.
44 See Info node `Sentences'."
45 :type 'boolean
46 :group 'fill)
47
48 (defcustom colon-double-space nil
49 "*Non-nil means put two spaces after a colon when filling."
50 :type 'boolean
51 :group 'fill)
52
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."
56 :type 'boolean
57 :group 'fill)
58
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.")
62
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.")
68
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."
73 (interactive)
74 (setq fill-prefix (buffer-substring
75 (save-excursion (move-to-left-margin) (point))
76 (point)))
77 (if (equal fill-prefix "")
78 (setq fill-prefix nil))
79 (if fill-prefix
80 (message "fill-prefix: \"%s\"" fill-prefix)
81 (message "fill-prefix cancelled")))
82
83 (defcustom adaptive-fill-mode t
84 "*Non-nil means determine a paragraph's fill prefix from its text."
85 :type 'boolean
86 :group 'fill)
87
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.
93
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
96 a role."
97 :type 'regexp
98 :group 'fill)
99
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.
105
106 By default, this regexp matches sequences of just spaces and tabs.
107
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."
110 :type 'regexp
111 :group 'fill)
112
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."
116 :type 'function
117 :group 'fill)
118
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'.
124
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."
127 (save-excursion
128 (if fill-column
129 (let* ((here (progn (beginning-of-line) (point)))
130 (here-col 0)
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))
140 (< col fill-col)))
141 (setq here change
142 here-col col))
143 (max here-col fill-col)))))
144
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."
151 (interactive "r")
152 (save-excursion
153 (goto-char beg)
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".
157 ;; Blame the typist.
158 (subst-char-in-region beg end ?\t ?\ )
159 (while (and (< (point) end)
160 (re-search-forward " *" end t))
161 (delete-region
162 (+ (match-beginning 0)
163 ;; Determine number of spaces to leave:
164 (save-excursion
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)
173 (t 1))))
174 (match-end 0)))
175 ;; Make sure sentences ending at end of line get an extra space.
176 ;; loses on split abbrevs ("Mr.\nSmith")
177 (goto-char beg)
178 (while (and (< (point) end)
179 (re-search-forward "[.?!][])}\"']*$" end t))
180 ;; We insert before markers in case a caller such as
181 ;; do-auto-fill has done a save-excursion with point at the end
182 ;; of the line and wants it to stay at the end of the line.
183 (insert-before-markers-and-inherit ? ))))
184
185 (defun fill-context-prefix (from to &optional first-line-regexp)
186 "Compute a fill prefix from the text between FROM and TO.
187 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'
188 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
189 we reject a prefix based on a one-line paragraph if that prefix would
190 act as a paragraph-separator."
191 (or first-line-regexp
192 (setq first-line-regexp adaptive-fill-first-line-regexp))
193 (save-excursion
194 (goto-char from)
195 (if (eolp) (forward-line 1))
196 ;; Move to the second line unless there is just one.
197 (let ((firstline (point))
198 first-line-prefix
199 ;; Non-nil if we are on the second line.
200 at-second
201 second-line-prefix
202 start)
203 (move-to-left-margin)
204 (setq start (point))
205 (setq first-line-prefix
206 (cond ((looking-at paragraph-start) nil)
207 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
208 (buffer-substring-no-properties start (match-end 0)))
209 (adaptive-fill-function (funcall adaptive-fill-function))))
210 (forward-line 1)
211 (if (>= (point) to)
212 (goto-char firstline)
213 (setq at-second t)
214 (move-to-left-margin)
215 (setq start (point))
216 (setq second-line-prefix
217 (cond ((looking-at paragraph-start) nil)
218 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
219 (buffer-substring-no-properties start (match-end 0)))
220 (adaptive-fill-function (funcall adaptive-fill-function)))))
221 (if at-second
222 ;; If we get a fill prefix from the second line,
223 ;; make sure it or something compatible is on the first line too.
224 (and second-line-prefix first-line-prefix
225 ;; If the first line has the second line prefix too, use it.
226 (if (or (string-match (concat "\\`"
227 (regexp-quote second-line-prefix)
228 "\\(\\'\\|[ \t]\\)")
229 first-line-prefix)
230 ;; If the second line prefix is whitespace, use it.
231 (string-match "\\`[ \t]+\\'" second-line-prefix))
232 second-line-prefix
233 ;; If the second line has the first line prefix,
234 ;; plus whitespace, use the part that the first line shares.
235 (if (string-match (concat "\\`"
236 (regexp-quote first-line-prefix)
237 "[ \t]*\\'")
238 second-line-prefix)
239 first-line-prefix)))
240 ;; If we get a fill prefix from a one-line paragraph,
241 ;; maybe change it to whitespace,
242 ;; and check that it isn't a paragraph starter.
243 (if first-line-prefix
244 (let ((result
245 ;; If first-line-prefix comes from the first line,
246 ;; see if it seems reasonable to use for all lines.
247 ;; If not, replace it with whitespace.
248 (if (or (and first-line-regexp
249 (string-match first-line-regexp
250 first-line-prefix))
251 (and comment-start-skip
252 (string-match comment-start-skip
253 first-line-prefix)))
254 first-line-prefix
255 (make-string (string-width first-line-prefix) ?\ ))))
256 ;; But either way, reject it if it indicates the start
257 ;; of a paragraph when text follows it.
258 (if (not (eq 0 (string-match paragraph-start
259 (concat result "a"))))
260 result)))))))
261
262 (defvar fill-nobreak-predicate nil
263 "If non-nil, a predicate for recognizing places not to break a line.
264 The predicate is called with no arguments, with point at the place
265 to be tested. If it returns t, fill commands do not break the line there.")
266
267 ;; Put `fill-find-break-point-function' property to charsets which
268 ;; require special functions to find line breaking point.
269 (let ((alist '((katakana-jisx0201 . kinsoku)
270 (chinese-gb2312 . kinsoku)
271 (japanese-jisx0208 . kinsoku)
272 (japanese-jisx0212 . kinsoku)
273 (chinese-big5-1 . kinsoku)
274 (chinese-big5-2 . kinsoku))))
275 (while alist
276 (put-charset-property (car (car alist)) 'fill-find-break-point-function
277 (cdr (car alist)))
278 (setq alist (cdr alist))))
279
280 (defun fill-find-break-point (limit)
281 "Move point to a proper line breaking position of the current line.
282 Don't move back past the buffer position LIMIT.
283
284 This function is called when we are going to break the current line
285 after or before a non-ascii character. If the charset of the
286 character has the property `fill-find-break-point-function', this
287 function calls the property value as a function with one arg LINEBEG.
288 If the charset has no such property, do nothing."
289 (let* ((ch (following-char))
290 (charset (char-charset ch))
291 func)
292 (if (eq charset 'ascii)
293 (setq ch (preceding-char)
294 charset (char-charset ch)))
295 (if (eq charset 'ascii)
296 nil
297 (if (eq charset 'composition)
298 (setq charset (char-charset (composite-char-component ch 0)))))
299 (setq func (get-charset-property charset 'fill-find-break-point-function))
300 (if (and func (fboundp func))
301 (funcall func limit))))
302
303 (defun fill-region-as-paragraph (from to &optional justify
304 nosqueeze squeeze-after)
305 "Fill the region as one paragraph.
306 It removes any paragraph breaks in the region and extra newlines at the end,
307 indents and fills lines between the margins given by the
308 `current-left-margin' and `current-fill-column' functions.
309 \(In most cases, the variable `fill-column' controls the width.)
310 It leaves point at the beginning of the line following the paragraph.
311
312 Normally performs justification according to the `current-justification'
313 function, but with a prefix arg, does full justification instead.
314
315 From a program, optional third arg JUSTIFY can specify any type of
316 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
317 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
318 means don't canonicalize spaces before that position.
319
320 If `sentence-end-double-space' is non-nil, then period followed by one
321 space does not end a sentence, so don't break a line there."
322 (interactive (list (region-beginning) (region-end)
323 (if current-prefix-arg 'full)))
324 (unless (memq justify '(t nil none full center left right))
325 (setq justify 'full))
326 ;; Arrange for undoing the fill to restore point.
327 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
328 (setq buffer-undo-list (cons (point) buffer-undo-list)))
329
330 ;; Make sure "to" is the endpoint.
331 (goto-char (min from to))
332 (setq to (max from to))
333 ;; Ignore blank lines at beginning of region.
334 (skip-chars-forward " \t\n")
335
336 (let ((from-plus-indent (point))
337 (oneleft nil))
338
339 (beginning-of-line)
340 (setq from (point))
341
342 ;; Delete all but one soft newline at end of region.
343 ;; And leave TO before that one.
344 (goto-char to)
345 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
346 (if (and oneleft
347 (not (and use-hard-newlines
348 (get-text-property (1- (point)) 'hard))))
349 (delete-backward-char 1)
350 (backward-char 1)
351 (setq oneleft t)))
352 (setq to (point))
353 ;;; ;; If there was no newline, and there is text in the paragraph, then
354 ;;; ;; create a newline.
355 ;;; (if (and (not oneleft) (> to from-plus-indent))
356 ;;; (newline))
357 (goto-char from-plus-indent))
358
359 (if (not (> to (point)))
360 nil ; There is no paragraph, only whitespace: exit now.
361
362 (or justify (setq justify (current-justification)))
363
364 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
365 (let ((fill-prefix fill-prefix))
366 ;; Figure out how this paragraph is indented, if desired.
367 (if (and adaptive-fill-mode
368 (or (null fill-prefix) (string= fill-prefix "")))
369 (setq fill-prefix (fill-context-prefix from to)))
370
371 (save-restriction
372 (goto-char from)
373 (beginning-of-line)
374 (narrow-to-region (point) to)
375
376 (if (not justify) ; filling disabled: just check indentation
377 (progn
378 (goto-char from)
379 (while (not (eobp))
380 (if (and (not (eolp))
381 (< (current-indentation) (current-left-margin)))
382 (indent-to-left-margin))
383 (forward-line 1)))
384
385 (if use-hard-newlines
386 (remove-text-properties from (point-max) '(hard nil)))
387 ;; Make sure first line is indented (at least) to left margin...
388 (if (or (memq justify '(right center))
389 (< (current-indentation) (current-left-margin)))
390 (indent-to-left-margin))
391 ;; Delete the fill prefix from every line except the first.
392 ;; The first line may not even have a fill prefix.
393 (goto-char from)
394 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
395 (concat "[ \t]*"
396 (regexp-quote fill-prefix)
397 "[ \t]*"))))
398 (and fpre
399 (progn
400 (if (>= (+ (current-left-margin) (length fill-prefix))
401 (current-fill-column))
402 (error "fill-prefix too long for specified width"))
403 (goto-char from)
404 (forward-line 1)
405 (while (not (eobp))
406 (if (looking-at fpre)
407 (delete-region (point) (match-end 0)))
408 (forward-line 1))
409 (goto-char from)
410 (if (looking-at fpre)
411 (goto-char (match-end 0)))
412 (setq from (point)))))
413 ;; Remove indentation from lines other than the first.
414 (beginning-of-line 2)
415 (indent-region (point) (point-max) 0)
416 (goto-char from)
417
418 ;; FROM, and point, are now before the text to fill,
419 ;; but after any fill prefix on the first line.
420
421 ;; Make sure sentences ending at end of line get an extra space.
422 ;; loses on split abbrevs ("Mr.\nSmith")
423 (while (re-search-forward "[.?!][])}\"']*$" nil t)
424 (or (eobp) (insert-and-inherit ?\ )))
425
426 (goto-char from)
427 (if enable-multibyte-characters
428 ;; Delete unnecessay newlines surrounded by words. The
429 ;; character category `|' means that we can break a line
430 ;; at the character. And, charset property
431 ;; `nospace-between-words' tells how to concatenate
432 ;; words. If the value is non-nil, never put spaces
433 ;; between words, thus delete a newline between them.
434 ;; If the value is nil, delete a newline only when a
435 ;; character preceding a newline has text property
436 ;; `nospace-between-words'.
437 (while (search-forward "\n" nil t)
438 (let ((prev (char-before (match-beginning 0)))
439 (next (following-char)))
440 (if (cmpcharp prev)
441 (setq prev (composite-char-component prev 0)))
442 (if (cmpcharp next)
443 (setq next (composite-char-component next 0)))
444 (if (and (or (aref (char-category-set next) ?|)
445 (aref (char-category-set prev) ?|))
446 (or (get-charset-property (char-charset prev)
447 'nospace-between-words)
448 (get-text-property (1- (match-beginning 0))
449 'nospace-between-words)))
450 (delete-char -1)))))
451
452 (goto-char from)
453 (skip-chars-forward " \t")
454 ;; Then change all newlines to spaces.
455 (subst-char-in-region from (point-max) ?\n ?\ )
456 (if (and nosqueeze (not (eq justify 'full)))
457 nil
458 (canonically-space-region (or squeeze-after (point)) (point-max))
459 (goto-char (point-max))
460 (delete-horizontal-space)
461 (insert-and-inherit " "))
462 (goto-char (point-min))
463
464 ;; This is the actual filling loop.
465 (let ((prefixcol 0) linebeg)
466 (while (not (eobp))
467 (setq linebeg (point))
468 (move-to-column (1+ (current-fill-column)))
469 (if (eobp)
470 (or nosqueeze (delete-horizontal-space))
471 ;; Move back to the point where we can break the line
472 ;; at. We break the line between word or after/before
473 ;; the character which has character category `|'. We
474 ;; search space, \c| followed by a character, or \c|
475 ;; following a character. If not found, place
476 ;; the point at linebeg.
477 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
478 ;; In case of space, we place the point at next to
479 ;; the point where the break occurs acutually,
480 ;; because we don't want to change the following
481 ;; logic of original Emacs. In case of \c|, the
482 ;; point is at the place where the break occurs.
483 (forward-char 1))
484 ;; Don't break after a period followed by just one space.
485 ;; Move back to the previous place to break.
486 ;; The reason is that if a period ends up at the end of a line,
487 ;; further fills will assume it ends a sentence.
488 ;; If we now know it does not end a sentence,
489 ;; avoid putting it at the end of the line.
490 (while (or (and sentence-end-double-space
491 (> (point) (+ linebeg 2))
492 (eq (preceding-char) ?\ )
493 (not (eq (following-char) ?\ ))
494 (eq (char-after (- (point) 2)) ?\.)
495 (progn (forward-char -2) t))
496 (and fill-nobreak-predicate
497 (funcall fill-nobreak-predicate)
498 (goto-char (match-beginning 0))))
499 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
500 (forward-char 1)))
501 ;; If the left margin and fill prefix by themselves
502 ;; pass the fill-column. or if they are zero
503 ;; but we have no room for even one word,
504 ;; keep at least one word or a character which has
505 ;; category `|'anyway .
506 ;; This handles ALL BUT the first line of the paragraph.
507 (if (if (zerop prefixcol)
508 (save-excursion
509 (skip-chars-backward " \t" linebeg)
510 (bolp))
511 (>= prefixcol (current-column)))
512 ;; Ok, skip at least one word or one \c| character.
513 ;; Meanwhile, don't stop at a period followed by one space.
514 (let ((first t))
515 (move-to-column prefixcol)
516 (while (and (not (eobp))
517 (or first
518 (and (not (bobp))
519 sentence-end-double-space
520 (save-excursion (forward-char -1)
521 (and (looking-at "\\. ")
522 (not (looking-at "\\. ")))))
523 (and fill-nobreak-predicate
524 (funcall fill-nobreak-predicate))))
525 ;; Find a breakable point while ignoring the
526 ;; following spaces.
527 (skip-chars-forward " \t")
528 (if (looking-at "\\c|")
529 (forward-char 1)
530 (let ((pos (save-excursion
531 (skip-chars-forward "^ \n\t")
532 (point))))
533 (if (re-search-forward "\\c|" pos t)
534 (forward-char -1)
535 (goto-char pos))))
536 (setq first nil)))
537 ;; Normally, move back over the single space between the words.
538 (if (= (preceding-char) ?\ ) (forward-char -1))
539
540 (if enable-multibyte-characters
541 ;; If we are going to break the line after or
542 ;; before a non-ascii character, we may have to
543 ;; run a special function for the charset of the
544 ;; character to find the correct break point.
545 (if (not (and (eq (charset-after (1- (point))) 'ascii)
546 (eq (charset-after (point)) 'ascii)))
547 (fill-find-break-point linebeg))))
548
549 ;; If the left margin and fill prefix by themselves
550 ;; pass the fill-column, keep at least one word.
551 ;; This handles the first line of the paragraph.
552 (if (and (zerop prefixcol)
553 (let ((fill-point (point)) nchars)
554 (save-excursion
555 (move-to-left-margin)
556 (setq nchars (- fill-point (point)))
557 (or (< nchars 0)
558 (and fill-prefix
559 (< nchars (length fill-prefix))
560 (string= (buffer-substring (point) fill-point)
561 (substring fill-prefix 0 nchars)))))))
562 ;; Ok, skip at least one word. But
563 ;; don't stop at a period followed by just one space.
564 (let ((first t))
565 (while (and (not (eobp))
566 (or first
567 (and (not (bobp))
568 sentence-end-double-space
569 (save-excursion (forward-char -1)
570 (and (looking-at "\\. ")
571 (not (looking-at "\\. ")))))
572 (and fill-nobreak-predicate
573 (funcall fill-nobreak-predicate))))
574 ;; Find a breakable point while ignoring the
575 ;; following spaces.
576 (skip-chars-forward " \t")
577 (if (looking-at "\\c|")
578 (forward-char 1)
579 (let ((pos (save-excursion
580 (skip-chars-forward "^ \n\t")
581 (point))))
582 (if (re-search-forward "\\c|" pos t)
583 (forward-char -1)
584 (goto-char pos))))
585 (setq first nil))))
586 ;; Check again to see if we got to the end of the paragraph.
587 (if (save-excursion (skip-chars-forward " \t") (eobp))
588 (or nosqueeze (delete-horizontal-space))
589 ;; Replace whitespace here with one newline, then indent to left
590 ;; margin.
591 (skip-chars-backward " \t")
592 (if (and (= (following-char) ?\ )
593 (or (aref (char-category-set (preceding-char)) ?|)
594 (looking-at "[ \t]+\\c|")))
595 ;; We need one space at end of line so that
596 ;; further filling won't delete it. NOTE: We
597 ;; intentionally leave this one space to
598 ;; distingush the case that user wants to put
599 ;; space between \c| characters.
600 (forward-char 1))
601 (insert ?\n)
602 ;; Give newline the properties of the space(s) it replaces
603 (set-text-properties (1- (point)) (point)
604 (text-properties-at (point)))
605 (indent-to-left-margin)
606 ;; Insert the fill prefix after indentation.
607 ;; Set prefixcol so whitespace in the prefix won't get lost.
608 (and fill-prefix (not (equal fill-prefix ""))
609 (progn
610 (insert-and-inherit fill-prefix)
611 (setq prefixcol (current-column))))))
612 ;; Justify the line just ended, if desired.
613 (if justify
614 (if (save-excursion (skip-chars-forward " \t") (eobp))
615 (progn
616 (delete-horizontal-space)
617 (justify-current-line justify t t))
618 (forward-line -1)
619 (justify-current-line justify nil t)
620 (forward-line 1))))))
621 ;; Leave point after final newline.
622 (goto-char (point-max)))
623 (unless (eobp)
624 (forward-char 1)))))
625
626 (defun fill-paragraph (arg)
627 "Fill paragraph at or after point. Prefix arg means justify as well.
628 If `sentence-end-double-space' is non-nil, then period followed by one
629 space does not end a sentence, so don't break a line there.
630 the variable `fill-column' controls the width for filling.
631
632 If `fill-paragraph-function' is non-nil, we call it (passing our
633 argument to it), and if it returns non-nil, we simply return its value."
634 (interactive (list (if current-prefix-arg 'full)))
635 (or (and fill-paragraph-function
636 (let ((function fill-paragraph-function)
637 fill-paragraph-function)
638 (funcall function arg)))
639 (let ((before (point))
640 ;; If fill-paragraph is called recursively,
641 ;; don't give fill-paragraph-function a second chance.
642 fill-paragraph-function)
643 (save-excursion
644 (forward-paragraph)
645 (or (bolp) (newline 1))
646 (let ((end (point))
647 (beg (progn (backward-paragraph) (point))))
648 (goto-char before)
649 (if use-hard-newlines
650 ;; Can't use fill-region-as-paragraph, since this paragraph may
651 ;; still contain hard newlines. See fill-region.
652 (fill-region beg end arg)
653 (fill-region-as-paragraph beg end arg)))))))
654
655 (defun fill-region (from to &optional justify nosqueeze to-eop)
656 "Fill each of the paragraphs in the region.
657 A prefix arg means justify as well.
658 Ordinarily the variable `fill-column' controls the width.
659
660 Noninteractively, the third argument JUSTIFY specifies which
661 kind of justification to do: `full', `left', `right', `center',
662 or `none' (equivalent to nil). t means handle each paragraph
663 as specified by its text properties.
664
665 The fourth arg NOSQUEEZE non-nil means to leave
666 whitespace other than line breaks untouched, and fifth arg TO-EOP
667 non-nil means to keep filling to the end of the paragraph (or next
668 hard newline, if `use-hard-newlines' is on).
669
670 If `sentence-end-double-space' is non-nil, then period followed by one
671 space does not end a sentence, so don't break a line there."
672 (interactive (list (region-beginning) (region-end)
673 (if current-prefix-arg 'full)))
674 (unless (memq justify '(t nil none full center left right))
675 (setq justify 'full))
676 (let (end beg)
677 (save-restriction
678 (goto-char (max from to))
679 (if to-eop
680 (progn (skip-chars-backward "\n")
681 (forward-paragraph)))
682 (setq end (point))
683 (goto-char (setq beg (min from to)))
684 (beginning-of-line)
685 (narrow-to-region (point) end)
686 (while (not (eobp))
687 (let ((initial (point))
688 end)
689 ;; If using hard newlines, break at every one for filling
690 ;; purposes rather than using paragraph breaks.
691 (if use-hard-newlines
692 (progn
693 (while (and (setq end (text-property-any (point) (point-max)
694 'hard t))
695 (not (= ?\n (char-after end)))
696 (not (= end (point-max))))
697 (goto-char (1+ end)))
698 (setq end (if end (min (point-max) (1+ end)) (point-max)))
699 (goto-char initial))
700 (forward-paragraph 1)
701 (setq end (point))
702 (forward-paragraph -1))
703 (if (< (point) beg)
704 (goto-char beg))
705 (if (>= (point) initial)
706 (fill-region-as-paragraph (point) end justify nosqueeze)
707 (goto-char end)))))))
708
709 \f
710 (defcustom default-justification 'left
711 "*Method of justifying text not otherwise specified.
712 Possible values are `left', `right', `full', `center', or `none'.
713 The requested kind of justification is done whenever lines are filled.
714 The `justification' text-property can locally override this variable.
715 This variable automatically becomes buffer-local when set in any fashion."
716 :type '(choice (const left)
717 (const right)
718 (const full)
719 (const center)
720 (const none))
721 :group 'fill)
722 (make-variable-buffer-local 'default-justification)
723
724 (defun current-justification ()
725 "How should we justify this line?
726 This returns the value of the text-property `justification',
727 or the variable `default-justification' if there is no text-property.
728 However, it returns nil rather than `none' to mean \"don't justify\"."
729 (let ((j (or (get-text-property
730 ;; Make sure we're looking at paragraph body.
731 (save-excursion (skip-chars-forward " \t")
732 (if (and (eobp) (not (bobp)))
733 (1- (point)) (point)))
734 'justification)
735 default-justification)))
736 (if (eq 'none j)
737 nil
738 j)))
739
740 (defun set-justification (begin end value &optional whole-par)
741 "Set the region's justification style.
742 The kind of justification to use is prompted for.
743 If the mark is not active, this command operates on the current paragraph.
744 If the mark is active, the region is used. However, if the beginning and end
745 of the region are not at paragraph breaks, they are moved to the beginning and
746 end of the paragraphs they are in.
747 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
748 breaks.
749
750 When calling from a program, operates just on region between BEGIN and END,
751 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
752 extended to include entire paragraphs as in the interactive command."
753 (interactive (list (if mark-active (region-beginning) (point))
754 (if mark-active (region-end) (point))
755 (let ((s (completing-read
756 "Set justification to: "
757 '(("left") ("right") ("full")
758 ("center") ("none"))
759 nil t)))
760 (if (equal s "") (error ""))
761 (intern s))
762 t))
763 (save-excursion
764 (save-restriction
765 (if whole-par
766 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
767 (paragraph-ignore-fill-prefix (if use-hard-newlines t
768 paragraph-ignore-fill-prefix)))
769 (goto-char begin)
770 (while (and (bolp) (not (eobp))) (forward-char 1))
771 (backward-paragraph)
772 (setq begin (point))
773 (goto-char end)
774 (skip-chars-backward " \t\n" begin)
775 (forward-paragraph)
776 (setq end (point))))
777
778 (narrow-to-region (point-min) end)
779 (unjustify-region begin (point-max))
780 (put-text-property begin (point-max) 'justification value)
781 (fill-region begin (point-max) nil t))))
782
783 (defun set-justification-none (b e)
784 "Disable automatic filling for paragraphs in the region.
785 If the mark is not active, this applies to the current paragraph."
786 (interactive (list (if mark-active (region-beginning) (point))
787 (if mark-active (region-end) (point))))
788 (set-justification b e 'none t))
789
790 (defun set-justification-left (b e)
791 "Make paragraphs in the region left-justified.
792 This is usually the default, but see the variable `default-justification'.
793 If the mark is not active, this applies to the current paragraph."
794 (interactive (list (if mark-active (region-beginning) (point))
795 (if mark-active (region-end) (point))))
796 (set-justification b e 'left t))
797
798 (defun set-justification-right (b e)
799 "Make paragraphs in the region right-justified:
800 Flush at the right margin and ragged on the left.
801 If the mark is not active, this applies to the current paragraph."
802 (interactive (list (if mark-active (region-beginning) (point))
803 (if mark-active (region-end) (point))))
804 (set-justification b e 'right t))
805
806 (defun set-justification-full (b e)
807 "Make paragraphs in the region fully justified:
808 This makes lines flush on both margins by inserting spaces between words.
809 If the mark is not active, this applies to the current paragraph."
810 (interactive (list (if mark-active (region-beginning) (point))
811 (if mark-active (region-end) (point))))
812 (set-justification b e 'full t))
813
814 (defun set-justification-center (b e)
815 "Make paragraphs in the region centered.
816 If the mark is not active, this applies to the current paragraph."
817 (interactive (list (if mark-active (region-beginning) (point))
818 (if mark-active (region-end) (point))))
819 (set-justification b e 'center t))
820
821 ;; A line has up to six parts:
822 ;;
823 ;; >>> hello.
824 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
825 ;;
826 ;; "Indent-1" is the left-margin indentation; normally it ends at column
827 ;; given by the `current-left-margin' function.
828 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
829 ;; "Indent-2" is added to justify a line if the `current-justification' is
830 ;; `center' or `right'. In `left' and `full' justification regions, any
831 ;; whitespace there is part of the line's text, and should not be changed.
832 ;; Trailing whitespace is not counted as part of the line length when
833 ;; center- or right-justifying.
834 ;;
835 ;; All parts of the line are optional, although the final newline can
836 ;; only be missing on the last line of the buffer.
837
838 (defun justify-current-line (&optional how eop nosqueeze)
839 "Do some kind of justification on this line.
840 Normally does full justification: adds spaces to the line to make it end at
841 the column given by `current-fill-column'.
842 Optional first argument HOW specifies alternate type of justification:
843 it can be `left', `right', `full', `center', or `none'.
844 If HOW is t, will justify however the `current-justification' function says to.
845 If HOW is nil or missing, full justification is done by default.
846 Second arg EOP non-nil means that this is the last line of the paragraph, so
847 it will not be stretched by full justification.
848 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
849 otherwise it is made canonical."
850 (interactive)
851 (if (eq t how) (setq how (or (current-justification) 'none))
852 (if (null how) (setq how 'full)
853 (or (memq how '(none left right center))
854 (setq how 'full))))
855 (or (memq how '(none left)) ; No action required for these.
856 (let ((fc (current-fill-column))
857 (pos (point-marker))
858 fp-end ; point at end of fill prefix
859 beg ; point at beginning of line's text
860 end ; point at end of line's text
861 indent ; column of `beg'
862 endcol ; column of `end'
863 ncols ; new indent point or offset
864 (nspaces 0) ; number of spaces between words
865 ; in line (not space characters)
866 fracspace ; fractional amount of space to be
867 ; added between each words
868 (curr-fracspace 0) ; current fractional space amount
869 count)
870 (end-of-line)
871 ;; Check if this is the last line of the paragraph.
872 (if (and use-hard-newlines (null eop)
873 (get-text-property (point) 'hard))
874 (setq eop t))
875 (skip-chars-backward " \t")
876 ;; Quick exit if it appears to be properly justified already
877 ;; or there is no text.
878 (if (or (bolp)
879 (and (memq how '(full right))
880 (= (current-column) fc)))
881 nil
882 (setq end (point))
883 (beginning-of-line)
884 (skip-chars-forward " \t")
885 ;; Skip over fill-prefix.
886 (if (and fill-prefix
887 (not (string-equal fill-prefix ""))
888 (equal fill-prefix
889 (buffer-substring
890 (point) (min (point-max) (+ (length fill-prefix)
891 (point))))))
892 (forward-char (length fill-prefix))
893 (if (and adaptive-fill-mode
894 (looking-at adaptive-fill-regexp))
895 (goto-char (match-end 0))))
896 (setq fp-end (point))
897 (skip-chars-forward " \t")
898 ;; This is beginning of the line's text.
899 (setq indent (current-column))
900 (setq beg (point))
901 (goto-char end)
902 (setq endcol (current-column))
903
904 ;; HOW can't be null or left--we would have exited already
905 (cond ((eq 'right how)
906 (setq ncols (- fc endcol))
907 (if (< ncols 0)
908 ;; Need to remove some indentation
909 (delete-region
910 (progn (goto-char fp-end)
911 (if (< (current-column) (+ indent ncols))
912 (move-to-column (+ indent ncols) t))
913 (point))
914 (progn (move-to-column indent) (point)))
915 ;; Need to add some
916 (goto-char beg)
917 (indent-to (+ indent ncols))
918 ;; If point was at beginning of text, keep it there.
919 (if (= beg pos)
920 (move-marker pos (point)))))
921
922 ((eq 'center how)
923 ;; Figure out how much indentation is needed
924 (setq ncols (+ (current-left-margin)
925 (/ (- fc (current-left-margin) ;avail. space
926 (- endcol indent)) ;text width
927 2)))
928 (if (< ncols indent)
929 ;; Have too much indentation - remove some
930 (delete-region
931 (progn (goto-char fp-end)
932 (if (< (current-column) ncols)
933 (move-to-column ncols t))
934 (point))
935 (progn (move-to-column indent) (point)))
936 ;; Have too little - add some
937 (goto-char beg)
938 (indent-to ncols)
939 ;; If point was at beginning of text, keep it there.
940 (if (= beg pos)
941 (move-marker pos (point)))))
942
943 ((eq 'full how)
944 ;; Insert extra spaces between words to justify line
945 (save-restriction
946 (narrow-to-region beg end)
947 (or nosqueeze
948 (canonically-space-region beg end))
949 (goto-char (point-max))
950 ;; count word spaces in line
951 (while (search-backward " " nil t)
952 (setq nspaces (1+ nspaces))
953 (skip-chars-backward " "))
954 (setq ncols (- fc endcol))
955 ;; Ncols is number of additional space chars needed
956 (if (and (> ncols 0) (> nspaces 0) (not eop))
957 (progn
958 (setq curr-fracspace (+ ncols (/ (1+ nspaces) 2))
959 count nspaces)
960 (while (> count 0)
961 (skip-chars-forward " ")
962 (insert-and-inherit
963 (make-string (/ curr-fracspace nspaces) ?\ ))
964 (search-forward " " nil t)
965 (setq count (1- count)
966 curr-fracspace
967 (+ (% curr-fracspace nspaces) ncols)))))))
968 (t (error "Unknown justification value"))))
969 (goto-char pos)
970 (move-marker pos nil)))
971 nil)
972
973 (defun unjustify-current-line ()
974 "Remove justification whitespace from current line.
975 If the line is centered or right-justified, this function removes any
976 indentation past the left margin. If the line is full-justified, it removes
977 extra spaces between words. It does nothing in other justification modes."
978 (let ((justify (current-justification)))
979 (cond ((eq 'left justify) nil)
980 ((eq nil justify) nil)
981 ((eq 'full justify) ; full justify: remove extra spaces
982 (beginning-of-line-text)
983 (canonically-space-region
984 (point) (save-excursion (end-of-line) (point))))
985 ((memq justify '(center right))
986 (save-excursion
987 (move-to-left-margin nil t)
988 ;; Position ourselves after any fill-prefix.
989 (if (and fill-prefix
990 (not (string-equal fill-prefix ""))
991 (equal fill-prefix
992 (buffer-substring
993 (point) (min (point-max) (+ (length fill-prefix)
994 (point))))))
995 (forward-char (length fill-prefix)))
996 (delete-region (point) (progn (skip-chars-forward " \t")
997 (point))))))))
998
999 (defun unjustify-region (&optional begin end)
1000 "Remove justification whitespace from region.
1001 For centered or right-justified regions, this function removes any indentation
1002 past the left margin from each line. For full-justified lines, it removes
1003 extra spaces between words. It does nothing in other justification modes.
1004 Arguments BEGIN and END are optional; default is the whole buffer."
1005 (save-excursion
1006 (save-restriction
1007 (if end (narrow-to-region (point-min) end))
1008 (goto-char (or begin (point-min)))
1009 (while (not (eobp))
1010 (unjustify-current-line)
1011 (forward-line 1)))))
1012
1013 \f
1014 (defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
1015 "Fill paragraphs within the region, allowing varying indentation within each.
1016 This command divides the region into \"paragraphs\",
1017 only at paragraph-separator lines, then fills each paragraph
1018 using as the fill prefix the smallest indentation of any line
1019 in the paragraph.
1020
1021 When calling from a program, pass range to fill as first two arguments.
1022
1023 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1024 JUSTIFY to justify paragraphs (prefix arg),
1025 When filling a mail message, pass a regexp for CITATION-REGEXP
1026 which will match the prefix of a line which is a citation marker
1027 plus whitespace, but no other kind of prefix.
1028 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1029 (interactive (list (region-beginning) (region-end)
1030 (if current-prefix-arg 'full)))
1031 (let ((fill-individual-varying-indent t))
1032 (fill-individual-paragraphs min max justifyp citation-regexp)))
1033
1034 (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
1035 "Fill paragraphs of uniform indentation within the region.
1036 This command divides the region into \"paragraphs\",
1037 treating every change in indentation level or prefix as a paragraph boundary,
1038 then fills each paragraph using its indentation level as the fill prefix.
1039
1040 There is one special case where a change in indentation does not start
1041 a new paragraph. This is for text of this form:
1042
1043 foo> This line with extra indentation starts
1044 foo> a paragraph that continues on more lines.
1045
1046 These lines are filled together.
1047
1048 When calling from a program, pass the range to fill
1049 as the first two arguments.
1050
1051 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1052 JUSTIFY to justify paragraphs (prefix arg),
1053 When filling a mail message, pass a regexp for CITATION-REGEXP
1054 which will match the prefix of a line which is a citation marker
1055 plus whitespace, but no other kind of prefix.
1056 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1057 (interactive (list (region-beginning) (region-end)
1058 (if current-prefix-arg 'full)))
1059 (save-restriction
1060 (save-excursion
1061 (goto-char min)
1062 (beginning-of-line)
1063 (narrow-to-region (point) max)
1064 (if citation-regexp
1065 (while (and (not (eobp))
1066 (or (looking-at "[ \t]*[^ \t\n]+:")
1067 (looking-at "[ \t]*$")))
1068 (if (looking-at "[ \t]*[^ \t\n]+:")
1069 (search-forward "\n\n" nil 'move)
1070 (forward-line 1))))
1071 (narrow-to-region (point) max)
1072 ;; Loop over paragraphs.
1073 (while (let ((here (point)))
1074 ;; Skip over all paragraph-separating lines
1075 ;; so as to not include them in any paragraph.
1076 (while (and (not (eobp))
1077 (progn (move-to-left-margin)
1078 (and (not (eobp))
1079 (looking-at paragraph-separate))))
1080 (forward-line 1))
1081 (skip-chars-forward " \t\n") (not (eobp)))
1082 (move-to-left-margin)
1083 (let ((start (point))
1084 fill-prefix fill-prefix-regexp)
1085 ;; Find end of paragraph, and compute the smallest fill-prefix
1086 ;; that fits all the lines in this paragraph.
1087 (while (progn
1088 ;; Update the fill-prefix on the first line
1089 ;; and whenever the prefix good so far is too long.
1090 (if (not (and fill-prefix
1091 (looking-at fill-prefix-regexp)))
1092 (setq fill-prefix
1093 (fill-individual-paragraphs-prefix citation-regexp)
1094 fill-prefix-regexp (regexp-quote fill-prefix)))
1095 (forward-line 1)
1096 (if (bolp)
1097 ;; If forward-line went past a newline,
1098 ;; move further to the left margin.
1099 (move-to-left-margin))
1100 ;; Now stop the loop if end of paragraph.
1101 (and (not (eobp))
1102 (if fill-individual-varying-indent
1103 ;; If this line is a separator line, with or
1104 ;; without prefix, end the paragraph.
1105 (and
1106 (not (looking-at paragraph-separate))
1107 (save-excursion
1108 (not (and (looking-at fill-prefix-regexp)
1109 (progn (forward-char (length fill-prefix))
1110 (looking-at paragraph-separate))))))
1111 ;; If this line has more or less indent
1112 ;; than the fill prefix wants, end the paragraph.
1113 (and (looking-at fill-prefix-regexp)
1114 (save-excursion
1115 (not (progn (forward-char (length fill-prefix))
1116 (or (looking-at "[ \t]")
1117 (looking-at paragraph-separate)
1118 (looking-at paragraph-start))))))))))
1119 ;; Fill this paragraph, but don't add a newline at the end.
1120 (let ((had-newline (bolp)))
1121 (fill-region-as-paragraph start (point) justify)
1122 (if (and (bolp) (not had-newline))
1123 (delete-char -1))))))))
1124
1125 (defun fill-individual-paragraphs-prefix (citation-regexp)
1126 (or (let ((adaptive-fill-first-line-regexp "")
1127 just-one-line-prefix
1128 two-lines-prefix
1129 one-line-citation-part
1130 two-lines-citation-part
1131 adjusted-two-lines-citation-part)
1132 (setq just-one-line-prefix
1133 (fill-context-prefix
1134 (point)
1135 (save-excursion (forward-line 1)
1136 (point))))
1137 (setq two-lines-prefix
1138 (fill-context-prefix
1139 (point)
1140 (save-excursion (forward-line 2)
1141 (point))))
1142 (when just-one-line-prefix
1143 (setq one-line-citation-part
1144 (if citation-regexp
1145 (fill-individual-paragraphs-citation just-one-line-prefix
1146 citation-regexp)
1147 just-one-line-prefix)))
1148 (when two-lines-prefix
1149 (setq two-lines-citation-part
1150 (if citation-regexp
1151 (fill-individual-paragraphs-citation two-lines-prefix
1152 citation-regexp)
1153 just-one-line-prefix))
1154 (or two-lines-citation-part (setq two-lines-citation-part ""))
1155 (setq adjusted-two-lines-citation-part
1156 (substring two-lines-citation-part 0
1157 (string-match "[ \t]*\\'"
1158 two-lines-citation-part))))
1159 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1160 ;; is the same as that of TWO-LINES-PREFIX,
1161 ;; except perhaps with longer whitespace.
1162 (if (and just-one-line-prefix
1163 two-lines-prefix
1164 (string-match (concat "\\`"
1165 (regexp-quote adjusted-two-lines-citation-part)
1166 "[ \t]*\\'")
1167 one-line-citation-part)
1168 (>= (string-width one-line-citation-part)
1169 (string-width two-lines-citation-part)))
1170 two-lines-prefix
1171 just-one-line-prefix))
1172 (buffer-substring
1173 (point)
1174 (save-excursion (skip-chars-forward " \t")
1175 (point)))))
1176
1177 (defun fill-individual-paragraphs-citation (string citation-regexp)
1178 (string-match citation-regexp
1179 string)
1180 (match-string 0 string))
1181
1182 ;;; fill.el ends here