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