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