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