108
|
1 ;;; Adaptive fill
|
|
2 ;;; Copyright (C) 1989, 1995, 1996, 1997 Kyle E. Jones
|
0
|
3 ;;;
|
|
4 ;;; This program is free software; you can redistribute it and/or modify
|
|
5 ;;; it under the terms of the GNU General Public License as published by
|
|
6 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
7 ;;; any later version.
|
|
8 ;;;
|
|
9 ;;; This program is distributed in the hope that it will be useful,
|
|
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 ;;; GNU General Public License for more details.
|
|
13 ;;;
|
|
14 ;;; A copy of the GNU General Public License can be obtained from this
|
|
15 ;;; program's author (send electronic mail to kyle@uunet.uu.net) or from
|
|
16 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
|
|
17 ;;; 02139, USA.
|
|
18 ;;;
|
108
|
19 ;;; Send bug reports to kyle_jones@wonderworks.com
|
0
|
20
|
|
21 ;; LCD Archive Entry:
|
|
22 ;; filladapt|Kyle Jones|kyle_jones@wonderworks.com|
|
|
23 ;; Minor mode to adaptively set fill-prefix and overload filling functions|
|
108
|
24 ;; 10-June-1996|2.09|~/packages/filladapt.el|
|
0
|
25
|
|
26 ;; These functions enhance the default behavior of Emacs' Auto Fill
|
108
|
27 ;; mode and the commands fill-paragraph, lisp-fill-paragraph,
|
|
28 ;; fill-region-as-paragraph and fill-region.
|
0
|
29 ;;
|
|
30 ;; The chief improvement is that the beginning of a line to be
|
|
31 ;; filled is examined and, based on information gathered, an
|
|
32 ;; appropriate value for fill-prefix is constructed. Also the
|
|
33 ;; boundaries of the current paragraph are located. This occurs
|
|
34 ;; only if the fill prefix is not already non-nil.
|
|
35 ;;
|
|
36 ;; The net result of this is that blurbs of text that are offset
|
|
37 ;; from left margin by asterisks, dashes, and/or spaces, numbered
|
|
38 ;; examples, included text from USENET news articles, etc. are
|
|
39 ;; generally filled correctly with no fuss.
|
|
40 ;;
|
|
41 ;; Since this package replaces existing Emacs functions, it cannot
|
|
42 ;; be autoloaded. Save this in a file named filladapt.el in a
|
|
43 ;; Lisp directory that Emacs knows about, byte-compile it and put
|
|
44 ;; (require 'filladapt)
|
|
45 ;; in your .emacs file.
|
|
46 ;;
|
|
47 ;; Note that in this release Filladapt mode is a minor mode and it is
|
|
48 ;; _off_ by default. If you want it to be on by default, use
|
|
49 ;; (setq-default filladapt-mode t)
|
|
50 ;;
|
|
51 ;; M-x filladapt-mode toggles Filladapt mode on/off in the current
|
|
52 ;; buffer.
|
|
53 ;;
|
|
54 ;; Use
|
|
55 ;; (add-hook 'text-mode-hook 'turn-on-filladapt-mode)
|
|
56 ;; to have Filladapt always enabled in Text mode.
|
|
57 ;;
|
|
58 ;; Use
|
|
59 ;; (add-hook 'c-mode-hook 'turn-off-filladapt-mode)
|
|
60 ;; to have Filladapt always disabled in C mode.
|
|
61 ;;
|
|
62 ;; In many cases, you can extend Filladapt by adding appropriate
|
|
63 ;; entries to the following three `defvar's. See `postscript-comment'
|
|
64 ;; or `texinfo-comment' as a sample of what needs to be done.
|
|
65 ;;
|
|
66 ;; filladapt-token-table
|
|
67 ;; filladapt-token-match-table
|
|
68 ;; filladapt-token-conversion-table
|
|
69
|
108
|
70 (and (featurep 'filladapt)
|
|
71 (error "filladapt cannot be loaded twice in the same Emacs session."))
|
|
72
|
0
|
73 (provide 'filladapt)
|
|
74
|
108
|
75 (defvar filladapt-version "2.09"
|
0
|
76 "Version string for filladapt.")
|
|
77
|
|
78 (defvar filladapt-mode nil
|
|
79 "*Non-nil means that Filladapt minor mode is enabled.
|
|
80 Use the filladapt-mode command to toggle the mode on/off.")
|
|
81 (make-variable-buffer-local 'filladapt-mode)
|
|
82
|
|
83 (defvar filladapt-mode-line-string " Filladapt"
|
|
84 "*String to display in the modeline when Filladapt mode is active.
|
|
85 Set this to nil if you don't want a modeline indicator for Filladapt.")
|
|
86
|
108
|
87 (defvar filladapt-fill-column-tolerance nil
|
|
88 "*Tolerate filled paragraph lines ending this far from the fill column.
|
|
89 If any lines other than the last paragraph line end at a column
|
|
90 less than fill-column - filladapt-fill-column-tolerance, fill-column will
|
|
91 be adjusted using the filladapt-fill-column-*-fuzz variables and
|
|
92 the paragraph will be re-filled until the tolerance is achieved
|
|
93 or filladapt runs out of fuzz values to try.
|
|
94
|
|
95 A nil value means behave normally, that is, don't try refilling
|
|
96 paragraphs to make filled line lengths fit within any particular
|
|
97 range.")
|
|
98
|
|
99 (defvar filladapt-fill-column-forward-fuzz 5
|
|
100 "*Try values from fill-column to fill-column plus this variable
|
|
101 when trying to make filled paragraph lines fall with the tolerance
|
|
102 range specified by filladapt-fill-column-tolerance.")
|
|
103
|
|
104 (defvar filladapt-fill-column-backward-fuzz 5
|
|
105 "*Try values from fill-column to fill-column minus this variable
|
|
106 when trying to make filled paragraph lines fall with the tolerance
|
|
107 range specified by filladapt-fill-column-tolerance.")
|
|
108
|
0
|
109 ;; install on minor-mode-alist
|
|
110 (or (assq 'filladapt-mode minor-mode-alist)
|
|
111 (setq minor-mode-alist (cons (list 'filladapt-mode
|
|
112 'filladapt-mode-line-string)
|
|
113 minor-mode-alist)))
|
|
114
|
|
115 (defvar filladapt-token-table
|
|
116 '(
|
108
|
117 ;; this must be first
|
|
118 ("^" beginning-of-line)
|
0
|
119 ;; Included text in news or mail replies
|
108
|
120 (">+" citation->)
|
0
|
121 ;; Included text generated by SUPERCITE. We can't hope to match all
|
|
122 ;; the possible variations, your mileage may vary.
|
108
|
123 ("[A-Za-z0-9][^'`\"< \t\n]*>[ \t]*" supercite-citation)
|
0
|
124 ;; Lisp comments
|
108
|
125 (";+" lisp-comment)
|
0
|
126 ;; UNIX shell comments
|
108
|
127 ("#+" sh-comment)
|
0
|
128 ;; Postscript comments
|
108
|
129 ("%+" postscript-comment)
|
0
|
130 ;; C++ comments
|
108
|
131 ("///*" c++-comment)
|
0
|
132 ;; Texinfo comments
|
108
|
133 ("@c[ \t]" texinfo-comment)
|
|
134 ("@comment[ \t]" texinfo-comment)
|
0
|
135 ;; Bullet types.
|
|
136 ;;
|
108
|
137 ;; LaTex \item
|
|
138 ;;
|
|
139 ("\\\\item[ \t]" bullet)
|
|
140 ;;
|
0
|
141 ;; 1. xxxxx
|
|
142 ;; xxxxx
|
|
143 ;;
|
108
|
144 ("[0-9]+\\.[ \t]" bullet)
|
0
|
145 ;;
|
|
146 ;; 2.1.3 xxxxx xx x xx x
|
|
147 ;; xxx
|
|
148 ;;
|
108
|
149 ("[0-9]+\\(\\.[0-9]+\\)+[ \t]" bullet)
|
0
|
150 ;;
|
|
151 ;; a. xxxxxx xx
|
|
152 ;; xxx xxx
|
|
153 ;;
|
108
|
154 ("[A-Za-z]\\.[ \t]" bullet)
|
0
|
155 ;;
|
|
156 ;; 1) xxxx x xx x xx or (1) xx xx x x xx xx
|
|
157 ;; xx xx xxxx xxx xx x x xx x
|
|
158 ;;
|
108
|
159 ("(?[0-9]+)[ \t]" bullet)
|
0
|
160 ;;
|
|
161 ;; a) xxxx x xx x xx or (a) xx xx x x xx xx
|
|
162 ;; xx xx xxxx xxx xx x x xx x
|
|
163 ;;
|
108
|
164 ("(?[A-Za-z])[ \t]" bullet)
|
0
|
165 ;;
|
|
166 ;; 2a. xx x xxx x x xxx
|
|
167 ;; xxx xx x xx x
|
|
168 ;;
|
108
|
169 ("[0-9]+[A-Za-z]\\.[ \t]" bullet)
|
0
|
170 ;;
|
|
171 ;; 1a) xxxx x xx x xx or (1a) xx xx x x xx xx
|
|
172 ;; xx xx xxxx xxx xx x x xx x
|
|
173 ;;
|
108
|
174 ("(?[0-9]+[A-Za-z])[ \t]" bullet)
|
0
|
175 ;;
|
|
176 ;; - xx xxx xxxx or * xx xx x xxx xxx
|
|
177 ;; xxx xx xx x xxx x xx x x x
|
|
178 ;;
|
108
|
179 ("[-~*+]+[ \t]" bullet)
|
0
|
180 ;;
|
|
181 ;; o xx xxx xxxx xx x xx xxx x xxx xx x xxx
|
|
182 ;; xxx xx xx
|
|
183 ;;
|
108
|
184 ("o[ \t]" bullet)
|
0
|
185 ;; don't touch
|
108
|
186 ("[ \t]+" space)
|
|
187 ("$" end-of-line)
|
0
|
188 )
|
|
189 "Table of tokens filladapt knows about.
|
|
190 Format is
|
|
191
|
108
|
192 ((REGEXP SYM) ...)
|
0
|
193
|
|
194 filladapt uses this table to build a tokenized representation of
|
|
195 the beginning of the current line. Each REGEXP is matched
|
|
196 against the beginning of the line until a match is found.
|
|
197 Matching is done case-sensitively. The corresponding SYM is
|
|
198 added to the list, point is moved to (match-end 0) and the
|
|
199 process is repeated. The process ends when there is no REGEXP in
|
|
200 the table that matches what is at point.")
|
|
201
|
|
202 (defvar filladapt-not-token-table
|
|
203 '(
|
|
204 "[Ee].g."
|
|
205 "[Ii].e."
|
|
206 ;; end-of-line isn't a token if whole line is empty
|
|
207 "^$"
|
|
208 )
|
|
209 "List of regexps that can never be a token.
|
|
210 Before trying the regular expressions in filladapt-token-table,
|
|
211 the regexps in this list are tried. If any regexp in this list
|
|
212 matches what is at point then the token generator gives up and
|
|
213 doesn't try any of the regexps in filladapt-token-table.
|
|
214
|
|
215 Regexp matching is done case-sensitively.")
|
|
216
|
|
217 (defvar filladapt-token-match-table
|
|
218 '(
|
|
219 (citation-> citation->)
|
|
220 (supercite-citation supercite-citation)
|
|
221 (lisp-comment lisp-comment)
|
|
222 (sh-comment sh-comment)
|
|
223 (postscript-comment postscript-comment)
|
|
224 (c++-comment c++-comment)
|
|
225 (texinfo-comment texinfo-comment)
|
|
226 (bullet)
|
|
227 (space bullet space)
|
108
|
228 (beginning-of-line beginning-of-line)
|
0
|
229 )
|
|
230 "Table describing what tokens a certain token will match.
|
|
231
|
|
232 To decide whether a line belongs in the current paragraph,
|
|
233 filladapt creates a token list for the fill prefix of both lines.
|
|
234 Tokens and the columns where tokens end are compared. This table
|
|
235 specifies what a certain token will match.
|
|
236
|
|
237 Table format is
|
|
238
|
|
239 (SYM [SYM1 [SYM2 ...]])
|
|
240
|
|
241 The first symbol SYM is the token, subsequent symbols are the
|
|
242 tokens that SYM will match.")
|
|
243
|
|
244 (defvar filladapt-token-match-many-table
|
|
245 '(
|
|
246 space
|
|
247 )
|
|
248 "List of tokens that can match multiple tokens.
|
|
249 If one of these tokens appears in a token list, it will eat all
|
|
250 matching tokens in a token list being matched against it until it
|
|
251 encounters a token that doesn't match or a token that ends on
|
|
252 a greater column number.")
|
|
253
|
|
254 (defvar filladapt-token-paragraph-start-table
|
|
255 '(
|
|
256 bullet
|
|
257 )
|
|
258 "List of tokens that indicate the start of a paragraph.
|
|
259 If parsing a line generates a token list containing one of
|
|
260 these tokens, then the line is considered to be the start of a
|
|
261 paragraph.")
|
|
262
|
|
263 (defvar filladapt-token-conversion-table
|
|
264 '(
|
|
265 (citation-> . exact)
|
|
266 (supercite-citation . exact)
|
|
267 (lisp-comment . exact)
|
|
268 (sh-comment . exact)
|
|
269 (postscript-comment . exact)
|
|
270 (c++-comment . exact)
|
|
271 (texinfo-comment . exact)
|
|
272 (bullet . spaces)
|
|
273 (space . exact)
|
|
274 (end-of-line . exact)
|
|
275 )
|
|
276 "Table that specifies how to convert a token into a fill prefix.
|
|
277 Table format is
|
|
278
|
|
279 ((SYM . HOWTO) ...)
|
|
280
|
|
281 SYM is the symbol naming the token to be converted.
|
|
282 HOWTO specifies how to do the conversion.
|
|
283 `exact' means copy the token's string directly into the fill prefix.
|
|
284 `spaces' means convert all characters in the token string that are
|
|
285 not a TAB or a space into spaces and copy the resulting string into
|
|
286 the fill prefix.")
|
|
287
|
|
288 (defvar filladapt-function-table
|
|
289 (let ((assoc-list
|
|
290 (list (cons 'fill-paragraph (symbol-function 'fill-paragraph))
|
108
|
291 (cons 'fill-region (symbol-function 'fill-region))
|
0
|
292 (cons 'fill-region-as-paragraph
|
|
293 (symbol-function 'fill-region-as-paragraph))
|
|
294 (cons 'do-auto-fill (symbol-function 'do-auto-fill)))))
|
|
295 ;; v18 Emacs doesn't have lisp-fill-paragraph
|
|
296 (if (fboundp 'lisp-fill-paragraph)
|
|
297 (nconc assoc-list
|
|
298 (list (cons 'lisp-fill-paragraph
|
|
299 (symbol-function 'lisp-fill-paragraph)))))
|
|
300 assoc-list )
|
|
301 "Table containing the old function definitions that filladapt usurps.")
|
|
302
|
|
303 (defvar filladapt-fill-paragraph-post-hook nil
|
|
304 "Hooks run after filladapt runs fill-paragraph.")
|
|
305
|
|
306 (defvar filladapt-inside-filladapt nil
|
|
307 "Non-nil if the filladapt version of a fill function executing.
|
|
308 Currently this is only checked by the filladapt version of
|
|
309 fill-region-as-paragraph to avoid this infinite recursion:
|
|
310
|
|
311 fill-region-as-paragraph -> fill-paragraph -> fill-region-as-paragraph ...")
|
|
312
|
|
313 (defvar filladapt-debug nil
|
|
314 "Non-nil means filladapt debugging is enabled.
|
|
315 Use the filladapt-debug command to turn on debugging.
|
|
316
|
|
317 With debugging enabled, filladapt will
|
|
318
|
|
319 a. display the proposed indentation with the tokens highlighted
|
|
320 using filladapt-debug-indentation-face-1 and
|
|
321 filladapt-debug-indentation-face-2.
|
|
322 b. display the current paragraph using the face specified by
|
|
323 filladapt-debug-paragraph-face.")
|
|
324
|
|
325 (if filladapt-debug
|
|
326 (add-hook 'post-command-hook 'filladapt-display-debug-info-maybe))
|
|
327
|
|
328 (defvar filladapt-debug-indentation-face-1 'highlight
|
|
329 "Face used to display the indentation when debugging is enabled.")
|
|
330
|
|
331 (defvar filladapt-debug-indentation-face-2 'secondary-selection
|
|
332 "Another face used to display the indentation when debugging is enabled.")
|
|
333
|
|
334 (defvar filladapt-debug-paragraph-face 'bold
|
|
335 "Face used to display the current paragraph when debugging is enabled.")
|
|
336
|
|
337 (defvar filladapt-debug-indentation-extents nil)
|
|
338 (make-variable-buffer-local 'filladapt-debug-indentation-extents)
|
|
339 (defvar filladapt-debug-paragraph-extent nil)
|
|
340 (make-variable-buffer-local 'filladapt-debug-paragraph-extent)
|
|
341
|
|
342 ;; kludge city, see references in code.
|
|
343 (defvar filladapt-old-line-prefix)
|
|
344
|
|
345 (defun do-auto-fill ()
|
|
346 (catch 'done
|
|
347 (if (and filladapt-mode (null fill-prefix))
|
|
348 (save-restriction
|
|
349 (let ((paragraph-ignore-fill-prefix nil)
|
|
350 ;; if the user wanted this stuff, they probably
|
|
351 ;; wouldn't be using filladapt-mode.
|
|
352 (adaptive-fill-mode nil)
|
|
353 (adaptive-fill-regexp nil)
|
|
354 ;; need this or Emacs 19 ignores fill-prefix when
|
|
355 ;; inside a comment.
|
|
356 (comment-multi-line t)
|
|
357 (filladapt-inside-filladapt t)
|
|
358 fill-prefix retval)
|
|
359 (if (filladapt-adapt nil nil)
|
|
360 (progn
|
|
361 (setq retval (filladapt-funcall 'do-auto-fill))
|
|
362 (throw 'done retval))))))
|
|
363 (filladapt-funcall 'do-auto-fill)))
|
|
364
|
|
365 (defun filladapt-fill-paragraph (function arg)
|
|
366 (catch 'done
|
|
367 (if (and filladapt-mode (null fill-prefix))
|
|
368 (save-restriction
|
|
369 (let ((paragraph-ignore-fill-prefix nil)
|
|
370 ;; if the user wanted this stuff, they probably
|
|
371 ;; wouldn't be using filladapt-mode.
|
|
372 (adaptive-fill-mode nil)
|
|
373 (adaptive-fill-regexp nil)
|
|
374 ;; need this or Emacs 19 ignores fill-prefix when
|
|
375 ;; inside a comment.
|
|
376 (comment-multi-line t)
|
|
377 fill-prefix retval)
|
|
378 (if (filladapt-adapt t nil)
|
|
379 (progn
|
108
|
380 (if filladapt-fill-column-tolerance
|
|
381 (let* ((low (- fill-column
|
|
382 filladapt-fill-column-backward-fuzz))
|
|
383 (high (+ fill-column
|
|
384 filladapt-fill-column-forward-fuzz))
|
|
385 (old-fill-column fill-column)
|
|
386 (fill-column fill-column)
|
|
387 (lim (- high low))
|
|
388 (done nil)
|
|
389 (sign 1)
|
|
390 (delta 0))
|
|
391 (while (not done)
|
|
392 (setq retval (filladapt-funcall function arg))
|
|
393 (if (filladapt-paragraph-within-fill-tolerance)
|
|
394 (setq done 'success)
|
|
395 (setq delta (1+ delta)
|
|
396 sign (* sign -1)
|
|
397 fill-column (+ fill-column (* delta sign)))
|
|
398 (while (and (<= delta lim)
|
|
399 (or (< fill-column low)
|
|
400 (> fill-column high)))
|
|
401 (setq delta (1+ delta)
|
|
402 sign (* sign -1)
|
|
403 fill-column (+ fill-column
|
|
404 (* delta sign))))
|
|
405 (setq done (> delta lim))))
|
|
406 ;; if the paragraph lines never fell
|
|
407 ;; within the tolerances, refill using
|
|
408 ;; the old fill-column.
|
|
409 (if (not (eq done 'success))
|
|
410 (let ((fill-column old-fill-column))
|
|
411 (setq retval (filladapt-funcall function arg)))))
|
|
412 (setq retval (filladapt-funcall function arg)))
|
0
|
413 (run-hooks 'filladapt-fill-paragraph-post-hook)
|
|
414 (throw 'done retval))))))
|
|
415 ;; filladapt-adapt failed, so do fill-paragraph normally.
|
|
416 (filladapt-funcall function arg)))
|
|
417
|
|
418 (defun fill-paragraph (arg)
|
108
|
419 "Fill paragraph at or after point. Prefix arg means justify as well.
|
|
420
|
|
421 (This function has been overloaded with the `filladapt' version.)
|
|
422
|
|
423 If `sentence-end-double-space' is non-nil, then period followed by one
|
|
424 space does not end a sentence, so don't break a line there.
|
|
425
|
|
426 If `fill-paragraph-function' is non-nil, we call it (passing our
|
|
427 argument to it), and if it returns non-nil, we simply return its value."
|
0
|
428 (interactive "*P")
|
|
429 (let ((filladapt-inside-filladapt t))
|
|
430 (filladapt-fill-paragraph 'fill-paragraph arg)))
|
|
431
|
|
432 (defun lisp-fill-paragraph (&optional arg)
|
108
|
433 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
|
|
434
|
|
435 (This function has been overloaded with the `filladapt' version.)
|
|
436
|
|
437 If any of the current line is a comment, fill the comment or the
|
|
438 paragraph of it that point is in, preserving the comment's indentation
|
|
439 and initial semicolons."
|
0
|
440 (interactive "*P")
|
|
441 (let ((filladapt-inside-filladapt t))
|
|
442 (filladapt-fill-paragraph 'lisp-fill-paragraph arg)))
|
|
443
|
108
|
444 (defun fill-region-as-paragraph (beg end &optional justify
|
|
445 nosqueeze squeeze-after)
|
|
446 "Fill the region as one paragraph.
|
|
447
|
|
448 (This function has been overloaded with the `filladapt' version.)
|
|
449
|
|
450 It removes any paragraph breaks in the region and extra newlines at the end,
|
|
451 indents and fills lines between the margins given by the
|
|
452 `current-left-margin' and `current-fill-column' functions.
|
|
453 It leaves point at the beginning of the line following the paragraph.
|
|
454
|
|
455 Normally performs justification according to the `current-justification'
|
|
456 function, but with a prefix arg, does full justification instead.
|
|
457
|
|
458 From a program, optional third arg JUSTIFY can specify any type of
|
|
459 justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
|
|
460 between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
|
|
461 means don't canonicalize spaces before that position.
|
|
462
|
|
463 If `sentence-end-double-space' is non-nil, then period followed by one
|
|
464 space does not end a sentence, so don't break a line there."
|
0
|
465 (interactive "*r\nP")
|
|
466 (if (and filladapt-mode (not filladapt-inside-filladapt))
|
|
467 (save-restriction
|
|
468 (narrow-to-region beg end)
|
|
469 (let ((filladapt-inside-filladapt t)
|
|
470 line-start last-token)
|
|
471 (goto-char beg)
|
108
|
472 (while (equal (char-after (point)) ?\n)
|
|
473 (delete-char 1))
|
0
|
474 (end-of-line)
|
|
475 (while (zerop (forward-line))
|
|
476 (if (setq last-token
|
|
477 (car (filladapt-tail (filladapt-parse-prefixes))))
|
|
478 (progn
|
|
479 (setq line-start (point))
|
|
480 (move-to-column (nth 1 last-token))
|
|
481 (delete-region line-start (point))))
|
|
482 ;; Dance...
|
|
483 ;;
|
|
484 ;; Do this instead of (delete-char -1) to keep
|
|
485 ;; markers on the correct side of the whitespace.
|
|
486 (goto-char (1- (point)))
|
|
487 (insert " ")
|
|
488 (delete-char 1)
|
|
489
|
|
490 (end-of-line))
|
|
491 (goto-char beg)
|
|
492 (fill-paragraph justify))
|
|
493 ;; In XEmacs 19.12 and Emacs 18.59 fill-region relies on
|
|
494 ;; fill-region-as-paragraph to do this. If we don't do
|
|
495 ;; it, fill-region will spin in an endless loop.
|
|
496 (goto-char (point-max)))
|
|
497 (condition-case nil
|
|
498 ;; five args for Emacs 19.31
|
|
499 (filladapt-funcall 'fill-region-as-paragraph beg end
|
|
500 justify nosqueeze squeeze-after)
|
|
501 (wrong-number-of-arguments
|
|
502 (condition-case nil
|
|
503 ;; four args for Emacs 19.29
|
|
504 (filladapt-funcall 'fill-region-as-paragraph beg end
|
|
505 justify nosqueeze)
|
108
|
506 ;; three args for the rest of the world.
|
|
507 (wrong-number-of-arguments
|
|
508 (filladapt-funcall 'fill-region-as-paragraph beg end justify)))))))
|
|
509
|
|
510 (defun fill-region (beg end &optional justify nosqueeze to-eop)
|
|
511 "Fill each of the paragraphs in the region.
|
|
512
|
|
513 (This function has been overloaded with the `filladapt' version.)
|
|
514
|
|
515 Prefix arg (non-nil third arg, if called from program) means justify as well.
|
|
516
|
|
517 Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
|
|
518 whitespace other than line breaks untouched, and fifth arg TO-EOP
|
|
519 non-nil means to keep filling to the end of the paragraph (or next
|
|
520 hard newline, if `use-hard-newlines' is on).
|
|
521
|
|
522 If `sentence-end-double-space' is non-nil, then period followed by one
|
|
523 space does not end a sentence, so don't break a line there."
|
|
524 (interactive "*r\nP")
|
|
525 (if (and filladapt-mode (not filladapt-inside-filladapt))
|
|
526 (save-restriction
|
|
527 (narrow-to-region beg end)
|
|
528 (let ((filladapt-inside-filladapt t)
|
|
529 start)
|
|
530 (goto-char beg)
|
|
531 (while (not (eobp))
|
|
532 (setq start (point))
|
|
533 (while (and (not (eobp)) (not (filladapt-parse-prefixes)))
|
|
534 (forward-line 1))
|
|
535 (if (not (equal start (point)))
|
|
536 (progn
|
|
537 (save-restriction
|
|
538 (narrow-to-region start (point))
|
|
539 (fill-region start (point) justify nosqueeze to-eop)
|
|
540 (goto-char (point-max)))
|
|
541 (if (and (not (bolp)) (not (eobp)))
|
|
542 (forward-line 1))))
|
|
543 (if (filladapt-parse-prefixes)
|
|
544 (progn
|
|
545 (save-restriction
|
|
546 ;; for the clipping region
|
|
547 (filladapt-adapt t t)
|
|
548 (fill-paragraph justify)
|
|
549 (goto-char (point-max)))
|
|
550 (if (and (not (bolp)) (not (eobp)))
|
|
551 (forward-line 1)))))))
|
|
552 (condition-case nil
|
|
553 (filladapt-funcall 'fill-region beg end justify nosqueeze to-eop)
|
0
|
554 (wrong-number-of-arguments
|
108
|
555 (condition-case nil
|
|
556 (filladapt-funcall 'fill-region beg end justify nosqueeze)
|
|
557 (wrong-number-of-arguments
|
|
558 (filladapt-funcall 'fill-region beg end justify)))))))
|
0
|
559
|
|
560 (defvar zmacs-region-stays) ; for XEmacs
|
|
561
|
|
562 (defun filladapt-mode (&optional arg)
|
|
563 "Toggle Filladapt minor mode.
|
|
564 With arg, turn Filladapt mode on iff arg is positive. When
|
|
565 Filladapt mode is enabled, auto-fill-mode and the fill-paragraph
|
|
566 command are both smarter about guessing a proper fill-prefix and
|
|
567 finding paragraph boundaries when bulleted and indented lines and
|
|
568 paragraphs are used."
|
|
569 (interactive "P")
|
|
570 ;; don't deactivate the region.
|
|
571 (setq zmacs-region-stays t)
|
|
572 (setq filladapt-mode (or (and arg (> (prefix-numeric-value arg) 0))
|
|
573 (and (null arg) (null filladapt-mode))))
|
|
574 (if (fboundp 'force-mode-line-update)
|
|
575 (force-mode-line-update)
|
|
576 (set-buffer-modified-p (buffer-modified-p))))
|
|
577
|
|
578 (defun turn-on-filladapt-mode ()
|
|
579 "Unconditionally turn on Filladapt mode in the current buffer."
|
|
580 (filladapt-mode 1))
|
|
581
|
|
582 (defun turn-off-filladapt-mode ()
|
|
583 "Unconditionally turn off Filladapt mode in the current buffer."
|
|
584 (filladapt-mode -1))
|
|
585
|
|
586 (defun filladapt-funcall (function &rest args)
|
|
587 "Call the old definition of a function that filladapt has usurped."
|
|
588 (apply (cdr (assoc function filladapt-function-table)) args))
|
|
589
|
|
590 (defun filladapt-paragraph-start (list)
|
|
591 "Returns non-nil if LIST contains a paragraph starting token.
|
|
592 LIST should be a token list as returned by filladapt-parse-prefixes."
|
|
593 (catch 'done
|
|
594 (while list
|
|
595 (if (memq (car (car list)) filladapt-token-paragraph-start-table)
|
|
596 (throw 'done t))
|
|
597 (setq list (cdr list)))))
|
|
598
|
|
599 (defun filladapt-parse-prefixes ()
|
|
600 "Parse all the tokens after point and return a list of them.
|
|
601 The tokens regular expressions are specified in
|
|
602 filladapt-token-table. The list returned is of this form
|
|
603
|
|
604 ((SYM COL STRING) ...)
|
|
605
|
|
606 SYM is a token symbol as found in filladapt-token-table.
|
|
607 COL is the column at which the token ended.
|
|
608 STRING is the token's text."
|
|
609 (save-excursion
|
|
610 (let ((token-list nil)
|
|
611 (done nil)
|
|
612 (old-point (point))
|
|
613 (case-fold-search nil)
|
108
|
614 token-table not-token-table moved)
|
0
|
615 (catch 'done
|
|
616 (while (not done)
|
|
617 (setq not-token-table filladapt-not-token-table)
|
|
618 (while not-token-table
|
|
619 (if (looking-at (car not-token-table))
|
|
620 (throw 'done t))
|
|
621 (setq not-token-table (cdr not-token-table)))
|
|
622 (setq token-table filladapt-token-table
|
|
623 done t)
|
|
624 (while token-table
|
|
625 (if (null (looking-at (car (car token-table))))
|
|
626 (setq token-table (cdr token-table))
|
|
627 (goto-char (match-end 0))
|
108
|
628 (setq token-list (cons (list (nth 1 (car token-table))
|
0
|
629 (current-column)
|
|
630 (buffer-substring
|
|
631 (match-beginning 0)
|
|
632 (match-end 0)))
|
|
633 token-list)
|
108
|
634 moved (not (eq (point) old-point))
|
|
635 token-table (if moved nil (cdr token-table))
|
|
636 done (not moved)
|
0
|
637 old-point (point))))))
|
|
638 (nreverse token-list))))
|
|
639
|
|
640 (defun filladapt-tokens-match-p (list1 list2)
|
|
641 "Compare two token lists and return non-nil if they match, nil otherwise.
|
|
642 The lists are walked through in lockstep, comparing tokens.
|
|
643
|
|
644 When two tokens A and B are compared, they are considered to
|
|
645 match if
|
|
646
|
|
647 1. A appears in B's list of matching tokens or
|
|
648 B appears in A's list of matching tokens
|
|
649 and
|
|
650 2. A and B both end at the same column
|
|
651 or
|
|
652 A can match multiple tokens and ends at a column > than B
|
|
653 or
|
|
654 B can match multiple tokens and ends at a column > than A
|
|
655
|
|
656 In the case where the end columns differ the list pointer for the
|
|
657 token with the greater end column is not moved forward, which
|
|
658 allows its current token to be matched against the next token in
|
|
659 the other list in the next iteration of the matching loop.
|
|
660
|
|
661 All tokens must be matched in order for the lists to be considered
|
|
662 matching."
|
|
663 (let ((matched t)
|
|
664 (done nil))
|
|
665 (while (and (not done) list1 list2)
|
|
666 (let* ((token1 (car (car list1)))
|
|
667 (token1-matches-many-p
|
|
668 (memq token1 filladapt-token-match-many-table))
|
|
669 (token1-matches (cdr (assq token1 filladapt-token-match-table)))
|
|
670 (token1-endcol (nth 1 (car list1)))
|
|
671 (token2 (car (car list2)))
|
|
672 (token2-matches-many-p
|
|
673 (memq token2 filladapt-token-match-many-table))
|
|
674 (token2-matches (cdr (assq token2 filladapt-token-match-table)))
|
|
675 (token2-endcol (nth 1 (car list2)))
|
|
676 (tokens-match (or (memq token1 token2-matches)
|
|
677 (memq token2 token1-matches))))
|
|
678 (cond ((not tokens-match)
|
|
679 (setq matched nil
|
|
680 done t))
|
|
681 ((and token1-matches-many-p token2-matches-many-p)
|
|
682 (cond ((= token1-endcol token2-endcol)
|
|
683 (setq list1 (cdr list1)
|
|
684 list2 (cdr list2)))
|
|
685 ((< token1-endcol token2-endcol)
|
|
686 (setq list1 (cdr list1)))
|
|
687 (t
|
|
688 (setq list2 (cdr list2)))))
|
|
689 (token1-matches-many-p
|
|
690 (cond ((= token1-endcol token2-endcol)
|
|
691 (setq list1 (cdr list1)
|
|
692 list2 (cdr list2)))
|
|
693 ((< token1-endcol token2-endcol)
|
|
694 (setq matched nil
|
|
695 done t))
|
|
696 (t
|
|
697 (setq list2 (cdr list2)))))
|
|
698 (token2-matches-many-p
|
|
699 (cond ((= token1-endcol token2-endcol)
|
|
700 (setq list1 (cdr list1)
|
|
701 list2 (cdr list2)))
|
|
702 ((< token2-endcol token1-endcol)
|
|
703 (setq matched nil
|
|
704 done t))
|
|
705 (t
|
|
706 (setq list1 (cdr list1)))))
|
|
707 ((= token1-endcol token2-endcol)
|
|
708 (setq list1 (cdr list1)
|
|
709 list2 (cdr list2)))
|
|
710 (t
|
|
711 (setq matched nil
|
|
712 done t)))))
|
|
713 (and matched (null list1) (null list2)) ))
|
|
714
|
|
715 (defun filladapt-make-fill-prefix (list)
|
|
716 "Build a fill-prefix for a token LIST.
|
|
717 filladapt-token-conversion-table specifies how this is done."
|
|
718 (let ((prefix-list nil)
|
|
719 (conversion-spec nil))
|
|
720 (while list
|
|
721 (setq conversion-spec (cdr (assq (car (car list))
|
|
722 filladapt-token-conversion-table)))
|
|
723 (cond ((eq conversion-spec 'spaces)
|
|
724 (setq prefix-list
|
|
725 (cons
|
|
726 (filladapt-convert-to-spaces (nth 2 (car list)))
|
|
727 prefix-list)))
|
|
728 ((eq conversion-spec 'exact)
|
|
729 (setq prefix-list
|
|
730 (cons
|
|
731 (nth 2 (car list))
|
|
732 prefix-list))))
|
|
733 (setq list (cdr list)))
|
|
734 (apply (function concat) (nreverse prefix-list)) ))
|
|
735
|
108
|
736 (defun filladapt-paragraph-within-fill-tolerance ()
|
|
737 (catch 'done
|
|
738 (save-excursion
|
|
739 (let ((low (- fill-column filladapt-fill-column-tolerance))
|
|
740 (shortline nil))
|
|
741 (goto-char (point-min))
|
|
742 (while (not (eobp))
|
|
743 (if shortline
|
|
744 (throw 'done nil)
|
|
745 (end-of-line)
|
|
746 (setq shortline (< (current-column) low))
|
|
747 (forward-line 1)))
|
|
748 t ))))
|
|
749
|
0
|
750 (defun filladapt-convert-to-spaces (string)
|
|
751 "Return a copy of STRING, with all non-tabs and non-space changed to spaces."
|
|
752 (let ((i 0)
|
|
753 (space-list '(?\ ?\t))
|
|
754 (space ?\ )
|
|
755 (lim (length string)))
|
|
756 (setq string (copy-sequence string))
|
|
757 (while (< i lim)
|
|
758 (if (not (memq (aref string i) space-list))
|
|
759 (aset string i space))
|
|
760 (setq i (1+ i)))
|
|
761 string ))
|
|
762
|
|
763 (defun filladapt-adapt (paragraph debugging)
|
|
764 "Set fill-prefix based on the contents of the current line.
|
|
765
|
|
766 If the first arg PARAGRAPH is non-nil, also set a clipping region
|
|
767 around the current paragraph.
|
|
768
|
|
769 If the second arg DEBUGGING is non-nil, don't do the kludge that's
|
|
770 necessary to make certain paragraph fills work properly."
|
|
771 (save-excursion
|
|
772 (beginning-of-line)
|
|
773 (let ((token-list (filladapt-parse-prefixes))
|
|
774 curr-list done)
|
|
775 (if (null token-list)
|
|
776 nil
|
|
777 (setq fill-prefix (filladapt-make-fill-prefix token-list))
|
|
778 (if paragraph
|
|
779 (let (beg end)
|
|
780 (if (filladapt-paragraph-start token-list)
|
|
781 (setq beg (point))
|
|
782 (save-excursion
|
|
783 (setq done nil)
|
|
784 (while (not done)
|
|
785 (cond ((not (= 0 (forward-line -1)))
|
|
786 (setq done t
|
|
787 beg (point)))
|
|
788 ((not (filladapt-tokens-match-p
|
|
789 token-list
|
|
790 (setq curr-list (filladapt-parse-prefixes))))
|
|
791 (forward-line 1)
|
|
792 (setq done t
|
|
793 beg (point)))
|
|
794 ((filladapt-paragraph-start curr-list)
|
|
795 (setq done t
|
|
796 beg (point)))))))
|
|
797 (save-excursion
|
|
798 (setq done nil)
|
|
799 (while (not done)
|
|
800 (cond ((not (= 0 (progn (end-of-line) (forward-line 1))))
|
|
801 (setq done t
|
|
802 end (point)))
|
|
803 ((not (filladapt-tokens-match-p
|
|
804 token-list
|
|
805 (setq curr-list (filladapt-parse-prefixes))))
|
|
806 (setq done t
|
|
807 end (point)))
|
|
808 ((filladapt-paragraph-start curr-list)
|
|
809 (setq done t
|
|
810 end (point))))))
|
|
811 (narrow-to-region beg end)
|
|
812 ;; Multiple spaces after the bullet at the start of
|
|
813 ;; a hanging list paragraph get squashed by
|
|
814 ;; fill-paragraph. We kludge around this by
|
|
815 ;; replacing the line prefix with the fill-prefix
|
|
816 ;; used by the rest of the lines in the paragraph.
|
|
817 ;; fill-paragraph will not alter the fill prefix so
|
|
818 ;; we win. The post hook restores the old line prefix
|
|
819 ;; after fill-paragraph has been called.
|
|
820 (if (and paragraph (not debugging))
|
|
821 (let (col)
|
|
822 (setq col (nth 1 (car (filladapt-tail token-list))))
|
|
823 (goto-char (point-min))
|
|
824 (move-to-column col)
|
|
825 (setq filladapt-old-line-prefix
|
|
826 (buffer-substring (point-min) (point)))
|
|
827 (delete-region (point-min) (point))
|
|
828 (insert fill-prefix)
|
|
829 (add-hook 'filladapt-fill-paragraph-post-hook
|
|
830 'filladapt-cleanup-kludge-at-point-min)))))
|
|
831 t ))))
|
|
832
|
|
833 (defun filladapt-cleanup-kludge-at-point-min ()
|
|
834 "Cleanup the paragraph fill kludge.
|
|
835 See filladapt-adapt."
|
|
836 (save-excursion
|
|
837 (goto-char (point-min))
|
|
838 (insert filladapt-old-line-prefix)
|
|
839 (delete-char (length fill-prefix))
|
|
840 (remove-hook 'filladapt-fill-paragraph-post-hook
|
|
841 'filladapt-cleanup-kludge-at-point-min)))
|
|
842
|
|
843 (defun filladapt-tail (list)
|
|
844 "Returns the last cons in LIST."
|
|
845 (if (null list)
|
|
846 nil
|
|
847 (while (consp (cdr list))
|
|
848 (setq list (cdr list)))
|
|
849 list ))
|
|
850
|
|
851 (defun filladapt-delete-extent (e)
|
|
852 (if (fboundp 'delete-extent)
|
|
853 (delete-extent e)
|
|
854 (delete-overlay e)))
|
|
855
|
|
856 (defun filladapt-make-extent (beg end)
|
|
857 (if (fboundp 'make-extent)
|
|
858 (make-extent beg end)
|
|
859 (make-overlay beg end)))
|
|
860
|
|
861 (defun filladapt-set-extent-endpoints (e beg end)
|
|
862 (if (fboundp 'set-extent-endpoints)
|
|
863 (set-extent-endpoints e beg end)
|
|
864 (move-overlay e beg end)))
|
|
865
|
|
866 (defun filladapt-set-extent-property (e prop val)
|
|
867 (if (fboundp 'set-extent-property)
|
|
868 (set-extent-property e prop val)
|
|
869 (overlay-put e prop val)))
|
|
870
|
|
871 (defun filladapt-debug ()
|
|
872 "Toggle filladapt debugging on/off in the current buffer."
|
|
873 ;; (interactive)
|
|
874 (make-local-variable 'filladapt-debug)
|
|
875 (setq filladapt-debug (not filladapt-debug))
|
|
876 (if (null filladapt-debug)
|
|
877 (progn
|
|
878 (mapcar (function (lambda (e) (filladapt-set-extent-endpoints e 1 1)))
|
|
879 filladapt-debug-indentation-extents)
|
|
880 (if filladapt-debug-paragraph-extent
|
|
881 (progn
|
|
882 (filladapt-delete-extent filladapt-debug-paragraph-extent)
|
|
883 (setq filladapt-debug-paragraph-extent nil)))))
|
|
884 (add-hook 'post-command-hook 'filladapt-display-debug-info-maybe))
|
|
885
|
|
886 (defun filladapt-display-debug-info-maybe ()
|
|
887 (cond ((null filladapt-debug) nil)
|
|
888 (fill-prefix nil)
|
|
889 (t
|
|
890 (if (null filladapt-debug-paragraph-extent)
|
|
891 (let ((e (filladapt-make-extent 1 1)))
|
|
892 (filladapt-set-extent-property e 'detachable nil)
|
|
893 (filladapt-set-extent-property e 'evaporate nil)
|
|
894 (filladapt-set-extent-property e 'face
|
|
895 filladapt-debug-paragraph-face)
|
|
896 (setq filladapt-debug-paragraph-extent e)))
|
|
897 (save-excursion
|
|
898 (save-restriction
|
|
899 (let ((ei-list filladapt-debug-indentation-extents)
|
|
900 (ep filladapt-debug-paragraph-extent)
|
|
901 (face filladapt-debug-indentation-face-1)
|
|
902 fill-prefix token-list)
|
|
903 (if (null (filladapt-adapt t t))
|
|
904 (progn
|
|
905 (filladapt-set-extent-endpoints ep 1 1)
|
|
906 (while ei-list
|
|
907 (filladapt-set-extent-endpoints (car ei-list) 1 1)
|
|
908 (setq ei-list (cdr ei-list))))
|
|
909 (filladapt-set-extent-endpoints ep (point-min) (point-max))
|
|
910 (beginning-of-line)
|
|
911 (setq token-list (filladapt-parse-prefixes))
|
|
912 (message "(%s)" (mapconcat (function
|
|
913 (lambda (q) (symbol-name (car q))))
|
|
914 token-list
|
|
915 " "))
|
|
916 (while token-list
|
|
917 (if ei-list
|
|
918 (setq e (car ei-list)
|
|
919 ei-list (cdr ei-list))
|
|
920 (setq e (filladapt-make-extent 1 1))
|
|
921 (filladapt-set-extent-property e 'detachable nil)
|
|
922 (filladapt-set-extent-property e 'evaporate nil)
|
|
923 (setq filladapt-debug-indentation-extents
|
|
924 (cons e filladapt-debug-indentation-extents)))
|
|
925 (filladapt-set-extent-property e 'face face)
|
|
926 (filladapt-set-extent-endpoints e (point)
|
|
927 (progn
|
|
928 (move-to-column
|
|
929 (nth 1
|
|
930 (car token-list)))
|
|
931 (point)))
|
|
932 (if (eq face filladapt-debug-indentation-face-1)
|
|
933 (setq face filladapt-debug-indentation-face-2)
|
|
934 (setq face filladapt-debug-indentation-face-1))
|
|
935 (setq token-list (cdr token-list)))
|
|
936 (while ei-list
|
|
937 (filladapt-set-extent-endpoints (car ei-list) 1 1)
|
|
938 (setq ei-list (cdr ei-list))))))))))
|