Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / mail / mailabbrev.el
CommitLineData
55535639 1;;; mailabbrev.el --- abbrev-expansion of mail aliases
275da787 2
f2e3589a 3;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1996, 1997, 2000, 2001,
5df4f04c 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
a4d9b7bc 5;; Free Software Foundation, Inc.
275da787 6
4c120732 7;; Author: Jamie Zawinski <jwz@lucid.com; now jwz@jwz.org>
fcc31755 8;; Maintainer: FSF
275da787
RM
9;; Created: 19 Oct 90
10;; Keywords: mail
11
b578f267 12;; This file is part of GNU Emacs.
b01c3008 13
b1fc2b50 14;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 15;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
b01c3008 18
b578f267
EN
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
b01c3008 23
b578f267 24;; You should have received a copy of the GNU General Public License
b1fc2b50 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
f6babbac 32;; `mail-personal-alias-file') if it exists. Your mail aliases will
b578f267
EN
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
fba75b6b 44;; mode-specific 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;;
85d0e9ed 52;; You can also insert a mail alias with mail-abbrev-insert-alias
b578f267
EN
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
bde4c7ce 111;; 'mail-mode-hook
c0df1972 112;; (lambda ()
6b823692
AS
113;; (define-key mail-mode-map [remap next-line] 'mail-abbrev-next-line)
114;; (define-key mail-mode-map [remap end-of-buffer] 'mail-abbrev-end-of-buffer)))
b578f267
EN
115;;
116;; If you want multiple addresses separated by a string other than ", " then
117;; you can set the variable mail-alias-separator-string to it. This has to
118;; be a comma bracketed by whitespace if you want any kind of reasonable
fffa137c 119;; behavior.
b578f267
EN
120;;
121;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
122;; Noah Friedman for suggestions and bug reports.
123
bde4c7ce 124;; To use this package, do (add-hook 'mail-mode-hook 'mail-abbrevs-setup).
b01c3008 125
275da787
RM
126;;; Code:
127
d075ed70
RS
128(eval-when-compile
129 (require 'sendmail))
b01c3008 130
7d4c958f
KH
131(defgroup mail-abbrev nil
132 "Expand mail aliases as abbrevs, in certain mail headers."
133 :group 'abbrev-mode)
134
dcbb251e
SM
135;;;###autoload
136(define-minor-mode mail-abbrevs-mode
137 "Non-nil means expand mail aliases as abbrevs, in certain message headers."
138 :global t
7d4c958f 139 :group 'mail-abbrev
dcbb251e
SM
140 :version "20.3"
141 (if mail-abbrevs-mode (mail-abbrevs-enable) (mail-abbrevs-disable)))
7d4c958f
KH
142
143(defcustom mail-abbrevs-only nil
dcbb251e 144 "Non-nil means only mail abbrevs should expand automatically.
7d4c958f
KH
145Other abbrevs expand only when you explicitly use `expand-abbrev'."
146 :type 'boolean
147 :group 'mail-abbrev)
148
b01c3008 149;; originally defined in sendmail.el - used to be an alist, now is a table.
753d16a6 150(defvar mail-abbrevs nil
22f4ef2e 151 "Word-abbrev table of mail address aliases.
b01c3008
RS
152If this is nil, it means the aliases have not yet been initialized and
153should be read from the .mailrc file. (This is distinct from there being
154no aliases, which is represented by this being a table with no entries.)")
155
00bafa45
RS
156(defvar mail-abbrev-modtime nil
157 "The modification time of your mail alias file when it was last examined.")
158
159(defun mail-abbrevs-sync-aliases ()
f7e85dcf
RS
160 (when mail-personal-alias-file
161 (if (file-exists-p mail-personal-alias-file)
162 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
163 (if (not (equal mail-abbrev-modtime modtime))
164 (progn
165 (setq mail-abbrev-modtime modtime)
166 (build-mail-abbrevs)))))))
00bafa45 167
aa228418 168;;;###autoload
753d16a6 169(defun mail-abbrevs-setup ()
17804728 170 "Initialize use of the `mailabbrev' package."
753d16a6 171 (if (and (not (vectorp mail-abbrevs))
a3039e32 172 (file-exists-p mail-personal-alias-file))
00bafa45 173 (progn
0f965ba3 174 (setq mail-abbrev-modtime
00bafa45
RS
175 (nth 5 (file-attributes mail-personal-alias-file)))
176 (build-mail-abbrevs)))
177 (mail-abbrevs-sync-aliases)
dcbb251e 178 (add-hook 'abbrev-expand-functions 'mail-abbrev-expand-wrapper nil t)
b01c3008
RS
179 (abbrev-mode 1))
180
7d4c958f 181(defun mail-abbrevs-enable ()
bde4c7ce 182 (add-hook 'mail-mode-hook 'mail-abbrevs-setup))
7d4c958f
KH
183
184(defun mail-abbrevs-disable ()
185 "Turn off use of the `mailabbrev' package."
bde4c7ce 186 (remove-hook 'mail-mode-hook 'mail-abbrevs-setup)
7d4c958f
KH
187 (abbrev-mode (if (default-value 'abbrev-mode) 1 -1)))
188
22f4ef2e 189;;;###autoload
753d16a6 190(defun build-mail-abbrevs (&optional file recursivep)
a3039e32
NF
191 "Read mail aliases from personal mail alias file and set `mail-abbrevs'.
192By default this is the file specified by `mail-personal-alias-file'."
193 (setq file (expand-file-name (or file mail-personal-alias-file)))
753d16a6 194 (if (vectorp mail-abbrevs)
b01c3008 195 nil
753d16a6
RS
196 (setq mail-abbrevs nil)
197 (define-abbrev-table 'mail-abbrevs '()))
5aefeeba 198 (message "Parsing %s..." file)
dcbb251e
SM
199 (with-temp-buffer
200 (buffer-disable-undo)
201 (cond ((get-file-buffer file)
202 (insert (with-current-buffer (get-file-buffer file)
203 (buffer-substring (point-min) (point-max)))))
204 ((not (file-exists-p file)))
205 (t (insert-file-contents file)))
206 ;; Don't lose if no final newline.
207 (goto-char (point-max))
208 (or (eq (preceding-char) ?\n) (newline))
209 (goto-char (point-min))
210 ;; Delete comments from the file
211 (while (search-forward "# " nil t)
212 (let ((p (- (point) 2)))
213 (end-of-line)
214 (delete-region p (point))))
215 (goto-char (point-min))
216 ;; handle "\\\n" continuation lines
217 (while (not (eobp))
218 (end-of-line)
219 (if (= (preceding-char) ?\\)
220 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
221 (forward-char 1)))
222 (goto-char (point-min))
223 (while (re-search-forward
224 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t)
225 (beginning-of-line)
226 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
227 (progn
228 (end-of-line)
229 (build-mail-abbrevs
230 (substitute-in-file-name
231 (buffer-substring (match-beginning 1) (match-end 1)))
232 t))
233 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
234 (let* ((name (buffer-substring
235 (match-beginning 1) (match-end 1)))
236 (start (progn (skip-chars-forward " \t") (point))))
237 (end-of-line)
238 ;; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
239 (define-mail-abbrev
240 name
241 (buffer-substring start (point))
242 t))))
243 ;; Resolve forward references in .mailrc file.
244 ;; This would happen automatically before the first abbrev was
245 ;; expanded, but why not do it now.
246 (or recursivep (mail-resolve-all-aliases))
247 mail-abbrevs)
248 (message "Parsing %s... done" file))
b01c3008 249
f22cd786 250(defvar mail-alias-separator-string ", "
b01c3008 251 "*A string inserted between addresses in multi-address mail aliases.
0f965ba3 252This has to contain a comma, so \", \" is a reasonable value. You might
b01c3008
RS
253also want something like \",\\n \" to get each address on its own line.")
254
51e4ece8 255;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases
b01c3008
RS
256;; to be called before expanding abbrevs if it's necessary.
257(defvar mail-abbrev-aliases-need-to-be-resolved t)
258
753d16a6 259;; originally defined in mailalias.el ; build-mail-abbrevs calls this with
b01c3008
RS
260;; stuff parsed from the .mailrc file.
261;;
aa228418 262;;;###autoload
51e4ece8 263(defun define-mail-abbrev (name definition &optional from-mailrc-file)
92182a70 264 "Define NAME as a mail alias abbrev that translates to DEFINITION.
4bb79f82
CY
265If DEFINITION contains multiple addresses, separate them with commas.
266
267Optional argument FROM-MAILRC-FILE means that DEFINITION comes
268from a mailrc file. In that case, addresses are separated with
269spaces and addresses with embedded spaces are surrounded by
270double-quotes."
753d16a6 271 ;; When this is called from build-mail-abbrevs, the third argument is
b01c3008
RS
272 ;; true, and we do some evil space->comma hacking like /bin/mail does.
273 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
274 ;; Read the defaults first, if we have not done so.
dcbb251e 275 (unless (vectorp mail-abbrevs) (build-mail-abbrevs))
b01c3008
RS
276 ;; strip garbage from front and end
277 (if (string-match "\\`[ \t\n,]+" definition)
278 (setq definition (substring definition (match-end 0))))
279 (if (string-match "[ \t\n,]+\\'" definition)
280 (setq definition (substring definition 0 (match-beginning 0))))
4bb79f82 281 (let* ((L (length definition))
8eb6c7da 282 (start (if (> L 0) 0))
4bb79f82 283 end this-entry result)
b01c3008 284 (while start
4bb79f82
CY
285 (cond
286 (from-mailrc-file
287 ;; If we're reading from the mailrc file, addresses are
288 ;; delimited by spaces, and addresses with embedded spaces are
289 ;; surrounded by non-escaped double-quotes.
290 (if (eq ?\" (aref definition start))
291 (setq start (1+ start)
292 end (and (string-match
293 "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*"
294 definition start)
295 (match-end 1)))
296 (setq end (string-match "[ \t,]+" definition start)))
297 ;; Extract the address and advance the loop past it.
298 (setq this-entry (substring definition start end)
299 start (and end (/= (match-end 0) L) (match-end 0)))
300 ;; If the full name contains a problem character, quote it.
301 (and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
302 (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
303 (match-string 1 this-entry))
304 (setq this-entry (replace-regexp-in-string
305 "\\(.+?\\)[ \t]*\\(<.*>\\)"
306 "\"\\1\" \\2"
307 this-entry)))
308 (push this-entry result))
309 ;; When we are not reading from .mailrc, addresses are
310 ;; separated by commas. Try to accept a rfc822-like syntax.
311 ;; (Todo: extend rfc822.el to do the work for us.)
312 ((equal (string-match
313 "[ \t,]*\\(\"\\(?:[^\"]\\|[^\\]\\(?:[\\][\\]\\)*\"\\)*\"[ \t]*\
314<[-.!#$%&'*+/0-9=?A-Za-z^_`{|}~@]+>\\)[ \t,]*"
315 definition start)
316 start)
317 ;; If an entry has a valid [ "foo bar" <foo@example.com> ]
318 ;; form, use it literally . This also allows commas in the
319 ;; quoted string, e.g. [ "foo bar, jr" <foo@example.com> ]
320 (push (match-string 1 definition) result)
321 (setq start (and (/= (match-end 0) L) (match-end 0))))
322 (t
323 ;; Otherwise, read the next address by looking for a comma.
324 (setq end (string-match "[ \t\n,]*,[ \t\n]*" definition start))
325 (setq this-entry (substring definition start end))
69ba53f7 326 ;; Advance the loop past this address.
4bb79f82 327 (setq start (and end (/= (match-end 0) L) (match-end 0)))
69ba53f7 328 ;; If the full name contains a problem character, quote it.
4bb79f82
CY
329 (and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
330 (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
331 (match-string 1 this-entry))
332 (setq this-entry (replace-regexp-in-string
333 "\\(.+?\\)[ \t]*\\(<.*>\\)" "\"\\1\" \\2"
334 this-entry)))
335 (push this-entry result))))
336
b01c3008
RS
337 (setq definition (mapconcat (function identity)
338 (nreverse result)
f22cd786 339 mail-alias-separator-string)))
b01c3008
RS
340 (setq mail-abbrev-aliases-need-to-be-resolved t)
341 (setq name (downcase name))
753d16a6 342 ;; use an abbrev table instead of an alist for mail-abbrevs.
b01c3008 343 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed.
8fad672b 344 (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook 0 t)))
b01c3008
RS
345
346
347(defun mail-resolve-all-aliases ()
348 "Resolve all forward references in the mail aliases table."
349 (if mail-abbrev-aliases-need-to-be-resolved
350 (progn
351;; (message "Resolving mail aliases...")
753d16a6
RS
352 (if (vectorp mail-abbrevs)
353 (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs))
b01c3008
RS
354 (setq mail-abbrev-aliases-need-to-be-resolved nil)
355;; (message "Resolving mail aliases... done.")
356 )))
357
d7c1ec4b
RM
358(defun mail-resolve-all-aliases-1 (sym &optional so-far)
359 (if (memq sym so-far)
360 (error "mail alias loop detected: %s"
361 (mapconcat 'symbol-name (cons sym so-far) " <- ")))
b01c3008
RS
362 (let ((definition (and (boundp sym) (symbol-value sym))))
363 (if definition
364 (let ((result '())
365 (start 0))
366 (while start
367 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start)))
368 (setq result (cons (substring definition start end) result)
369 start (and end (match-end 0)))))
370 (setq definition
371 (mapconcat (function (lambda (x)
372 (or (mail-resolve-all-aliases-1
e1d24183 373 (intern-soft (downcase x) mail-abbrevs)
d7c1ec4b 374 (cons sym so-far))
b01c3008
RS
375 x)))
376 (nreverse result)
f22cd786 377 mail-alias-separator-string))
b01c3008
RS
378 (set sym definition))))
379 (symbol-value sym))
380
381
f22cd786 382(defun mail-abbrev-expand-hook ()
92182a70
RS
383 "For use as the fourth arg to `define-abbrev'.
384After expanding a mail-abbrev, if Auto Fill mode is on and we're past the
385fill-column, break the line at the previous comma, and indent the next line."
0f965ba3
RM
386 ;; Disable abbrev mode to avoid recursion in indent-relative expanding
387 ;; part of the abbrev expansion as an abbrev itself.
388 (let ((abbrev-mode nil))
389 (save-excursion
390 (let ((p (point))
391 bol comma fp)
392 (beginning-of-line)
393 (setq bol (point))
394 (goto-char p)
395 (while (and auto-fill-function
396 (>= (current-column) fill-column)
397 (search-backward "," bol t))
398 (setq comma (point))
399 (forward-char 1) ; Now we are just past the comma.
400 (insert "\n")
401 (delete-horizontal-space)
402 (setq p (point))
403 (indent-relative)
404 (setq fp (buffer-substring p (point)))
405 ;; Go to the end of the new line.
406 (end-of-line)
407 (if (> (current-column) fill-column)
408 ;; It's still too long; do normal auto-fill.
409 (let ((fill-prefix (or fp "\t")))
410 (do-auto-fill)))
411 ;; Resume the search.
412 (goto-char comma)
413 )))))
22f4ef2e
RM
414\f
415;;; Syntax tables and abbrev-expansion
b01c3008 416
0f965ba3 417(defvar mail-abbrev-mode-regexp
450b15a0 418 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
92182a70 419 "*Regexp to select mail-headers in which mail abbrevs should be expanded.
fb11ccf6 420This string will be handed to `looking-at' with point at the beginning
b01c3008
RS
421of the current line; if it matches, abbrev mode will be turned on, otherwise
422it will be turned off. (You don't need to worry about continuation lines.)
423This should be set to match those mail fields in which you want abbreviations
424turned on.")
425
d075ed70 426(defvar mail-abbrev-syntax-table nil
4d2795c3
RS
427 "The syntax-table used for abbrev-expansion purposes.
428This is not actually made the current syntax table of the buffer, but
429simply controls the set of characters which may be a part of the name
d075ed70 430of a mail alias. The value is set up, buffer-local, when first needed.")
22f4ef2e 431
cf8b484f
RS
432(defun mail-abbrev-make-syntax-table ()
433 (make-local-variable 'mail-abbrev-syntax-table)
434 (unless mail-abbrev-syntax-table
f436b1cb 435 (let ((tab (copy-syntax-table (syntax-table)))
cf8b484f
RS
436 (_ (aref (standard-syntax-table) ?_))
437 (w (aref (standard-syntax-table) ?w)))
438 (map-char-table
439 (function (lambda (key value)
6946ae5b
AS
440 (if (null value)
441 ;; Fetch the inherited value
442 (setq value (aref tab key)))
cf8b484f
RS
443 (if (equal value _)
444 (set-char-table-range tab key w))))
445 tab)
446 (modify-syntax-entry ?@ "w" tab)
f8aba1ce
MR
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 (modify-syntax-entry ?- "w" tab)
cf8b484f 452 (setq mail-abbrev-syntax-table tab))))
22f4ef2e
RM
453
454(defun mail-abbrev-in-expansion-header-p ()
455 "Whether point is in a mail-address header field."
456 (let ((case-fold-search t))
457 (and ;;
458 ;; we are on an appropriate header line...
459 (save-excursion
349deff4
GM
460 (unless (eobp) (forward-char 1))
461 (re-search-backward "^[^ \t]" nil 'move)
22f4ef2e
RM
462 ;; are we at the front of an appropriate header line?
463 (looking-at mail-abbrev-mode-regexp))
464 ;;
13aa1cde 465 ;; ...and are we in the headers?
8fad672b
RS
466 (< (point)
467 (save-restriction
468 (widen)
469 (save-excursion
470 (rfc822-goto-eoh)
471 (point)))))))
22f4ef2e 472
dcbb251e
SM
473(defun mail-abbrev-expand-wrapper (expand)
474 (if (and mail-abbrevs (not (eq mail-abbrevs t)))
475 (if (mail-abbrev-in-expansion-header-p)
476
477 ;; We are in a To: (or CC:, or whatever) header, and
478 ;; should use word-abbrevs to expand mail aliases.
479 (let ((local-abbrev-table mail-abbrevs))
480
481 ;; Before anything else, resolve aliases if they need it.
482 (and mail-abbrev-aliases-need-to-be-resolved
483 (mail-resolve-all-aliases))
484
485 ;; Now proceed with the abbrev section.
486 ;; - We already installed mail-abbrevs as the abbrev table.
487 ;; - Then install the mail-abbrev-syntax-table, which
488 ;; temporarily marks all of the
489 ;; non-alphanumeric-atom-characters (the "_"
490 ;; syntax ones) as being normal word-syntax. We do this
491 ;; because the C code for expand-abbrev only works on words,
492 ;; and we want these characters to be considered words for
493 ;; the purpose of abbrev expansion.
494 ;; - Then we call the expand function, to do
495 ;; the abbrev expansion with the above syntax table.
496
497 (mail-abbrev-make-syntax-table)
498
499 ;; If the character just typed was non-alpha-symbol-syntax,
500 ;; then don't expand the abbrev now (that is, don't expand
501 ;; when the user types -.) Check the character's syntax in
502 ;; the usual syntax table.
503
a4d9b7bc 504 (or (and (integerp last-command-event)
dcbb251e
SM
505 ;; Some commands such as M-> may want to expand first.
506 (equal this-command 'self-insert-command)
a4d9b7bc 507 (or (eq (char-syntax last-command-event) ?_)
dcbb251e 508 ;; Don't expand on @.
a4d9b7bc 509 (memq last-command-event '(?@ ?. ?% ?! ?_ ?-))))
dcbb251e
SM
510 ;; Use this table so that abbrevs can have hyphens in them.
511 (with-syntax-table mail-abbrev-syntax-table
512 (funcall expand))))
513
514 (if (or (not mail-abbrevs-only)
515 (eq this-command 'expand-abbrev))
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 (funcall expand)
520 ;; This is not a mail abbrev, and we should not expand it.
521 ;; Don't expand anything.
522 nil))
523 ;; No mail-abbrevs at all, do the normal thing.
524 (funcall expand)))
22f4ef2e
RM
525\f
526;;; utilities
b01c3008 527
753d16a6 528(defun merge-mail-abbrevs (file)
b01c3008
RS
529 "Merge mail aliases from the given file with existing ones."
530 (interactive (list
531 (let ((insert-default-directory t)
532 (default-directory (expand-file-name "~/"))
a3039e32 533 (def mail-personal-alias-file))
b01c3008 534 (read-file-name
5b76833f 535 (format "Read additional aliases from file (default %s): "
b01c3008
RS
536 def)
537 default-directory
538 (expand-file-name def default-directory)
539 t))))
753d16a6 540 (build-mail-abbrevs file))
b01c3008 541
cf376527 542(defun rebuild-mail-abbrevs (&optional file)
b01c3008
RS
543 "Rebuild all the mail aliases from the given file."
544 (interactive (list
545 (let ((insert-default-directory t)
546 (default-directory (expand-file-name "~/"))
a3039e32 547 (def mail-personal-alias-file))
b01c3008 548 (read-file-name
5b76833f 549 (format "Read mail aliases from file (default %s): " def)
b01c3008
RS
550 default-directory
551 (expand-file-name def default-directory)
552 t))))
cf376527
RS
553 (if (null file)
554 (setq file buffer-file-name))
753d16a6
RS
555 (setq mail-abbrevs nil)
556 (build-mail-abbrevs file))
49116ac0 557
85d0e9ed 558(defun mail-abbrev-insert-alias (&optional alias)
22f4ef2e 559 "Prompt for and insert a mail alias."
c540863c 560 (interactive (progn
753d16a6
RS
561 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
562 (list (completing-read "Expand alias: " mail-abbrevs nil t))))
563 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
0eef787b
RS
564 (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) ""))
565 (mail-abbrev-expand-hook))
753d16a6 566
85d0e9ed
GM
567(defun mail-abbrev-complete-alias ()
568 "Perform completion on alias preceding point."
569 ;; Based on lisp.el:lisp-complete-symbol
570 (interactive)
da00bb18 571 (mail-abbrev-make-syntax-table)
e2d4ea5a
SM
572 (let ((end (point))
573 (beg (with-syntax-table mail-abbrev-syntax-table
574 (save-excursion
575 (backward-word 1)
576 (point)))))
577 (completion-in-region beg end mail-abbrevs)))
85d0e9ed 578
753d16a6 579(defun mail-abbrev-next-line (&optional arg)
1fa1ea7d
GM
580 "Expand a mail abbrev before point, then move vertically down ARG lines.
581This only expands an abbrev (if one is present) if called with
582point at the end of a line, or on whitespace before the end of a line.
583
584In terms of line motion, this behaves like `next-line', which see."
22f4ef2e 585 (interactive "p")
d7c1ec4b
RM
586 (if (looking-at "[ \t]*\n") (expand-abbrev))
587 (setq this-command 'next-line)
91dd4dc4 588 (with-no-warnings (next-line arg)))
22f4ef2e 589
753d16a6 590(defun mail-abbrev-end-of-buffer (&optional arg)
1fa1ea7d
GM
591 "Expand a mail abbrev before point, then move to the end of the buffer.
592This only expands an abbrev (if one is present) if called with
593point at the end of a line, or on whitespace before the end of a line.
753d16a6 594
1fa1ea7d 595In other respects, this behaves like `end-of-buffer', which see."
650d6486 596 (interactive "P")
d7c1ec4b
RM
597 (if (looking-at "[ \t]*\n") (expand-abbrev))
598 (setq this-command 'end-of-buffer)
1fa1ea7d 599 (with-no-warnings (end-of-buffer arg)))
22f4ef2e 600
f526e65a
RS
601(eval-after-load "sendmail"
602 '(progn
603 (define-key mail-mode-map "\C-c\C-a" 'mail-abbrev-insert-alias)
604 (define-key mail-mode-map "\e\t" ; like lisp-complete-symbol
605 'mail-abbrev-complete-alias)))
22f4ef2e 606
753d16a6
RS
607;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
608;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
b01c3008 609
3e1b7a46 610(provide 'mailabbrev)
b578f267 611
dcbb251e 612;; arch-tag: 5aa2d901-73f8-4ad7-b73c-4802282ad2ff
55535639 613;;; mailabbrev.el ends here