aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1991-03-20 04:10:45 +0000
committerRichard M. Stallman1991-03-20 04:10:45 +0000
commit54d7f6504d777f9780827f680b22c3375a3aaaba (patch)
treee23152d328edea83496464b5ca8c00c2b7c6e9f0
parent540671f32b14364f13618c99f8b6772c1387550c (diff)
downloademacs-54d7f6504d777f9780827f680b22c3375a3aaaba.tar.gz
emacs-54d7f6504d777f9780827f680b22c3375a3aaaba.zip
*** empty log message ***
-rw-r--r--lisp/textmodes/fill.el225
1 files changed, 129 insertions, 96 deletions
diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el
index 520a235d3ea..d7526a192b5 100644
--- a/lisp/textmodes/fill.el
+++ b/lisp/textmodes/fill.el
@@ -20,8 +20,8 @@
20 20
21(defun set-fill-prefix () 21(defun set-fill-prefix ()
22 "Set the fill-prefix to the current line up to point. 22 "Set the fill-prefix to the current line up to point.
23Filling expects lines to start with the fill prefix 23Filling expects lines to start with the fill prefix and
24and reinserts the fill prefix in each resulting line." 24reinserts the fill prefix in each resulting line."
25 (interactive) 25 (interactive)
26 (setq fill-prefix (buffer-substring 26 (setq fill-prefix (buffer-substring
27 (save-excursion (beginning-of-line) (point)) 27 (save-excursion (beginning-of-line) (point))
@@ -32,94 +32,123 @@ and reinserts the fill prefix in each resulting line."
32 (message "fill-prefix: \"%s\"" fill-prefix) 32 (message "fill-prefix: \"%s\"" fill-prefix)
33 (message "fill-prefix cancelled"))) 33 (message "fill-prefix cancelled")))
34 34
35(defconst adaptive-fill-mode t
36 "*Non-nil means determine a paragraph's fill prefix from its text.")
37
38(defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
39 "*Regexp to match text at start of line that constitutes indentation.
40If Adaptive Fill mode is enabled, whatever text matches this pattern
41on the second line of a paragraph is used as the standard indentation
42for the paragraph.")
43
35(defun fill-region-as-paragraph (from to &optional justify-flag) 44(defun fill-region-as-paragraph (from to &optional justify-flag)
36 "Fill region as one paragraph: break lines to fit fill-column. 45 "Fill region as one paragraph: break lines to fit fill-column.
37Prefix arg means justify too. 46Prefix arg means justify too.
38From program, pass args FROM, TO and JUSTIFY-FLAG." 47From program, pass args FROM, TO and JUSTIFY-FLAG."
39 (interactive "r\nP") 48 (interactive "r\nP")
40 (save-restriction 49 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
41 (narrow-to-region from to) 50 (let ((fill-prefix fill-prefix))
42 (goto-char (point-min)) 51 ;; Figure out how this paragraph is indented, if desired.
43 (skip-chars-forward "\n") 52 (if adaptive-fill-mode
44 (narrow-to-region (point) (point-max)) 53 (save-excursion
45 (setq from (point)) 54 (goto-char (min from to))
46 (goto-char (point-max)) 55 (if (eolp) (forward-line 1))
47 (let ((fpre (and fill-prefix (not (equal fill-prefix "")) 56 (forward-line 1)
48 (regexp-quote fill-prefix)))) 57 (if (< (point) (max from to))
49 ;; Delete the fill prefix from every line except the first. 58 (let ((start (point)))
50 ;; The first line may not even have a fill prefix. 59 (re-search-forward adaptive-fill-regexp)
51 (and fpre 60 (setq fill-prefix (buffer-substring start (point))))
52 (progn 61 (goto-char (min from to))
53 (if (>= (length fill-prefix) fill-column) 62 (if (eolp) (forward-line 1))
54 (error "fill-prefix too long for specified width")) 63 ;; If paragraph has only one line, don't assume
55 (goto-char (point-min)) 64 ;; that additional lines would have the same starting
56 (forward-line 1) 65 ;; decoration. Instead, assume they would have white space
57 (while (not (eobp)) 66 ;; reaching to the same column.
58 (if (looking-at fpre) 67 (re-search-forward adaptive-fill-regexp)
59 (delete-region (point) (match-end 0))) 68 (setq fill-prefix (make-string (current-column) ?\ )))))
60 (forward-line 1)) 69
61 (goto-char (point-min)) 70 (save-restriction
62 (and (looking-at fpre) (forward-char (length fill-prefix))) 71 (narrow-to-region from to)
63 (setq from (point))))) 72 (goto-char (point-min))
64 ;; from is now before the text to fill, 73 (skip-chars-forward "\n")
65 ;; but after any fill prefix on the first line. 74 (narrow-to-region (point) (point-max))
66 75 (setq from (point))
67 ;; Make sure sentences ending at end of line get an extra space. 76 (goto-char (point-max))
68 ;; loses on split abbrevs ("Mr.\nSmith") 77 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
69 (goto-char from) 78 (regexp-quote fill-prefix))))
70 (while (re-search-forward "[.?!][])\"']*$" nil t) 79 ;; Delete the fill prefix from every line except the first.
71 (insert ? )) 80 ;; The first line may not even have a fill prefix.
72 81 (and fpre
73 ;; Then change all newlines to spaces.
74 (subst-char-in-region from (point-max) ?\n ?\ )
75
76 ;; Flush excess spaces, except in the paragraph indentation.
77 (goto-char from)
78 (skip-chars-forward " \t")
79 ;; nuke tabs while we're at it; they get screwed up in a fill
80 ;; this is quick, but loses when a sole tab follows the end of a sentence.
81 ;; actually, it is difficult to tell that from "Mr.\tSmith".
82 ;; blame the typist.
83 (subst-char-in-region (point) (point-max) ?\t ?\ )
84 (while (re-search-forward " *" nil t)
85 (delete-region
86 (+ (match-beginning 0)
87 (if (save-excursion
88 (skip-chars-backward " ])\"'")
89 (memq (preceding-char) '(?. ?? ?!)))
90 2 1))
91 (match-end 0)))
92 (goto-char (point-max))
93 (delete-horizontal-space)
94 (insert " ")
95 (goto-char (point-min))
96
97 (let ((prefixcol 0))
98 (while (not (eobp))
99 (move-to-column (1+ fill-column))
100 (if (eobp)
101 nil
102 (skip-chars-backward "^ \n")
103 (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
104 (skip-chars-forward "^ \n")
105 (forward-char -1)))
106 ;; Inserting the newline first prevents losing track of point.
107 (skip-chars-backward " ")
108 (insert ?\n)
109 (delete-horizontal-space)
110 (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
111 (progn
112 (insert fill-prefix)
113 (setq prefixcol (current-column))))
114 (and justify-flag (not (eobp))
115 (progn 82 (progn
116 (forward-line -1) 83 (if (>= (length fill-prefix) fill-column)
117 (justify-current-line) 84 (error "fill-prefix too long for specified width"))
118 (forward-line 1))))))) 85 (goto-char (point-min))
86 (forward-line 1)
87 (while (not (eobp))
88 (if (looking-at fpre)
89 (delete-region (point) (match-end 0)))
90 (forward-line 1))
91 (goto-char (point-min))
92 (and (looking-at fpre) (forward-char (length fill-prefix)))
93 (setq from (point)))))
94 ;; from is now before the text to fill,
95 ;; but after any fill prefix on the first line.
96
97 ;; Make sure sentences ending at end of line get an extra space.
98 ;; loses on split abbrevs ("Mr.\nSmith")
99 (goto-char from)
100 (while (re-search-forward "[.?!][])\"']*$" nil t)
101 (insert ? ))
102
103 ;; Then change all newlines to spaces.
104 (subst-char-in-region from (point-max) ?\n ?\ )
105
106 ;; Flush excess spaces, except in the paragraph indentation.
107 (goto-char from)
108 (skip-chars-forward " \t")
109 ;; nuke tabs while we're at it; they get screwed up in a fill
110 ;; this is quick, but loses when a sole tab follows the end of a sentence.
111 ;; actually, it is difficult to tell that from "Mr.\tSmith".
112 ;; blame the typist.
113 (subst-char-in-region (point) (point-max) ?\t ?\ )
114 (while (re-search-forward " *" nil t)
115 (delete-region
116 (+ (match-beginning 0)
117 (if (save-excursion
118 (skip-chars-backward " ])\"'")
119 (memq (preceding-char) '(?. ?? ?!)))
120 2 1))
121 (match-end 0)))
122 (goto-char (point-max))
123 (delete-horizontal-space)
124 (insert " ")
125 (goto-char (point-min))
126
127 (let ((prefixcol 0))
128 (while (not (eobp))
129 (move-to-column (1+ fill-column))
130 (if (eobp)
131 nil
132 (skip-chars-backward "^ \n")
133 (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
134 (skip-chars-forward "^ \n")
135 (forward-char -1)))
136 ;; Inserting the newline first prevents losing track of point.
137 (skip-chars-backward " ")
138 (insert ?\n)
139 (delete-horizontal-space)
140 (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
141 (progn
142 (insert fill-prefix)
143 (setq prefixcol (current-column))))
144 (and justify-flag (not (eobp))
145 (progn
146 (forward-line -1)
147 (justify-current-line)
148 (forward-line 1))))))))
119 149
120(defun fill-paragraph (arg) 150(defun fill-paragraph (arg)
121 "Fill paragraph at or after point. 151 "Fill paragraph at or after point. Prefix arg means justify as well."
122Prefix arg means justify as well."
123 (interactive "P") 152 (interactive "P")
124 (save-excursion 153 (save-excursion
125 (forward-paragraph) 154 (forward-paragraph)
@@ -130,8 +159,7 @@ Prefix arg means justify as well."
130 159
131(defun fill-region (from to &optional justify-flag) 160(defun fill-region (from to &optional justify-flag)
132 "Fill each of the paragraphs in the region. 161 "Fill each of the paragraphs in the region.
133Prefix arg (non-nil third arg, if called from program) 162Prefix arg (non-nil third arg, if called from program) means justify as well."
134means justify as well."
135 (interactive "r\nP") 163 (interactive "r\nP")
136 (save-restriction 164 (save-restriction
137 (narrow-to-region from to) 165 (narrow-to-region from to)
@@ -146,14 +174,15 @@ means justify as well."
146 (goto-char end)))))) 174 (goto-char end))))))
147 175
148(defun justify-current-line () 176(defun justify-current-line ()
149 "Add spaces to line point is in, so it ends at fill-column." 177 "Add spaces to line point is in, so it ends at `fill-column'."
150 (interactive) 178 (interactive)
151 (save-excursion 179 (save-excursion
152 (save-restriction 180 (save-restriction
153 (let (ncols beg) 181 (let (ncols beg indent)
154 (beginning-of-line) 182 (beginning-of-line)
155 (forward-char (length fill-prefix)) 183 (forward-char (length fill-prefix))
156 (skip-chars-forward " \t") 184 (skip-chars-forward " \t")
185 (setq indent (current-column))
157 (setq beg (point)) 186 (setq beg (point))
158 (end-of-line) 187 (end-of-line)
159 (narrow-to-region beg (point)) 188 (narrow-to-region beg (point))
@@ -171,7 +200,9 @@ means justify as well."
171 (forward-char -1) 200 (forward-char -1)
172 (insert ? )) 201 (insert ? ))
173 (goto-char (point-max)) 202 (goto-char (point-max))
174 (setq ncols (- fill-column (current-column))) 203 ;; Note that the buffer bounds start after the indentation,
204 ;; so the columns counted by INDENT don't appear in (current-column).
205 (setq ncols (- fill-column (current-column) indent))
175 (if (search-backward " " nil t) 206 (if (search-backward " " nil t)
176 (while (> ncols 0) 207 (while (> ncols 0)
177 (let ((nmove (+ 3 (random 3)))) 208 (let ((nmove (+ 3 (random 3))))
@@ -196,18 +227,20 @@ MAIL-FLAG for a mail message, i. e. don't fill header lines."
196 (let (fill-prefix) 227 (let (fill-prefix)
197 (save-restriction 228 (save-restriction
198 (save-excursion 229 (save-excursion
199 (narrow-to-region min max) 230 (goto-char min)
200 (goto-char (point-min)) 231 (if mailp
232 (while (looking-at "[^ \t\n]*:")
233 (forward-line 1)))
234 (narrow-to-region (point) max)
201 (while (progn 235 (while (progn
202 (skip-chars-forward " \t\n") 236 (skip-chars-forward " \t\n")
203 (not (eobp))) 237 (not (eobp)))
204 (setq fill-prefix (buffer-substring (point) (progn (beginning-of-line) (point)))) 238 (setq fill-prefix
239 (buffer-substring (point) (progn (beginning-of-line) (point))))
205 (let ((fin (save-excursion (forward-paragraph) (point))) 240 (let ((fin (save-excursion (forward-paragraph) (point)))
206 (start (point))) 241 (start (point)))
207 (if mailp 242 (fill-region-as-paragraph (point) fin justifyp)
208 (while (re-search-forward "^[ \t]*[^ \t\n]*:" fin t) 243 (goto-char start)
209 (forward-line 1))) 244 (forward-paragraph)))))))
210 (cond ((= start (point)) 245
211 (fill-region-as-paragraph (point) fin justifyp)
212 (goto-char fin)))))))))
213 246