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