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