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