d7526a192b57f639df22580fead3da947628d92b
[bpt/emacs.git] / lisp / textmodes / fill.el
1 ;; Fill commands for Emacs
2 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU Emacs.
5
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
10
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21 (defun set-fill-prefix ()
22 "Set the fill-prefix to the current line up to point.
23 Filling expects lines to start with the fill prefix and
24 reinserts the fill prefix in each resulting line."
25 (interactive)
26 (setq fill-prefix (buffer-substring
27 (save-excursion (beginning-of-line) (point))
28 (point)))
29 (if (equal fill-prefix "")
30 (setq fill-prefix nil))
31 (if fill-prefix
32 (message "fill-prefix: \"%s\"" fill-prefix)
33 (message "fill-prefix cancelled")))
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.
40 If Adaptive Fill mode is enabled, whatever text matches this pattern
41 on the second line of a paragraph is used as the standard indentation
42 for the paragraph.")
43
44 (defun fill-region-as-paragraph (from to &optional justify-flag)
45 "Fill region as one paragraph: break lines to fit fill-column.
46 Prefix arg means justify too.
47 From program, pass args FROM, TO and JUSTIFY-FLAG."
48 (interactive "r\nP")
49 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
50 (let ((fill-prefix fill-prefix))
51 ;; Figure out how this paragraph is indented, if desired.
52 (if adaptive-fill-mode
53 (save-excursion
54 (goto-char (min from to))
55 (if (eolp) (forward-line 1))
56 (forward-line 1)
57 (if (< (point) (max from to))
58 (let ((start (point)))
59 (re-search-forward adaptive-fill-regexp)
60 (setq fill-prefix (buffer-substring start (point))))
61 (goto-char (min from to))
62 (if (eolp) (forward-line 1))
63 ;; If paragraph has only one line, don't assume
64 ;; that additional lines would have the same starting
65 ;; decoration. Instead, assume they would have white space
66 ;; reaching to the same column.
67 (re-search-forward adaptive-fill-regexp)
68 (setq fill-prefix (make-string (current-column) ?\ )))))
69
70 (save-restriction
71 (narrow-to-region from to)
72 (goto-char (point-min))
73 (skip-chars-forward "\n")
74 (narrow-to-region (point) (point-max))
75 (setq from (point))
76 (goto-char (point-max))
77 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
78 (regexp-quote fill-prefix))))
79 ;; Delete the fill prefix from every line except the first.
80 ;; The first line may not even have a fill prefix.
81 (and fpre
82 (progn
83 (if (>= (length fill-prefix) fill-column)
84 (error "fill-prefix too long for specified width"))
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))))))))
149
150 (defun fill-paragraph (arg)
151 "Fill paragraph at or after point. Prefix arg means justify as well."
152 (interactive "P")
153 (save-excursion
154 (forward-paragraph)
155 (or (bolp) (newline 1))
156 (let ((end (point)))
157 (backward-paragraph)
158 (fill-region-as-paragraph (point) end arg))))
159
160 (defun fill-region (from to &optional justify-flag)
161 "Fill each of the paragraphs in the region.
162 Prefix arg (non-nil third arg, if called from program) means justify as well."
163 (interactive "r\nP")
164 (save-restriction
165 (narrow-to-region from to)
166 (goto-char (point-min))
167 (while (not (eobp))
168 (let ((initial (point))
169 (end (progn
170 (forward-paragraph 1) (point))))
171 (forward-paragraph -1)
172 (if (>= (point) initial)
173 (fill-region-as-paragraph (point) end justify-flag)
174 (goto-char end))))))
175
176 (defun justify-current-line ()
177 "Add spaces to line point is in, so it ends at `fill-column'."
178 (interactive)
179 (save-excursion
180 (save-restriction
181 (let (ncols beg indent)
182 (beginning-of-line)
183 (forward-char (length fill-prefix))
184 (skip-chars-forward " \t")
185 (setq indent (current-column))
186 (setq beg (point))
187 (end-of-line)
188 (narrow-to-region beg (point))
189 (goto-char beg)
190 (while (re-search-forward " *" nil t)
191 (delete-region
192 (+ (match-beginning 0)
193 (if (save-excursion
194 (skip-chars-backward " ])\"'")
195 (memq (preceding-char) '(?. ?? ?!)))
196 2 1))
197 (match-end 0)))
198 (goto-char beg)
199 (while (re-search-forward "[.?!][])""']*\n" nil t)
200 (forward-char -1)
201 (insert ? ))
202 (goto-char (point-max))
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))
206 (if (search-backward " " nil t)
207 (while (> ncols 0)
208 (let ((nmove (+ 3 (random 3))))
209 (while (> nmove 0)
210 (or (search-backward " " nil t)
211 (progn
212 (goto-char (point-max))
213 (search-backward " ")))
214 (skip-chars-backward " ")
215 (setq nmove (1- nmove))))
216 (insert " ")
217 (skip-chars-backward " ")
218 (setq ncols (1- ncols))))))))
219 \f
220 (defun fill-individual-paragraphs (min max &optional justifyp mailp)
221 "Fill each paragraph in region according to its individual fill prefix.
222 Calling from a program, pass range to fill as first two arguments.
223 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
224 JUSTIFY-FLAG to justify paragraphs (prefix arg),
225 MAIL-FLAG for a mail message, i. e. don't fill header lines."
226 (interactive "r\nP")
227 (let (fill-prefix)
228 (save-restriction
229 (save-excursion
230 (goto-char min)
231 (if mailp
232 (while (looking-at "[^ \t\n]*:")
233 (forward-line 1)))
234 (narrow-to-region (point) max)
235 (while (progn
236 (skip-chars-forward " \t\n")
237 (not (eobp)))
238 (setq fill-prefix
239 (buffer-substring (point) (progn (beginning-of-line) (point))))
240 (let ((fin (save-excursion (forward-paragraph) (point)))
241 (start (point)))
242 (fill-region-as-paragraph (point) fin justifyp)
243 (goto-char start)
244 (forward-paragraph)))))))
245
246