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