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