fill.el 29 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; fill.el --- fill commands for Emacs

Karl Heuer's avatar
Karl Heuer committed
3
;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
root's avatar
root committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5 6
;; Keywords: wp

root's avatar
root committed
7 8 9 10
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Eric S. Raymond's avatar
Eric S. Raymond committed
11
;; the Free Software Foundation; either version 2, or (at your option)
root's avatar
root committed
12 13 14 15 16 17 18 19 20 21 22
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

23 24 25 26 27
;;; Commentary:

;; All the commands for filling text.  These are documented in the Emacs
;; manual.

Eric S. Raymond's avatar
Eric S. Raymond committed
28
;;; Code:
root's avatar
root committed
29

Jim Blandy's avatar
Jim Blandy committed
30 31 32 33 34
(defconst fill-individual-varying-indent nil
  "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
Non-nil means changing indent doesn't end a paragraph.
That mode can handle paragraphs with extra indentation on the first line,
but it requires separator lines between paragraphs.
35 36 37 38
A value of nil means that any change in indentation starts a new paragraph.")

(defconst sentence-end-double-space t
  "*Non-nil means a single space does not end a sentence.")
Jim Blandy's avatar
Jim Blandy committed
39

40 41 42
(defvar fill-paragraph-function nil
  "Mode-specific function to fill a paragraph.")

root's avatar
root committed
43
(defun set-fill-prefix ()
44
  "Set the fill prefix to the current line up to point.
45 46
Filling expects lines to start with the fill prefix and
reinserts the fill prefix in each resulting line."
root's avatar
root committed
47 48
  (interactive)
  (setq fill-prefix (buffer-substring
49
		     (save-excursion (move-to-left-margin) (point))
root's avatar
root committed
50 51 52 53 54 55 56
		     (point)))
  (if (equal fill-prefix "")
      (setq fill-prefix nil))
  (if fill-prefix
      (message "fill-prefix: \"%s\"" fill-prefix)
    (message "fill-prefix cancelled")))

57 58 59
(defconst adaptive-fill-mode t
  "*Non-nil means determine a paragraph's fill prefix from its text.")

60
(defconst adaptive-fill-regexp "[ \t]*\\([#;>*]+ +\\)?"
61 62 63 64 65
  "*Regexp to match text at start of line that constitutes indentation.
If Adaptive Fill mode is enabled, whatever text matches this pattern
on the second line of a paragraph is used as the standard indentation
for the paragraph.")

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
(defun current-fill-column ()
  "Return the fill-column to use for this line.
The fill-column to use for a buffer is stored in the variable `fill-column',
but can be locally modified by the `right-margin' text property, which is
subtracted from `fill-column'.

The fill column to use for a line is the first column at which the column
number equals or exceeds the local fill-column - right-margin difference."
  (save-excursion
    (let* ((here (progn (beginning-of-line) (point)))
	   (here-col 0)
	   (eol (progn (end-of-line) (point)))
	   margin fill-col change col)
      ;; Look separately at each region of line with a different right-margin
      (while (and (setq margin (get-text-property here 'right-margin)
			fill-col (- fill-column (or margin 0))
			change (text-property-not-all here eol 
						      'right-margin margin))
		  (progn (goto-char (1- change))
			 (setq col (current-column))
			 (< col fill-col)))
	(setq here change
	      here-col col))
      (max here-col fill-col))))

(defun canonically-space-region (beg end)
  "Remove extra spaces between words in region.
Puts one space between words in region; two between sentences.
Remove indenation from each line."
  (interactive "r")
  (save-excursion
    (goto-char beg)
    ;; Nuke tabs; they get screwed up in a fill.
    ;; This is quick, but loses when a tab follows the end of a sentence.
    ;; Actually, it is difficult to tell that from "Mr.\tSmith".
    ;; Blame the typist.
    (subst-char-in-region beg end ?\t ?\ )
    (while (and (< (point) end)
		(re-search-forward "   *" end t))
      (delete-region
       (+ (match-beginning 0)
	  ;; Determine number of spaces to leave:
	  (save-excursion
	    (skip-chars-backward " ]})\"'")
	    (cond ((and sentence-end-double-space
			(memq (preceding-char) '(?. ?? ?!)))  2)
		  ((char-equal (preceding-char) ?\n)  0)
		  (t 1))))
       (match-end 0)))
    ;; Make sure sentences ending at end of line get an extra space.
    ;; loses on split abbrevs ("Mr.\nSmith")
    (goto-char beg)
    (while (and (< (point) end)
		(re-search-forward "[.?!][])}\"']*$" end t))
      (insert-and-inherit ? ))))

(defun fill-region-as-paragraph (from to &optional justify nosqueeze)
123 124 125 126
  "Fill the region as one paragraph.
Removes any paragraph breaks in the region and extra newlines at the end,
indents and fills lines between the margins given by the
`current-left-margin' and `current-fill-column' functions.
127

128 129 130 131 132 133
Normally performs justification according to the `current-justification'
function, but with a prefix arg, does full justification instead.

From a program, optional third arg JUSTIFY can specify any type of
justification, and fourth arg NOSQUEEZE non-nil means not to make spaces
between words canonical before filling.
134

135
If `sentence-end-double-space' is non-nil, then period followed by one
136
space does not end a sentence, so don't break a line there."
root's avatar
root committed
137
  (interactive "r\nP")
138 139 140
  ;; Arrange for undoing the fill to restore point.
  (if (and buffer-undo-list (not (eq buffer-undo-list t)))
      (setq buffer-undo-list (cons (point) buffer-undo-list)))
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

  ;; Make sure "to" is the endpoint.  Make sure that we end up there.
  (goto-char (min from to))
  (setq to   (max from to))
  (setq from (point))

  ;; Delete all but one soft newline at end of region.
  (goto-char to)
  (let ((oneleft nil))
    (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
      (if (and oneleft
	       (not (and use-hard-newlines
			 (get-text-property (1- (point)) 'hard))))
	  (delete-backward-char 1)
	(backward-char 1)
	(setq oneleft t)))
    ;; If there was no newline, create one.
    (if (and (not oneleft) (> (point) from))
	(save-excursion (newline))))
  (setq to (point))

  ;; Ignore blank lines at beginning of region.
  (goto-char from)
  (skip-chars-forward " \t\n")
  (beginning-of-line)
  (setq from (point))
  
  (if (>= from to)
      nil ; There is no paragraph at all.

    (or justify (setq justify (current-justification)))

    ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
    (let ((fill-prefix fill-prefix))
      ;; Figure out how this paragraph is indented, if desired.
      (if (and adaptive-fill-mode
	       (or (null fill-prefix) (string= fill-prefix "")))
	  (save-excursion
	    (goto-char from)
180
	    (if (eolp) (forward-line 1))
181 182 183 184 185 186 187 188
	    (forward-line 1)
	    (move-to-left-margin)
	    (if (< (point) to)
		(let ((start (point)))
		  (re-search-forward adaptive-fill-regexp)
		  (setq fill-prefix (buffer-substring start (point)))
		  (set-text-properties 0 (length fill-prefix) nil
				       fill-prefix)))
189
	    ;; If paragraph has only one line, don't assume in general
190
	    ;; that additional lines would have the same starting
191
	    ;; decoration.  Assume no indentation.
192
	    ))
193 194 195

      (save-restriction
	(goto-char from)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	(beginning-of-line)
	(narrow-to-region (point) to)

	(if (not justify)	    ; filling disabled: just check indentation
	    (progn
	      (goto-char from)
	      (while (not (eobp))
		(if (and (not (eolp))
			 (< (current-indentation) (current-left-margin)))
		    (indent-to-left-margin))
		(forward-line 1)))

	  (if use-hard-newlines
	      (remove-text-properties from (point-max) '(hard nil)))
	  ;; Make sure first line is indented (at least) to left margin...
	  (if (or (memq justify '(right center))
		  (< (current-indentation) (current-left-margin)))
	      (indent-to-left-margin))
	  ;; and remove indentation from other lines.
	  (beginning-of-line 2)
	  (indent-region (point) (point-max) 0)
	  ;; Delete the fill prefix from every line except the first.
	  ;; The first line may not even have a fill prefix.
	  (goto-char from)
	  (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
			   (concat "[ \t]*"
				   (regexp-quote fill-prefix)
				   "[ \t]*"))))
	    (and fpre
		 (progn
		   (if (>= (+ (current-left-margin) (length fill-prefix))
			   (current-fill-column))
		       (error "fill-prefix too long for specified width"))
		   (goto-char from)
		   (forward-line 1)
		   (while (not (eobp))
		     (if (looking-at fpre)
			 (delete-region (point) (match-end 0)))
		     (forward-line 1))
		   (goto-char from)
		   (and (looking-at fpre) (goto-char (match-end 0)))
		   (setq from (point)))))
	  ;; "from" is now before the text to fill,
	  ;; but after any fill prefix on the first line.

	  ;; Make sure sentences ending at end of line get an extra space.
	  ;; loses on split abbrevs ("Mr.\nSmith")
	  (while (re-search-forward "[.?!][])}\"']*$" nil t)
	    (insert-and-inherit ? ))
	  (goto-char from)
	  (skip-chars-forward " \t")
	  ;; Then change all newlines to spaces.
	  (subst-char-in-region from (point-max) ?\n ?\ )
	  (if (and nosqueeze (not (eq justify 'full)))
	      nil
	    (canonically-space-region (point) (point-max))
	    (goto-char (point-max))
	    (delete-horizontal-space)
	    (insert-and-inherit " "))
	  (goto-char (point-min))

	  ;; This is the actual filling loop.
	  (let ((prefixcol 0) linebeg)
	    (while (not (eobp))
	      (setq linebeg (point))
	      (move-to-column (1+ (current-fill-column)))
	      (if (eobp)
		  (or nosqueeze (delete-horizontal-space))
		;; Move back to start of word.
		(skip-chars-backward "^ \n" linebeg)
		;; Don't break after a period followed by just one space.
		;; Move back to the previous place to break.
		;; The reason is that if a period ends up at the end of a line,
		;; further fills will assume it ends a sentence.
		;; If we now know it does not end a sentence,
		;; avoid putting it at the end of the line.
		(if sentence-end-double-space
		    (while (and (> (point) (+ linebeg 2))
				(eq (preceding-char) ?\ )
				(not (eq (following-char) ?\ ))
				(eq (char-after (- (point) 2)) ?\.))
		      (forward-char -2)
		      (skip-chars-backward "^ \n" linebeg)))
		(if (if (zerop prefixcol)
			(save-excursion
			  (skip-chars-backward " " linebeg)
			  (bolp))
		      (>= prefixcol (current-column)))
		    ;; Keep at least one word even if fill prefix exceeds margin.
		    ;; This handles all but the first line of the paragraph.
		    ;; Meanwhile, don't stop at a period followed by one space.
		    (let ((first t))
		      (move-to-column prefixcol)
		      (while (and (not (eobp))
				  (or first
				      (and (not (bobp))
					   sentence-end-double-space
					   (save-excursion (forward-char -1)
							   (and (looking-at "\\. ")
								(not (looking-at "\\.  ")))))))
			(skip-chars-forward " ")
			(skip-chars-forward "^ \n")
			(setq first nil)))
		  ;; Normally, move back over the single space between the words.
		  (forward-char -1))
		(if (and fill-prefix (zerop prefixcol)
			 (< (- (point) (point-min)) (length fill-prefix))
			 (string= (buffer-substring (point-min) (point))
				  (substring fill-prefix 0 (- (point) (point-min)))))
		    ;; Keep at least one word even if fill prefix exceeds margin.
		    ;; This handles the first line of the paragraph.
		    ;; Don't stop at a period followed by just one space.
		    (let ((first t))
		      (while (and (not (eobp))
				  (or first
				      (and (not (bobp))
					   sentence-end-double-space
					   (save-excursion (forward-char -1)
							   (and (looking-at "\\. ")
								(not (looking-at "\\.  ")))))))
			(skip-chars-forward " ")
			(skip-chars-forward "^ \n")
			(setq first nil))))
		;; Replace whitespace here with one newline, then indent to left
		;; margin.
		(skip-chars-backward " ")
		(insert ?\n)
		;; Give newline the properties of the space(s) it replaces
		(set-text-properties (1- (point)) (point)
				     (text-properties-at (point)))
		(indent-to-left-margin)
		;; Insert the fill prefix after indentation.
		;; Set prefixcol so whitespace in the prefix won't get lost.
		(and fill-prefix (not (equal fill-prefix ""))
		     (progn
		       (insert-and-inherit fill-prefix)
		       (setq prefixcol (current-column)))))
	      ;; Justify the line just ended, if desired.
	      (if justify
		  (if (eobp)
		      (justify-current-line justify t t)
		    (forward-line -1)
		    (justify-current-line justify nil t)
		    (forward-line 1))))))
	;; Leave point after final newline.
	(goto-char (point-max)))
    (forward-char 1))))
root's avatar
root committed
343 344

(defun fill-paragraph (arg)
345 346
  "Fill paragraph at or after point.  Prefix arg means justify as well.
If `sentence-end-double-space' is non-nil, then period followed by one
347 348 349 350
space does not end a sentence, so don't break a line there.

If `fill-paragraph-function' is non-nil, we call it (passing our
argument to it), and if it returns non-nil, we simply return its value."
root's avatar
root committed
351
  (interactive "P")
352
  (or (and fill-paragraph-function
353 354 355
	   (let ((function fill-paragraph-function)
		 fill-paragraph-function)
	     (funcall function arg)))
356 357 358 359 360 361 362 363 364 365 366 367
      (let ((before (point)))
	(save-excursion
	  (forward-paragraph)
	  (or (bolp) (newline 1))
	  (let ((end (point))
		(beg (progn (backward-paragraph) (point))))
	    (goto-char before)
	    (if use-hard-newlines
		;; Can't use fill-region-as-paragraph, since this paragraph may
		;; still contain hard newlines.  See fill-region.
		(fill-region beg end arg)
	      (fill-region-as-paragraph beg end arg)))))))
root's avatar
root committed
368

369
(defun fill-region (from to &optional justify nosqueeze to-eop)
root's avatar
root committed
370
  "Fill each of the paragraphs in the region.
371
Prefix arg (non-nil third arg, if called from program) means justify as well.
372 373 374 375 376 377

Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
whitespace other than line breaks untouched, and fifth arg TO-EOP
non-nil means to keep filling to the end of the paragraph (or next
hard newline, if `use-hard-newlines' is on).

378 379
If `sentence-end-double-space' is non-nil, then period followed by one
space does not end a sentence, so don't break a line there."
root's avatar
root committed
380
  (interactive "r\nP")
381
  (let (end beg)
382 383
    (save-restriction
      (goto-char (max from to))
384 385 386
      (if to-eop
	  (progn (skip-chars-backward "\n")
		 (forward-paragraph)))
387 388 389 390 391 392
      (setq end (point))
      (goto-char (setq beg (min from to)))
      (beginning-of-line)
      (narrow-to-region (point) end)
      (while (not (eobp))
	(let ((initial (point))
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	      end)
	  ;; If using hard newlines, break at every one for filling
	  ;; purposes rather than using paragraph breaks. 
	  (if use-hard-newlines
	      (progn 
		(while (and (setq end (text-property-any (point) (point-max)
							 'hard t))
			    (not (= ?\n (char-after end)))
			    (not (= end (point-max))))
		  (goto-char (1+ end)))
		(setq end (min (point-max) (1+ end)))
		(goto-char initial))
	    (forward-paragraph 1)
	    (setq end (point))
	    (forward-paragraph -1))
408 409 410
	  (if (< (point) beg)
	      (goto-char beg))
	  (if (>= (point) initial)
411
	      (fill-region-as-paragraph (point) end justify nosqueeze)
412
	    (goto-char end)))))))
root's avatar
root committed
413

414 415 416 417 418 419 420 421 422

(defconst default-justification 'left
  "*Method of justifying text not otherwise specified.
Possible values are `left', `right', `full', `center', or `none'.
The requested kind of justification is done whenever lines are filled.
The `justification' text-property  can locally override this variable.
This variable automatically becomes buffer-local when set in any fashion.")
(make-variable-buffer-local 'default-justification)

423
(defun current-justification ()
424 425 426 427 428 429
  "How should we justify this line?
This returns the value of the text-property `justification',
or the variable `default-justification' if there is no text-property.
However, it returns nil rather than `none' to mean \"don't justify\"."
  (let ((j (or (get-text-property 
		;; Make sure we're looking at paragraph body.
430 431 432
		(save-excursion (skip-chars-forward " \t") 
				(if (and (eobp) (not (bobp)))
				    (1- (point)) (point)))
433 434 435 436 437 438
		'justification)
	       default-justification)))
    (if (eq 'none j)
	nil
      j)))

439
(defun set-justification (begin end value &optional whole-par)
440
  "Set the region's justification style.
441 442 443 444 445 446 447 448 449 450 451
The kind of justification to use is prompted for.
If the mark is not active, this command operates on the current paragraph.
If the mark is active, the region is used.  However, if the beginning and end
of the region are not at paragraph breaks, they are moved to the beginning and
end of the paragraphs they are in.
If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
breaks.

When calling from a program, operates just on region between BEGIN and END,
unless optional fourth arg WHOLE-PAR is non-nil.  In that case bounds are
extended to include entire paragraphs as in the interactive command."
452 453
  (interactive (list (if mark-active (region-beginning) (point))
		     (if mark-active (region-end) (point))
454
		     (let ((s (completing-read
455
			       "Set justification to: "
456 457
			       '(("left") ("right") ("full")
				 ("center") ("none"))
458
			       nil t)))
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		       (if (equal s "") (error ""))
		       (intern s))
		     t))
  (save-excursion
    (save-restriction
      (if whole-par
	  (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
		(paragraph-ignore-fill-prefix (if use-hard-newlines t 
						paragraph-ignore-fill-prefix)))
	    (goto-char begin)
	    (while (and (bolp) (not (eobp))) (forward-char 1))
	    (backward-paragraph)
	    (setq begin (point))
	    (goto-char end)
	    (skip-chars-backward " \t\n" begin)
	    (forward-paragraph)
	    (setq end (point))))

      (narrow-to-region (point-min) end)
      (unjustify-region begin (point-max))
      (put-text-property begin (point-max) 'justification value)
      (fill-region begin (point-max) nil t))))
481 482 483 484

(defun set-justification-none (b e)
  "Disable automatic filling for paragraphs in the region.
If the mark is not active, this applies to the current paragraph."
485 486 487
  (interactive (list (if mark-active (region-beginning) (point))
		     (if mark-active (region-end) (point))))
  (set-justification b e 'none t))
488 489 490

(defun set-justification-left (b e)
  "Make paragraphs in the region left-justified.
491
This is usually the default, but see the variable `default-justification'.
492
If the mark is not active, this applies to the current paragraph."
493 494 495
  (interactive (list (if mark-active (region-beginning) (point))
		     (if mark-active (region-end) (point))))
  (set-justification b e 'left t))
496 497 498 499 500

(defun set-justification-right (b e)
  "Make paragraphs in the region right-justified:
Flush at the right margin and ragged on the left.
If the mark is not active, this applies to the current paragraph."
501 502 503
  (interactive (list (if mark-active (region-beginning) (point))
		     (if mark-active (region-end) (point))))
  (set-justification b e 'right t))
504 505 506

(defun set-justification-full (b e)
  "Make paragraphs in the region fully justified:
507
This makes lines flush on both margins by inserting spaces between words.
508
If the mark is not active, this applies to the current paragraph."
509 510 511
  (interactive (list (if mark-active (region-beginning) (point))
		     (if mark-active (region-end) (point))))
  (set-justification b e 'full t))
512 513 514 515

(defun set-justification-center (b e)
  "Make paragraphs in the region centered.
If the mark is not active, this applies to the current paragraph."
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
  (interactive (list (if mark-active (region-beginning) (point))
		     (if mark-active (region-end) (point))))
  (set-justification b e 'center t))

;; A line has up to six parts:
;;
;;           >>>                    hello.  		       
;; [Indent-1][FP][    Indent-2     ][text][trailing whitespace][newline]
;;
;; "Indent-1" is the left-margin indentation; normally it ends at column
;;     given by the `current-left-margin' function.
;; "FP" is the fill-prefix.  It can be any string, including whitespace.
;; "Indent-2" is added to justify a line if the `current-justification' is
;;     `center' or `right'.  In `left' and `full' justification regions, any
;;     whitespace there is part of the line's text, and should not be changed.
;; Trailing whitespace is not counted as part of the line length when
;; center- or right-justifying.
;;
;; All parts of the line are optional, although the final newline can 
;;     only be missing on the last line of the buffer.
536 537

(defun justify-current-line (&optional how eop nosqueeze)
538 539 540
  "Do some kind of justification on this line.
Normally does full justification: adds spaces to the line to make it end at
the column given by `current-fill-column'.
541 542
Optional first argument HOW specifies alternate type of justification:
it can be `left', `right', `full', `center', or `none'.  
543 544
If HOW is t, will justify however the `current-justification' function says to.
If HOW is nil or missing, full justification is done by default.
545 546 547 548
Second arg EOP non-nil means that this is the last line of the paragraph, so
it will not be stretched by full justification.
Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
otherwise it is made canonical."
549
  (interactive)
550
  (if (eq t how) (setq how (or (current-justification) 'none)))
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
  (if (null how) (setq how 'full))
  (or (memq how '(none left))  ; No action required for these.
      (let ((fc (current-fill-column))
	    (pos (point-marker))
	    fp-end			; point at end of fill prefix
	    beg				; point at beginning of line's text
	    end				; point at end of line's text
	    indent			; column of `beg'
	    endcol			; column of `end'
	    ncols)			; new indent point or offset
	(end-of-line)
	;; Check if this is the last line of the paragraph.
	(if (and use-hard-newlines (null eop) 
		 (get-text-property (point) 'hard))
	    (setq eop t))
	(skip-chars-backward " \t")
	;; Quick exit if it appears to be properly justified already
	;; or there is no text.
	(if (or (bolp)
		(and (memq how '(full right))
		     (= (current-column) fc)))
	    nil
	  (setq end (point))
	  (beginning-of-line)
	  (skip-chars-forward " \t")
	  ;; Skip over fill-prefix.
	  (if (and fill-prefix 
		   (not (string-equal fill-prefix ""))
		   (equal fill-prefix
			  (buffer-substring 
			   (point) (min (point-max) (+ (length fill-prefix)
						       (point))))))
	      (forward-char (length fill-prefix))
	    (if (and adaptive-fill-mode 
		     (looking-at adaptive-fill-regexp))
		(goto-char (match-end 0))))
	  (setq fp-end (point))
	  (skip-chars-forward " \t")
	  ;; This is beginning of the line's text.
	  (setq indent (current-column))
	  (setq beg (point))
	  (goto-char end)
	  (setq endcol (current-column))

	  ;; HOW can't be null or left--we would have exited already
	  (cond ((eq 'right how) 
		 (setq ncols (- fc endcol))
		 (if (< ncols 0)
		     ;; Need to remove some indentation
		     (delete-region 
		      (progn (goto-char fp-end)
			     (if (< (current-column) (+ indent ncols))
				 (move-to-column (+ indent ncols) t))
			     (point))
		      (progn (move-to-column indent) (point)))
		   ;; Need to add some
		   (goto-char beg)
		   (indent-to (+ indent ncols))
		   ;; If point was at beginning of text, keep it there.
		   (if (= beg pos) 
		       (move-marker pos (point)))))

		((eq 'center how)
		 ;; Figure out how much indentation is needed
		 (setq ncols (+ (current-left-margin)
				(/ (- fc (current-left-margin) ;avail. space
				      (- endcol indent)) ;text width
				   2)))
		 (if (< ncols indent)
		     ;; Have too much indentation - remove some
		     (delete-region
		      (progn (goto-char fp-end)
			     (if (< (current-column) ncols)
				 (move-to-column ncols t))
			     (point))
		      (progn (move-to-column indent) (point)))
		   ;; Have too little - add some
		   (goto-char beg)
		   (indent-to ncols)
		   ;; If point was at beginning of text, keep it there.
		   (if (= beg pos)
		       (move-marker pos (point)))))

		((eq 'full how)
		 ;; Insert extra spaces between words to justify line
		 (save-restriction
637 638 639 640
		   (narrow-to-region beg end)
		   (or nosqueeze
		       (canonically-space-region beg end))
		   (goto-char (point-max))
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
		   (setq ncols (- fc endcol))
		   ;; Ncols is number of additional spaces needed
		   (if (> ncols 0)
		       (if (and (not eop)
				(search-backward " " nil t))
			   (while (> ncols 0)
			     (let ((nmove (+ 3 (random 3))))
			       (while (> nmove 0)
				 (or (search-backward " " nil t)
				     (progn
				       (goto-char (point-max))
				       (search-backward " ")))
				 (skip-chars-backward " ")
				 (setq nmove (1- nmove))))
			     (insert-and-inherit " ")
			     (skip-chars-backward " ")
			     (setq ncols (1- ncols)))))))
		(t (error "Unknown justification value"))))
	(goto-char pos)
	(move-marker pos nil)))
661
  nil)
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
(defun unjustify-current-line ()
  "Remove justification whitespace from current line.
If the line is centered or right-justified, this function removes any
indentation past the left margin.  If the line is full-jusitified, it removes
extra spaces between words.  It does nothing in other justification modes."
  (let ((justify (current-justification)))
    (cond ((eq 'left justify) nil)
	  ((eq  nil  justify) nil)
	  ((eq 'full justify)		; full justify: remove extra spaces
	   (beginning-of-line-text)
	   (canonically-space-region
	    (point) (save-excursion (end-of-line) (point))))
	  ((memq justify '(center right))
	   (save-excursion
	     (move-to-left-margin nil t)
	     ;; Position ourselves after any fill-prefix.
	     (if (and fill-prefix 
		      (not (string-equal fill-prefix ""))
		      (equal fill-prefix
			     (buffer-substring 
			      (point) (min (point-max) (+ (length fill-prefix)
							  (point))))))
		 (forward-char (length fill-prefix)))
	     (delete-region (point) (progn (skip-chars-forward " \t")
					   (point))))))))

(defun unjustify-region (&optional begin end)
  "Remove justification whitespace from region.
For centered or right-justified regions, this function removes any indentation
past the left margin from each line.  For full-jusitified lines, it removes 
extra spaces between words.  It does nothing in other justification modes.
Arguments BEGIN and END are optional; default is the whole buffer."
  (save-excursion
    (save-restriction
      (if end (narrow-to-region (point-min) end))
      (goto-char (or begin (point-min)))
      (while (not (eobp))
	(unjustify-current-line)
	(forward-line 1)))))

root's avatar
root committed
703

704 705 706 707 708 709 710 711
(defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
  "Fill paragraphs within the region, allowing varying indentation within each.
This command divides the region into \"paragraphs\",
only at paragraph-separator lines, then fills each paragraph
using as the fill prefix the smallest indentation of any line
in the paragraph.

When calling from a program, pass range to fill as first two arguments.
Jim Blandy's avatar
Jim Blandy committed
712

713 714
Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
JUSTIFY to justify paragraphs (prefix arg),
715 716 717 718 719
MAIL-FLAG for a mail message, i. e. don't fill header lines."
  (interactive "r\nP")
  (let ((fill-individual-varying-indent t))
    (fill-individual-paragraphs min max justifyp mailp)))

720
(defun fill-individual-paragraphs (min max &optional justify mailp)
721 722 723 724
  "Fill paragraphs of uniform indentation within the region.
This command divides the region into \"paragraphs\", 
treating every change in indentation level as a paragraph boundary,
then fills each paragraph using its indentation level as the fill prefix.
Jim Blandy's avatar
Jim Blandy committed
725 726 727

When calling from a program, pass range to fill as first two arguments.

728 729
Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
JUSTIFY to justify paragraphs (prefix arg),
root's avatar
root committed
730 731
MAIL-FLAG for a mail message, i. e. don't fill header lines."
  (interactive "r\nP")
Jim Blandy's avatar
Jim Blandy committed
732 733 734 735
  (save-restriction
    (save-excursion
      (goto-char min)
      (beginning-of-line)
736
      (narrow-to-region (point) max)
Jim Blandy's avatar
Jim Blandy committed
737
      (if mailp 
738 739 740
	  (while (and (not (eobp))
		      (or (looking-at "[ \t]*[^ \t\n]*:")
			  (looking-at "[ \t]*$")))
741 742 743
	    (if (looking-at "[ \t]*[^ \t\n]*:")
		(search-forward "\n\n" nil 'move)
	      (forward-line 1))))
Jim Blandy's avatar
Jim Blandy committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
      (narrow-to-region (point) max)
      ;; Loop over paragraphs.
      (while (progn (skip-chars-forward " \t\n") (not (eobp)))
	(beginning-of-line)
	(let ((start (point))
	      fill-prefix fill-prefix-regexp)
	  ;; Find end of paragraph, and compute the smallest fill-prefix
	  ;; that fits all the lines in this paragraph.
	  (while (progn
		   ;; Update the fill-prefix on the first line
		   ;; and whenever the prefix good so far is too long.
		   (if (not (and fill-prefix
				 (looking-at fill-prefix-regexp)))
		       (setq fill-prefix
			     (buffer-substring (point)
					       (save-excursion (skip-chars-forward " \t") (point)))
			     fill-prefix-regexp
			     (regexp-quote fill-prefix)))
		   (forward-line 1)
		   ;; Now stop the loop if end of paragraph.
		   (and (not (eobp))
Jim Blandy's avatar
Jim Blandy committed
765 766 767 768
			(if fill-individual-varying-indent
			    ;; If this line is a separator line, with or
			    ;; without prefix, end the paragraph.
			    (and 
Jim Blandy's avatar
Jim Blandy committed
769 770 771 772
			(not (looking-at paragraph-separate))
			(save-excursion
			  (not (and (looking-at fill-prefix-regexp)
				    (progn (forward-char (length fill-prefix))
Jim Blandy's avatar
Jim Blandy committed
773 774 775 776 777 778 779 780
						(looking-at paragraph-separate))))))
			  ;; If this line has more or less indent
			  ;; than the fill prefix wants, end the paragraph.
			  (and (looking-at fill-prefix-regexp)
			       (save-excursion
				 (not (progn (forward-char (length fill-prefix))
					     (or (looking-at paragraph-separate)
						 (looking-at paragraph-start))))))))))
Jim Blandy's avatar
Jim Blandy committed
781 782
	  ;; Fill this paragraph, but don't add a newline at the end.
	  (let ((had-newline (bolp)))
783
	    (fill-region-as-paragraph start (point) justify)
Jim Blandy's avatar
Jim Blandy committed
784
	    (or had-newline (delete-char -1))))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
785

Jim Blandy's avatar
Jim Blandy committed
786
;;; fill.el ends here