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