ChangeLog fix
[emacs.git] / lisp / textmodes / fill.el
blob5b6d5f359e64e1075e91a3f161031ca49e85d977
1 ;;; fill.el --- fill commands for Emacs -*- coding: utf-8 -*-
3 ;; Copyright (C) 1985-1986, 1992, 1994-1997, 1999, 2001-2013 Free
4 ;; Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
27 ;; All the commands for filling text. These are documented in the Emacs
28 ;; manual.
30 ;;; Code:
32 (defgroup fill nil
33 "Indenting and filling text."
34 :link '(custom-manual "(emacs)Filling")
35 :group 'editing)
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."
43 :type 'boolean
44 :group 'fill)
46 (defcustom colon-double-space nil
47 "Non-nil means put two spaces after a colon when filling."
48 :type 'boolean
49 :group 'fill)
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."
68 :type 'boolean
69 :group 'fill)
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."
75 (interactive)
76 (let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
77 (if (> (point) left-margin-pos)
78 (progn
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)))
83 (if fill-prefix
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."
89 :type 'boolean
90 :group 'fill)
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
105 a role."
106 :type 'regexp
107 :group 'fill)
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."
119 :type 'regexp
120 :group 'fill)
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)
126 :group 'fill)
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."
139 (save-excursion
140 (if fill-column
141 (let* ((here (line-beginning-position))
142 (here-col 0)
143 (eol (progn (end-of-line) (point)))
144 margin fill-col change col)
145 ;; Look separately at each region of line with a different
146 ;; right-margin.
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))
153 (< col fill-col)))
154 (setq here change
155 here-col col))
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."
164 (interactive "*r")
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
168 ;; a marker.
169 (unless (markerp end) (setq end (copy-marker end t)))
170 (let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +")))
171 (save-excursion
172 (goto-char beg)
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".
176 ;; Blame the typist.
177 (subst-char-in-region beg end ?\t ?\s)
178 (while (and (< (point) end)
179 (re-search-forward end-spc-re end t))
180 (delete-region
181 (cond
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'.
189 ((match-end 1)
190 (min (match-end 0)
191 (+ (if sentence-end-double-space 2 1)
192 (save-excursion (goto-char (match-end 0))
193 (skip-chars-backward " ")
194 (point)))))
195 (t ;; It's not an end of sentence.
196 (+ (match-beginning 0)
197 ;; Determine number of spaces to leave:
198 (save-excursion
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)
207 (t 1))))))
208 (match-end 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)))
213 (if (eq cmp t)
215 (setq cmp (1- (abs cmp)))
216 (unless (zerop cmp)
217 (substring s1 0 cmp)))))
219 (defun fill-match-adaptive-prefix ()
220 (let ((str (or
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.
227 str)))
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))
237 (save-excursion
238 (goto-char from)
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.
244 second-line-prefix)
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))
252 (forward-line 1)
253 (if (< (point) to)
254 (progn
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
266 ;; ` * ...'.
267 (let ((tmp second-line-prefix)
268 (re "\\`"))
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)
275 second-line-prefix
277 ;; Use the longest common substring of both prefixes,
278 ;; if there is one.
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
285 (let ((result
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
291 first-line-prefix))
292 (and comment-start-skip
293 (string-match comment-start-skip
294 first-line-prefix)))
295 first-line-prefix
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"))))
301 result)))))))
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]*$"))
308 (save-excursion
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
321 places."
322 (or (looking-at "[ \t]*[])}»?!;:-]")
323 (save-excursion
324 (skip-chars-backward " \t")
325 (unless (bolp)
326 (backward-char 1)
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."
336 :group 'fill
337 :type 'hook
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."
342 :type 'boolean
343 :group 'fill)
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)))
351 (unless (bolp)
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
360 (save-excursion
361 (skip-chars-backward " ")
362 (and (eq (preceding-char) ?.)
363 (looking-at " \\([^ ]\\|$\\)"))))
364 ;; Another approach to the same problem.
365 (save-excursion
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
371 (save-excursion
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.
376 (and (not (eolp))
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.")
386 (progn
387 ;; Register `kinsoku' for scripts HAN, KANA, BOPOMOFO, and CJK-MISC.
388 ;; Also tell that they don't use space between words.
389 (map-char-table
390 #'(lambda (key val)
391 (when (memq val '(han kana bopomofo cjk-misc))
392 (set-char-table-range fill-find-break-point-function-table
393 key 'kinsoku)
394 (set-char-table-range fill-nospace-between-words-table
395 key t)))
396 char-script-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."
413 (let ((func (or
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)))
424 (concat "[ \t]*\\("
425 (replace-regexp-in-string
426 "[ \t]+" "[ \t]*"
427 (regexp-quote prefix))
428 "\\)?[ \t]*")
429 "[ \t]*")))
430 (goto-char from)
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"))
435 (forward-line 1)
436 (while (< (point) to)
437 (if (looking-at fpre)
438 (delete-region (point) (match-end 0)))
439 (forward-line 1))
440 (goto-char from)
441 (if (looking-at fpre)
442 (goto-char (match-end 0)))
443 (point)))
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)
452 (goto-char from)
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
456 (cond
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))))
471 (goto-char from)
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))))))
492 (goto-char from)
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"))))
502 (goto-char from))
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.
515 (while
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.
522 (forward-char 1)
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))
537 (first t))
538 (goto-char linebeg)
539 (while (and (< (point) to) (or first (fill-nobreak-p)))
540 ;; Find a breakable point while ignoring the
541 ;; following spaces.
542 (skip-chars-forward " \t")
543 (if (looking-at "\\c|")
544 (forward-char 1)
545 (let ((pos (save-excursion
546 (skip-chars-forward "^ \n\t")
547 (point))))
548 (if (re-search-forward "\\c|" pos t)
549 (forward-char -1)
550 (goto-char pos))))
551 (setq first nil)))
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))
566 prop-list)
567 (while l
568 (unless (eq (car l) 'composition)
569 (setq prop-list
570 (cons (car l) (cons (cadr l) prop-list))))
571 (setq l (cddr l)))
572 prop-list))
574 (defun fill-newline ()
575 ;; Replace whitespace here with one newline, then
576 ;; indent to left margin.
577 (skip-chars-backward " \t")
578 (insert ?\n)
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)) ?|)
584 (match-end 2))
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
587 ;; when we unfill.
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)
593 '(invisible t)))
594 (if (or fill-prefix
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'."
606 (let ((beg (point)))
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."
631 (interactive (progn
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))
645 (oneleft nil))
647 (beginning-of-line)
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.
657 (goto-char to)
658 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
659 (if (and oneleft
660 (not (and use-hard-newlines
661 (get-text-property (1- (point)) 'hard))))
662 (delete-char -1)
663 (backward-char 1)
664 (setq oneleft t)))
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))
669 ;; (newline))
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)))
689 (goto-char from)
690 (beginning-of-line)
692 (if (not justify) ; filling disabled: just check indentation
693 (progn
694 (goto-char from)
695 (while (< (point) to)
696 (if (and (not (eolp))
697 (< (current-indentation) (current-left-margin)))
698 (fill-indent-to-left-margin))
699 (forward-line 1)))
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)
709 (setq from (point))
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.
717 (goto-char from)
718 (let (linebeg)
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 ;; Use an immediately following space, if any.
725 ;; However, note that `move-to-column' may overshoot
726 ;; if there are wide characters (Bug#3234).
727 (unless (> (current-column) (current-fill-column))
728 (forward-char 1))
729 (fill-move-to-break-point linebeg)
730 ;; Check again to see if we got to the end of
731 ;; the paragraph.
732 (skip-chars-forward " \t")
733 (< (point) to))
734 ;; Found a place to cut.
735 (progn
736 (fill-newline)
737 (when justify
738 ;; Justify the line just ended, if desired.
739 (save-excursion
740 (forward-line -1)
741 (justify-current-line justify nil t))))
743 (goto-char to)
744 ;; Justify this last line, if desired.
745 (if justify (justify-current-line justify t t))))))
746 ;; Leave point after final newline.
747 (goto-char to)
748 (unless (eobp) (forward-char 1))
749 ;; Return the fill-prefix we used
750 fill-prefix)))
752 (defsubst skip-line-prefix (prefix)
753 "If point is inside the string PREFIX at the beginning of line, move past it."
754 (when (and prefix
755 (< (- (point) (line-beginning-position)) (length prefix))
756 (save-excursion
757 (beginning-of-line)
758 (looking-at (regexp-quote prefix))))
759 (goto-char (match-end 0))))
761 (defun fill-minibuffer-function (arg)
762 "Fill a paragraph in the minibuffer, ignoring the prompt."
763 (save-restriction
764 (narrow-to-region (minibuffer-prompt-end) (point-max))
765 (fill-paragraph arg)))
767 (defvar fill-forward-paragraph-function 'forward-paragraph
768 "Function to move over paragraphs used by the filling code.
769 It is called with a single argument specifying the number of paragraphs to move.
770 Just like `forward-paragraph', it should return the number of paragraphs
771 left to move.")
773 (defun fill-forward-paragraph (arg)
774 (funcall fill-forward-paragraph-function arg))
776 (defun fill-paragraph (&optional justify region)
777 "Fill paragraph at or after point.
779 If JUSTIFY is non-nil (interactively, with prefix argument), justify as well.
780 If `sentence-end-double-space' is non-nil, then period followed by one
781 space does not end a sentence, so don't break a line there.
782 The variable `fill-column' controls the width for filling.
784 If `fill-paragraph-function' is non-nil, we call it (passing our
785 argument to it), and if it returns non-nil, we simply return its value.
787 If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling.
789 The REGION argument is non-nil if called interactively; in that
790 case, if Transient Mark mode is enabled and the mark is active,
791 call `fill-region' to fill each of the paragraphs in the active
792 region, instead of just filling the current paragraph."
793 (interactive (progn
794 (barf-if-buffer-read-only)
795 (list (if current-prefix-arg 'full) t)))
797 ;; 1. Fill the region if it is active when called interactively.
798 (and region transient-mark-mode mark-active
799 (not (eq (region-beginning) (region-end)))
800 (or (fill-region (region-beginning) (region-end) justify) t))
801 ;; 2. Try fill-paragraph-function.
802 (and (not (eq fill-paragraph-function t))
803 (or fill-paragraph-function
804 (and (minibufferp (current-buffer))
805 (= 1 (point-min))))
806 (let ((function (or fill-paragraph-function
807 ;; In the minibuffer, don't count the width
808 ;; of the prompt.
809 'fill-minibuffer-function))
810 ;; If fill-paragraph-function is set, it probably takes care
811 ;; of comments and stuff. If not, it will have to set
812 ;; fill-paragraph-handle-comment back to t explicitly or
813 ;; return nil.
814 (fill-paragraph-handle-comment nil)
815 (fill-paragraph-function t))
816 (funcall function justify)))
817 ;; 3. Try our syntax-aware filling code.
818 (and fill-paragraph-handle-comment
819 ;; Our code only handles \n-terminated comments right now.
820 comment-start (equal comment-end "")
821 (let ((fill-paragraph-handle-comment nil))
822 (fill-comment-paragraph justify)))
823 ;; 4. If it all fails, default to the good ol' text paragraph filling.
824 (let ((before (point))
825 (paragraph-start paragraph-start)
826 ;; Fill prefix used for filling the paragraph.
827 fill-pfx)
828 ;; Try to prevent code sections and comment sections from being
829 ;; filled together.
830 (when (and fill-paragraph-handle-comment comment-start-skip)
831 (setq paragraph-start
832 (concat paragraph-start "\\|[ \t]*\\(?:"
833 comment-start-skip "\\)")))
834 (save-excursion
835 ;; To make sure the return value of forward-paragraph is meaningful,
836 ;; we have to start from the beginning of line, otherwise skipping
837 ;; past the last few chars of a paragraph-separator would count as
838 ;; a paragraph (and not skipping any chars at EOB would not count
839 ;; as a paragraph even if it is).
840 (move-to-left-margin)
841 (if (not (zerop (fill-forward-paragraph 1)))
842 ;; There's no paragraph at or after point: give up.
843 (setq fill-pfx "")
844 (let ((end (point))
845 (beg (progn (fill-forward-paragraph -1) (point))))
846 (goto-char before)
847 (setq fill-pfx
848 (if use-hard-newlines
849 ;; Can't use fill-region-as-paragraph, since this
850 ;; paragraph may still contain hard newlines. See
851 ;; fill-region.
852 (fill-region beg end justify)
853 (fill-region-as-paragraph beg end justify))))))
854 fill-pfx)))
856 (declare-function comment-search-forward "newcomment" (limit &optional noerror))
857 (declare-function comment-string-strip "newcomment" (str beforep afterp))
860 (defun fill-comment-paragraph (&optional justify)
861 "Fill current comment.
862 If we're not in a comment, just return nil so that the caller
863 can take care of filling. JUSTIFY is used as in `fill-paragraph'."
864 (comment-normalize-vars)
865 (let (has-code-and-comment ; Non-nil if it contains code and a comment.
866 comin comstart)
867 ;; Figure out what kind of comment we are looking at.
868 (save-excursion
869 (beginning-of-line)
870 (when (setq comstart (comment-search-forward (line-end-position) t))
871 (setq comin (point))
872 (goto-char comstart) (skip-chars-backward " \t")
873 (setq has-code-and-comment (not (bolp)))))
875 (if (not (and comstart
876 ;; Make sure the comment-start mark we found is accepted by
877 ;; comment-start-skip. If not, all bets are off, and
878 ;; we'd better not mess with it.
879 (string-match comment-start-skip
880 (buffer-substring comstart comin))))
882 ;; Return nil, so the normal filling will take place.
885 ;; Narrow to include only the comment, and then fill the region.
886 (let* ((fill-prefix fill-prefix)
887 (commark
888 (comment-string-strip (buffer-substring comstart comin) nil t))
889 (comment-re
890 ;; A regexp more specialized than comment-start-skip, that only
891 ;; matches the current commark rather than any valid commark.
893 ;; The specialized regexp only works for "normal" comment
894 ;; syntax, not for Texinfo's "@c" (which can't be immediately
895 ;; followed by word-chars) or Fortran's "C" (which needs to be
896 ;; at bol), so check that comment-start-skip indeed allows the
897 ;; commark to appear in the middle of the line and followed by
898 ;; word chars. The choice of "\0" and "a" is mostly arbitrary.
899 (if (string-match comment-start-skip (concat "\0" commark "a"))
900 (concat "[ \t]*" (regexp-quote commark)
901 ;; Make sure we only match comments that
902 ;; use the exact same comment marker.
903 "[^" (substring commark -1) "]")
904 (concat "[ \t]*\\(?:" comment-start-skip "\\)")))
905 (comment-fill-prefix ; Compute a fill prefix.
906 (save-excursion
907 (goto-char comstart)
908 (if has-code-and-comment
909 (concat
910 (if (not indent-tabs-mode)
911 (make-string (current-column) ?\s)
912 (concat
913 (make-string (/ (current-column) tab-width) ?\t)
914 (make-string (% (current-column) tab-width) ?\s)))
915 (buffer-substring (point) comin))
916 (buffer-substring (line-beginning-position) comin))))
917 beg end)
918 (save-excursion
919 (save-restriction
920 (beginning-of-line)
921 (narrow-to-region
922 ;; Find the first line we should include in the region to fill.
923 (if has-code-and-comment
924 (line-beginning-position)
925 (save-excursion
926 (while (and (zerop (forward-line -1))
927 (looking-at comment-re)))
928 ;; We may have gone too far. Go forward again.
929 (line-beginning-position
930 (if (progn
931 (goto-char
932 (or (comment-search-forward (line-end-position) t)
933 (point)))
934 (looking-at comment-re))
935 (progn (setq comstart (point)) 1)
936 (progn (setq comstart (point)) 2)))))
937 ;; Find the beginning of the first line past the region to fill.
938 (save-excursion
939 (while (progn (forward-line 1)
940 (looking-at comment-re)))
941 (point)))
942 ;; Obey paragraph starters and boundaries within comments.
943 (let* ((paragraph-separate
944 ;; Use the default values since they correspond to
945 ;; the values to use for plain text.
946 (concat paragraph-separate "\\|[ \t]*\\(?:"
947 comment-start-skip "\\)\\(?:"
948 (default-value 'paragraph-separate) "\\)"))
949 (paragraph-start
950 (concat paragraph-start "\\|[ \t]*\\(?:"
951 comment-start-skip "\\)\\(?:"
952 (default-value 'paragraph-start) "\\)"))
953 ;; We used to rely on fill-prefix to break paragraph at
954 ;; comment-starter changes, but it did not work for the
955 ;; first line (mixed comment&code).
956 ;; We now use comment-re instead to "manually" make sure
957 ;; we treat comment-marker changes as paragraph boundaries.
958 ;; (paragraph-ignore-fill-prefix nil)
959 ;; (fill-prefix comment-fill-prefix)
960 (after-line (if has-code-and-comment
961 (line-beginning-position 2))))
962 (setq end (progn (forward-paragraph) (point)))
963 ;; If this comment starts on a line with code,
964 ;; include that line in the filling.
965 (setq beg (progn (backward-paragraph)
966 (if (eq (point) after-line)
967 (forward-line -1))
968 (point)))))
970 ;; Find the fill-prefix to use.
971 (cond
972 (fill-prefix) ; Use the user-provided fill prefix.
973 ((and adaptive-fill-mode ; Try adaptive fill mode.
974 (setq fill-prefix (fill-context-prefix beg end))
975 (string-match comment-start-skip fill-prefix)))
977 (setq fill-prefix comment-fill-prefix)))
979 ;; Don't fill with narrowing.
981 (fill-region-as-paragraph
982 (max comstart beg) end justify nil
983 ;; Don't canonicalize spaces within the code just before
984 ;; the comment.
985 (save-excursion
986 (goto-char beg)
987 (if (looking-at fill-prefix)
989 (re-search-forward comment-start-skip))))
990 ;; Make sure we don't return nil.
991 t))))))
993 (defun fill-region (from to &optional justify nosqueeze to-eop)
994 "Fill each of the paragraphs in the region.
995 A prefix arg means justify as well.
996 The `fill-column' variable controls the width.
998 Noninteractively, the third argument JUSTIFY specifies which
999 kind of justification to do: `full', `left', `right', `center',
1000 or `none' (equivalent to nil). A value of t means handle each
1001 paragraph as specified by its text properties.
1003 The fourth arg NOSQUEEZE non-nil means to leave whitespace other
1004 than line breaks untouched, and fifth arg TO-EOP non-nil means
1005 to keep filling to the end of the paragraph (or next hard newline,
1006 if variable `use-hard-newlines' is on).
1008 Return the fill-prefix used for filling the last paragraph.
1010 If `sentence-end-double-space' is non-nil, then period followed by one
1011 space does not end a sentence, so don't break a line there."
1012 (interactive (progn
1013 (barf-if-buffer-read-only)
1014 (list (region-beginning) (region-end)
1015 (if current-prefix-arg 'full))))
1016 (unless (memq justify '(t nil none full center left right))
1017 (setq justify 'full))
1018 (let ((start-point (point-marker))
1019 max beg fill-pfx)
1020 (goto-char (max from to))
1021 (when to-eop
1022 (skip-chars-backward "\n")
1023 (fill-forward-paragraph 1))
1024 (setq max (copy-marker (point) t))
1025 (goto-char (setq beg (min from to)))
1026 (beginning-of-line)
1027 (while (< (point) max)
1028 (let ((initial (point))
1029 end)
1030 ;; If using hard newlines, break at every one for filling
1031 ;; purposes rather than using paragraph breaks.
1032 (if use-hard-newlines
1033 (progn
1034 (while (and (setq end (text-property-any (point) max
1035 'hard t))
1036 (not (= ?\n (char-after end)))
1037 (not (>= end max)))
1038 (goto-char (1+ end)))
1039 (setq end (if end (min max (1+ end)) max))
1040 (goto-char initial))
1041 (fill-forward-paragraph 1)
1042 (setq end (min max (point)))
1043 (fill-forward-paragraph -1))
1044 (if (< (point) beg)
1045 (goto-char beg))
1046 (if (and (>= (point) initial) (< (point) end))
1047 (setq fill-pfx
1048 (fill-region-as-paragraph (point) end justify nosqueeze))
1049 (goto-char end))))
1050 (goto-char start-point)
1051 (set-marker start-point nil)
1052 fill-pfx))
1055 (defcustom default-justification 'left
1056 "Method of justifying text not otherwise specified.
1057 Possible values are `left', `right', `full', `center', or `none'.
1058 The requested kind of justification is done whenever lines are filled.
1059 The `justification' text-property can locally override this variable."
1060 :type '(choice (const left)
1061 (const right)
1062 (const full)
1063 (const center)
1064 (const none))
1065 :safe 'symbolp
1066 :group 'fill)
1067 (make-variable-buffer-local 'default-justification)
1069 (defun current-justification ()
1070 "How should we justify this line?
1071 This returns the value of the text-property `justification',
1072 or the variable `default-justification' if there is no text-property.
1073 However, it returns nil rather than `none' to mean \"don't justify\"."
1074 (let ((j (or (get-text-property
1075 ;; Make sure we're looking at paragraph body.
1076 (save-excursion (skip-chars-forward " \t")
1077 (if (and (eobp) (not (bobp)))
1078 (1- (point)) (point)))
1079 'justification)
1080 default-justification)))
1081 (if (eq 'none j)
1083 j)))
1085 (defun set-justification (begin end style &optional whole-par)
1086 "Set the region's justification style to STYLE.
1087 This commands prompts for the kind of justification to use.
1088 If the mark is not active, this command operates on the current paragraph.
1089 If the mark is active, it operates on the region. However, if the
1090 beginning and end of the region are not at paragraph breaks, they are
1091 moved to the beginning and end \(respectively) of the paragraphs they
1092 are in.
1094 If variable `use-hard-newlines' is true, all hard newlines are
1095 taken to be paragraph breaks.
1097 When calling from a program, operates just on region between BEGIN and END,
1098 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
1099 extended to include entire paragraphs as in the interactive command."
1100 (interactive (list (if mark-active (region-beginning) (point))
1101 (if mark-active (region-end) (point))
1102 (let ((s (completing-read
1103 "Set justification to: "
1104 '(("left") ("right") ("full")
1105 ("center") ("none"))
1106 nil t)))
1107 (if (equal s "") (error ""))
1108 (intern s))
1110 (save-excursion
1111 (save-restriction
1112 (if whole-par
1113 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
1114 (paragraph-ignore-fill-prefix (if use-hard-newlines t
1115 paragraph-ignore-fill-prefix)))
1116 (goto-char begin)
1117 (while (and (bolp) (not (eobp))) (forward-char 1))
1118 (backward-paragraph)
1119 (setq begin (point))
1120 (goto-char end)
1121 (skip-chars-backward " \t\n" begin)
1122 (forward-paragraph)
1123 (setq end (point))))
1125 (narrow-to-region (point-min) end)
1126 (unjustify-region begin (point-max))
1127 (put-text-property begin (point-max) 'justification style)
1128 (fill-region begin (point-max) nil t))))
1130 (defun set-justification-none (b e)
1131 "Disable automatic filling for paragraphs in the region.
1132 If the mark is not active, this applies to the current paragraph."
1133 (interactive (list (if mark-active (region-beginning) (point))
1134 (if mark-active (region-end) (point))))
1135 (set-justification b e 'none t))
1137 (defun set-justification-left (b e)
1138 "Make paragraphs in the region left-justified.
1139 This means they are flush at the left margin and ragged on the right.
1140 This is usually the default, but see the variable `default-justification'.
1141 If the mark is not active, this applies to the current paragraph."
1142 (interactive (list (if mark-active (region-beginning) (point))
1143 (if mark-active (region-end) (point))))
1144 (set-justification b e 'left t))
1146 (defun set-justification-right (b e)
1147 "Make paragraphs in the region right-justified.
1148 This means they are flush at the right margin and ragged on the left.
1149 If the mark is not active, this applies to the current paragraph."
1150 (interactive (list (if mark-active (region-beginning) (point))
1151 (if mark-active (region-end) (point))))
1152 (set-justification b e 'right t))
1154 (defun set-justification-full (b e)
1155 "Make paragraphs in the region fully justified.
1156 This makes lines flush on both margins by inserting spaces between words.
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 'full t))
1162 (defun set-justification-center (b e)
1163 "Make paragraphs in the region centered.
1164 If the mark is not active, this applies to the current paragraph."
1165 (interactive (list (if mark-active (region-beginning) (point))
1166 (if mark-active (region-end) (point))))
1167 (set-justification b e 'center t))
1169 ;; A line has up to six parts:
1171 ;; >>> hello.
1172 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
1174 ;; "Indent-1" is the left-margin indentation; normally it ends at column
1175 ;; given by the `current-left-margin' function.
1176 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
1177 ;; "Indent-2" is added to justify a line if the `current-justification' is
1178 ;; `center' or `right'. In `left' and `full' justification regions, any
1179 ;; whitespace there is part of the line's text, and should not be changed.
1180 ;; Trailing whitespace is not counted as part of the line length when
1181 ;; center- or right-justifying.
1183 ;; All parts of the line are optional, although the final newline can
1184 ;; only be missing on the last line of the buffer.
1186 (defun justify-current-line (&optional how eop nosqueeze)
1187 "Do some kind of justification on this line.
1188 Normally does full justification: adds spaces to the line to make it end at
1189 the column given by `current-fill-column'.
1190 Optional first argument HOW specifies alternate type of justification:
1191 it can be `left', `right', `full', `center', or `none'.
1192 If HOW is t, will justify however the `current-justification' function says to.
1193 If HOW is nil or missing, full justification is done by default.
1194 Second arg EOP non-nil means that this is the last line of the paragraph, so
1195 it will not be stretched by full justification.
1196 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
1197 otherwise it is made canonical."
1198 (interactive "*")
1199 (if (eq t how) (setq how (or (current-justification) 'none))
1200 (if (null how) (setq how 'full)
1201 (or (memq how '(none left right center))
1202 (setq how 'full))))
1203 (or (memq how '(none left)) ; No action required for these.
1204 (let ((fc (current-fill-column))
1205 (pos (point-marker))
1206 fp-end ; point at end of fill prefix
1207 beg ; point at beginning of line's text
1208 end ; point at end of line's text
1209 indent ; column of `beg'
1210 endcol ; column of `end'
1211 ncols ; new indent point or offset
1212 (nspaces 0) ; number of spaces between words
1213 ; in line (not space characters)
1214 (curr-fracspace 0) ; current fractional space amount
1215 count)
1216 (end-of-line)
1217 ;; Check if this is the last line of the paragraph.
1218 (if (and use-hard-newlines (null eop)
1219 (get-text-property (point) 'hard))
1220 (setq eop t))
1221 (skip-chars-backward " \t")
1222 ;; Quick exit if it appears to be properly justified already
1223 ;; or there is no text.
1224 (if (or (bolp)
1225 (and (memq how '(full right))
1226 (= (current-column) fc)))
1228 (setq end (point))
1229 (beginning-of-line)
1230 (skip-chars-forward " \t")
1231 ;; Skip over fill-prefix.
1232 (if (and fill-prefix
1233 (not (string-equal fill-prefix ""))
1234 (equal fill-prefix
1235 (buffer-substring
1236 (point) (min (point-max) (+ (length fill-prefix)
1237 (point))))))
1238 (forward-char (length fill-prefix))
1239 (if (and adaptive-fill-mode
1240 (looking-at adaptive-fill-regexp))
1241 (goto-char (match-end 0))))
1242 (setq fp-end (point))
1243 (skip-chars-forward " \t")
1244 ;; This is beginning of the line's text.
1245 (setq indent (current-column))
1246 (setq beg (point))
1247 (goto-char end)
1248 (setq endcol (current-column))
1250 ;; HOW can't be null or left--we would have exited already
1251 (cond ((eq 'right how)
1252 (setq ncols (- fc endcol))
1253 (if (< ncols 0)
1254 ;; Need to remove some indentation
1255 (delete-region
1256 (progn (goto-char fp-end)
1257 (if (< (current-column) (+ indent ncols))
1258 (move-to-column (+ indent ncols) t))
1259 (point))
1260 (progn (move-to-column indent) (point)))
1261 ;; Need to add some
1262 (goto-char beg)
1263 (indent-to (+ indent ncols))
1264 ;; If point was at beginning of text, keep it there.
1265 (if (= beg pos)
1266 (move-marker pos (point)))))
1268 ((eq 'center how)
1269 ;; Figure out how much indentation is needed
1270 (setq ncols (+ (current-left-margin)
1271 (/ (- fc (current-left-margin) ;avail. space
1272 (- endcol indent)) ;text width
1273 2)))
1274 (if (< ncols indent)
1275 ;; Have too much indentation - remove some
1276 (delete-region
1277 (progn (goto-char fp-end)
1278 (if (< (current-column) ncols)
1279 (move-to-column ncols t))
1280 (point))
1281 (progn (move-to-column indent) (point)))
1282 ;; Have too little - add some
1283 (goto-char beg)
1284 (indent-to ncols)
1285 ;; If point was at beginning of text, keep it there.
1286 (if (= beg pos)
1287 (move-marker pos (point)))))
1289 ((eq 'full how)
1290 ;; Insert extra spaces between words to justify line
1291 (save-restriction
1292 (narrow-to-region beg end)
1293 (or nosqueeze
1294 (canonically-space-region beg end))
1295 (goto-char (point-max))
1296 ;; count word spaces in line
1297 (while (search-backward " " nil t)
1298 (setq nspaces (1+ nspaces))
1299 (skip-chars-backward " "))
1300 (setq ncols (- fc endcol))
1301 ;; Ncols is number of additional space chars needed
1302 (when (and (> ncols 0) (> nspaces 0) (not eop))
1303 (setq curr-fracspace (+ ncols (/ nspaces 2))
1304 count nspaces)
1305 (while (> count 0)
1306 (skip-chars-forward " ")
1307 (insert-char ?\s (/ curr-fracspace nspaces) t)
1308 (search-forward " " nil t)
1309 (setq count (1- count)
1310 curr-fracspace
1311 (+ (% curr-fracspace nspaces) ncols))))))
1312 (t (error "Unknown justification value"))))
1313 (goto-char pos)
1314 (move-marker pos nil)))
1315 nil)
1317 (defun unjustify-current-line ()
1318 "Remove justification whitespace from current line.
1319 If the line is centered or right-justified, this function removes any
1320 indentation past the left margin. If the line is full-justified, it removes
1321 extra spaces between words. It does nothing in other justification modes."
1322 (let ((justify (current-justification)))
1323 (cond ((eq 'left justify) nil)
1324 ((eq nil justify) nil)
1325 ((eq 'full justify) ; full justify: remove extra spaces
1326 (beginning-of-line-text)
1327 (canonically-space-region (point) (line-end-position)))
1328 ((memq justify '(center right))
1329 (save-excursion
1330 (move-to-left-margin nil t)
1331 ;; Position ourselves after any fill-prefix.
1332 (if (and fill-prefix
1333 (not (string-equal fill-prefix ""))
1334 (equal fill-prefix
1335 (buffer-substring
1336 (point) (min (point-max) (+ (length fill-prefix)
1337 (point))))))
1338 (forward-char (length fill-prefix)))
1339 (delete-region (point) (progn (skip-chars-forward " \t")
1340 (point))))))))
1342 (defun unjustify-region (&optional begin end)
1343 "Remove justification whitespace from region.
1344 For centered or right-justified regions, this function removes any indentation
1345 past the left margin from each line. For full-justified lines, it removes
1346 extra spaces between words. It does nothing in other justification modes.
1347 Arguments BEGIN and END are optional; default is the whole buffer."
1348 (save-excursion
1349 (save-restriction
1350 (if end (narrow-to-region (point-min) end))
1351 (goto-char (or begin (point-min)))
1352 (while (not (eobp))
1353 (unjustify-current-line)
1354 (forward-line 1)))))
1357 (defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
1358 "Fill paragraphs within the region, allowing varying indentation within each.
1359 This command divides the region into \"paragraphs\",
1360 only at paragraph-separator lines, then fills each paragraph
1361 using as the fill prefix the smallest indentation of any line
1362 in the paragraph.
1364 When calling from a program, pass range to fill as first two arguments.
1366 Optional third and fourth arguments JUSTIFYP and CITATION-REGEXP:
1367 JUSTIFYP to justify paragraphs (prefix arg).
1368 When filling a mail message, pass a regexp for CITATION-REGEXP
1369 which will match the prefix of a line which is a citation marker
1370 plus whitespace, but no other kind of prefix.
1371 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1372 (interactive (progn
1373 (barf-if-buffer-read-only)
1374 (list (region-beginning) (region-end)
1375 (if current-prefix-arg 'full))))
1376 (let ((fill-individual-varying-indent t))
1377 (fill-individual-paragraphs min max justifyp citation-regexp)))
1379 (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
1380 "Fill paragraphs of uniform indentation within the region.
1381 This command divides the region into \"paragraphs\",
1382 treating every change in indentation level or prefix as a paragraph boundary,
1383 then fills each paragraph using its indentation level as the fill prefix.
1385 There is one special case where a change in indentation does not start
1386 a new paragraph. This is for text of this form:
1388 foo> This line with extra indentation starts
1389 foo> a paragraph that continues on more lines.
1391 These lines are filled together.
1393 When calling from a program, pass the range to fill
1394 as the first two arguments.
1396 Optional third and fourth arguments JUSTIFY and CITATION-REGEXP:
1397 JUSTIFY to justify paragraphs (prefix arg).
1398 When filling a mail message, pass a regexp for CITATION-REGEXP
1399 which will match the prefix of a line which is a citation marker
1400 plus whitespace, but no other kind of prefix.
1401 Also, if CITATION-REGEXP is non-nil, don't fill header lines."
1402 (interactive (progn
1403 (barf-if-buffer-read-only)
1404 (list (region-beginning) (region-end)
1405 (if current-prefix-arg 'full))))
1406 (save-restriction
1407 (save-excursion
1408 (goto-char min)
1409 (beginning-of-line)
1410 (narrow-to-region (point) max)
1411 (if citation-regexp
1412 (while (and (not (eobp))
1413 (or (looking-at "[ \t]*[^ \t\n]+:")
1414 (looking-at "[ \t]*$")))
1415 (if (looking-at "[ \t]*[^ \t\n]+:")
1416 (search-forward "\n\n" nil 'move)
1417 (forward-line 1))))
1418 (narrow-to-region (point) max)
1419 ;; Loop over paragraphs.
1420 (while (progn
1421 ;; Skip over all paragraph-separating lines
1422 ;; so as to not include them in any paragraph.
1423 (while (and (not (eobp))
1424 (progn (move-to-left-margin)
1425 (and (not (eobp))
1426 (looking-at paragraph-separate))))
1427 (forward-line 1))
1428 (skip-chars-forward " \t\n") (not (eobp)))
1429 (move-to-left-margin)
1430 (let ((start (point))
1431 fill-prefix fill-prefix-regexp)
1432 ;; Find end of paragraph, and compute the smallest fill-prefix
1433 ;; that fits all the lines in this paragraph.
1434 (while (progn
1435 ;; Update the fill-prefix on the first line
1436 ;; and whenever the prefix good so far is too long.
1437 (if (not (and fill-prefix
1438 (looking-at fill-prefix-regexp)))
1439 (setq fill-prefix
1440 (fill-individual-paragraphs-prefix
1441 citation-regexp)
1442 fill-prefix-regexp (regexp-quote fill-prefix)))
1443 (forward-line 1)
1444 (if (bolp)
1445 ;; If forward-line went past a newline,
1446 ;; move further to the left margin.
1447 (move-to-left-margin))
1448 ;; Now stop the loop if end of paragraph.
1449 (and (not (eobp))
1450 (if fill-individual-varying-indent
1451 ;; If this line is a separator line, with or
1452 ;; without prefix, end the paragraph.
1453 (and
1454 (not (looking-at paragraph-separate))
1455 (save-excursion
1456 (not (and (looking-at fill-prefix-regexp)
1457 (progn (forward-char
1458 (length fill-prefix))
1459 (looking-at
1460 paragraph-separate))))))
1461 ;; If this line has more or less indent
1462 ;; than the fill prefix wants, end the paragraph.
1463 (and (looking-at fill-prefix-regexp)
1464 ;; If fill prefix is shorter than a new
1465 ;; fill prefix computed here, end paragraph.
1466 (let ((this-line-fill-prefix
1467 (fill-individual-paragraphs-prefix
1468 citation-regexp)))
1469 (>= (length fill-prefix)
1470 (length this-line-fill-prefix)))
1471 (save-excursion
1472 (not (progn (forward-char
1473 (length fill-prefix))
1474 (or (looking-at "[ \t]")
1475 (looking-at paragraph-separate)
1476 (looking-at paragraph-start)))))
1477 (not (and (equal fill-prefix "")
1478 citation-regexp
1479 (looking-at citation-regexp))))))))
1480 ;; Fill this paragraph, but don't add a newline at the end.
1481 (let ((had-newline (bolp)))
1482 (fill-region-as-paragraph start (point) justify)
1483 (if (and (bolp) (not had-newline))
1484 (delete-char -1))))))))
1486 (defun fill-individual-paragraphs-prefix (citation-regexp)
1487 (let* ((adaptive-fill-first-line-regexp ".*")
1488 (just-one-line-prefix
1489 ;; Accept any prefix rather than just the ones matched by
1490 ;; adaptive-fill-first-line-regexp.
1491 (fill-context-prefix (point) (line-beginning-position 2)))
1492 (two-lines-prefix
1493 (fill-context-prefix (point) (line-beginning-position 3))))
1494 (if (not just-one-line-prefix)
1495 (buffer-substring
1496 (point) (save-excursion (skip-chars-forward " \t") (point)))
1497 ;; See if the citation part of JUST-ONE-LINE-PREFIX
1498 ;; is the same as that of TWO-LINES-PREFIX,
1499 ;; except perhaps with longer whitespace.
1500 (if (and just-one-line-prefix two-lines-prefix
1501 (let* ((one-line-citation-part
1502 (fill-individual-paragraphs-citation
1503 just-one-line-prefix citation-regexp))
1504 (two-lines-citation-part
1505 (fill-individual-paragraphs-citation
1506 two-lines-prefix citation-regexp))
1507 (adjusted-two-lines-citation-part
1508 (substring two-lines-citation-part 0
1509 (string-match "[ \t]*\\'"
1510 two-lines-citation-part))))
1511 (and
1512 (string-match (concat "\\`"
1513 (regexp-quote
1514 adjusted-two-lines-citation-part)
1515 "[ \t]*\\'")
1516 one-line-citation-part)
1517 (>= (string-width one-line-citation-part)
1518 (string-width two-lines-citation-part)))))
1519 two-lines-prefix
1520 just-one-line-prefix))))
1522 (defun fill-individual-paragraphs-citation (string citation-regexp)
1523 (if citation-regexp
1524 (if (string-match citation-regexp string)
1525 (match-string 0 string)
1527 string))
1529 ;;; fill.el ends here