1 ;;; fill.el --- fill commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1992 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; All the commands for filling text. These are documented in the Emacs
30 (defconst fill-individual-varying-indent nil
31 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
32 Non-nil means changing indent doesn't end a paragraph.
33 That mode can handle paragraphs with extra indentation on the first line,
34 but it requires separator lines between paragraphs.
35 Nil means that any change in indentation starts a new paragraph.")
37 (defun set-fill-prefix ()
38 "Set the fill-prefix to the current line up to point.
39 Filling expects lines to start with the fill prefix and
40 reinserts the fill prefix in each resulting line."
42 (setq fill-prefix
(buffer-substring
43 (save-excursion (beginning-of-line) (point))
45 (if (equal fill-prefix
"")
46 (setq fill-prefix nil
))
48 (message "fill-prefix: \"%s\"" fill-prefix
)
49 (message "fill-prefix cancelled")))
51 (defconst adaptive-fill-mode t
52 "*Non-nil means determine a paragraph's fill prefix from its text.")
54 (defconst adaptive-fill-regexp
"[ \t]*\\([>*] +\\)?"
55 "*Regexp to match text at start of line that constitutes indentation.
56 If Adaptive Fill mode is enabled, whatever text matches this pattern
57 on the second line of a paragraph is used as the standard indentation
60 (defun fill-region-as-paragraph (from to
&optional justify-flag
)
61 "Fill region as one paragraph: break lines to fit fill-column.
62 Prefix arg means justify too.
63 From program, pass args FROM, TO and JUSTIFY-FLAG."
65 ;; Arrange for undoing the fill to restore point.
66 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
67 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
68 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
69 (let ((fill-prefix fill-prefix
))
70 ;; Figure out how this paragraph is indented, if desired.
71 (if (and adaptive-fill-mode
72 (or (null fill-prefix
) (string= fill-prefix
"")))
74 (goto-char (min from to
))
75 (if (eolp) (forward-line 1))
77 (if (< (point) (max from to
))
78 (let ((start (point)))
79 (re-search-forward adaptive-fill-regexp
)
80 (setq fill-prefix
(buffer-substring start
(point))))
81 (goto-char (min from to
))
82 (if (eolp) (forward-line 1))
83 ;; If paragraph has only one line, don't assume
84 ;; that additional lines would have the same starting
85 ;; decoration. Assume no indentation.
86 ;; (re-search-forward adaptive-fill-regexp)
87 ;; (setq fill-prefix (make-string (current-column) ?\ ))
91 (narrow-to-region from to
)
92 (goto-char (point-min))
93 (skip-chars-forward "\n")
94 (narrow-to-region (point) (point-max))
96 (goto-char (point-max))
97 (let ((fpre (and fill-prefix
(not (equal fill-prefix
""))
98 (regexp-quote fill-prefix
))))
99 ;; Delete the fill prefix from every line except the first.
100 ;; The first line may not even have a fill prefix.
103 (if (>= (length fill-prefix
) fill-column
)
104 (error "fill-prefix too long for specified width"))
105 (goto-char (point-min))
108 (if (looking-at fpre
)
109 (delete-region (point) (match-end 0)))
111 (goto-char (point-min))
112 (and (looking-at fpre
) (forward-char (length fill-prefix
)))
113 (setq from
(point)))))
114 ;; from is now before the text to fill,
115 ;; but after any fill prefix on the first line.
117 ;; Make sure sentences ending at end of line get an extra space.
118 ;; loses on split abbrevs ("Mr.\nSmith")
120 (while (re-search-forward "[.?!][])}\"']*$" nil t
)
123 ;; Then change all newlines to spaces.
124 (subst-char-in-region from
(point-max) ?
\n ?\
)
126 ;; Flush excess spaces, except in the paragraph indentation.
128 (skip-chars-forward " \t")
129 ;; nuke tabs while we're at it; they get screwed up in a fill
130 ;; this is quick, but loses when a sole tab follows the end of a sentence.
131 ;; actually, it is difficult to tell that from "Mr.\tSmith".
133 (subst-char-in-region (point) (point-max) ?
\t ?\
)
134 (while (re-search-forward " *" nil t
)
136 (+ (match-beginning 0)
138 (skip-chars-backward " ]})\"'")
139 (memq (preceding-char) '(?. ?? ?
!)))
142 (goto-char (point-max))
143 (delete-horizontal-space)
145 (goto-char (point-min))
147 ;; This is the actual filling loop.
148 (let ((prefixcol 0) linebeg
)
150 (setq linebeg
(point))
151 (move-to-column (1+ fill-column
))
154 ;; Move back to start of word.
155 (skip-chars-backward "^ \n" linebeg
)
156 ;; Don't break after a period followed by just one space.
157 ;; Move back to the previous place to break.
158 ;; The reason is that if a period ends up at the end of a line,
159 ;; further fills will assume it ends a sentence.
160 ;; If we now know it does not end a sentence,
161 ;; avoid putting it at the end of the line.
162 (while (and (> (point) (+ linebeg
2))
163 (eq (preceding-char) ?\
)
164 (eq (char-after (- (point) 2)) ?\.
))
166 (skip-chars-backward "^ \n" linebeg
))
167 (if (if (zerop prefixcol
) (bolp) (>= prefixcol
(current-column)))
168 ;; Keep at least one word even if fill prefix exceeds margin.
169 ;; This handles all but the first line of the paragraph.
171 (skip-chars-forward " ")
172 (skip-chars-forward "^ \n"))
173 ;; Normally, move back over the single space between the words.
175 (if (and fill-prefix
(zerop prefixcol
)
176 (< (- (point) (point-min)) (length fill-prefix
))
177 (string= (buffer-substring (point-min) (point))
178 (substring fill-prefix
0 (- (point) (point-min)))))
179 ;; Keep at least one word even if fill prefix exceeds margin.
180 ;; This handles the first line of the paragraph.
182 (skip-chars-forward " ")
183 (skip-chars-forward "^ \n")))
184 ;; Replace all whitespace here with one newline.
185 ;; Insert before deleting, so we don't forget which side of
186 ;; the whitespace point or markers used to be on.
187 (skip-chars-backward " ")
189 (delete-horizontal-space)
190 ;; Insert the fill prefix at start of each line.
191 ;; Set prefixcol so whitespace in the prefix won't get lost.
192 (and (not (eobp)) fill-prefix
(not (equal fill-prefix
""))
195 (setq prefixcol
(current-column))))
196 ;; Justify the line just ended, if desired.
197 (and justify-flag
(not (eobp))
200 (justify-current-line)
201 (forward-line 1))))))))
203 (defun fill-paragraph (arg)
204 "Fill paragraph at or after point. Prefix arg means justify as well."
206 (let ((before (point)))
209 (or (bolp) (newline 1))
211 (beg (progn (backward-paragraph) (point))))
213 (fill-region-as-paragraph beg end arg
)))))
215 (defun fill-region (from to
&optional justify-flag
)
216 "Fill each of the paragraphs in the region.
217 Prefix arg (non-nil third arg, if called from program) means justify as well."
220 (narrow-to-region from to
)
221 (goto-char (point-min))
223 (let ((initial (point))
225 (forward-paragraph 1) (point))))
226 (forward-paragraph -
1)
227 (if (>= (point) initial
)
228 (fill-region-as-paragraph (point) end justify-flag
)
231 (defun justify-current-line ()
232 "Add spaces to line point is in, so it ends at `fill-column'."
236 (let (ncols beg indent
)
238 (forward-char (length fill-prefix
))
239 (skip-chars-forward " \t")
240 (setq indent
(current-column))
243 (narrow-to-region beg
(point))
245 (while (re-search-forward " *" nil t
)
247 (+ (match-beginning 0)
249 (skip-chars-backward " ])\"'")
250 (memq (preceding-char) '(?. ?? ?
!)))
254 (while (re-search-forward "[.?!][])\"']*\n" nil t
)
257 (goto-char (point-max))
258 ;; Note that the buffer bounds start after the indentation,
259 ;; so the columns counted by INDENT don't appear in (current-column).
260 (setq ncols
(- fill-column
(current-column) indent
))
261 (if (search-backward " " nil t
)
263 (let ((nmove (+ 3 (random 3))))
265 (or (search-backward " " nil t
)
267 (goto-char (point-max))
268 (search-backward " ")))
269 (skip-chars-backward " ")
270 (setq nmove
(1- nmove
))))
272 (skip-chars-backward " ")
273 (setq ncols
(1- ncols
)))))))
276 (defun fill-individual-paragraphs (min max
&optional justifyp mailp
)
277 "Fill each paragraph in region according to its individual fill prefix.
279 If `fill-individual-varying-indent' is non-nil,
280 then a mere change in indentation does not end a paragraph. In this mode,
281 the indentation for a paragraph is the minimum indentation of any line in it.
283 When calling from a program, pass range to fill as first two arguments.
285 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
286 JUSTIFY-FLAG to justify paragraphs (prefix arg),
287 MAIL-FLAG for a mail message, i. e. don't fill header lines."
294 (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$"))
296 (narrow-to-region (point) max
)
297 ;; Loop over paragraphs.
298 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
300 (let ((start (point))
301 fill-prefix fill-prefix-regexp
)
302 ;; Find end of paragraph, and compute the smallest fill-prefix
303 ;; that fits all the lines in this paragraph.
305 ;; Update the fill-prefix on the first line
306 ;; and whenever the prefix good so far is too long.
307 (if (not (and fill-prefix
308 (looking-at fill-prefix-regexp
)))
310 (buffer-substring (point)
311 (save-excursion (skip-chars-forward " \t") (point)))
313 (regexp-quote fill-prefix
)))
315 ;; Now stop the loop if end of paragraph.
317 (if fill-individual-varying-indent
318 ;; If this line is a separator line, with or
319 ;; without prefix, end the paragraph.
321 (not (looking-at paragraph-separate
))
323 (not (and (looking-at fill-prefix-regexp
)
324 (progn (forward-char (length fill-prefix
))
325 (looking-at paragraph-separate
))))))
326 ;; If this line has more or less indent
327 ;; than the fill prefix wants, end the paragraph.
328 (and (looking-at fill-prefix-regexp
)
330 (not (progn (forward-char (length fill-prefix
))
331 (or (looking-at paragraph-separate
)
332 (looking-at paragraph-start
))))))))))
333 ;; Fill this paragraph, but don't add a newline at the end.
334 (let ((had-newline (bolp)))
335 (fill-region-as-paragraph start
(point) justifyp
)
336 (or had-newline
(delete-char -
1))))))))
338 ;;; fill.el ends here