0
|
1 ;;; fill.el --- fill commands for XEmacs.
|
|
2
|
112
|
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 1997 Free Software Foundation, Inc.
|
0
|
4
|
|
5 ;; Keywords: wp
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
21 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
22 ;; 02111-1307, USA.
|
0
|
23
|
2
|
24 ;;; Synched up with: FSF 19.34.
|
0
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; All the commands for filling text. These are documented in the XEmacs
|
|
29 ;; Reference Manual.
|
|
30
|
110
|
31 ;; 97/3/14 Jareth Hein (jhod@po.iijnet.or.jp) added functions for kinsoku (asian text
|
|
32 ;; line break processing)
|
159
|
33 ;; 97/06/11 Steve Baur (steve@altair.xemacs.org) converted broken
|
|
34 ;; following-char/preceding-char calls to char-after/char-before.
|
110
|
35
|
0
|
36 ;;; Code:
|
|
37
|
2
|
38 (defconst fill-individual-varying-indent nil
|
0
|
39 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
|
|
40 Non-nil means changing indent doesn't end a paragraph.
|
|
41 That mode can handle paragraphs with extra indentation on the first line,
|
|
42 but it requires separator lines between paragraphs.
|
|
43 A value of nil means that any change in indentation starts a new paragraph.")
|
|
44
|
2
|
45 (defconst sentence-end-double-space t
|
70
|
46 "*Non-nil means a single space does not end a sentence.")
|
0
|
47
|
|
48 (defconst colon-double-space nil
|
|
49 "*Non-nil means put two spaces after a colon when filling.")
|
|
50
|
|
51 (defvar fill-paragraph-function nil
|
2
|
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.")
|
0
|
54
|
|
55 (defun set-fill-prefix ()
|
|
56 "Set the fill prefix to the current line up to point.
|
2
|
57 Filling expects lines to start with the fill prefix and
|
|
58 reinserts the fill prefix in each resulting line."
|
0
|
59 (interactive)
|
|
60 (setq fill-prefix (buffer-substring
|
|
61 (save-excursion (move-to-left-margin) (point))
|
|
62 (point)))
|
|
63 (if (equal fill-prefix "")
|
|
64 (setq fill-prefix nil))
|
|
65 (if fill-prefix
|
|
66 (message "fill-prefix: \"%s\"" fill-prefix)
|
|
67 (message "fill-prefix cancelled")))
|
|
68
|
2
|
69 (defconst adaptive-fill-mode t
|
0
|
70 "*Non-nil means determine a paragraph's fill prefix from its text.")
|
|
71
|
|
72 ;; #### - this is still weak. Yeah, there's filladapt, but this should
|
|
73 ;; still be better... --Stig
|
2
|
74 (defconst adaptive-fill-regexp (purecopy "[ \t]*\\([#;>*]+ +\\)?")
|
0
|
75 "*Regexp to match text at start of line that constitutes indentation.
|
|
76 If Adaptive Fill mode is enabled, whatever text matches this pattern
|
|
77 on the second line of a paragraph is used as the standard indentation
|
|
78 for the paragraph. If the paragraph has just one line, the indentation
|
|
79 is taken from that line.")
|
|
80
|
|
81 (defvar adaptive-fill-function nil
|
|
82 "*Function to call to choose a fill prefix for a paragraph.
|
|
83 This function is used when `adaptive-fill-regexp' does not match.")
|
|
84
|
110
|
85 ;; Added for kinsoku processing. Use this instead of
|
|
86 ;; (skip-chars-backward "^ \t\n")
|
|
87 ;; (skip-chars-backward "^ \n" linebeg)
|
|
88 (defun fill-move-backward-to-break-point (regexp &optional lim)
|
|
89 (let ((opoint (point)))
|
|
90 ;; 93.8.23 by kawamoto@ics.es.osaka-u.ac.jp
|
|
91 ;; case of first 'word' being longer than fill-column
|
|
92 (if (not (re-search-backward regexp lim 'move))
|
|
93 nil
|
|
94 ;; we have skipped backward SPC or WAN (word-across-newline). So move point forward again.
|
|
95 (forward-char)
|
112
|
96 (if (< opoint (point))
|
110
|
97 (forward-char -1)))))
|
|
98
|
|
99 ;; Added for kinsoku processing. Use instead of
|
|
100 ;; (re-search-forward "[ \t]" opoint t)
|
|
101 ;; (skip-chars-forward "^ \n")
|
|
102 ;; (skip-chars-forward "^ \n")
|
|
103 (defun fill-move-forward-to-break-point (regexp &optional lim)
|
|
104 (let ((opoint (point)))
|
|
105 (if (not (re-search-forward regexp lim 'move))
|
|
106 nil
|
|
107 (forward-char -1)
|
112
|
108 (if (< (point) opoint)
|
110
|
109 (forward-char))))
|
|
110 (if (featurep 'mule) (kinsoku-process-extend)))
|
|
111
|
|
112 (defun fill-end-of-sentence-p ()
|
|
113 (save-excursion
|
|
114 (skip-chars-backward " ]})\"'")
|
159
|
115 (memq (char-before (point)) '(?. ?? ?!))))
|
110
|
116
|
0
|
117 (defun current-fill-column ()
|
|
118 "Return the fill-column to use for this line.
|
|
119 The fill-column to use for a buffer is stored in the variable `fill-column',
|
|
120 but can be locally modified by the `right-margin' text property, which is
|
|
121 subtracted from `fill-column'.
|
|
122
|
|
123 The fill column to use for a line is the first column at which the column
|
|
124 number equals or exceeds the local fill-column - right-margin difference."
|
|
125 (save-excursion
|
|
126 (if fill-column
|
|
127 (let* ((here (progn (beginning-of-line) (point)))
|
|
128 (here-col 0)
|
|
129 (eol (progn (end-of-line) (point)))
|
|
130 margin fill-col change col)
|
|
131 ;; Look separately at each region of line with a different right-margin.
|
|
132 (while (and (setq margin (get-text-property here 'right-margin)
|
|
133 fill-col (- fill-column (or margin 0))
|
|
134 change (text-property-not-all
|
|
135 here eol 'right-margin margin))
|
|
136 (progn (goto-char (1- change))
|
|
137 (setq col (current-column))
|
|
138 (< col fill-col)))
|
|
139 (setq here change
|
|
140 here-col col))
|
|
141 (max here-col fill-col)))))
|
|
142
|
|
143 (defun canonically-space-region (beg end)
|
|
144 "Remove extra spaces between words in region.
|
2
|
145 Leave one space between words, two at end of sentences or after colons
|
110
|
146 \(depending on values of `sentence-end-double-space' and `colon-double-space').
|
|
147 Remove indentation from each line."
|
0
|
148 (interactive "r")
|
110
|
149 ;;;### 97/3/14 jhod: Do I have to add anything here for kinsoku?
|
0
|
150 (save-excursion
|
|
151 (goto-char beg)
|
|
152 ;; XEmacs - (ENE/stig from fa-extras.el): Skip the start of a comment.
|
|
153 (and comment-start-skip
|
|
154 (looking-at comment-start-skip)
|
|
155 (goto-char (match-end 0)))
|
|
156 ;; Nuke tabs; they get screwed up in a fill.
|
|
157 ;; This is quick, but loses when a tab follows the end of a sentence.
|
|
158 ;; Actually, it is difficult to tell that from "Mr.\tSmith".
|
|
159 ;; Blame the typist.
|
|
160 (subst-char-in-region beg end ?\t ?\ )
|
|
161 (while (and (< (point) end)
|
|
162 (re-search-forward " *" end t))
|
|
163 (delete-region
|
|
164 (+ (match-beginning 0)
|
|
165 ;; Determine number of spaces to leave:
|
|
166 (save-excursion
|
|
167 (skip-chars-backward " ]})\"'")
|
|
168 (cond ((and sentence-end-double-space
|
159
|
169 (memq (char-before (point)) '(?. ?? ?!))) 2)
|
0
|
170 ((and colon-double-space
|
159
|
171 (eq (char-before (point)) ?:)) 2)
|
|
172 ((char-equal (char-before (point)) ?\n) 0)
|
0
|
173 (t 1))))
|
|
174 (match-end 0)))
|
|
175 ;; Make sure sentences ending at end of line get an extra space.
|
|
176 ;; loses on split abbrevs ("Mr.\nSmith")
|
|
177 (goto-char beg)
|
|
178 (while (and (< (point) end)
|
|
179 (re-search-forward "[.?!][])}\"']*$" end t))
|
2
|
180 ;; We insert before markers in case a caller such as
|
|
181 ;; do-auto-fill has done a save-excursion with point at the end
|
|
182 ;; of the line and wants it to stay at the end of the line.
|
0
|
183 (insert ? ))))
|
2
|
184 ;; XEmacs: we don't have this function.
|
|
185 ;; (insert-before-markers-and-inherit ? ))))
|
0
|
186
|
|
187 ;; XEmacs -- added DONT-SKIP-FIRST. Port of older code changes by Stig.
|
|
188 ;; #### probably this junk is broken -- do-auto-fill doesn't actually use
|
|
189 ;; it. If so, it should be removed.
|
110
|
190
|
0
|
191 (defun fill-context-prefix (from to &optional first-line-regexp
|
|
192 dont-skip-first)
|
|
193 "Compute a fill prefix from the text between FROM and TO.
|
2
|
194 This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
|
0
|
195 If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
|
|
196 first line, insist it must match FIRST-LINE-REGEXP."
|
|
197 (save-excursion
|
|
198 (goto-char from)
|
|
199 (if (eolp) (forward-line 1))
|
|
200 ;; Move to the second line unless there is just one.
|
|
201 (let ((firstline (point))
|
|
202 ;; Non-nil if we are on the second line.
|
|
203 at-second
|
|
204 result)
|
2
|
205 ;; XEmacs change
|
0
|
206 (if (not dont-skip-first)
|
|
207 (forward-line 1))
|
|
208 (if (>= (point) to)
|
|
209 (goto-char firstline)
|
|
210 (setq at-second t))
|
|
211 (move-to-left-margin)
|
2
|
212 ;; XEmacs change
|
0
|
213 (let ((start (point))
|
110
|
214 ; jhod: no longer used?
|
|
215 ;(eol (save-excursion (end-of-line) (point)))
|
|
216 )
|
0
|
217 (setq result
|
|
218 (if (not (looking-at paragraph-start))
|
|
219 (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
|
|
220 (buffer-substring-no-properties start (match-end 0)))
|
|
221 (adaptive-fill-function (funcall adaptive-fill-function)))))
|
|
222 (and result
|
|
223 (or at-second
|
|
224 (null first-line-regexp)
|
|
225 (string-match first-line-regexp result))
|
|
226 result)))))
|
|
227
|
|
228 ;; XEmacs (stig) - this is pulled out of fill-region-as-paragraph so that it
|
|
229 ;; can also be called from do-auto-fill
|
|
230 ;; #### But it's not used there. Chuck pulled it out because it broke things.
|
|
231 (defun maybe-adapt-fill-prefix (&optional from to dont-skip-first)
|
|
232 (if (and adaptive-fill-mode
|
|
233 (or (null fill-prefix) (string= fill-prefix "")))
|
|
234 (setq fill-prefix (fill-context-prefix from to nil dont-skip-first))))
|
|
235
|
2
|
236 (defun fill-region-as-paragraph (from to &optional justify
|
|
237 nosqueeze squeeze-after)
|
0
|
238 "Fill the region as one paragraph.
|
|
239 It removes any paragraph breaks in the region and extra newlines at the end,
|
|
240 indents and fills lines between the margins given by the
|
|
241 `current-left-margin' and `current-fill-column' functions.
|
|
242 It leaves point at the beginning of the line following the paragraph.
|
|
243
|
|
244 Normally performs justification according to the `current-justification'
|
|
245 function, but with a prefix arg, does full justification instead.
|
|
246
|
|
247 From a program, optional third arg JUSTIFY can specify any type of
|
108
|
248 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
|
2
|
249 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
|
|
250 means don't canonicalize spaces before that position.
|
0
|
251
|
|
252 If `sentence-end-double-space' is non-nil, then period followed by one
|
|
253 space does not end a sentence, so don't break a line there."
|
|
254 (interactive
|
|
255 (progn
|
|
256 ;; XEmacs addition:
|
|
257 (barf-if-buffer-read-only nil (region-beginning) (region-end))
|
|
258 (list (region-beginning) (region-end)
|
|
259 (if current-prefix-arg 'full))))
|
|
260 ;; Arrange for undoing the fill to restore point.
|
|
261 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
|
|
262 (setq buffer-undo-list (cons (point) buffer-undo-list)))
|
|
263
|
|
264 ;; Make sure "to" is the endpoint.
|
|
265 (goto-char (min from to))
|
|
266 (setq to (max from to))
|
|
267 ;; Ignore blank lines at beginning of region.
|
|
268 (skip-chars-forward " \t\n")
|
|
269
|
|
270 (let ((from-plus-indent (point))
|
|
271 (oneleft nil))
|
|
272
|
|
273 (beginning-of-line)
|
|
274 (setq from (point))
|
|
275
|
|
276 ;; Delete all but one soft newline at end of region.
|
2
|
277 ;; And leave TO before that one.
|
0
|
278 (goto-char to)
|
|
279 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
|
|
280 (if (and oneleft
|
|
281 (not (and use-hard-newlines
|
|
282 (get-text-property (1- (point)) 'hard))))
|
|
283 (delete-backward-char 1)
|
|
284 (backward-char 1)
|
|
285 (setq oneleft t)))
|
|
286 (setq to (point))
|
|
287
|
|
288 ;; If there was no newline, and there is text in the paragraph, then
|
|
289 ;; create a newline.
|
|
290 (if (and (not oneleft) (> to from-plus-indent))
|
|
291 (newline))
|
|
292 (goto-char from-plus-indent))
|
|
293
|
|
294 (if (not (> to (point)))
|
|
295 nil ; There is no paragraph, only whitespace: exit now.
|
|
296
|
|
297 (or justify (setq justify (current-justification)))
|
|
298
|
|
299 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
|
|
300 (let ((fill-prefix fill-prefix))
|
|
301 ;; Figure out how this paragraph is indented, if desired.
|
|
302 ;; XEmacs: move some code here to a separate function.
|
|
303 (maybe-adapt-fill-prefix from to t)
|
|
304
|
|
305 (save-restriction
|
|
306 (goto-char from)
|
|
307 (beginning-of-line)
|
|
308 (narrow-to-region (point) to)
|
|
309
|
|
310 (if (not justify) ; filling disabled: just check indentation
|
|
311 (progn
|
|
312 (goto-char from)
|
|
313 (while (not (eobp))
|
|
314 (if (and (not (eolp))
|
|
315 (< (current-indentation) (current-left-margin)))
|
|
316 (indent-to-left-margin))
|
|
317 (forward-line 1)))
|
|
318
|
|
319 (if use-hard-newlines
|
|
320 (remove-text-properties from (point-max) '(hard nil)))
|
|
321 ;; Make sure first line is indented (at least) to left margin...
|
|
322 (if (or (memq justify '(right center))
|
|
323 (< (current-indentation) (current-left-margin)))
|
|
324 (indent-to-left-margin))
|
|
325 ;; Delete the fill prefix from every line except the first.
|
|
326 ;; The first line may not even have a fill prefix.
|
|
327 (goto-char from)
|
|
328 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
|
|
329 (concat "[ \t]*"
|
|
330 (regexp-quote fill-prefix)
|
|
331 "[ \t]*"))))
|
|
332 (and fpre
|
|
333 (progn
|
|
334 (if (>= (+ (current-left-margin) (length fill-prefix))
|
|
335 (current-fill-column))
|
|
336 (error "fill-prefix too long for specified width"))
|
|
337 (goto-char from)
|
|
338 (forward-line 1)
|
|
339 (while (not (eobp))
|
|
340 (if (looking-at fpre)
|
|
341 (delete-region (point) (match-end 0)))
|
|
342 (forward-line 1))
|
|
343 (goto-char from)
|
2
|
344 (if (looking-at fpre)
|
|
345 (goto-char (match-end 0)))
|
0
|
346 (setq from (point)))))
|
|
347 ;; Remove indentation from lines other than the first.
|
|
348 (beginning-of-line 2)
|
|
349 (indent-region (point) (point-max) 0)
|
|
350 (goto-char from)
|
|
351
|
|
352 ;; FROM, and point, are now before the text to fill,
|
|
353 ;; but after any fill prefix on the first line.
|
|
354
|
|
355 ;; Make sure sentences ending at end of line get an extra space.
|
|
356 ;; loses on split abbrevs ("Mr.\nSmith")
|
|
357 (while (re-search-forward "[.?!][])}\"']*$" nil t)
|
2
|
358 ;; XEmacs change (no insert-and-inherit)
|
129
|
359 (or (eobp) (insert ?\ ?\ )))
|
0
|
360 (goto-char from)
|
|
361 (skip-chars-forward " \t")
|
|
362 ;; Then change all newlines to spaces.
|
110
|
363 ;;; 97/3/14 jhod: Kinsoku change
|
|
364 ;; Spacing is not necessary for charcters of no word-separater.
|
|
365 ;; The regexp word-across-newline is used for this check.
|
|
366 (if (not (and (featurep 'mule)
|
|
367 (stringp word-across-newline)))
|
|
368 (subst-char-in-region from (point-max) ?\n ?\ )
|
|
369 ;;
|
|
370 ;; WAN +NL+WAN --> WAN + WAN
|
|
371 ;; not(WAN)+NL+WAN --> not(WAN) + WAN
|
|
372 ;; WAN +NL+not(WAN) --> WAN + not(WAN)
|
|
373 ;; SPC +NL+not(WAN) --> SPC + not(WAN)
|
|
374 ;; not(WAN)+NL+not(WAN) --> not(WAN) + SPC + not(WAN)
|
|
375 ;;
|
|
376 (goto-char from)
|
|
377 (end-of-line)
|
|
378 (while (not (eobp))
|
|
379 ;; Insert SPC only when point is between nonWAN. Insert
|
|
380 ;; before deleting to preserve marker if possible.
|
|
381 (if (or (prog2 ; check following char.
|
|
382 (forward-char) ; skip newline
|
|
383 (or (eobp)
|
|
384 (looking-at word-across-newline))
|
|
385 (forward-char -1))
|
|
386 (prog2 ; check previous char.
|
|
387 (forward-char -1)
|
159
|
388 (or (eq (char-after (point)) ?\ )
|
110
|
389 (looking-at word-across-newline))
|
|
390 (forward-char)))
|
|
391 nil
|
|
392 (insert ?\ ))
|
|
393 (delete-char 1) ; delete newline
|
|
394 (end-of-line)))
|
|
395 ;; end patch
|
129
|
396 (goto-char from)
|
142
|
397 (skip-chars-forward " \t")
|
110
|
398 (if (and nosqueeze (not (eq justify 'full)))
|
0
|
399 nil
|
2
|
400 (canonically-space-region (or squeeze-after (point)) (point-max))
|
0
|
401 (goto-char (point-max))
|
|
402 (delete-horizontal-space)
|
2
|
403 ;; XEmacs change (no insert-and-inherit)
|
0
|
404 (insert " "))
|
|
405 (goto-char (point-min))
|
|
406
|
|
407 ;; This is the actual filling loop.
|
110
|
408 (let ((prefixcol 0) linebeg
|
|
409 (re-break-point (if (featurep 'mule)
|
|
410 (concat "[ \n\t]\\|" word-across-newline)
|
|
411 "[ \n\t]")))
|
0
|
412 (while (not (eobp))
|
|
413 (setq linebeg (point))
|
|
414 (move-to-column (1+ (current-fill-column)))
|
|
415 (if (eobp)
|
|
416 (or nosqueeze (delete-horizontal-space))
|
|
417 ;; Move back to start of word.
|
110
|
418 ;; 97/3/14 jhod: Kinsoku
|
|
419 ;(skip-chars-backward "^ \n" linebeg)
|
|
420 (fill-move-backward-to-break-point re-break-point linebeg)
|
|
421 ;; end patch
|
0
|
422 ;; Don't break after a period followed by just one space.
|
|
423 ;; Move back to the previous place to break.
|
|
424 ;; The reason is that if a period ends up at the end of a line,
|
|
425 ;; further fills will assume it ends a sentence.
|
|
426 ;; If we now know it does not end a sentence,
|
|
427 ;; avoid putting it at the end of the line.
|
|
428 (if sentence-end-double-space
|
|
429 (while (and (> (point) (+ linebeg 2))
|
159
|
430 (eq (char-before (point)) ?\ )
|
|
431 (not (eq (char-after (point)) ?\ ))
|
0
|
432 (eq (char-after (- (point) 2)) ?\.))
|
|
433 (forward-char -2)
|
110
|
434 ;; 97/3/14 jhod: Kinsoku
|
|
435 ;(skip-chars-backward "^ \n" linebeg)))
|
|
436 (fill-move-backward-to-break-point re-break-point linebeg)))
|
|
437 (if (featurep 'mule) (kinsoku-process))
|
|
438 ;end patch
|
|
439
|
0
|
440 ;; If the left margin and fill prefix by themselves
|
2
|
441 ;; pass the fill-column. or if they are zero
|
|
442 ;; but we have no room for even one word,
|
|
443 ;; keep at least one word anyway.
|
0
|
444 ;; This handles ALL BUT the first line of the paragraph.
|
|
445 (if (if (zerop prefixcol)
|
|
446 (save-excursion
|
|
447 (skip-chars-backward " \t" linebeg)
|
|
448 (bolp))
|
|
449 (>= prefixcol (current-column)))
|
|
450 ;; Ok, skip at least one word.
|
|
451 ;; Meanwhile, don't stop at a period followed by one space.
|
|
452 (let ((first t))
|
|
453 (move-to-column prefixcol)
|
|
454 (while (and (not (eobp))
|
|
455 (or first
|
|
456 (and (not (bobp))
|
|
457 sentence-end-double-space
|
|
458 (save-excursion (forward-char -1)
|
|
459 (and (looking-at "\\. ")
|
|
460 (not (looking-at "\\. ")))))))
|
|
461 (skip-chars-forward " \t")
|
110
|
462 ;; 94/3/14 jhod: Kinsoku
|
|
463 ;(skip-chars-forward "^ \n\t")
|
|
464 (fill-move-forward-to-break-point re-break-point)
|
|
465 ;; end patch
|
0
|
466 (setq first nil)))
|
|
467 ;; Normally, move back over the single space between the words.
|
159
|
468 (if (eq (char-before (point)) ?\ )
|
126
|
469 (forward-char -1)))
|
0
|
470 ;; If the left margin and fill prefix by themselves
|
|
471 ;; pass the fill-column, keep at least one word.
|
|
472 ;; This handles the first line of the paragraph.
|
|
473 (if (and (zerop prefixcol)
|
|
474 (let ((fill-point (point)) nchars)
|
|
475 (save-excursion
|
|
476 (move-to-left-margin)
|
|
477 (setq nchars (- fill-point (point)))
|
|
478 (or (< nchars 0)
|
|
479 (and fill-prefix
|
|
480 (< nchars (length fill-prefix))
|
|
481 (string= (buffer-substring (point) fill-point)
|
|
482 (substring fill-prefix 0 nchars)))))))
|
|
483 ;; Ok, skip at least one word. But
|
|
484 ;; don't stop at a period followed by just one space.
|
|
485 (let ((first t))
|
|
486 (while (and (not (eobp))
|
|
487 (or first
|
|
488 (and (not (bobp))
|
|
489 sentence-end-double-space
|
|
490 (save-excursion (forward-char -1)
|
|
491 (and (looking-at "\\. ")
|
|
492 (not (looking-at "\\. ")))))))
|
|
493 (skip-chars-forward " \t")
|
110
|
494 ;; 97/3/14 jhod: Kinsoku
|
|
495 ;(skip-chars-forward "^ \t\n")
|
|
496 (fill-move-forward-to-break-point re-break-point)
|
|
497 ;; end patch
|
0
|
498 (setq first nil))))
|
2
|
499 ;; Check again to see if we got to the end of the paragraph.
|
|
500 (if (save-excursion (skip-chars-forward " \t") (eobp))
|
|
501 (or nosqueeze (delete-horizontal-space))
|
|
502 ;; Replace whitespace here with one newline, then indent to left
|
|
503 ;; margin.
|
|
504 (skip-chars-backward " \t")
|
110
|
505 ;; 97/3/14 jhod: More kinsoku stuff
|
|
506 (if (featurep 'mule)
|
|
507 ;; WAN means chars which match word-across-newline.
|
|
508 ;; (0) | SPC + SPC* <EOB> --> NL
|
|
509 ;; (1) WAN | SPC + SPC* --> WAN + SPC + NL
|
|
510 ;; (2) | SPC + SPC* + WAN --> SPC + NL + WAN
|
|
511 ;; (3) '.' | SPC + nonSPC --> '.' + SPC + NL + nonSPC
|
|
512 ;; (4) '.' | SPC + SPC --> '.' + NL
|
|
513 ;; (5) | SPC* --> NL
|
|
514 (let ((start (point)) ; 92.6.30 by K.Handa
|
159
|
515 (ch (char-after (point))))
|
110
|
516 (if (and (= ch ? )
|
|
517 (progn ; not case (0) -- 92.6.30 by K.Handa
|
|
518 (skip-chars-forward " \t")
|
|
519 (not (eobp)))
|
|
520 (or
|
|
521 (progn ; case (1)
|
|
522 (goto-char start)
|
|
523 (forward-char -1)
|
|
524 (looking-at word-across-newline))
|
|
525 (progn ; case (2)
|
|
526 (goto-char start)
|
|
527 (skip-chars-forward " \t")
|
|
528 (and (not (eobp))
|
|
529 (looking-at word-across-newline)
|
|
530 ;; never leave space after the end of sentence
|
|
531 (not (fill-end-of-sentence-p))))
|
|
532 (progn ; case (3)
|
|
533 (goto-char (1+ start))
|
|
534 (and (not (eobp))
|
159
|
535 (not (eq (char-after (point)) ? ))
|
110
|
536 (fill-end-of-sentence-p)))))
|
|
537 ;; We should keep one SPACE before NEWLINE. (1),(2),(3)
|
|
538 (goto-char (1+ start))
|
|
539 ;; We should delete all SPACES around break point. (4),(5)
|
|
540 (goto-char start))))
|
|
541 ;; end of patch
|
2
|
542 (insert ?\n)
|
|
543 ;; Give newline the properties of the space(s) it replaces
|
|
544 (set-text-properties (1- (point)) (point)
|
|
545 (text-properties-at (point)))
|
|
546 (indent-to-left-margin)
|
|
547 ;; Insert the fill prefix after indentation.
|
|
548 ;; Set prefixcol so whitespace in the prefix won't get lost.
|
|
549 (and fill-prefix (not (equal fill-prefix ""))
|
|
550 (progn
|
|
551 (insert fill-prefix)
|
|
552 (setq prefixcol (current-column))))))
|
0
|
553 ;; Justify the line just ended, if desired.
|
|
554 (if justify
|
|
555 (if (eobp)
|
|
556 (justify-current-line justify t t)
|
|
557 (forward-line -1)
|
|
558 (justify-current-line justify nil t)
|
|
559 (forward-line 1))))))
|
|
560 ;; Leave point after final newline.
|
|
561 (goto-char (point-max)))
|
|
562 (forward-char 1))))
|
|
563
|
|
564 (defun fill-paragraph (arg)
|
|
565 "Fill paragraph at or after point. Prefix arg means justify as well.
|
|
566 If `sentence-end-double-space' is non-nil, then period followed by one
|
|
567 space does not end a sentence, so don't break a line there.
|
|
568
|
|
569 If `fill-paragraph-function' is non-nil, we call it (passing our
|
|
570 argument to it), and if it returns non-nil, we simply return its value."
|
|
571 (interactive (list (if current-prefix-arg 'full)))
|
|
572 (or (and fill-paragraph-function
|
|
573 (let ((function fill-paragraph-function)
|
|
574 fill-paragraph-function)
|
|
575 (funcall function arg)))
|
|
576 (let ((before (point)))
|
|
577 (save-excursion
|
|
578 (forward-paragraph)
|
|
579 (or (bolp) (newline 1))
|
|
580 (let ((end (point))
|
|
581 (beg (progn (backward-paragraph) (point))))
|
|
582 (goto-char before)
|
|
583 (if use-hard-newlines
|
|
584 ;; Can't use fill-region-as-paragraph, since this paragraph may
|
|
585 ;; still contain hard newlines. See fill-region.
|
|
586 (fill-region beg end arg)
|
|
587 (fill-region-as-paragraph beg end arg)))))))
|
|
588
|
|
589 (defun fill-region (from to &optional justify nosqueeze to-eop)
|
|
590 "Fill each of the paragraphs in the region.
|
|
591 Prefix arg (non-nil third arg, if called from program) means justify as well.
|
|
592
|
|
593 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
|
|
594 whitespace other than line breaks untouched, and fifth arg TO-EOP
|
|
595 non-nil means to keep filling to the end of the paragraph (or next
|
|
596 hard newline, if `use-hard-newlines' is on).
|
|
597
|
|
598 If `sentence-end-double-space' is non-nil, then period followed by one
|
|
599 space does not end a sentence, so don't break a line there."
|
|
600 (interactive
|
|
601 (progn
|
|
602 ;; XEmacs addition:
|
|
603 (barf-if-buffer-read-only nil (region-beginning) (region-end))
|
|
604 (list (region-beginning) (region-end)
|
|
605 (if current-prefix-arg 'full))))
|
|
606 (let (end beg)
|
|
607 (save-restriction
|
|
608 (goto-char (max from to))
|
|
609 (if to-eop
|
|
610 (progn (skip-chars-backward "\n")
|
|
611 (forward-paragraph)))
|
|
612 (setq end (point))
|
|
613 (goto-char (setq beg (min from to)))
|
|
614 (beginning-of-line)
|
|
615 (narrow-to-region (point) end)
|
|
616 (while (not (eobp))
|
|
617 (let ((initial (point))
|
|
618 end)
|
|
619 ;; If using hard newlines, break at every one for filling
|
|
620 ;; purposes rather than using paragraph breaks.
|
|
621 (if use-hard-newlines
|
|
622 (progn
|
|
623 (while (and (setq end (text-property-any (point) (point-max)
|
|
624 'hard t))
|
159
|
625 (not (eq ?\n (char-after end)))
|
0
|
626 (not (= end (point-max))))
|
|
627 (goto-char (1+ end)))
|
|
628 (setq end (if end (min (point-max) (1+ end)) (point-max)))
|
|
629 (goto-char initial))
|
|
630 (forward-paragraph 1)
|
|
631 (setq end (point))
|
|
632 (forward-paragraph -1))
|
|
633 (if (< (point) beg)
|
|
634 (goto-char beg))
|
|
635 (if (>= (point) initial)
|
|
636 (fill-region-as-paragraph (point) end justify nosqueeze)
|
|
637 (goto-char end)))))))
|
|
638
|
|
639 ;; XEmacs addition: from Tim Bradshaw <tfb@edinburgh.ac.uk>
|
|
640 (defun fill-paragraph-or-region (arg)
|
|
641 "Fill the current region, if it's active; otherwise, fill the paragraph.
|
|
642 See `fill-paragraph' and `fill-region' for more information."
|
|
643 (interactive "*P")
|
|
644 (if (region-active-p)
|
|
645 (fill-region (point) (mark) arg)
|
|
646 (fill-paragraph arg)))
|
|
647
|
|
648
|
|
649 (defconst default-justification 'left
|
|
650 "*Method of justifying text not otherwise specified.
|
|
651 Possible values are `left', `right', `full', `center', or `none'.
|
|
652 The requested kind of justification is done whenever lines are filled.
|
|
653 The `justification' text-property can locally override this variable.
|
|
654 This variable automatically becomes buffer-local when set in any fashion.")
|
|
655 (make-variable-buffer-local 'default-justification)
|
|
656
|
|
657 (defun current-justification ()
|
|
658 "How should we justify this line?
|
|
659 This returns the value of the text-property `justification',
|
|
660 or the variable `default-justification' if there is no text-property.
|
|
661 However, it returns nil rather than `none' to mean \"don't justify\"."
|
|
662 (let ((j (or (get-text-property
|
|
663 ;; Make sure we're looking at paragraph body.
|
|
664 (save-excursion (skip-chars-forward " \t")
|
|
665 (if (and (eobp) (not (bobp)))
|
|
666 (1- (point)) (point)))
|
|
667 'justification)
|
|
668 default-justification)))
|
|
669 (if (eq 'none j)
|
|
670 nil
|
|
671 j)))
|
|
672
|
|
673 (defun set-justification (begin end value &optional whole-par)
|
|
674 "Set the region's justification style.
|
|
675 The kind of justification to use is prompted for.
|
|
676 If the mark is not active, this command operates on the current paragraph.
|
|
677 If the mark is active, the region is used. However, if the beginning and end
|
|
678 of the region are not at paragraph breaks, they are moved to the beginning and
|
|
679 end of the paragraphs they are in.
|
|
680 If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
|
|
681 breaks.
|
|
682
|
|
683 When calling from a program, operates just on region between BEGIN and END,
|
|
684 unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
|
|
685 extended to include entire paragraphs as in the interactive command."
|
2
|
686 ;; XEmacs change (was mark-active)
|
0
|
687 (interactive (list (if (region-active-p) (region-beginning) (point))
|
|
688 (if (region-active-p) (region-end) (point))
|
|
689 (let ((s (completing-read
|
|
690 "Set justification to: "
|
|
691 '(("left") ("right") ("full")
|
|
692 ("center") ("none"))
|
|
693 nil t)))
|
|
694 (if (equal s "") (error ""))
|
|
695 (intern s))
|
|
696 t))
|
|
697 (save-excursion
|
|
698 (save-restriction
|
|
699 (if whole-par
|
|
700 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
|
|
701 (paragraph-ignore-fill-prefix (if use-hard-newlines t
|
|
702 paragraph-ignore-fill-prefix)))
|
|
703 (goto-char begin)
|
|
704 (while (and (bolp) (not (eobp))) (forward-char 1))
|
|
705 (backward-paragraph)
|
|
706 (setq begin (point))
|
|
707 (goto-char end)
|
|
708 (skip-chars-backward " \t\n" begin)
|
|
709 (forward-paragraph)
|
|
710 (setq end (point))))
|
|
711
|
|
712 (narrow-to-region (point-min) end)
|
|
713 (unjustify-region begin (point-max))
|
|
714 (put-text-property begin (point-max) 'justification value)
|
|
715 (fill-region begin (point-max) nil t))))
|
|
716
|
|
717 (defun set-justification-none (b e)
|
|
718 "Disable automatic filling for paragraphs in the region.
|
|
719 If the mark is not active, this applies to the current paragraph."
|
2
|
720 ;; XEmacs change (was mark-active)
|
0
|
721 (interactive (list (if (region-active-p) (region-beginning) (point))
|
|
722 (if (region-active-p) (region-end) (point))))
|
|
723 (set-justification b e 'none t))
|
|
724
|
|
725 (defun set-justification-left (b e)
|
|
726 "Make paragraphs in the region left-justified.
|
|
727 This is usually the default, but see the variable `default-justification'.
|
|
728 If the mark is not active, this applies to the current paragraph."
|
2
|
729 ;; XEmacs change (was mark-active)
|
0
|
730 (interactive (list (if (region-active-p) (region-beginning) (point))
|
|
731 (if (region-active-p) (region-end) (point))))
|
|
732 (set-justification b e 'left t))
|
|
733
|
|
734 (defun set-justification-right (b e)
|
|
735 "Make paragraphs in the region right-justified:
|
|
736 Flush at the right margin and ragged on the left.
|
|
737 If the mark is not active, this applies to the current paragraph."
|
2
|
738 ;; XEmacs change (was mark-active)
|
0
|
739 (interactive (list (if (region-active-p) (region-beginning) (point))
|
|
740 (if (region-active-p) (region-end) (point))))
|
|
741 (set-justification b e 'right t))
|
|
742
|
|
743 (defun set-justification-full (b e)
|
|
744 "Make paragraphs in the region fully justified:
|
|
745 This makes lines flush on both margins by inserting spaces between words.
|
|
746 If the mark is not active, this applies to the current paragraph."
|
2
|
747 ;; XEmacs change (was mark-active)
|
0
|
748 (interactive (list (if (region-active-p) (region-beginning) (point))
|
|
749 (if (region-active-p) (region-end) (point))))
|
|
750 (set-justification b e 'full t))
|
|
751
|
|
752 (defun set-justification-center (b e)
|
|
753 "Make paragraphs in the region centered.
|
|
754 If the mark is not active, this applies to the current paragraph."
|
2
|
755 ;; XEmacs change (was mark-active)
|
0
|
756 (interactive (list (if (region-active-p) (region-beginning) (point))
|
|
757 (if (region-active-p) (region-end) (point))))
|
|
758 (set-justification b e 'center t))
|
|
759
|
110
|
760 ;; 97/3/14 jhod: This functions are added for Kinsoku support
|
|
761 (defun find-space-insertable-point ()
|
|
762 "Search backward for a permissable point for inserting justification spaces"
|
|
763 (if (boundp 'space-insertable)
|
|
764 (if (re-search-backward space-insertable nil t)
|
|
765 (progn (forward-char 1)
|
|
766 t)
|
|
767 nil)
|
|
768 (search-backward " " nil t)))
|
|
769
|
0
|
770 ;; A line has up to six parts:
|
|
771 ;;
|
|
772 ;; >>> hello.
|
|
773 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
|
|
774 ;;
|
|
775 ;; "Indent-1" is the left-margin indentation; normally it ends at column
|
|
776 ;; given by the `current-left-margin' function.
|
|
777 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
|
|
778 ;; "Indent-2" is added to justify a line if the `current-justification' is
|
|
779 ;; `center' or `right'. In `left' and `full' justification regions, any
|
|
780 ;; whitespace there is part of the line's text, and should not be changed.
|
|
781 ;; Trailing whitespace is not counted as part of the line length when
|
|
782 ;; center- or right-justifying.
|
|
783 ;;
|
|
784 ;; All parts of the line are optional, although the final newline can
|
|
785 ;; only be missing on the last line of the buffer.
|
|
786
|
|
787 (defun justify-current-line (&optional how eop nosqueeze)
|
|
788 "Do some kind of justification on this line.
|
|
789 Normally does full justification: adds spaces to the line to make it end at
|
|
790 the column given by `current-fill-column'.
|
|
791 Optional first argument HOW specifies alternate type of justification:
|
|
792 it can be `left', `right', `full', `center', or `none'.
|
|
793 If HOW is t, will justify however the `current-justification' function says to.
|
|
794 If HOW is nil or missing, full justification is done by default.
|
|
795 Second arg EOP non-nil means that this is the last line of the paragraph, so
|
|
796 it will not be stretched by full justification.
|
|
797 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
|
|
798 otherwise it is made canonical."
|
|
799 (interactive)
|
|
800 (if (eq t how) (setq how (or (current-justification) 'none))
|
|
801 (if (null how) (setq how 'full)
|
|
802 (or (memq how '(none left right center))
|
|
803 (setq how 'full))))
|
|
804 (or (memq how '(none left)) ; No action required for these.
|
|
805 (let ((fc (current-fill-column))
|
|
806 (pos (point-marker))
|
|
807 fp-end ; point at end of fill prefix
|
|
808 beg ; point at beginning of line's text
|
|
809 end ; point at end of line's text
|
|
810 indent ; column of `beg'
|
|
811 endcol ; column of `end'
|
|
812 ncols) ; new indent point or offset
|
|
813 (end-of-line)
|
|
814 ;; Check if this is the last line of the paragraph.
|
|
815 (if (and use-hard-newlines (null eop)
|
|
816 (get-text-property (point) 'hard))
|
|
817 (setq eop t))
|
|
818 (skip-chars-backward " \t")
|
|
819 ;; Quick exit if it appears to be properly justified already
|
|
820 ;; or there is no text.
|
|
821 (if (or (bolp)
|
|
822 (and (memq how '(full right))
|
|
823 (= (current-column) fc)))
|
|
824 nil
|
|
825 (setq end (point))
|
|
826 (beginning-of-line)
|
|
827 (skip-chars-forward " \t")
|
|
828 ;; Skip over fill-prefix.
|
|
829 (if (and fill-prefix
|
|
830 (not (string-equal fill-prefix ""))
|
|
831 (equal fill-prefix
|
|
832 (buffer-substring
|
|
833 (point) (min (point-max) (+ (length fill-prefix)
|
|
834 (point))))))
|
|
835 (forward-char (length fill-prefix))
|
70
|
836 (if (and adaptive-fill-mode
|
0
|
837 (looking-at adaptive-fill-regexp))
|
|
838 (goto-char (match-end 0))))
|
|
839 (setq fp-end (point))
|
|
840 (skip-chars-forward " \t")
|
|
841 ;; This is beginning of the line's text.
|
|
842 (setq indent (current-column))
|
|
843 (setq beg (point))
|
|
844 (goto-char end)
|
|
845 (setq endcol (current-column))
|
|
846
|
|
847 ;; HOW can't be null or left--we would have exited already
|
|
848 (cond ((eq 'right how)
|
|
849 (setq ncols (- fc endcol))
|
|
850 (if (< ncols 0)
|
|
851 ;; Need to remove some indentation
|
|
852 (delete-region
|
|
853 (progn (goto-char fp-end)
|
|
854 (if (< (current-column) (+ indent ncols))
|
|
855 (move-to-column (+ indent ncols) t))
|
|
856 (point))
|
|
857 (progn (move-to-column indent) (point)))
|
|
858 ;; Need to add some
|
|
859 (goto-char beg)
|
|
860 (indent-to (+ indent ncols))
|
|
861 ;; If point was at beginning of text, keep it there.
|
|
862 (if (= beg pos)
|
|
863 (move-marker pos (point)))))
|
|
864
|
|
865 ((eq 'center how)
|
|
866 ;; Figure out how much indentation is needed
|
|
867 (setq ncols (+ (current-left-margin)
|
|
868 (/ (- fc (current-left-margin) ;avail. space
|
|
869 (- endcol indent)) ;text width
|
|
870 2)))
|
|
871 (if (< ncols indent)
|
|
872 ;; Have too much indentation - remove some
|
|
873 (delete-region
|
|
874 (progn (goto-char fp-end)
|
|
875 (if (< (current-column) ncols)
|
|
876 (move-to-column ncols t))
|
|
877 (point))
|
|
878 (progn (move-to-column indent) (point)))
|
|
879 ;; Have too little - add some
|
|
880 (goto-char beg)
|
|
881 (indent-to ncols)
|
|
882 ;; If point was at beginning of text, keep it there.
|
|
883 (if (= beg pos)
|
|
884 (move-marker pos (point)))))
|
|
885
|
|
886 ((eq 'full how)
|
|
887 ;; Insert extra spaces between words to justify line
|
|
888 (save-restriction
|
|
889 (narrow-to-region beg end)
|
|
890 (or nosqueeze
|
|
891 (canonically-space-region beg end))
|
|
892 (goto-char (point-max))
|
|
893 (setq ncols (- fc endcol))
|
|
894 ;; Ncols is number of additional spaces needed
|
|
895 (if (> ncols 0)
|
|
896 (if (and (not eop)
|
110
|
897 ;; 97/3/14 jhod: Kinsoku
|
|
898 (find-space-insertable-point)) ;(search-backward " " nil t))
|
0
|
899 (while (> ncols 0)
|
|
900 (let ((nmove (+ 3 (random 3))))
|
|
901 (while (> nmove 0)
|
110
|
902 (or (find-space-insertable-point) ;(search-backward " " nil t)
|
0
|
903 (progn
|
|
904 (goto-char (point-max))
|
110
|
905 (find-space-insertable-point))) ;(search-backward " ")))
|
0
|
906 (skip-chars-backward " ")
|
|
907 (setq nmove (1- nmove))))
|
2
|
908 ;; XEmacs change
|
0
|
909 (insert " ")
|
|
910 (skip-chars-backward " ")
|
|
911 (setq ncols (1- ncols)))))))
|
|
912 (t (error "Unknown justification value"))))
|
|
913 (goto-char pos)
|
|
914 (move-marker pos nil)))
|
|
915 nil)
|
|
916
|
|
917 (defun unjustify-current-line ()
|
|
918 "Remove justification whitespace from current line.
|
|
919 If the line is centered or right-justified, this function removes any
|
2
|
920 indentation past the left margin. If the line is full-justified, it removes
|
0
|
921 extra spaces between words. It does nothing in other justification modes."
|
|
922 (let ((justify (current-justification)))
|
|
923 (cond ((eq 'left justify) nil)
|
|
924 ((eq nil justify) nil)
|
|
925 ((eq 'full justify) ; full justify: remove extra spaces
|
|
926 (beginning-of-line-text)
|
|
927 (canonically-space-region
|
|
928 (point) (save-excursion (end-of-line) (point))))
|
|
929 ((memq justify '(center right))
|
|
930 (save-excursion
|
|
931 (move-to-left-margin nil t)
|
|
932 ;; Position ourselves after any fill-prefix.
|
|
933 (if (and fill-prefix
|
|
934 (not (string-equal fill-prefix ""))
|
|
935 (equal fill-prefix
|
|
936 (buffer-substring
|
|
937 (point) (min (point-max) (+ (length fill-prefix)
|
|
938 (point))))))
|
|
939 (forward-char (length fill-prefix)))
|
|
940 (delete-region (point) (progn (skip-chars-forward " \t")
|
|
941 (point))))))))
|
|
942
|
|
943 (defun unjustify-region (&optional begin end)
|
|
944 "Remove justification whitespace from region.
|
|
945 For centered or right-justified regions, this function removes any indentation
|
2
|
946 past the left margin from each line. For full-justified lines, it removes
|
0
|
947 extra spaces between words. It does nothing in other justification modes.
|
|
948 Arguments BEGIN and END are optional; default is the whole buffer."
|
|
949 (save-excursion
|
|
950 (save-restriction
|
|
951 (if end (narrow-to-region (point-min) end))
|
|
952 (goto-char (or begin (point-min)))
|
|
953 (while (not (eobp))
|
|
954 (unjustify-current-line)
|
|
955 (forward-line 1)))))
|
|
956
|
|
957
|
|
958 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
|
|
959 "Fill paragraphs within the region, allowing varying indentation within each.
|
|
960 This command divides the region into \"paragraphs\",
|
|
961 only at paragraph-separator lines, then fills each paragraph
|
|
962 using as the fill prefix the smallest indentation of any line
|
|
963 in the paragraph.
|
|
964
|
|
965 When calling from a program, pass range to fill as first two arguments.
|
|
966
|
|
967 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
|
|
968 JUSTIFY to justify paragraphs (prefix arg),
|
|
969 MAIL-FLAG for a mail message, i. e. don't fill header lines."
|
|
970 (interactive (list (region-beginning) (region-end)
|
|
971 (if current-prefix-arg 'full)))
|
|
972 (let ((fill-individual-varying-indent t))
|
|
973 (fill-individual-paragraphs min max justifyp mailp)))
|
|
974
|
|
975 (defun fill-individual-paragraphs (min max &optional justify mailp)
|
|
976 "Fill paragraphs of uniform indentation within the region.
|
|
977 This command divides the region into \"paragraphs\",
|
|
978 treating every change in indentation level as a paragraph boundary,
|
|
979 then fills each paragraph using its indentation level as the fill prefix.
|
|
980
|
|
981 When calling from a program, pass range to fill as first two arguments.
|
|
982
|
|
983 Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
|
|
984 JUSTIFY to justify paragraphs (prefix arg),
|
|
985 MAIL-FLAG for a mail message, i. e. don't fill header lines."
|
|
986 (interactive (list (region-beginning) (region-end)
|
|
987 (if current-prefix-arg 'full)))
|
|
988 (save-restriction
|
|
989 (save-excursion
|
|
990 (goto-char min)
|
|
991 (beginning-of-line)
|
|
992 (narrow-to-region (point) max)
|
|
993 (if mailp
|
|
994 (while (and (not (eobp))
|
|
995 (or (looking-at "[ \t]*[^ \t\n]+:")
|
|
996 (looking-at "[ \t]*$")))
|
|
997 (if (looking-at "[ \t]*[^ \t\n]+:")
|
|
998 (search-forward "\n\n" nil 'move)
|
|
999 (forward-line 1))))
|
|
1000 (narrow-to-region (point) max)
|
|
1001 ;; Loop over paragraphs.
|
|
1002 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
|
|
1003 (move-to-left-margin)
|
|
1004 (let ((start (point))
|
|
1005 fill-prefix fill-prefix-regexp)
|
|
1006 ;; Find end of paragraph, and compute the smallest fill-prefix
|
|
1007 ;; that fits all the lines in this paragraph.
|
|
1008 (while (progn
|
|
1009 ;; Update the fill-prefix on the first line
|
|
1010 ;; and whenever the prefix good so far is too long.
|
|
1011 (if (not (and fill-prefix
|
|
1012 (looking-at fill-prefix-regexp)))
|
|
1013 (setq fill-prefix
|
|
1014 (if (and adaptive-fill-mode adaptive-fill-regexp
|
|
1015 (looking-at adaptive-fill-regexp))
|
|
1016 (match-string 0)
|
|
1017 (buffer-substring
|
|
1018 (point)
|
|
1019 (save-excursion (skip-chars-forward " \t")
|
|
1020 (point))))
|
|
1021 fill-prefix-regexp (regexp-quote fill-prefix)))
|
|
1022 (forward-line 1)
|
2
|
1023 (if (bolp)
|
|
1024 ;; If forward-line went past a newline
|
|
1025 ;; move further to the left margin.
|
|
1026 (move-to-left-margin))
|
0
|
1027 ;; Now stop the loop if end of paragraph.
|
|
1028 (and (not (eobp))
|
|
1029 (if fill-individual-varying-indent
|
|
1030 ;; If this line is a separator line, with or
|
|
1031 ;; without prefix, end the paragraph.
|
|
1032 (and
|
|
1033 (not (looking-at paragraph-separate))
|
|
1034 (save-excursion
|
|
1035 (not (and (looking-at fill-prefix-regexp)
|
2
|
1036 ;; XEmacs change
|
0
|
1037 (progn
|
|
1038 (forward-char (length fill-prefix))
|
|
1039 (looking-at paragraph-separate))))))
|
|
1040 ;; If this line has more or less indent
|
|
1041 ;; than the fill prefix wants, end the paragraph.
|
|
1042 (and (looking-at fill-prefix-regexp)
|
|
1043 (save-excursion
|
|
1044 (not
|
|
1045 (progn
|
|
1046 (forward-char (length fill-prefix))
|
|
1047 (or (looking-at paragraph-separate)
|
|
1048 (looking-at paragraph-start))))))))))
|
|
1049 ;; Fill this paragraph, but don't add a newline at the end.
|
|
1050 (let ((had-newline (bolp)))
|
|
1051 (fill-region-as-paragraph start (point) justify)
|
|
1052 (or had-newline (delete-char -1))))))))
|
|
1053
|
|
1054 ;;; fill.el ends here
|