Merge from trunk
[bpt/emacs.git] / lisp / mail / mailabbrev.el
1 ;;; mailabbrev.el --- abbrev-expansion of mail aliases
2
3 ;; Copyright (C) 1985-1987, 1992-1993, 1996-1997, 2000-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Jamie Zawinski <jwz@lucid.com; now jwz@jwz.org>
7 ;; Maintainer: FSF
8 ;; Created: 19 Oct 90
9 ;; Keywords: mail
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
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
31 ;; `mail-personal-alias-file') if it exists. Your mail aliases will
32 ;; expand any time you type a word-delimiter at the end of an abbreviation.
33 ;;
34 ;; What you see is what you get: if mailabbrev is in use when you type
35 ;; a name, and the name does not expand, you know it is not an abbreviation.
36 ;; However, if you yank abbreviations into the headers
37 ;; in a way that bypasses the check for abbreviations,
38 ;; they are expanded (but not visibly) when you send the message.
39 ;;
40 ;; Your mail alias abbrevs will be in effect only when the point is in an
41 ;; appropriate header field. When in the body of the message, or other
42 ;; header fields, the mail aliases will not expand. Rather, the normal
43 ;; mode-specific abbrev table will be used if
44 ;; defined. So if you use mail-mode specific abbrevs, this code will not
45 ;; adversely affect you. You can control which header fields the abbrevs
46 ;; are used in by changing the variable mail-abbrev-mode-regexp.
47 ;;
48 ;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word
49 ;; boundaries; also, header continuation-lines will be properly indented.
50 ;;
51 ;; You can also insert a mail alias with mail-abbrev-insert-alias
52 ;; (bound to C-c C-a), which prompts you for an alias (with completion)
53 ;; and inserts its expansion at point.
54 ;;
55 ;; This file fixes a bug in the old system which prohibited your .mailrc
56 ;; file from having lines like
57 ;;
58 ;; alias someone "John Doe <doe@quux.com>"
59 ;;
60 ;; That is, if you want an address to have embedded spaces, simply surround it
61 ;; with double-quotes. This is necessary because the format of the .mailrc
62 ;; file bogusly uses spaces as address delimiters. The following line defines
63 ;; an alias which expands to three addresses:
64 ;;
65 ;; alias foobar addr-1 addr-2 "address three <addr-3>"
66 ;;
67 ;; (This is bogus because mail-delivery programs want commas, not spaces,
68 ;; but that's what the file format is, so we have to live with it.)
69 ;;
70 ;; If you like, you can call the function define-mail-abbrev to define your
71 ;; mail aliases instead of using a .mailrc file. When you call it in this
72 ;; way, addresses are separated by commas.
73 ;;
74 ;; CAVEAT: This works on most Sun systems; I have been told that some versions
75 ;; of /bin/mail do not understand double-quotes in the .mailrc file. So you
76 ;; should make sure your version does before including verbose addresses like
77 ;; this. One solution to this, if you are on a system whose /bin/mail doesn't
78 ;; work that way, (and you still want to be able to /bin/mail to send mail in
79 ;; addition to emacs) is to define minimal aliases (without full names) in
80 ;; your .mailrc file, and use define-mail-abbrev to redefine them when sending
81 ;; mail from emacs; this way, mail sent from /bin/mail will work, and mail
82 ;; sent from emacs will be pretty.
83 ;;
84 ;; Aliases in the mailrc file may be nested. If you define aliases like
85 ;; alias group1 fred ethel
86 ;; alias group2 larry curly moe
87 ;; alias everybody group1 group2
88 ;; Then when you type "everybody" on the To: line, it will be expanded to
89 ;; fred, ethyl, larry, curly, moe
90 ;;
91 ;; Aliases may also contain forward references; the alias of "everybody" can
92 ;; precede the aliases of "group1" and "group2".
93 ;;
94 ;; This code also understands the "source" .mailrc command, for reading
95 ;; aliases from some other file as well.
96 ;;
97 ;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs
98 ;; normally cannot contain hyphens, but this code works around that for the
99 ;; specific case of mail-alias word-abbrevs.
100 ;;
101 ;; To read in the contents of another .mailrc-type file from emacs, use the
102 ;; command Meta-X merge-mail-abbrevs. The rebuild-mail-abbrevs command is
103 ;; similar, but will delete existing aliases first.
104 ;;
105 ;; If you would like your aliases to be expanded when you type M-> or ^N to
106 ;; move out of the mail-header into the message body (instead of having to
107 ;; type SPC at the end of the abbrev before moving away) then you can do
108 ;;
109 ;; (add-hook
110 ;; 'mail-mode-hook
111 ;; (lambda ()
112 ;; (define-key mail-mode-map [remap next-line] 'mail-abbrev-next-line)
113 ;; (define-key mail-mode-map [remap end-of-buffer] 'mail-abbrev-end-of-buffer)))
114 ;;
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
117 ;; be a comma bracketed by whitespace if you want any kind of reasonable
118 ;; behavior.
119 ;;
120 ;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
121 ;; Noah Friedman for suggestions and bug reports.
122
123 ;; To use this package, do (add-hook 'mail-mode-hook 'mail-abbrevs-setup).
124
125 ;;; Code:
126
127 (eval-when-compile
128 (require 'sendmail))
129
130 (defgroup mail-abbrev nil
131 "Expand mail aliases as abbrevs, in certain mail headers."
132 :group 'abbrev-mode)
133
134 ;;;###autoload
135 (define-minor-mode mail-abbrevs-mode
136 "Non-nil means expand mail aliases as abbrevs, in certain message headers."
137 :global t
138 :group 'mail-abbrev
139 :version "20.3"
140 (if mail-abbrevs-mode (mail-abbrevs-enable) (mail-abbrevs-disable)))
141
142 (defcustom mail-abbrevs-only nil
143 "Non-nil means only mail abbrevs should expand automatically.
144 Other abbrevs expand only when you explicitly use `expand-abbrev'."
145 :type 'boolean
146 :group 'mail-abbrev)
147
148 ;; originally defined in sendmail.el - used to be an alist, now is a table.
149 (defvar mail-abbrevs nil
150 "Word-abbrev table of mail address aliases.
151 If this is nil, it means the aliases have not yet been initialized and
152 should be read from the .mailrc file. (This is distinct from there being
153 no aliases, which is represented by this being a table with no entries.)")
154
155 (defvar mail-abbrev-modtime nil
156 "The modification time of your mail alias file when it was last examined.")
157
158 (defun mail-abbrevs-sync-aliases ()
159 (when mail-personal-alias-file
160 (if (file-exists-p mail-personal-alias-file)
161 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
162 (if (not (equal mail-abbrev-modtime modtime))
163 (progn
164 (setq mail-abbrev-modtime modtime)
165 (build-mail-abbrevs)))))))
166
167 ;;;###autoload
168 (defun mail-abbrevs-setup ()
169 "Initialize use of the `mailabbrev' package."
170 (if (and (not (vectorp mail-abbrevs))
171 (file-exists-p mail-personal-alias-file))
172 (progn
173 (setq mail-abbrev-modtime
174 (nth 5 (file-attributes mail-personal-alias-file)))
175 (build-mail-abbrevs)))
176 (mail-abbrevs-sync-aliases)
177 (add-hook 'abbrev-expand-functions 'mail-abbrev-expand-wrapper nil t)
178 (abbrev-mode 1))
179
180 (defun mail-abbrevs-enable ()
181 (add-hook 'mail-mode-hook 'mail-abbrevs-setup))
182
183 (defun mail-abbrevs-disable ()
184 "Turn off use of the `mailabbrev' package."
185 (remove-hook 'mail-mode-hook 'mail-abbrevs-setup)
186 (abbrev-mode (if (default-value 'abbrev-mode) 1 -1)))
187
188 ;;;###autoload
189 (defun build-mail-abbrevs (&optional file recursivep)
190 "Read mail aliases from personal mail alias file and set `mail-abbrevs'.
191 By default this is the file specified by `mail-personal-alias-file'."
192 (setq file (expand-file-name (or file mail-personal-alias-file)))
193 (if (vectorp mail-abbrevs)
194 nil
195 (setq mail-abbrevs nil)
196 (define-abbrev-table 'mail-abbrevs '()))
197 (message "Parsing %s..." file)
198 (with-temp-buffer
199 (buffer-disable-undo)
200 (cond ((get-file-buffer file)
201 (insert (with-current-buffer (get-file-buffer file)
202 (buffer-substring (point-min) (point-max)))))
203 ((not (file-exists-p file)))
204 (t (insert-file-contents file)))
205 ;; Don't lose if no final newline.
206 (goto-char (point-max))
207 (or (eq (preceding-char) ?\n) (newline))
208 (goto-char (point-min))
209 ;; Delete comments from the file
210 (while (search-forward "# " nil t)
211 (let ((p (- (point) 2)))
212 (end-of-line)
213 (delete-region p (point))))
214 (goto-char (point-min))
215 ;; handle "\\\n" continuation lines
216 (while (not (eobp))
217 (end-of-line)
218 (if (= (preceding-char) ?\\)
219 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
220 (forward-char 1)))
221 (goto-char (point-min))
222 (while (re-search-forward
223 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t)
224 (beginning-of-line)
225 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
226 (progn
227 (end-of-line)
228 (build-mail-abbrevs
229 (substitute-in-file-name
230 (buffer-substring (match-beginning 1) (match-end 1)))
231 t))
232 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
233 (let* ((name (buffer-substring
234 (match-beginning 1) (match-end 1)))
235 (start (progn (skip-chars-forward " \t") (point))))
236 (end-of-line)
237 ;; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
238 (define-mail-abbrev
239 name
240 (buffer-substring start (point))
241 t))))
242 ;; Resolve forward references in .mailrc file.
243 ;; This would happen automatically before the first abbrev was
244 ;; expanded, but why not do it now.
245 (or recursivep (mail-resolve-all-aliases))
246 mail-abbrevs)
247 (message "Parsing %s... done" file))
248
249 (defvar mail-alias-separator-string ", "
250 "*A string inserted between addresses in multi-address mail aliases.
251 This has to contain a comma, so \", \" is a reasonable value. You might
252 also want something like \",\\n \" to get each address on its own line.")
253
254 ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases
255 ;; to be called before expanding abbrevs if it's necessary.
256 (defvar mail-abbrev-aliases-need-to-be-resolved t)
257
258 ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with
259 ;; stuff parsed from the .mailrc file.
260 ;;
261 ;;;###autoload
262 (defun define-mail-abbrev (name definition &optional from-mailrc-file)
263 "Define NAME as a mail alias abbrev that translates to DEFINITION.
264 If DEFINITION contains multiple addresses, separate them with commas.
265
266 Optional argument FROM-MAILRC-FILE means that DEFINITION comes
267 from a mailrc file. In that case, addresses are separated with
268 spaces and addresses with embedded spaces are surrounded by
269 double-quotes."
270 ;; When this is called from build-mail-abbrevs, the third argument is
271 ;; true, and we do some evil space->comma hacking like /bin/mail does.
272 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
273 ;; Read the defaults first, if we have not done so.
274 (unless (vectorp mail-abbrevs) (build-mail-abbrevs))
275 ;; strip garbage from front and end
276 (if (string-match "\\`[ \t\n,]+" definition)
277 (setq definition (substring definition (match-end 0))))
278 (if (string-match "[ \t\n,]+\\'" definition)
279 (setq definition (substring definition 0 (match-beginning 0))))
280 (let* ((L (length definition))
281 (start (if (> L 0) 0))
282 end this-entry result)
283 (while start
284 (cond
285 (from-mailrc-file
286 ;; If we're reading from the mailrc file, addresses are
287 ;; delimited by spaces, and addresses with embedded spaces are
288 ;; surrounded by non-escaped double-quotes.
289 (if (eq ?\" (aref definition start))
290 (setq start (1+ start)
291 end (and (string-match
292 "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*"
293 definition start)
294 (match-end 1)))
295 (setq end (string-match "[ \t,]+" definition start)))
296 ;; Extract the address and advance the loop past it.
297 (setq this-entry (substring definition start end)
298 start (and end (/= (match-end 0) L) (match-end 0)))
299 ;; If the full name contains a problem character, quote it.
300 (and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
301 (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
302 (match-string 1 this-entry))
303 (setq this-entry (replace-regexp-in-string
304 "\\(.+?\\)[ \t]*\\(<.*>\\)"
305 "\"\\1\" \\2"
306 this-entry)))
307 (push this-entry result))
308 ;; When we are not reading from .mailrc, addresses are
309 ;; separated by commas. Try to accept a rfc822-like syntax.
310 ;; (Todo: extend rfc822.el to do the work for us.)
311 ((equal (string-match
312 "[ \t,]*\\(\"\\(?:[^\"]\\|[^\\]\\(?:[\\][\\]\\)*\"\\)*\"[ \t]*\
313 <[-.!#$%&'*+/0-9=?A-Za-z^_`{|}~@]+>\\)[ \t,]*"
314 definition start)
315 start)
316 ;; If an entry has a valid [ "foo bar" <foo@example.com> ]
317 ;; form, use it literally . This also allows commas in the
318 ;; quoted string, e.g. [ "foo bar, jr" <foo@example.com> ]
319 (push (match-string 1 definition) result)
320 (setq start (and (/= (match-end 0) L) (match-end 0))))
321 (t
322 ;; Otherwise, read the next address by looking for a comma.
323 (setq end (string-match "[ \t\n,]*,[ \t\n]*" definition start))
324 (setq this-entry (substring definition start end))
325 ;; Advance the loop past this address.
326 (setq start (and end (/= (match-end 0) L) (match-end 0)))
327 ;; If the full name contains a problem character, quote it.
328 (and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
329 (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
330 (match-string 1 this-entry))
331 (setq this-entry (replace-regexp-in-string
332 "\\(.+?\\)[ \t]*\\(<.*>\\)" "\"\\1\" \\2"
333 this-entry)))
334 (push this-entry result))))
335
336 (setq definition (mapconcat (function identity)
337 (nreverse result)
338 mail-alias-separator-string)))
339 (setq mail-abbrev-aliases-need-to-be-resolved t)
340 (setq name (downcase name))
341 ;; use an abbrev table instead of an alist for mail-abbrevs.
342 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed.
343 (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook 0 t)))
344
345
346 (defun mail-resolve-all-aliases ()
347 "Resolve all forward references in the mail aliases table."
348 (if mail-abbrev-aliases-need-to-be-resolved
349 (progn
350 ;; (message "Resolving mail aliases...")
351 (if (vectorp mail-abbrevs)
352 (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs))
353 (setq mail-abbrev-aliases-need-to-be-resolved nil)
354 ;; (message "Resolving mail aliases... done.")
355 )))
356
357 (defun mail-resolve-all-aliases-1 (sym &optional so-far)
358 (if (memq sym so-far)
359 (error "mail alias loop detected: %s"
360 (mapconcat 'symbol-name (cons sym so-far) " <- ")))
361 (let ((definition (and (boundp sym) (symbol-value sym))))
362 (if definition
363 (let ((result '())
364 (start 0))
365 (while start
366 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start)))
367 (setq result (cons (substring definition start end) result)
368 start (and end (match-end 0)))))
369 (setq definition
370 (mapconcat (function (lambda (x)
371 (or (mail-resolve-all-aliases-1
372 (intern-soft (downcase x) mail-abbrevs)
373 (cons sym so-far))
374 x)))
375 (nreverse result)
376 mail-alias-separator-string))
377 (set sym definition))))
378 (symbol-value sym))
379
380
381 (defun mail-abbrev-expand-hook ()
382 "For use as the fourth arg to `define-abbrev'.
383 After expanding a mail-abbrev, if Auto Fill mode is on and we're past the
384 fill-column, break the line at the previous comma, and indent the next line."
385 ;; Disable abbrev mode to avoid recursion in indent-relative expanding
386 ;; part of the abbrev expansion as an abbrev itself.
387 (let ((abbrev-mode nil))
388 (save-excursion
389 (let ((p (point))
390 bol comma fp)
391 (beginning-of-line)
392 (setq bol (point))
393 (goto-char p)
394 (while (and auto-fill-function
395 (>= (current-column) fill-column)
396 (search-backward "," bol t))
397 (setq comma (point))
398 (forward-char 1) ; Now we are just past the comma.
399 (insert "\n")
400 (delete-horizontal-space)
401 (setq p (point))
402 (indent-relative)
403 (setq fp (buffer-substring p (point)))
404 ;; Go to the end of the new line.
405 (end-of-line)
406 (if (> (current-column) fill-column)
407 ;; It's still too long; do normal auto-fill.
408 (let ((fill-prefix (or fp "\t")))
409 (do-auto-fill)))
410 ;; Resume the search.
411 (goto-char comma)
412 )))))
413 \f
414 ;;; Syntax tables and abbrev-expansion
415
416 (defvar mail-abbrev-mode-regexp
417 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
418 "*Regexp to select mail-headers in which mail abbrevs should be expanded.
419 This string will be handed to `looking-at' with point at the beginning
420 of the current line; if it matches, abbrev mode will be turned on, otherwise
421 it will be turned off. (You don't need to worry about continuation lines.)
422 This should be set to match those mail fields in which you want abbreviations
423 turned on.")
424
425 (defvar mail-abbrev-syntax-table nil
426 "The syntax-table used for abbrev-expansion purposes.
427 This is not actually made the current syntax table of the buffer, but
428 simply controls the set of characters which may be a part of the name
429 of a mail alias. The value is set up, buffer-local, when first needed.")
430
431 (defun mail-abbrev-make-syntax-table ()
432 (make-local-variable 'mail-abbrev-syntax-table)
433 (unless mail-abbrev-syntax-table
434 (let ((tab (copy-syntax-table (syntax-table)))
435 (_ (aref (standard-syntax-table) ?_))
436 (w (aref (standard-syntax-table) ?w)))
437 (map-char-table
438 (function (lambda (key value)
439 (if (null value)
440 ;; Fetch the inherited value
441 (setq value (aref tab key)))
442 (if (equal value _)
443 (set-char-table-range tab key w))))
444 tab)
445 (modify-syntax-entry ?@ "w" tab)
446 (modify-syntax-entry ?% "w" tab)
447 (modify-syntax-entry ?! "w" tab)
448 (modify-syntax-entry ?. "w" tab)
449 (modify-syntax-entry ?_ "w" tab)
450 (modify-syntax-entry ?- "w" tab)
451 (setq mail-abbrev-syntax-table tab))))
452
453 (defun mail-abbrev-in-expansion-header-p ()
454 "Whether point is in a mail-address header field."
455 (let ((case-fold-search t))
456 (and ;;
457 ;; we are on an appropriate header line...
458 (save-excursion
459 (unless (eobp) (forward-char 1))
460 (re-search-backward "^[^ \t]" nil 'move)
461 ;; are we at the front of an appropriate header line?
462 (looking-at mail-abbrev-mode-regexp))
463 ;;
464 ;; ...and are we in the headers?
465 (< (point)
466 (save-restriction
467 (widen)
468 (save-excursion
469 (rfc822-goto-eoh)
470 (point)))))))
471
472 (defun mail-abbrev-expand-wrapper (expand)
473 (if (and mail-abbrevs (not (eq mail-abbrevs t)))
474 (if (mail-abbrev-in-expansion-header-p)
475
476 ;; We are in a To: (or CC:, or whatever) header, and
477 ;; should use word-abbrevs to expand mail aliases.
478 (let ((local-abbrev-table mail-abbrevs))
479
480 ;; Before anything else, resolve aliases if they need it.
481 (and mail-abbrev-aliases-need-to-be-resolved
482 (mail-resolve-all-aliases))
483
484 ;; Now proceed with the abbrev section.
485 ;; - We already installed mail-abbrevs as the abbrev table.
486 ;; - Then install the mail-abbrev-syntax-table, which
487 ;; temporarily marks all of the
488 ;; non-alphanumeric-atom-characters (the "_"
489 ;; syntax ones) as being normal word-syntax. We do this
490 ;; because the C code for expand-abbrev only works on words,
491 ;; and we want these characters to be considered words for
492 ;; the purpose of abbrev expansion.
493 ;; - Then we call the expand function, to do
494 ;; the abbrev expansion with the above syntax table.
495
496 (mail-abbrev-make-syntax-table)
497
498 ;; If the character just typed was non-alpha-symbol-syntax,
499 ;; then don't expand the abbrev now (that is, don't expand
500 ;; when the user types -.) Check the character's syntax in
501 ;; the usual syntax table.
502
503 (or (and (integerp last-command-event)
504 ;; Some commands such as M-> may want to expand first.
505 (equal this-command 'self-insert-command)
506 (or (eq (char-syntax last-command-event) ?_)
507 ;; Don't expand on @.
508 (memq last-command-event '(?@ ?. ?% ?! ?_ ?-))))
509 ;; Use this table so that abbrevs can have hyphens in them.
510 (with-syntax-table mail-abbrev-syntax-table
511 (funcall expand))))
512
513 (if (or (not mail-abbrevs-only)
514 (eq this-command 'expand-abbrev))
515 ;; We're not in a mail header where mail aliases should
516 ;; be expanded, then use the normal mail-mode abbrev table
517 ;; (if any) and the normal mail-mode syntax table.
518 (funcall expand)
519 ;; This is not a mail abbrev, and we should not expand it.
520 ;; Don't expand anything.
521 nil))
522 ;; No mail-abbrevs at all, do the normal thing.
523 (funcall expand)))
524 \f
525 ;;; utilities
526
527 (defun merge-mail-abbrevs (file)
528 "Merge mail aliases from the given file with existing ones."
529 (interactive (list
530 (let ((insert-default-directory t)
531 (default-directory (expand-file-name "~/"))
532 (def mail-personal-alias-file))
533 (read-file-name
534 (format "Read additional aliases from file (default %s): "
535 def)
536 default-directory
537 (expand-file-name def default-directory)
538 t))))
539 (build-mail-abbrevs file))
540
541 (defun rebuild-mail-abbrevs (&optional file)
542 "Rebuild all the mail aliases from the given file."
543 (interactive (list
544 (let ((insert-default-directory t)
545 (default-directory (expand-file-name "~/"))
546 (def mail-personal-alias-file))
547 (read-file-name
548 (format "Read mail aliases from file (default %s): " def)
549 default-directory
550 (expand-file-name def default-directory)
551 t))))
552 (if (null file)
553 (setq file buffer-file-name))
554 (setq mail-abbrevs nil)
555 (build-mail-abbrevs file))
556
557 (defun mail-abbrev-insert-alias (&optional alias)
558 "Prompt for and insert a mail alias."
559 (interactive (progn
560 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
561 (list (completing-read "Expand alias: " mail-abbrevs nil t))))
562 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
563 (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) ""))
564 (mail-abbrev-expand-hook))
565
566 (defun mail-abbrev-complete-alias ()
567 "Perform completion on alias preceding point."
568 ;; Based on lisp.el:lisp-complete-symbol
569 (interactive)
570 (mail-abbrev-make-syntax-table)
571 (let ((end (point))
572 (beg (with-syntax-table mail-abbrev-syntax-table
573 (save-excursion
574 (backward-word 1)
575 (point)))))
576 (completion-in-region beg end mail-abbrevs)))
577
578 (defun mail-abbrev-next-line (&optional arg)
579 "Expand a mail abbrev before point, then move vertically down ARG lines.
580 This only expands an abbrev (if one is present) if called with
581 point at the end of a line, or on whitespace before the end of a line.
582
583 In terms of line motion, this behaves like `next-line', which see."
584 (interactive "p")
585 (if (looking-at "[ \t]*\n") (expand-abbrev))
586 (setq this-command 'next-line)
587 (with-no-warnings (next-line arg)))
588
589 (defun mail-abbrev-end-of-buffer (&optional arg)
590 "Expand a mail abbrev before point, then move to the end of the buffer.
591 This only expands an abbrev (if one is present) if called with
592 point at the end of a line, or on whitespace before the end of a line.
593
594 In other respects, this behaves like `end-of-buffer', which see."
595 (interactive "P")
596 (if (looking-at "[ \t]*\n") (expand-abbrev))
597 (setq this-command 'end-of-buffer)
598 (with-no-warnings (end-of-buffer arg)))
599
600 (eval-after-load "sendmail"
601 '(progn
602 (define-key mail-mode-map "\C-c\C-a" 'mail-abbrev-insert-alias)
603 (define-key mail-mode-map "\e\t" ; like lisp-complete-symbol
604 'mail-abbrev-complete-alias)))
605
606 ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
607 ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
608
609 (provide 'mailabbrev)
610
611 ;;; mailabbrev.el ends here