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