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