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