Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; mailabbrev.el --- abbrev-expansion of mail aliases |
275da787 | 2 | |
f2e3589a | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1996, 1997, 2000, 2001, |
d7a0267c | 4 | ;; 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
275da787 | 5 | |
fcc31755 DL |
6 | ;; Author: Jamie Zawinski <jwz@lucid.com>, now <jwz@jwz.org> |
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 | |
b578f267 EN |
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 | |
ceaeecb0 | 15 | ;; the Free Software Foundation; either version 3, or (at your option) |
b578f267 | 16 | ;; 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 EN |
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 | |
3a35cf56 LK |
25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 | ;; Boston, MA 02110-1301, USA. | |
b01c3008 | 27 | |
275da787 RM |
28 | ;;; Commentary: |
29 | ||
0f965ba3 | 30 | ;; This file ensures that, when the point is in a To:, CC:, BCC:, or From: |
b578f267 EN |
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 | |
f6babbac | 33 | ;; `mail-personal-alias-file') if it exists. Your mail aliases will |
b578f267 EN |
34 | ;; expand any time you type a word-delimiter at the end of an abbreviation. |
35 | ;; | |
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. | |
41 | ;; | |
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 | |
fba75b6b | 45 | ;; mode-specific abbrev table will be used if |
b578f267 EN |
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. | |
49 | ;; | |
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. | |
52 | ;; | |
85d0e9ed | 53 | ;; You can also insert a mail alias with mail-abbrev-insert-alias |
b578f267 EN |
54 | ;; (bound to C-c C-a), which prompts you for an alias (with completion) |
55 | ;; and inserts its expansion at point. | |
56 | ;; | |
57 | ;; This file fixes a bug in the old system which prohibited your .mailrc | |
58 | ;; file from having lines like | |
59 | ;; | |
60 | ;; alias someone "John Doe <doe@quux.com>" | |
61 | ;; | |
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: | |
66 | ;; | |
67 | ;; alias foobar addr-1 addr-2 "address three <addr-3>" | |
68 | ;; | |
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.) | |
71 | ;; | |
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. | |
75 | ;; | |
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. | |
85 | ;; | |
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 | |
92 | ;; | |
93 | ;; Aliases may also contain forward references; the alias of "everybody" can | |
94 | ;; precede the aliases of "group1" and "group2". | |
95 | ;; | |
96 | ;; This code also understands the "source" .mailrc command, for reading | |
97 | ;; aliases from some other file as well. | |
98 | ;; | |
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. | |
102 | ;; | |
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. | |
106 | ;; | |
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 | |
110 | ;; | |
111 | ;; (add-hook | |
bde4c7ce | 112 | ;; 'mail-mode-hook |
c0df1972 | 113 | ;; (lambda () |
6b823692 AS |
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))) | |
b578f267 EN |
116 | ;; |
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 | |
120 | ;; behaviour. | |
121 | ;; | |
122 | ;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and | |
123 | ;; Noah Friedman for suggestions and bug reports. | |
124 | ||
bde4c7ce | 125 | ;; To use this package, do (add-hook 'mail-mode-hook 'mail-abbrevs-setup). |
b01c3008 | 126 | |
275da787 RM |
127 | ;;; Code: |
128 | ||
d075ed70 RS |
129 | (eval-when-compile |
130 | (require 'sendmail)) | |
b01c3008 | 131 | |
7d4c958f KH |
132 | (defgroup mail-abbrev nil |
133 | "Expand mail aliases as abbrevs, in certain mail headers." | |
134 | :group 'abbrev-mode) | |
135 | ||
dcbb251e SM |
136 | ;;;###autoload |
137 | (define-minor-mode mail-abbrevs-mode | |
138 | "Non-nil means expand mail aliases as abbrevs, in certain message headers." | |
139 | :global t | |
7d4c958f | 140 | :group 'mail-abbrev |
dcbb251e SM |
141 | :version "20.3" |
142 | (if mail-abbrevs-mode (mail-abbrevs-enable) (mail-abbrevs-disable))) | |
7d4c958f KH |
143 | |
144 | (defcustom mail-abbrevs-only nil | |
dcbb251e | 145 | "Non-nil means only mail abbrevs should expand automatically. |
7d4c958f KH |
146 | Other abbrevs expand only when you explicitly use `expand-abbrev'." |
147 | :type 'boolean | |
148 | :group 'mail-abbrev) | |
149 | ||
b01c3008 | 150 | ;; originally defined in sendmail.el - used to be an alist, now is a table. |
753d16a6 | 151 | (defvar mail-abbrevs nil |
22f4ef2e | 152 | "Word-abbrev table of mail address aliases. |
b01c3008 RS |
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.)") | |
156 | ||
00bafa45 RS |
157 | (defvar mail-abbrev-modtime nil |
158 | "The modification time of your mail alias file when it was last examined.") | |
159 | ||
160 | (defun mail-abbrevs-sync-aliases () | |
f7e85dcf RS |
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)) | |
165 | (progn | |
166 | (setq mail-abbrev-modtime modtime) | |
167 | (build-mail-abbrevs))))))) | |
00bafa45 | 168 | |
aa228418 | 169 | ;;;###autoload |
753d16a6 | 170 | (defun mail-abbrevs-setup () |
17804728 | 171 | "Initialize use of the `mailabbrev' package." |
753d16a6 | 172 | (if (and (not (vectorp mail-abbrevs)) |
a3039e32 | 173 | (file-exists-p mail-personal-alias-file)) |
00bafa45 | 174 | (progn |
0f965ba3 | 175 | (setq mail-abbrev-modtime |
00bafa45 RS |
176 | (nth 5 (file-attributes mail-personal-alias-file))) |
177 | (build-mail-abbrevs))) | |
178 | (mail-abbrevs-sync-aliases) | |
dcbb251e | 179 | (add-hook 'abbrev-expand-functions 'mail-abbrev-expand-wrapper nil t) |
b01c3008 RS |
180 | (abbrev-mode 1)) |
181 | ||
7d4c958f | 182 | (defun mail-abbrevs-enable () |
bde4c7ce | 183 | (add-hook 'mail-mode-hook 'mail-abbrevs-setup)) |
7d4c958f KH |
184 | |
185 | (defun mail-abbrevs-disable () | |
186 | "Turn off use of the `mailabbrev' package." | |
bde4c7ce | 187 | (remove-hook 'mail-mode-hook 'mail-abbrevs-setup) |
7d4c958f KH |
188 | (abbrev-mode (if (default-value 'abbrev-mode) 1 -1))) |
189 | ||
22f4ef2e | 190 | ;;;###autoload |
753d16a6 | 191 | (defun build-mail-abbrevs (&optional file recursivep) |
a3039e32 NF |
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))) | |
753d16a6 | 195 | (if (vectorp mail-abbrevs) |
b01c3008 | 196 | nil |
753d16a6 RS |
197 | (setq mail-abbrevs nil) |
198 | (define-abbrev-table 'mail-abbrevs '())) | |
5aefeeba | 199 | (message "Parsing %s..." file) |
dcbb251e SM |
200 | (with-temp-buffer |
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))) | |
214 | (end-of-line) | |
215 | (delete-region p (point)))) | |
216 | (goto-char (point-min)) | |
217 | ;; handle "\\\n" continuation lines | |
218 | (while (not (eobp)) | |
219 | (end-of-line) | |
220 | (if (= (preceding-char) ?\\) | |
221 | (progn (delete-char -1) (delete-char 1) (insert ?\ )) | |
222 | (forward-char 1))) | |
223 | (goto-char (point-min)) | |
224 | (while (re-search-forward | |
225 | "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t) | |
226 | (beginning-of-line) | |
227 | (if (looking-at "source[ \t]+\\([^ \t\n]+\\)") | |
228 | (progn | |
229 | (end-of-line) | |
230 | (build-mail-abbrevs | |
231 | (substitute-in-file-name | |
232 | (buffer-substring (match-beginning 1) (match-end 1))) | |
233 | t)) | |
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)))) | |
238 | (end-of-line) | |
239 | ;; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1) | |
240 | (define-mail-abbrev | |
241 | name | |
242 | (buffer-substring start (point)) | |
243 | t)))) | |
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)) | |
248 | mail-abbrevs) | |
249 | (message "Parsing %s... done" file)) | |
b01c3008 | 250 | |
f22cd786 | 251 | (defvar mail-alias-separator-string ", " |
b01c3008 | 252 | "*A string inserted between addresses in multi-address mail aliases. |
0f965ba3 | 253 | This has to contain a comma, so \", \" is a reasonable value. You might |
b01c3008 RS |
254 | also want something like \",\\n \" to get each address on its own line.") |
255 | ||
51e4ece8 | 256 | ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases |
b01c3008 RS |
257 | ;; to be called before expanding abbrevs if it's necessary. |
258 | (defvar mail-abbrev-aliases-need-to-be-resolved t) | |
259 | ||
753d16a6 | 260 | ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with |
b01c3008 RS |
261 | ;; stuff parsed from the .mailrc file. |
262 | ;; | |
aa228418 | 263 | ;;;###autoload |
51e4ece8 | 264 | (defun define-mail-abbrev (name definition &optional from-mailrc-file) |
92182a70 | 265 | "Define NAME as a mail alias abbrev that translates to DEFINITION. |
f22cd786 | 266 | If DEFINITION contains multiple addresses, separate them with commas." |
753d16a6 | 267 | ;; When this is called from build-mail-abbrevs, the third argument is |
b01c3008 RS |
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. | |
dcbb251e | 271 | (unless (vectorp mail-abbrevs) (build-mail-abbrevs)) |
b01c3008 RS |
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)))) | |
8eb6c7da RS |
277 | (let* ((result '()) |
278 | (L (length definition)) | |
279 | (start (if (> L 0) 0)) | |
280 | end) | |
b01c3008 RS |
281 | (while start |
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 | |
f22cd786 | 284 | ;; double-quotes. Otherwise, addresses are separated by commas. |
b01c3008 RS |
285 | (if from-mailrc-file |
286 | (if (eq ?\" (aref definition start)) | |
287 | (setq start (1+ start) | |
288 | end (string-match "\"[ \t,]*" definition start)) | |
8eb6c7da RS |
289 | (setq end (string-match "[ \t,]+" definition start))) |
290 | (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) | |
69ba53f7 RS |
291 | (let ((tem (substring definition start end))) |
292 | ;; Advance the loop past this address. | |
293 | (setq start (and end | |
294 | (/= (match-end 0) L) | |
295 | (match-end 0))) | |
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" | |
302 | tem)))) | |
303 | (push tem result))) | |
b01c3008 RS |
304 | (setq definition (mapconcat (function identity) |
305 | (nreverse result) | |
f22cd786 | 306 | mail-alias-separator-string))) |
b01c3008 RS |
307 | (setq mail-abbrev-aliases-need-to-be-resolved t) |
308 | (setq name (downcase name)) | |
753d16a6 | 309 | ;; use an abbrev table instead of an alist for mail-abbrevs. |
b01c3008 | 310 | (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed. |
8fad672b | 311 | (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook 0 t))) |
b01c3008 RS |
312 | |
313 | ||
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 | |
317 | (progn | |
318 | ;; (message "Resolving mail aliases...") | |
753d16a6 RS |
319 | (if (vectorp mail-abbrevs) |
320 | (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs)) | |
b01c3008 RS |
321 | (setq mail-abbrev-aliases-need-to-be-resolved nil) |
322 | ;; (message "Resolving mail aliases... done.") | |
323 | ))) | |
324 | ||
d7c1ec4b RM |
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) " <- "))) | |
b01c3008 RS |
329 | (let ((definition (and (boundp sym) (symbol-value sym)))) |
330 | (if definition | |
331 | (let ((result '()) | |
332 | (start 0)) | |
333 | (while start | |
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))))) | |
337 | (setq definition | |
338 | (mapconcat (function (lambda (x) | |
339 | (or (mail-resolve-all-aliases-1 | |
e1d24183 | 340 | (intern-soft (downcase x) mail-abbrevs) |
d7c1ec4b | 341 | (cons sym so-far)) |
b01c3008 RS |
342 | x))) |
343 | (nreverse result) | |
f22cd786 | 344 | mail-alias-separator-string)) |
b01c3008 RS |
345 | (set sym definition)))) |
346 | (symbol-value sym)) | |
347 | ||
348 | ||
f22cd786 | 349 | (defun mail-abbrev-expand-hook () |
92182a70 RS |
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." | |
0f965ba3 RM |
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)) | |
356 | (save-excursion | |
357 | (let ((p (point)) | |
358 | bol comma fp) | |
359 | (beginning-of-line) | |
360 | (setq bol (point)) | |
361 | (goto-char p) | |
362 | (while (and auto-fill-function | |
363 | (>= (current-column) fill-column) | |
364 | (search-backward "," bol t)) | |
365 | (setq comma (point)) | |
366 | (forward-char 1) ; Now we are just past the comma. | |
367 | (insert "\n") | |
368 | (delete-horizontal-space) | |
369 | (setq p (point)) | |
370 | (indent-relative) | |
371 | (setq fp (buffer-substring p (point))) | |
372 | ;; Go to the end of the new line. | |
373 | (end-of-line) | |
374 | (if (> (current-column) fill-column) | |
375 | ;; It's still too long; do normal auto-fill. | |
376 | (let ((fill-prefix (or fp "\t"))) | |
377 | (do-auto-fill))) | |
378 | ;; Resume the search. | |
379 | (goto-char comma) | |
380 | ))))) | |
22f4ef2e RM |
381 | \f |
382 | ;;; Syntax tables and abbrev-expansion | |
b01c3008 | 383 | |
0f965ba3 | 384 | (defvar mail-abbrev-mode-regexp |
450b15a0 | 385 | "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):" |
92182a70 | 386 | "*Regexp to select mail-headers in which mail abbrevs should be expanded. |
fb11ccf6 | 387 | This string will be handed to `looking-at' with point at the beginning |
b01c3008 RS |
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 | |
391 | turned on.") | |
392 | ||
d075ed70 | 393 | (defvar mail-abbrev-syntax-table nil |
4d2795c3 RS |
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 | |
d075ed70 | 397 | of a mail alias. The value is set up, buffer-local, when first needed.") |
22f4ef2e | 398 | |
cf8b484f RS |
399 | (defun mail-abbrev-make-syntax-table () |
400 | (make-local-variable 'mail-abbrev-syntax-table) | |
401 | (unless mail-abbrev-syntax-table | |
f436b1cb | 402 | (let ((tab (copy-syntax-table (syntax-table))) |
cf8b484f RS |
403 | (_ (aref (standard-syntax-table) ?_)) |
404 | (w (aref (standard-syntax-table) ?w))) | |
405 | (map-char-table | |
406 | (function (lambda (key value) | |
6946ae5b AS |
407 | (if (null value) |
408 | ;; Fetch the inherited value | |
409 | (setq value (aref tab key))) | |
cf8b484f RS |
410 | (if (equal value _) |
411 | (set-char-table-range tab key w)))) | |
412 | tab) | |
413 | (modify-syntax-entry ?@ "w" tab) | |
f8aba1ce MR |
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) | |
cf8b484f | 419 | (setq mail-abbrev-syntax-table tab)))) |
22f4ef2e RM |
420 | |
421 | (defun mail-abbrev-in-expansion-header-p () | |
422 | "Whether point is in a mail-address header field." | |
423 | (let ((case-fold-search t)) | |
424 | (and ;; | |
425 | ;; we are on an appropriate header line... | |
426 | (save-excursion | |
349deff4 GM |
427 | (unless (eobp) (forward-char 1)) |
428 | (re-search-backward "^[^ \t]" nil 'move) | |
22f4ef2e RM |
429 | ;; are we at the front of an appropriate header line? |
430 | (looking-at mail-abbrev-mode-regexp)) | |
431 | ;; | |
13aa1cde | 432 | ;; ...and are we in the headers? |
8fad672b RS |
433 | (< (point) |
434 | (save-restriction | |
435 | (widen) | |
436 | (save-excursion | |
437 | (rfc822-goto-eoh) | |
438 | (point))))))) | |
22f4ef2e | 439 | |
dcbb251e SM |
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) | |
443 | ||
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)) | |
447 | ||
448 | ;; Before anything else, resolve aliases if they need it. | |
449 | (and mail-abbrev-aliases-need-to-be-resolved | |
450 | (mail-resolve-all-aliases)) | |
451 | ||
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. | |
463 | ||
464 | (mail-abbrev-make-syntax-table) | |
465 | ||
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. | |
470 | ||
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 | |
479 | (funcall expand)))) | |
480 | ||
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. | |
486 | (funcall expand) | |
487 | ;; This is not a mail abbrev, and we should not expand it. | |
488 | ;; Don't expand anything. | |
489 | nil)) | |
490 | ;; No mail-abbrevs at all, do the normal thing. | |
491 | (funcall expand))) | |
22f4ef2e RM |
492 | \f |
493 | ;;; utilities | |
b01c3008 | 494 | |
753d16a6 | 495 | (defun merge-mail-abbrevs (file) |
b01c3008 RS |
496 | "Merge mail aliases from the given file with existing ones." |
497 | (interactive (list | |
498 | (let ((insert-default-directory t) | |
499 | (default-directory (expand-file-name "~/")) | |
a3039e32 | 500 | (def mail-personal-alias-file)) |
b01c3008 | 501 | (read-file-name |
5b76833f | 502 | (format "Read additional aliases from file (default %s): " |
b01c3008 RS |
503 | def) |
504 | default-directory | |
505 | (expand-file-name def default-directory) | |
506 | t)))) | |
753d16a6 | 507 | (build-mail-abbrevs file)) |
b01c3008 | 508 | |
cf376527 | 509 | (defun rebuild-mail-abbrevs (&optional file) |
b01c3008 RS |
510 | "Rebuild all the mail aliases from the given file." |
511 | (interactive (list | |
512 | (let ((insert-default-directory t) | |
513 | (default-directory (expand-file-name "~/")) | |
a3039e32 | 514 | (def mail-personal-alias-file)) |
b01c3008 | 515 | (read-file-name |
5b76833f | 516 | (format "Read mail aliases from file (default %s): " def) |
b01c3008 RS |
517 | default-directory |
518 | (expand-file-name def default-directory) | |
519 | t)))) | |
cf376527 RS |
520 | (if (null file) |
521 | (setq file buffer-file-name)) | |
753d16a6 RS |
522 | (setq mail-abbrevs nil) |
523 | (build-mail-abbrevs file)) | |
49116ac0 | 524 | |
85d0e9ed | 525 | (defun mail-abbrev-insert-alias (&optional alias) |
22f4ef2e | 526 | "Prompt for and insert a mail alias." |
c540863c | 527 | (interactive (progn |
753d16a6 RS |
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)) | |
0eef787b RS |
531 | (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) "")) |
532 | (mail-abbrev-expand-hook)) | |
753d16a6 | 533 | |
85d0e9ed GM |
534 | (defun mail-abbrev-complete-alias () |
535 | "Perform completion on alias preceding point." | |
536 | ;; Based on lisp.el:lisp-complete-symbol | |
537 | (interactive) | |
da00bb18 | 538 | (mail-abbrev-make-syntax-table) |
85d0e9ed | 539 | (let* ((end (point)) |
dcbb251e SM |
540 | (beg (with-syntax-table mail-abbrev-syntax-table |
541 | (save-excursion | |
542 | (backward-word 1) | |
543 | (point)))) | |
544 | (alias (buffer-substring beg end)) | |
85d0e9ed GM |
545 | (completion (try-completion alias mail-abbrevs))) |
546 | (cond ((eq completion t) | |
547 | (message "%s" alias)) ; confirm | |
548 | ((null completion) | |
549 | (error "[Can't complete \"%s\"]" alias)) ; (message ...) (ding) | |
550 | ((not (string= completion alias)) | |
551 | (delete-region beg end) | |
552 | (insert completion)) | |
553 | (t (with-output-to-temp-buffer "*Completions*" | |
554 | (display-completion-list | |
555 | (prog2 | |
556 | (message "Making completion list...") | |
557 | (all-completions alias mail-abbrevs) | |
f5fab556 MY |
558 | (message "Making completion list...done")) |
559 | alias)))))) | |
85d0e9ed | 560 | |
753d16a6 RS |
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. | |
569 | ||
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. | |
574 | ||
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.)." | |
22f4ef2e | 578 | (interactive "p") |
d7c1ec4b RM |
579 | (if (looking-at "[ \t]*\n") (expand-abbrev)) |
580 | (setq this-command 'next-line) | |
91dd4dc4 | 581 | (with-no-warnings (next-line arg))) |
22f4ef2e | 582 | |
753d16a6 RS |
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. | |
587 | ||
588 | Don't use this command in Lisp programs! | |
589 | \(goto-char (point-max)) is faster and avoids clobbering the mark." | |
650d6486 | 590 | (interactive "P") |
d7c1ec4b RM |
591 | (if (looking-at "[ \t]*\n") (expand-abbrev)) |
592 | (setq this-command 'end-of-buffer) | |
69ba53f7 RS |
593 | (with-no-warnings |
594 | (end-of-buffer arg))) | |
22f4ef2e | 595 | |
f526e65a RS |
596 | (eval-after-load "sendmail" |
597 | '(progn | |
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))) | |
22f4ef2e | 601 | |
753d16a6 RS |
602 | ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line) |
603 | ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer) | |
b01c3008 | 604 | |
3e1b7a46 | 605 | (provide 'mailabbrev) |
b578f267 | 606 | |
dcbb251e | 607 | ;; arch-tag: 5aa2d901-73f8-4ad7-b73c-4802282ad2ff |
55535639 | 608 | ;;; mailabbrev.el ends here |