(text-mode): Use mode-require-final-newline.
[bpt/emacs.git] / lisp / textmodes / fill.el
CommitLineData
c0274f38
ER
1;;; fill.el --- fill commands for Emacs
2
f5e85051 3;; Copyright (C) 1985,86,92,94,95,96,97,1999,2001,02,03,2004
d47e9c06 4;; Free Software Foundation, Inc.
f53a262d 5
6228c05b 6;; Maintainer: FSF
3a801d0c
ER
7;; Keywords: wp
8
f53a262d 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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
f53a262d 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
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
f53a262d 25
e41b2db1
ER
26;;; Commentary:
27
28;; All the commands for filling text. These are documented in the Emacs
29;; manual.
30
e5167999 31;;; Code:
f53a262d 32
e4b7444d
SM
33(defgroup fill nil
34 "Indenting and filling text."
4efbcf61 35 :link '(custom-manual "(emacs)Filling")
e4b7444d
SM
36 :group 'editing)
37
9d325ebf 38(defcustom fill-individual-varying-indent nil
e065a56e
JB
39 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
40Non-nil means changing indent doesn't end a paragraph.
41That mode can handle paragraphs with extra indentation on the first line,
42but it requires separator lines between paragraphs.
9d325ebf
RS
43A value of nil means that any change in indentation starts a new paragraph."
44 :type 'boolean
45 :group 'fill)
8f985fe2 46
9d325ebf
RS
47(defcustom colon-double-space nil
48 "*Non-nil means put two spaces after a colon when filling."
49 :type 'boolean
50 :group 'fill)
68152e8f 51
86dfb30a 52(defvar fill-paragraph-function nil
3103b038
RS
53 "Mode-specific function to fill a paragraph, or nil if there is none.
54If the function returns nil, then `fill-paragraph' does its normal work.")
86dfb30a 55
3d9ce27e 56(defvar fill-paragraph-handle-comment t
e4b7444d 57 "Non-nil means paragraph filling will try to pay attention to comments.")
3d9ce27e 58
e4b7444d
SM
59(defcustom enable-kinsoku t
60 "*Non-nil means enable \"kinsoku\" processing on filling paragraphs.
4dc0f0fc
RS
61Kinsoku processing is designed to prevent certain characters from being
62placed at the beginning or end of a line by filling.
e4b7444d
SM
63See the documentation of `kinsoku' for more information."
64 :type 'boolean)
c9d611f4 65
f53a262d 66(defun set-fill-prefix ()
8f985fe2 67 "Set the fill prefix to the current line up to point.
54d7f650
RS
68Filling expects lines to start with the fill prefix and
69reinserts the fill prefix in each resulting line."
f53a262d 70 (interactive)
2441692d
GM
71 (let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
72 (if (> (point) left-margin-pos)
73 (progn
74 (setq fill-prefix (buffer-substring left-margin-pos (point)))
75 (if (equal fill-prefix "")
76 (setq fill-prefix nil)))
77 (setq fill-prefix nil)))
f53a262d 78 (if fill-prefix
79 (message "fill-prefix: \"%s\"" fill-prefix)
80 (message "fill-prefix cancelled")))
81
9d325ebf
RS
82(defcustom adaptive-fill-mode t
83 "*Non-nil means determine a paragraph's fill prefix from its text."
84 :type 'boolean
85 :group 'fill)
54d7f650 86
742c1822 87(defcustom adaptive-fill-regexp
d47e9c06
SM
88 ;; Added `!' for doxygen comments starting with `//!' or `/*!'.
89 ;; Added `%' for TeX comments.
90 (purecopy "[ \t]*\\([-!|#%;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*")
54d7f650 91 "*Regexp to match text at start of line that constitutes indentation.
849f04e1
RS
92If Adaptive Fill mode is enabled, a prefix matching this pattern
93on the first and second lines of a paragraph is used as the
94standard indentation for the whole paragraph.
95
96If the paragraph has just one line, the indentation is taken from that
97line, but in that case `adaptive-fill-first-line-regexp' also plays
98a role."
99 :type 'regexp
100 :group 'fill)
101
21942e01 102(defcustom adaptive-fill-first-line-regexp "\\`[ \t]*\\'"
849f04e1
RS
103 "*Regexp specifying whether to set fill prefix from a one-line paragraph.
104When a paragraph has just one line, then after `adaptive-fill-regexp'
105finds the prefix at the beginning of the line, if it doesn't
106match this regexp, it is replaced with whitespace.
107
108By default, this regexp matches sequences of just spaces and tabs.
109
110However, we never use a prefix from a one-line paragraph
111if it would act as a paragraph-starter on the second line."
9d325ebf
RS
112 :type 'regexp
113 :group 'fill)
54d7f650 114
9d325ebf 115(defcustom adaptive-fill-function nil
ef4d4394 116 "*Function to call to choose a fill prefix for a paragraph, or nil.
9d325ebf 117This function is used when `adaptive-fill-regexp' does not match."
ef4d4394 118 :type '(choice (const nil) function)
9d325ebf 119 :group 'fill)
68152e8f 120
4bcb1be1 121(defvar fill-indent-according-to-mode nil ;Screws up CC-mode's filling tricks.
178932de
SM
122 "Whether or not filling should try to use the major mode's indentation.")
123
0cb08f98
RS
124(defun current-fill-column ()
125 "Return the fill-column to use for this line.
126The fill-column to use for a buffer is stored in the variable `fill-column',
127but can be locally modified by the `right-margin' text property, which is
128subtracted from `fill-column'.
129
130The fill column to use for a line is the first column at which the column
131number equals or exceeds the local fill-column - right-margin difference."
132 (save-excursion
1e87252c
RS
133 (if fill-column
134 (let* ((here (progn (beginning-of-line) (point)))
135 (here-col 0)
136 (eol (progn (end-of-line) (point)))
137 margin fill-col change col)
742c1822
DL
138 ;; Look separately at each region of line with a different
139 ;; right-margin.
1e87252c
RS
140 (while (and (setq margin (get-text-property here 'right-margin)
141 fill-col (- fill-column (or margin 0))
142 change (text-property-not-all
143 here eol 'right-margin margin))
144 (progn (goto-char (1- change))
145 (setq col (current-column))
146 (< col fill-col)))
147 (setq here change
148 here-col col))
149 (max here-col fill-col)))))
0cb08f98
RS
150
151(defun canonically-space-region (beg end)
152 "Remove extra spaces between words in region.
8c379895 153Leave one space between words, two at end of sentences or after colons
ce82deed
KH
154\(depending on values of `sentence-end-double-space', `colon-double-space',
155and `sentence-end-without-period').
34a5f45f 156Remove indentation from each line."
369aeb97 157 (interactive "*r")
20796cbc 158 (let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +")))
bb424945
SM
159 (save-excursion
160 (goto-char beg)
161 ;; Nuke tabs; they get screwed up in a fill.
162 ;; This is quick, but loses when a tab follows the end of a sentence.
163 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
164 ;; Blame the typist.
165 (subst-char-in-region beg end ?\t ?\ )
ea9ae18a 166 (while (and (< (point) end)
bb424945
SM
167 (re-search-forward end-spc-re end t))
168 (delete-region
169 (cond
170 ;; `sentence-end' matched and did not match all spaces.
171 ;; I.e. it only matched the number of spaces it needs: drop the rest.
172 ((and (match-end 1) (> (match-end 0) (match-end 1))) (match-end 1))
173 ;; `sentence-end' matched but with nothing left. Either that means
174 ;; nothing should be removed, or it means it's the "old-style"
175 ;; sentence-end which matches all it can. Keep only 2 spaces.
176 ;; We probably don't even need to check `sentence-end-double-space'.
177 ((match-end 1)
178 (min (match-end 0)
179 (+ (if sentence-end-double-space 2 1)
180 (save-excursion (goto-char (match-end 0))
181 (skip-chars-backward " ")
182 (point)))))
183 (t ;; It's not an end of sentence.
184 (+ (match-beginning 0)
185 ;; Determine number of spaces to leave:
186 (save-excursion
187 (skip-chars-backward " ]})\"'")
188 (cond ((and sentence-end-double-space
189 (or (memq (preceding-char) '(?. ?? ?!))
190 (and sentence-end-without-period
191 (= (char-syntax (preceding-char)) ?w)))) 2)
192 ((and colon-double-space
193 (= (preceding-char) ?:)) 2)
194 ((char-equal (preceding-char) ?\n) 0)
195 (t 1))))))
196 (match-end 0))))))
0cb08f98 197
7190ab87
GM
198(defun fill-common-string-prefix (s1 s2)
199 "Return the longest common prefix of strings S1 and S2, or nil if none."
200 (let ((cmp (compare-strings s1 nil nil s2 nil nil)))
178932de 201 (if (eq cmp t)
7190ab87
GM
202 s1
203 (setq cmp (1- (abs cmp)))
204 (unless (zerop cmp)
205 (substring s1 0 cmp)))))
db95369b 206
0bcfa3ac 207(defun fill-context-prefix (from to &optional first-line-regexp)
d09d7ba9 208 "Compute a fill prefix from the text between FROM and TO.
39f5988d 209This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
849f04e1
RS
210and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
211we reject a prefix based on a one-line paragraph if that prefix would
212act as a paragraph-separator."
213 (or first-line-regexp
214 (setq first-line-regexp adaptive-fill-first-line-regexp))
d09d7ba9
RS
215 (save-excursion
216 (goto-char from)
217 (if (eolp) (forward-line 1))
218 ;; Move to the second line unless there is just one.
6d1f0a5e 219 (move-to-left-margin)
0bcfa3ac 220 (let ((firstline (point))
b5263b80 221 first-line-prefix
0bcfa3ac 222 ;; Non-nil if we are on the second line.
b5263b80
RS
223 second-line-prefix
224 start)
b5263b80
RS
225 (setq start (point))
226 (setq first-line-prefix
3212cc84
SM
227 ;; We don't need to consider `paragraph-start' here since it
228 ;; will be explicitly checked later on.
229 ;; Also setting first-line-prefix to nil prevents
230 ;; second-line-prefix from being used.
231 (cond ;; ((looking-at paragraph-start) nil)
b5263b80 232 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
03e3e2e9 233 (match-string-no-properties 0))
b5263b80 234 (adaptive-fill-function (funcall adaptive-fill-function))))
d09d7ba9 235 (forward-line 1)
03e3e2e9
SM
236 (if (< (point) to)
237 (progn
dc864d6e
SM
238 (move-to-left-margin)
239 (setq start (point))
240 (setq second-line-prefix
241 (cond ((looking-at paragraph-start) nil) ;Can it happen ? -stef
242 ((and adaptive-fill-regexp
243 (looking-at adaptive-fill-regexp))
244 (buffer-substring-no-properties start (match-end 0)))
245 (adaptive-fill-function
246 (funcall adaptive-fill-function))))
b5263b80
RS
247 ;; If we get a fill prefix from the second line,
248 ;; make sure it or something compatible is on the first line too.
03e3e2e9
SM
249 (when second-line-prefix
250 (unless first-line-prefix (setq first-line-prefix ""))
dc864d6e
SM
251 ;; If the non-whitespace chars match the first line,
252 ;; just use it (this subsumes the 2 checks used previously).
253 ;; Used when first line is `/* ...' and second-line is
254 ;; ` * ...'.
255 (let ((tmp second-line-prefix)
256 (re "\\`"))
257 (while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp)
258 (setq re (concat re ".*" (regexp-quote (match-string 1 tmp))))
259 (setq tmp (substring tmp (match-end 0))))
260 ;; (assert (string-match "\\`[ \t]*\\'" tmp))
03e3e2e9 261
dc864d6e
SM
262 (if (string-match re first-line-prefix)
263 second-line-prefix
7190ab87 264
dc864d6e
SM
265 ;; Use the longest common substring of both prefixes,
266 ;; if there is one.
267 (fill-common-string-prefix first-line-prefix
268 second-line-prefix)))))
b5263b80
RS
269 ;; If we get a fill prefix from a one-line paragraph,
270 ;; maybe change it to whitespace,
271 ;; and check that it isn't a paragraph starter.
272 (if first-line-prefix
273 (let ((result
274 ;; If first-line-prefix comes from the first line,
275 ;; see if it seems reasonable to use for all lines.
276 ;; If not, replace it with whitespace.
277 (if (or (and first-line-regexp
278 (string-match first-line-regexp
279 first-line-prefix))
280 (and comment-start-skip
281 (string-match comment-start-skip
282 first-line-prefix)))
283 first-line-prefix
284 (make-string (string-width first-line-prefix) ?\ ))))
285 ;; But either way, reject it if it indicates the start
286 ;; of a paragraph when text follows it.
287 (if (not (eq 0 (string-match paragraph-start
288 (concat result "a"))))
44a0dbd7 289 result)))))))
d09d7ba9 290
03e3e2e9
SM
291(defun fill-single-word-nobreak-p ()
292 "Don't break a line after the first or before the last word of a sentence."
293 (or (looking-at "[ \t]*\\sw+[ \t]*[.?!:][ \t]*$")
294 (save-excursion
295 (skip-chars-backward " \t")
296 (and (/= (skip-syntax-backward "w") 0)
297 (/= (skip-chars-backward " \t") 0)
298 (/= (skip-chars-backward ".?!:") 0)))))
299
300(defun fill-french-nobreak-p ()
301 "Return nil if French style allows breaking the line at point.
302This is used in `fill-nobreak-predicate' to prevent breaking lines just
303after an opening paren or just before a closing paren or a punctuation
304mark such as `?' or `:'. It is common in French writing to put a space
305at such places, which would normally allow breaking the line at those
306places."
307