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