Mercurial > emacs
annotate lisp/textmodes/fill.el @ 5549:b36de4d370be
(telnet): Fix previous change--tentative idea
wasn't fully removed.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 10 Jan 1994 18:15:22 +0000 |
parents | 872d39813f70 |
children | 92dd602768d2 |
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 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
23 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
24 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
25 ;; All the commands for filling text. These are documented in the Emacs |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
26 ;; manual. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
27 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
28 ;;; Code: |
75 | 29 |
638 | 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.") | |
36 | |
75 | 37 (defun set-fill-prefix () |
38 "Set the fill-prefix to the current line up to point. | |
218 | 39 Filling expects lines to start with the fill prefix and |
40 reinserts the fill prefix in each resulting line." | |
75 | 41 (interactive) |
42 (setq fill-prefix (buffer-substring | |
43 (save-excursion (beginning-of-line) (point)) | |
44 (point))) | |
45 (if (equal fill-prefix "") | |
46 (setq fill-prefix nil)) | |
47 (if fill-prefix | |
48 (message "fill-prefix: \"%s\"" fill-prefix) | |
49 (message "fill-prefix cancelled"))) | |
50 | |
218 | 51 (defconst adaptive-fill-mode t |
52 "*Non-nil means determine a paragraph's fill prefix from its text.") | |
53 | |
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 | |
58 for the paragraph.") | |
59 | |
75 | 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." | |
64 (interactive "r\nP") | |
1332
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
65 ;; 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
|
66 (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
|
67 (setq buffer-undo-list (cons (point) buffer-undo-list))) |
218 | 68 ;; Don't let Adaptive Fill mode alter the fill prefix permanently. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
69 (let ((fill-prefix fill-prefix)) |
218 | 70 ;; Figure out how this paragraph is indented, if desired. |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
71 (if (and adaptive-fill-mode |
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
72 (or (null fill-prefix) (string= fill-prefix ""))) |
218 | 73 (save-excursion |
74 (goto-char (min from to)) | |
75 (if (eolp) (forward-line 1)) | |
76 (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 | |
729
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
85 ;; decoration. Assume no indentation. |
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
86 ;; (re-search-forward adaptive-fill-regexp) |
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
87 ;; (setq fill-prefix (make-string (current-column) ?\ )) |
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
88 ))) |
75 | 89 |
218 | 90 (save-restriction |
91 (narrow-to-region from to) | |
92 (goto-char (point-min)) | |
93 (skip-chars-forward "\n") | |
94 (narrow-to-region (point) (point-max)) | |
95 (setq from (point)) | |
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. | |
101 (and fpre | |
102 (progn | |
103 (if (>= (length fill-prefix) fill-column) | |
104 (error "fill-prefix too long for specified width")) | |
105 (goto-char (point-min)) | |
106 (forward-line 1) | |
107 (while (not (eobp)) | |
108 (if (looking-at fpre) | |
109 (delete-region (point) (match-end 0))) | |
110 (forward-line 1)) | |
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. | |
75 | 116 |
218 | 117 ;; Make sure sentences ending at end of line get an extra space. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
118 ;; loses on split abbrevs ("Mr.\nSmith") |
218 | 119 (goto-char from) |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
120 (while (re-search-forward "[.?!][])}\"']*$" nil t) |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
121 (insert ? )) |
218 | 122 |
123 ;; Then change all newlines to spaces. | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
124 (subst-char-in-region from (point-max) ?\n ?\ ) |
2376
f7a3795bd1c5
(fill-paragraph, justify-current-line) Now uses the skip-syntax-
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2307
diff
changeset
|
125 |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
126 ;; Flush excess spaces, except in the paragraph indentation. |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
127 (goto-char from) |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
128 (skip-chars-forward " \t") |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
129 ;; nuke tabs while we're at it; they get screwed up in a fill |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
130 ;; this is quick, but loses when a sole tab follows the end of a sentence. |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
131 ;; actually, it is difficult to tell that from "Mr.\tSmith". |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
132 ;; blame the typist. |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
133 (subst-char-in-region (point) (point-max) ?\t ?\ ) |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
134 (while (re-search-forward " *" nil t) |
218 | 135 (delete-region |
136 (+ (match-beginning 0) | |
137 (if (save-excursion | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
138 (skip-chars-backward " ]})\"'") |
218 | 139 (memq (preceding-char) '(?. ?? ?!))) |
140 2 1)) | |
141 (match-end 0))) | |
142 (goto-char (point-max)) | |
143 (delete-horizontal-space) | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
144 (insert " ") |
218 | 145 (goto-char (point-min)) |
146 | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
147 ;; This is the actual filling loop. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
148 (let ((prefixcol 0) linebeg) |
218 | 149 (while (not (eobp)) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
150 (setq linebeg (point)) |
218 | 151 (move-to-column (1+ fill-column)) |
152 (if (eobp) | |
153 nil | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
154 ;; Move back to start of word. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
155 (skip-chars-backward "^ \n" linebeg) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
156 ;; 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
|
157 ;; Move back to the previous place to break. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
158 ;; 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
|
159 ;; further fills will assume it ends a sentence. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
160 ;; If we now know it does not end a sentence, |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
161 ;; avoid putting it at the end of the line. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
162 (while (and (> (point) (+ linebeg 2)) |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
163 (eq (preceding-char) ?\ ) |
4352
d00889d02f75
(fill-region-as-paragraph): Move misplaced paren
Richard M. Stallman <rms@gnu.org>
parents:
2520
diff
changeset
|
164 (not (eq (following-char) ?\ )) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
165 (eq (char-after (- (point) 2)) ?\.)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
166 (forward-char -2) |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
167 (skip-chars-backward "^ \n" linebeg)) |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
168 (if (if (zerop prefixcol) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
169 (save-excursion |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
170 (skip-chars-backward " " linebeg) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
171 (bolp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
172 (>= prefixcol (current-column))) |
719
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 all but the first line of the paragraph. |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
175 ;; Meanwhile, don't stop at a period followed by one space. |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
176 (let ((first t)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
177 (move-to-column prefixcol) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
178 (while (and (not (eobp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
179 (or first |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
180 (and (not (bobp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
181 (save-excursion (forward-char -1) |
5291
872d39813f70
(fill-region-as-paragraph): Do allow breaking line at a period
Richard M. Stallman <rms@gnu.org>
parents:
4992
diff
changeset
|
182 (looking-at "\\. ") |
872d39813f70
(fill-region-as-paragraph): Do allow breaking line at a period
Richard M. Stallman <rms@gnu.org>
parents:
4992
diff
changeset
|
183 (not (looking-at "\\. ")))))) |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
184 (skip-chars-forward " ") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
185 (skip-chars-forward "^ \n") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
186 (setq first nil))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
187 ;; Normally, move back over the single space between the words. |
4352
d00889d02f75
(fill-region-as-paragraph): Move misplaced paren
Richard M. Stallman <rms@gnu.org>
parents:
2520
diff
changeset
|
188 (forward-char -1)) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
189 (if (and fill-prefix (zerop prefixcol) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
190 (< (- (point) (point-min)) (length fill-prefix)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
191 (string= (buffer-substring (point-min) (point)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
192 (substring fill-prefix 0 (- (point) (point-min))))) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
193 ;; 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
|
194 ;; This handles the first line of the paragraph. |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
195 ;; Don't stop at a period followed by just one space. |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
196 (let ((first t)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
197 (while (and (not (eobp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
198 (or first |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
199 (and (not (bobp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
200 (save-excursion (forward-char -1) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
201 (looking-at "\\. "))))) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
202 (skip-chars-forward " ") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
203 (skip-chars-forward "^ \n") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
204 (setq first nil))))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
205 ;; Replace all whitespace here with one newline. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
206 ;; 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
|
207 ;; the whitespace point or markers used to be on. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
208 (skip-chars-backward " ") |
218 | 209 (insert ?\n) |
210 (delete-horizontal-space) | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
211 ;; Insert the fill prefix at start of each line. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
212 ;; Set prefixcol so whitespace in the prefix won't get lost. |
218 | 213 (and (not (eobp)) fill-prefix (not (equal fill-prefix "")) |
214 (progn | |
215 (insert fill-prefix) | |
216 (setq prefixcol (current-column)))) | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
217 ;; Justify the line just ended, if desired. |
218 | 218 (and justify-flag (not (eobp)) |
219 (progn | |
220 (forward-line -1) | |
221 (justify-current-line) | |
222 (forward-line 1)))))))) | |
75 | 223 |
224 (defun fill-paragraph (arg) | |
218 | 225 "Fill paragraph at or after point. Prefix arg means justify as well." |
75 | 226 (interactive "P") |
1332
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
227 (let ((before (point))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
228 (save-excursion |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
229 (forward-paragraph) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
230 (or (bolp) (newline 1)) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
231 (let ((end (point)) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
232 (beg (progn (backward-paragraph) (point)))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
233 (goto-char before) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
234 (fill-region-as-paragraph beg end arg))))) |
75 | 235 |
236 (defun fill-region (from to &optional justify-flag) | |
237 "Fill each of the paragraphs in the region. | |
218 | 238 Prefix arg (non-nil third arg, if called from program) means justify as well." |
75 | 239 (interactive "r\nP") |
240 (save-restriction | |
241 (narrow-to-region from to) | |
242 (goto-char (point-min)) | |
243 (while (not (eobp)) | |
244 (let ((initial (point)) | |
245 (end (progn | |
246 (forward-paragraph 1) (point)))) | |
247 (forward-paragraph -1) | |
248 (if (>= (point) initial) | |
249 (fill-region-as-paragraph (point) end justify-flag) | |
250 (goto-char end)))))) | |
251 | |
252 (defun justify-current-line () | |
218 | 253 "Add spaces to line point is in, so it ends at `fill-column'." |
75 | 254 (interactive) |
255 (save-excursion | |
256 (save-restriction | |
218 | 257 (let (ncols beg indent) |
75 | 258 (beginning-of-line) |
259 (forward-char (length fill-prefix)) | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
260 (skip-chars-forward " \t") |
218 | 261 (setq indent (current-column)) |
75 | 262 (setq beg (point)) |
263 (end-of-line) | |
264 (narrow-to-region beg (point)) | |
265 (goto-char beg) | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
266 (while (re-search-forward " *" nil t) |
75 | 267 (delete-region |
268 (+ (match-beginning 0) | |
269 (if (save-excursion | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
270 (skip-chars-backward " ])\"'") |
75 | 271 (memq (preceding-char) '(?. ?? ?!))) |
272 2 1)) | |
273 (match-end 0))) | |
274 (goto-char beg) | |
1067 | 275 (while (re-search-forward "[.?!][])\"']*\n" nil t) |
75 | 276 (forward-char -1) |
277 (insert ? )) | |
278 (goto-char (point-max)) | |
218 | 279 ;; Note that the buffer bounds start after the indentation, |
280 ;; so the columns counted by INDENT don't appear in (current-column). | |
281 (setq ncols (- fill-column (current-column) indent)) | |
75 | 282 (if (search-backward " " nil t) |
283 (while (> ncols 0) | |
284 (let ((nmove (+ 3 (random 3)))) | |
285 (while (> nmove 0) | |
286 (or (search-backward " " nil t) | |
287 (progn | |
288 (goto-char (point-max)) | |
289 (search-backward " "))) | |
290 (skip-chars-backward " ") | |
291 (setq nmove (1- nmove)))) | |
292 (insert " ") | |
293 (skip-chars-backward " ") | |
2109
73485780a8f9
* simple.el (yank, yank-pop): Always return nil; don't rely on
Jim Blandy <jimb@redhat.com>
parents:
1332
diff
changeset
|
294 (setq ncols (1- ncols))))))) |
73485780a8f9
* simple.el (yank, yank-pop): Always return nil; don't rely on
Jim Blandy <jimb@redhat.com>
parents:
1332
diff
changeset
|
295 nil) |
75 | 296 |
2520
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
297 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
298 "Fill paragraphs within the region, allowing varying indentation within each. |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
299 This command divides the region into \"paragraphs\", |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
300 only at paragraph-separator lines, then fills each paragraph |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
301 using as the fill prefix the smallest indentation of any line |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
302 in the paragraph. |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
303 |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
304 When calling from a program, pass range to fill as first two arguments. |
638 | 305 |
2520
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
306 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG: |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
307 JUSTIFY-FLAG to justify paragraphs (prefix arg), |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
308 MAIL-FLAG for a mail message, i. e. don't fill header lines." |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
309 (interactive "r\nP") |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
310 (let ((fill-individual-varying-indent t)) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
311 (fill-individual-paragraphs min max justifyp mailp))) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
312 |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
313 (defun fill-individual-paragraphs (min max &optional justifyp mailp) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
314 "Fill paragraphs of uniform indentation within the region. |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
315 This command divides the region into \"paragraphs\", |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
316 treating every change in indentation level as a paragraph boundary, |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
317 then fills each paragraph using its indentation level as the fill prefix. |
638 | 318 |
319 When calling from a program, pass range to fill as first two arguments. | |
320 | |
75 | 321 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG: |
322 JUSTIFY-FLAG to justify paragraphs (prefix arg), | |
323 MAIL-FLAG for a mail message, i. e. don't fill header lines." | |
324 (interactive "r\nP") | |
474 | 325 (save-restriction |
326 (save-excursion | |
327 (goto-char min) | |
328 (beginning-of-line) | |
329 (if mailp | |
821
cee415e2dae8
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
811
diff
changeset
|
330 (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$")) |
4992
8a3293abe1ba
(fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents:
4413
diff
changeset
|
331 (if (looking-at "[ \t]*[^ \t\n]*:") |
8a3293abe1ba
(fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents:
4413
diff
changeset
|
332 (search-forward "\n\n" nil 'move) |
8a3293abe1ba
(fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents:
4413
diff
changeset
|
333 (forward-line 1)))) |
474 | 334 (narrow-to-region (point) max) |
335 ;; Loop over paragraphs. | |
336 (while (progn (skip-chars-forward " \t\n") (not (eobp))) | |
337 (beginning-of-line) | |
338 (let ((start (point)) | |
339 fill-prefix fill-prefix-regexp) | |
340 ;; Find end of paragraph, and compute the smallest fill-prefix | |
341 ;; that fits all the lines in this paragraph. | |
342 (while (progn | |
343 ;; Update the fill-prefix on the first line | |
344 ;; and whenever the prefix good so far is too long. | |
345 (if (not (and fill-prefix | |
346 (looking-at fill-prefix-regexp))) | |
347 (setq fill-prefix | |
348 (buffer-substring (point) | |
349 (save-excursion (skip-chars-forward " \t") (point))) | |
350 fill-prefix-regexp | |
351 (regexp-quote fill-prefix))) | |
352 (forward-line 1) | |
353 ;; Now stop the loop if end of paragraph. | |
354 (and (not (eobp)) | |
638 | 355 (if fill-individual-varying-indent |
356 ;; If this line is a separator line, with or | |
357 ;; without prefix, end the paragraph. | |
358 (and | |
474 | 359 (not (looking-at paragraph-separate)) |
360 (save-excursion | |
361 (not (and (looking-at fill-prefix-regexp) | |
362 (progn (forward-char (length fill-prefix)) | |
638 | 363 (looking-at paragraph-separate)))))) |
364 ;; If this line has more or less indent | |
365 ;; than the fill prefix wants, end the paragraph. | |
366 (and (looking-at fill-prefix-regexp) | |
367 (save-excursion | |
368 (not (progn (forward-char (length fill-prefix)) | |
369 (or (looking-at paragraph-separate) | |
370 (looking-at paragraph-start)))))))))) | |
474 | 371 ;; Fill this paragraph, but don't add a newline at the end. |
372 (let ((had-newline (bolp))) | |
373 (fill-region-as-paragraph start (point) justifyp) | |
638 | 374 (or had-newline (delete-char -1)))))))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
375 |
732 | 376 ;;; fill.el ends here |