Remove directory part from filenames in function declarations.
[bpt/emacs.git] / lisp / textmodes / fill.el
CommitLineData
ac249c4c 1;;; fill.el --- fill commands for Emacs -*- coding: iso-2022-7bit -*-
c0274f38 2
d95da8d3 3;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1996, 1997, 1999, 2001, 2002,
d7a0267c 4;; 2003, 2004, 2005, 2006, 2007 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
5a9dffec 13;; the Free Software Foundation; either version 3, 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 22;; along with GNU Emacs; see the file COPYING. If not, write to the
4fc5845f
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, 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)
119d72b6 51;;;###autoload(put 'colon-double-space 'safe-local-variable 'booleanp)
68152e8f 52
86dfb30a 53(defvar fill-paragraph-function nil
3103b038 54 "Mode-specific function to fill a paragraph, or nil if there is none.
bbc02d00
RS
55If the function returns nil, then `fill-paragraph' does its normal work.
56A value of t means explicitly \"do nothing special\".")
86dfb30a 57
3d9ce27e 58(defvar fill-paragraph-handle-comment t
e4b7444d 59 "Non-nil means paragraph filling will try to pay attention to comments.")
3d9ce27e 60
e4b7444d
SM
61(defcustom enable-kinsoku t
62 "*Non-nil means enable \"kinsoku\" processing on filling paragraphs.
4dc0f0fc
RS
63Kinsoku processing is designed to prevent certain characters from being
64placed at the beginning or end of a line by filling.
e4b7444d 65See the documentation of `kinsoku' for more information."
f5307782
JB
66 :type 'boolean
67 :group 'fill)
c9d611f4 68
f53a262d 69(defun set-fill-prefix ()
8f985fe2 70 "Set the fill prefix to the current line up to point.
54d7f650
RS
71Filling expects lines to start with the fill prefix and
72reinserts the fill prefix in each resulting line."
f53a262d 73 (interactive)
2441692d
GM
74 (let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
75 (if (> (point) left-margin-pos)
76 (progn
77 (setq fill-prefix (buffer-substring left-margin-pos (point)))
78 (if (equal fill-prefix "")
79 (setq fill-prefix nil)))
80 (setq fill-prefix nil)))
f53a262d 81 (if fill-prefix
82 (message "fill-prefix: \"%s\"" fill-prefix)
83 (message "fill-prefix cancelled")))
84
9d325ebf
RS
85(defcustom adaptive-fill-mode t
86 "*Non-nil means determine a paragraph's fill prefix from its text."
87 :type 'boolean
88 :group 'fill)
54d7f650 89
742c1822 90(defcustom adaptive-fill-regexp
d47e9c06
SM
91 ;; Added `!' for doxygen comments starting with `//!' or `/*!'.
92 ;; Added `%' for TeX comments.
4e05bbf5
RS
93 ;; RMS: deleted the code to match `1.' and `(1)'.
94 "[ \t]*\\([-!|#%;>*\e,A7\e$,1s"s#sC\e$,2"F\e(B]+[ \t]*\\)*"
54d7f650 95 "*Regexp to match text at start of line that constitutes indentation.
849f04e1
RS
96If Adaptive Fill mode is enabled, a prefix matching this pattern
97on the first and second lines of a paragraph is used as the
98standard indentation for the whole paragraph.
99
100If the paragraph has just one line, the indentation is taken from that
101line, but in that case `adaptive-fill-first-line-regexp' also plays
102a role."
103 :type 'regexp
104 :group 'fill)
105
21942e01 106(defcustom adaptive-fill-first-line-regexp "\\`[ \t]*\\'"
849f04e1
RS
107 "*Regexp specifying whether to set fill prefix from a one-line paragraph.
108When a paragraph has just one line, then after `adaptive-fill-regexp'
109finds the prefix at the beginning of the line, if it doesn't
110match this regexp, it is replaced with whitespace.
111
112By default, this regexp matches sequences of just spaces and tabs.
113
114However, we never use a prefix from a one-line paragraph
115if it would act as a paragraph-starter on the second line."
9d325ebf
RS
116 :type 'regexp
117 :group 'fill)
54d7f650 118
9d325ebf 119(defcustom adaptive-fill-function nil
3e4dfbb6
JB
120 "Function to call to choose a fill prefix for a paragraph, or nil.
121A nil value means the function has not determined the fill prefix."
ef4d4394 122 :type '(choice (const nil) function)
9d325ebf 123 :group 'fill)
68152e8f 124
4bcb1be1 125(defvar fill-indent-according-to-mode nil ;Screws up CC-mode's filling tricks.
178932de
SM
126 "Whether or not filling should try to use the major mode's indentation.")
127
0cb08f98
RS
128(defun current-fill-column ()
129 "Return the fill-column to use for this line.
130The fill-column to use for a buffer is stored in the variable `fill-column',
131but can be locally modified by the `right-margin' text property, which is
132subtracted from `fill-column'.
133
134The fill column to use for a line is the first column at which the column
135number equals or exceeds the local fill-column - right-margin difference."
136 (save-excursion
1e87252c
RS
137 (if fill-column
138 (let* ((here (progn (beginning-of-line) (point)))
139 (here-col 0)
140 (eol (progn (end-of-line) (point)))
141 margin fill-col change col)
742c1822
DL
142 ;; Look separately at each region of line with a different
143 ;; right-margin.
1e87252c
RS
144 (while (and (setq margin (get-text-property here 'right-margin)
145 fill-col (- fill-column (or margin 0))
146 change (text-property-not-all
147 here eol 'right-margin margin))
148 (progn (goto-char (1- change))
149 (setq col (current-column))
150 (< col fill-col)))
151 (setq here change
152 here-col col))
153 (max here-col fill-col)))))
0cb08f98
RS
154
155(defun canonically-space-region (beg end)
156 "Remove extra spaces between words in region.
8c379895 157Leave one space between words, two at end of sentences or after colons
ce82deed
KH
158\(depending on values of `sentence-end-double-space', `colon-double-space',
159and `sentence-end-without-period').
34a5f45f 160Remove indentation from each line."
369aeb97 161 (interactive "*r")
4b880ac1
SM
162 ;; Ideally, we'd want to scan the text from the end, so that changes to
163 ;; text don't affect the boundary, but the regexp we match against does
164 ;; not match as eagerly when matching backward, so we instead use
165 ;; a marker.
166 (unless (markerp end) (setq end (copy-marker end t)))
20796cbc 167 (let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +")))
bb424945
SM
168 (save-excursion
169 (goto-char beg)
170 ;; Nuke tabs; they get screwed up in a fill.
171 ;; This is quick, but loses when a tab follows the end of a sentence.
172 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
173 ;; Blame the typist.
8ad8316c 174 (subst-char-in-region beg end ?\t ?\s)
ea9ae18a 175 (while (and (< (point) end)
bb424945
SM
176 (re-search-forward end-spc-re end t))
177 (delete-region
178 (cond
179 ;; `sentence-end' matched and did not match all spaces.
180 ;; I.e. it only matched the number of spaces it needs: drop the rest.
181 ((and (match-end 1) (> (match-end 0) (match-end 1))) (match-end 1))
182 ;; `sentence-end' matched but with nothing left. Either that means
183 ;; nothing should be removed, or it means it's the "old-style"
184 ;; sentence-end which matches all it can. Keep only 2 spaces.
185 ;; We probably don't even need to check `sentence-end-double-space'.
186 ((match-end 1)
187 (min (match-end 0)
188 (+ (if sentence-end-double-space 2 1)
189 (save-excursion (goto-char (match-end 0))
190 (skip-chars-backward " ")
191 (point)))))
192 (t ;; It's not an end of sentence.
193 (+ (match-beginning 0)
194 ;; Determine number of spaces to leave:
195 (save-excursion
196 (skip-chars-backward " ]})\"'")
197 (cond ((and sentence-end-double-space
198 (or (memq (preceding-char) '(?. ?? ?!))
199 (and sentence-end-without-period
200 (= (char-syntax (preceding-char)) ?w)))) 2)
201 ((and colon-double-space
202 (= (preceding-char) ?:)) 2)
203 ((char-equal (preceding-char) ?\n) 0)
204 (t 1))))))
205 (match-end 0))))))
0cb08f98 206
7190ab87
GM
207(defun fill-common-string-prefix (s1 s2)
208 "Return the longest common prefix of strings S1 and S2, or nil if none."
209 (let ((cmp (compare-strings s1 nil nil s2 nil nil)))
178932de 210 (if (eq cmp t)
7190ab87
GM
211 s1
212 (setq cmp (1- (abs cmp)))
213 (unless (zerop cmp)
214 (substring s1 0 cmp)))))
db95369b 215
d95da8d3
SM
216(defun fill-match-adaptive-prefix ()
217 (let ((str (or
218 (and adaptive-fill-function (funcall adaptive-fill-function))
219 (and adaptive-fill-regexp (looking-at adaptive-fill-regexp)
220 (match-string-no-properties 0)))))
221 (if (>= (+ (current-left-margin) (length str)) (current-fill-column))
222 ;; Death to insanely long prefixes.
223 nil
224 str)))
225
0bcfa3ac 226(defun fill-context-prefix (from to &optional first-line-regexp)
d09d7ba9 227 "Compute a fill prefix from the text between FROM and TO.
39f5988d 228This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
849f04e1
RS
229and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
230we reject a prefix based on a one-line paragraph if that prefix would
231act as a paragraph-separator."
232 (or first-line-regexp
233 (setq first-line-regexp adaptive-fill-first-line-regexp))
d09d7ba9
RS
234 (save-excursion
235 (goto-char from)
236 (if (eolp) (forward-line 1))
237 ;; Move to the second line unless there is just one.
6d1f0a5e 238 (move-to-left-margin)
d95da8d3 239 (let (first-line-prefix
0bcfa3ac 240 ;; Non-nil if we are on the second line.
d95da8d3 241 second-line-prefix)
b5263b80 242 (setq first-line-prefix
3212cc84
SM
243 ;; We don't need to consider `paragraph-start' here since it
244 ;; will be explicitly checked later on.
245 ;; Also setting first-line-prefix to nil prevents
246 ;; second-line-prefix from being used.
d95da8d3
SM
247 ;; ((looking-at paragraph-start) nil)
248 (fill-match-adaptive-prefix))
d09d7ba9 249 (forward-line 1)
03e3e2e9 250 (if (< (point) to)
d95da8d3
SM
251 (progn
252 (move-to-left-margin)
253 (setq second-line-prefix
254 (cond ((looking-at paragraph-start) nil) ;Can it happen? -Stef
255 (t (fill-match-adaptive-prefix))))
256 ;; If we get a fill prefix from the second line,
257 ;; make sure it or something compatible is on the first line too.
258 (when second-line-prefix
259 (unless first-line-prefix (setq first-line-prefix ""))
260 ;; If the non-whitespace chars match the first line,
261 ;; just use it (this subsumes the 2 checks used previously).
262 ;; Used when first line is `/* ...' and second-line is
263 ;; ` * ...'.
264 (let ((tmp second-line-prefix)
265 (re "\\`"))
266 (while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp)
267 (setq re (concat re ".*" (regexp-quote (match-string 1 tmp))))
268 (setq tmp (substring tmp (match-end 0))))
269 ;; (assert (string-match "\\`[ \t]*\\'" tmp))
270
271 (if (string-match re first-line-prefix)
272 second-line-prefix
273
274 ;; Use the longest common substring of both prefixes,
275 ;; if there is one.
276 (fill-common-string-prefix first-line-prefix
277 second-line-prefix)))))
b5263b80
RS
278 ;; If we get a fill prefix from a one-line paragraph,
279 ;; maybe change it to whitespace,
280 ;; and check that it isn't a paragraph starter.
281 (if first-line-prefix
282 (let ((result
283 ;; If first-line-prefix comes from the first line,
284 ;; see if it seems reasonable to use for all lines.
285 ;; If not, replace it with whitespace.
286 (if (or (and first-line-regexp
287 (string-match first-line-regexp
288 first-line-prefix))
289 (and comment-start-skip
290 (string-match comment-start-skip
291 first-line-prefix)))
292 first-line-prefix
8ad8316c 293 (make-string (string-width first-line-prefix) ?\s))))
b5263b80
RS
294 ;; But either way, reject it if it indicates the start
295 ;; of a paragraph when text follows it.
296 (if (not (eq 0 (string-match paragraph-start
297 (concat result "a"))))
44a0dbd7 298 result)))))))
d09d7ba9 299
03e3e2e9
SM
300(defun fill-single-word-nobreak-p ()
301 "Don't break a line after the first or before the last word of a sentence."
4a3608f5
SM
302 ;; Actually, allow breaking before the last word of a sentence, so long as
303 ;; it's not the last word of the paragraph.
304 (or (looking-at (concat "[ \t]*\\sw+" "\\(?:" (sentence-end) "\\)[ \t]*$"))
03e3e2e9
SM
305 (save-excursion
306 (skip-chars-backward " \t")
307 (and (/= (skip-syntax-backward "w") 0)
308 (/= (skip-chars-backward " \t") 0)
74f36ff0
JL
309 (/= (skip-chars-backward ".?!:") 0)
310 (looking-at (sentence-end))))))
03e3e2e9
SM
311
312(defun fill-french-nobreak-p ()
313 "Return nil if French style allows breaking the line at point.
314This is used in `fill-nobreak-predicate' to prevent breaking lines just
315after an opening paren or just before a closing paren or a punctuation
316mark such as `?' or `:'. It is common in French writing to put a space
317at such places, which would normally allow breaking the line at those
318places."
ac249c4c 319 (or (looking-at "[ \t]*[])}\e,A;\e,b;\e(B?!;:-]")
03e3e2e9
SM
320 (save-excursion
321 (skip-chars-backward " \t")
322 (unless (bolp)
323 (backward-char 1)
ac249c4c 324 (or (looking-at "[([{\e,A+\e,b+\e(B]")
03e3e2e9 325 ;; Don't cut right after a single-letter word.
8ad8316c 326 (and (memq (preceding-char) '(?\t ?\s))
03e3e2e9
SM
327 (eq (char-syntax (following-char)) ?w)))))))
328
329(defcustom fill-nobreak-predicate nil
330 "List of predicates for recognizing places not to break a line.
331The predicates are called with no arguments, with point at the place to
332be tested. If it returns t, fill commands do not break the line there."
333 :group 'fill
334 :type 'hook
335 :options '(fill-french-nobreak-p fill-single-word-nobreak-p))
336
1b457e18 337(defcustom fill-nobreak-invisible nil
e4b7444d 338 "Non-nil means that fill commands do not break lines in invisible text."
1b457e18
KS
339 :type 'boolean
340 :group 'fill)
341
03e3e2e9
SM
342(defun fill-nobreak-p ()
343 "Return nil if breaking the line at point is allowed.
1b457e18
KS
344Can be customized with the variables `fill-nobreak-predicate'
345and `fill-nobreak-invisible'."
346 (or
72229917 347 (and fill-nobreak-invisible (invisible-p (point)))
1b457e18 348 (unless (bolp)
03e3e2e9
SM
349 (or
350 ;; Don't break after a period followed by just one space.
351 ;; Move back to the previous place to break.
352 ;; The reason is that if a period ends up at the end of a
353 ;; line, further fills will assume it ends a sentence.
354 ;; If we now know it does not end a sentence, avoid putting
d47e9c06 355 ;; it at the end of the line.
03e3e2e9
SM
356 (and sentence-end-double-space
357 (save-excursion
08150aa0
JL
358 (skip-chars-backward " ")
359 (and (eq (preceding-char) ?.)
360 (looking-at " \\([^ ]\\|$\\)"))))
03e3e2e9
SM
361 ;; Another approach to the same problem.
362 (save-excursion
08150aa0
JL
363 (skip-chars-backward " ")
364 (and (eq (preceding-char) ?.)
365 (not (progn (forward-char -1) (looking-at (sentence-end))))))
03e3e2e9
SM
366 ;; Don't split a line if the rest would look like a new paragraph.
367 (unless use-hard-newlines
368 (save-excursion
1637ed87
RS
369 (skip-chars-forward " \t")
370 ;; If this break point is at the end of the line,
371 ;; which can occur for auto-fill, don't consider the newline
372 ;; which follows as a reason to return t.
373 (and (not (eolp))
374 (looking-at paragraph-start))))
1b457e18 375 (run-hook-with-args-until-success 'fill-nobreak-predicate)))))
a0d8840a 376
ce82deed
KH
377;; Put `fill-find-break-point-function' property to charsets which
378;; require special functions to find line breaking point.
03e3e2e9 379(dolist (pair '((katakana-jisx0201 . kinsoku)
2f4fa275
SM
380 (chinese-gb2312 . kinsoku)
381 (japanese-jisx0208 . kinsoku)
382 (japanese-jisx0212 . kinsoku)
383 (chinese-big5-1 . kinsoku)
03e3e2e9
SM
384 (chinese-big5-2 . kinsoku)))
385 (put-charset-property (car pair) 'fill-find-break-point-function (cdr pair)))
97323804 386
ce82deed
KH
387(defun fill-find-break-point (limit)
388 "Move point to a proper line breaking position of the current line.
97323804
KH
389Don't move back past the buffer position LIMIT.
390
ce82deed 391This function is called when we are going to break the current line
e4b7444d 392after or before a non-ASCII character. If the charset of the
ce82deed 393character has the property `fill-find-break-point-function', this
97323804 394function calls the property value as a function with one arg LINEBEG.
ce82deed
KH
395If the charset has no such property, do nothing."
396 (let* ((ch (following-char))
397 (charset (char-charset ch))
398 func)
399 (if (eq charset 'ascii)
400 (setq ch (preceding-char)
401 charset (char-charset ch)))
b1376368
KH
402 (if (charsetp charset)
403 (setq func
404 (get-charset-property charset 'fill-find-break-point-function)))
34a500b3
KH
405 (if (and func (fboundp func))
406 (funcall func limit))))
97323804 407
2f4fa275
SM
408(defun fill-delete-prefix (from to prefix)
409 "Delete the fill prefix from every line except the first.
410The first line may not even have a fill prefix.
411Point is moved to just past the fill prefix on the first line."
d47e9c06
SM
412 (let ((fpre (if (and prefix (not (string-match "\\`[ \t]*\\'" prefix)))
413 (concat "[ \t]*\\("
414 (replace-regexp-in-string
415 "[ \t]+" "[ \t]*"
416 (regexp-quote prefix))
417 "\\)?[ \t]*")
418 "[ \t]*")))
419 (goto-char from)
55f6a280
SM
420 ;; Why signal an error here? The problem needs to be caught elsewhere.
421 ;; (if (>= (+ (current-left-margin) (length prefix))
422 ;; (current-fill-column))
423 ;; (error "fill-prefix too long for specified width"))
d47e9c06
SM
424 (forward-line 1)
425 (while (< (point) to)
2f4fa275 426 (if (looking-at fpre)
55f6a280 427 (delete-region (point) (match-end 0)))
d47e9c06
SM
428 (forward-line 1))
429 (goto-char from)
430 (if (looking-at fpre)
431 (goto-char (match-end 0)))
55f6a280 432 (point)))
2f4fa275 433
e3d2084c
SM
434;; The `fill-space' property carries the string with which a newline
435;; should be replaced when unbreaking a line (in fill-delete-newlines).
436;; It is added to newline characters by fill-newline when the default
437;; behavior of fill-delete-newlines is not what we want.
438(add-to-list 'text-property-default-nonsticky '(fill-space . t))
439
2f4fa275
SM
440(defun fill-delete-newlines (from to justify nosqueeze squeeze-after)
441 (goto-char from)
442 ;; Make sure sentences ending at end of line get an extra space.
443 ;; loses on split abbrevs ("Mr.\nSmith")
d47e9c06
SM
444 (let ((eol-double-space-re
445 (cond
20796cbc 446 ((not colon-double-space) (concat (sentence-end) "$"))
d47e9c06 447 ;; Try to add the : inside the `sentence-end' regexp.
20796cbc
JL
448 ((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" (sentence-end))
449 (concat (replace-match ".:" nil nil (sentence-end) 1) "$"))
d47e9c06 450 ;; Can't find the right spot to insert the colon.
f43a0b98
KH
451 (t "[.?!:][])}\"']*$")))
452 (sentence-end-without-space-list
453 (string-to-list sentence-end-without-space)))
2f4fa275 454 (while (re-search-forward eol-double-space-re to t)
8ad8316c 455 (or (>= (point) to) (memq (char-before) '(?\t ?\s))
e4b7444d 456 (memq (char-after (match-beginning 0))
f43a0b98 457 sentence-end-without-space-list)
8ad8316c 458 (insert-and-inherit ?\s))))
2f4fa275
SM
459
460 (goto-char from)
461 (if enable-multibyte-characters
462 ;; Delete unnecessay newlines surrounded by words. The
463 ;; character category `|' means that we can break a line
464 ;; at the character. And, charset property
465 ;; `nospace-between-words' tells how to concatenate
466 ;; words. If the value is non-nil, never put spaces
467 ;; between words, thus delete a newline between them.
468 ;; If the value is nil, delete a newline only when a
469 ;; character preceding a newline has text property
470 ;; `nospace-between-words'.
471 (while (search-forward "\n" to t)
e3d2084c
SM
472 (if (get-text-property (match-beginning 0) 'fill-space)
473 (replace-match (get-text-property (match-beginning 0) 'fill-space))
474 (let ((prev (char-before (match-beginning 0)))
475 (next (following-char)))
476 (if (and (or (aref (char-category-set next) ?|)
477 (aref (char-category-set prev) ?|))
478 (or (get-charset-property (char-charset prev)
479 'nospace-between-words)
480 (get-text-property (1- (match-beginning 0))
481 'nospace-between-words)))
482 (delete-char -1))))))
2f4fa275
SM
483
484 (goto-char from)
485 (skip-chars-forward " \t")
486 ;; Then change all newlines to spaces.
8ad8316c 487 (subst-char-in-region from to ?\n ?\s)
2f4fa275
SM
488 (if (and nosqueeze (not (eq justify 'full)))
489 nil
51153542
SM
490 (canonically-space-region (or squeeze-after (point)) to)
491 ;; Remove trailing whitespace.
492 ;; Maybe canonically-space-region should do that.
05670fa8 493 (goto-char to) (delete-char (- (skip-chars-backward " \t"))))
2f4fa275
SM
494 (goto-char from))
495
d47e9c06
SM
496(defun fill-move-to-break-point (linebeg)
497 "Move to the position where the line should be broken.
bb424945
SM
498The break position will be always after LINEBEG and generally before point."
499 ;; If the fill column is before linebeg, move to linebeg.
500 (if (> linebeg (point)) (goto-char linebeg))
d47e9c06
SM
501 ;; Move back to the point where we can break the line
502 ;; at. We break the line between word or after/before
503 ;; the character which has character category `|'. We
504 ;; search space, \c| followed by a character, or \c|
505 ;; following a character. If not found, place
506 ;; the point at linebeg.
507 (while
508 (when (re-search-backward "[ \t]\\|\\c|.\\|.\\c|" linebeg 0)
509 ;; In case of space, we place the point at next to
510 ;; the point where the break occurs actually,
511 ;; because we don't want to change the following
512 ;; logic of original Emacs. In case of \c|, the
513 ;; point is at the place where the break occurs.
514 (forward-char 1)
515 (when (fill-nobreak-p) (skip-chars-backward " \t" linebeg))))
ef11ff9b
SM
516
517 ;; Move back over the single space between the words.
518 (skip-chars-backward " \t")
519
d47e9c06
SM
520 ;; If the left margin and fill prefix by themselves
521 ;; pass the fill-column. or if they are zero
522 ;; but we have no room for even one word,
523 ;; keep at least one word or a character which has
524 ;; category `|' anyway.
525 (if (>= linebeg (point))
526 ;; Ok, skip at least one word or one \c| character.
527 ;; Meanwhile, don't stop at a period followed by one space.
528 (let ((to (line-end-position))
d47e9c06
SM
529 (first t))
530 (goto-char linebeg)
531 (while (and (< (point) to) (or first (fill-nobreak-p)))
532 ;; Find a breakable point while ignoring the
533 ;; following spaces.
534 (skip-chars-forward " \t")
535 (if (looking-at "\\c|")
536 (forward-char 1)
537 (let ((pos (save-excursion
538 (skip-chars-forward "^ \n\t")
539 (point))))
540 (if (re-search-forward "\\c|" pos t)
541 (forward-char -1)
542 (goto-char pos))))
543 (setq first nil)))
d47e9c06
SM
544
545 (if enable-multibyte-characters
546 ;; If we are going to break the line after or
547 ;; before a non-ascii character, we may have to
548 ;; run a special function for the charset of the
549 ;; character to find the correct break point.
550 (if (not (and (eq (charset-after (1- (point))) 'ascii)
551 (eq (charset-after (point)) 'ascii)))
552 ;; Make sure we take SOMETHING after the fill prefix if any.
553 (fill-find-break-point linebeg)))))
554
7c315e1c
KH
555;; Like text-properties-at but don't include `composition' property.
556(defun fill-text-properties-at (pos)
557 (let ((l (text-properties-at pos))
558 prop-list)
559 (while l
560 (unless (eq (car l) 'composition)
561 (setq prop-list
562 (cons (car l) (cons (cadr l) prop-list))))
563 (setq l (cddr l)))
564 prop-list))
565
d47e9c06
SM
566(defun fill-newline ()
567 ;; Replace whitespace here with one newline, then
568 ;; indent to left margin.
569 (skip-chars-backward " \t")
d47e9c06
SM
570 (insert ?\n)
571 ;; Give newline the properties of the space(s) it replaces
572 (set-text-properties (1- (point)) (point)
7c315e1c 573 (fill-text-properties-at (point)))
e3d2084c
SM
574 (and (looking-at "\\( [ \t]*\\)\\(\\c|\\)?")
575 (or (aref (char-category-set (or (char-before (1- (point))) ?\000)) ?|)
576 (match-end 2))
577 ;; When refilling later on, this newline would normally not be replaced
578 ;; by a space, so we need to mark it specially to re-install the space
579 ;; when we unfill.
580 (put-text-property (1- (point)) (point) 'fill-space (match-string 1)))
1b457e18
KS
581 ;; If we don't want breaks in invisible text, don't insert
582 ;; an invisible newline.
583 (if fill-nobreak-invisible
584 (remove-text-properties (1- (point)) (point)
585 '(invisible t)))
d47e9c06
SM
586 (if (or fill-prefix
587 (not fill-indent-according-to-mode))
f601aaf8 588 (fill-indent-to-left-margin)
d47e9c06
SM
589 (indent-according-to-mode))
590 ;; Insert the fill prefix after indentation.
d47e9c06 591 (and fill-prefix (not (equal fill-prefix ""))
29c1653c
SM
592 ;; Markers that were after the whitespace are now at point: insert
593 ;; before them so they don't get stuck before the prefix.
594 (insert-before-markers-and-inherit fill-prefix)))
d47e9c06 595
f601aaf8
RS
596(defun fill-indent-to-left-margin ()
597 "Indent current line to the column given by `current-left-margin'."
598 (let ((beg (point)))
599 (indent-line-to (current-left-margin))
600 (put-text-property beg (point) 'face 'default)))
601
8c379895
KH
602(defun fill-region-as-paragraph (from to &optional justify
603 nosqueeze squeeze-after)
1095bc3c 604 "Fill the region as one paragraph.
2b6eb8c6 605It removes any paragraph breaks in the region and extra newlines at the end,
1095bc3c
BG
606indents and fills lines between the margins given by the
607`current-left-margin' and `current-fill-column' functions.
368d104a 608\(In most cases, the variable `fill-column' controls the width.)
2b6eb8c6 609It leaves point at the beginning of the line following the paragraph.
0cb08f98 610
1095bc3c
BG
611Normally performs justification according to the `current-justification'
612function, but with a prefix arg, does full justification instead.
613
614From a program, optional third arg JUSTIFY can specify any type of
8c379895 615justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
f3253b8f 616between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
8c379895 617means don't canonicalize spaces before that position.
0cb08f98 618
ef11ff9b 619Return the `fill-prefix' used for filling.
27849a65 620
8f985fe2 621If `sentence-end-double-space' is non-nil, then period followed by one
0cb08f98 622space does not end a sentence, so don't break a line there."
369aeb97
DL
623 (interactive (progn
624 (barf-if-buffer-read-only)
625 (list (region-beginning) (region-end)
626 (if current-prefix-arg 'full))))
d09af6a5 627 (unless (memq justify '(t nil none full center left right))
73b33545 628 (setq justify 'full))
1095bc3c 629
2b6eb8c6 630 ;; Make sure "to" is the endpoint.
1095bc3c
BG
631 (goto-char (min from to))
632 (setq to (max from to))
2b6eb8c6
RS
633 ;; Ignore blank lines at beginning of region.
634 (skip-chars-forward " \t\n")
1095bc3c 635
2b6eb8c6
RS
636 (let ((from-plus-indent (point))
637 (oneleft nil))
638
5fc5b7e8 639 (beginning-of-line)
55f6a280 640 ;; We used to round up to whole line, but that prevents us from
5fc5b7e8
SM
641 ;; correctly handling filling of mixed code-and-comment where we do want
642 ;; to fill the comment but not the code. So only use (point) if it's
643 ;; further than `from', which means that `from' is followed by some
644 ;; number of empty lines.
645 (setq from (max (point) from))
db95369b 646
2b6eb8c6 647 ;; Delete all but one soft newline at end of region.
dcfe5c05 648 ;; And leave TO before that one.
2b6eb8c6 649 (goto-char to)
1095bc3c
BG
650 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
651 (if (and oneleft
652 (not (and use-hard-newlines
653 (get-text-property (1- (point)) 'hard))))
654 (delete-backward-char 1)
655 (backward-char 1)
656 (setq oneleft t)))
2f4fa275 657 (setq to (copy-marker (point) t))
178932de
SM
658 ;; ;; If there was no newline, and there is text in the paragraph, then
659 ;; ;; create a newline.
660 ;; (if (and (not oneleft) (> to from-plus-indent))
661 ;; (newline))
2b6eb8c6
RS
662 (goto-char from-plus-indent))
663
664 (if (not (> to (point)))
d47e9c06 665 nil ;; There is no paragraph, only whitespace: exit now.
1095bc3c
BG
666
667 (or justify (setq justify (current-justification)))
668
669 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
670 (let ((fill-prefix fill-prefix))
671 ;; Figure out how this paragraph is indented, if desired.
2f4fa275
SM
672 (when (and adaptive-fill-mode
673 (or (null fill-prefix) (string= fill-prefix "")))
674 (setq fill-prefix (fill-context-prefix from to))
675 ;; Ignore a white-space only fill-prefix
676 ;; if we indent-according-to-mode.
677 (when (and fill-prefix fill-indent-according-to-mode
678 (string-match "\\`[ \t]*\\'" fill-prefix))
679 (setq fill-prefix nil)))
0cb08f98 680
1eb29f50
SM
681 (goto-char from)
682 (beginning-of-line)
683
684 (if (not justify) ; filling disabled: just check indentation
685 (progn
686 (goto-char from)
d47e9c06 687 (while (< (point) to)
1eb29f50
SM
688 (if (and (not (eolp))
689 (< (current-indentation) (current-left-margin)))
690 (fill-indent-to-left-margin))
691 (forward-line 1)))
692
693 (if use-hard-newlines
694 (remove-list-of-text-properties from to '(hard)))
695 ;; Make sure first line is indented (at least) to left margin...
696 (if (or (memq justify '(right center))
697 (< (current-indentation) (current-left-margin)))
698 (fill-indent-to-left-margin))
699 ;; Delete the fill-prefix from every line.
700 (fill-delete-prefix from to fill-prefix)
701 (setq from (point))
702
703 ;; FROM, and point, are now before the text to fill,
704 ;; but after any fill prefix on the first line.
705
706 (fill-delete-newlines from to justify nosqueeze squeeze-after)
707
708 ;; This is the actual filling loop.
709 (goto-char from)
710 (let (linebeg)
711 (while (< (point) to)
712 (setq linebeg (point))
9bcdf9a8 713 (move-to-column (current-fill-column))
1eb29f50
SM
714 (if (when (< (point) to)
715 ;; Find the position where we'll break the line.
9bcdf9a8 716 (forward-char 1) ;Use an immediately following space, if any.
1eb29f50
SM
717 (fill-move-to-break-point linebeg)
718 ;; Check again to see if we got to the end of
719 ;; the paragraph.
720 (skip-chars-forward " \t")
721 (< (point) to))
722 ;; Found a place to cut.
723 (progn
724 (fill-newline)
725 (when justify
726 ;; Justify the line just ended, if desired.
727 (save-excursion
728 (forward-line -1)
729 (justify-current-line justify nil t))))
730
731 (goto-char to)
732 ;; Justify this last line, if desired.
733 (if justify (justify-current-line justify t t))))))
734 ;; Leave point after final newline.
735 (goto-char to)
736 (unless (eobp) (forward-char 1))
27849a65
MB
737 ;; Return the fill-prefix we used
738 fill-prefix)))
739
740(defsubst skip-line-prefix (prefix)
741 "If point is inside the string PREFIX at the beginning of line, move past it."
742 (when (and prefix
743 (< (- (point) (line-beginning-position)) (length prefix))
744 (save-excursion
745 (beginning-of-line)
746 (looking-at (regexp-quote prefix))))
747 (goto-char (match-end 0))))
f53a262d 748
708ca5aa
RS
749(defun fill-minibuffer-function (arg)
750 "Fill a paragraph in the minibuffer, ignoring the prompt."
743a6977 751 (save-restriction
708ca5aa
RS
752 (narrow-to-region (minibuffer-prompt-end) (point-max))
753 (fill-paragraph arg)))
754
b2d35abb 755(defun fill-paragraph (&optional justify region)
a9bdfaa4
EZ
756 "Fill paragraph at or after point.
757
b2d35abb 758If JUSTIFY is non-nil (interactively, with prefix argument), justify as well.
8f985fe2 759If `sentence-end-double-space' is non-nil, then period followed by one
86dfb30a 760space does not end a sentence, so don't break a line there.
368d104a 761the variable `fill-column' controls the width for filling.
86dfb30a
RS
762
763If `fill-paragraph-function' is non-nil, we call it (passing our
27849a65
MB
764argument to it), and if it returns non-nil, we simply return its value.
765
b2d35abb
JL
766If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling.
767
768Interactively (when `region' is non-nil) in Transient Mark mode when
769the mark is active, call `fill-region' to fill each of the paragraphs
770in the active region."
369aeb97
DL
771 (interactive (progn
772 (barf-if-buffer-read-only)
b2d35abb
JL
773 (list (if current-prefix-arg 'full) t)))
774 (or
775 ;; 1. Fill the region if it is active when called interactively.
776 (and region transient-mark-mode mark-active
777 (not (eq (region-beginning) (region-end)))
64b53a1f 778 (or (fill-region (region-beginning) (region-end) justify) t))
b2d35abb
JL
779 ;; 2. Try fill-paragraph-function.
780 (and (not (eq fill-paragraph-function t))
781 (or fill-paragraph-function
782 (and (minibufferp (current-buffer))
783 (= 1 (point-min))))
784 (let ((function (or fill-paragraph-function
785 ;; In the minibuffer, don't count the width
786 ;; of the prompt.
787 'fill-minibuffer-function))
788 ;; If fill-paragraph-function is set, it probably takes care
789 ;; of comments and stuff. If not, it will have to set
790 ;; fill-paragraph-handle-comment back to t explicitly or
791 ;; return nil.
792 (fill-paragraph-handle-comment nil)
793 (fill-paragraph-function t))
794 (funcall function justify)))
795 ;; 3. Try our syntax-aware filling code.
796 (and fill-paragraph-handle-comment
797 ;; Our code only handles \n-terminated comments right now.
798 comment-start (equal comment-end "")
799 (let ((fill-paragraph-handle-comment nil))
800 (fill-comment-paragraph justify)))
801 ;; 4. If it all fails, default to the good ol' text paragraph filling.
802 (let ((before (point))
803 (paragraph-start paragraph-start)
804 ;; Fill prefix used for filling the paragraph.
805 fill-pfx)
806 ;; Try to prevent code sections and comment sections from being
807 ;; filled together.
808 (when (and fill-paragraph-handle-comment comment-start-skip)
809 (setq paragraph-start
810 (concat paragraph-start "\\|[ \t]*\\(?:"
811 comment-start-skip "\\)")))
812 (save-excursion
813 ;; To make sure the return value of forward-paragraph is meaningful,
814 ;; we have to start from the beginning of line, otherwise skipping
815 ;; past the last few chars of a paragraph-separator would count as
816 ;; a paragraph (and not skipping any chars at EOB would not count
817 ;; as a paragraph even if it is).
818 (move-to-left-margin)
819 (if (not (zerop (forward-paragraph)))
820 ;; There's no paragraph at or after point: give up.
821 (setq fill-pfx "")
822 (let ((end (point))
823 (beg (progn (backward-paragraph) (point))))
824 (goto-char before)
825 (setq fill-pfx
826 (if use-hard-newlines
827 ;; Can't use fill-region-as-paragraph, since this
828 ;; paragraph may still contain hard newlines. See
829 ;; fill-region.
830 (fill-region beg end justify)
831 (fill-region-as-paragraph beg end justify))))))
832 fill-pfx)))
f53a262d 833
5cec3056
GM
834(declare-function comment-search-forward "newcomment" (limit &optional noerror))
835(declare-function comment-string-strip "newcomment" (str beforep afterp))
153ef845
DN
836
837
3d9ce27e
SM
838(defun fill-comment-paragraph (&optional justify)
839 "Fill current comment.
840If we're not in a comment, just return nil so that the caller
1ddad36e
SM
841can take care of filling. JUSTIFY is used as in `fill-paragraph'."
842 (comment-normalize-vars)
55f6a280 843 (let (has-code-and-comment ; Non-nil if it contains code and a comment.
1ddad36e 844 comin comstart)
3d9ce27e
SM
845 ;; Figure out what kind of comment we are looking at.
846 (save-excursion
847 (beginning-of-line)
1ddad36e
SM
848 (when (setq comstart (comment-search-forward (line-end-position) t))
849 (setq comin (point))
850 (goto-char comstart) (skip-chars-backward " \t")
851 (setq has-code-and-comment (not (bolp)))))
db95369b 852
1ddad36e 853 (if (not comstart)
3d9ce27e
SM
854 ;; Return nil, so the normal filling will take place.
855 nil
856
857 ;; Narrow to include only the comment, and then fill the region.
1ddad36e 858 (let* ((fill-prefix fill-prefix)
50ef8ff2
SM
859 (commark
860 (comment-string-strip (buffer-substring comstart comin) nil t))
861 (comment-re
af5debda
SM
862 ;; A regexp more specialized than comment-start-skip, that only
863 ;; matches the current commark rather than any valid commark.
743a6977 864 ;;
af5debda
SM
865 ;; The specialized regexp only works for "normal" comment
866 ;; syntax, not for Texinfo's "@c" (which can't be immediately
867 ;; followed by word-chars) or Fortran's "C" (which needs to be
868 ;; at bol), so check that comment-start-skip indeed allows the
869 ;; commark to appear in the middle of the line and followed by
870 ;; word chars. The choice of "\0" and "a" is mostly arbitrary.
871 (if (string-match comment-start-skip (concat "\0" commark "a"))
872 (concat "[ \t]*" (regexp-quote commark)
873 ;; Make sure we only match comments that
874 ;; use the exact same comment marker.
875 "[^" (substring commark -1) "]")
876 (concat "[ \t]*\\(?:" comment-start-skip "\\)")))
877 (comment-fill-prefix ; Compute a fill prefix.
1ddad36e
SM
878 (save-excursion
879 (goto-char comstart)
880 (if has-code-and-comment
8b44bc06
SM
881 (concat
882 (if (not indent-tabs-mode)
8ad8316c 883 (make-string (current-column) ?\s)
8b44bc06
SM
884 (concat
885 (make-string (/ (current-column) tab-width) ?\t)
8ad8316c 886 (make-string (% (current-column) tab-width) ?\s)))
8b44bc06 887 (buffer-substring (point) comin))
1ddad36e
SM
888 (buffer-substring (line-beginning-position) comin))))
889 beg end)
890 (save-excursion
891 (save-restriction
892 (beginning-of-line)
893 (narrow-to-region
894 ;; Find the first line we should include in the region to fill.
895 (if has-code-and-comment
896 (line-beginning-position)
897 (save-excursion
898 (while (and (zerop (forward-line -1))
899 (looking-at comment-re)))
900 ;; We may have gone too far. Go forward again.
901 (line-beginning-position
5a7139ee
SM
902 (if (progn
903 (goto-char
904 (or (comment-search-forward (line-end-position) t)
905 (point)))
906 (looking-at comment-re))
55f6a280
SM
907 (progn (setq comstart (point)) 1)
908 (progn (setq comstart (point)) 2)))))
909 ;; Find the beginning of the first line past the region to fill.
910 (save-excursion
911 (while (progn (forward-line 1)
912 (looking-at comment-re)))
913 (point)))
1ddad36e
SM
914 ;; Obey paragraph starters and boundaries within comments.
915 (let* ((paragraph-separate
916 ;; Use the default values since they correspond to
917 ;; the values to use for plain text.
918 (concat paragraph-separate "\\|[ \t]*\\(?:"
919 comment-start-skip "\\)\\(?:"
920 (default-value 'paragraph-separate) "\\)"))
921 (paragraph-start
922 (concat paragraph-start "\\|[ \t]*\\(?:"
923 comment-start-skip "\\)\\(?:"
924 (default-value 'paragraph-start) "\\)"))
55f6a280 925 ;; We used to rely on fill-prefix to break paragraph at
e4b7444d
SM
926 ;; comment-starter changes, but it did not work for the
927 ;; first line (mixed comment&code).
928 ;; We now use comment-re instead to "manually" make sure
929 ;; we treat comment-marker changes as paragraph boundaries.
930 ;; (paragraph-ignore-fill-prefix nil)
931 ;; (fill-prefix comment-fill-prefix)
1ddad36e 932 (after-line (if has-code-and-comment
1eb29f50
SM
933 (line-beginning-position 2))))
934 (setq end (progn (forward-paragraph) (point)))
1ddad36e
SM
935 ;; If this comment starts on a line with code,
936 ;; include that line in the filling.
937 (setq beg (progn (backward-paragraph)
938 (if (eq (point) after-line)
939 (forward-line -1))
1eb29f50 940 (point)))))
1ddad36e
SM
941
942 ;; Find the fill-prefix to use.
943 (cond
55f6a280 944 (fill-prefix) ; Use the user-provided fill prefix.
1ddad36e
SM
945 ((and adaptive-fill-mode ; Try adaptive fill mode.
946 (setq fill-prefix (fill-context-prefix beg end))
947 (string-match comment-start-skip fill-prefix)))
948 (t
949 (setq fill-prefix comment-fill-prefix)))
950
951 ;; Don't fill with narrowing.
952 (or
953 (fill-region-as-paragraph
55f6a280 954 (max comstart beg) end justify nil
1ddad36e
SM
955 ;; Don't canonicalize spaces within the code just before
956 ;; the comment.
957 (save-excursion
958 (goto-char beg)
959 (if (looking-at fill-prefix)
960 nil
961 (re-search-forward comment-start-skip))))
962 ;; Make sure we don't return nil.
963 t))))))
3d9ce27e 964
0cb08f98 965(defun fill-region (from to &optional justify nosqueeze to-eop)
f53a262d 966 "Fill each of the paragraphs in the region.
73b33545 967A prefix arg means justify as well.
368d104a 968Ordinarily the variable `fill-column' controls the width.
0cb08f98 969
73b33545
RS
970Noninteractively, the third argument JUSTIFY specifies which
971kind of justification to do: `full', `left', `right', `center',
743a6977
JB
972or `none' (equivalent to nil). A value of t means handle each
973paragraph as specified by its text properties.
d09af6a5 974
743a6977
JB
975The fourth arg NOSQUEEZE non-nil means to leave whitespace other
976than line breaks untouched, and fifth arg TO-EOP non-nil means
977to keep filling to the end of the paragraph (or next hard newline,
978if variable `use-hard-newlines' is on).
0cb08f98 979
27849a65
MB
980Return the fill-prefix used for filling the last paragraph.
981
8f985fe2
RS
982If `sentence-end-double-space' is non-nil, then period followed by one
983space does not end a sentence, so don't break a line there."
369aeb97
DL
984 (interactive (progn
985 (barf-if-buffer-read-only)
986 (list (region-beginning) (region-end)
987 (if current-prefix-arg 'full))))
d09af6a5 988 (unless (memq justify '(t nil none full center left right))
73b33545 989 (setq justify 'full))
ef11ff9b
SM
990 (let (max beg fill-pfx)
991 (goto-char (max from to))
992 (when to-eop
993 (skip-chars-backward "\n")
994 (forward-paragraph))
995 (setq max (copy-marker (point) t))
996 (goto-char (setq beg (min from to)))
997 (beginning-of-line)
998 (while (< (point) max)
999 (let ((initial (point))
1000 end)
1001 ;; If using hard newlines, break at every one for filling
1002 ;; purposes rather than using paragraph breaks.
1003 (if use-hard-newlines
1004 (progn
1005 (while (and (setq end (text-property-any (point) max
1006 'hard t))
1007 (not (= ?\n (char-after end)))
1008 (not (>= end max)))
1009 (goto-char (1+ end)))
1010 (setq end (if end (min max (1+ end)) max))
1011 (goto-char initial))
1012 (forward-paragraph 1)
1013 (setq end (min max (point)))
1014 (forward-paragraph -1))
1015 (if (< (point) beg)
1016 (goto-char beg))
1017 (if (>= (point) initial)
1018 (setq fill-pfx
1019 (fill-region-as-paragraph (point) end justify nosqueeze))
1020 (goto-char end))))
1021 fill-pfx))
f53a262d 1022
0cb08f98 1023\f
9d325ebf 1024(defcustom default-justification 'left
0cb08f98
RS
1025 "*Method of justifying text not otherwise specified.
1026Possible values are `left', `right', `full', `center', or `none'.
1027The requested kind of justification is done whenever lines are filled.
03e3e2e9 1028The `justification' text-property can locally override this variable."
9d325ebf
RS
1029 :type '(choice (const left)
1030 (const right)
1031 (const full)
1032 (const center)
1033 (const none))
1034 :group 'fill)
0cb08f98
RS
1035(make-variable-buffer-local 'default-justification)
1036
f43726fd 1037(defun current-justification ()
0cb08f98
RS
1038 "How should we justify this line?
1039This returns the value of the text-property `justification',
1040or the variable `default-justification' if there is no text-property.
1041However, it returns nil rather than `none' to mean \"don't justify\"."
db95369b 1042 (let ((j (or (get-text-property
0cb08f98 1043 ;; Make sure we're looking at paragraph body.
db95369b 1044 (save-excursion (skip-chars-forward " \t")
1095bc3c
BG
1045 (if (and (eobp) (not (bobp)))
1046 (1- (point)) (point)))
0cb08f98
RS
1047 'justification)
1048 default-justification)))
1049 (if (eq 'none j)
1050 nil
1051 j)))
1052
c3f9a06a
RS
1053(defun set-justification (begin end style &optional whole-par)
1054 "Set the region's justification style to STYLE.
1055This commands prompts for the kind of justification to use.
1095bc3c 1056If the mark is not active, this command operates on the current paragraph.
c3f9a06a
RS
1057If the mark is active, it operates on the region. However, if the
1058beginning and end of the region are not at paragraph breaks, they are
1059moved to the beginning and end \(respectively) of the paragraphs they
1060are in.
1061
e4b7444d
SM
1062If variable `use-hard-newlines' is true, all hard newlines are
1063taken to be paragraph breaks.
1095bc3c
BG
1064
1065When calling from a program, operates just on region between BEGIN and END,
1066unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
1067extended to include entire paragraphs as in the interactive command."
0cb08f98
RS
1068 (interactive (list (if mark-active (region-beginning) (point))
1069 (if mark-active (region-end) (point))
1095bc3c 1070 (let ((s (completing-read
0cb08f98 1071 "Set justification to: "
1095bc3c
BG
1072 '(("left") ("right") ("full")
1073 ("center") ("none"))
0cb08f98 1074 nil t)))
1095bc3c
BG
1075 (if (equal s "") (error ""))
1076 (intern s))
1077 t))
1078 (save-excursion
1079 (save-restriction
1080 (if whole-par
1081 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
db95369b 1082 (paragraph-ignore-fill-prefix (if use-hard-newlines t
1095bc3c
BG
1083 paragraph-ignore-fill-prefix)))
1084 (goto-char begin)
1085 (while (and (bolp) (not (eobp))) (forward-char 1))
1086 (backward-paragraph)
1087 (setq begin (point))
1088 (goto-char end)
1089 (skip-chars-backward " \t\n" begin)
1090 (forward-paragraph)
1091 (setq end (point))))
1092
1093 (narrow-to-region (point-min) end)
1094 (unjustify-region begin (point-max))
c3f9a06a 1095 (put-text-property begin (point-max) 'justification style)
1095bc3c 1096 (fill-region begin (point-max) nil t))))
0cb08f98
RS
1097
1098(defun set-justification-none (b e)
1099 "Disable automatic filling for paragraphs in the region.
1100If the mark is not active, this applies to the current paragraph."
1095bc3c
BG
1101 (interactive (list (if mark-active (region-beginning) (point))
1102 (if mark-active (region-end) (point))))
1103 (set-justification b e 'none t))
0cb08f98
RS
1104
1105(defun set-justification-left (b e)
1106 "Make paragraphs in the region left-justified.
c3f9a06a 1107This means they are flush at the left margin and ragged on the right.
1095bc3c 1108This is usually the default, but see the variable `default-justification'.
0cb08f98 1109If the mark is not active, this applies to the current paragraph."
1095bc3c
BG
1110 (interactive (list (if mark-active (region-beginning) (point))
1111 (if mark-active (region-end) (point))))
1112 (set-justification b e 'left t))
0cb08f98
RS
1113
1114(defun set-justification-right (b e)
d9a49740 1115 "Make paragraphs in the region right-justified.
c3f9a06a 1116This means they are flush at the right margin and ragged on the left.
0cb08f98 1117If the mark is not active, this applies to the current paragraph."
1095bc3c
BG
1118 (interactive (list (if mark-active (region-beginning) (point))
1119 (if mark-active (region-end) (point))))
1120 (set-justification b e 'right t))
0cb08f98
RS
1121
1122(defun set-justification-full (b e)
d9a49740 1123 "Make paragraphs in the region fully justified.
1095bc3c 1124This makes lines flush on both margins by inserting spaces between words.
0cb08f98 1125If the mark is not active, this applies to the current paragraph."
1095bc3c
BG
1126 (interactive (list (if mark-active (region-beginning) (point))
1127 (if mark-active (region-end) (point))))
1128 (set-justification b e 'full t))
0cb08f98
RS
1129
1130(defun set-justification-center (b e)
1131 "Make paragraphs in the region centered.
1132If the mark is not active, this applies to the current paragraph."
1095bc3c
BG
1133 (interactive (list (if mark-active (region-beginning) (point))
1134 (if mark-active (region-end) (point))))
1135 (set-justification b e 'center t))
1136
1137;; A line has up to six parts:
1138;;
db95369b 1139;; >>> hello.
1095bc3c
BG
1140;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
1141;;
1142;; "Indent-1" is the left-margin indentation; normally it ends at column
1143;; given by the `current-left-margin' function.
1144;; "FP" is the fill-prefix. It can be any string, including whitespace.
1145;; "Indent-2" is added to justify a line if the `current-justification' is
1146;; `center' or `right'. In `left' and `full' justification regions, any
1147;; whitespace there is part of the line's text, and should not be changed.
1148;; Trailing whitespace is not counted as part of the line length when
1149;; center- or right-justifying.
1150;;
db95369b 1151;; All parts of the line are optional, although the final newline can
1095bc3c 1152;; only be missing on the last line of the buffer.
0cb08f98
RS
1153
1154(defun justify-current-line (&optional how eop nosqueeze)
1095bc3c
BG
1155 "Do some kind of justification on this line.
1156Normally does full justification: adds spaces to the line to make it end at
1157the column given by `current-fill-column'.
0cb08f98 1158Optional first argument HOW specifies alternate type of justification:
db95369b 1159it can be `left', `right', `full', `center', or `none'.
1095bc3c
BG
1160If HOW is t, will justify however the `current-justification' function says to.
1161If HOW is nil or missing, full justification is done by default.
0cb08f98
RS
1162Second arg EOP non-nil means that this is the last line of the paragraph, so
1163it will not be stretched by full justification.
1164Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
1165otherwise it is made canonical."
369aeb97 1166 (interactive "*")
d09d7ba9
RS
1167 (if (eq t how) (setq how (or (current-justification) 'none))
1168 (if (null how) (setq how 'full)
1169 (or (memq how '(none left right center))
1170 (setq how 'full))))
1095bc3c
BG
1171 (or (memq how '(none left)) ; No action required for these.
1172 (let ((fc (current-fill-column))
1173 (pos (point-marker))
1174 fp-end ; point at end of fill prefix
1175 beg ; point at beginning of line's text
1176 end ; point at end of line's text
1177 indent ; column of `beg'
1178 endcol ; column of `end'
9fc0913c
RS
1179 ncols ; new indent point or offset
1180 (nspaces 0) ; number of spaces between words
1181 ; in line (not space characters)
9fc0913c
RS
1182 (curr-fracspace 0) ; current fractional space amount
1183 count)
1095bc3c
BG
1184 (end-of-line)
1185 ;; Check if this is the last line of the paragraph.
db95369b 1186 (if (and use-hard-newlines (null eop)
1095bc3c
BG
1187 (get-text-property (point) 'hard))
1188 (setq eop t))
1189 (skip-chars-backward " \t")
1190 ;; Quick exit if it appears to be properly justified already
1191 ;; or there is no text.
1192 (if (or (bolp)
1193 (and (memq how '(full right))
1194 (= (current-column) fc)))
1195 nil
1196 (setq end (point))
1197 (beginning-of-line)
1198 (skip-chars-forward " \t")
1199 ;; Skip over fill-prefix.
db95369b 1200 (if (and fill-prefix
1095bc3c
BG
1201 (not (string-equal fill-prefix ""))
1202 (equal fill-prefix
db95369b 1203 (buffer-substring
1095bc3c
BG
1204 (point) (min (point-max) (+ (length fill-prefix)
1205 (point))))))
1206 (forward-char (length fill-prefix))
db95369b 1207 (if (and adaptive-fill-mode
1095bc3c
BG
1208 (looking-at adaptive-fill-regexp))
1209 (goto-char (match-end 0))))
1210 (setq fp-end (point))
1211 (skip-chars-forward " \t")
1212 ;; This is beginning of the line's text.
1213 (setq indent (current-column))
1214 (setq beg (point))
1215 (goto-char end)
1216 (setq endcol (current-column))
1217
1218 ;; HOW can't be null or left--we would have exited already
db95369b 1219 (cond ((eq 'right how)
1095bc3c
BG
1220 (setq ncols (- fc endcol))
1221 (if (< ncols 0)
1222 ;; Need to remove some indentation
db95369b 1223 (delete-region
1095bc3c
BG
1224 (progn (goto-char fp-end)
1225 (if (< (current-column) (+ indent ncols))
1226 (move-to-column (+ indent ncols) t))
1227 (point))
1228 (progn (move-to-column indent) (point)))
1229 ;; Need to add some
1230 (goto-char beg)
1231 (indent-to (+ indent ncols))
1232 ;; If point was at beginning of text, keep it there.
db95369b 1233 (if (= beg pos)
1095bc3c
BG
1234 (move-marker pos (point)))))
1235
1236 ((eq 'center how)
1237 ;; Figure out how much indentation is needed
1238 (setq ncols (+ (current-left-margin)
1239 (/ (- fc (current-left-margin) ;avail. space
1240 (- endcol indent)) ;text width
1241 2)))
1242 (if (< ncols indent)
1243 ;; Have too much indentation - remove some
1244 (delete-region
1245 (progn (goto-char fp-end)
1246 (if (< (current-column) ncols)
1247 (move-to-column ncols t))
1248 (point))
1249 (progn (move-to-column indent) (point)))
1250 ;; Have too little - add some
1251 (goto-char beg)
1252 (indent-to ncols)
1253 ;; If point was at beginning of text, keep it there.
1254 (if (= beg pos)
1255 (move-marker pos (point)))))
1256
1257 ((eq 'full how)
1258 ;; Insert extra spaces between words to justify line
1259 (save-restriction
5f636376
RS
1260 (narrow-to-region beg end)
1261 (or nosqueeze
1262 (canonically-space-region beg end))
1263 (goto-char (point-max))
9fc0913c
RS
1264 ;; count word spaces in line
1265 (while (search-backward " " nil t)
1266 (setq nspaces (1+ nspaces))
1267 (skip-chars-backward " "))
1095bc3c 1268 (setq ncols (- fc endcol))
9fc0913c
RS
1269 ;; Ncols is number of additional space chars needed
1270 (if (and (> ncols 0) (> nspaces 0) (not eop))
1271 (progn
1272 (setq curr-fracspace (+ ncols (/ (1+ nspaces) 2))
1273 count nspaces)
1274 (while (> count 0)
1275 (skip-chars-forward " ")
1276 (insert-and-inherit
8ad8316c 1277 (make-string (/ curr-fracspace nspaces) ?\s))
9fc0913c
RS
1278 (search-forward " " nil t)
1279 (setq count (1- count)
1280 curr-fracspace
1281 (+ (% curr-fracspace nspaces) ncols)))))))
1095bc3c
BG
1282 (t (error "Unknown justification value"))))
1283 (goto-char pos)
1284 (move-marker pos nil)))
9dfcfbc9 1285 nil)
30d653c4 1286
1095bc3c
BG
1287(defun unjustify-current-line ()
1288 "Remove justification whitespace from current line.
1289If the line is centered or right-justified, this function removes any
5512735e 1290indentation past the left margin. If the line is full-justified, it removes
1095bc3c
BG
1291extra spaces between words. It does nothing in other justification modes."
1292 (let ((justify (current-justification)))
1293 (cond ((eq 'left justify) nil)
1294 ((eq nil justify) nil)
1295 ((eq 'full justify) ; full justify: remove extra spaces
1296 (beginning-of-line-text)
742c1822 1297 (canonically-space-region (point) (line-end-position)))
1095bc3c
BG
1298 ((memq justify '(center right))
1299 (save-excursion
1300 (move-to-left-margin nil t)
1301 ;; Position ourselves after any fill-prefix.
db95369b 1302 (if (and fill-prefix
1095bc3c
BG
1303 (not (string-equal fill-prefix ""))
1304 (equal fill-prefix
db95369b 1305 (buffer-substring
1095bc3c
BG
1306 (point) (min (point-max) (+ (length fill-prefix)
1307 (point))))))
1308 (forward-char (length fill-prefix)))
1309 (delete-region (point) (progn (skip-chars-forward " \t")
1310 (point))))))))
1311
1312(defun unjustify-region (&optional begin end)
1313 "Remove justification whitespace from region.
1314For centered or right-justified regions, this function removes any indentation
db95369b 1315past the left margin from each line. For full-justified lines, it removes
1095bc3c
BG
1316extra spaces between words. It does nothing in other justification modes.
1317Arguments BEGIN and END are optional; default is the whole buffer."
1318 (save-excursion
1319 (save-restriction
1320 (if end (narrow-to-region (point-min) end))
1321 (goto-char (or begin (point-min)))
1322 (while (not (eobp))
1323 (unjustify-current-line)
1324 (forward-line 1)))))
1325
f53a262d 1326\f
b3a0387c 1327(defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
e407986c
RS
1328 "Fill paragraphs within the region, allowing varying indentation within each.
1329This command divides the region into \"paragraphs\",
1330only at paragraph-separator lines, then fills each paragraph
1331using as the fill prefix the smallest indentation of any line
1332in the paragraph.
1333
1334When calling from a program, pass range to fill as first two arguments.
e065a56e 1335
8ad8316c
JB
1336Optional third and fourth arguments JUSTIFYP and CITATION-REGEXP:
1337JUSTIFYP to justify paragraphs (prefix arg).
b3a0387c
RS
1338When filling a mail message, pass a regexp for CITATION-REGEXP
1339which will match the prefix of a line which is a citation marker
1340plus whitespace, but no other kind of prefix.
f7a53cae 1341Also, if CITATION-REGEXP is non-nil, don't fill header lines."
369aeb97
DL
1342 (interactive (progn
1343 (barf-if-buffer-read-only)
1344 (list (region-beginning) (region-end)
1345 (if current-prefix-arg 'full))))
e407986c 1346 (let ((fill-individual-varying-indent t))
b3a0387c 1347 (fill-individual-paragraphs min max justifyp citation-regexp)))
e407986c 1348
b3a0387c 1349(defun fill-individual-paragraphs (min max &optional justify citation-regexp)
e407986c 1350 "Fill paragraphs of uniform indentation within the region.
db95369b 1351This command divides the region into \"paragraphs\",
f37bbf08 1352treating every change in indentation level or prefix as a paragraph boundary,
e407986c 1353then fills each paragraph using its indentation level as the fill prefix.
e065a56e 1354
f37bbf08
RS
1355There is one special case where a change in indentation does not start
1356a new paragraph. This is for text of this form:
1357
1358 foo> This line with extra indentation starts
1359 foo> a paragraph that continues on more lines.
1360
1361These lines are filled together.
1362
1363When calling from a program, pass the range to fill
1364as the first two arguments.
e065a56e 1365
3e4dfbb6 1366Optional third and fourth arguments JUSTIFY and CITATION-REGEXP:
fdd2ae6d 1367JUSTIFY to justify paragraphs (prefix arg).
b3a0387c
RS
1368When filling a mail message, pass a regexp for CITATION-REGEXP
1369which will match the prefix of a line which is a citation marker
1370plus whitespace, but no other kind of prefix.
f7a53cae 1371Also, if CITATION-REGEXP is non-nil, don't fill header lines."
369aeb97
DL
1372 (interactive (progn
1373 (barf-if-buffer-read-only)
1374 (list (region-beginning) (region-end)
1375 (if current-prefix-arg 'full))))
aa228418
JB
1376 (save-restriction
1377 (save-excursion
1378 (goto-char min)
1379 (beginning-of-line)
a461b8e0 1380 (narrow-to-region (point) max)
b3a0387c 1381 (if citation-regexp
a461b8e0 1382 (while (and (not (eobp))
d09d7ba9 1383 (or (looking-at "[ \t]*[^ \t\n]+:")
a461b8e0 1384 (looking-at "[ \t]*$")))
d09d7ba9 1385 (if (looking-at "[ \t]*[^ \t\n]+:")
30b786c3
RS
1386 (search-forward "\n\n" nil 'move)
1387 (forward-line 1))))
aa228418
JB
1388 (narrow-to-region (point) max)
1389 ;; Loop over paragraphs.
d95da8d3 1390 (while (progn
4574ee6c
RS
1391 ;; Skip over all paragraph-separating lines
1392 ;; so as to not include them in any paragraph.
183e4bd6
KH
1393 (while (and (not (eobp))
1394 (progn (move-to-left-margin)
1395 (and (not (eobp))
1396 (looking-at paragraph-separate))))
4574ee6c
RS
1397 (forward-line 1))
1398 (skip-chars-forward " \t\n") (not (eobp)))
16cf6ab2 1399 (move-to-left-margin)
aa228418
JB
1400 (let ((start (point))
1401 fill-prefix fill-prefix-regexp)
1402 ;; Find end of paragraph, and compute the smallest fill-prefix
1403 ;; that fits all the lines in this paragraph.
1404 (while (progn
1405 ;; Update the fill-prefix on the first line
1406 ;; and whenever the prefix good so far is too long.
1407 (if (not (and fill-prefix
1408 (looking-at fill-prefix-regexp)))
1409 (setq fill-prefix
742c1822
DL
1410 (fill-individual-paragraphs-prefix
1411 citation-regexp)
16cf6ab2 1412 fill-prefix-regexp (regexp-quote fill-prefix)))
c6286174 1413 (forward-line 1)
7b9f0657
RS
1414 (if (bolp)
1415 ;; If forward-line went past a newline,
1416 ;; move further to the left margin.
1417 (move-to-left-margin))
aa228418
JB
1418 ;; Now stop the loop if end of paragraph.
1419 (and (not (eobp))
e065a56e
JB
1420 (if fill-individual-varying-indent
1421 ;; If this line is a separator line, with or
1422 ;; without prefix, end the paragraph.
db95369b 1423 (and
c6286174
RS
1424 (not (looking-at paragraph-separate))
1425 (save-excursion
1426 (not (and (looking-at fill-prefix-regexp)
742c1822
DL
1427 (progn (forward-char
1428 (length fill-prefix))
1429 (looking-at
1430 paragraph-separate))))))
e065a56e
JB
1431 ;; If this line has more or less indent
1432 ;; than the fill prefix wants, end the paragraph.
1433 (and (looking-at fill-prefix-regexp)
cc7e9720
KH
1434 ;; If fill prefix is shorter than a new
1435 ;; fill prefix computed here, end paragraph.
1436 (let ((this-line-fill-prefix
db95369b 1437 (fill-individual-paragraphs-prefix
cc7e9720 1438 citation-regexp)))
db95369b 1439 (>= (length fill-prefix)
cc7e9720 1440 (length this-line-fill-prefix)))
e065a56e 1441 (save-excursion
742c1822
DL
1442 (not (progn (forward-char
1443 (length fill-prefix))
f37bbf08
RS
1444 (or (looking-at "[ \t]")
1445 (looking-at paragraph-separate)
3b99a4f8
GM
1446 (looking-at paragraph-start)))))
1447 (not (and (equal fill-prefix "")
1448 citation-regexp
1449 (looking-at citation-regexp))))))))
aa228418
JB
1450 ;; Fill this paragraph, but don't add a newline at the end.
1451 (let ((had-newline (bolp)))
0cb08f98 1452 (fill-region-as-paragraph start (point) justify)
c97a5db4
KH
1453 (if (and (bolp) (not had-newline))
1454 (delete-char -1))))))))
3e4dfbb6 1455
b3a0387c 1456(defun fill-individual-paragraphs-prefix (citation-regexp)
03e3e2e9
SM
1457 (let* ((adaptive-fill-first-line-regexp ".*")
1458 (just-one-line-prefix
1459 ;; Accept any prefix rather than just the ones matched by
1460 ;; adaptive-fill-first-line-regexp.
1461 (fill-context-prefix (point) (line-beginning-position 2)))
1462 (two-lines-prefix
1463 (fill-context-prefix (point) (line-beginning-position 3))))
1464 (if (not just-one-line-prefix)
1465 (buffer-substring
1466 (point) (save-excursion (skip-chars-forward " \t") (point)))
b3a0387c
RS
1467 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1468 ;; is the same as that of TWO-LINES-PREFIX,
1469 ;; except perhaps with longer whitespace.
03e3e2e9
SM
1470 (if (and just-one-line-prefix two-lines-prefix
1471 (let* ((one-line-citation-part
1472 (fill-individual-paragraphs-citation
1473 just-one-line-prefix citation-regexp))
1474 (two-lines-citation-part
1475 (fill-individual-paragraphs-citation
1476 two-lines-prefix citation-regexp))
1477 (adjusted-two-lines-citation-part
1478 (substring two-lines-citation-part 0
1479 (string-match "[ \t]*\\'"
1480 two-lines-citation-part))))
1481 (and
b3a0387c 1482 (string-match (concat "\\`"
742c1822
DL
1483 (regexp-quote
1484 adjusted-two-lines-citation-part)
b3a0387c
RS
1485 "[ \t]*\\'")
1486 one-line-citation-part)
1487 (>= (string-width one-line-citation-part)
03e3e2e9 1488 (string-width two-lines-citation-part)))))
b3a0387c 1489 two-lines-prefix
03e3e2e9 1490 just-one-line-prefix))))
b3a0387c
RS
1491
1492(defun fill-individual-paragraphs-citation (string citation-regexp)
03e3e2e9
SM
1493 (if citation-regexp
1494 (if (string-match citation-regexp string)
1495 (match-string 0 string)
1496 "")
1497 string))
b3a0387c 1498
d95da8d3 1499;; arch-tag: 727ad455-1161-4fa9-8df5-0f74b179216d
e5d77022 1500;;; fill.el ends here