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