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