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