]> code.delx.au - gnu-emacs/blob - lisp/textmodes/fill.el
(dabbrev--search): Don't downcase the result here.
[gnu-emacs] / lisp / textmodes / fill.el
1 ;;; fill.el --- fill commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 1996 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 (defconst 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
38 (defconst sentence-end-double-space t
39 "*Non-nil means a single space does not end a sentence.")
40
41 (defconst colon-double-space nil
42 "*Non-nil means put two spaces after a colon when filling.")
43
44 (defvar fill-paragraph-function nil
45 "Mode-specific function to fill a paragraph, or nil if there is none.
46 If the function returns nil, then `fill-paragraph' does its normal work.")
47
48 (defun set-fill-prefix ()
49 "Set the fill prefix to the current line up to point.
50 Filling expects lines to start with the fill prefix and
51 reinserts the fill prefix in each resulting line."
52 (interactive)
53 (setq fill-prefix (buffer-substring
54 (save-excursion (move-to-left-margin) (point))
55 (point)))
56 (if (equal fill-prefix "")
57 (setq fill-prefix nil))
58 (if fill-prefix
59 (message "fill-prefix: \"%s\"" fill-prefix)
60 (message "fill-prefix cancelled")))
61
62 (defconst adaptive-fill-mode t
63 "*Non-nil means determine a paragraph's fill prefix from its text.")
64
65 (defconst adaptive-fill-regexp "[ \t]*\\([#;>*]+ +\\)?"
66 "*Regexp to match text at start of line that constitutes indentation.
67 If Adaptive Fill mode is enabled, whatever text matches this pattern
68 on the second line of a paragraph is used as the standard indentation
69 for the paragraph. If the paragraph has just one line, the indentation
70 is taken from that line.")
71
72 (defvar adaptive-fill-function nil
73 "*Function to call to choose a fill prefix for a paragraph.
74 This function is used when `adaptive-fill-regexp' does not match.")
75
76 (defun current-fill-column ()
77 "Return the fill-column to use for this line.
78 The fill-column to use for a buffer is stored in the variable `fill-column',
79 but can be locally modified by the `right-margin' text property, which is
80 subtracted from `fill-column'.
81
82 The fill column to use for a line is the first column at which the column
83 number equals or exceeds the local fill-column - right-margin difference."
84 (save-excursion
85 (if fill-column
86 (let* ((here (progn (beginning-of-line) (point)))
87 (here-col 0)
88 (eol (progn (end-of-line) (point)))
89 margin fill-col change col)
90 ;; Look separately at each region of line with a different right-margin.
91 (while (and (setq margin (get-text-property here 'right-margin)
92 fill-col (- fill-column (or margin 0))
93 change (text-property-not-all
94 here eol 'right-margin margin))
95 (progn (goto-char (1- change))
96 (setq col (current-column))
97 (< col fill-col)))
98 (setq here change
99 here-col col))
100 (max here-col fill-col)))))
101
102 (defun canonically-space-region (beg end)
103 "Remove extra spaces between words in region.
104 Leave one space between words, two at end of sentences or after colons
105 (depending on values of `sentence-end-double-space' and `colon-double-space').
106 Remove indentation from each line."
107 (interactive "r")
108 (save-excursion
109 (goto-char beg)
110 ;; Nuke tabs; they get screwed up in a fill.
111 ;; This is quick, but loses when a tab follows the end of a sentence.
112 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
113 ;; Blame the typist.
114 (subst-char-in-region beg end ?\t ?\ )
115 (while (and (< (point) end)
116 (re-search-forward " *" end t))
117 (delete-region
118 (+ (match-beginning 0)
119 ;; Determine number of spaces to leave:
120 (save-excursion
121 (skip-chars-backward " ]})\"'")
122 (cond ((and sentence-end-double-space
123 (memq (preceding-char) '(?. ?? ?!))) 2)
124 ((and colon-double-space
125 (= (preceding-char) ?:)) 2)
126 ((char-equal (preceding-char) ?\n) 0)
127 (t 1))))
128 (match-end 0)))
129 ;; Make sure sentences ending at end of line get an extra space.
130 ;; loses on split abbrevs ("Mr.\nSmith")
131 (goto-char beg)
132 (while (and (< (point) end)
133 (re-search-forward "[.?!][])}\"']*$" end t))
134 ;; We insert before markers in case a caller such as
135 ;; do-auto-fill has done a save-excursion with point at the end
136 ;; of the line and wants it to stay at the end of the line.
137 (insert-before-markers-and-inherit ? ))))
138
139 (defun fill-context-prefix (from to &optional first-line-regexp)
140 "Compute a fill prefix from the text between FROM and TO.
141 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
142 If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
143 first line, insist it must match FIRST-LINE-REGEXP."
144 (save-excursion
145 (goto-char from)
146 (if (eolp) (forward-line 1))
147 ;; Move to the second line unless there is just one.
148 (let ((firstline (point))
149 ;; Non-nil if we are on the second line.
150 at-second
151 result)
152 (forward-line 1)
153 (if (>= (point) to)
154 (goto-char firstline)
155 (setq at-second t))
156 (move-to-left-margin)
157 (let ((start (point)))
158 (setq result
159 (if (not (looking-at paragraph-start))
160 (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
161 (buffer-substring-no-properties start (match-end 0)))
162 (adaptive-fill-function (funcall adaptive-fill-function)))))
163 (and result
164 (or at-second
165 (null first-line-regexp)
166 (string-match first-line-regexp result))
167 result)))))
168
169 (defun fill-region-as-paragraph (from to &optional justify
170 nosqueeze squeeze-after)
171 "Fill the region as one paragraph.
172 It removes any paragraph breaks in the region and extra newlines at the end,
173 indents and fills lines between the margins given by the
174 `current-left-margin' and `current-fill-column' functions.
175 It leaves point at the beginning of the line following the paragraph.
176
177 Normally performs justification according to the `current-justification'
178 function, but with a prefix arg, does full justification instead.
179
180 From a program, optional third arg JUSTIFY can specify any type of
181 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
182 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
183 means don't canonicalize spaces before that position.
184
185 If `sentence-end-double-space' is non-nil, then period followed by one
186 space does not end a sentence, so don't break a line there."
187 (interactive (list (region-beginning) (region-end)
188 (if current-prefix-arg 'full)))
189 ;; Arrange for undoing the fill to restore point.
190 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
191 (setq buffer-undo-list (cons (point) buffer-undo-list)))
192
193 ;; Make sure "to" is the endpoint.
194 (goto-char (min from to))
195 (setq to (max from to))
196 ;; Ignore blank lines at beginning of region.
197 (skip-chars-forward " \t\n")
198
199 (let ((from-plus-indent (point))
200 (oneleft nil))
201
202 (beginning-of-line)
203 (setq from (point))
204
205 ;; Delete all but one soft newline at end of region.
206 ;; And leave TO before that one.
207 (goto-char to)
208 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
209 (if (and oneleft
210 (not (and use-hard-newlines
211 (get-text-property (1- (point)) 'hard))))
212 (delete-backward-char 1)
213 (backward-char 1)
214 (setq oneleft t)))
215 (setq to (point))
216
217 ;; If there was no newline, and there is text in the paragraph, then
218 ;; create a newline.
219 (if (and (not oneleft) (> to from-plus-indent))
220 (newline))
221 (goto-char from-plus-indent))
222
223 (if (not (> to (point)))
224 nil ; There is no paragraph, only whitespace: exit now.
225
226 (or justify (setq justify (current-justification)))
227
228 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
229 (let ((fill-prefix fill-prefix))
230 ;; Figure out how this paragraph is indented, if desired.
231 (if (and adaptive-fill-mode
232 (or (null fill-prefix) (string= fill-prefix "")))
233 (setq fill-prefix (fill-context-prefix from to)))
234
235 (save-restriction
236 (goto-char from)
237 (beginning-of-line)
238 (narrow-to-region (point) to)
239
240 (if (not justify) ; filling disabled: just check indentation
241 (progn
242 (goto-char from)
243 (while (not (eobp))
244 (if (and (not (eolp))
245 (< (current-indentation) (current-left-margin)))
246 (indent-to-left-margin))
247 (forward-line 1)))
248
249 (if use-hard-newlines
250 (remove-text-properties from (point-max) '(hard nil)))
251 ;; Make sure first line is indented (at least) to left margin...
252 (if (or (memq justify '(right center))
253 (< (current-indentation) (current-left-margin)))
254 (indent-to-left-margin))
255 ;; Delete the fill prefix from every line except the first.
256 ;; The first line may not even have a fill prefix.
257 (goto-char from)
258 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
259 (concat "[ \t]*"
260 (regexp-quote fill-prefix)
261 "[ \t]*"))))
262 (and fpre
263 (progn
264 (if (>= (+ (current-left-margin) (length fill-prefix))
265 (current-fill-column))
266 (error "fill-prefix too long for specified width"))
267 (goto-char from)
268 (forward-line 1)
269 (while (not (eobp))
270 (if (looking-at fpre)
271 (delete-region (point) (match-end 0)))
272 (forward-line 1))
273 (goto-char from)
274 (if (looking-at fpre)
275 (goto-char (match-end 0)))
276 (setq from (point)))))
277 ;; Remove indentation from lines other than the first.
278 (beginning-of-line 2)
279 (indent-region (point) (point-max) 0)
280 (goto-char from)
281
282 ;; FROM, and point, are now before the text to fill,
283 ;; but after any fill prefix on the first line.
284
285 ;; Make sure sentences ending at end of line get an extra space.
286 ;; loses on split abbrevs ("Mr.\nSmith")
287 (while (re-search-forward "[.?!][])}\"']*$" nil t)
288 (or (eobp) (insert-and-inherit ?\ )))
289 (goto-char from)
290 (skip-chars-forward " \t")
291 ;; Then change all newlines to spaces.
292 (subst-char-in-region from (point-max) ?\n ?\ )
293 (if (and nosqueeze (not (eq justify 'full)))
294 nil
295 (canonically-space-region (or squeeze-after (point)) (point-max))
296 (goto-char (point-max))
297 (delete-horizontal-space)
298 (insert-and-inherit " "))
299 (goto-char (point-min))
300
301 ;; This is the actual filling loop.
302 (let ((prefixcol 0) linebeg)
303 (while (not (eobp))
304 (setq linebeg (point))
305 (move-to-column (1+ (current-fill-column)))
306 (if (eobp)
307 (or nosqueeze (delete-horizontal-space))
308 ;; Move back to start of word.
309 (skip-chars-backward "^ \n" linebeg)
310 ;; Don't break after a period followed by just one space.
311 ;; Move back to the previous place to break.
312 ;; The reason is that if a period ends up at the end of a line,
313 ;; further fills will assume it ends a sentence.
314 ;; If we now know it does not end a sentence,
315 ;; avoid putting it at the end of the line.
316 (if sentence-end-double-space
317 (while (and (> (point) (+ linebeg 2))
318 (eq (preceding-char) ?\ )
319 (not (eq (following-char) ?\ ))
320 (eq (char-after (- (point) 2)) ?\.))
321 (forward-char -2)
322 (skip-chars-backward "^ \n" linebeg)))
323 ;; If the left margin and fill prefix by themselves
324 ;; pass the fill-column. or if they are zero
325 ;; but we have no room for even one word,
326 ;; keep at least one word anyway.
327 ;; This handles ALL BUT the first line of the paragraph.
328 (if (if (zerop prefixcol)
329 (save-excursion
330 (skip-chars-backward " \t" linebeg)
331 (bolp))
332 (>= prefixcol (current-column)))
333 ;; Ok, skip at least one word.
334 ;; Meanwhile, don't stop at a period followed by one space.
335 (let ((first t))
336 (move-to-column prefixcol)
337 (while (and (not (eobp))
338 (or first
339 (and (not (bobp))
340 sentence-end-double-space
341 (save-excursion (forward-char -1)
342 (and (looking-at "\\. ")
343 (not (looking-at "\\. ")))))))
344 (skip-chars-forward " \t")
345 (skip-chars-forward "^ \n\t")
346 (setq first nil)))
347 ;; Normally, move back over the single space between the words.
348 (forward-char -1))
349 ;; If the left margin and fill prefix by themselves
350 ;; pass the fill-column, keep at least one word.
351 ;; This handles the first line of the paragraph.
352 (if (and (zerop prefixcol)
353 (let ((fill-point (point)) nchars)
354 (save-excursion
355 (move-to-left-margin)
356 (setq nchars (- fill-point (point)))
357 (or (< nchars 0)
358 (and fill-prefix
359 (< nchars (length fill-prefix))
360 (string= (buffer-substring (point) fill-point)
361 (substring fill-prefix 0 nchars)))))))
362 ;; Ok, skip at least one word. But
363 ;; don't stop at a period followed by just one space.
364 (let ((first t))
365 (while (and (not (eobp))
366 (or first
367 (and (not (bobp))
368 sentence-end-double-space
369 (save-excursion (forward-char -1)
370 (and (looking-at "\\. ")
371 (not (looking-at "\\. ")))))))
372 (skip-chars-forward " \t")
373 (skip-chars-forward "^ \t\n")
374 (setq first nil))))
375 ;; Check again to see if we got to the end of the paragraph.
376 (if (save-excursion (skip-chars-forward " \t") (eobp))
377 (or nosqueeze (delete-horizontal-space))
378 ;; Replace whitespace here with one newline, then indent to left
379 ;; margin.
380 (skip-chars-backward " \t")
381 (insert ?\n)
382 ;; Give newline the properties of the space(s) it replaces
383 (set-text-properties (1- (point)) (point)
384 (text-properties-at (point)))
385 (indent-to-left-margin)
386 ;; Insert the fill prefix after indentation.
387 ;; Set prefixcol so whitespace in the prefix won't get lost.
388 (and fill-prefix (not (equal fill-prefix ""))
389 (progn
390 (insert-and-inherit fill-prefix)
391 (setq prefixcol (current-column))))))
392 ;; Justify the line just ended, if desired.
393 (if justify
394 (if (eobp)
395 (justify-current-line justify t t)
396 (forward-line -1)
397 (justify-current-line justify nil t)
398 (forward-line 1))))))
399 ;; Leave point after final newline.
400 (goto-char (point-max)))
401 (forward-char 1))))
402
403 (defun fill-paragraph (arg)
404 "Fill paragraph at or after point. Prefix arg means justify as well.
405 If `sentence-end-double-space' is non-nil, then period followed by one
406 space does not end a sentence, so don't break a line there.
407
408 If `fill-paragraph-function' is non-nil, we call it (passing our
409 argument to it), and if it returns non-nil, we simply return its value."
410 (interactive (list (if current-prefix-arg 'full)))
411 (or (and fill-paragraph-function
412 (let ((function fill-paragraph-function)
413 fill-paragraph-function)
414 (funcall function arg)))
415 (let ((before (point)))
416 (save-excursion
417 (forward-paragraph)
418 (or (bolp) (newline 1))
419 (let ((end (point))
420 (beg (progn (backward-paragraph) (point))))
421 (goto-char before)
422 (if use-hard-newlines
423 ;; Can't use fill-region-as-paragraph, since this paragraph may
424 ;; still contain hard newlines. See fill-region.
425 (fill-region beg end arg)
426 (fill-region-as-paragraph beg end arg)))))))
427
428 (defun fill-region (from to &optional justify nosqueeze to-eop)
429 "Fill each of the paragraphs in the region.
430 Prefix arg (non-nil third arg, if called from program) means justify as well.
431
432 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
433 whitespace other than line breaks untouched, and fifth arg TO-EOP
434 non-nil means to keep filling to the end of the paragraph (or next
435 hard newline, if `use-hard-newlines' is on).
436
437 If `sentence-end-double-space' is non-nil, then period followed by one
438 space does not end a sentence, so don't break a line there."
439 (interactive (list (region-beginning) (region-end)
440 (if current-prefix-arg 'full)))
441 (let (end beg)
442 (save-restriction
443 (goto-char (max from to))
444 (if to-eop
445 (progn (skip-chars-backward "\n")
446 (forward-paragraph)))
447 (setq end (point))
448 (goto-char (setq beg (min from to)))
449 (beginning-of-line)
450 (narrow-to-region (point) end)
451 (while (not (eobp))
452 (let ((initial (point))
453 end)
454 ;; If using hard newlines, break at every one for filling
455 ;; purposes rather than using paragraph breaks.
456 (if use-hard-newlines
457 (progn
458 (while (and (setq end (text-property-any (point) (point-max)
459 'hard t))
460 (not (= ?\n (char-after end)))
461 (not (= end (point-max))))
462 (goto-char (1+ end)))
463 (setq end (if end (min (point-max) (1+ end)) (point-max)))
464 (goto-char initial))
465 (forward-paragraph 1)
466 (setq end (point))
467 (forward-paragraph -1))
468 (if (< (point) beg)
469 (goto-char beg))
470 (if (>= (point) initial)
471 (fill-region-as-paragraph (point) end justify nosqueeze)
472 (goto-char end)))))))
473
474 \f
475 (defconst default-justification 'left
476 "*Method of justifying text not otherwise specified.
477 Possible values are `left', `right', `full', `center', or `none'.
478 The requested kind of justification is done whenever lines are filled.
479 The `justification' text-property can locally override this variable.
480 This variable automatically becomes buffer-local when set in any fashion.")
481 (make-variable-buffer-local 'default-justification)
482
483 (defun current-justification ()
484 "How should we justify this line?
485 This returns the value of the text-property `justification',
486 or the variable `default-justification' if there is no text-property.
487 However, it returns nil rather than `none' to mean \"don't justify\"."
488 (let ((j (or (get-text-property
489 ;; Make sure we're looking at paragraph body.
490 (save-excursion (skip-chars-forward " \t")
491 (if (and (eobp) (not (bobp)))
492 (1- (point)) (point)))
493 'justification)
494 default-justification)))
495 (if (eq 'none j)
496 nil
497 j)))
498
499 (defun set-justification (begin end value &optional whole-par)
500 "Set the region's justification style.
501 The kind of justification to use is prompted for.
502 If the mark is not active, this command operates on the current paragraph.
503 If the mark is active, the region is used. However, if the beginning and end
504 of the region are not at paragraph breaks, they are moved to the beginning and
505 end of the paragraphs they are in.
506 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
507 breaks.
508
509 When calling from a program, operates just on region between BEGIN and END,
510 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
511 extended to include entire paragraphs as in the interactive command."
512 (interactive (list (if mark-active (region-beginning) (point))
513 (if mark-active (region-end) (point))
514 (let ((s (completing-read
515 "Set justification to: "
516 '(("left") ("right") ("full")
517 ("center") ("none"))
518 nil t)))
519 (if (equal s "") (error ""))
520 (intern s))
521 t))
522 (save-excursion
523 (save-restriction
524 (if whole-par
525 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
526 (paragraph-ignore-fill-prefix (if use-hard-newlines t
527 paragraph-ignore-fill-prefix)))
528 (goto-char begin)
529 (while (and (bolp) (not (eobp))) (forward-char 1))
530 (backward-paragraph)
531 (setq begin (point))
532 (goto-char end)
533 (skip-chars-backward " \t\n" begin)
534 (forward-paragraph)
535 (setq end (point))))
536
537 (narrow-to-region (point-min) end)
538 (unjustify-region begin (point-max))
539 (put-text-property begin (point-max) 'justification value)
540 (fill-region begin (point-max) nil t))))
541
542 (defun set-justification-none (b e)
543 "Disable automatic filling for paragraphs in the region.
544 If the mark is not active, this applies to the current paragraph."
545 (interactive (list (if mark-active (region-beginning) (point))
546 (if mark-active (region-end) (point))))
547 (set-justification b e 'none t))
548
549 (defun set-justification-left (b e)
550 "Make paragraphs in the region left-justified.
551 This is usually the default, but see the variable `default-justification'.
552 If the mark is not active, this applies to the current paragraph."
553 (interactive (list (if mark-active (region-beginning) (point))
554 (if mark-active (region-end) (point))))
555 (set-justification b e 'left t))
556
557 (defun set-justification-right (b e)
558 "Make paragraphs in the region right-justified:
559 Flush at the right margin and ragged on the left.
560 If the mark is not active, this applies to the current paragraph."
561 (interactive (list (if mark-active (region-beginning) (point))
562 (if mark-active (region-end) (point))))
563 (set-justification b e 'right t))
564
565 (defun set-justification-full (b e)
566 "Make paragraphs in the region fully justified:
567 This makes lines flush on both margins by inserting spaces between words.
568 If the mark is not active, this applies to the current paragraph."
569 (interactive (list (if mark-active (region-beginning) (point))
570 (if mark-active (region-end) (point))))
571 (set-justification b e 'full t))
572
573 (defun set-justification-center (b e)
574 "Make paragraphs in the region centered.
575 If the mark is not active, this applies to the current paragraph."
576 (interactive (list (if mark-active (region-beginning) (point))
577 (if mark-active (region-end) (point))))
578 (set-justification b e 'center t))
579
580 ;; A line has up to six parts:
581 ;;
582 ;; >>> hello.
583 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
584 ;;
585 ;; "Indent-1" is the left-margin indentation; normally it ends at column
586 ;; given by the `current-left-margin' function.
587 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
588 ;; "Indent-2" is added to justify a line if the `current-justification' is
589 ;; `center' or `right'. In `left' and `full' justification regions, any
590 ;; whitespace there is part of the line's text, and should not be changed.
591 ;; Trailing whitespace is not counted as part of the line length when
592 ;; center- or right-justifying.
593 ;;
594 ;; All parts of the line are optional, although the final newline can
595 ;; only be missing on the last line of the buffer.
596
597 (defun justify-current-line (&optional how eop nosqueeze)
598 "Do some kind of justification on this line.
599 Normally does full justification: adds spaces to the line to make it end at
600 the column given by `current-fill-column'.
601 Optional first argument HOW specifies alternate type of justification:
602 it can be `left', `right', `full', `center', or `none'.
603 If HOW is t, will justify however the `current-justification' function says to.
604 If HOW is nil or missing, full justification is done by default.
605 Second arg EOP non-nil means that this is the last line of the paragraph, so
606 it will not be stretched by full justification.
607 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
608 otherwise it is made canonical."
609 (interactive)
610 (if (eq t how) (setq how (or (current-justification) 'none))
611 (if (null how) (setq how 'full)
612 (or (memq how '(none left right center))
613 (setq how 'full))))
614 (or (memq how '(none left)) ; No action required for these.
615 (let ((fc (current-fill-column))
616 (pos (point-marker))
617 fp-end ; point at end of fill prefix
618 beg ; point at beginning of line's text
619 end ; point at end of line's text
620 indent ; column of `beg'
621 endcol ; column of `end'
622 ncols) ; new indent point or offset
623 (end-of-line)
624 ;; Check if this is the last line of the paragraph.
625 (if (and use-hard-newlines (null eop)
626 (get-text-property (point) 'hard))
627 (setq eop t))
628 (skip-chars-backward " \t")
629 ;; Quick exit if it appears to be properly justified already
630 ;; or there is no text.
631 (if (or (bolp)
632 (and (memq how '(full right))
633 (= (current-column) fc)))
634 nil
635 (setq end (point))
636 (beginning-of-line)
637 (skip-chars-forward " \t")
638 ;; Skip over fill-prefix.
639 (if (and fill-prefix
640 (not (string-equal fill-prefix ""))
641 (equal fill-prefix
642 (buffer-substring
643 (point) (min (point-max) (+ (length fill-prefix)
644 (point))))))
645 (forward-char (length fill-prefix))
646 (if (and adaptive-fill-mode
647 (looking-at adaptive-fill-regexp))
648 (goto-char (match-end 0))))
649 (setq fp-end (point))
650 (skip-chars-forward " \t")
651 ;; This is beginning of the line's text.
652 (setq indent (current-column))
653 (setq beg (point))
654 (goto-char end)
655 (setq endcol (current-column))
656
657 ;; HOW can't be null or left--we would have exited already
658 (cond ((eq 'right how)
659 (setq ncols (- fc endcol))
660 (if (< ncols 0)
661 ;; Need to remove some indentation
662 (delete-region
663 (progn (goto-char fp-end)
664 (if (< (current-column) (+ indent ncols))
665 (move-to-column (+ indent ncols) t))
666 (point))
667 (progn (move-to-column indent) (point)))
668 ;; Need to add some
669 (goto-char beg)
670 (indent-to (+ indent ncols))
671 ;; If point was at beginning of text, keep it there.
672 (if (= beg pos)
673 (move-marker pos (point)))))
674
675 ((eq 'center how)
676 ;; Figure out how much indentation is needed
677 (setq ncols (+ (current-left-margin)
678 (/ (- fc (current-left-margin) ;avail. space
679 (- endcol indent)) ;text width
680 2)))
681 (if (< ncols indent)
682 ;; Have too much indentation - remove some
683 (delete-region
684 (progn (goto-char fp-end)
685 (if (< (current-column) ncols)
686 (move-to-column ncols t))
687 (point))
688 (progn (move-to-column indent) (point)))
689 ;; Have too little - add some
690 (goto-char beg)
691 (indent-to ncols)
692 ;; If point was at beginning of text, keep it there.
693 (if (= beg pos)
694 (move-marker pos (point)))))
695
696 ((eq 'full how)
697 ;; Insert extra spaces between words to justify line
698 (save-restriction
699 (narrow-to-region beg end)
700 (or nosqueeze
701 (canonically-space-region beg end))
702 (goto-char (point-max))
703 (setq ncols (- fc endcol))
704 ;; Ncols is number of additional spaces needed
705 (if (> ncols 0)
706 (if (and (not eop)
707 (search-backward " " nil t))
708 (while (> ncols 0)
709 (let ((nmove (+ 3 (random 3))))
710 (while (> nmove 0)
711 (or (search-backward " " nil t)
712 (progn
713 (goto-char (point-max))
714 (search-backward " ")))
715 (skip-chars-backward " ")
716 (setq nmove (1- nmove))))
717 (insert-and-inherit " ")
718 (skip-chars-backward " ")
719 (setq ncols (1- ncols)))))))
720 (t (error "Unknown justification value"))))
721 (goto-char pos)
722 (move-marker pos nil)))
723 nil)
724
725 (defun unjustify-current-line ()
726 "Remove justification whitespace from current line.
727 If the line is centered or right-justified, this function removes any
728 indentation past the left margin. If the line is full-justified, it removes
729 extra spaces between words. It does nothing in other justification modes."
730 (let ((justify (current-justification)))
731 (cond ((eq 'left justify) nil)
732 ((eq nil justify) nil)
733 ((eq 'full justify) ; full justify: remove extra spaces
734 (beginning-of-line-text)
735 (canonically-space-region
736 (point) (save-excursion (end-of-line) (point))))
737 ((memq justify '(center right))
738 (save-excursion
739 (move-to-left-margin nil t)
740 ;; Position ourselves after any fill-prefix.
741 (if (and fill-prefix
742 (not (string-equal fill-prefix ""))
743 (equal fill-prefix
744 (buffer-substring
745 (point) (min (point-max) (+ (length fill-prefix)
746 (point))))))
747 (forward-char (length fill-prefix)))
748 (delete-region (point) (progn (skip-chars-forward " \t")
749 (point))))))))
750
751 (defun unjustify-region (&optional begin end)
752 "Remove justification whitespace from region.
753 For centered or right-justified regions, this function removes any indentation
754 past the left margin from each line. For full-justified lines, it removes
755 extra spaces between words. It does nothing in other justification modes.
756 Arguments BEGIN and END are optional; default is the whole buffer."
757 (save-excursion
758 (save-restriction
759 (if end (narrow-to-region (point-min) end))
760 (goto-char (or begin (point-min)))
761 (while (not (eobp))
762 (unjustify-current-line)
763 (forward-line 1)))))
764
765 \f
766 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
767 "Fill paragraphs within the region, allowing varying indentation within each.
768 This command divides the region into \"paragraphs\",
769 only at paragraph-separator lines, then fills each paragraph
770 using as the fill prefix the smallest indentation of any line
771 in the paragraph.
772
773 When calling from a program, pass range to fill as first two arguments.
774
775 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
776 JUSTIFY to justify paragraphs (prefix arg),
777 MAIL-FLAG for a mail message, i. e. don't fill header lines."
778 (interactive (list (region-beginning) (region-end)
779 (if current-prefix-arg 'full)))
780 (let ((fill-individual-varying-indent t))
781 (fill-individual-paragraphs min max justifyp mailp)))
782
783 (defun fill-individual-paragraphs (min max &optional justify mailp)
784 "Fill paragraphs of uniform indentation within the region.
785 This command divides the region into \"paragraphs\",
786 treating every change in indentation level as a paragraph boundary,
787 then fills each paragraph using its indentation level as the fill prefix.
788
789 When calling from a program, pass range to fill as first two arguments.
790
791 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
792 JUSTIFY to justify paragraphs (prefix arg),
793 MAIL-FLAG for a mail message, i. e. don't fill header lines."
794 (interactive (list (region-beginning) (region-end)
795 (if current-prefix-arg 'full)))
796 (save-restriction
797 (save-excursion
798 (goto-char min)
799 (beginning-of-line)
800 (narrow-to-region (point) max)
801 (if mailp
802 (while (and (not (eobp))
803 (or (looking-at "[ \t]*[^ \t\n]+:")
804 (looking-at "[ \t]*$")))
805 (if (looking-at "[ \t]*[^ \t\n]+:")
806 (search-forward "\n\n" nil 'move)
807 (forward-line 1))))
808 (narrow-to-region (point) max)
809 ;; Loop over paragraphs.
810 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
811 (move-to-left-margin)
812 (let ((start (point))
813 fill-prefix fill-prefix-regexp)
814 ;; Find end of paragraph, and compute the smallest fill-prefix
815 ;; that fits all the lines in this paragraph.
816 (while (progn
817 ;; Update the fill-prefix on the first line
818 ;; and whenever the prefix good so far is too long.
819 (if (not (and fill-prefix
820 (looking-at fill-prefix-regexp)))
821 (setq fill-prefix
822 (if (and adaptive-fill-mode adaptive-fill-regexp
823 (looking-at adaptive-fill-regexp))
824 (match-string 0)
825 (buffer-substring
826 (point)
827 (save-excursion (skip-chars-forward " \t")
828 (point))))
829 fill-prefix-regexp (regexp-quote fill-prefix)))
830 (forward-line 1)
831 (if (bolp)
832 ;; If forward-line went past a newline,
833 ;; move further to the left margin.
834 (move-to-left-margin))
835 ;; Now stop the loop if end of paragraph.
836 (and (not (eobp))
837 (if fill-individual-varying-indent
838 ;; If this line is a separator line, with or
839 ;; without prefix, end the paragraph.
840 (and
841 (not (looking-at paragraph-separate))
842 (save-excursion
843 (not (and (looking-at fill-prefix-regexp)
844 (progn (forward-char (length fill-prefix))
845 (looking-at paragraph-separate))))))
846 ;; If this line has more or less indent
847 ;; than the fill prefix wants, end the paragraph.
848 (and (looking-at fill-prefix-regexp)
849 (save-excursion
850 (not (progn (forward-char (length fill-prefix))
851 (or (looking-at paragraph-separate)
852 (looking-at paragraph-start))))))))))
853 ;; Fill this paragraph, but don't add a newline at the end.
854 (let ((had-newline (bolp)))
855 (fill-region-as-paragraph start (point) justify)
856 (or had-newline (delete-char -1))))))))
857
858 ;;; fill.el ends here