1 ;;; mailabbrev.el --- abbrev-expansion of mail aliases.
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Maintainer: Jamie Zawinski <jwz@lucid.com>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
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.
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.
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
44 ;; mode-specific abbrev table (mail-mode-abbrev-table) will be used if
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.
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.
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.
56 ;; This file fixes a bug in the old system which prohibited your .mailrc
57 ;; file from having lines like
59 ;; alias someone "John Doe <doe@quux.com>"
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:
66 ;; alias foobar addr-1 addr-2 "address three <addr-3>"
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.)
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.
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.
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
92 ;; Aliases may also contain forward references; the alias of "everybody" can
93 ;; precede the aliases of "group1" and "group2".
95 ;; This code also understands the "source" .mailrc command, for reading
96 ;; aliases from some other file as well.
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.
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.
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
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)))
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
123 ;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
124 ;; Noah Friedman for suggestions and bug reports.
126 ;; To use this package, do (add-hook 'mail-setup-hook 'mail-abbrevs-setup).
132 ;; originally defined in sendmail.el - used to be an alist, now is a table.
133 (defvar mail-abbrevs nil
134 "Word-abbrev table of mail address aliases.
135 If this is nil, it means the aliases have not yet been initialized and
136 should be read from the .mailrc file. (This is distinct from there being
137 no aliases, which is represented by this being a table with no entries.)")
140 (defun mail-abbrevs-setup ()
141 (if (and (not (vectorp mail-abbrevs
))
142 (file-exists-p mail-personal-alias-file
))
143 (build-mail-abbrevs))
144 (make-local-hook 'pre-abbrev-expand-hook
)
145 (add-hook 'pre-abbrev-expand-hook
'sendmail-pre-abbrev-expand-hook
150 (defun build-mail-abbrevs (&optional file recursivep
)
151 "Read mail aliases from personal mail alias file and set `mail-abbrevs'.
152 By default this is the file specified by `mail-personal-alias-file'."
153 (setq file
(expand-file-name (or file mail-personal-alias-file
)))
154 (if (vectorp mail-abbrevs
)
156 (setq mail-abbrevs nil
)
157 (define-abbrev-table 'mail-abbrevs
'()))
158 (message "Parsing %s..." file
)
160 (obuf (current-buffer)))
163 (setq buffer
(generate-new-buffer "mailrc"))
164 (buffer-disable-undo buffer
)
166 (cond ((get-file-buffer file
)
167 (insert (save-excursion
168 (set-buffer (get-file-buffer file
))
169 (buffer-substring (point-min) (point-max)))))
170 ((not (file-exists-p file
)))
171 (t (insert-file-contents file
)))
172 ;; Don't lose if no final newline.
173 (goto-char (point-max))
174 (or (eq (preceding-char) ?
\n) (newline))
175 (goto-char (point-min))
176 ;; Delete comments from the file
177 (while (search-forward "# " nil t
)
178 (let ((p (- (point) 2)))
180 (delete-region p
(point))))
181 (goto-char (point-min))
182 ;; handle "\\\n" continuation lines
185 (if (= (preceding-char) ?
\\)
186 (progn (delete-char -
1) (delete-char 1) (insert ?\
))
188 (goto-char (point-min))
189 (while (re-search-forward
190 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t
)
192 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
196 (substitute-in-file-name
197 (buffer-substring (match-beginning 1) (match-end 1)))
199 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
200 (let* ((name (buffer-substring
201 (match-beginning 1) (match-end 1)))
202 (start (progn (skip-chars-forward " \t") (point))))
204 ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
207 (buffer-substring start
(point))
209 ;; Resolve forward references in .mailrc file.
210 ;; This would happen automatically before the first abbrev was
211 ;; expanded, but why not do it now.
212 (or recursivep
(mail-resolve-all-aliases))
214 (if buffer
(kill-buffer buffer
))
216 (message "Parsing %s... done" file
))
218 (defvar mail-alias-separator-string
", "
219 "*A string inserted between addresses in multi-address mail aliases.
220 This has to contain a comma, so \", \" is a reasonable value. You might
221 also want something like \",\\n \" to get each address on its own line.")
223 ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases
224 ;; to be called before expanding abbrevs if it's necessary.
225 (defvar mail-abbrev-aliases-need-to-be-resolved t
)
227 ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with
228 ;; stuff parsed from the .mailrc file.
231 (defun define-mail-abbrev (name definition
&optional from-mailrc-file
)
232 "Define NAME as a mail alias abbrev that translates to DEFINITION.
233 If DEFINITION contains multiple addresses, separate them with commas."
234 ;; When this is called from build-mail-abbrevs, the third argument is
235 ;; true, and we do some evil space->comma hacking like /bin/mail does.
236 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
237 ;; Read the defaults first, if we have not done so.
238 (if (vectorp mail-abbrevs
)
240 (setq mail-abbrevs nil
)
241 (define-abbrev-table 'mail-abbrevs
'())
242 (if (file-exists-p mail-personal-alias-file
)
243 (build-mail-abbrevs)))
244 ;; strip garbage from front and end
245 (if (string-match "\\`[ \t\n,]+" definition
)
246 (setq definition
(substring definition
(match-end 0))))
247 (if (string-match "[ \t\n,]+\\'" definition
)
248 (setq definition
(substring definition
0 (match-beginning 0))))
250 (L (length definition
))
251 (start (if (> L
0) 0))
254 ;; If we're reading from the mailrc file, then addresses are delimited
255 ;; by spaces, and addresses with embedded spaces must be surrounded by
256 ;; double-quotes. Otherwise, addresses are separated by commas.
258 (if (eq ?
\" (aref definition start
))
259 (setq start
(1+ start
)
260 end
(string-match "\"[ \t,]*" definition start
))
261 (setq end
(string-match "[ \t,]+" definition start
)))
262 (setq end
(string-match "[ \t\n,]*,[ \t\n,]*" definition start
)))
263 (setq result
(cons (substring definition start end
) result
))
267 (setq definition
(mapconcat (function identity
)
269 mail-alias-separator-string
)))
270 (setq mail-abbrev-aliases-need-to-be-resolved t
)
271 (setq name
(downcase name
))
272 ;; use an abbrev table instead of an alist for mail-abbrevs.
273 (let ((abbrevs-changed abbrevs-changed
)) ; protect this from being changed.
274 (define-abbrev mail-abbrevs name definition
'mail-abbrev-expand-hook
)))
277 (defun mail-resolve-all-aliases ()
278 "Resolve all forward references in the mail aliases table."
279 (if mail-abbrev-aliases-need-to-be-resolved
281 ;; (message "Resolving mail aliases...")
282 (if (vectorp mail-abbrevs
)
283 (mapatoms (function mail-resolve-all-aliases-1
) mail-abbrevs
))
284 (setq mail-abbrev-aliases-need-to-be-resolved nil
)
285 ;; (message "Resolving mail aliases... done.")
288 (defun mail-resolve-all-aliases-1 (sym &optional so-far
)
289 (if (memq sym so-far
)
290 (error "mail alias loop detected: %s"
291 (mapconcat 'symbol-name
(cons sym so-far
) " <- ")))
292 (let ((definition (and (boundp sym
) (symbol-value sym
))))
297 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start
)))
298 (setq result
(cons (substring definition start end
) result
)
299 start
(and end
(match-end 0)))))
301 (mapconcat (function (lambda (x)
302 (or (mail-resolve-all-aliases-1
303 (intern-soft (downcase x
) mail-abbrevs
)
307 mail-alias-separator-string
))
308 (set sym definition
))))
312 (defun mail-abbrev-expand-hook ()
313 "For use as the fourth arg to `define-abbrev'.
314 After expanding a mail-abbrev, if Auto Fill mode is on and we're past the
315 fill-column, break the line at the previous comma, and indent the next line."
322 (while (and auto-fill-function
323 (>= (current-column) fill-column
)
324 (search-backward "," bol t
))
326 (forward-char 1) ; Now we are just past the comma.
328 (delete-horizontal-space)
331 (setq fp
(buffer-substring p
(point)))
332 ;; Go to the end of the new line.
334 (if (> (current-column) fill-column
)
335 ;; It's still too long; do normal auto-fill.
336 (let ((fill-prefix (or fp
"\t")))
338 ;; Resume the search.
342 ;;; Syntax tables and abbrev-expansion
344 (defvar mail-abbrev-mode-regexp
345 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
346 "*Regexp to select mail-headers in which mail abbrevs should be expanded.
347 This string will be handed to `looking-at' with point at the beginning
348 of the current line; if it matches, abbrev mode will be turned on, otherwise
349 it will be turned off. (You don't need to worry about continuation lines.)
350 This should be set to match those mail fields in which you want abbreviations
353 (defvar mail-mode-header-syntax-table
354 (let ((tab (copy-syntax-table text-mode-syntax-table
)))
355 ;; This makes the characters "@%!._-" be considered symbol-constituents
356 ;; but not word-constituents, so forward-sexp will move you over an
357 ;; entire address, but forward-word will only move you over a sequence
358 ;; of alphanumerics. (Clearly the right thing.)
359 (modify-syntax-entry ?
@ "_" tab
)
360 (modify-syntax-entry ?%
"_" tab
)
361 (modify-syntax-entry ?
! "_" tab
)
362 (modify-syntax-entry ?.
"_" tab
)
363 (modify-syntax-entry ?_
"_" tab
)
364 (modify-syntax-entry ?-
"_" tab
)
365 (modify-syntax-entry ?
< "(>" tab
)
366 (modify-syntax-entry ?
> ")<" tab
)
368 "The syntax table used in send-mail mode when in a mail-address header.
369 `mail-mode-syntax-table' is used when the cursor is in the message body or in
370 non-address headers.")
372 (defvar mail-abbrev-syntax-table
373 (let* ((tab (copy-syntax-table mail-mode-header-syntax-table
))
374 (_ (aref (standard-syntax-table) ?_
))
375 (w (aref (standard-syntax-table) ?w
)))
377 (function (lambda (key value
)
379 (set-char-table-range tab key w
))))
382 "The syntax-table used for abbrev-expansion purposes.
383 This is not actually made the current syntax table of the buffer, but
384 simply controls the set of characters which may be a part of the name
388 (defun mail-abbrev-in-expansion-header-p ()
389 "Whether point is in a mail-address header field."
390 (let ((case-fold-search t
))
392 ;; we are on an appropriate header line...
395 ;; skip backwards over continuation lines.
396 (while (and (looking-at "^[ \t]")
397 (not (= (point) (point-min))))
399 ;; are we at the front of an appropriate header line?
400 (looking-at mail-abbrev-mode-regexp
))
402 ;; ...and we are before the mail-header-separator
405 (goto-char (point-min))
406 (search-forward (concat "\n" mail-header-separator
"\n")
410 (defvar mail-mode-abbrev-table
) ; quiet the compiler
412 (defun sendmail-pre-abbrev-expand-hook ()
413 (and (and mail-abbrevs
(not (eq mail-abbrevs t
)))
414 (if (mail-abbrev-in-expansion-header-p)
417 ;; We are in a To: (or CC:, or whatever) header, and
418 ;; should use word-abbrevs to expand mail aliases.
420 ;; Before anything else, resolve aliases if they need it.
421 (and mail-abbrev-aliases-need-to-be-resolved
422 (mail-resolve-all-aliases))
424 ;; Now proceed with the abbrev section.
425 ;; - First, install the mail-abbrevs as the word-abbrev table.
426 ;; - Then install the mail-abbrev-syntax-table, which
427 ;; temporarily marks all of the
428 ;; non-alphanumeric-atom-characters (the "_"
429 ;; syntax ones) as being normal word-syntax. We do this
430 ;; because the C code for expand-abbrev only works on words,
431 ;; and we want these characters to be considered words for
432 ;; the purpose of abbrev expansion.
433 ;; - Then we call expand-abbrev again, recursively, to do
434 ;; the abbrev expansion with the above syntax table.
435 ;; - Then we do a trick which tells the expand-abbrev frame
436 ;; which invoked us to not continue (and thus not
437 ;; expand twice.) This means that any abbrev expansion
438 ;; will happen as a result of this function's call to
439 ;; expand-abbrev, and not as a result of the call to
440 ;; expand-abbrev which invoked *us*.
441 ;; - Then we set the syntax table to
442 ;; mail-mode-header-syntax-table, which doesn't have
443 ;; anything to do with abbrev expansion, but
444 ;; is just for the user's convenience (see its doc string.)
447 (setq local-abbrev-table mail-abbrevs
)
449 ;; If the character just typed was non-alpha-symbol-syntax,
450 ;; then don't expand the abbrev now (that is, don't expand
451 ;; when the user types -.) Check the character's syntax in
452 ;; the mail-mode-header-syntax-table.
454 (set-syntax-table mail-mode-header-syntax-table
)
455 (or (and (integerp last-command-char
)
456 (eq (char-syntax last-command-char
) ?_
))
457 (let ((pre-abbrev-expand-hook nil
)) ; That's us; don't loop.
458 ;; Use this table so that abbrevs can have hyphens in them.
459 (set-syntax-table mail-abbrev-syntax-table
)
461 ;; Now set it back to what it was before.
462 (set-syntax-table mail-mode-header-syntax-table
)))
463 (setq abbrev-start-location
(point-max) ; This is the trick.
464 abbrev-start-location-buffer
(current-buffer)))
466 ;; We're not in a mail header where mail aliases should
467 ;; be expanded, then use the normal mail-mode abbrev table
468 ;; (if any) and the normal mail-mode syntax table.
470 (setq local-abbrev-table
(and (boundp 'mail-mode-abbrev-table
)
471 mail-mode-abbrev-table
))
472 (set-syntax-table mail-mode-syntax-table
))
477 (defun merge-mail-abbrevs (file)
478 "Merge mail aliases from the given file with existing ones."
480 (let ((insert-default-directory t
)
481 (default-directory (expand-file-name "~/"))
482 (def mail-personal-alias-file
))
484 (format "Read additional aliases from file: (default %s) "
487 (expand-file-name def default-directory
)
489 (build-mail-abbrevs file
))
491 (defun rebuild-mail-abbrevs (&optional file
)
492 "Rebuild all the mail aliases from the given file."
494 (let ((insert-default-directory t
)
495 (default-directory (expand-file-name "~/"))
496 (def mail-personal-alias-file
))
498 (format "Read mail aliases from file: (default %s) " def
)
500 (expand-file-name def default-directory
)
503 (setq file buffer-file-name
))
504 (setq mail-abbrevs nil
)
505 (build-mail-abbrevs file
))
507 (defun mail-interactive-insert-alias (&optional alias
)
508 "Prompt for and insert a mail alias."
510 (if (not (vectorp mail-abbrevs
)) (mail-abbrevs-setup))
511 (list (completing-read "Expand alias: " mail-abbrevs nil t
))))
512 (if (not (vectorp mail-abbrevs
)) (mail-abbrevs-setup))
513 (insert (or (and alias
(symbol-value (intern-soft alias mail-abbrevs
))) ""))
514 (mail-abbrev-expand-hook))
516 (defun mail-abbrev-next-line (&optional arg
)
517 "Expand any mail abbrev, then move cursor vertically down ARG lines.
518 If there is no character in the target line exactly under the current column,
519 the cursor is positioned after the character in that line which spans this
520 column, or at the end of the line if it is not long enough.
521 If there is no line in the buffer after this one,
522 a newline character is inserted to create a line
523 and the cursor moves to that line.
525 The command \\[set-goal-column] can be used to create
526 a semipermanent goal column to which this command always moves.
527 Then it does not try to move vertically. This goal column is stored
528 in `goal-column', which is nil when there is none.
530 If you are thinking of using this in a Lisp program, consider
531 using `forward-line' instead. It is usually easier to use
532 and more reliable (no dependence on goal column, etc.)."
534 (if (looking-at "[ \t]*\n") (expand-abbrev))
535 (setq this-command
'next-line
)
538 (defun mail-abbrev-end-of-buffer (&optional arg
)
539 "Expand any mail abbrev, then move point to end of buffer.
540 Leave mark at previous position.
541 With arg N, put point N/10 of the way from the true end.
543 Don't use this command in Lisp programs!
544 \(goto-char (point-max)) is faster and avoids clobbering the mark."
546 (if (looking-at "[ \t]*\n") (expand-abbrev))
547 (setq this-command
'end-of-buffer
)
550 (define-key mail-mode-map
"\C-c\C-a" 'mail-interactive-insert-alias
)
552 ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
553 ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
555 (provide 'mailabbrev
)
557 ;;; mailabbrev.el ends here.