]> code.delx.au - gnu-emacs/blob - lisp/textmodes/fill.el
(tmm-menubar-mouse): Add autoload cookie.
[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 ;; And leave TO before that one.
201 (goto-char to)
202 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
203 (if (and oneleft
204 (not (and use-hard-newlines
205 (get-text-property (1- (point)) 'hard))))
206 (delete-backward-char 1)
207 (backward-char 1)
208 (setq oneleft t)))
209 (setq to (point))
210
211 ;; If there was no newline, and there is text in the paragraph, then
212 ;; create a newline.
213 (if (and (not oneleft) (> to from-plus-indent))
214 (newline))
215 (goto-char from-plus-indent))
216
217 (if (not (> to (point)))
218 nil ; There is no paragraph, only whitespace: exit now.
219
220 (or justify (setq justify (current-justification)))
221
222 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
223 (let ((fill-prefix fill-prefix))
224 ;; Figure out how this paragraph is indented, if desired.
225 (if (and adaptive-fill-mode
226 (or (null fill-prefix) (string= fill-prefix "")))
227 (setq fill-prefix (fill-context-prefix from to)))
228
229 (save-restriction
230 (goto-char from)
231 (beginning-of-line)
232 (narrow-to-region (point) to)
233
234 (if (not justify) ; filling disabled: just check indentation
235 (progn
236 (goto-char from)
237 (while (not (eobp))
238 (if (and (not (eolp))
239 (< (current-indentation) (current-left-margin)))
240 (indent-to-left-margin))
241 (forward-line 1)))
242
243 (if use-hard-newlines
244 (remove-text-properties from (point-max) '(hard nil)))
245 ;; Make sure first line is indented (at least) to left margin...
246 (if (or (memq justify '(right center))
247 (< (current-indentation) (current-left-margin)))
248 (indent-to-left-margin))
249 ;; Delete the fill prefix from every line except the first.
250 ;; The first line may not even have a fill prefix.
251 (goto-char from)
252 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
253 (concat "[ \t]*"
254 (regexp-quote fill-prefix)
255 "[ \t]*"))))
256 (and fpre
257 (progn
258 (if (>= (+ (current-left-margin) (length fill-prefix))
259 (current-fill-column))
260 (error "fill-prefix too long for specified width"))
261 (goto-char from)
262 (forward-line 1)
263 (while (not (eobp))
264 (if (looking-at fpre)
265 (delete-region (point) (match-end 0)))
266 (forward-line 1))
267 (goto-char from)
268 (and (looking-at fpre) (goto-char (match-end 0)))
269 (setq from (point)))))
270 ;; Remove indentation from lines other than the first.
271 (beginning-of-line 2)
272 (indent-region (point) (point-max) 0)
273 (goto-char from)
274
275 ;; FROM, and point, are now before the text to fill,
276 ;; but after any fill prefix on the first line.
277
278 ;; Make sure sentences ending at end of line get an extra space.
279 ;; loses on split abbrevs ("Mr.\nSmith")
280 (while (re-search-forward "[.?!][])}\"']*$" nil t)
281 (or (eobp) (insert-and-inherit ?\ )))
282 (goto-char from)
283 (skip-chars-forward " \t")
284 ;; Then change all newlines to spaces.
285 (subst-char-in-region from (point-max) ?\n ?\ )
286 (if (and nosqueeze (not (eq justify 'full)))
287 nil
288 (canonically-space-region (point) (point-max))
289 (goto-char (point-max))
290 (delete-horizontal-space)
291 (insert-and-inherit " "))
292 (goto-char (point-min))
293
294 ;; This is the actual filling loop.
295 (let ((prefixcol 0) linebeg)
296 (while (not (eobp))
297 (setq linebeg (point))
298 (move-to-column (1+ (current-fill-column)))
299 (if (eobp)
300 (or nosqueeze (delete-horizontal-space))
301 ;; Move back to start of word.
302 (skip-chars-backward "^ \n" linebeg)
303 ;; Don't break after a period followed by just one space.
304 ;; Move back to the previous place to break.
305 ;; The reason is that if a period ends up at the end of a line,
306 ;; further fills will assume it ends a sentence.
307 ;; If we now know it does not end a sentence,
308 ;; avoid putting it at the end of the line.
309 (if sentence-end-double-space
310 (while (and (> (point) (+ linebeg 2))
311 (eq (preceding-char) ?\ )
312 (not (eq (following-char) ?\ ))
313 (eq (char-after (- (point) 2)) ?\.))
314 (forward-char -2)
315 (skip-chars-backward "^ \n" linebeg)))
316 ;; If the left margin and fill prefix by themselves
317 ;; pass the fill-column, keep at least one word.
318 ;; This handles ALL BUT the first line of the paragraph.
319 (if (if (zerop prefixcol)
320 (save-excursion
321 (skip-chars-backward " \t" linebeg)
322 (bolp))
323 (>= prefixcol (current-column)))
324 ;; Ok, skip at least one word.
325 ;; Meanwhile, don't stop at a period followed by one space.
326 (let ((first t))
327 (move-to-column prefixcol)
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 " \t")
336 (skip-chars-forward "^ \n\t")
337 (setq first nil)))
338 ;; Normally, move back over the single space between the words.
339 (forward-char -1))
340 ;; If the left margin and fill prefix by themselves
341 ;; pass the fill-column, keep at least one word.
342 ;; This handles the first line of the paragraph.
343 (if (and (zerop prefixcol)
344 (let ((fill-point (point)) nchars)
345 (save-excursion
346 (move-to-left-margin)
347 (setq nchars (- fill-point (point)))
348 (or (< nchars 0)
349 (and fill-prefix
350 (< nchars (length fill-prefix))
351 (string= (buffer-substring (point) fill-point)
352 (substring fill-prefix 0 nchars)))))))
353 ;; Ok, skip at least one word. But
354 ;; don't stop at a period followed by just one space.
355 (let ((first t))
356 (while (and (not (eobp))
357 (or first
358 (and (not (bobp))
359 sentence-end-double-space
360 (save-excursion (forward-char -1)
361 (and (looking-at "\\. ")
362 (not (looking-at "\\. ")))))))
363 (skip-chars-forward " \t")
364 (skip-chars-forward "^ \t\n")
365 (setq first nil))))
366 ;; Check again to see if we got to the end of the paragraph.
367 (if (eobp)
368 (or nosqueeze (delete-horizontal-space))
369 ;; Replace whitespace here with one newline, then indent to left
370 ;; margin.
371 (skip-chars-backward " \t")
372 (insert ?\n)
373 ;; Give newline the properties of the space(s) it replaces
374 (set-text-properties (1- (point)) (point)
375 (text-properties-at (point)))
376 (indent-to-left-margin)
377 ;; Insert the fill prefix after indentation.
378 ;; Set prefixcol so whitespace in the prefix won't get lost.
379 (and fill-prefix (not (equal fill-prefix ""))
380 (progn
381 (insert-and-inherit fill-prefix)
382 (setq prefixcol (current-column))))))
383 ;; Justify the line just ended, if desired.
384 (if justify
385 (if (eobp)
386 (justify-current-line justify t t)
387 (forward-line -1)
388 (justify-current-line justify nil t)
389 (forward-line 1))))))
390 ;; Leave point after final newline.
391 (goto-char (point-max)))
392 (forward-char 1))))
393
394 (defun fill-paragraph (arg)
395 "Fill paragraph at or after point. Prefix arg means justify as well.
396 If `sentence-end-double-space' is non-nil, then period followed by one
397 space does not end a sentence, so don't break a line there.
398
399 If `fill-paragraph-function' is non-nil, we call it (passing our
400 argument to it), and if it returns non-nil, we simply return its value."
401 (interactive (list (if current-prefix-arg 'full)))
402 (or (and fill-paragraph-function
403 (let ((function fill-paragraph-function)
404 fill-paragraph-function)
405 (funcall function arg)))
406 (let ((before (point)))
407 (save-excursion
408 (forward-paragraph)
409 (or (bolp) (newline 1))
410 (let ((end (point))
411 (beg (progn (backward-paragraph) (point))))
412 (goto-char before)
413 (if use-hard-newlines
414 ;; Can't use fill-region-as-paragraph, since this paragraph may
415 ;; still contain hard newlines. See fill-region.
416 (fill-region beg end arg)
417 (fill-region-as-paragraph beg end arg)))))))
418
419 (defun fill-region (from to &optional justify nosqueeze to-eop)
420 "Fill each of the paragraphs in the region.
421 Prefix arg (non-nil third arg, if called from program) means justify as well.
422
423 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
424 whitespace other than line breaks untouched, and fifth arg TO-EOP
425 non-nil means to keep filling to the end of the paragraph (or next
426 hard newline, if `use-hard-newlines' is on).
427
428 If `sentence-end-double-space' is non-nil, then period followed by one
429 space does not end a sentence, so don't break a line there."
430 (interactive (list (region-beginning) (region-end)
431 (if current-prefix-arg 'full)))
432 (let (end beg)
433 (save-restriction
434 (goto-char (max from to))
435 (if to-eop
436 (progn (skip-chars-backward "\n")
437 (forward-paragraph)))
438 (setq end (point))
439 (goto-char (setq beg (min from to)))
440 (beginning-of-line)
441 (narrow-to-region (point) end)
442 (while (not (eobp))
443 (let ((initial (point))
444 end)
445 ;; If using hard newlines, break at every one for filling
446 ;; purposes rather than using paragraph breaks.
447 (if use-hard-newlines
448 (progn
449 (while (and (setq end (text-property-any (point) (point-max)
450 'hard t))
451 (not (= ?\n (char-after end)))
452 (not (= end (point-max))))
453 (goto-char (1+ end)))
454 (setq end (if end (min (point-max) (1+ end)) (point-max)))
455 (goto-char initial))
456 (forward-paragraph 1)
457 (setq end (point))
458 (forward-paragraph -1))
459 (if (< (point) beg)
460 (goto-char beg))
461 (if (>= (point) initial)
462 (fill-region-as-paragraph (point) end justify nosqueeze)
463 (goto-char end)))))))
464
465 \f
466 (defconst default-justification 'left
467 "*Method of justifying text not otherwise specified.
468 Possible values are `left', `right', `full', `center', or `none'.
469 The requested kind of justification is done whenever lines are filled.
470 The `justification' text-property can locally override this variable.
471 This variable automatically becomes buffer-local when set in any fashion.")
472 (make-variable-buffer-local 'default-justification)
473
474 (defun current-justification ()
475 "How should we justify this line?
476 This returns the value of the text-property `justification',
477 or the variable `default-justification' if there is no text-property.
478 However, it returns nil rather than `none' to mean \"don't justify\"."
479 (let ((j (or (get-text-property
480 ;; Make sure we're looking at paragraph body.
481 (save-excursion (skip-chars-forward " \t")
482 (if (and (eobp) (not (bobp)))
483 (1- (point)) (point)))
484 'justification)
485 default-justification)))
486 (if (eq 'none j)
487 nil
488 j)))
489
490 (defun set-justification (begin end value &optional whole-par)
491 "Set the region's justification style.
492 The kind of justification to use is prompted for.
493 If the mark is not active, this command operates on the current paragraph.
494 If the mark is active, the region is used. However, if the beginning and end
495 of the region are not at paragraph breaks, they are moved to the beginning and
496 end of the paragraphs they are in.
497 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
498 breaks.
499
500 When calling from a program, operates just on region between BEGIN and END,
501 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
502 extended to include entire paragraphs as in the interactive command."
503 (interactive (list (if mark-active (region-beginning) (point))
504 (if mark-active (region-end) (point))
505 (let ((s (completing-read
506 "Set justification to: "
507 '(("left") ("right") ("full")
508 ("center") ("none"))
509 nil t)))
510 (if (equal s "") (error ""))
511 (intern s))
512 t))
513 (save-excursion
514 (save-restriction
515 (if whole-par
516 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
517 (paragraph-ignore-fill-prefix (if use-hard-newlines t
518 paragraph-ignore-fill-prefix)))
519 (goto-char begin)
520 (while (and (bolp) (not (eobp))) (forward-char 1))
521 (backward-paragraph)
522 (setq begin (point))
523 (goto-char end)
524 (skip-chars-backward " \t\n" begin)
525 (forward-paragraph)
526 (setq end (point))))
527
528 (narrow-to-region (point-min) end)
529 (unjustify-region begin (point-max))
530 (put-text-property begin (point-max) 'justification value)
531 (fill-region begin (point-max) nil t))))
532
533 (defun set-justification-none (b e)
534 "Disable automatic filling for paragraphs in the region.
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 'none t))
539
540 (defun set-justification-left (b e)
541 "Make paragraphs in the region left-justified.
542 This is usually the default, but see the variable `default-justification'.
543 If the mark is not active, this applies to the current paragraph."
544 (interactive (list (if mark-active (region-beginning) (point))
545 (if mark-active (region-end) (point))))
546 (set-justification b e 'left t))
547
548 (defun set-justification-right (b e)
549 "Make paragraphs in the region right-justified:
550 Flush at the right margin and ragged on the left.
551 If the mark is not active, this applies to the current paragraph."
552 (interactive (list (if mark-active (region-beginning) (point))
553 (if mark-active (region-end) (point))))
554 (set-justification b e 'right t))
555
556 (defun set-justification-full (b e)
557 "Make paragraphs in the region fully justified:
558 This makes lines flush on both margins by inserting spaces between words.
559 If the mark is not active, this applies to the current paragraph."
560 (interactive (list (if mark-active (region-beginning) (point))
561 (if mark-active (region-end) (point))))
562 (set-justification b e 'full t))
563
564 (defun set-justification-center (b e)
565 "Make paragraphs in the region centered.
566 If the mark is not active, this applies to the current paragraph."
567 (interactive (list (if mark-active (region-beginning) (point))
568 (if mark-active (region-end) (point))))
569 (set-justification b e 'center t))
570
571 ;; A line has up to six parts:
572 ;;
573 ;; >>> hello.
574 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
575 ;;
576 ;; "Indent-1" is the left-margin indentation; normally it ends at column
577 ;; given by the `current-left-margin' function.
578 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
579 ;; "Indent-2" is added to justify a line if the `current-justification' is
580 ;; `center' or `right'. In `left' and `full' justification regions, any
581 ;; whitespace there is part of the line's text, and should not be changed.
582 ;; Trailing whitespace is not counted as part of the line length when
583 ;; center- or right-justifying.
584 ;;
585 ;; All parts of the line are optional, although the final newline can
586 ;; only be missing on the last line of the buffer.
587
588 (defun justify-current-line (&optional how eop nosqueeze)
589 "Do some kind of justification on this line.
590 Normally does full justification: adds spaces to the line to make it end at
591 the column given by `current-fill-column'.
592 Optional first argument HOW specifies alternate type of justification:
593 it can be `left', `right', `full', `center', or `none'.
594 If HOW is t, will justify however the `current-justification' function says to.
595 If HOW is nil or missing, full justification is done by default.
596 Second arg EOP non-nil means that this is the last line of the paragraph, so
597 it will not be stretched by full justification.
598 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
599 otherwise it is made canonical."
600 (interactive)
601 (if (eq t how) (setq how (or (current-justification) 'none))
602 (if (null how) (setq how 'full)
603 (or (memq how '(none left right center))
604 (setq how 'full))))
605 (or (memq how '(none left)) ; No action required for these.
606 (let ((fc (current-fill-column))
607 (pos (point-marker))
608 fp-end ; point at end of fill prefix
609 beg ; point at beginning of line's text
610 end ; point at end of line's text
611 indent ; column of `beg'
612 endcol ; column of `end'
613 ncols) ; new indent point or offset
614 (end-of-line)
615 ;; Check if this is the last line of the paragraph.
616 (if (and use-hard-newlines (null eop)
617 (get-text-property (point) 'hard))
618 (setq eop t))
619 (skip-chars-backward " \t")
620 ;; Quick exit if it appears to be properly justified already
621 ;; or there is no text.
622 (if (or (bolp)
623 (and (memq how '(full right))
624 (= (current-column) fc)))
625 nil
626 (setq end (point))
627 (beginning-of-line)
628 (skip-chars-forward " \t")
629 ;; Skip over fill-prefix.
630 (if (and fill-prefix
631 (not (string-equal fill-prefix ""))
632 (equal fill-prefix
633 (buffer-substring
634 (point) (min (point-max) (+ (length fill-prefix)
635 (point))))))
636 (forward-char (length fill-prefix))
637 (if (and adaptive-fill-mode
638 (looking-at adaptive-fill-regexp))
639 (goto-char (match-end 0))))
640 (setq fp-end (point))
641 (skip-chars-forward " \t")
642 ;; This is beginning of the line's text.
643 (setq indent (current-column))
644 (setq beg (point))
645 (goto-char end)
646 (setq endcol (current-column))
647
648 ;; HOW can't be null or left--we would have exited already
649 (cond ((eq 'right how)
650 (setq ncols (- fc endcol))
651 (if (< ncols 0)
652 ;; Need to remove some indentation
653 (delete-region
654 (progn (goto-char fp-end)
655 (if (< (current-column) (+ indent ncols))
656 (move-to-column (+ indent ncols) t))
657 (point))
658 (progn (move-to-column indent) (point)))
659 ;; Need to add some
660 (goto-char beg)
661 (indent-to (+ indent ncols))
662 ;; If point was at beginning of text, keep it there.
663 (if (= beg pos)
664 (move-marker pos (point)))))
665
666 ((eq 'center how)
667 ;; Figure out how much indentation is needed
668 (setq ncols (+ (current-left-margin)
669 (/ (- fc (current-left-margin) ;avail. space
670 (- endcol indent)) ;text width
671 2)))
672 (if (< ncols indent)
673 ;; Have too much indentation - remove some
674 (delete-region
675 (progn (goto-char fp-end)
676 (if (< (current-column) ncols)
677 (move-to-column ncols t))
678 (point))
679 (progn (move-to-column indent) (point)))
680 ;; Have too little - add some
681 (goto-char beg)
682 (indent-to ncols)
683 ;; If point was at beginning of text, keep it there.
684 (if (= beg pos)
685 (move-marker pos (point)))))
686
687 ((eq 'full how)
688 ;; Insert extra spaces between words to justify line
689 (save-restriction
690 (narrow-to-region beg end)
691 (or nosqueeze
692 (canonically-space-region beg end))
693 (goto-char (point-max))
694 (setq ncols (- fc endcol))
695 ;; Ncols is number of additional spaces needed
696 (if (> ncols 0)
697 (if (and (not eop)
698 (search-backward " " nil t))
699 (while (> ncols 0)
700 (let ((nmove (+ 3 (random 3))))
701 (while (> nmove 0)
702 (or (search-backward " " nil t)
703 (progn
704 (goto-char (point-max))
705 (search-backward " ")))
706 (skip-chars-backward " ")
707 (setq nmove (1- nmove))))
708 (insert-and-inherit " ")
709 (skip-chars-backward " ")
710 (setq ncols (1- ncols)))))))
711 (t (error "Unknown justification value"))))
712 (goto-char pos)
713 (move-marker pos nil)))
714 nil)
715
716 (defun unjustify-current-line ()
717 "Remove justification whitespace from current line.
718 If the line is centered or right-justified, this function removes any
719 indentation past the left margin. If the line is full-justified, it removes
720 extra spaces between words. It does nothing in other justification modes."
721 (let ((justify (current-justification)))
722 (cond ((eq 'left justify) nil)
723 ((eq nil justify) nil)
724 ((eq 'full justify) ; full justify: remove extra spaces
725 (beginning-of-line-text)
726 (canonically-space-region
727 (point) (save-excursion (end-of-line) (point))))
728 ((memq justify '(center right))
729 (save-excursion
730 (move-to-left-margin nil t)
731 ;; Position ourselves after any fill-prefix.
732 (if (and fill-prefix
733 (not (string-equal fill-prefix ""))
734 (equal fill-prefix
735 (buffer-substring
736 (point) (min (point-max) (+ (length fill-prefix)
737 (point))))))
738 (forward-char (length fill-prefix)))
739 (delete-region (point) (progn (skip-chars-forward " \t")
740 (point))))))))
741
742 (defun unjustify-region (&optional begin end)
743 "Remove justification whitespace from region.
744 For centered or right-justified regions, this function removes any indentation
745 past the left margin from each line. For full-justified lines, it removes
746 extra spaces between words. It does nothing in other justification modes.
747 Arguments BEGIN and END are optional; default is the whole buffer."
748 (save-excursion
749 (save-restriction
750 (if end (narrow-to-region (point-min) end))
751 (goto-char (or begin (point-min)))
752 (while (not (eobp))
753 (unjustify-current-line)
754 (forward-line 1)))))
755
756 \f
757 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
758 "Fill paragraphs within the region, allowing varying indentation within each.
759 This command divides the region into \"paragraphs\",
760 only at paragraph-separator lines, then fills each paragraph
761 using as the fill prefix the smallest indentation of any line
762 in the paragraph.
763
764 When calling from a program, pass range to fill as first two arguments.
765
766 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
767 JUSTIFY to justify paragraphs (prefix arg),
768 MAIL-FLAG for a mail message, i. e. don't fill header lines."
769 (interactive (list (region-beginning) (region-end)
770 (if current-prefix-arg 'full)))
771 (let ((fill-individual-varying-indent t))
772 (fill-individual-paragraphs min max justifyp mailp)))
773
774 (defun fill-individual-paragraphs (min max &optional justify mailp)
775 "Fill paragraphs of uniform indentation within the region.
776 This command divides the region into \"paragraphs\",
777 treating every change in indentation level as a paragraph boundary,
778 then fills each paragraph using its indentation level as the fill prefix.
779
780 When calling from a program, pass range to fill as first two arguments.
781
782 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
783 JUSTIFY to justify paragraphs (prefix arg),
784 MAIL-FLAG for a mail message, i. e. don't fill header lines."
785 (interactive (list (region-beginning) (region-end)
786 (if current-prefix-arg 'full)))
787 (save-restriction
788 (save-excursion
789 (goto-char min)
790 (beginning-of-line)
791 (narrow-to-region (point) max)
792 (if mailp
793 (while (and (not (eobp))
794 (or (looking-at "[ \t]*[^ \t\n]+:")
795 (looking-at "[ \t]*$")))
796 (if (looking-at "[ \t]*[^ \t\n]+:")
797 (search-forward "\n\n" nil 'move)
798 (forward-line 1))))
799 (narrow-to-region (point) max)
800 ;; Loop over paragraphs.
801 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
802 (move-to-left-margin)
803 (let ((start (point))
804 fill-prefix fill-prefix-regexp)
805 ;; Find end of paragraph, and compute the smallest fill-prefix
806 ;; that fits all the lines in this paragraph.
807 (while (progn
808 ;; Update the fill-prefix on the first line
809 ;; and whenever the prefix good so far is too long.
810 (if (not (and fill-prefix
811 (looking-at fill-prefix-regexp)))
812 (setq fill-prefix
813 (if (and adaptive-fill-mode adaptive-fill-regexp
814 (looking-at adaptive-fill-regexp))
815 (match-string 0)
816 (buffer-substring
817 (point)
818 (save-excursion (skip-chars-forward " \t")
819 (point))))
820 fill-prefix-regexp (regexp-quote fill-prefix)))
821 (forward-line 1)
822 (move-to-left-margin)
823 ;; Now stop the loop if end of paragraph.
824 (and (not (eobp))
825 (if fill-individual-varying-indent
826 ;; If this line is a separator line, with or
827 ;; without prefix, end the paragraph.
828 (and
829 (not (looking-at paragraph-separate))
830 (save-excursion
831 (not (and (looking-at fill-prefix-regexp)
832 (progn (forward-char (length fill-prefix))
833 (looking-at paragraph-separate))))))
834 ;; If this line has more or less indent
835 ;; than the fill prefix wants, end the paragraph.
836 (and (looking-at fill-prefix-regexp)
837 (save-excursion
838 (not (progn (forward-char (length fill-prefix))
839 (or (looking-at paragraph-separate)
840 (looking-at paragraph-start))))))))))
841 ;; Fill this paragraph, but don't add a newline at the end.
842 (let ((had-newline (bolp)))
843 (fill-region-as-paragraph start (point) justify)
844 (or had-newline (delete-char -1))))))))
845
846 ;;; fill.el ends here