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