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