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