fill.el 14.4 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

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

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

(defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
  "*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.")

root's avatar
root committed
63
(defun fill-region-as-paragraph (from to &optional justify-flag)
64
  "Fill region as one paragraph: break lines to fit `fill-column'.
root's avatar
root committed
65
Prefix arg means justify too.
66 67
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
68 69
From program, pass args FROM, TO and JUSTIFY-FLAG."
  (interactive "r\nP")
70 71 72
  ;; 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)))
73
  ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
74
  (let ((fill-prefix fill-prefix))
75
    ;; Figure out how this paragraph is indented, if desired.
76 77
    (if (and adaptive-fill-mode
	     (or (null fill-prefix) (string= fill-prefix "")))
78 79 80 81 82 83 84 85 86 87
	(save-excursion
	  (goto-char (min from to))
	  (if (eolp) (forward-line 1))
	  (forward-line 1)
	  (if (< (point) (max from to))
	      (let ((start (point)))
		(re-search-forward adaptive-fill-regexp)
		(setq fill-prefix (buffer-substring start (point))))
	    (goto-char (min from to))
	    (if (eolp) (forward-line 1))
88
	    ;; If paragraph has only one line, don't assume in general
89
	    ;; that additional lines would have the same starting
90
	    ;; decoration.  Assume no indentation.
91 92 93 94 95 96
	    ;; But if left-margin is nonzero, we can assume ordinary
	    ;; lines do have indentation.
	    (if (> left-margin 0)
		(progn
		  (re-search-forward adaptive-fill-regexp)
		  (setq fill-prefix (make-string (current-column) ?\ ))))
97
	    )))
98 99 100 101 102 103 104 105 106 107 108 109 110

    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (skip-chars-forward "\n")
      (narrow-to-region (point) (point-max))
      (setq from (point))
      (goto-char (point-max))
      (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
		       (regexp-quote fill-prefix))))
	;; Delete the fill prefix from every line except the first.
	;; The first line may not even have a fill prefix.
	(and fpre
root's avatar
root committed
111
	     (progn
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	       (if (>= (length fill-prefix) fill-column)
		   (error "fill-prefix too long for specified width"))
	       (goto-char (point-min))
	       (forward-line 1)
	       (while (not (eobp))
		 (if (looking-at fpre)
		     (delete-region (point) (match-end 0)))
		 (forward-line 1))
	       (goto-char (point-min))
	       (and (looking-at fpre) (forward-char (length fill-prefix)))
	       (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.
127
      ;; loses on split abbrevs ("Mr.\nSmith")
128
      (goto-char from)
129
      (while (re-search-forward "[.?!][])}\"']*$" nil t)
130
	(insert ? ))
131 132

      ;; Then change all newlines to spaces.
133
      (subst-char-in-region from (point-max) ?\n ?\ )
134

135
      ;; Flush excess spaces, except in the paragraph indentation.
136
      (goto-char from)
137
      (skip-chars-forward " \t")
138 139 140 141
      ;; Nuke tabs while we're at it; 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.
142 143
      (subst-char-in-region (point) (point-max) ?\t ?\ )
      (while (re-search-forward "   *" nil t)
144 145
	(delete-region
	 (+ (match-beginning 0)
146 147 148 149
	    (if (and sentence-end-double-space
		     (save-excursion
		       (skip-chars-backward " ]})\"'")
		       (memq (preceding-char) '(?. ?? ?!))))
150 151 152 153
		2 1))
	 (match-end 0)))
      (goto-char (point-max))
      (delete-horizontal-space)
154
      (insert "  ")
155 156
      (goto-char (point-min))

157 158
      ;; This is the actual filling loop.
      (let ((prefixcol 0) linebeg)
159
	(while (not (eobp))
160
	  (setq linebeg (point))
161 162 163
	  (move-to-column (1+ fill-column))
	  (if (eobp)
	      nil
164
	    ;; Move back to start of word.
165
	    (skip-chars-backward "^ \n" linebeg)
166 167 168 169 170 171
	    ;; 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.
172 173 174 175 176 177 178
	    (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)))
179 180 181 182 183
	    (if (if (zerop prefixcol)
		    (save-excursion
		      (skip-chars-backward " " linebeg)
		      (bolp))
		  (>= prefixcol (current-column)))
184 185
		;; Keep at least one word even if fill prefix exceeds margin.
		;; This handles all but the first line of the paragraph.
186 187 188 189 190 191
		;; 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))
192
				       sentence-end-double-space
193
				       (save-excursion (forward-char -1)
194 195
						       (and (looking-at "\\. ")
							    (not (looking-at "\\.  ")))))))
196 197 198
		    (skip-chars-forward " ")
		    (skip-chars-forward "^ \n")
		    (setq first nil)))
199
	      ;; Normally, move back over the single space between the words.
200
	      (forward-char -1))
201 202 203 204 205 206
	    (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.
207 208 209 210 211
		;; Don't stop at a period followed by just one space.
		(let ((first t))
		  (while (and (not (eobp))
			      (or first
				  (and (not (bobp))
212
				       sentence-end-double-space
213
				       (save-excursion (forward-char -1)
214 215
						       (and (looking-at "\\. ")
							    (not (looking-at "\\.  ")))))))
216 217 218
		    (skip-chars-forward " ")
		    (skip-chars-forward "^ \n")
		    (setq first nil)))))
219 220 221
	  ;; Replace all whitespace here with one newline.
	  ;; Insert before deleting, so we don't forget which side of
	  ;; the whitespace point or markers used to be on.
222
	  (skip-chars-backward " ")
223 224
	  (insert ?\n)
	  (delete-horizontal-space)
225 226
	  ;; Insert the fill prefix at start of each line.
	  ;; Set prefixcol so whitespace in the prefix won't get lost.
227 228 229 230
	  (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
	       (progn
		 (insert fill-prefix)
		 (setq prefixcol (current-column))))
231
	  ;; Justify the line just ended, if desired.
232 233 234 235 236
	  (and justify-flag (not (eobp))
	       (progn
		 (forward-line -1)
		 (justify-current-line)
		 (forward-line 1))))))))
root's avatar
root committed
237 238

(defun fill-paragraph (arg)
239 240 241
  "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
space does not end a sentence, so don't break a line there."
root's avatar
root committed
242
  (interactive "P")
243 244 245 246 247 248 249 250
  (let ((before (point)))
    (save-excursion
      (forward-paragraph)
      (or (bolp) (newline 1))
      (let ((end (point))
	    (beg (progn (backward-paragraph) (point))))
	(goto-char before)
	(fill-region-as-paragraph beg end arg)))))
root's avatar
root committed
251 252 253

(defun fill-region (from to &optional justify-flag)
  "Fill each of the paragraphs in the region.
254 255 256
Prefix arg (non-nil third arg, if called from program) means justify as well.
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
257 258 259 260 261 262 263 264 265 266 267 268 269 270
  (interactive "r\nP")
  (save-restriction
   (narrow-to-region from to)
   (goto-char (point-min))
   (while (not (eobp))
     (let ((initial (point))
	   (end (progn
		 (forward-paragraph 1) (point))))
       (forward-paragraph -1)
       (if (>= (point) initial)
	   (fill-region-as-paragraph (point) end justify-flag)
	 (goto-char end))))))

(defun justify-current-line ()
271
  "Add spaces to line point is in, so it ends at `fill-column'."
root's avatar
root committed
272 273 274
  (interactive)
  (save-excursion
   (save-restriction
275
    (let (ncols beg indent)
root's avatar
root committed
276 277
      (beginning-of-line)
      (forward-char (length fill-prefix))
278
      (skip-chars-forward " \t")
279
      (setq indent (current-column))
root's avatar
root committed
280 281 282 283
      (setq beg (point))
      (end-of-line)
      (narrow-to-region beg (point))
      (goto-char beg)
284
      (while (re-search-forward "   *" nil t)
root's avatar
root committed
285 286 287
	(delete-region
	 (+ (match-beginning 0)
	    (if (save-excursion
288
		 (skip-chars-backward " ])\"'")
root's avatar
root committed
289 290 291 292
		 (memq (preceding-char) '(?. ?? ?!)))
		2 1))
	 (match-end 0)))
      (goto-char beg)
Richard M. Stallman's avatar
Richard M. Stallman committed
293
      (while (re-search-forward "[.?!][])\"']*\n" nil t)
root's avatar
root committed
294 295 296
	(forward-char -1)
	(insert ? ))
      (goto-char (point-max))
297 298 299
      ;; Note that the buffer bounds start after the indentation,
      ;; so the columns counted by INDENT don't appear in (current-column).
      (setq ncols (- fill-column (current-column) indent))
root's avatar
root committed
300 301 302 303 304 305 306 307 308 309 310 311
      (if (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 " ")
	    (skip-chars-backward " ")
312 313
	    (setq ncols (1- ncols)))))))
  nil)
root's avatar
root committed
314

315 316 317 318 319 320 321 322
(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
323

324 325 326 327 328 329 330 331 332 333 334 335
Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
JUSTIFY-FLAG to justify paragraphs (prefix arg),
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)))

(defun fill-individual-paragraphs (min max &optional justifyp mailp)
  "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
336 337 338

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

root's avatar
root committed
339 340 341 342
Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
JUSTIFY-FLAG to justify paragraphs (prefix arg),
MAIL-FLAG for a mail message, i. e. don't fill header lines."
  (interactive "r\nP")
Jim Blandy's avatar
Jim Blandy committed
343 344 345 346 347
  (save-restriction
    (save-excursion
      (goto-char min)
      (beginning-of-line)
      (if mailp 
348
	  (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$"))
349 350 351
	    (if (looking-at "[ \t]*[^ \t\n]*:")
		(search-forward "\n\n" nil 'move)
	      (forward-line 1))))
Jim Blandy's avatar
Jim Blandy committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
      (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
373 374 375 376
			(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
377 378 379 380
			(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
381 382 383 384 385 386 387 388
						(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
389 390 391
	  ;; Fill this paragraph, but don't add a newline at the end.
	  (let ((had-newline (bolp)))
	    (fill-region-as-paragraph start (point) justifyp)
Jim Blandy's avatar
Jim Blandy committed
392
	    (or had-newline (delete-char -1))))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
393

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