1 ;;; fill.el --- fill commands for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 1996 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; All the commands for filling text. These are documented in the Emacs
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."
40 (defcustom sentence-end-double-space t
41 "*Non-nil means a single space does not end a sentence."
45 (defcustom colon-double-space nil
46 "*Non-nil means put two spaces after a colon when filling."
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.")
55 "*Non-nil means do `kinsoku' processing.
56 See the document of `kinsoku' for more detail.")
58 (defun set-fill-prefix ()
59 "Set the fill prefix to the current line up to point.
60 Filling expects lines to start with the fill prefix and
61 reinserts the fill prefix in each resulting line."
63 (setq fill-prefix
(buffer-substring
64 (save-excursion (move-to-left-margin) (point))
66 (if (equal fill-prefix
"")
67 (setq fill-prefix nil
))
69 (message "fill-prefix: \"%s\"" fill-prefix
)
70 (message "fill-prefix cancelled")))
72 (defcustom adaptive-fill-mode t
73 "*Non-nil means determine a paragraph's fill prefix from its text."
77 (defcustom adaptive-fill-regexp
"[ \t]*\\([#;>*]+ +\\)?"
78 "*Regexp to match text at start of line that constitutes indentation.
79 If Adaptive Fill mode is enabled, whatever text matches this pattern
80 on the second line of a paragraph is used as the standard indentation
81 for the paragraph. If the paragraph has just one line, the indentation
82 is taken from that line."
86 (defcustom adaptive-fill-function nil
87 "*Function to call to choose a fill prefix for a paragraph.
88 This function is used when `adaptive-fill-regexp' does not match."
92 (defun current-fill-column ()
93 "Return the fill-column to use for this line.
94 The fill-column to use for a buffer is stored in the variable `fill-column',
95 but can be locally modified by the `right-margin' text property, which is
96 subtracted from `fill-column'.
98 The fill column to use for a line is the first column at which the column
99 number equals or exceeds the local fill-column - right-margin difference."
102 (let* ((here (progn (beginning-of-line) (point)))
104 (eol (progn (end-of-line) (point)))
105 margin fill-col change col
)
106 ;; Look separately at each region of line with a different right-margin.
107 (while (and (setq margin
(get-text-property here
'right-margin
)
108 fill-col
(- fill-column
(or margin
0))
109 change
(text-property-not-all
110 here eol
'right-margin margin
))
111 (progn (goto-char (1- change
))
112 (setq col
(current-column))
116 (max here-col fill-col
)))))
118 (defun canonically-space-region (beg end
)
119 "Remove extra spaces between words in region.
120 Leave one space between words, two at end of sentences or after colons
121 (depending on values of `sentence-end-double-space' and `colon-double-space').
122 Remove indentation from each line."
126 ;; Nuke tabs; they get screwed up in a fill.
127 ;; This is quick, but loses when a tab follows the end of a sentence.
128 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
130 (subst-char-in-region beg end ?
\t ?\
)
131 (while (and (< (point) end
)
132 (re-search-forward " *" end t
))
134 (+ (match-beginning 0)
135 ;; Determine number of spaces to leave:
137 (skip-chars-backward " ]})\"'")
138 (cond ((and sentence-end-double-space
139 (memq (preceding-char) '(?. ?? ?
!))) 2)
140 ((and colon-double-space
141 (= (preceding-char) ?
:)) 2)
142 ((char-equal (preceding-char) ?
\n) 0)
145 ;; Make sure sentences ending at end of line get an extra space.
146 ;; loses on split abbrevs ("Mr.\nSmith")
148 (while (and (< (point) end
)
149 (re-search-forward "[.?!][])}\"']*$" end t
))
150 ;; We insert before markers in case a caller such as
151 ;; do-auto-fill has done a save-excursion with point at the end
152 ;; of the line and wants it to stay at the end of the line.
153 (insert-before-markers-and-inherit ?
))))
155 (defun fill-context-prefix (from to
&optional first-line-regexp
)
156 "Compute a fill prefix from the text between FROM and TO.
157 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
158 If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
159 first line, insist it must match FIRST-LINE-REGEXP."
162 (if (eolp) (forward-line 1))
163 ;; Move to the second line unless there is just one.
164 (let ((firstline (point))
165 ;; Non-nil if we are on the second line.
170 (goto-char firstline
)
172 (move-to-left-margin)
173 (let ((start (point)))
175 (if (not (looking-at paragraph-start
))
176 (cond ((and adaptive-fill-regexp
(looking-at adaptive-fill-regexp
))
177 (buffer-substring-no-properties start
(match-end 0)))
178 (adaptive-fill-function (funcall adaptive-fill-function
)))))
181 (null first-line-regexp
)
182 (string-match first-line-regexp result
))
185 (defun fill-region-as-paragraph (from to
&optional justify
186 nosqueeze squeeze-after
)
187 "Fill the region as one paragraph.
188 It removes any paragraph breaks in the region and extra newlines at the end,
189 indents and fills lines between the margins given by the
190 `current-left-margin' and `current-fill-column' functions.
191 It leaves point at the beginning of the line following the paragraph.
193 Normally performs justification according to the `current-justification'
194 function, but with a prefix arg, does full justification instead.
196 From a program, optional third arg JUSTIFY can specify any type of
197 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
198 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
199 means don't canonicalize spaces before that position.
201 If `sentence-end-double-space' is non-nil, then period followed by one
202 space does not end a sentence, so don't break a line there."
203 (interactive (list (region-beginning) (region-end)
204 (if current-prefix-arg
'full
)))
205 ;; Arrange for undoing the fill to restore point.
206 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
207 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
209 ;; Make sure "to" is the endpoint.
210 (goto-char (min from to
))
211 (setq to
(max from to
))
212 ;; Ignore blank lines at beginning of region.
213 (skip-chars-forward " \t\n")
215 (let ((from-plus-indent (point))
221 ;; Delete all but one soft newline at end of region.
222 ;; And leave TO before that one.
224 (while (and (> (point) from
) (eq ?
\n (char-after (1- (point)))))
226 (not (and use-hard-newlines
227 (get-text-property (1- (point)) 'hard
))))
228 (delete-backward-char 1)
233 ;; If there was no newline, and there is text in the paragraph, then
235 (if (and (not oneleft
) (> to from-plus-indent
))
237 (goto-char from-plus-indent
))
239 (if (not (> to
(point)))
240 nil
; There is no paragraph, only whitespace: exit now.
242 (or justify
(setq justify
(current-justification)))
244 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
245 (let ((fill-prefix fill-prefix
))
246 ;; Figure out how this paragraph is indented, if desired.
247 (if (and adaptive-fill-mode
248 (or (null fill-prefix
) (string= fill-prefix
"")))
249 (setq fill-prefix
(fill-context-prefix from to
)))
254 (narrow-to-region (point) to
)
256 (if (not justify
) ; filling disabled: just check indentation
260 (if (and (not (eolp))
261 (< (current-indentation) (current-left-margin)))
262 (indent-to-left-margin))
265 (if use-hard-newlines
266 (remove-text-properties from
(point-max) '(hard nil
)))
267 ;; Make sure first line is indented (at least) to left margin...
268 (if (or (memq justify
'(right center
))
269 (< (current-indentation) (current-left-margin)))
270 (indent-to-left-margin))
271 ;; Delete the fill prefix from every line except the first.
272 ;; The first line may not even have a fill prefix.
274 (let ((fpre (and fill-prefix
(not (equal fill-prefix
""))
276 (regexp-quote fill-prefix
)
280 (if (>= (+ (current-left-margin) (length fill-prefix
))
281 (current-fill-column))
282 (error "fill-prefix too long for specified width"))
286 (if (looking-at fpre
)
287 (delete-region (point) (match-end 0)))
290 (if (looking-at fpre
)
291 (goto-char (match-end 0)))
292 (setq from
(point)))))
293 ;; Remove indentation from lines other than the first.
294 (beginning-of-line 2)
295 (indent-region (point) (point-max) 0)
298 ;; FROM, and point, are now before the text to fill,
299 ;; but after any fill prefix on the first line.
301 ;; Make sure sentences ending at end of line get an extra space.
302 ;; loses on split abbrevs ("Mr.\nSmith")
303 (while (re-search-forward "[.?!][])}\"']*$" nil t
)
304 (or (eobp) (insert-and-inherit ?\
)))
306 ;; The character category `|' means that we can break a line
307 ;; at the character. Since we don't need a space between
308 ;; them, delete all newlines between them ...
309 (while (re-search-forward "\\c|\n\\|\n\\c|" nil t
)
312 (if (= (char-before (match-beginning 0)) ?\
)
313 ;; ... except when there is end of sentence. The
314 ;; variable `sentence-end-double-space' is handled
317 (delete-region (match-beginning 0) (1+ (match-beginning 0))))))
319 (skip-chars-forward " \t")
320 ;; Then change all newlines to spaces.
321 (subst-char-in-region from
(point-max) ?
\n ?\
)
322 (if (and nosqueeze
(not (eq justify
'full
)))
324 (canonically-space-region (or squeeze-after
(point)) (point-max))
325 (goto-char (point-max))
326 (delete-horizontal-space)
327 (insert-and-inherit " "))
328 (goto-char (point-min))
330 ;; This is the actual filling loop.
331 (let ((prefixcol 0) linebeg
)
333 (setq linebeg
(point))
334 (move-to-column (1+ (current-fill-column)))
336 (or nosqueeze
(delete-horizontal-space))
337 ;; Move back to the point where we can break the line
338 ;; at. We break the line between word or after/before
339 ;; the character which has character category `|'. We
340 ;; search space, \c| followed by a character, or \c|
341 ;; following a character. If not found, place
342 ;; the point at linebeg.
343 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg
0)
344 ;; In case of space, we place the point at next to
345 ;; the point where the break occurs acutually,
346 ;; because we don't want to change the following
347 ;; logic of original Emacs. In case of \c|, the
348 ;; point is at the place where the break occurs.
350 ;; Don't break after a period followed by just one space.
351 ;; Move back to the previous place to break.
352 ;; The reason is that if a period ends up at the end of a line,
353 ;; further fills will assume it ends a sentence.
354 ;; If we now know it does not end a sentence,
355 ;; avoid putting it at the end of the line.
356 (if sentence-end-double-space
357 (while (and (> (point) (+ linebeg
2))
358 (eq (preceding-char) ?\
)
359 (not (eq (following-char) ?\
))
360 (eq (char-after (- (point) 2)) ?\.
))
362 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg
0)
364 ;; If the left margin and fill prefix by themselves
365 ;; pass the fill-column. or if they are zero
366 ;; but we have no room for even one word,
367 ;; keep at least one word or a character which has
368 ;; category `|'anyway .
369 ;; This handles ALL BUT the first line of the paragraph.
370 (if (if (zerop prefixcol
)
372 (skip-chars-backward " \t" linebeg
)
374 (>= prefixcol
(current-column)))
375 ;; Ok, skip at least one word or one \c| character.
376 ;; Meanwhile, don't stop at a period followed by one space.
378 (move-to-column prefixcol
)
379 (while (and (not (eobp))
382 sentence-end-double-space
383 (save-excursion (forward-char -
1)
384 (and (looking-at "\\. ")
385 (not (looking-at "\\. ")))))))
386 (skip-chars-forward " \t")
387 ;; Skip one \c| character or one word.
388 (if (looking-at "$\\|\\c|\\|[^ \t\n]+")
389 (goto-char (match-end 0)))
391 ;; Normally, move back over the single space between the words.
392 (if (= (preceding-char) ?\
) (forward-char -
1))
393 ;; Do KINSOKU processing.
394 (if do-kinsoku
(kinsoku linebeg
)))
396 ;; If the left margin and fill prefix by themselves
397 ;; pass the fill-column, keep at least one word.
398 ;; This handles the first line of the paragraph.
399 (if (and (zerop prefixcol
)
400 (let ((fill-point (point)) nchars
)
402 (move-to-left-margin)
403 (setq nchars
(- fill-point
(point)))
406 (< nchars
(length fill-prefix
))
407 (string= (buffer-substring (point) fill-point
)
408 (substring fill-prefix
0 nchars
)))))))
409 ;; Ok, skip at least one word. But
410 ;; don't stop at a period followed by just one space.
412 (while (and (not (eobp))
415 sentence-end-double-space
416 (save-excursion (forward-char -
1)
417 (and (looking-at "\\. ")
418 (not (looking-at "\\. ")))))))
419 (skip-chars-forward " \t")
420 ;; Skip one \c| character or one word.
421 (if (looking-at "$\\|\\c|\\|[^ \t\n]+")
422 (goto-char (match-end 0)))
424 ;; Check again to see if we got to the end of the paragraph.
425 (if (save-excursion (skip-chars-forward " \t") (eobp))
426 (or nosqueeze
(delete-horizontal-space))
427 ;; Replace whitespace here with one newline, then indent to left
429 (skip-chars-backward " \t")
430 (if (and (= (following-char) ?\
)
431 (or (aref (char-category-set (preceding-char)) ?|
)
432 (looking-at "[ \t]+\\c|")))
433 ;; We need one space at end of line so that
434 ;; further filling won't delete it. NOTE: We
435 ;; intentionally leave this one space to
436 ;; distingush the case that user wants to put
437 ;; space between \c| characters.
440 ;; Give newline the properties of the space(s) it replaces
441 (set-text-properties (1- (point)) (point)
442 (text-properties-at (point)))
443 (indent-to-left-margin)
444 ;; Insert the fill prefix after indentation.
445 ;; Set prefixcol so whitespace in the prefix won't get lost.
446 (and fill-prefix
(not (equal fill-prefix
""))
448 (insert-and-inherit fill-prefix
)
449 (setq prefixcol
(current-column))))))
450 ;; Justify the line just ended, if desired.
453 (justify-current-line justify t t
)
455 (justify-current-line justify nil t
)
456 (forward-line 1))))))
457 ;; Leave point after final newline.
458 (goto-char (point-max)))
461 (defun fill-paragraph (arg)
462 "Fill paragraph at or after point. Prefix arg means justify as well.
463 If `sentence-end-double-space' is non-nil, then period followed by one
464 space does not end a sentence, so don't break a line there.
466 If `fill-paragraph-function' is non-nil, we call it (passing our
467 argument to it), and if it returns non-nil, we simply return its value."
468 (interactive (list (if current-prefix-arg
'full
)))
469 (or (and fill-paragraph-function
470 (let ((function fill-paragraph-function
)
471 fill-paragraph-function
)
472 (funcall function arg
)))
473 (let ((before (point)))
476 (or (bolp) (newline 1))
478 (beg (progn (backward-paragraph) (point))))
480 (if use-hard-newlines
481 ;; Can't use fill-region-as-paragraph, since this paragraph may
482 ;; still contain hard newlines. See fill-region.
483 (fill-region beg end arg
)
484 (fill-region-as-paragraph beg end arg
)))))))
486 (defun fill-region (from to
&optional justify nosqueeze to-eop
)
487 "Fill each of the paragraphs in the region.
488 Prefix arg (non-nil third arg, if called from program) means justify as well.
490 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
491 whitespace other than line breaks untouched, and fifth arg TO-EOP
492 non-nil means to keep filling to the end of the paragraph (or next
493 hard newline, if `use-hard-newlines' is on).
495 If `sentence-end-double-space' is non-nil, then period followed by one
496 space does not end a sentence, so don't break a line there."
497 (interactive (list (region-beginning) (region-end)
498 (if current-prefix-arg
'full
)))
501 (goto-char (max from to
))
503 (progn (skip-chars-backward "\n")
504 (forward-paragraph)))
506 (goto-char (setq beg
(min from to
)))
508 (narrow-to-region (point) end
)
510 (let ((initial (point))
512 ;; If using hard newlines, break at every one for filling
513 ;; purposes rather than using paragraph breaks.
514 (if use-hard-newlines
516 (while (and (setq end
(text-property-any (point) (point-max)
518 (not (= ?
\n (char-after end
)))
519 (not (= end
(point-max))))
520 (goto-char (1+ end
)))
521 (setq end
(if end
(min (point-max) (1+ end
)) (point-max)))
523 (forward-paragraph 1)
525 (forward-paragraph -
1))
528 (if (>= (point) initial
)
529 (fill-region-as-paragraph (point) end justify nosqueeze
)
530 (goto-char end
)))))))
533 (defcustom default-justification
'left
534 "*Method of justifying text not otherwise specified.
535 Possible values are `left', `right', `full', `center', or `none'.
536 The requested kind of justification is done whenever lines are filled.
537 The `justification' text-property can locally override this variable.
538 This variable automatically becomes buffer-local when set in any fashion."
539 :type
'(choice (const left
)
545 (make-variable-buffer-local 'default-justification
)
547 (defun current-justification ()
548 "How should we justify this line?
549 This returns the value of the text-property `justification',
550 or the variable `default-justification' if there is no text-property.
551 However, it returns nil rather than `none' to mean \"don't justify\"."
552 (let ((j (or (get-text-property
553 ;; Make sure we're looking at paragraph body.
554 (save-excursion (skip-chars-forward " \t")
555 (if (and (eobp) (not (bobp)))
556 (1- (point)) (point)))
558 default-justification
)))
563 (defun set-justification (begin end value
&optional whole-par
)
564 "Set the region's justification style.
565 The kind of justification to use is prompted for.
566 If the mark is not active, this command operates on the current paragraph.
567 If the mark is active, the region is used. However, if the beginning and end
568 of the region are not at paragraph breaks, they are moved to the beginning and
569 end of the paragraphs they are in.
570 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
573 When calling from a program, operates just on region between BEGIN and END,
574 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
575 extended to include entire paragraphs as in the interactive command."
576 (interactive (list (if mark-active
(region-beginning) (point))
577 (if mark-active
(region-end) (point))
578 (let ((s (completing-read
579 "Set justification to: "
580 '(("left") ("right") ("full")
583 (if (equal s
"") (error ""))
589 (let ((paragraph-start (if use-hard-newlines
"." paragraph-start
))
590 (paragraph-ignore-fill-prefix (if use-hard-newlines t
591 paragraph-ignore-fill-prefix
)))
593 (while (and (bolp) (not (eobp))) (forward-char 1))
597 (skip-chars-backward " \t\n" begin
)
601 (narrow-to-region (point-min) end
)
602 (unjustify-region begin
(point-max))
603 (put-text-property begin
(point-max) 'justification value
)
604 (fill-region begin
(point-max) nil t
))))
606 (defun set-justification-none (b e
)
607 "Disable automatic filling for paragraphs in the region.
608 If the mark is not active, this applies to the current paragraph."
609 (interactive (list (if mark-active
(region-beginning) (point))
610 (if mark-active
(region-end) (point))))
611 (set-justification b e
'none t
))
613 (defun set-justification-left (b e
)
614 "Make paragraphs in the region left-justified.
615 This is usually the default, but see the variable `default-justification'.
616 If the mark is not active, this applies to the current paragraph."
617 (interactive (list (if mark-active
(region-beginning) (point))
618 (if mark-active
(region-end) (point))))
619 (set-justification b e
'left t
))
621 (defun set-justification-right (b e
)
622 "Make paragraphs in the region right-justified:
623 Flush at the right margin and ragged on the left.
624 If the mark is not active, this applies to the current paragraph."
625 (interactive (list (if mark-active
(region-beginning) (point))
626 (if mark-active
(region-end) (point))))
627 (set-justification b e
'right t
))
629 (defun set-justification-full (b e
)
630 "Make paragraphs in the region fully justified:
631 This makes lines flush on both margins by inserting spaces between words.
632 If the mark is not active, this applies to the current paragraph."
633 (interactive (list (if mark-active
(region-beginning) (point))
634 (if mark-active
(region-end) (point))))
635 (set-justification b e
'full t
))
637 (defun set-justification-center (b e
)
638 "Make paragraphs in the region centered.
639 If the mark is not active, this applies to the current paragraph."
640 (interactive (list (if mark-active
(region-beginning) (point))
641 (if mark-active
(region-end) (point))))
642 (set-justification b e
'center t
))
644 ;; A line has up to six parts:
647 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
649 ;; "Indent-1" is the left-margin indentation; normally it ends at column
650 ;; given by the `current-left-margin' function.
651 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
652 ;; "Indent-2" is added to justify a line if the `current-justification' is
653 ;; `center' or `right'. In `left' and `full' justification regions, any
654 ;; whitespace there is part of the line's text, and should not be changed.
655 ;; Trailing whitespace is not counted as part of the line length when
656 ;; center- or right-justifying.
658 ;; All parts of the line are optional, although the final newline can
659 ;; only be missing on the last line of the buffer.
661 (defun justify-current-line (&optional how eop nosqueeze
)
662 "Do some kind of justification on this line.
663 Normally does full justification: adds spaces to the line to make it end at
664 the column given by `current-fill-column'.
665 Optional first argument HOW specifies alternate type of justification:
666 it can be `left', `right', `full', `center', or `none'.
667 If HOW is t, will justify however the `current-justification' function says to.
668 If HOW is nil or missing, full justification is done by default.
669 Second arg EOP non-nil means that this is the last line of the paragraph, so
670 it will not be stretched by full justification.
671 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
672 otherwise it is made canonical."
674 (if (eq t how
) (setq how
(or (current-justification) 'none
))
675 (if (null how
) (setq how
'full
)
676 (or (memq how
'(none left right center
))
678 (or (memq how
'(none left
)) ; No action required for these.
679 (let ((fc (current-fill-column))
681 fp-end
; point at end of fill prefix
682 beg
; point at beginning of line's text
683 end
; point at end of line's text
684 indent
; column of `beg'
685 endcol
; column of `end'
686 ncols
) ; new indent point or offset
688 ;; Check if this is the last line of the paragraph.
689 (if (and use-hard-newlines
(null eop
)
690 (get-text-property (point) 'hard
))
692 (skip-chars-backward " \t")
693 ;; Quick exit if it appears to be properly justified already
694 ;; or there is no text.
696 (and (memq how
'(full right
))
697 (= (current-column) fc
)))
701 (skip-chars-forward " \t")
702 ;; Skip over fill-prefix.
704 (not (string-equal fill-prefix
""))
707 (point) (min (point-max) (+ (length fill-prefix
)
709 (forward-char (length fill-prefix
))
710 (if (and adaptive-fill-mode
711 (looking-at adaptive-fill-regexp
))
712 (goto-char (match-end 0))))
713 (setq fp-end
(point))
714 (skip-chars-forward " \t")
715 ;; This is beginning of the line's text.
716 (setq indent
(current-column))
719 (setq endcol
(current-column))
721 ;; HOW can't be null or left--we would have exited already
722 (cond ((eq 'right how
)
723 (setq ncols
(- fc endcol
))
725 ;; Need to remove some indentation
727 (progn (goto-char fp-end
)
728 (if (< (current-column) (+ indent ncols
))
729 (move-to-column (+ indent ncols
) t
))
731 (progn (move-to-column indent
) (point)))
734 (indent-to (+ indent ncols
))
735 ;; If point was at beginning of text, keep it there.
737 (move-marker pos
(point)))))
740 ;; Figure out how much indentation is needed
741 (setq ncols
(+ (current-left-margin)
742 (/ (- fc
(current-left-margin) ;avail. space
743 (- endcol indent
)) ;text width
746 ;; Have too much indentation - remove some
748 (progn (goto-char fp-end
)
749 (if (< (current-column) ncols
)
750 (move-to-column ncols t
))
752 (progn (move-to-column indent
) (point)))
753 ;; Have too little - add some
756 ;; If point was at beginning of text, keep it there.
758 (move-marker pos
(point)))))
761 ;; Insert extra spaces between words to justify line
763 (narrow-to-region beg end
)
765 (canonically-space-region beg end
))
766 (goto-char (point-max))
767 (setq ncols
(- fc endcol
))
768 ;; Ncols is number of additional spaces needed
771 (search-backward " " nil t
))
773 (let ((nmove (+ 3 (random 3))))
775 (or (search-backward " " nil t
)
777 (goto-char (point-max))
778 (search-backward " ")))
779 (skip-chars-backward " ")
780 (setq nmove
(1- nmove
))))
781 (insert-and-inherit " ")
782 (skip-chars-backward " ")
783 (setq ncols
(1- ncols
)))))))
784 (t (error "Unknown justification value"))))
786 (move-marker pos nil
)))
789 (defun unjustify-current-line ()
790 "Remove justification whitespace from current line.
791 If the line is centered or right-justified, this function removes any
792 indentation past the left margin. If the line is full-justified, it removes
793 extra spaces between words. It does nothing in other justification modes."
794 (let ((justify (current-justification)))
795 (cond ((eq 'left justify
) nil
)
796 ((eq nil justify
) nil
)
797 ((eq 'full justify
) ; full justify: remove extra spaces
798 (beginning-of-line-text)
799 (canonically-space-region
800 (point) (save-excursion (end-of-line) (point))))
801 ((memq justify
'(center right
))
803 (move-to-left-margin nil t
)
804 ;; Position ourselves after any fill-prefix.
806 (not (string-equal fill-prefix
""))
809 (point) (min (point-max) (+ (length fill-prefix
)
811 (forward-char (length fill-prefix
)))
812 (delete-region (point) (progn (skip-chars-forward " \t")
815 (defun unjustify-region (&optional begin end
)
816 "Remove justification whitespace from region.
817 For centered or right-justified regions, this function removes any indentation
818 past the left margin from each line. For full-justified lines, it removes
819 extra spaces between words. It does nothing in other justification modes.
820 Arguments BEGIN and END are optional; default is the whole buffer."
823 (if end
(narrow-to-region (point-min) end
))
824 (goto-char (or begin
(point-min)))
826 (unjustify-current-line)
830 (defun fill-nonuniform-paragraphs (min max
&optional justifyp mailp
)
831 "Fill paragraphs within the region, allowing varying indentation within each.
832 This command divides the region into \"paragraphs\",
833 only at paragraph-separator lines, then fills each paragraph
834 using as the fill prefix the smallest indentation of any line
837 When calling from a program, pass range to fill as first two arguments.
839 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
840 JUSTIFY to justify paragraphs (prefix arg),
841 MAIL-FLAG for a mail message, i. e. don't fill header lines."
842 (interactive (list (region-beginning) (region-end)
843 (if current-prefix-arg
'full
)))
844 (let ((fill-individual-varying-indent t
))
845 (fill-individual-paragraphs min max justifyp mailp
)))
847 (defun fill-individual-paragraphs (min max
&optional justify mailp
)
848 "Fill paragraphs of uniform indentation within the region.
849 This command divides the region into \"paragraphs\",
850 treating every change in indentation level as a paragraph boundary,
851 then fills each paragraph using its indentation level as the fill prefix.
853 When calling from a program, pass range to fill as first two arguments.
855 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
856 JUSTIFY to justify paragraphs (prefix arg),
857 MAIL-FLAG for a mail message, i. e. don't fill header lines."
858 (interactive (list (region-beginning) (region-end)
859 (if current-prefix-arg
'full
)))
864 (narrow-to-region (point) max
)
866 (while (and (not (eobp))
867 (or (looking-at "[ \t]*[^ \t\n]+:")
868 (looking-at "[ \t]*$")))
869 (if (looking-at "[ \t]*[^ \t\n]+:")
870 (search-forward "\n\n" nil
'move
)
872 (narrow-to-region (point) max
)
873 ;; Loop over paragraphs.
874 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
875 (move-to-left-margin)
876 (let ((start (point))
877 fill-prefix fill-prefix-regexp
)
878 ;; Find end of paragraph, and compute the smallest fill-prefix
879 ;; that fits all the lines in this paragraph.
881 ;; Update the fill-prefix on the first line
882 ;; and whenever the prefix good so far is too long.
883 (if (not (and fill-prefix
884 (looking-at fill-prefix-regexp
)))
886 (if (and adaptive-fill-mode adaptive-fill-regexp
887 (looking-at adaptive-fill-regexp
))
891 (save-excursion (skip-chars-forward " \t")
893 fill-prefix-regexp
(regexp-quote fill-prefix
)))
896 ;; If forward-line went past a newline,
897 ;; move further to the left margin.
898 (move-to-left-margin))
899 ;; Now stop the loop if end of paragraph.
901 (if fill-individual-varying-indent
902 ;; If this line is a separator line, with or
903 ;; without prefix, end the paragraph.
905 (not (looking-at paragraph-separate
))
907 (not (and (looking-at fill-prefix-regexp
)
908 (progn (forward-char (length fill-prefix
))
909 (looking-at paragraph-separate
))))))
910 ;; If this line has more or less indent
911 ;; than the fill prefix wants, end the paragraph.
912 (and (looking-at fill-prefix-regexp
)
914 (not (progn (forward-char (length fill-prefix
))
915 (or (looking-at paragraph-separate
)
916 (looking-at paragraph-start
))))))))))
917 ;; Fill this paragraph, but don't add a newline at the end.
918 (let ((had-newline (bolp)))
919 (fill-region-as-paragraph start
(point) justify
)
920 (or had-newline
(delete-char -
1))))))))
922 ;;; fill.el ends here