Mercurial > emacs
annotate lisp/mail/mailabbrev.el @ 1150:2819233917c8
entered into RCS
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Tue, 15 Sep 1992 21:11:58 +0000 |
parents | b32ae4969b78 |
children | 7446ab662993 |
rev | line source |
---|---|
1148 | 1 ;;; mailabbrev.el --- abbrev-expansion of mail aliases. |
2 | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
1148 | 4 |
5 ;; Author: Jamie Zawinski <jwz@lucid.com> | |
6 ;; Maintainer: Jamie Zawinski <jwz@lucid.com> | |
7 ;; Created: 19 Oct 90 | |
8 ;; Keywords: mail | |
9 | |
459 | 10 ;;; This file is part of GNU Emacs. |
11 | |
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;;; it under the terms of the GNU General Public License as published by | |
717 | 14 ;;; the Free Software Foundation; either version 2, or (at your option) |
459 | 15 ;;; any later version. |
16 | |
17 ;;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;;; GNU General Public License for more details. | |
21 | |
22 ;;; You should have received a copy of the GNU General Public License | |
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to | |
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | |
1148 | 26 ;;; Commentary: |
27 | |
459 | 28 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From: |
29 ;;; field, word-abbrevs are defined for each of your mail aliases. These | |
30 ;;; aliases will be defined from your .mailrc file (or the file specified by | |
598 | 31 ;;; the MAILRC environment variable) if it exists. Your mail aliases will |
32 ;;; expand any time you type a word-delimiter at the end of an abbreviation. | |
459 | 33 ;;; |
34 ;;; What you see is what you get: no abbreviations will be expanded after you | |
35 ;;; have sent the mail, unlike the old system. This means you don't suffer | |
36 ;;; the annoyance of having the system do things behind your back -- if an | |
37 ;;; address you typed is going to be rewritten, you know it immediately, | |
38 ;;; instead of after the mail has been sent and it's too late to do anything | |
39 ;;; about it. You will never again be screwed because you forgot to delete an | |
40 ;;; old alias from your .mailrc when a new local user arrives and is given a | |
41 ;;; userid which conflicts with one of your aliases, for example. | |
42 ;;; | |
43 ;;; Your mail alias abbrevs will be in effect only when the point is in an | |
44 ;;; appropriate header field. When in the body of the message, or other | |
45 ;;; header fields, the mail aliases will not expand. Rather, the normal | |
46 ;;; mode-specific abbrev table (mail-mode-abbrev-table) will be used if | |
47 ;;; defined. So if you use mail-mode specific abbrevs, this code will not | |
48 ;;; adversely affect you. You can control which header fields the abbrevs | |
49 ;;; are used in by changing the variable mail-abbrev-mode-regexp. | |
50 ;;; | |
51 ;;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word | |
52 ;;; boundaries; also, header continuation-lines will be properly indented. | |
53 ;;; | |
54 ;;; You can also insert a mail alias with mail-interactive-insert-alias | |
55 ;;; (bound to C-c C-a), which prompts you for an alias (with completion) | |
56 ;;; and inserts its expansion at point. | |
57 ;;; | |
58 ;;; This file fixes a bug in the old system which prohibited your .mailrc | |
59 ;;; file from having lines like | |
60 ;;; | |
61 ;;; alias someone "John Doe <doe@quux.com>" | |
62 ;;; | |
63 ;;; That is, if you want an address to have embedded spaces, simply surround it | |
64 ;;; with double-quotes. This is necessary because the format of the .mailrc | |
65 ;;; file bogusly uses spaces as address delimiters. The following line defines | |
66 ;;; an alias which expands to three addresses: | |
67 ;;; | |
68 ;;; alias foobar addr-1 addr-2 "address three <addr-3>" | |
69 ;;; | |
70 ;;; (This is bogus because mail-delivery programs want commas, not spaces, | |
71 ;;; but that's what the file format is, so we have to live with it.) | |
72 ;;; | |
73 ;;; If you like, you can call the function define-mail-alias to define your | |
74 ;;; mail-aliases instead of using a .mailrc file. When you call it in this | |
627 | 75 ;;; way, addresses are separated by commas. |
459 | 76 ;;; |
77 ;;; CAVEAT: This works on most Sun systems; I have been told that some versions | |
78 ;;; of /bin/mail do not understand double-quotes in the .mailrc file. So you | |
79 ;;; should make sure your version does before including verbose addresses like | |
80 ;;; this. One solution to this, if you are on a system whose /bin/mail doesn't | |
81 ;;; work that way, (and you still want to be able to /bin/mail to send mail in | |
82 ;;; addition to emacs) is to define minimal aliases (without full names) in | |
83 ;;; your .mailrc file, and use define-mail-alias to redefine them when sending | |
84 ;;; mail from emacs; this way, mail sent from /bin/mail will work, and mail | |
85 ;;; sent from emacs will be pretty. | |
86 ;;; | |
87 ;;; Aliases in the mailrc file may be nested. If you define aliases like | |
88 ;;; alias group1 fred ethel | |
89 ;;; alias group2 larry curly moe | |
90 ;;; alias everybody group1 group2 | |
91 ;;; Then when you type "everybody" on the To: line, it will be expanded to | |
92 ;;; fred, ethyl, larry, curly, moe | |
93 ;;; | |
94 ;;; Aliases may also contain forward references; the alias of "everybody" can | |
95 ;;; preceed the aliases of "group1" and "group2". | |
96 ;;; | |
97 ;;; This code also understands the "source" .mailrc command, for reading | |
98 ;;; aliases from some other file as well. | |
99 ;;; | |
598 | 100 ;;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs |
101 ;;; normally cannot contain hyphens, but this code works around that for the | |
102 ;;; specific case of mail-alias word-abbrevs. | |
103 ;;; | |
459 | 104 ;;; To read in the contents of another .mailrc-type file from emacs, use the |
105 ;;; command Meta-X merge-mail-aliases. The rebuild-mail-aliases command is | |
106 ;;; similar, but will delete existing aliases first. | |
107 ;;; | |
598 | 108 ;;; If you would like your aliases to be expanded when you type M-> or ^N to |
109 ;;; move out of the mail-header into the message body (instead of having to | |
110 ;;; type SPC at the end of the abbrev before moving away) then you can do | |
111 ;;; | |
112 ;;; (define-key mail-mode-map "\C-n" 'abbrev-hacking-next-line) | |
113 ;;; (define-key mail-mode-map "\M->" 'abbrev-hacking-end-of-buffer) | |
114 ;;; | |
627 | 115 ;;; If you want multiple addresses separated by a string other than ", " then |
116 ;;; you can set the variable mail-alias-separator-string to it. This has to | |
459 | 117 ;;; be a comma bracketed by whitespace if you want any kind of reasonable |
118 ;;; behaviour. | |
119 ;;; | |
120 ;;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and | |
121 ;;; Noah Friedman for suggestions and bug reports. | |
627 | 122 ;;; |
123 ;;; INSTALLATION | |
124 ;;; | |
125 ;;; If you are using Emacs 18, you shouldn't have to do anything at all to | |
126 ;;; install this code other than load this file. You might want to do this | |
127 ;;; to have this code loaded only when needed: | |
128 ;;; | |
129 ;;; (setq mail-setup-hook '(lambda () (require 'mail-abbrevs))) | |
130 ;;; | |
131 ;;; Simply loading this file will redefine and overload the required | |
132 ;;; functions. | |
133 ;;; | |
134 ;;; If you want to install this code more permanently (instead of loading | |
135 ;;; it as a patch) you need to do the following: | |
136 ;;; | |
137 ;;; - Remove the entire file mailalias.el; | |
138 ;;; - Remove the definition of mail-aliases from sendmail.el; | |
139 ;;; - Add a call to mail-aliases-setup to the front of the function | |
140 ;;; mail-setup in the file sendmail.el; | |
141 ;;; - Remove the call to expand-mail-aliases from the function | |
142 ;;; sendmail-send-it in the file sendmail.el; | |
143 ;;; - Remove the autoload of expand-mail-aliases from the file sendmail.el; | |
144 ;;; - Remove the autoload of build-mail-aliases from the file sendmail.el; | |
145 ;;; - Add an autoload of define-mail-alias to loaddefs.el. | |
459 | 146 |
1148 | 147 ;;; Code: |
148 | |
459 | 149 (require 'sendmail) |
150 | |
151 (defvar mail-abbrev-mailrc-file nil | |
152 "Name of file with mail aliases. If nil, ~/.mailrc is used.") | |
153 | |
154 (defmacro mail-abbrev-mailrc-file () | |
155 '(or mail-abbrev-mailrc-file | |
156 (setq mail-abbrev-mailrc-file | |
157 (or (getenv "MAILRC") "~/.mailrc")))) | |
158 | |
159 ;; originally defined in sendmail.el - used to be an alist, now is a table. | |
160 (defvar mail-aliases nil | |
598 | 161 "Word-abbrev table of mail address aliases. |
459 | 162 If this is nil, it means the aliases have not yet been initialized and |
163 should be read from the .mailrc file. (This is distinct from there being | |
164 no aliases, which is represented by this being a table with no entries.)") | |
165 | |
474 | 166 ;;;###autoload |
459 | 167 (defun mail-aliases-setup () |
168 (if (and (not (vectorp mail-aliases)) | |
169 (file-exists-p (mail-abbrev-mailrc-file))) | |
170 (build-mail-aliases)) | |
606 | 171 (make-local-variable 'pre-abbrev-expand-hook) |
172 (setq pre-abbrev-expand-hook | |
173 (cond ((and (listp pre-abbrev-expand-hook) | |
174 (not (eq 'lambda (car pre-abbrev-expand-hook)))) | |
175 (cons 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook)) | |
176 (t | |
177 (list 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook)))) | |
459 | 178 (abbrev-mode 1)) |
179 | |
180 ;;; Originally defined in mailalias.el. Changed to call define-mail-alias | |
181 ;;; with an additional argument. | |
598 | 182 ;;;###autoload |
459 | 183 (defun build-mail-aliases (&optional file recursivep) |
184 "Read mail aliases from .mailrc and set mail-aliases." | |
185 (setq file (expand-file-name (or file (mail-abbrev-mailrc-file)))) | |
186 (if (vectorp mail-aliases) | |
187 nil | |
188 (setq mail-aliases nil) | |
189 (define-abbrev-table 'mail-aliases '())) | |
606 | 190 (message "Parsing %s..." file) |
459 | 191 (let ((buffer nil) |
192 (obuf (current-buffer))) | |
193 (unwind-protect | |
194 (progn | |
195 (setq buffer (generate-new-buffer "mailrc")) | |
598 | 196 (buffer-disable-undo buffer) |
459 | 197 (set-buffer buffer) |
198 (cond ((get-file-buffer file) | |
199 (insert (save-excursion | |
200 (set-buffer (get-file-buffer file)) | |
201 (buffer-substring (point-min) (point-max))))) | |
202 ((not (file-exists-p file))) | |
203 (t (insert-file-contents file))) | |
204 ;; Don't lose if no final newline. | |
205 (goto-char (point-max)) | |
206 (or (eq (preceding-char) ?\n) (newline)) | |
207 (goto-char (point-min)) | |
208 ;; Delete comments from the file | |
209 (while (search-forward "# " nil t) | |
210 (let ((p (- (point) 2))) | |
211 (end-of-line) | |
212 (delete-region p (point)))) | |
213 (goto-char (point-min)) | |
214 ;; handle "\\\n" continuation lines | |
215 (while (not (eobp)) | |
216 (end-of-line) | |
217 (if (= (preceding-char) ?\\) | |
218 (progn (delete-char -1) (delete-char 1) (insert ?\ )) | |
219 (forward-char 1))) | |
220 (goto-char (point-min)) | |
221 (while (re-search-forward | |
907 | 222 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t) |
459 | 223 (beginning-of-line) |
224 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)") | |
225 (progn | |
226 (end-of-line) | |
227 (build-mail-aliases | |
228 (buffer-substring (match-beginning 1) (match-end 1)) t)) | |
229 (re-search-forward "[ \t]+\\([^ \t\n]+\\)") | |
230 (let* ((name (buffer-substring | |
231 (match-beginning 1) (match-end 1))) | |
232 (start (progn (skip-chars-forward " \t") (point)))) | |
233 (end-of-line) | |
234 ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1) | |
235 (define-mail-alias | |
236 name | |
237 (buffer-substring start (point)) | |
238 t)))) | |
239 ;; Resolve forward references in .mailrc file. | |
240 ;; This would happen automatically before the first abbrev was | |
241 ;; expanded, but why not do it now. | |
242 (or recursivep (mail-resolve-all-aliases)) | |
243 mail-aliases) | |
244 (if buffer (kill-buffer buffer)) | |
245 (set-buffer obuf))) | |
606 | 246 (message "Parsing %s... done" file)) |
459 | 247 |
627 | 248 (defvar mail-alias-separator-string ", " |
459 | 249 "*A string inserted between addresses in multi-address mail aliases. |
250 This has to contain a comma, so \", \" is a reasonable value. You might | |
251 also want something like \",\\n \" to get each address on its own line.") | |
252 | |
253 ;; define-mail-alias sets this flag, which causes mail-resolve-all-aliases | |
254 ;; to be called before expanding abbrevs if it's necessary. | |
255 (defvar mail-abbrev-aliases-need-to-be-resolved t) | |
256 | |
257 ;; originally defined in mailalias.el ; build-mail-aliases calls this with | |
258 ;; stuff parsed from the .mailrc file. | |
259 ;; | |
474 | 260 ;;;###autoload |
459 | 261 (defun define-mail-alias (name definition &optional from-mailrc-file) |
262 "Define NAME as a mail-alias that translates to DEFINITION. | |
627 | 263 If DEFINITION contains multiple addresses, separate them with commas." |
459 | 264 ;; When this is called from build-mail-aliases, the third argument is |
265 ;; true, and we do some evil space->comma hacking like /bin/mail does. | |
266 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ") | |
267 ;; Read the defaults first, if we have not done so. | |
268 (if (vectorp mail-aliases) | |
269 nil | |
270 (setq mail-aliases nil) | |
271 (define-abbrev-table 'mail-aliases '()) | |
272 (if (file-exists-p (mail-abbrev-mailrc-file)) | |
273 (build-mail-aliases))) | |
274 ;; strip garbage from front and end | |
275 (if (string-match "\\`[ \t\n,]+" definition) | |
276 (setq definition (substring definition (match-end 0)))) | |
277 (if (string-match "[ \t\n,]+\\'" definition) | |
278 (setq definition (substring definition 0 (match-beginning 0)))) | |
279 (let ((result '()) | |
280 (start 0) | |
281 (L (length definition)) | |
282 end) | |
283 (while start | |
284 ;; If we're reading from the mailrc file, then addresses are delimited | |
285 ;; by spaces, and addresses with embedded spaces must be surrounded by | |
627 | 286 ;; double-quotes. Otherwise, addresses are separated by commas. |
459 | 287 (if from-mailrc-file |
288 (if (eq ?\" (aref definition start)) | |
289 (setq start (1+ start) | |
290 end (string-match "\"[ \t,]*" definition start)) | |
291 (setq end (string-match "[ \t,]+" definition start))) | |
292 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) | |
293 (setq result (cons (substring definition start end) result)) | |
294 (setq start (and end | |
295 (/= (match-end 0) L) | |
296 (match-end 0)))) | |
297 (setq definition (mapconcat (function identity) | |
298 (nreverse result) | |
627 | 299 mail-alias-separator-string))) |
459 | 300 (setq mail-abbrev-aliases-need-to-be-resolved t) |
301 (setq name (downcase name)) | |
302 ;; use an abbrev table instead of an alist for mail-aliases. | |
303 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed. | |
304 (define-abbrev mail-aliases name definition 'mail-abbrev-expand-hook))) | |
305 | |
306 | |
307 (defun mail-resolve-all-aliases () | |
308 "Resolve all forward references in the mail aliases table." | |
309 (if mail-abbrev-aliases-need-to-be-resolved | |
310 (progn | |
311 ;; (message "Resolving mail aliases...") | |
312 (if (vectorp mail-aliases) | |
313 (mapatoms (function mail-resolve-all-aliases-1) mail-aliases)) | |
314 (setq mail-abbrev-aliases-need-to-be-resolved nil) | |
315 ;; (message "Resolving mail aliases... done.") | |
316 ))) | |
317 | |
717 | 318 (defun mail-resolve-all-aliases-1 (sym &optional so-far) |
319 (if (memq sym so-far) | |
320 (error "mail alias loop detected: %s" | |
321 (mapconcat 'symbol-name (cons sym so-far) " <- "))) | |
459 | 322 (let ((definition (and (boundp sym) (symbol-value sym)))) |
323 (if definition | |
324 (let ((result '()) | |
325 (start 0)) | |
326 (while start | |
327 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start))) | |
328 (setq result (cons (substring definition start end) result) | |
329 start (and end (match-end 0))))) | |
330 (setq definition | |
331 (mapconcat (function (lambda (x) | |
332 (or (mail-resolve-all-aliases-1 | |
717 | 333 (intern-soft x mail-aliases) |
334 (cons sym so-far)) | |
459 | 335 x))) |
336 (nreverse result) | |
627 | 337 mail-alias-separator-string)) |
459 | 338 (set sym definition)))) |
339 (symbol-value sym)) | |
340 | |
341 | |
627 | 342 (defun mail-abbrev-expand-hook () |
598 | 343 "For use as the fourth arg to define-abbrev. |
627 | 344 After expanding a mail-abbrev, if fill-mode is on and we're past the |
598 | 345 fill-column, break the line at the previous comma, and indent the next |
346 line." | |
608 | 347 (save-excursion |
348 (let ((p (point)) | |
349 bol comma fp) | |
350 (beginning-of-line) | |
351 (setq bol (point)) | |
352 (goto-char p) | |
606 | 353 (while (and auto-fill-function |
608 | 354 (>= (current-column) fill-column) |
355 (search-backward "," bol t)) | |
356 (setq comma (point)) | |
357 (forward-char 1) ; Now we are just past the comma. | |
358 (insert "\n") | |
359 (delete-horizontal-space) | |
627 | 360 (setq p (point)) |
608 | 361 (indent-relative) |
362 (setq fp (buffer-substring p (point))) | |
363 ;; Go to the end of the new line. | |
364 (end-of-line) | |
365 (if (> (current-column) fill-column) | |
366 ;; It's still too long; do normal auto-fill. | |
367 (let ((fill-prefix (or fp "\t"))) | |
368 (do-auto-fill))) | |
369 ;; Resume the search. | |
370 (goto-char comma) | |
371 )))) | |
598 | 372 |
373 ;;; Syntax tables and abbrev-expansion | |
459 | 374 |
598 | 375 (defvar mail-abbrev-mode-regexp "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\):" |
459 | 376 "*Regexp to select mail-headers in which mail-aliases should be expanded. |
377 This string it will be handed to `looking-at' with the point at the beginning | |
378 of the current line; if it matches, abbrev mode will be turned on, otherwise | |
379 it will be turned off. (You don't need to worry about continuation lines.) | |
380 This should be set to match those mail fields in which you want abbreviations | |
381 turned on.") | |
382 | |
383 (defvar mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table) | |
598 | 384 "The syntax table which is used in send-mail mode message bodies.") |
459 | 385 |
386 (defvar mail-mode-header-syntax-table | |
387 (let ((tab (copy-syntax-table text-mode-syntax-table))) | |
627 | 388 ;; This makes the characters "@%!._-" be considered symbol-consituents |
459 | 389 ;; but not word-constituents, so forward-sexp will move you over an |
390 ;; entire address, but forward-word will only move you over a sequence | |
391 ;; of alphanumerics. (Clearly the right thing.) | |
392 (modify-syntax-entry ?@ "_" tab) | |
393 (modify-syntax-entry ?% "_" tab) | |
394 (modify-syntax-entry ?! "_" tab) | |
395 (modify-syntax-entry ?. "_" tab) | |
396 (modify-syntax-entry ?_ "_" tab) | |
397 (modify-syntax-entry ?- "_" tab) | |
398 (modify-syntax-entry ?< "(>" tab) | |
399 (modify-syntax-entry ?> ")<" tab) | |
400 tab) | |
598 | 401 "The syntax table used in send-mail mode when in a mail-address header. |
402 mail-mode-syntax-table is used when the cursor is in the message body or in | |
403 non-address headers.") | |
404 | |
405 (defvar mail-abbrev-syntax-table | |
406 (let* ((tab (copy-syntax-table mail-mode-header-syntax-table)) | |
407 (i (1- (length tab))) | |
408 (_ (aref (standard-syntax-table) ?_)) | |
409 (w (aref (standard-syntax-table) ?w))) | |
410 (while (>= i 0) | |
411 (if (= (aref tab i) _) (aset tab i w)) | |
412 (setq i (1- i))) | |
413 tab) | |
414 "The syntax-table used for abbrev-expansion purposes; this is not actually | |
415 made the current syntax table of the buffer, but simply controls the set of | |
416 characters which may be a part of the name of a mail-alias.") | |
417 | |
459 | 418 |
598 | 419 (defun mail-abbrev-in-expansion-header-p () |
420 "Whether point is in a mail-address header field." | |
421 (let ((case-fold-search t)) | |
422 (and ;; | |
423 ;; we are on an appropriate header line... | |
424 (save-excursion | |
425 (beginning-of-line) | |
426 ;; skip backwards over continuation lines. | |
427 (while (and (looking-at "^[ \t]") | |
428 (not (= (point) (point-min)))) | |
429 (forward-line -1)) | |
430 ;; are we at the front of an appropriate header line? | |
431 (looking-at mail-abbrev-mode-regexp)) | |
432 ;; | |
433 ;; ...and we are before the mail-header-separator | |
434 (< (point) | |
435 (save-excursion | |
436 (goto-char (point-min)) | |
437 (search-forward (concat "\n" mail-header-separator "\n") | |
438 nil 0) | |
439 (point)))))) | |
440 | |
441 (defvar mail-mode-abbrev-table) ; quiet the compiler | |
442 | |
627 | 443 (defun sendmail-pre-abbrev-expand-hook () |
1148 | 444 (and (and mail-aliases (not (eq mail-aliases t))) |
445 (if (mail-abbrev-in-expansion-header-p) | |
446 (progn | |
447 ;; | |
448 ;; We are in a To: (or CC:, or whatever) header, and | |
449 ;; should use word-abbrevs to expand mail aliases. | |
450 | |
451 ;; Before anything else, resolve aliases if they need it. | |
452 (and mail-abbrev-aliases-need-to-be-resolved | |
453 (mail-resolve-all-aliases)) | |
454 | |
455 ;; Now proceed with the abbrev section. | |
456 ;; - First, install the mail-aliases as the word-abbrev table. | |
457 ;; - Then install the mail-abbrev-syntax-table, which | |
458 ;; temporarily marks all of the | |
459 ;; non-alphanumeric-atom-characters (the "_" | |
460 ;; syntax ones) as being normal word-syntax. We do this | |
461 ;; because the C code for expand-abbrev only works on words, | |
462 ;; and we want these characters to be considered words for | |
463 ;; the purpose of abbrev expansion. | |
464 ;; - Then we call expand-abbrev again, recursively, to do | |
465 ;; the abbrev expansion with the above syntax table. | |
466 ;; - Then we do a trick which tells the expand-abbrev frame | |
467 ;; which invoked us to not continue (and thus not | |
468 ;; expand twice.) This means that any abbrev expansion | |
469 ;; will happen as a result of this function's call to | |
470 ;; expand-abbrev, and not as a result of the call to | |
471 ;; expand-abbrev which invoked *us*. | |
472 ;; - Then we set the syntax table to | |
473 ;; mail-mode-header-syntax-table, which doesn't have | |
474 ;; anything to do with abbrev expansion, but | |
475 ;; is just for the user's convenience (see its doc string.) | |
476 ;; | |
477 | |
478 (setq local-abbrev-table mail-aliases) | |
479 | |
480 ;; If the character just typed was non-alpha-symbol-syntax, | |
481 ;; then don't expand the abbrev now (that is, don't expand | |
482 ;; when the user types -.) Check the character's syntax in | |
483 ;; the mail-mode-header-syntax-table. | |
484 | |
485 (set-syntax-table mail-mode-header-syntax-table) | |
486 (or (eq (char-syntax last-command-char) ?_) | |
487 (let ((pre-abbrev-expand-hook nil)) ; That's us; don't loop. | |
488 ;; Use this table so that abbrevs can have hyphens in them. | |
489 (set-syntax-table mail-abbrev-syntax-table) | |
490 (expand-abbrev) | |
491 ;; Now set it back to what it was before. | |
492 (set-syntax-table mail-mode-header-syntax-table))) | |
493 (setq abbrev-start-location (point) ; This is the trick. | |
494 abbrev-start-location-buffer (current-buffer))) | |
495 | |
496 ;; We're not in a mail header where mail aliases should | |
497 ;; be expanded, then use the normal mail-mode abbrev table | |
498 ;; (if any) and the normal mail-mode syntax table. | |
499 | |
500 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table) | |
501 mail-mode-abbrev-table)) | |
502 (set-syntax-table mail-mode-syntax-table)) | |
503 )) | |
598 | 504 |
505 ;;; utilities | |
459 | 506 |
507 (defun merge-mail-aliases (file) | |
508 "Merge mail aliases from the given file with existing ones." | |
509 (interactive (list | |
510 (let ((insert-default-directory t) | |
511 (default-directory (expand-file-name "~/")) | |
512 (def (mail-abbrev-mailrc-file))) | |
513 (read-file-name | |
514 (format "Read additional aliases from file: (default %s) " | |
515 def) | |
516 default-directory | |
517 (expand-file-name def default-directory) | |
518 t)))) | |
519 (build-mail-aliases file)) | |
520 | |
521 (defun rebuild-mail-aliases (file) | |
522 "Rebuild all the mail aliases from the given file." | |
523 (interactive (list | |
524 (let ((insert-default-directory t) | |
525 (default-directory (expand-file-name "~/")) | |
526 (def (mail-abbrev-mailrc-file))) | |
527 (read-file-name | |
528 (format "Read mail aliases from file: (default %s) " def) | |
529 default-directory | |
530 (expand-file-name def default-directory) | |
531 t)))) | |
532 (setq mail-aliases nil) | |
533 (build-mail-aliases file)) | |
584 | 534 |
598 | 535 (defun mail-interactive-insert-alias (&optional alias) |
536 "Prompt for and insert a mail alias." | |
907 | 537 (interactive (progn |
538 (if (not (vectorp mail-aliases)) (mail-aliases-setup)) | |
539 (list (completing-read "Expand alias: " mail-aliases nil t)))) | |
540 (if (not (vectorp mail-aliases)) (mail-aliases-setup)) | |
599 | 541 (insert (or (and alias (symbol-value (intern-soft alias mail-aliases))) ""))) |
598 | 542 |
543 (defun abbrev-hacking-next-line (&optional arg) | |
544 "Just like `next-line' (\\[next-line]) but expands abbrevs when at \ | |
545 end of line." | |
546 (interactive "p") | |
717 | 547 (if (looking-at "[ \t]*\n") (expand-abbrev)) |
548 (setq this-command 'next-line) | |
598 | 549 (next-line arg)) |
550 | |
551 (defun abbrev-hacking-end-of-buffer (&optional arg) | |
552 "Just like `end-of-buffer' (\\[end-of-buffer]) but expands abbrevs when at \ | |
553 end of line." | |
554 (interactive "P") | |
717 | 555 (if (looking-at "[ \t]*\n") (expand-abbrev)) |
556 (setq this-command 'end-of-buffer) | |
598 | 557 (end-of-buffer arg)) |
558 | |
559 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias) | |
560 | |
606 | 561 ;;(define-key mail-mode-map "\C-n" 'abbrev-hacking-next-line) |
562 ;;(define-key mail-mode-map "\M->" 'abbrev-hacking-end-of-buffer) | |
598 | 563 |
459 | 564 (provide 'mail-abbrevs) |
606 | 565 |
566 | |
567 ;;; V18 compatibility | |
627 | 568 ;;; |
569 ;;; All of the Emacs18 stuff is isolated down here so that it will be | |
570 ;;; easy to delete once v18 finally bites the dust. | |
571 ;;; | |
572 ;;; These defuns and defvars aren't inside the cond in deference to | |
573 ;;; the intense brokenness of the v18 byte-compiler. | |
717 | 574 ;;; |
575 ;;; All the code on this page is gross and hidious and awful and might | |
576 ;;; not even work all that well. Comfort yourself with knowing that the | |
577 ;;; v19 code above works wonderfully. | |
606 | 578 |
579 (defun sendmail-v18-self-insert-command (arg) | |
580 "Just like self-insert-command, but runs sendmail-pre-abbrev-expand-hook." | |
581 (interactive "p") | |
717 | 582 (if (not (eq (char-syntax last-command-char) ?w)) |
627 | 583 (progn |
584 (sendmail-pre-abbrev-expand-hook) | |
585 ;; Unhack expand-abbrev, so it will work right next time around. | |
586 (setq abbrev-start-location nil))) | |
717 | 587 ;; this is gross and wasteful. |
588 (let ((abbrev-mode (if (mail-abbrev-in-expansion-header-p) | |
589 nil | |
590 abbrev-mode))) | |
627 | 591 (self-insert-command arg))) |
606 | 592 |
717 | 593 (defun abbrev-hacking-next-line-v18 (arg) |
594 (if (looking-at "[ \t]*\n") (sendmail-pre-abbrev-expand-hook)) | |
595 (setq this-command 'next-line) | |
596 (next-line arg)) | |
597 | |
598 (defun abbrev-hacking-end-of-buffer-v18 (arg) | |
599 (if (looking-at "[ \t]*\n") (sendmail-pre-abbrev-expand-hook)) | |
600 (setq this-command 'end-of-buffer) | |
601 (end-of-buffer arg)) | |
602 | |
606 | 603 (defvar mail-abbrevs-v18-map-munged nil) |
604 | |
605 (defun mail-abbrevs-v18-munge-map () | |
627 | 606 ;; For every key that is bound to self-insert-command in global-map, |
607 ;; bind that key to sendmail-self-insert-command in mail-mode-map. | |
608 ;; We used to do this by making the mail-mode-map be a non-sparse map, | |
609 ;; but that made the esc-map be shared in such a way that making a | |
610 ;; local meta binding in the mail-mode-map made a *global* binding | |
611 ;; instead. Yucko. | |
612 (let ((global-map (current-global-map)) | |
717 | 613 new-bindings |
627 | 614 (i 0)) |
615 (while (< i 128) | |
616 (if (eq 'self-insert-command (or (cdr (assq i mail-mode-map)) | |
617 (aref global-map i))) | |
717 | 618 (setq new-bindings |
619 (cons (cons i 'sendmail-v18-self-insert-command) | |
620 new-bindings))) | |
621 (setq i (1+ i))) | |
622 (setq mail-mode-map | |
623 (nconc (copy-keymap mail-mode-map) (nreverse new-bindings)))) | |
606 | 624 (setq mail-abbrevs-v18-map-munged t)) |
625 | |
627 | 626 (defun mail-aliases-setup-v18 () |
606 | 627 "Put this on `mail-setup-hook' to use mail-abbrevs." |
717 | 628 (if (not (eq major-mode 'mail-mode)) |
629 nil | |
630 (or (and mail-mode-map (eq (current-local-map) mail-mode-map)) | |
631 (error "shut 'er down clancy, she's suckin' mud")) | |
632 (if (and (not (vectorp mail-aliases)) | |
633 (file-exists-p (mail-abbrev-mailrc-file))) | |
634 (build-mail-aliases)) | |
635 (or mail-abbrevs-v18-map-munged (mail-abbrevs-v18-munge-map)) | |
636 (use-local-map mail-mode-map) | |
637 (abbrev-mode 1))) | |
606 | 638 |
639 | |
1148 | 640 (cond ((string-match "^18\\." emacs-version) |
627 | 641 ;; |
642 ;; v19 (and this code) uses a new name for this function. | |
606 | 643 (or (fboundp 'buffer-disable-undo) |
644 (fset 'buffer-disable-undo 'buffer-flush-undo)) | |
627 | 645 ;; |
646 ;; v19 (and this code) uses a new name for auto-fill-hook (-function). | |
647 ;; Encapsulate the function that uses it to bind the new name. | |
606 | 648 (or (fboundp 'mail-abbrev-expand-hook-v19) |
649 (fset 'mail-abbrev-expand-hook-v19 | |
650 (symbol-function 'mail-abbrev-expand-hook))) | |
627 | 651 (fset 'mail-abbrev-expand-hook |
652 (function (lambda () | |
653 (let ((auto-fill-function auto-fill-hook)) | |
654 (mail-abbrev-expand-hook-v19))))) | |
655 ;; | |
656 ;; Turn off the broken v18 code (that is still called from sendmail.el) | |
606 | 657 (fset 'expand-mail-aliases |
627 | 658 (function (lambda (&rest args) |
659 "Obsoleted by mail-abbrevs. Does nothing." | |
660 nil))) | |
661 ;; | |
717 | 662 ;; Redefine the abbrev-hacking functions. Yuck. |
663 (fset 'abbrev-hacking-next-line | |
664 (function (lambda (p) (interactive "p") | |
665 (abbrev-hacking-next-line-v18 p)))) | |
666 (fset 'abbrev-hacking-end-of-buffer | |
667 (function (lambda (p) (interactive "P") | |
668 (abbrev-hacking-end-of-buffer-v18 p)))) | |
669 ;; | |
627 | 670 ;; Encapsulate mail-setup to do the necessary buffer initializations. |
671 (or (fboundp 'mail-setup-v18) | |
672 (fset 'mail-setup-v18 (symbol-function 'mail-setup))) | |
673 (fset 'mail-setup | |
674 (function (lambda (&rest args) | |
675 (mail-aliases-setup-v18) | |
676 (apply 'mail-setup-v18 args)))) | |
717 | 677 |
678 ;; | |
679 ;; Encapsulate VM's version of mail-setup as well, if vm-mail is | |
680 ;; defined as a function or as an autoload. | |
681 (cond ((and (fboundp 'vm-mail) | |
682 (if (eq 'autoload (car-safe (symbol-function 'vm-mail))) | |
683 (load (nth 1 (symbol-function 'vm-mail)) t) | |
684 t)) | |
685 (or (fboundp 'vm-mail-internal-v18) | |
686 (fset 'vm-mail-internal-v18 | |
687 (symbol-function 'vm-mail-internal))) | |
688 (fset 'vm-mail-internal | |
689 (function (lambda (&rest args) | |
1148 | 690 (mail-aliases-setup-v18) |
691 (apply 'vm-mail-internal-v18 args)))))) | |
717 | 692 |
693 ;; If we're being loaded from mail-setup-hook or mail-mode-hook | |
694 ;; as run from inside mail-setup or vm-mail-internal, then install | |
695 ;; right now. | |
696 (if (eq major-mode 'mail-mode) | |
697 (mail-aliases-setup-v18)) | |
606 | 698 ) |
627 | 699 |
606 | 700 (t ; v19 |
701 (fmakunbound 'expand-mail-aliases))) |