(sentence-end-double-space, sentence-end-without-period): Move to paragraphs.
[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 t
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 (let ((firstline (point))
204 first-line-prefix
205 ;; Non-nil if we are on the second line.
206 second-line-prefix
207 start)
208 (move-to-left-margin)
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 start)
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-region-as-paragraph (from to &optional justify
372 nosqueeze squeeze-after)
373 "Fill the region as one paragraph.
374 It removes any paragraph breaks in the region and extra newlines at the end,
375 indents and fills lines between the margins given by the
376 `current-left-margin' and `current-fill-column' functions.
377 \(In most cases, the variable `fill-column' controls the width.)
378 It leaves point at the beginning of the line following the paragraph.
379
380 Normally performs justification according to the `current-justification'
381 function, but with a prefix arg, does full justification instead.
382
383 From a program, optional third arg JUSTIFY can specify any type of
384 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
385 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
386 means don't canonicalize spaces before that position.
387
388 Return the fill-prefix used for filling.
389
390 If `sentence-end-double-space' is non-nil, then period followed by one
391 space does not end a sentence, so don't break a line there."
392 (interactive (progn
393 (barf-if-buffer-read-only)
394 (list (region-beginning) (region-end)
395 (if current-prefix-arg 'full))))
396 (unless (memq justify '(t nil none full center left right))
397 (setq justify 'full))
398 ;; Arrange for undoing the fill to restore point.
399 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
400 (setq buffer-undo-list (cons (point) buffer-undo-list)))
401
402 ;; Make sure "to" is the endpoint.
403 (goto-char (min from to))
404 (setq to (max from to))
405 ;; Ignore blank lines at beginning of region.
406 (skip-chars-forward " \t\n")
407
408 (let ((from-plus-indent (point))
409 (oneleft nil))
410
411 (beginning-of-line)
412 (setq from (point))
413
414 ;; Delete all but one soft newline at end of region.
415 ;; And leave TO before that one.
416 (goto-char to)
417 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
418 (if (and oneleft
419 (not (and use-hard-newlines
420 (get-text-property (1- (point)) 'hard))))
421 (delete-backward-char 1)
422 (backward-char 1)
423 (setq oneleft t)))
424 (setq to (point))
425 ;; ;; If there was no newline, and there is text in the paragraph, then
426 ;; ;; create a newline.
427 ;; (if (and (not oneleft) (> to from-plus-indent))
428 ;; (newline))
429 (goto-char from-plus-indent))
430
431 (if (not (> to (point)))
432 nil ; There is no paragraph, only whitespace: exit now.
433
434 (or justify (setq justify (current-justification)))
435
436 ;; Never indent-according-to-mode with brain dead "indenting" functions.
437 (when (and fill-indent-according-to-mode
438 (memq indent-line-function
439 '(indent-relative-maybe indent-relative
440 indent-to-left-margin)))
441 (set (make-local-variable 'fill-indent-according-to-mode) nil))
442
443 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
444 (let ((fill-prefix fill-prefix))
445 ;; Figure out how this paragraph is indented, if desired.
446 (if (and adaptive-fill-mode
447 (or (null fill-prefix) (string= fill-prefix "")))
448 (setq fill-prefix (fill-context-prefix from to)))
449
450 (save-restriction
451 (goto-char from)
452 (beginning-of-line)
453 (narrow-to-region (point) to)
454
455 (if (not justify) ; filling disabled: just check indentation
456 (progn
457 (goto-char from)
458 (while (not (eobp))
459 (if (and (not (eolp))
460 (< (current-indentation) (current-left-margin)))
461 (indent-to-left-margin))
462 (forward-line 1)))
463
464 (if use-hard-newlines
465 (remove-text-properties from (point-max) '(hard nil)))
466 ;; Make sure first line is indented (at least) to left margin...
467 (if (or (memq justify '(right center))
468 (< (current-indentation) (current-left-margin)))
469 (indent-to-left-margin))
470 ;; Delete the fill prefix from every line except the first.
471 ;; The first line may not even have a fill prefix.
472 (goto-char from)
473 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
474 (concat "[ \t]*"
475 (regexp-quote fill-prefix)
476 "[ \t]*"))))
477 (and fpre
478 (progn
479 (if (>= (+ (current-left-margin) (length fill-prefix))
480 (current-fill-column))
481 (error "fill-prefix too long for specified width"))
482 (goto-char from)
483 (forward-line 1)
484 (while (not (eobp))
485 (if (looking-at fpre)
486 (delete-region (point) (match-end 0)))
487 (forward-line 1))
488 (goto-char from)
489 (if (looking-at fpre)
490 (goto-char (match-end 0)))
491 (setq from (point)))))
492 ;; Remove indentation from lines other than the first.
493 (beginning-of-line 2)
494 (indent-region (point) (point-max) 0)
495 (goto-char from)
496
497 ;; FROM, and point, are now before the text to fill,
498 ;; but after any fill prefix on the first line.
499
500 ;; Make sure sentences ending at end of line get an extra space.
501 ;; loses on split abbrevs ("Mr.\nSmith")
502 (let ((eol-double-space-re (if colon-double-space
503 "[.?!:][])}\"']*$"
504 "[.?!][])}\"']*$")))
505 (while (re-search-forward eol-double-space-re nil t)
506 (or (eobp) (insert-and-inherit ?\ ))))
507
508 (goto-char from)
509 (if enable-multibyte-characters
510 ;; Delete unnecessay newlines surrounded by words. The
511 ;; character category `|' means that we can break a line
512 ;; at the character. And, charset property
513 ;; `nospace-between-words' tells how to concatenate
514 ;; words. If the value is non-nil, never put spaces
515 ;; between words, thus delete a newline between them.
516 ;; If the value is nil, delete a newline only when a
517 ;; character preceding a newline has text property
518 ;; `nospace-between-words'.
519 (while (search-forward "\n" nil t)
520 (let ((prev (char-before (match-beginning 0)))
521 (next (following-char)))
522 (if (and (or (aref (char-category-set next) ?|)
523 (aref (char-category-set prev) ?|))
524 (or (get-charset-property (char-charset prev)
525 'nospace-between-words)
526 (get-text-property (1- (match-beginning 0))
527 'nospace-between-words)))
528 (delete-char -1)))))
529
530 (goto-char from)
531 (skip-chars-forward " \t")
532 ;; Then change all newlines to spaces.
533 (subst-char-in-region from (point-max) ?\n ?\ )
534 (if (and nosqueeze (not (eq justify 'full)))
535 nil
536 (canonically-space-region (or squeeze-after (point)) (point-max))
537 (goto-char (point-max))
538 (delete-horizontal-space)
539 (insert-and-inherit " "))
540 (goto-char (point-min))
541
542 ;; This is the actual filling loop.
543 (let ((prefixcol 0) linebeg)
544 (while (not (eobp))
545 (setq linebeg (point))
546 (move-to-column (1+ (current-fill-column)))
547 (if (eobp)
548 (or nosqueeze (delete-horizontal-space))
549 ;; Move back to the point where we can break the line
550 ;; at. We break the line between word or after/before
551 ;; the character which has character category `|'. We
552 ;; search space, \c| followed by a character, or \c|
553 ;; following a character. If not found, place
554 ;; the point at linebeg.
555 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
556 ;; In case of space, we place the point at next to
557 ;; the point where the break occurs acutually,
558 ;; because we don't want to change the following
559 ;; logic of original Emacs. In case of \c|, the
560 ;; point is at the place where the break occurs.
561 (forward-char 1))
562 ;; Don't break after a period followed by just one space.
563 ;; Move back to the previous place to break.
564 ;; The reason is that if a period ends up at the end of a line,
565 ;; further fills will assume it ends a sentence.
566 ;; If we now know it does not end a sentence,
567 ;; avoid putting it at the end of the line.
568 (while (and (> (point) linebeg) (fill-nobreak-p))
569 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
570 (forward-char 1)))
571 ;; If the left margin and fill prefix by themselves
572 ;; pass the fill-column. or if they are zero
573 ;; but we have no room for even one word,
574 ;; keep at least one word or a character which has
575 ;; category `|'anyway .
576 ;; This handles ALL BUT the first line of the paragraph.
577 (if (if (zerop prefixcol)
578 (save-excursion
579 (skip-chars-backward " \t" linebeg)
580 (bolp))
581 (>= prefixcol (current-column)))
582 ;; Ok, skip at least one word or one \c| character.
583 ;; Meanwhile, don't stop at a period followed by one space.
584 (let ((fill-nobreak-predicate nil) ;to break sooner.
585 (first t))
586 (move-to-column prefixcol)
587 (while (and (not (eobp)) (or first (fill-nobreak-p)))
588 ;; Find a breakable point while ignoring the
589 ;; following spaces.
590 (skip-chars-forward " \t")
591 (if (looking-at "\\c|")
592 (forward-char 1)
593 (let ((pos (save-excursion
594 (skip-chars-forward "^ \n\t")
595 (point))))
596 (if (re-search-forward "\\c|" pos t)
597 (forward-char -1)
598 (goto-char pos))))
599 (setq first nil)))
600 ;; Normally, move back over the single space between
601 ;; the words.
602 (skip-chars-backward " \t")
603
604 (if enable-multibyte-characters
605 ;; If we are going to break the line after or
606 ;; before a non-ascii character, we may have to
607 ;; run a special function for the charset of the
608 ;; character to find the correct break point.
609 (if (not (and (eq (charset-after (1- (point))) 'ascii)
610 (eq (charset-after (point)) 'ascii)))
611 ;; Make sure we take SOMETHING after the
612 ;; fill prefix if any.
613 (fill-find-break-point
614 (save-excursion
615 (goto-char linebeg)
616 (move-to-column prefixcol)
617 (point))))))
618
619 ;; If the left margin and fill prefix by themselves
620 ;; pass the fill-column, keep at least one word.
621 ;; This handles the first line of the paragraph.
622 (if (and (zerop prefixcol)
623 (let ((fill-point (point)) nchars)
624 (save-excursion
625 (move-to-left-margin)
626 (setq nchars (- fill-point (point)))
627 (or (< nchars 0)
628 (and fill-prefix
629 (< nchars (length fill-prefix))
630 (string= (buffer-substring (point)
631 fill-point)
632 (substring fill-prefix
633 0 nchars)))))))
634 ;; Ok, skip at least one word. But
635 ;; don't stop at a period followed by just one space.
636 (let ((fill-nobreak-predicate nil) ;to break sooner.
637 (first t))
638 (while (and (not (eobp)) (or first (fill-nobreak-p)))
639 ;; Find a breakable point while ignoring the
640 ;; following spaces.
641 (skip-chars-forward " \t")
642 (if (looking-at "\\c|")
643 (forward-char 1)
644 (let ((pos (save-excursion
645 (skip-chars-forward "^ \n\t")
646 (point))))
647 (if (re-search-forward "\\c|" pos t)
648 (forward-char -1)
649 (goto-char pos))))
650 (setq first nil))))
651 ;; Check again to see if we got to the end of the paragraph.
652 (if (save-excursion (skip-chars-forward " \t") (eobp))
653 (or nosqueeze (delete-horizontal-space))
654 ;; Replace whitespace here with one newline, then
655 ;; indent to left margin.
656 (skip-chars-backward " \t")
657 (if (and (= (following-char) ?\ )
658 (or (aref (char-category-set (preceding-char)) ?|)
659 (looking-at "[ \t]+\\c|")))
660 ;; We need one space at end of line so that
661 ;; further filling won't delete it. NOTE: We
662 ;; intentionally leave this one space to
663 ;; distingush the case that user wants to put
664 ;; space between \c| characters.
665 (forward-char 1))
666 (insert ?\n)
667 ;; Give newline the properties of the space(s) it replaces
668 (set-text-properties (1- (point)) (point)
669 (text-properties-at (point)))
670 (if (or fill-prefix
671 (not fill-indent-according-to-mode))
672 (indent-to-left-margin)
673 (indent-according-to-mode))
674 ;; Insert the fill prefix after indentation.
675 ;; Set prefixcol so whitespace in the prefix won't get lost.
676 (and fill-prefix (not (equal fill-prefix ""))
677 (progn
678 (insert-and-inherit fill-prefix)
679 (setq prefixcol (current-column))))))
680 ;; Justify the line just ended, if desired.
681 (if justify
682 (if (save-excursion (skip-chars-forward " \t") (eobp))
683 (progn
684 (delete-horizontal-space)
685 (justify-current-line justify t t))
686 (forward-line -1)
687 (justify-current-line justify nil t)
688 (forward-line 1))))))
689 ;; Leave point after final newline.
690 (goto-char (point-max)))
691 (unless (eobp)
692 (forward-char 1))
693 ;; Return the fill-prefix we used
694 fill-prefix)))
695
696 (defsubst skip-line-prefix (prefix)
697 "If point is inside the string PREFIX at the beginning of line, move past it."
698 (when (and prefix
699 (< (- (point) (line-beginning-position)) (length prefix))
700 (save-excursion
701 (beginning-of-line)
702 (looking-at (regexp-quote prefix))))
703 (goto-char (match-end 0))))
704
705 (defun fill-paragraph (arg)
706 "Fill paragraph at or after point. Prefix ARG means justify as well.
707 If `sentence-end-double-space' is non-nil, then period followed by one
708 space does not end a sentence, so don't break a line there.
709 the variable `fill-column' controls the width for filling.
710
711 If `fill-paragraph-function' is non-nil, we call it (passing our
712 argument to it), and if it returns non-nil, we simply return its value.
713
714 If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling."
715 (interactive (progn
716 (barf-if-buffer-read-only)
717 (list (if current-prefix-arg 'full))))
718 (or (and fill-paragraph-function
719 (let ((function fill-paragraph-function)
720 fill-paragraph-function)
721 (funcall function arg)))
722 (let ((before (point))
723 ;; Fill prefix used for filling the paragraph
724 fill-pfx
725 ;; If fill-paragraph is called recursively,
726 ;; don't give fill-paragraph-function a second chance.
727 fill-paragraph-function)
728 (save-excursion
729 (forward-paragraph)
730 (or (bolp) (newline 1))
731 (let ((end (point))
732 (beg (progn (backward-paragraph) (point))))
733 (goto-char before)
734 (setq fill-pfx
735 (if use-hard-newlines
736 ;; Can't use fill-region-as-paragraph, since this
737 ;; paragraph may still contain hard newlines. See
738 ;; fill-region.
739 (fill-region beg end arg)
740 (fill-region-as-paragraph beg end arg)))))
741 ;; See if point ended up inside the fill-prefix, and if so, move
742 ;; past it.
743 (skip-line-prefix fill-pfx)
744 fill-pfx)))
745
746 (defun fill-region (from to &optional justify nosqueeze to-eop)
747 "Fill each of the paragraphs in the region.
748 A prefix arg means justify as well.
749 Ordinarily the variable `fill-column' controls the width.
750
751 Noninteractively, the third argument JUSTIFY specifies which
752 kind of justification to do: `full', `left', `right', `center',
753 or `none' (equivalent to nil). t means handle each paragraph
754 as specified by its text properties.
755
756 The fourth arg NOSQUEEZE non-nil means to leave
757 whitespace other than line breaks untouched, and fifth arg TO-EOP
758 non-nil means to keep filling to the end of the paragraph (or next
759 hard newline, if `use-hard-newlines' is on).
760
761 Return the fill-prefix used for filling the last paragraph.
762
763 If `sentence-end-double-space' is non-nil, then period followed by one
764 space does not end a sentence, so don't break a line there."
765 (interactive (progn
766 (barf-if-buffer-read-only)
767 (list (region-beginning) (region-end)
768 (if current-prefix-arg 'full))))
769 (unless (memq justify '(t nil none full center left right))
770 (setq justify 'full))
771 (let (end beg fill-pfx)
772 (save-restriction
773 (goto-char (max from to))
774 (when to-eop
775 (skip-chars-backward "\n")
776 (forward-paragraph))
777 (setq end (point))
778 (goto-char (setq beg (min from to)))
779 (beginning-of-line)
780 (narrow-to-region (point) end)
781 (while (not (eobp))
782 (let ((initial (point))
783 end)
784 ;; If using hard newlines, break at every one for filling
785 ;; purposes rather than using paragraph breaks.
786 (if use-hard-newlines
787 (progn
788 (while (and (setq end (text-property-any (point) (point-max)
789 'hard t))
790 (not (= ?\n (char-after end)))
791 (not (= end (point-max))))
792 (goto-char (1+ end)))
793 (setq end (if end (min (point-max) (1+ end)) (point-max)))
794 (goto-char initial))
795 (forward-paragraph 1)
796 (setq end (point))
797 (forward-paragraph -1))
798 (if (< (point) beg)
799 (goto-char beg))
800 (if (>= (point) initial)
801 (setq fill-pfx
802 (fill-region-as-paragraph (point) end justify nosqueeze))
803 (goto-char end))))
804 fill-pfx)))
805
806 \f
807 (defcustom default-justification 'left
808 "*Method of justifying text not otherwise specified.
809 Possible values are `left', `right', `full', `center', or `none'.
810 The requested kind of justification is done whenever lines are filled.
811 The `justification' text-property can locally override this variable."
812 :type '(choice (const left)
813 (const right)
814 (const full)
815 (const center)
816 (const none))
817 :group 'fill)
818 (make-variable-buffer-local 'default-justification)
819
820 (defun current-justification ()
821 "How should we justify this line?
822 This returns the value of the text-property `justification',
823 or the variable `default-justification' if there is no text-property.
824 However, it returns nil rather than `none' to mean \"don't justify\"."
825 (let ((j (or (get-text-property
826 ;; Make sure we're looking at paragraph body.
827 (save-excursion (skip-chars-forward " \t")
828 (if (and (eobp) (not (bobp)))
829 (1- (point)) (point)))
830 'justification)
831 default-justification)))
832 (if (eq 'none j)
833 nil
834 j)))
835
836 (defun set-justification (begin end value &optional whole-par)
837 "Set the region's justification style.
838 The kind of justification to use is prompted for.
839 If the mark is not active, this command operates on the current paragraph.
840 If the mark is active, the region is used. However, if the beginning and end
841 of the region are not at paragraph breaks, they are moved to the beginning and
842 end of the paragraphs they are in.
843 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
844 breaks.
845
846 When calling from a program, operates just on region between BEGIN and END,
847 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
848 extended to include entire paragraphs as in the interactive command."
849 (interactive (list (if mark-active (region-beginning) (point))
850 (if mark-active (region-end) (point))
851 (let ((s (completing-read
852 "Set justification to: "
853 '(("left") ("right") ("full")
854 ("center") ("none"))
855 nil t)))
856 (if (equal s "") (error ""))
857 (intern s))
858 t))
859 (save-excursion
860 (save-restriction
861 (if whole-par
862 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
863 (paragraph-ignore-fill-prefix (if use-hard-newlines t
864 paragraph-ignore-fill-prefix)))
865 (goto-char begin)
866 (while (and (bolp) (not (eobp))) (forward-char 1))
867 (backward-paragraph)
868 (setq begin (point))
869 (goto-char end)
870 (skip-chars-backward " \t\n" begin)
871 (forward-paragraph)
872 (setq end (point))))
873
874 (narrow-to-region (point-min) end)
875 (unjustify-region begin (point-max))
876 (put-text-property begin (point-max) 'justification value)
877 (fill-region begin (point-max) nil t))))
878
879 (defun set-justification-none (b e)
880 "Disable automatic filling for paragraphs in the region.
881 If the mark is not active, this applies to the current paragraph."
882 (interactive (list (if mark-active (region-beginning) (point))
883 (if mark-active (region-end) (point))))
884 (set-justification b e 'none t))
885
886 (defun set-justification-left (b e)
887 "Make paragraphs in the region left-justified.
888 This is usually the default, but see the variable `default-justification'.
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 'left t))
893
894 (defun set-justification-right (b e)
895 "Make paragraphs in the region right-justified:
896 Flush at the right margin and ragged on the left.
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 'right t))
901
902 (defun set-justification-full (b e)
903 "Make paragraphs in the region fully justified:
904 This makes lines flush on both margins by inserting spaces between words.
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 'full t))
909
910 (defun set-justification-center (b e)
911 "Make paragraphs in the region centered.
912 If the mark is not active, this applies to the current paragraph."
913 (interactive (list (if mark-active (region-beginning) (point))
914 (if mark-active (region-end) (point))))
915 (set-justification b e 'center t))
916
917 ;; A line has up to six parts:
918 ;;
919 ;; >>> hello.
920 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
921 ;;
922 ;; "Indent-1" is the left-margin indentation; normally it ends at column
923 ;; given by the `current-left-margin' function.
924 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
925 ;; "Indent-2" is added to justify a line if the `current-justification' is
926 ;; `center' or `right'. In `left' and `full' justification regions, any
927 ;; whitespace there is part of the line's text, and should not be changed.
928 ;; Trailing whitespace is not counted as part of the line length when
929 ;; center- or right-justifying.
930 ;;
931 ;; All parts of the line are optional, although the final newline can
932 ;; only be missing on the last line of the buffer.
933
934 (defun justify-current-line (&optional how eop nosqueeze)
935 "Do some kind of justification on this line.
936 Normally does full justification: adds spaces to the line to make it end at
937 the column given by `current-fill-column'.
938 Optional first argument HOW specifies alternate type of justification:
939 it can be `left', `right', `full', `center', or `none'.
940 If HOW is t, will justify however the `current-justification' function says to.
941 If HOW is nil or missing, full justification is done by default.
942 Second arg EOP non-nil means that this is the last line of the paragraph, so
943 it will not be stretched by full justification.
944 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
945 otherwise it is made canonical."
946 (interactive "*")
947 (if (eq t how) (setq how (or (current-justification) 'none))
948 (if (null how) (setq how 'full)
949 (or (memq how '(none left right center))
950 (setq how 'full))))
951 (or (memq how '(none left)) ; No action required for these.
952 (let ((fc (current-fill-column))
953 (pos (point-marker))
954 fp-end ; point at end of fill prefix
955 beg ; point at beginning of line's text
956 end ; point at end of line's text
957 indent ; column of `beg'
958 endcol ; column of `end'
959 ncols ; new indent point or offset
960 (nspaces 0) ; number of spaces between words
961 ; in line (not space characters)
962 fracspace ; fractional amount of space to be
963 ; added between each words
964 (curr-fracspace 0) ; current fractional space amount
965 count)
966 (end-of-line)
967 ;; Check if this is the last line of the paragraph.
968 (if (and use-hard-newlines (null eop)
969 (get-text-property (point) 'hard))
970 (setq eop t))
971 (skip-chars-backward " \t")
972 ;; Quick exit if it appears to be properly justified already
973 ;; or there is no text.
974 (if (or (bolp)
975 (and (memq how '(full right))
976 (= (current-column) fc)))
977 nil
978 (setq end (point))
979 (beginning-of-line)
980 (skip-chars-forward " \t")
981 ;; Skip over fill-prefix.
982 (if (and fill-prefix
983 (not (string-equal fill-prefix ""))
984 (equal fill-prefix
985 (buffer-substring
986 (point) (min (point-max) (+ (length fill-prefix)
987 (point))))))
988 (forward-char (length fill-prefix))
989 (if (and adaptive-fill-mode
990 (looking-at adaptive-fill-regexp))
991 (goto-char (match-end 0))))
992 (setq fp-end (point))
993 (skip-chars-forward " \t")
994 ;; This is beginning of the line's text.
995 (setq indent (current-column))
996 (setq beg (point))
997 (goto-char end)
998 (setq endcol (current-column))
999
1000 ;; HOW can't be null or left--we would have exited already
1001 (cond ((eq 'right how)
1002 (setq ncols (- fc endcol))
1003 (if (< ncols 0)
1004 ;; Need to remove some indentation
1005 (delete-region
1006 (progn (goto-char fp-end)
1007 (if (< (current-column) (+ indent ncols))
1008 (move-to-column (+ indent ncols) t))
1009 (point))
1010 (progn (move-to-column indent) (point)))
1011 ;; Need to add some
1012 (goto-char beg)
1013 (indent-to (+ indent ncols))
1014 ;; If point was at beginning of text, keep it there.
1015 (if (= beg pos)
1016 (move-marker pos (point)))))
1017
1018 ((eq 'center how)
1019 ;; Figure out how much indentation is needed
1020 (setq ncols (+ (current-left-margin)
1021 (/ (- fc (current-left-margin) ;avail. space
1022 (- endcol indent)) ;text width
1023 2)))
1024 (if (< ncols indent)
1025 ;; Have too much indentation - remove some
1026 (delete-region
1027 (progn (goto-char fp-end)
1028 (if (< (current-column) ncols)
1029 (move-to-column ncols t))
1030 (point))
1031 (progn (move-to-column indent) (point)))
1032 ;; Have too little - add some
1033 (goto-char beg)
1034 (indent-to ncols)
1035 ;; If point was at beginning of text, keep it there.
1036 (if (= beg pos)
1037 (move-marker pos (point)))))
1038
1039 ((eq 'full how)
1040 ;; Insert extra spaces between words to justify line
1041 (save-restriction
1042 (narrow-to-region beg end)
1043 (or nosqueeze
1044 (canonically-space-region beg end))
1045 (goto-char (point-max))
1046 ;; count word spaces in line
1047 (while (search-backward " " nil t)
1048 (setq nspaces (1+ nspaces))
1049 (skip-chars-backward " "))
1050 (setq ncols (- fc endcol))
1051 ;; Ncols is number of additional space chars needed
1052 (if (and (> ncols 0) (> nspaces 0) (not eop))
1053 (progn
1054 (setq curr-fracspace (+ ncols (/ (1+ nspaces) 2))
1055 count nspaces)
1056 (while (> count 0)
1057 (skip-chars-forward " ")
1058 (insert-and-inherit
1059 (make-string (/ curr-fracspace nspaces) ?\ ))
1060 (search-forward " " nil t)
1061 (setq count (1- count)
1062 curr-fracspace
1063 (+ (% curr-fracspace nspaces) ncols)))))))
1064 (t (error "Unknown justification value"))))
1065 (goto-char pos)
1066 (move-marker pos nil)))
1067 nil)
1068
1069 (defun unjustify-current-line ()
1070 "Remove justification whitespace from current line.
1071 If the line is centered or right-justified, this function removes any
1072 indentation past the left margin. If the line is full-justified, it removes
1073 extra spaces between words. It does nothing in other justification modes."
1074 (let ((justify (current-justification)))
1075 (cond ((eq 'left justify) nil)
1076 ((eq nil justify) nil)
1077 ((eq 'full justify) ; full justify: remove extra spaces
1078 (beginning-of-line-text)
1079 (canonically-space-region (point) (line-end-position)))
1080 ((memq justify '(center right))
1081 (save-excursion
1082 (move-to-left-margin nil t)
1083 ;; Position ourselves after any fill-prefix.
1084 (if (and fill-prefix
1085 (not (string-equal fill-prefix ""))
1086 (equal fill-prefix
1087 (buffer-substring
1088 (point) (min (point-max) (+ (length fill-prefix)
1089 (point))))))
1090 (forward-char (length fill-prefix)))
1091 (delete-region (point) (progn (skip-chars-forward " \t")
1092 (point))))))))
1093
1094 (defun unjustify-region (&optional begin end)
1095 "Remove justification whitespace from region.
1096 For centered or right-justified regions, this function removes any indentation
1097 past the left margin from each line. For full-justified lines, it removes
1098 extra spaces between words. It does nothing in other justification modes.
1099 Arguments BEGIN and END are optional; default is the whole buffer."
1100 (save-excursion
1101 (save-restriction
1102 (if end (narrow-to-region (point-min) end))
1103 (goto-char (or begin (point-min)))
1104 (while (not (eobp))
1105 (unjustify-current-line)
1106 (forward-line 1)))))
1107
1108 \f
1109 (defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
1110 "Fill paragraphs within the region, allowing varying indentation within each.
1111 This command divides the region into \"paragraphs\",
1112 only at paragraph-separator lines, then fills each paragraph
1113 using as the fill prefix the smallest indentation of any line
1114 in the paragraph.
1115
1116 When calling from a program, pass range to fill as first two arguments.
1117
1118 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1119 JUSTIFY to justify paragraphs (prefix arg),
1120 When filling a mail message, pass a regexp for CITATION-REGEXP
1121 which will match the prefix of a line which is a citation marker
1122 plus whitespace, but no other kind of prefix.
1123 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1124 (interactive (progn
1125 (barf-if-buffer-read-only)
1126 (list (region-beginning) (region-end)
1127 (if current-prefix-arg 'full))))
1128 (let ((fill-individual-varying-indent t))
1129 (fill-individual-paragraphs min max justifyp citation-regexp)))
1130
1131 (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
1132 "Fill paragraphs of uniform indentation within the region.
1133 This command divides the region into \"paragraphs\",
1134 treating every change in indentation level or prefix as a paragraph boundary,
1135 then fills each paragraph using its indentation level as the fill prefix.
1136
1137 There is one special case where a change in indentation does not start
1138 a new paragraph. This is for text of this form:
1139
1140 foo> This line with extra indentation starts
1141 foo> a paragraph that continues on more lines.
1142
1143 These lines are filled together.
1144
1145 When calling from a program, pass the range to fill
1146 as the first two arguments.
1147
1148 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
1149 JUSTIFY to justify paragraphs (prefix arg),
1150 When filling a mail message, pass a regexp for CITATION-REGEXP
1151 which will match the prefix of a line which is a citation marker
1152 plus whitespace, but no other kind of prefix.
1153 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1154 (interactive (progn
1155 (barf-if-buffer-read-only)
1156 (list (region-beginning) (region-end)
1157 (if current-prefix-arg 'full))))
1158 (save-restriction
1159 (save-excursion
1160 (goto-char min)
1161 (beginning-of-line)
1162 (narrow-to-region (point) max)
1163 (if citation-regexp
1164 (while (and (not (eobp))
1165 (or (looking-at "[ \t]*[^ \t\n]+:")
1166 (looking-at "[ \t]*$")))
1167 (if (looking-at "[ \t]*[^ \t\n]+:")
1168 (search-forward "\n\n" nil 'move)
1169 (forward-line 1))))
1170 (narrow-to-region (point) max)
1171 ;; Loop over paragraphs.
1172 (while (let ((here (point)))
1173 ;; Skip over all paragraph-separating lines
1174 ;; so as to not include them in any paragraph.
1175 (while (and (not (eobp))
1176 (progn (move-to-left-margin)
1177 (and (not (eobp))
1178 (looking-at paragraph-separate))))
1179 (forward-line 1))
1180 (skip-chars-forward " \t\n") (not (eobp)))
1181 (move-to-left-margin)
1182 (let ((start (point))
1183 fill-prefix fill-prefix-regexp)
1184 ;; Find end of paragraph, and compute the smallest fill-prefix
1185 ;; that fits all the lines in this paragraph.
1186 (while (progn
1187 ;; Update the fill-prefix on the first line
1188 ;; and whenever the prefix good so far is too long.
1189 (if (not (and fill-prefix
1190 (looking-at fill-prefix-regexp)))
1191 (setq fill-prefix
1192 (fill-individual-paragraphs-prefix
1193 citation-regexp)
1194 fill-prefix-regexp (regexp-quote fill-prefix)))
1195 (forward-line 1)
1196 (if (bolp)
1197 ;; If forward-line went past a newline,
1198 ;; move further to the left margin.
1199 (move-to-left-margin))
1200 ;; Now stop the loop if end of paragraph.
1201 (and (not (eobp))
1202 (if fill-individual-varying-indent
1203 ;; If this line is a separator line, with or
1204 ;; without prefix, end the paragraph.
1205 (and
1206 (not (looking-at paragraph-separate))
1207 (save-excursion
1208 (not (and (looking-at fill-prefix-regexp)
1209 (progn (forward-char
1210 (length fill-prefix))
1211 (looking-at
1212 paragraph-separate))))))
1213 ;; If this line has more or less indent
1214 ;; than the fill prefix wants, end the paragraph.
1215 (and (looking-at fill-prefix-regexp)
1216 ;; If fill prefix is shorter than a new
1217 ;; fill prefix computed here, end paragraph.
1218 (let ((this-line-fill-prefix
1219 (fill-individual-paragraphs-prefix
1220 citation-regexp)))
1221 (>= (length fill-prefix)
1222 (length this-line-fill-prefix)))
1223 (save-excursion
1224 (not (progn (forward-char
1225 (length fill-prefix))
1226 (or (looking-at "[ \t]")
1227 (looking-at paragraph-separate)
1228 (looking-at paragraph-start)))))
1229 (not (and (equal fill-prefix "")
1230 citation-regexp
1231 (looking-at citation-regexp))))))))
1232 ;; Fill this paragraph, but don't add a newline at the end.
1233 (let ((had-newline (bolp)))
1234 (fill-region-as-paragraph start (point) justify)
1235 (if (and (bolp) (not had-newline))
1236 (delete-char -1))))))))
1237 (defun fill-individual-paragraphs-prefix (citation-regexp)
1238 (let* ((adaptive-fill-first-line-regexp ".*")
1239 (just-one-line-prefix
1240 ;; Accept any prefix rather than just the ones matched by
1241 ;; adaptive-fill-first-line-regexp.
1242 (fill-context-prefix (point) (line-beginning-position 2)))
1243 (two-lines-prefix
1244 (fill-context-prefix (point) (line-beginning-position 3))))
1245 (if (not just-one-line-prefix)
1246 (buffer-substring
1247 (point) (save-excursion (skip-chars-forward " \t") (point)))
1248 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1249 ;; is the same as that of TWO-LINES-PREFIX,
1250 ;; except perhaps with longer whitespace.
1251 (if (and just-one-line-prefix two-lines-prefix
1252 (let* ((one-line-citation-part
1253 (fill-individual-paragraphs-citation
1254 just-one-line-prefix citation-regexp))
1255 (two-lines-citation-part
1256 (fill-individual-paragraphs-citation
1257 two-lines-prefix citation-regexp))
1258 (adjusted-two-lines-citation-part
1259 (substring two-lines-citation-part 0
1260 (string-match "[ \t]*\\'"
1261 two-lines-citation-part))))
1262 (and
1263 (string-match (concat "\\`"
1264 (regexp-quote
1265 adjusted-two-lines-citation-part)
1266 "[ \t]*\\'")
1267 one-line-citation-part)
1268 (>= (string-width one-line-citation-part)
1269 (string-width two-lines-citation-part)))))
1270 two-lines-prefix
1271 just-one-line-prefix))))
1272
1273 (defun fill-individual-paragraphs-citation (string citation-regexp)
1274 (if citation-regexp
1275 (if (string-match citation-regexp string)
1276 (match-string 0 string)
1277 "")
1278 string))
1279
1280 ;;; fill.el ends here