1 ;;; mailabbrev.el --- abbrev-expansion of mail aliases
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1996, 1997, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Jamie Zawinski <jwz@lucid.com>, now <jwz@jwz.org>
11 ;; This file is part of GNU Emacs.
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, or (at your option)
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
31 ;; field, word-abbrevs are defined for each of your mail aliases. These
32 ;; aliases will be defined from your .mailrc file (or the file specified by
33 ;; `mail-personal-alias-file') if it exists. Your mail aliases will
34 ;; expand any time you type a word-delimiter at the end of an abbreviation.
36 ;; What you see is what you get: if mailabbrev is in use when you type
37 ;; a name, and the name does not expand, you know it is not an abbreviation.
38 ;; However, if you yank abbreviations into the headers
39 ;; in a way that bypasses the check for abbreviations,
40 ;; they are expanded (but not visibly) when you send the message.
42 ;; Your mail alias abbrevs will be in effect only when the point is in an
43 ;; appropriate header field. When in the body of the message, or other
44 ;; header fields, the mail aliases will not expand. Rather, the normal
45 ;; mode-specific abbrev table will be used if
46 ;; defined. So if you use mail-mode specific abbrevs, this code will not
47 ;; adversely affect you. You can control which header fields the abbrevs
48 ;; are used in by changing the variable mail-abbrev-mode-regexp.
50 ;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word
51 ;; boundaries; also, header continuation-lines will be properly indented.
53 ;; You can also insert a mail alias with mail-abbrev-insert-alias
54 ;; (bound to C-c C-a), which prompts you for an alias (with completion)
55 ;; and inserts its expansion at point.
57 ;; This file fixes a bug in the old system which prohibited your .mailrc
58 ;; file from having lines like
60 ;; alias someone "John Doe <doe@quux.com>"
62 ;; That is, if you want an address to have embedded spaces, simply surround it
63 ;; with double-quotes. This is necessary because the format of the .mailrc
64 ;; file bogusly uses spaces as address delimiters. The following line defines
65 ;; an alias which expands to three addresses:
67 ;; alias foobar addr-1 addr-2 "address three <addr-3>"
69 ;; (This is bogus because mail-delivery programs want commas, not spaces,
70 ;; but that's what the file format is, so we have to live with it.)
72 ;; If you like, you can call the function define-mail-abbrev to define your
73 ;; mail aliases instead of using a .mailrc file. When you call it in this
74 ;; way, addresses are separated by commas.
76 ;; CAVEAT: This works on most Sun systems; I have been told that some versions
77 ;; of /bin/mail do not understand double-quotes in the .mailrc file. So you
78 ;; should make sure your version does before including verbose addresses like
79 ;; this. One solution to this, if you are on a system whose /bin/mail doesn't
80 ;; work that way, (and you still want to be able to /bin/mail to send mail in
81 ;; addition to emacs) is to define minimal aliases (without full names) in
82 ;; your .mailrc file, and use define-mail-abbrev to redefine them when sending
83 ;; mail from emacs; this way, mail sent from /bin/mail will work, and mail
84 ;; sent from emacs will be pretty.
86 ;; Aliases in the mailrc file may be nested. If you define aliases like
87 ;; alias group1 fred ethel
88 ;; alias group2 larry curly moe
89 ;; alias everybody group1 group2
90 ;; Then when you type "everybody" on the To: line, it will be expanded to
91 ;; fred, ethyl, larry, curly, moe
93 ;; Aliases may also contain forward references; the alias of "everybody" can
94 ;; precede the aliases of "group1" and "group2".
96 ;; This code also understands the "source" .mailrc command, for reading
97 ;; aliases from some other file as well.
99 ;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs
100 ;; normally cannot contain hyphens, but this code works around that for the
101 ;; specific case of mail-alias word-abbrevs.
103 ;; To read in the contents of another .mailrc-type file from emacs, use the
104 ;; command Meta-X merge-mail-abbrevs. The rebuild-mail-abbrevs command is
105 ;; similar, but will delete existing aliases first.
107 ;; If you would like your aliases to be expanded when you type M-> or ^N to
108 ;; move out of the mail-header into the message body (instead of having to
109 ;; type SPC at the end of the abbrev before moving away) then you can do
114 ;; (define-key mail-mode-map [remap next-line] 'mail-abbrev-next-line)
115 ;; (define-key mail-mode-map [remap end-of-buffer] 'mail-abbrev-end-of-buffer)))
117 ;; If you want multiple addresses separated by a string other than ", " then
118 ;; you can set the variable mail-alias-separator-string to it. This has to
119 ;; be a comma bracketed by whitespace if you want any kind of reasonable
122 ;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
123 ;; Noah Friedman for suggestions and bug reports.
125 ;; To use this package, do (add-hook 'mail-mode-hook 'mail-abbrevs-setup).
132 (defgroup mail-abbrev nil
133 "Expand mail aliases as abbrevs, in certain mail headers."
137 (define-minor-mode mail-abbrevs-mode
138 "Non-nil means expand mail aliases as abbrevs, in certain message headers."
142 (if mail-abbrevs-mode
(mail-abbrevs-enable) (mail-abbrevs-disable)))
144 (defcustom mail-abbrevs-only nil
145 "Non-nil means only mail abbrevs should expand automatically.
146 Other abbrevs expand only when you explicitly use `expand-abbrev'."
150 ;; originally defined in sendmail.el - used to be an alist, now is a table.
151 (defvar mail-abbrevs nil
152 "Word-abbrev table of mail address aliases.
153 If this is nil, it means the aliases have not yet been initialized and
154 should be read from the .mailrc file. (This is distinct from there being
155 no aliases, which is represented by this being a table with no entries.)")
157 (defvar mail-abbrev-modtime nil
158 "The modification time of your mail alias file when it was last examined.")
160 (defun mail-abbrevs-sync-aliases ()
161 (when mail-personal-alias-file
162 (if (file-exists-p mail-personal-alias-file
)
163 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file
))))
164 (if (not (equal mail-abbrev-modtime modtime
))
166 (setq mail-abbrev-modtime modtime
)
167 (build-mail-abbrevs)))))))
170 (defun mail-abbrevs-setup ()
171 "Initialize use of the `mailabbrev' package."
172 (if (and (not (vectorp mail-abbrevs
))
173 (file-exists-p mail-personal-alias-file
))
175 (setq mail-abbrev-modtime
176 (nth 5 (file-attributes mail-personal-alias-file
)))
177 (build-mail-abbrevs)))
178 (mail-abbrevs-sync-aliases)
179 (add-hook 'abbrev-expand-functions
'mail-abbrev-expand-wrapper nil t
)
182 (defun mail-abbrevs-enable ()
183 (add-hook 'mail-mode-hook
'mail-abbrevs-setup
))
185 (defun mail-abbrevs-disable ()
186 "Turn off use of the `mailabbrev' package."
187 (remove-hook 'mail-mode-hook
'mail-abbrevs-setup
)
188 (abbrev-mode (if (default-value 'abbrev-mode
) 1 -
1)))
191 (defun build-mail-abbrevs (&optional file recursivep
)
192 "Read mail aliases from personal mail alias file and set `mail-abbrevs'.
193 By default this is the file specified by `mail-personal-alias-file'."
194 (setq file
(expand-file-name (or file mail-personal-alias-file
)))
195 (if (vectorp mail-abbrevs
)
197 (setq mail-abbrevs nil
)
198 (define-abbrev-table 'mail-abbrevs
'()))
199 (message "Parsing %s..." file
)
201 (buffer-disable-undo)
202 (cond ((get-file-buffer file
)
203 (insert (with-current-buffer (get-file-buffer file
)
204 (buffer-substring (point-min) (point-max)))))
205 ((not (file-exists-p file
)))
206 (t (insert-file-contents file
)))
207 ;; Don't lose if no final newline.
208 (goto-char (point-max))
209 (or (eq (preceding-char) ?
\n) (newline))
210 (goto-char (point-min))
211 ;; Delete comments from the file
212 (while (search-forward "# " nil t
)
213 (let ((p (- (point) 2)))
215 (delete-region p
(point))))
216 (goto-char (point-min))
217 ;; handle "\\\n" continuation lines
220 (if (= (preceding-char) ?
\\)
221 (progn (delete-char -
1) (delete-char 1) (insert ?\
))
223 (goto-char (point-min))
224 (while (re-search-forward
225 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t
)
227 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
231 (substitute-in-file-name
232 (buffer-substring (match-beginning 1) (match-end 1)))
234 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
235 (let* ((name (buffer-substring
236 (match-beginning 1) (match-end 1)))
237 (start (progn (skip-chars-forward " \t") (point))))
239 ;; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
242 (buffer-substring start
(point))
244 ;; Resolve forward references in .mailrc file.
245 ;; This would happen automatically before the first abbrev was
246 ;; expanded, but why not do it now.
247 (or recursivep
(mail-resolve-all-aliases))
249 (message "Parsing %s... done" file
))
251 (defvar mail-alias-separator-string
", "
252 "*A string inserted between addresses in multi-address mail aliases.
253 This has to contain a comma, so \", \" is a reasonable value. You might
254 also want something like \",\\n \" to get each address on its own line.")
256 ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases
257 ;; to be called before expanding abbrevs if it's necessary.
258 (defvar mail-abbrev-aliases-need-to-be-resolved t
)
260 ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with
261 ;; stuff parsed from the .mailrc file.
264 (defun define-mail-abbrev (name definition
&optional from-mailrc-file
)
265 "Define NAME as a mail alias abbrev that translates to DEFINITION.
266 If DEFINITION contains multiple addresses, separate them with commas."
267 ;; When this is called from build-mail-abbrevs, the third argument is
268 ;; true, and we do some evil space->comma hacking like /bin/mail does.
269 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
270 ;; Read the defaults first, if we have not done so.
271 (unless (vectorp mail-abbrevs
) (build-mail-abbrevs))
272 ;; strip garbage from front and end
273 (if (string-match "\\`[ \t\n,]+" definition
)
274 (setq definition
(substring definition
(match-end 0))))
275 (if (string-match "[ \t\n,]+\\'" definition
)
276 (setq definition
(substring definition
0 (match-beginning 0))))
278 (L (length definition
))
279 (start (if (> L
0) 0))
282 ;; If we're reading from the mailrc file, then addresses are delimited
283 ;; by spaces, and addresses with embedded spaces must be surrounded by
284 ;; double-quotes. Otherwise, addresses are separated by commas.
286 (if (eq ?
\" (aref definition start
))
287 (setq start
(1+ start
)
288 end
(string-match "\"[ \t,]*" definition start
))
289 (setq end
(string-match "[ \t,]+" definition start
)))
290 (setq end
(string-match "[ \t\n,]*,[ \t\n,]*" definition start
)))
291 (let ((tem (substring definition start end
)))
292 ;; Advance the loop past this address.
296 ;; If the full name contains a problem character, quote it.
297 (when (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" tem
)
298 (if (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
299 (match-string 1 tem
))
300 (setq tem
(replace-regexp-in-string
301 "\\(.+?\\)[ \t]*\\(<.*>\\)" "\"\\1\" \\2"
304 (setq definition
(mapconcat (function identity
)
306 mail-alias-separator-string
)))
307 (setq mail-abbrev-aliases-need-to-be-resolved t
)
308 (setq name
(downcase name
))
309 ;; use an abbrev table instead of an alist for mail-abbrevs.
310 (let ((abbrevs-changed abbrevs-changed
)) ; protect this from being changed.
311 (define-abbrev mail-abbrevs name definition
'mail-abbrev-expand-hook
0 t
)))
314 (defun mail-resolve-all-aliases ()
315 "Resolve all forward references in the mail aliases table."
316 (if mail-abbrev-aliases-need-to-be-resolved
318 ;; (message "Resolving mail aliases...")
319 (if (vectorp mail-abbrevs
)
320 (mapatoms (function mail-resolve-all-aliases-1
) mail-abbrevs
))
321 (setq mail-abbrev-aliases-need-to-be-resolved nil
)
322 ;; (message "Resolving mail aliases... done.")
325 (defun mail-resolve-all-aliases-1 (sym &optional so-far
)
326 (if (memq sym so-far
)
327 (error "mail alias loop detected: %s"
328 (mapconcat 'symbol-name
(cons sym so-far
) " <- ")))
329 (let ((definition (and (boundp sym
) (symbol-value sym
))))
334 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start
)))
335 (setq result
(cons (substring definition start end
) result
)
336 start
(and end
(match-end 0)))))
338 (mapconcat (function (lambda (x)
339 (or (mail-resolve-all-aliases-1
340 (intern-soft (downcase x
) mail-abbrevs
)
344 mail-alias-separator-string
))
345 (set sym definition
))))
349 (defun mail-abbrev-expand-hook ()
350 "For use as the fourth arg to `define-abbrev'.
351 After expanding a mail-abbrev, if Auto Fill mode is on and we're past the
352 fill-column, break the line at the previous comma, and indent the next line."
353 ;; Disable abbrev mode to avoid recursion in indent-relative expanding
354 ;; part of the abbrev expansion as an abbrev itself.
355 (let ((abbrev-mode nil
))
362 (while (and auto-fill-function
363 (>= (current-column) fill-column
)
364 (search-backward "," bol t
))
366 (forward-char 1) ; Now we are just past the comma.
368 (delete-horizontal-space)
371 (setq fp
(buffer-substring p
(point)))
372 ;; Go to the end of the new line.
374 (if (> (current-column) fill-column
)
375 ;; It's still too long; do normal auto-fill.
376 (let ((fill-prefix (or fp
"\t")))
378 ;; Resume the search.
382 ;;; Syntax tables and abbrev-expansion
384 (defvar mail-abbrev-mode-regexp
385 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
386 "*Regexp to select mail-headers in which mail abbrevs should be expanded.
387 This string will be handed to `looking-at' with point at the beginning
388 of the current line; if it matches, abbrev mode will be turned on, otherwise
389 it will be turned off. (You don't need to worry about continuation lines.)
390 This should be set to match those mail fields in which you want abbreviations
393 (defvar mail-abbrev-syntax-table nil
394 "The syntax-table used for abbrev-expansion purposes.
395 This is not actually made the current syntax table of the buffer, but
396 simply controls the set of characters which may be a part of the name
397 of a mail alias. The value is set up, buffer-local, when first needed.")
399 (defun mail-abbrev-make-syntax-table ()
400 (make-local-variable 'mail-abbrev-syntax-table
)
401 (unless mail-abbrev-syntax-table
402 (let ((tab (copy-syntax-table (syntax-table)))
403 (_ (aref (standard-syntax-table) ?_
))
404 (w (aref (standard-syntax-table) ?w
)))
406 (function (lambda (key value
)
408 ;; Fetch the inherited value
409 (setq value
(aref tab key
)))
411 (set-char-table-range tab key w
))))
413 (modify-syntax-entry ?
@ "w" tab
)
414 (modify-syntax-entry ?%
"w" tab
)
415 (modify-syntax-entry ?
! "w" tab
)
416 (modify-syntax-entry ?.
"w" tab
)
417 (modify-syntax-entry ?_
"w" tab
)
418 (modify-syntax-entry ?-
"w" tab
)
419 (setq mail-abbrev-syntax-table tab
))))
421 (defun mail-abbrev-in-expansion-header-p ()
422 "Whether point is in a mail-address header field."
423 (let ((case-fold-search t
))
425 ;; we are on an appropriate header line...
427 (unless (eobp) (forward-char 1))
428 (re-search-backward "^[^ \t]" nil
'move
)
429 ;; are we at the front of an appropriate header line?
430 (looking-at mail-abbrev-mode-regexp
))
432 ;; ...and are we in the headers?
440 (defun mail-abbrev-expand-wrapper (expand)
441 (if (and mail-abbrevs
(not (eq mail-abbrevs t
)))
442 (if (mail-abbrev-in-expansion-header-p)
444 ;; We are in a To: (or CC:, or whatever) header, and
445 ;; should use word-abbrevs to expand mail aliases.
446 (let ((local-abbrev-table mail-abbrevs
))
448 ;; Before anything else, resolve aliases if they need it.
449 (and mail-abbrev-aliases-need-to-be-resolved
450 (mail-resolve-all-aliases))
452 ;; Now proceed with the abbrev section.
453 ;; - We already installed mail-abbrevs as the abbrev table.
454 ;; - Then install the mail-abbrev-syntax-table, which
455 ;; temporarily marks all of the
456 ;; non-alphanumeric-atom-characters (the "_"
457 ;; syntax ones) as being normal word-syntax. We do this
458 ;; because the C code for expand-abbrev only works on words,
459 ;; and we want these characters to be considered words for
460 ;; the purpose of abbrev expansion.
461 ;; - Then we call the expand function, to do
462 ;; the abbrev expansion with the above syntax table.
464 (mail-abbrev-make-syntax-table)
466 ;; If the character just typed was non-alpha-symbol-syntax,
467 ;; then don't expand the abbrev now (that is, don't expand
468 ;; when the user types -.) Check the character's syntax in
469 ;; the usual syntax table.
471 (or (and (integerp last-command-char
)
472 ;; Some commands such as M-> may want to expand first.
473 (equal this-command
'self-insert-command
)
474 (or (eq (char-syntax last-command-char
) ?_
)
475 ;; Don't expand on @.
476 (memq last-command-char
'(?
@ ?. ?% ?
! ?_ ?-
))))
477 ;; Use this table so that abbrevs can have hyphens in them.
478 (with-syntax-table mail-abbrev-syntax-table
481 (if (or (not mail-abbrevs-only
)
482 (eq this-command
'expand-abbrev
))
483 ;; We're not in a mail header where mail aliases should
484 ;; be expanded, then use the normal mail-mode abbrev table
485 ;; (if any) and the normal mail-mode syntax table.
487 ;; This is not a mail abbrev, and we should not expand it.
488 ;; Don't expand anything.
490 ;; No mail-abbrevs at all, do the normal thing.
495 (defun merge-mail-abbrevs (file)
496 "Merge mail aliases from the given file with existing ones."
498 (let ((insert-default-directory t
)
499 (default-directory (expand-file-name "~/"))
500 (def mail-personal-alias-file
))
502 (format "Read additional aliases from file (default %s): "
505 (expand-file-name def default-directory
)
507 (build-mail-abbrevs file
))
509 (defun rebuild-mail-abbrevs (&optional file
)
510 "Rebuild all the mail aliases from the given file."
512 (let ((insert-default-directory t
)
513 (default-directory (expand-file-name "~/"))
514 (def mail-personal-alias-file
))
516 (format "Read mail aliases from file (default %s): " def
)
518 (expand-file-name def default-directory
)
521 (setq file buffer-file-name
))
522 (setq mail-abbrevs nil
)
523 (build-mail-abbrevs file
))
525 (defun mail-abbrev-insert-alias (&optional alias
)
526 "Prompt for and insert a mail alias."
528 (if (not (vectorp mail-abbrevs
)) (mail-abbrevs-setup))
529 (list (completing-read "Expand alias: " mail-abbrevs nil t
))))
530 (if (not (vectorp mail-abbrevs
)) (mail-abbrevs-setup))
531 (insert (or (and alias
(symbol-value (intern-soft alias mail-abbrevs
))) ""))
532 (mail-abbrev-expand-hook))
534 (defun mail-abbrev-complete-alias ()
535 "Perform completion on alias preceding point."
536 ;; Based on lisp.el:lisp-complete-symbol
538 (mail-abbrev-make-syntax-table)
540 (beg (with-syntax-table mail-abbrev-syntax-table
544 (alias (buffer-substring beg end
))
545 (completion (try-completion alias mail-abbrevs
)))
546 (cond ((eq completion t
)
547 (message "%s" alias
)) ; confirm
549 (error "[Can't complete \"%s\"]" alias
)) ; (message ...) (ding)
550 ((not (string= completion alias
))
551 (delete-region beg end
)
553 (t (with-output-to-temp-buffer "*Completions*"
554 (display-completion-list
556 (message "Making completion list...")
557 (all-completions alias mail-abbrevs
)
558 (message "Making completion list...done"))
561 (defun mail-abbrev-next-line (&optional arg
)
562 "Expand any mail abbrev, then move cursor vertically down ARG lines.
563 If there is no character in the target line exactly under the current column,
564 the cursor is positioned after the character in that line which spans this
565 column, or at the end of the line if it is not long enough.
566 If there is no line in the buffer after this one,
567 a newline character is inserted to create a line
568 and the cursor moves to that line.
570 The command \\[set-goal-column] can be used to create
571 a semipermanent goal column to which this command always moves.
572 Then it does not try to move vertically. This goal column is stored
573 in `goal-column', which is nil when there is none.
575 If you are thinking of using this in a Lisp program, consider
576 using `forward-line' instead. It is usually easier to use
577 and more reliable (no dependence on goal column, etc.)."
579 (if (looking-at "[ \t]*\n") (expand-abbrev))
580 (setq this-command
'next-line
)
581 (with-no-warnings (next-line arg
)))
583 (defun mail-abbrev-end-of-buffer (&optional arg
)
584 "Expand any mail abbrev, then move point to end of buffer.
585 Leave mark at previous position.
586 With arg N, put point N/10 of the way from the true end.
588 Don't use this command in Lisp programs!
589 \(goto-char (point-max)) is faster and avoids clobbering the mark."
591 (if (looking-at "[ \t]*\n") (expand-abbrev))
592 (setq this-command
'end-of-buffer
)
594 (end-of-buffer arg
)))
596 (eval-after-load "sendmail"
598 (define-key mail-mode-map
"\C-c\C-a" 'mail-abbrev-insert-alias
)
599 (define-key mail-mode-map
"\e\t" ; like lisp-complete-symbol
600 'mail-abbrev-complete-alias
)))
602 ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
603 ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
605 (provide 'mailabbrev
)
607 ;; arch-tag: 5aa2d901-73f8-4ad7-b73c-4802282ad2ff
608 ;;; mailabbrev.el ends here