1 ;;; fill.el --- fill commands for Emacs -*- coding: utf-8 -*-
3 ;; Copyright (C) 1985-1986, 1992, 1994-1997, 1999, 2001-2012
4 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; All the commands for filling text. These are documented in the Emacs
33 "Indenting and filling text."
34 :link
'(custom-manual "(emacs)Filling")
37 (defcustom fill-individual-varying-indent nil
38 "Controls criterion for a new paragraph in `fill-individual-paragraphs'.
39 Non-nil means changing indent doesn't end a paragraph.
40 That mode can handle paragraphs with extra indentation on the first line,
41 but it requires separator lines between paragraphs.
42 A value of nil means that any change in indentation starts a new paragraph."
46 (defcustom colon-double-space nil
47 "Non-nil means put two spaces after a colon when filling."
50 (put 'colon-double-space
'safe-local-variable
'booleanp
)
52 (defvar fill-paragraph-function nil
53 "Mode-specific function to fill a paragraph, or nil if there is none.
54 If the function returns nil, then `fill-paragraph' does its normal work.
55 A value of t means explicitly \"do nothing special\".
56 Note: This only affects `fill-paragraph' and not `fill-region'
57 nor `auto-fill-mode', so it is often better to use some other hook,
58 such as `fill-forward-paragraph-function'.")
60 (defvar fill-paragraph-handle-comment t
61 "Non-nil means paragraph filling will try to pay attention to comments.")
63 (defcustom enable-kinsoku t
64 "Non-nil means enable \"kinsoku\" processing on filling paragraphs.
65 Kinsoku processing is designed to prevent certain characters from being
66 placed at the beginning or end of a line by filling.
67 See the documentation of `kinsoku' for more information."
71 (defun set-fill-prefix ()
72 "Set the fill prefix to the current line up to point.
73 Filling expects lines to start with the fill prefix and
74 reinserts the fill prefix in each resulting line."
76 (let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
77 (if (> (point) left-margin-pos
)
79 (setq fill-prefix
(buffer-substring left-margin-pos
(point)))
80 (if (equal fill-prefix
"")
81 (setq fill-prefix nil
)))
82 (setq fill-prefix nil
)))
84 (message "fill-prefix: \"%s\"" fill-prefix
)
85 (message "fill-prefix canceled")))
87 (defcustom adaptive-fill-mode t
88 "Non-nil means determine a paragraph's fill prefix from its text."
92 (defcustom adaptive-fill-regexp
93 ;; Added `!' for doxygen comments starting with `//!' or `/*!'.
94 ;; Added `%' for TeX comments.
95 ;; RMS: deleted the code to match `1.' and `(1)'.
96 ;; Update mail-mode's paragraph-separate if you change this.
97 (purecopy "[ \t]*\\([-–!|#%;>*·•‣⁃◦]+[ \t]*\\)*")
98 "Regexp to match text at start of line that constitutes indentation.
99 If Adaptive Fill mode is enabled, a prefix matching this pattern
100 on the first and second lines of a paragraph is used as the
101 standard indentation for the whole paragraph.
103 If the paragraph has just one line, the indentation is taken from that
104 line, but in that case `adaptive-fill-first-line-regexp' also plays
109 (defcustom adaptive-fill-first-line-regexp
(purecopy "\\`[ \t]*\\'")
110 "Regexp specifying whether to set fill prefix from a one-line paragraph.
111 When a paragraph has just one line, then after `adaptive-fill-regexp'
112 finds the prefix at the beginning of the line, if it doesn't
113 match this regexp, it is replaced with whitespace.
115 By default, this regexp matches sequences of just spaces and tabs.
117 However, we never use a prefix from a one-line paragraph
118 if it would act as a paragraph-starter on the second line."
122 (defcustom adaptive-fill-function nil
123 "Function to call to choose a fill prefix for a paragraph, or nil.
124 A nil value means the function has not determined the fill prefix."
125 :type
'(choice (const nil
) function
)
128 (defvar fill-indent-according-to-mode nil
;Screws up CC-mode's filling tricks.
129 "Whether or not filling should try to use the major mode's indentation.")
131 (defun current-fill-column ()
132 "Return the fill-column to use for this line.
133 The fill-column to use for a buffer is stored in the variable `fill-column',
134 but can be locally modified by the `right-margin' text property, which is
135 subtracted from `fill-column'.
137 The fill column to use for a line is the first column at which the column
138 number equals or exceeds the local fill-column - right-margin difference."
141 (let* ((here (line-beginning-position))
143 (eol (progn (end-of-line) (point)))
144 margin fill-col change col
)
145 ;; Look separately at each region of line with a different
147 (while (and (setq margin
(get-text-property here
'right-margin
)
148 fill-col
(- fill-column
(or margin
0))
149 change
(text-property-not-all
150 here eol
'right-margin margin
))
151 (progn (goto-char (1- change
))
152 (setq col
(current-column))
156 (max here-col fill-col
)))))
158 (defun canonically-space-region (beg end
)
159 "Remove extra spaces between words in region.
160 Leave one space between words, two at end of sentences or after colons
161 \(depending on values of `sentence-end-double-space', `colon-double-space',
162 and `sentence-end-without-period').
163 Remove indentation from each line."
165 ;; Ideally, we'd want to scan the text from the end, so that changes to
166 ;; text don't affect the boundary, but the regexp we match against does
167 ;; not match as eagerly when matching backward, so we instead use
169 (unless (markerp end
) (setq end
(copy-marker end t
)))
170 (let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +")))
173 ;; Nuke tabs; they get screwed up in a fill.
174 ;; This is quick, but loses when a tab follows the end of a sentence.
175 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
177 (subst-char-in-region beg end ?
\t ?\s
)
178 (while (and (< (point) end
)
179 (re-search-forward end-spc-re end t
))
182 ;; `sentence-end' matched and did not match all spaces.
183 ;; I.e. it only matched the number of spaces it needs: drop the rest.
184 ((and (match-end 1) (> (match-end 0) (match-end 1))) (match-end 1))
185 ;; `sentence-end' matched but with nothing left. Either that means
186 ;; nothing should be removed, or it means it's the "old-style"
187 ;; sentence-end which matches all it can. Keep only 2 spaces.
188 ;; We probably don't even need to check `sentence-end-double-space'.
191 (+ (if sentence-end-double-space
2 1)
192 (save-excursion (goto-char (match-end 0))
193 (skip-chars-backward " ")
195 (t ;; It's not an end of sentence.
196 (+ (match-beginning 0)
197 ;; Determine number of spaces to leave:
199 (skip-chars-backward " ]})\"'")
200 (cond ((and sentence-end-double-space
201 (or (memq (preceding-char) '(?. ?? ?
!))
202 (and sentence-end-without-period
203 (= (char-syntax (preceding-char)) ?w
)))) 2)
204 ((and colon-double-space
205 (= (preceding-char) ?
:)) 2)
206 ((char-equal (preceding-char) ?
\n) 0)
210 (defun fill-common-string-prefix (s1 s2
)
211 "Return the longest common prefix of strings S1 and S2, or nil if none."
212 (let ((cmp (compare-strings s1 nil nil s2 nil nil
)))
215 (setq cmp
(1- (abs cmp
)))
217 (substring s1
0 cmp
)))))
219 (defun fill-match-adaptive-prefix ()
221 (and adaptive-fill-function
(funcall adaptive-fill-function
))
222 (and adaptive-fill-regexp
(looking-at adaptive-fill-regexp
)
223 (match-string-no-properties 0)))))
224 (if (>= (+ (current-left-margin) (length str
)) (current-fill-column))
225 ;; Death to insanely long prefixes.
229 (defun fill-context-prefix (from to
&optional first-line-regexp
)
230 "Compute a fill prefix from the text between FROM and TO.
231 This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
232 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
233 we reject a prefix based on a one-line paragraph if that prefix would
234 act as a paragraph-separator."
235 (or first-line-regexp
236 (setq first-line-regexp adaptive-fill-first-line-regexp
))
239 (if (eolp) (forward-line 1))
240 ;; Move to the second line unless there is just one.
241 (move-to-left-margin)
242 (let (first-line-prefix
243 ;; Non-nil if we are on the second line.
245 (setq first-line-prefix
246 ;; We don't need to consider `paragraph-start' here since it
247 ;; will be explicitly checked later on.
248 ;; Also setting first-line-prefix to nil prevents
249 ;; second-line-prefix from being used.
250 ;; ((looking-at paragraph-start) nil)
251 (fill-match-adaptive-prefix))
255 (move-to-left-margin)
256 (setq second-line-prefix
257 (cond ((looking-at paragraph-start
) nil
) ;Can it happen? -Stef
258 (t (fill-match-adaptive-prefix))))
259 ;; If we get a fill prefix from the second line,
260 ;; make sure it or something compatible is on the first line too.
261 (when second-line-prefix
262 (unless first-line-prefix
(setq first-line-prefix
""))
263 ;; If the non-whitespace chars match the first line,
264 ;; just use it (this subsumes the 2 checks used previously).
265 ;; Used when first line is `/* ...' and second-line is
267 (let ((tmp second-line-prefix
)
269 (while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp
)
270 (setq re
(concat re
".*" (regexp-quote (match-string 1 tmp
))))
271 (setq tmp
(substring tmp
(match-end 0))))
272 ;; (assert (string-match "\\`[ \t]*\\'" tmp))
274 (if (string-match re first-line-prefix
)
277 ;; Use the longest common substring of both prefixes,
279 (fill-common-string-prefix first-line-prefix
280 second-line-prefix
)))))
281 ;; If we get a fill prefix from a one-line paragraph,
282 ;; maybe change it to whitespace,
283 ;; and check that it isn't a paragraph starter.
284 (if first-line-prefix
286 ;; If first-line-prefix comes from the first line,
287 ;; see if it seems reasonable to use for all lines.
288 ;; If not, replace it with whitespace.
289 (if (or (and first-line-regexp
290 (string-match first-line-regexp
292 (and comment-start-skip
293 (string-match comment-start-skip
296 (make-string (string-width first-line-prefix
) ?\s
))))
297 ;; But either way, reject it if it indicates the start
298 ;; of a paragraph when text follows it.
299 (if (not (eq 0 (string-match paragraph-start
300 (concat result
"a"))))
303 (defun fill-single-word-nobreak-p ()
304 "Don't break a line after the first or before the last word of a sentence."
305 ;; Actually, allow breaking before the last word of a sentence, so long as
306 ;; it's not the last word of the paragraph.
307 (or (looking-at (concat "[ \t]*\\sw+" "\\(?:" (sentence-end) "\\)[ \t]*$"))
309 (skip-chars-backward " \t")
310 (and (/= (skip-syntax-backward "w") 0)
311 (/= (skip-chars-backward " \t") 0)
312 (/= (skip-chars-backward ".?!:") 0)
313 (looking-at (sentence-end))))))
315 (defun fill-french-nobreak-p ()
316 "Return nil if French style allows breaking the line at point.
317 This is used in `fill-nobreak-predicate' to prevent breaking lines just
318 after an opening paren or just before a closing paren or a punctuation
319 mark such as `?' or `:'. It is common in French writing to put a space
320 at such places, which would normally allow breaking the line at those
322 (or (looking-at "[ \t]*[])}»?!;:-]")
324 (skip-chars-backward " \t")
327 (or (looking-at "[([{«]")
328 ;; Don't cut right after a single-letter word.
329 (and (memq (preceding-char) '(?
\t ?\s
))
330 (eq (char-syntax (following-char)) ?w
)))))))
332 (defcustom fill-nobreak-predicate nil
333 "List of predicates for recognizing places not to break a line.
334 The predicates are called with no arguments, with point at the place to
335 be tested. If it returns t, fill commands do not break the line there."
338 :options
'(fill-french-nobreak-p fill-single-word-nobreak-p
))
340 (defcustom fill-nobreak-invisible nil
341 "Non-nil means that fill commands do not break lines in invisible text."
345 (defun fill-nobreak-p ()
346 "Return nil if breaking the line at point is allowed.
347 Can be customized with the variables `fill-nobreak-predicate'
348 and `fill-nobreak-invisible'."
350 (and fill-nobreak-invisible
(invisible-p (point)))
353 ;; Don't break after a period followed by just one space.
354 ;; Move back to the previous place to break.
355 ;; The reason is that if a period ends up at the end of a
356 ;; line, further fills will assume it ends a sentence.
357 ;; If we now know it does not end a sentence, avoid putting
358 ;; it at the end of the line.
359 (and sentence-end-double-space
361 (skip-chars-backward " ")
362 (and (eq (preceding-char) ?.
)
363 (looking-at " \\([^ ]\\|$\\)"))))
364 ;; Another approach to the same problem.
366 (skip-chars-backward " ")
367 (and (eq (preceding-char) ?.
)
368 (not (progn (forward-char -
1) (looking-at (sentence-end))))))
369 ;; Don't split a line if the rest would look like a new paragraph.
370 (unless use-hard-newlines
372 (skip-chars-forward " \t")
373 ;; If this break point is at the end of the line,
374 ;; which can occur for auto-fill, don't consider the newline
375 ;; which follows as a reason to return t.
377 (looking-at paragraph-start
))))
378 (run-hook-with-args-until-success 'fill-nobreak-predicate
)))))
380 (defvar fill-find-break-point-function-table
(make-char-table nil
)
381 "Char-table of special functions to find line breaking point.")
383 (defvar fill-nospace-between-words-table
(make-char-table nil
)
384 "Char-table of characters that don't use space between words.")
387 ;; Register `kinsoku' for scripts HAN, KANA, BOPOMOFO, and CJK-MISC.
388 ;; Also tell that they don't use space between words.
391 (when (memq val
'(han kana bopomofo cjk-misc
))
392 (set-char-table-range fill-find-break-point-function-table
394 (set-char-table-range fill-nospace-between-words-table
397 ;; Do the same thing also for full width characters and half
398 ;; width kana variants.
399 (set-char-table-range fill-find-break-point-function-table
400 '(#xFF01 .
#xFFE6
) 'kinsoku
)
401 (set-char-table-range fill-nospace-between-words-table
402 '(#xFF01 .
#xFFE6
) 'kinsoku
))
404 (defun fill-find-break-point (limit)
405 "Move point to a proper line breaking position of the current line.
406 Don't move back past the buffer position LIMIT.
408 This function is called when we are going to break the current line
409 after or before a non-ASCII character. If the charset of the
410 character has the property `fill-find-break-point-function', this
411 function calls the property value as a function with one arg LIMIT.
412 If the charset has no such property, do nothing."
414 (aref fill-find-break-point-function-table
(following-char))
415 (aref fill-find-break-point-function-table
(preceding-char)))))
416 (if (and func
(fboundp func
))
417 (funcall func limit
))))
419 (defun fill-delete-prefix (from to prefix
)
420 "Delete the fill prefix from every line except the first.
421 The first line may not even have a fill prefix.
422 Point is moved to just past the fill prefix on the first line."
423 (let ((fpre (if (and prefix
(not (string-match "\\`[ \t]*\\'" prefix
)))
425 (replace-regexp-in-string
427 (regexp-quote prefix
))
431 ;; Why signal an error here? The problem needs to be caught elsewhere.
432 ;; (if (>= (+ (current-left-margin) (length prefix))
433 ;; (current-fill-column))
434 ;; (error "fill-prefix too long for specified width"))
436 (while (< (point) to
)
437 (if (looking-at fpre
)
438 (delete-region (point) (match-end 0)))
441 (if (looking-at fpre
)
442 (goto-char (match-end 0)))
445 ;; The `fill-space' property carries the string with which a newline
446 ;; should be replaced when unbreaking a line (in fill-delete-newlines).
447 ;; It is added to newline characters by fill-newline when the default
448 ;; behavior of fill-delete-newlines is not what we want.
449 (add-to-list 'text-property-default-nonsticky
'(fill-space . t
))
451 (defun fill-delete-newlines (from to justify nosqueeze squeeze-after
)
453 ;; Make sure sentences ending at end of line get an extra space.
454 ;; loses on split abbrevs ("Mr.\nSmith")
455 (let ((eol-double-space-re
457 ((not colon-double-space
) (concat (sentence-end) "$"))
458 ;; Try to add the : inside the `sentence-end' regexp.
459 ((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" (sentence-end))
460 (concat (replace-match ".:" nil nil
(sentence-end) 1) "$"))
461 ;; Can't find the right spot to insert the colon.
462 (t "[.?!:][])}\"']*$")))
463 (sentence-end-without-space-list
464 (string-to-list sentence-end-without-space
)))
465 (while (re-search-forward eol-double-space-re to t
)
466 (or (>= (point) to
) (memq (char-before) '(?
\t ?\s
))
467 (memq (char-after (match-beginning 0))
468 sentence-end-without-space-list
)
469 (insert-and-inherit ?\s
))))
472 (if enable-multibyte-characters
473 ;; Delete unnecessary newlines surrounded by words. The
474 ;; character category `|' means that we can break a line at the
475 ;; character. And, char-table
476 ;; `fill-nospace-between-words-table' tells how to concatenate
477 ;; words. If a character has non-nil value in the table, never
478 ;; put spaces between words, thus delete a newline between them.
479 ;; Otherwise, delete a newline only when a character preceding a
480 ;; newline has non-nil value in that table.
481 (while (search-forward "\n" to t
)
482 (if (get-text-property (match-beginning 0) 'fill-space
)
483 (replace-match (get-text-property (match-beginning 0) 'fill-space
))
484 (let ((prev (char-before (match-beginning 0)))
485 (next (following-char)))
486 (if (and (or (aref (char-category-set next
) ?|
)
487 (aref (char-category-set prev
) ?|
))
488 (or (aref fill-nospace-between-words-table next
)
489 (aref fill-nospace-between-words-table prev
)))
490 (delete-char -
1))))))
493 (skip-chars-forward " \t")
494 ;; Then change all newlines to spaces.
495 (subst-char-in-region from to ?
\n ?\s
)
496 (if (and nosqueeze
(not (eq justify
'full
)))
498 (canonically-space-region (or squeeze-after
(point)) to
)
499 ;; Remove trailing whitespace.
500 ;; Maybe canonically-space-region should do that.
501 (goto-char to
) (delete-char (- (skip-chars-backward " \t"))))
504 (defun fill-move-to-break-point (linebeg)
505 "Move to the position where the line should be broken.
506 The break position will be always after LINEBEG and generally before point."
507 ;; If the fill column is before linebeg, move to linebeg.
508 (if (> linebeg
(point)) (goto-char linebeg
))
509 ;; Move back to the point where we can break the line
510 ;; at. We break the line between word or after/before
511 ;; the character which has character category `|'. We
512 ;; search space, \c| followed by a character, or \c|
513 ;; following a character. If not found, place
514 ;; the point at linebeg.
516 (when (re-search-backward "[ \t]\\|\\c|.\\|.\\c|" linebeg
0)
517 ;; In case of space, we place the point at next to
518 ;; the point where the break occurs actually,
519 ;; because we don't want to change the following
520 ;; logic of original Emacs. In case of \c|, the
521 ;; point is at the place where the break occurs.
523 (when (fill-nobreak-p) (skip-chars-backward " \t" linebeg
))))
525 ;; Move back over the single space between the words.
526 (skip-chars-backward " \t")
528 ;; If the left margin and fill prefix by themselves
529 ;; pass the fill-column. or if they are zero
530 ;; but we have no room for even one word,
531 ;; keep at least one word or a character which has
532 ;; category `|' anyway.
533 (if (>= linebeg
(point))
534 ;; Ok, skip at least one word or one \c| character.
535 ;; Meanwhile, don't stop at a period followed by one space.
536 (let ((to (line-end-position))
539 (while (and (< (point) to
) (or first
(fill-nobreak-p)))
540 ;; Find a breakable point while ignoring the
542 (skip-chars-forward " \t")
543 (if (looking-at "\\c|")
545 (let ((pos (save-excursion
546 (skip-chars-forward "^ \n\t")
548 (if (re-search-forward "\\c|" pos t
)
553 (if enable-multibyte-characters
554 ;; If we are going to break the line after or
555 ;; before a non-ascii character, we may have to
556 ;; run a special function for the charset of the
557 ;; character to find the correct break point.
558 (if (not (and (eq (charset-after (1- (point))) 'ascii
)
559 (eq (charset-after (point)) 'ascii
)))
560 ;; Make sure we take SOMETHING after the fill prefix if any.
561 (fill-find-break-point linebeg
)))))
563 ;; Like text-properties-at but don't include `composition' property.
564 (defun fill-text-properties-at (pos)
565 (let ((l (text-properties-at pos
))
568 (unless (eq (car l
) 'composition
)
570 (cons (car l
) (cons (cadr l
) prop-list
))))
574 (defun fill-newline ()
575 ;; Replace whitespace here with one newline, then
576 ;; indent to left margin.
577 (skip-chars-backward " \t")
579 ;; Give newline the properties of the space(s) it replaces
580 (set-text-properties (1- (point)) (point)
581 (fill-text-properties-at (point)))
582 (and (looking-at "\\( [ \t]*\\)\\(\\c|\\)?")
583 (or (aref (char-category-set (or (char-before (1- (point))) ?
\000)) ?|
)
585 ;; When refilling later on, this newline would normally not be replaced
586 ;; by a space, so we need to mark it specially to re-install the space
588 (put-text-property (1- (point)) (point) 'fill-space
(match-string 1)))
589 ;; If we don't want breaks in invisible text, don't insert
590 ;; an invisible newline.
591 (if fill-nobreak-invisible
592 (remove-text-properties (1- (point)) (point)
595 (not fill-indent-according-to-mode
))
596 (fill-indent-to-left-margin)
597 (indent-according-to-mode))
598 ;; Insert the fill prefix after indentation.
599 (and fill-prefix
(not (equal fill-prefix
""))
600 ;; Markers that were after the whitespace are now at point: insert
601 ;; before them so they don't get stuck before the prefix.
602 (insert-before-markers-and-inherit fill-prefix
)))
604 (defun fill-indent-to-left-margin ()
605 "Indent current line to the column given by `current-left-margin'."
607 (indent-line-to (current-left-margin))
608 (put-text-property beg
(point) 'face
'default
)))
610 (defun fill-region-as-paragraph (from to
&optional justify
611 nosqueeze squeeze-after
)
612 "Fill the region as one paragraph.
613 It removes any paragraph breaks in the region and extra newlines at the end,
614 indents and fills lines between the margins given by the
615 `current-left-margin' and `current-fill-column' functions.
616 \(In most cases, the variable `fill-column' controls the width.)
617 It leaves point at the beginning of the line following the paragraph.
619 Normally performs justification according to the `current-justification'
620 function, but with a prefix arg, does full justification instead.
622 From a program, optional third arg JUSTIFY can specify any type of
623 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
624 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
625 means don't canonicalize spaces before that position.
627 Return the `fill-prefix' used for filling.
629 If `sentence-end-double-space' is non-nil, then period followed by one
630 space does not end a sentence, so don't break a line there."
632 (barf-if-buffer-read-only)
633 (list (region-beginning) (region-end)
634 (if current-prefix-arg
'full
))))
635 (unless (memq justify
'(t nil none full center left right
))
636 (setq justify
'full
))
638 ;; Make sure "to" is the endpoint.
639 (goto-char (min from to
))
640 (setq to
(max from to
))
641 ;; Ignore blank lines at beginning of region.
642 (skip-chars-forward " \t\n")
644 (let ((from-plus-indent (point))
648 ;; We used to round up to whole line, but that prevents us from
649 ;; correctly handling filling of mixed code-and-comment where we do want
650 ;; to fill the comment but not the code. So only use (point) if it's
651 ;; further than `from', which means that `from' is followed by some
652 ;; number of empty lines.
653 (setq from
(max (point) from
))
655 ;; Delete all but one soft newline at end of region.
656 ;; And leave TO before that one.
658 (while (and (> (point) from
) (eq ?
\n (char-after (1- (point)))))
660 (not (and use-hard-newlines
661 (get-text-property (1- (point)) 'hard
))))
665 (setq to
(copy-marker (point) t
))
666 ;; ;; If there was no newline, and there is text in the paragraph, then
667 ;; ;; create a newline.
668 ;; (if (and (not oneleft) (> to from-plus-indent))
670 (goto-char from-plus-indent
))
672 (if (not (> to
(point)))
673 nil
;; There is no paragraph, only whitespace: exit now.
675 (or justify
(setq justify
(current-justification)))
677 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
678 (let ((fill-prefix fill-prefix
))
679 ;; Figure out how this paragraph is indented, if desired.
680 (when (and adaptive-fill-mode
681 (or (null fill-prefix
) (string= fill-prefix
"")))
682 (setq fill-prefix
(fill-context-prefix from to
))
683 ;; Ignore a white-space only fill-prefix
684 ;; if we indent-according-to-mode.
685 (when (and fill-prefix fill-indent-according-to-mode
686 (string-match "\\`[ \t]*\\'" fill-prefix
))
687 (setq fill-prefix nil
)))
692 (if (not justify
) ; filling disabled: just check indentation
695 (while (< (point) to
)
696 (if (and (not (eolp))
697 (< (current-indentation) (current-left-margin)))
698 (fill-indent-to-left-margin))
701 (if use-hard-newlines
702 (remove-list-of-text-properties from to
'(hard)))
703 ;; Make sure first line is indented (at least) to left margin...
704 (if (or (memq justify
'(right center
))
705 (< (current-indentation) (current-left-margin)))
706 (fill-indent-to-left-margin))
707 ;; Delete the fill-prefix from every line.
708 (fill-delete-prefix from to fill-prefix
)
711 ;; FROM, and point, are now before the text to fill,
712 ;; but after any fill prefix on the first line.
714 (fill-delete-newlines from to justify nosqueeze squeeze-after
)
716 ;; This is the actual filling loop.
719 (while (< (point) to
)
720 (setq linebeg
(point))
721 (move-to-column (current-fill-column))
722 (if (when (< (point) to
)
723 ;; Find the position where we'll break the line.
724 (forward-char 1) ;Use an immediately following space, if any.
725 (fill-move-to-break-point linebeg
)
726 ;; Check again to see if we got to the end of
728 (skip-chars-forward " \t")
730 ;; Found a place to cut.
734 ;; Justify the line just ended, if desired.
737 (justify-current-line justify nil t
))))
740 ;; Justify this last line, if desired.
741 (if justify
(justify-current-line justify t t
))))))
742 ;; Leave point after final newline.
744 (unless (eobp) (forward-char 1))
745 ;; Return the fill-prefix we used
748 (defsubst skip-line-prefix
(prefix)
749 "If point is inside the string PREFIX at the beginning of line, move past it."
751 (< (- (point) (line-beginning-position)) (length prefix
))
754 (looking-at (regexp-quote prefix
))))
755 (goto-char (match-end 0))))
757 (defun fill-minibuffer-function (arg)
758 "Fill a paragraph in the minibuffer, ignoring the prompt."
760 (narrow-to-region (minibuffer-prompt-end) (point-max))
761 (fill-paragraph arg
)))
763 (defvar fill-forward-paragraph-function
'forward-paragraph
764 "Function to move over paragraphs used by the filling code.
765 It is called with a single argument specifying the number of paragraphs to move.
766 Just like `forward-paragraph', it should return the number of paragraphs
769 (defun fill-forward-paragraph (arg)
770 (funcall fill-forward-paragraph-function arg
))
772 (defun fill-paragraph (&optional justify region
)
773 "Fill paragraph at or after point.
775 If JUSTIFY is non-nil (interactively, with prefix argument), justify as well.
776 If `sentence-end-double-space' is non-nil, then period followed by one
777 space does not end a sentence, so don't break a line there.
778 The variable `fill-column' controls the width for filling.
780 If `fill-paragraph-function' is non-nil, we call it (passing our
781 argument to it), and if it returns non-nil, we simply return its value.
783 If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling.
785 The REGION argument is non-nil if called interactively; in that
786 case, if Transient Mark mode is enabled and the mark is active,
787 call `fill-region' to fill each of the paragraphs in the active
788 region, instead of just filling the current paragraph."
790 (barf-if-buffer-read-only)
791 (list (if current-prefix-arg
'full
) t
)))
793 ;; 1. Fill the region if it is active when called interactively.
794 (and region transient-mark-mode mark-active
795 (not (eq (region-beginning) (region-end)))
796 (or (fill-region (region-beginning) (region-end) justify
) t
))
797 ;; 2. Try fill-paragraph-function.
798 (and (not (eq fill-paragraph-function t
))
799 (or fill-paragraph-function
800 (and (minibufferp (current-buffer))
802 (let ((function (or fill-paragraph-function
803 ;; In the minibuffer, don't count the width
805 'fill-minibuffer-function
))
806 ;; If fill-paragraph-function is set, it probably takes care
807 ;; of comments and stuff. If not, it will have to set
808 ;; fill-paragraph-handle-comment back to t explicitly or
810 (fill-paragraph-handle-comment nil
)
811 (fill-paragraph-function t
))
812 (funcall function justify
)))
813 ;; 3. Try our syntax-aware filling code.
814 (and fill-paragraph-handle-comment
815 ;; Our code only handles \n-terminated comments right now.
816 comment-start
(equal comment-end
"")
817 (let ((fill-paragraph-handle-comment nil
))
818 (fill-comment-paragraph justify
)))
819 ;; 4. If it all fails, default to the good ol' text paragraph filling.
820 (let ((before (point))
821 (paragraph-start paragraph-start
)
822 ;; Fill prefix used for filling the paragraph.
824 ;; Try to prevent code sections and comment sections from being
826 (when (and fill-paragraph-handle-comment comment-start-skip
)
827 (setq paragraph-start
828 (concat paragraph-start
"\\|[ \t]*\\(?:"
829 comment-start-skip
"\\)")))
831 ;; To make sure the return value of forward-paragraph is meaningful,
832 ;; we have to start from the beginning of line, otherwise skipping
833 ;; past the last few chars of a paragraph-separator would count as
834 ;; a paragraph (and not skipping any chars at EOB would not count
835 ;; as a paragraph even if it is).
836 (move-to-left-margin)
837 (if (not (zerop (fill-forward-paragraph 1)))
838 ;; There's no paragraph at or after point: give up.
841 (beg (progn (fill-forward-paragraph -
1) (point))))
844 (if use-hard-newlines
845 ;; Can't use fill-region-as-paragraph, since this
846 ;; paragraph may still contain hard newlines. See
848 (fill-region beg end justify
)
849 (fill-region-as-paragraph beg end justify
))))))
852 (declare-function comment-search-forward
"newcomment" (limit &optional noerror
))
853 (declare-function comment-string-strip
"newcomment" (str beforep afterp
))
856 (defun fill-comment-paragraph (&optional justify
)
857 "Fill current comment.
858 If we're not in a comment, just return nil so that the caller
859 can take care of filling. JUSTIFY is used as in `fill-paragraph'."
860 (comment-normalize-vars)
861 (let (has-code-and-comment ; Non-nil if it contains code and a comment.
863 ;; Figure out what kind of comment we are looking at.
866 (when (setq comstart
(comment-search-forward (line-end-position) t
))
868 (goto-char comstart
) (skip-chars-backward " \t")
869 (setq has-code-and-comment
(not (bolp)))))
871 (if (not (and comstart
872 ;; Make sure the comment-start mark we found is accepted by
873 ;; comment-start-skip. If not, all bets are off, and
874 ;; we'd better not mess with it.
875 (string-match comment-start-skip
876 (buffer-substring comstart comin
))))
878 ;; Return nil, so the normal filling will take place.
881 ;; Narrow to include only the comment, and then fill the region.
882 (let* ((fill-prefix fill-prefix
)
884 (comment-string-strip (buffer-substring comstart comin
) nil t
))
886 ;; A regexp more specialized than comment-start-skip, that only
887 ;; matches the current commark rather than any valid commark.
889 ;; The specialized regexp only works for "normal" comment
890 ;; syntax, not for Texinfo's "@c" (which can't be immediately
891 ;; followed by word-chars) or Fortran's "C" (which needs to be
892 ;; at bol), so check that comment-start-skip indeed allows the
893 ;; commark to appear in the middle of the line and followed by
894 ;; word chars. The choice of "\0" and "a" is mostly arbitrary.
895 (if (string-match comment-start-skip
(concat "\0" commark
"a"))
896 (concat "[ \t]*" (regexp-quote commark
)
897 ;; Make sure we only match comments that
898 ;; use the exact same comment marker.
899 "[^" (substring commark -
1) "]")
900 (concat "[ \t]*\\(?:" comment-start-skip
"\\)")))
901 (comment-fill-prefix ; Compute a fill prefix.
904 (if has-code-and-comment
906 (if (not indent-tabs-mode
)
907 (make-string (current-column) ?\s
)
909 (make-string (/ (current-column) tab-width
) ?
\t)
910 (make-string (%
(current-column) tab-width
) ?\s
)))
911 (buffer-substring (point) comin
))
912 (buffer-substring (line-beginning-position) comin
))))
918 ;; Find the first line we should include in the region to fill.
919 (if has-code-and-comment
920 (line-beginning-position)
922 (while (and (zerop (forward-line -
1))
923 (looking-at comment-re
)))
924 ;; We may have gone too far. Go forward again.
925 (line-beginning-position
928 (or (comment-search-forward (line-end-position) t
)
930 (looking-at comment-re
))
931 (progn (setq comstart
(point)) 1)
932 (progn (setq comstart
(point)) 2)))))
933 ;; Find the beginning of the first line past the region to fill.
935 (while (progn (forward-line 1)
936 (looking-at comment-re
)))
938 ;; Obey paragraph starters and boundaries within comments.
939 (let* ((paragraph-separate
940 ;; Use the default values since they correspond to
941 ;; the values to use for plain text.
942 (concat paragraph-separate
"\\|[ \t]*\\(?:"
943 comment-start-skip
"\\)\\(?:"
944 (default-value 'paragraph-separate
) "\\)"))
946 (concat paragraph-start
"\\|[ \t]*\\(?:"
947 comment-start-skip
"\\)\\(?:"
948 (default-value 'paragraph-start
) "\\)"))
949 ;; We used to rely on fill-prefix to break paragraph at
950 ;; comment-starter changes, but it did not work for the
951 ;; first line (mixed comment&code).
952 ;; We now use comment-re instead to "manually" make sure
953 ;; we treat comment-marker changes as paragraph boundaries.
954 ;; (paragraph-ignore-fill-prefix nil)
955 ;; (fill-prefix comment-fill-prefix)
956 (after-line (if has-code-and-comment
957 (line-beginning-position 2))))
958 (setq end
(progn (forward-paragraph) (point)))
959 ;; If this comment starts on a line with code,
960 ;; include that line in the filling.
961 (setq beg
(progn (backward-paragraph)
962 (if (eq (point) after-line
)
966 ;; Find the fill-prefix to use.
968 (fill-prefix) ; Use the user-provided fill prefix.
969 ((and adaptive-fill-mode
; Try adaptive fill mode.
970 (setq fill-prefix
(fill-context-prefix beg end
))
971 (string-match comment-start-skip fill-prefix
)))
973 (setq fill-prefix comment-fill-prefix
)))
975 ;; Don't fill with narrowing.
977 (fill-region-as-paragraph
978 (max comstart beg
) end justify nil
979 ;; Don't canonicalize spaces within the code just before
983 (if (looking-at fill-prefix
)
985 (re-search-forward comment-start-skip
))))
986 ;; Make sure we don't return nil.
989 (defun fill-region (from to
&optional justify nosqueeze to-eop
)
990 "Fill each of the paragraphs in the region.
991 A prefix arg means justify as well.
992 The `fill-column' variable controls the width.
994 Noninteractively, the third argument JUSTIFY specifies which
995 kind of justification to do: `full', `left', `right', `center',
996 or `none' (equivalent to nil). A value of t means handle each
997 paragraph as specified by its text properties.
999 The fourth arg NOSQUEEZE non-nil means to leave whitespace other
1000 than line breaks untouched, and fifth arg TO-EOP non-nil means
1001 to keep filling to the end of the paragraph (or next hard newline,
1002 if variable `use-hard-newlines' is on).
1004 Return the fill-prefix used for filling the last paragraph.
1006 If `sentence-end-double-space' is non-nil, then period followed by one
1007 space does not end a sentence, so don't break a line there."
1009 (barf-if-buffer-read-only)
1010 (list (region-beginning) (region-end)
1011 (if current-prefix-arg
'full
))))
1012 (unless (memq justify
'(t nil none full center left right
))
1013 (setq justify
'full
))
1014 (let (max beg fill-pfx
)
1015 (goto-char (max from to
))
1017 (skip-chars-backward "\n")
1018 (fill-forward-paragraph 1))
1019 (setq max
(copy-marker (point) t
))
1020 (goto-char (setq beg
(min from to
)))
1022 (while (< (point) max
)
1023 (let ((initial (point))
1025 ;; If using hard newlines, break at every one for filling
1026 ;; purposes rather than using paragraph breaks.
1027 (if use-hard-newlines
1029 (while (and (setq end
(text-property-any (point) max
1031 (not (= ?
\n (char-after end
)))
1033 (goto-char (1+ end
)))
1034 (setq end
(if end
(min max
(1+ end
)) max
))
1035 (goto-char initial
))
1036 (fill-forward-paragraph 1)
1037 (setq end
(min max
(point)))
1038 (fill-forward-paragraph -
1))
1041 (if (and (>= (point) initial
) (< (point) end
))
1043 (fill-region-as-paragraph (point) end justify nosqueeze
))
1048 (defcustom default-justification
'left
1049 "Method of justifying text not otherwise specified.
1050 Possible values are `left', `right', `full', `center', or `none'.
1051 The requested kind of justification is done whenever lines are filled.
1052 The `justification' text-property can locally override this variable."
1053 :type
'(choice (const left
)
1060 (make-variable-buffer-local 'default-justification
)
1062 (defun current-justification ()
1063 "How should we justify this line?
1064 This returns the value of the text-property `justification',
1065 or the variable `default-justification' if there is no text-property.
1066 However, it returns nil rather than `none' to mean \"don't justify\"."
1067 (let ((j (or (get-text-property
1068 ;; Make sure we're looking at paragraph body.
1069 (save-excursion (skip-chars-forward " \t")
1070 (if (and (eobp) (not (bobp)))
1071 (1- (point)) (point)))
1073 default-justification
)))
1078 (defun set-justification (begin end style
&optional whole-par
)
1079 "Set the region's justification style to STYLE.
1080 This commands prompts for the kind of justification to use.
1081 If the mark is not active, this command operates on the current paragraph.
1082 If the mark is active, it operates on the region. However, if the
1083 beginning and end of the region are not at paragraph breaks, they are
1084 moved to the beginning and end \(respectively) of the paragraphs they
1087 If variable `use-hard-newlines' is true, all hard newlines are
1088 taken to be paragraph breaks.
1090 When calling from a program, operates just on region between BEGIN and END,
1091 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
1092 extended to include entire paragraphs as in the interactive command."
1093 (interactive (list (if mark-active
(region-beginning) (point))
1094 (if mark-active
(region-end) (point))
1095 (let ((s (completing-read
1096 "Set justification to: "
1097 '(("left") ("right") ("full")
1098 ("center") ("none"))
1100 (if (equal s
"") (error ""))
1106 (let ((paragraph-start (if use-hard-newlines
"." paragraph-start
))
1107 (paragraph-ignore-fill-prefix (if use-hard-newlines t
1108 paragraph-ignore-fill-prefix
)))
1110 (while (and (bolp) (not (eobp))) (forward-char 1))
1111 (backward-paragraph)
1112 (setq begin
(point))
1114 (skip-chars-backward " \t\n" begin
)
1116 (setq end
(point))))
1118 (narrow-to-region (point-min) end
)
1119 (unjustify-region begin
(point-max))
1120 (put-text-property begin
(point-max) 'justification style
)
1121 (fill-region begin
(point-max) nil t
))))
1123 (defun set-justification-none (b e
)
1124 "Disable automatic filling for paragraphs in the region.
1125 If the mark is not active, this applies to the current paragraph."
1126 (interactive (list (if mark-active
(region-beginning) (point))
1127 (if mark-active
(region-end) (point))))
1128 (set-justification b e
'none t
))
1130 (defun set-justification-left (b e
)
1131 "Make paragraphs in the region left-justified.
1132 This means they are flush at the left margin and ragged on the right.
1133 This is usually the default, but see the variable `default-justification'.
1134 If the mark is not active, this applies to the current paragraph."
1135 (interactive (list (if mark-active
(region-beginning) (point))
1136 (if mark-active
(region-end) (point))))
1137 (set-justification b e
'left t
))
1139 (defun set-justification-right (b e
)
1140 "Make paragraphs in the region right-justified.
1141 This means they are flush at the right margin and ragged on the left.
1142 If the mark is not active, this applies to the current paragraph."
1143 (interactive (list (if mark-active
(region-beginning) (point))
1144 (if mark-active
(region-end) (point))))
1145 (set-justification b e
'right t
))
1147 (defun set-justification-full (b e
)
1148 "Make paragraphs in the region fully justified.
1149 This makes lines flush on both margins by inserting spaces between words.
1150 If the mark is not active, this applies to the current paragraph."
1151 (interactive (list (if mark-active
(region-beginning) (point))
1152 (if mark-active
(region-end) (point))))
1153 (set-justification b e
'full t
))
1155 (defun set-justification-center (b e
)
1156 "Make paragraphs in the region centered.
1157 If the mark is not active, this applies to the current paragraph."
1158 (interactive (list (if mark-active
(region-beginning) (point))
1159 (if mark-active
(region-end) (point))))
1160 (set-justification b e
'center t
))
1162 ;; A line has up to six parts:
1165 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
1167 ;; "Indent-1" is the left-margin indentation; normally it ends at column
1168 ;; given by the `current-left-margin' function.
1169 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
1170 ;; "Indent-2" is added to justify a line if the `current-justification' is
1171 ;; `center' or `right'. In `left' and `full' justification regions, any
1172 ;; whitespace there is part of the line's text, and should not be changed.
1173 ;; Trailing whitespace is not counted as part of the line length when
1174 ;; center- or right-justifying.
1176 ;; All parts of the line are optional, although the final newline can
1177 ;; only be missing on the last line of the buffer.
1179 (defun justify-current-line (&optional how eop nosqueeze
)
1180 "Do some kind of justification on this line.
1181 Normally does full justification: adds spaces to the line to make it end at
1182 the column given by `current-fill-column'.
1183 Optional first argument HOW specifies alternate type of justification:
1184 it can be `left', `right', `full', `center', or `none'.
1185 If HOW is t, will justify however the `current-justification' function says to.
1186 If HOW is nil or missing, full justification is done by default.
1187 Second arg EOP non-nil means that this is the last line of the paragraph, so
1188 it will not be stretched by full justification.
1189 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
1190 otherwise it is made canonical."
1192 (if (eq t how
) (setq how
(or (current-justification) 'none
))
1193 (if (null how
) (setq how
'full
)
1194 (or (memq how
'(none left right center
))
1196 (or (memq how
'(none left
)) ; No action required for these.
1197 (let ((fc (current-fill-column))
1198 (pos (point-marker))
1199 fp-end
; point at end of fill prefix
1200 beg
; point at beginning of line's text
1201 end
; point at end of line's text
1202 indent
; column of `beg'
1203 endcol
; column of `end'
1204 ncols
; new indent point or offset
1205 (nspaces 0) ; number of spaces between words
1206 ; in line (not space characters)
1207 (curr-fracspace 0) ; current fractional space amount
1210 ;; Check if this is the last line of the paragraph.
1211 (if (and use-hard-newlines
(null eop
)
1212 (get-text-property (point) 'hard
))
1214 (skip-chars-backward " \t")
1215 ;; Quick exit if it appears to be properly justified already
1216 ;; or there is no text.
1218 (and (memq how
'(full right
))
1219 (= (current-column) fc
)))
1223 (skip-chars-forward " \t")
1224 ;; Skip over fill-prefix.
1225 (if (and fill-prefix
1226 (not (string-equal fill-prefix
""))
1229 (point) (min (point-max) (+ (length fill-prefix
)
1231 (forward-char (length fill-prefix
))
1232 (if (and adaptive-fill-mode
1233 (looking-at adaptive-fill-regexp
))
1234 (goto-char (match-end 0))))
1235 (setq fp-end
(point))
1236 (skip-chars-forward " \t")
1237 ;; This is beginning of the line's text.
1238 (setq indent
(current-column))
1241 (setq endcol
(current-column))
1243 ;; HOW can't be null or left--we would have exited already
1244 (cond ((eq 'right how
)
1245 (setq ncols
(- fc endcol
))
1247 ;; Need to remove some indentation
1249 (progn (goto-char fp-end
)
1250 (if (< (current-column) (+ indent ncols
))
1251 (move-to-column (+ indent ncols
) t
))
1253 (progn (move-to-column indent
) (point)))
1256 (indent-to (+ indent ncols
))
1257 ;; If point was at beginning of text, keep it there.
1259 (move-marker pos
(point)))))
1262 ;; Figure out how much indentation is needed
1263 (setq ncols
(+ (current-left-margin)
1264 (/ (- fc
(current-left-margin) ;avail. space
1265 (- endcol indent
)) ;text width
1267 (if (< ncols indent
)
1268 ;; Have too much indentation - remove some
1270 (progn (goto-char fp-end
)
1271 (if (< (current-column) ncols
)
1272 (move-to-column ncols t
))
1274 (progn (move-to-column indent
) (point)))
1275 ;; Have too little - add some
1278 ;; If point was at beginning of text, keep it there.
1280 (move-marker pos
(point)))))
1283 ;; Insert extra spaces between words to justify line
1285 (narrow-to-region beg end
)
1287 (canonically-space-region beg end
))
1288 (goto-char (point-max))
1289 ;; count word spaces in line
1290 (while (search-backward " " nil t
)
1291 (setq nspaces
(1+ nspaces
))
1292 (skip-chars-backward " "))
1293 (setq ncols
(- fc endcol
))
1294 ;; Ncols is number of additional space chars needed
1295 (when (and (> ncols
0) (> nspaces
0) (not eop
))
1296 (setq curr-fracspace
(+ ncols
(/ nspaces
2))
1299 (skip-chars-forward " ")
1300 (insert-char ?\s
(/ curr-fracspace nspaces
) t
)
1301 (search-forward " " nil t
)
1302 (setq count
(1- count
)
1304 (+ (% curr-fracspace nspaces
) ncols
))))))
1305 (t (error "Unknown justification value"))))
1307 (move-marker pos nil
)))
1310 (defun unjustify-current-line ()
1311 "Remove justification whitespace from current line.
1312 If the line is centered or right-justified, this function removes any
1313 indentation past the left margin. If the line is full-justified, it removes
1314 extra spaces between words. It does nothing in other justification modes."
1315 (let ((justify (current-justification)))
1316 (cond ((eq 'left justify
) nil
)
1317 ((eq nil justify
) nil
)
1318 ((eq 'full justify
) ; full justify: remove extra spaces
1319 (beginning-of-line-text)
1320 (canonically-space-region (point) (line-end-position)))
1321 ((memq justify
'(center right
))
1323 (move-to-left-margin nil t
)
1324 ;; Position ourselves after any fill-prefix.
1325 (if (and fill-prefix
1326 (not (string-equal fill-prefix
""))
1329 (point) (min (point-max) (+ (length fill-prefix
)
1331 (forward-char (length fill-prefix
)))
1332 (delete-region (point) (progn (skip-chars-forward " \t")
1335 (defun unjustify-region (&optional begin end
)
1336 "Remove justification whitespace from region.
1337 For centered or right-justified regions, this function removes any indentation
1338 past the left margin from each line. For full-justified lines, it removes
1339 extra spaces between words. It does nothing in other justification modes.
1340 Arguments BEGIN and END are optional; default is the whole buffer."
1343 (if end
(narrow-to-region (point-min) end
))
1344 (goto-char (or begin
(point-min)))
1346 (unjustify-current-line)
1347 (forward-line 1)))))
1350 (defun fill-nonuniform-paragraphs (min max
&optional justifyp citation-regexp
)
1351 "Fill paragraphs within the region, allowing varying indentation within each.
1352 This command divides the region into \"paragraphs\",
1353 only at paragraph-separator lines, then fills each paragraph
1354 using as the fill prefix the smallest indentation of any line
1357 When calling from a program, pass range to fill as first two arguments.
1359 Optional third and fourth arguments JUSTIFYP and CITATION-REGEXP:
1360 JUSTIFYP to justify paragraphs (prefix arg).
1361 When filling a mail message, pass a regexp for CITATION-REGEXP
1362 which will match the prefix of a line which is a citation marker
1363 plus whitespace, but no other kind of prefix.
1364 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1366 (barf-if-buffer-read-only)
1367 (list (region-beginning) (region-end)
1368 (if current-prefix-arg
'full
))))
1369 (let ((fill-individual-varying-indent t
))
1370 (fill-individual-paragraphs min max justifyp citation-regexp
)))
1372 (defun fill-individual-paragraphs (min max
&optional justify citation-regexp
)
1373 "Fill paragraphs of uniform indentation within the region.
1374 This command divides the region into \"paragraphs\",
1375 treating every change in indentation level or prefix as a paragraph boundary,
1376 then fills each paragraph using its indentation level as the fill prefix.
1378 There is one special case where a change in indentation does not start
1379 a new paragraph. This is for text of this form:
1381 foo> This line with extra indentation starts
1382 foo> a paragraph that continues on more lines.
1384 These lines are filled together.
1386 When calling from a program, pass the range to fill
1387 as the first two arguments.
1389 Optional third and fourth arguments JUSTIFY and CITATION-REGEXP:
1390 JUSTIFY to justify paragraphs (prefix arg).
1391 When filling a mail message, pass a regexp for CITATION-REGEXP
1392 which will match the prefix of a line which is a citation marker
1393 plus whitespace, but no other kind of prefix.
1394 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1396 (barf-if-buffer-read-only)
1397 (list (region-beginning) (region-end)
1398 (if current-prefix-arg
'full
))))
1403 (narrow-to-region (point) max
)
1405 (while (and (not (eobp))
1406 (or (looking-at "[ \t]*[^ \t\n]+:")
1407 (looking-at "[ \t]*$")))
1408 (if (looking-at "[ \t]*[^ \t\n]+:")
1409 (search-forward "\n\n" nil
'move
)
1411 (narrow-to-region (point) max
)
1412 ;; Loop over paragraphs.
1414 ;; Skip over all paragraph-separating lines
1415 ;; so as to not include them in any paragraph.
1416 (while (and (not (eobp))
1417 (progn (move-to-left-margin)
1419 (looking-at paragraph-separate
))))
1421 (skip-chars-forward " \t\n") (not (eobp)))
1422 (move-to-left-margin)
1423 (let ((start (point))
1424 fill-prefix fill-prefix-regexp
)
1425 ;; Find end of paragraph, and compute the smallest fill-prefix
1426 ;; that fits all the lines in this paragraph.
1428 ;; Update the fill-prefix on the first line
1429 ;; and whenever the prefix good so far is too long.
1430 (if (not (and fill-prefix
1431 (looking-at fill-prefix-regexp
)))
1433 (fill-individual-paragraphs-prefix
1435 fill-prefix-regexp
(regexp-quote fill-prefix
)))
1438 ;; If forward-line went past a newline,
1439 ;; move further to the left margin.
1440 (move-to-left-margin))
1441 ;; Now stop the loop if end of paragraph.
1443 (if fill-individual-varying-indent
1444 ;; If this line is a separator line, with or
1445 ;; without prefix, end the paragraph.
1447 (not (looking-at paragraph-separate
))
1449 (not (and (looking-at fill-prefix-regexp
)
1450 (progn (forward-char
1451 (length fill-prefix
))
1453 paragraph-separate
))))))
1454 ;; If this line has more or less indent
1455 ;; than the fill prefix wants, end the paragraph.
1456 (and (looking-at fill-prefix-regexp
)
1457 ;; If fill prefix is shorter than a new
1458 ;; fill prefix computed here, end paragraph.
1459 (let ((this-line-fill-prefix
1460 (fill-individual-paragraphs-prefix
1462 (>= (length fill-prefix
)
1463 (length this-line-fill-prefix
)))
1465 (not (progn (forward-char
1466 (length fill-prefix
))
1467 (or (looking-at "[ \t]")
1468 (looking-at paragraph-separate
)
1469 (looking-at paragraph-start
)))))
1470 (not (and (equal fill-prefix
"")
1472 (looking-at citation-regexp
))))))))
1473 ;; Fill this paragraph, but don't add a newline at the end.
1474 (let ((had-newline (bolp)))
1475 (fill-region-as-paragraph start
(point) justify
)
1476 (if (and (bolp) (not had-newline
))
1477 (delete-char -
1))))))))
1479 (defun fill-individual-paragraphs-prefix (citation-regexp)
1480 (let* ((adaptive-fill-first-line-regexp ".*")
1481 (just-one-line-prefix
1482 ;; Accept any prefix rather than just the ones matched by
1483 ;; adaptive-fill-first-line-regexp.
1484 (fill-context-prefix (point) (line-beginning-position 2)))
1486 (fill-context-prefix (point) (line-beginning-position 3))))
1487 (if (not just-one-line-prefix
)
1489 (point) (save-excursion (skip-chars-forward " \t") (point)))
1490 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1491 ;; is the same as that of TWO-LINES-PREFIX,
1492 ;; except perhaps with longer whitespace.
1493 (if (and just-one-line-prefix two-lines-prefix
1494 (let* ((one-line-citation-part
1495 (fill-individual-paragraphs-citation
1496 just-one-line-prefix citation-regexp
))
1497 (two-lines-citation-part
1498 (fill-individual-paragraphs-citation
1499 two-lines-prefix citation-regexp
))
1500 (adjusted-two-lines-citation-part
1501 (substring two-lines-citation-part
0
1502 (string-match "[ \t]*\\'"
1503 two-lines-citation-part
))))
1505 (string-match (concat "\\`"
1507 adjusted-two-lines-citation-part
)
1509 one-line-citation-part
)
1510 (>= (string-width one-line-citation-part
)
1511 (string-width two-lines-citation-part
)))))
1513 just-one-line-prefix
))))
1515 (defun fill-individual-paragraphs-citation (string citation-regexp
)
1517 (if (string-match citation-regexp string
)
1518 (match-string 0 string
)
1522 ;;; fill.el ends here