Mercurial > emacs
annotate lisp/textmodes/fill.el @ 2011:eea183a35396
(compare-windows): Use compare-buffer-substrings.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 05 Mar 1993 20:37:00 +0000 |
parents | 955ebdb98095 |
children | 73485780a8f9 |
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 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
821
diff
changeset
|
3 ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
821
diff
changeset
|
4 |
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 |
75 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
75 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
23 ;;; Code: |
75 | 24 |
638 | 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 | |
75 | 32 (defun set-fill-prefix () |
33 "Set the fill-prefix to the current line up to point. | |
218 | 34 Filling expects lines to start with the fill prefix and |
35 reinserts the fill prefix in each resulting line." | |
75 | 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 | |
218 | 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 | |
75 | 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") | |
1332
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
60 ;; Arrange for undoing the fill to restore point. |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
61 (if (and buffer-undo-list (not (eq buffer-undo-list t))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
62 (setq buffer-undo-list (cons (point) buffer-undo-list))) |
218 | 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. | |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
66 (if (and adaptive-fill-mode |
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
67 (or (null fill-prefix) (string= fill-prefix ""))) |
218 | 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 | |
729
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
80 ;; decoration. Assume no indentation. |
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
81 ;; (re-search-forward adaptive-fill-regexp) |
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
82 ;; (setq fill-prefix (make-string (current-column) ?\ )) |
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
83 ))) |
75 | 84 |
218 | 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. | |
75 | 111 |
218 | 112 ;; Make sure sentences ending at end of line get an extra space. |
113 ;; loses on split abbrevs ("Mr.\nSmith") | |
114 (goto-char from) | |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
115 (while (re-search-forward "[.?!][])}\"']*$" nil t) |
218 | 116 (insert ? )) |
117 | |
118 ;; Then change all newlines to spaces. | |
119 (subst-char-in-region from (point-max) ?\n ?\ ) | |
75 | 120 |
218 | 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 | |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
133 (skip-chars-backward " ]})\"'") |
218 | 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 | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
142 ;; This is the actual filling loop. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
143 (let ((prefixcol 0) linebeg) |
218 | 144 (while (not (eobp)) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
145 (setq linebeg (point)) |
218 | 146 (move-to-column (1+ fill-column)) |
147 (if (eobp) | |
148 nil | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
149 ;; Move back to start of word. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
150 (skip-chars-backward "^ \n" linebeg) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
151 ;; 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
|
152 ;; Move back to the previous place to break. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
153 ;; 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
|
154 ;; further fills will assume it ends a sentence. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
155 ;; If we now know it does not end a sentence, |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
156 ;; avoid putting it at the end of the line. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
157 (while (and (> (point) (+ linebeg 2)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
158 (eq (preceding-char) ?\ ) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
159 (eq (char-after (- (point) 2)) ?\.)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
160 (forward-char -2) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
161 (skip-chars-backward "^ \n" linebeg)) |
218 | 162 (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
163 ;; 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
|
164 ;; This handles all but the first line of the paragraph. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
165 (progn |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
166 (skip-chars-forward " ") |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
167 (skip-chars-forward "^ \n")) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
168 ;; Normally, move back over the single space between the words. |
218 | 169 (forward-char -1))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
170 (if (and fill-prefix (zerop prefixcol) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
171 (< (- (point) (point-min)) (length fill-prefix)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
172 (string= (buffer-substring (point-min) (point)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
173 (substring fill-prefix 0 (- (point) (point-min))))) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
174 ;; 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
|
175 ;; This handles the first line of the paragraph. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
176 (progn |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
177 (skip-chars-forward " ") |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
178 (skip-chars-forward "^ \n"))) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
179 ;; Replace all whitespace here with one newline. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
180 ;; 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
|
181 ;; the whitespace point or markers used to be on. |
218 | 182 (skip-chars-backward " ") |
183 (insert ?\n) | |
184 (delete-horizontal-space) | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
185 ;; Insert the fill prefix at start of each line. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
186 ;; Set prefixcol so whitespace in the prefix won't get lost. |
218 | 187 (and (not (eobp)) fill-prefix (not (equal fill-prefix "")) |
188 (progn | |
189 (insert fill-prefix) | |
190 (setq prefixcol (current-column)))) | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
191 ;; Justify the line just ended, if desired. |
218 | 192 (and justify-flag (not (eobp)) |
193 (progn | |
194 (forward-line -1) | |
195 (justify-current-line) | |
196 (forward-line 1)))))))) | |
75 | 197 |
198 (defun fill-paragraph (arg) | |
218 | 199 "Fill paragraph at or after point. Prefix arg means justify as well." |
75 | 200 (interactive "P") |
1332
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
201 (let ((before (point))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
202 (save-excursion |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
203 (forward-paragraph) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
204 (or (bolp) (newline 1)) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
205 (let ((end (point)) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
206 (beg (progn (backward-paragraph) (point)))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
207 (goto-char before) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
208 (fill-region-as-paragraph beg end arg))))) |
75 | 209 |
210 (defun fill-region (from to &optional justify-flag) | |
211 "Fill each of the paragraphs in the region. | |
218 | 212 Prefix arg (non-nil third arg, if called from program) means justify as well." |
75 | 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 () | |
218 | 227 "Add spaces to line point is in, so it ends at `fill-column'." |
75 | 228 (interactive) |
229 (save-excursion | |
230 (save-restriction | |
218 | 231 (let (ncols beg indent) |
75 | 232 (beginning-of-line) |
233 (forward-char (length fill-prefix)) | |
234 (skip-chars-forward " \t") | |
218 | 235 (setq indent (current-column)) |
75 | 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) | |
1067 | 249 (while (re-search-forward "[.?!][])\"']*\n" nil t) |
75 | 250 (forward-char -1) |
251 (insert ? )) | |
252 (goto-char (point-max)) | |
218 | 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)) | |
75 | 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 | |
270 (defun fill-individual-paragraphs (min max &optional justifyp mailp) | |
271 "Fill each paragraph in region according to its individual fill prefix. | |
638 | 272 |
273 If `fill-individual-varying-indent' is non-nil, | |
274 then a mere change in indentation does not end a paragraph. In this mode, | |
275 the indentation for a paragraph is the minimum indentation of any line in it. | |
276 | |
277 When calling from a program, pass range to fill as first two arguments. | |
278 | |
75 | 279 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG: |
280 JUSTIFY-FLAG to justify paragraphs (prefix arg), | |
281 MAIL-FLAG for a mail message, i. e. don't fill header lines." | |
282 (interactive "r\nP") | |
474 | 283 (save-restriction |
284 (save-excursion | |
285 (goto-char min) | |
286 (beginning-of-line) | |
287 (if mailp | |
821
cee415e2dae8
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
811
diff
changeset
|
288 (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$")) |
474 | 289 (forward-line 1))) |
290 (narrow-to-region (point) max) | |
291 ;; Loop over paragraphs. | |
292 (while (progn (skip-chars-forward " \t\n") (not (eobp))) | |
293 (beginning-of-line) | |
294 (let ((start (point)) | |
295 fill-prefix fill-prefix-regexp) | |
296 ;; Find end of paragraph, and compute the smallest fill-prefix | |
297 ;; that fits all the lines in this paragraph. | |
298 (while (progn | |
299 ;; Update the fill-prefix on the first line | |
300 ;; and whenever the prefix good so far is too long. | |
301 (if (not (and fill-prefix | |
302 (looking-at fill-prefix-regexp))) | |
303 (setq fill-prefix | |
304 (buffer-substring (point) | |
305 (save-excursion (skip-chars-forward " \t") (point))) | |
306 fill-prefix-regexp | |
307 (regexp-quote fill-prefix))) | |
308 (forward-line 1) | |
309 ;; Now stop the loop if end of paragraph. | |
310 (and (not (eobp)) | |
638 | 311 (if fill-individual-varying-indent |
312 ;; If this line is a separator line, with or | |
313 ;; without prefix, end the paragraph. | |
314 (and | |
474 | 315 (not (looking-at paragraph-separate)) |
316 (save-excursion | |
317 (not (and (looking-at fill-prefix-regexp) | |
318 (progn (forward-char (length fill-prefix)) | |
638 | 319 (looking-at paragraph-separate)))))) |
320 ;; If this line has more or less indent | |
321 ;; than the fill prefix wants, end the paragraph. | |
322 (and (looking-at fill-prefix-regexp) | |
323 (save-excursion | |
324 (not (progn (forward-char (length fill-prefix)) | |
325 (or (looking-at paragraph-separate) | |
326 (looking-at paragraph-start)))))))))) | |
474 | 327 ;; Fill this paragraph, but don't add a newline at the end. |
328 (let ((had-newline (bolp))) | |
329 (fill-region-as-paragraph start (point) justifyp) | |
638 | 330 (or had-newline (delete-char -1)))))))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
331 |
732 | 332 ;;; fill.el ends here |