*** empty log message ***
[emacs.git] / lisp / textmodes / fill.el
blob85a9c1dd2e4fd94f1b7aef5d018a1956c8d1ed97
1 ;;; fill.el --- fill commands for Emacs
3 ;; Maintainer: FSF
4 ;; Last-Modified: 24 Jun 1992
6 ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Code:
26 (defconst fill-individual-varying-indent nil
27 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
28 Non-nil means changing indent doesn't end a paragraph.
29 That mode can handle paragraphs with extra indentation on the first line,
30 but it requires separator lines between paragraphs.
31 Nil means that any change in indentation starts a new paragraph.")
33 (defun set-fill-prefix ()
34 "Set the fill-prefix to the current line up to point.
35 Filling expects lines to start with the fill prefix and
36 reinserts the fill prefix in each resulting line."
37 (interactive)
38 (setq fill-prefix (buffer-substring
39 (save-excursion (beginning-of-line) (point))
40 (point)))
41 (if (equal fill-prefix "")
42 (setq fill-prefix nil))
43 (if fill-prefix
44 (message "fill-prefix: \"%s\"" fill-prefix)
45 (message "fill-prefix cancelled")))
47 (defconst adaptive-fill-mode t
48 "*Non-nil means determine a paragraph's fill prefix from its text.")
50 (defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
51 "*Regexp to match text at start of line that constitutes indentation.
52 If Adaptive Fill mode is enabled, whatever text matches this pattern
53 on the second line of a paragraph is used as the standard indentation
54 for the paragraph.")
56 (defun fill-region-as-paragraph (from to &optional justify-flag)
57 "Fill region as one paragraph: break lines to fit fill-column.
58 Prefix arg means justify too.
59 From program, pass args FROM, TO and JUSTIFY-FLAG."
60 (interactive "r\nP")
61 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
62 (let ((fill-prefix fill-prefix))
63 ;; Figure out how this paragraph is indented, if desired.
64 (if (and adaptive-fill-mode
65 (or (null fill-prefix) (string= fill-prefix "")))
66 (save-excursion
67 (goto-char (min from to))
68 (if (eolp) (forward-line 1))
69 (forward-line 1)
70 (if (< (point) (max from to))
71 (let ((start (point)))
72 (re-search-forward adaptive-fill-regexp)
73 (setq fill-prefix (buffer-substring start (point))))
74 (goto-char (min from to))
75 (if (eolp) (forward-line 1))
76 ;; If paragraph has only one line, don't assume
77 ;; that additional lines would have the same starting
78 ;; decoration. Assume no indentation.
79 ;; (re-search-forward adaptive-fill-regexp)
80 ;; (setq fill-prefix (make-string (current-column) ?\ ))
81 )))
83 (save-restriction
84 (narrow-to-region from to)
85 (goto-char (point-min))
86 (skip-chars-forward "\n")
87 (narrow-to-region (point) (point-max))
88 (setq from (point))
89 (goto-char (point-max))
90 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
91 (regexp-quote fill-prefix))))
92 ;; Delete the fill prefix from every line except the first.
93 ;; The first line may not even have a fill prefix.
94 (and fpre
95 (progn
96 (if (>= (length fill-prefix) fill-column)
97 (error "fill-prefix too long for specified width"))
98 (goto-char (point-min))
99 (forward-line 1)
100 (while (not (eobp))
101 (if (looking-at fpre)
102 (delete-region (point) (match-end 0)))
103 (forward-line 1))
104 (goto-char (point-min))
105 (and (looking-at fpre) (forward-char (length fill-prefix)))
106 (setq from (point)))))
107 ;; from is now before the text to fill,
108 ;; but after any fill prefix on the first line.
110 ;; Make sure sentences ending at end of line get an extra space.
111 ;; loses on split abbrevs ("Mr.\nSmith")
112 (goto-char from)
113 (while (re-search-forward "[.?!][])}\"']*$" nil t)
114 (insert ? ))
116 ;; Then change all newlines to spaces.
117 (subst-char-in-region from (point-max) ?\n ?\ )
119 ;; Flush excess spaces, except in the paragraph indentation.
120 (goto-char from)
121 (skip-chars-forward " \t")
122 ;; nuke tabs while we're at it; they get screwed up in a fill
123 ;; this is quick, but loses when a sole tab follows the end of a sentence.
124 ;; actually, it is difficult to tell that from "Mr.\tSmith".
125 ;; blame the typist.
126 (subst-char-in-region (point) (point-max) ?\t ?\ )
127 (while (re-search-forward " *" nil t)
128 (delete-region
129 (+ (match-beginning 0)
130 (if (save-excursion
131 (skip-chars-backward " ]})\"'")
132 (memq (preceding-char) '(?. ?? ?!)))
133 2 1))
134 (match-end 0)))
135 (goto-char (point-max))
136 (delete-horizontal-space)
137 (insert " ")
138 (goto-char (point-min))
140 ;; This is the actual filling loop.
141 (let ((prefixcol 0) linebeg)
142 (while (not (eobp))
143 (setq linebeg (point))
144 (move-to-column (1+ fill-column))
145 (if (eobp)
147 ;; Move back to start of word.
148 (skip-chars-backward "^ \n" linebeg)
149 ;; Don't break after a period followed by just one space.
150 ;; Move back to the previous place to break.
151 ;; The reason is that if a period ends up at the end of a line,
152 ;; further fills will assume it ends a sentence.
153 ;; If we now know it does not end a sentence,
154 ;; avoid putting it at the end of the line.
155 (while (and (> (point) (+ linebeg 2))
156 (eq (preceding-char) ?\ )
157 (eq (char-after (- (point) 2)) ?\.))
158 (forward-char -2)
159 (skip-chars-backward "^ \n" linebeg))
160 (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
161 ;; Keep at least one word even if fill prefix exceeds margin.
162 ;; This handles all but the first line of the paragraph.
163 (progn
164 (skip-chars-forward " ")
165 (skip-chars-forward "^ \n"))
166 ;; Normally, move back over the single space between the words.
167 (forward-char -1)))
168 (if (and fill-prefix (zerop prefixcol)
169 (< (- (point) (point-min)) (length fill-prefix))
170 (string= (buffer-substring (point-min) (point))
171 (substring fill-prefix 0 (- (point) (point-min)))))
172 ;; Keep at least one word even if fill prefix exceeds margin.
173 ;; This handles the first line of the paragraph.
174 (progn
175 (skip-chars-forward " ")
176 (skip-chars-forward "^ \n")))
177 ;; Replace all whitespace here with one newline.
178 ;; Insert before deleting, so we don't forget which side of
179 ;; the whitespace point or markers used to be on.
180 (skip-chars-backward " ")
181 (insert ?\n)
182 (delete-horizontal-space)
183 ;; Insert the fill prefix at start of each line.
184 ;; Set prefixcol so whitespace in the prefix won't get lost.
185 (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
186 (progn
187 (insert fill-prefix)
188 (setq prefixcol (current-column))))
189 ;; Justify the line just ended, if desired.
190 (and justify-flag (not (eobp))
191 (progn
192 (forward-line -1)
193 (justify-current-line)
194 (forward-line 1))))))))
196 (defun fill-paragraph (arg)
197 "Fill paragraph at or after point. Prefix arg means justify as well."
198 (interactive "P")
199 (save-excursion
200 (forward-paragraph)
201 (or (bolp) (newline 1))
202 (let ((end (point)))
203 (backward-paragraph)
204 (fill-region-as-paragraph (point) end arg))))
206 (defun fill-region (from to &optional justify-flag)
207 "Fill each of the paragraphs in the region.
208 Prefix arg (non-nil third arg, if called from program) means justify as well."
209 (interactive "r\nP")
210 (save-restriction
211 (narrow-to-region from to)
212 (goto-char (point-min))
213 (while (not (eobp))
214 (let ((initial (point))
215 (end (progn
216 (forward-paragraph 1) (point))))
217 (forward-paragraph -1)
218 (if (>= (point) initial)
219 (fill-region-as-paragraph (point) end justify-flag)
220 (goto-char end))))))
222 (defun justify-current-line ()
223 "Add spaces to line point is in, so it ends at `fill-column'."
224 (interactive)
225 (save-excursion
226 (save-restriction
227 (let (ncols beg indent)
228 (beginning-of-line)
229 (forward-char (length fill-prefix))
230 (skip-chars-forward " \t")
231 (setq indent (current-column))
232 (setq beg (point))
233 (end-of-line)
234 (narrow-to-region beg (point))
235 (goto-char beg)
236 (while (re-search-forward " *" nil t)
237 (delete-region
238 (+ (match-beginning 0)
239 (if (save-excursion
240 (skip-chars-backward " ])\"'")
241 (memq (preceding-char) '(?. ?? ?!)))
242 2 1))
243 (match-end 0)))
244 (goto-char beg)
245 (while (re-search-forward "[.?!][])""']*\n" nil t)
246 (forward-char -1)
247 (insert ? ))
248 (goto-char (point-max))
249 ;; Note that the buffer bounds start after the indentation,
250 ;; so the columns counted by INDENT don't appear in (current-column).
251 (setq ncols (- fill-column (current-column) indent))
252 (if (search-backward " " nil t)
253 (while (> ncols 0)
254 (let ((nmove (+ 3 (random 3))))
255 (while (> nmove 0)
256 (or (search-backward " " nil t)
257 (progn
258 (goto-char (point-max))
259 (search-backward " ")))
260 (skip-chars-backward " ")
261 (setq nmove (1- nmove))))
262 (insert " ")
263 (skip-chars-backward " ")
264 (setq ncols (1- ncols))))))))
266 (defun fill-individual-paragraphs (min max &optional justifyp mailp)
267 "Fill each paragraph in region according to its individual fill prefix.
269 If `fill-individual-varying-indent' is non-nil,
270 then a mere change in indentation does not end a paragraph. In this mode,
271 the indentation for a paragraph is the minimum indentation of any line in it.
273 When calling from a program, pass range to fill as first two arguments.
275 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
276 JUSTIFY-FLAG to justify paragraphs (prefix arg),
277 MAIL-FLAG for a mail message, i. e. don't fill header lines."
278 (interactive "r\nP")
279 (save-restriction
280 (save-excursion
281 (goto-char min)
282 (beginning-of-line)
283 (if mailp
284 (while (looking-at "[^ \t\n]*:")
285 (forward-line 1)))
286 (narrow-to-region (point) max)
287 ;; Loop over paragraphs.
288 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
289 (beginning-of-line)
290 (let ((start (point))
291 fill-prefix fill-prefix-regexp)
292 ;; Find end of paragraph, and compute the smallest fill-prefix
293 ;; that fits all the lines in this paragraph.
294 (while (progn
295 ;; Update the fill-prefix on the first line
296 ;; and whenever the prefix good so far is too long.
297 (if (not (and fill-prefix
298 (looking-at fill-prefix-regexp)))
299 (setq fill-prefix
300 (buffer-substring (point)
301 (save-excursion (skip-chars-forward " \t") (point)))
302 fill-prefix-regexp
303 (regexp-quote fill-prefix)))
304 (forward-line 1)
305 ;; Now stop the loop if end of paragraph.
306 (and (not (eobp))
307 (if fill-individual-varying-indent
308 ;; If this line is a separator line, with or
309 ;; without prefix, end the paragraph.
310 (and
311 (not (looking-at paragraph-separate))
312 (save-excursion
313 (not (and (looking-at fill-prefix-regexp)
314 (progn (forward-char (length fill-prefix))
315 (looking-at paragraph-separate))))))
316 ;; If this line has more or less indent
317 ;; than the fill prefix wants, end the paragraph.
318 (and (looking-at fill-prefix-regexp)
319 (save-excursion
320 (not (progn (forward-char (length fill-prefix))
321 (or (looking-at paragraph-separate)
322 (looking-at paragraph-start))))))))))
323 ;; Fill this paragraph, but don't add a newline at the end.
324 (let ((had-newline (bolp)))
325 (fill-region-as-paragraph start (point) justifyp)
326 (or had-newline (delete-char -1))))))))
328 ;;; fill.el ends here