Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; mailabbrev.el --- abbrev-expansion of mail aliases |
275da787 | 2 | |
d075ed70 | 3 | ;; Copyright (C) 1985, 86, 87, 92, 93, 96, 1997, 2000, 2002 |
85d0e9ed | 4 | ;; 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 | |
15 | ;; the Free Software Foundation; either version 2, or (at your option) | |
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 | |
25 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
26 | ;; Boston, MA 02111-1307, 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 | |
33 | ;; the MAILRC environment variable) if it exists. Your mail aliases will | |
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 () |
b578f267 EN |
114 | ;; (substitute-key-definition 'next-line 'mail-abbrev-next-line |
115 | ;; mail-mode-map global-map) | |
116 | ;; (substitute-key-definition 'end-of-buffer 'mail-abbrev-end-of-buffer | |
117 | ;; mail-mode-map global-map))) | |
118 | ;; | |
119 | ;; If you want multiple addresses separated by a string other than ", " then | |
120 | ;; you can set the variable mail-alias-separator-string to it. This has to | |
121 | ;; be a comma bracketed by whitespace if you want any kind of reasonable | |
122 | ;; behaviour. | |
123 | ;; | |
124 | ;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and | |
125 | ;; Noah Friedman for suggestions and bug reports. | |
126 | ||
bde4c7ce | 127 | ;; To use this package, do (add-hook 'mail-mode-hook 'mail-abbrevs-setup). |
b01c3008 | 128 | |
275da787 RM |
129 | ;;; Code: |
130 | ||
d075ed70 RS |
131 | (eval-when-compile |
132 | (require 'sendmail)) | |
b01c3008 | 133 | |
7d4c958f KH |
134 | (defgroup mail-abbrev nil |
135 | "Expand mail aliases as abbrevs, in certain mail headers." | |
136 | :group 'abbrev-mode) | |
137 | ||
138 | (defcustom mail-abbrevs-mode nil | |
139 | "*Non-nil means expand mail aliases as abbrevs, in certain message headers." | |
140 | :type 'boolean | |
141 | :group 'mail-abbrev | |
142 | :require 'mailabbrev | |
c0df1972 SM |
143 | :set (lambda (symbol value) |
144 | (setq mail-abbrevs-mode value) | |
145 | (if value (mail-abbrevs-enable) (mail-abbrevs-disable))) | |
f9e9ac1d DN |
146 | :initialize 'custom-initialize-default |
147 | :version "20.3") | |
7d4c958f KH |
148 | |
149 | (defcustom mail-abbrevs-only nil | |
150 | "*Non-nil means only mail abbrevs should expand automatically. | |
151 | Other abbrevs expand only when you explicitly use `expand-abbrev'." | |
152 | :type 'boolean | |
153 | :group 'mail-abbrev) | |
154 | ||
b01c3008 | 155 | ;; originally defined in sendmail.el - used to be an alist, now is a table. |
753d16a6 | 156 | (defvar mail-abbrevs nil |
22f4ef2e | 157 | "Word-abbrev table of mail address aliases. |
b01c3008 RS |
158 | If this is nil, it means the aliases have not yet been initialized and |
159 | should be read from the .mailrc file. (This is distinct from there being | |
160 | no aliases, which is represented by this being a table with no entries.)") | |
161 | ||
00bafa45 RS |
162 | (defvar mail-abbrev-modtime nil |
163 | "The modification time of your mail alias file when it was last examined.") | |
164 | ||
165 | (defun mail-abbrevs-sync-aliases () | |
166 | (if (file-exists-p mail-personal-alias-file) | |
167 | (let ((modtime (nth 5 (file-attributes mail-personal-alias-file)))) | |
168 | (if (not (equal mail-abbrev-modtime modtime)) | |
169 | (progn | |
170 | (setq mail-abbrev-modtime modtime) | |
171 | (build-mail-abbrevs)))))) | |
172 | ||
aa228418 | 173 | ;;;###autoload |
753d16a6 | 174 | (defun mail-abbrevs-setup () |
17804728 | 175 | "Initialize use of the `mailabbrev' package." |
753d16a6 | 176 | (if (and (not (vectorp mail-abbrevs)) |
a3039e32 | 177 | (file-exists-p mail-personal-alias-file)) |
00bafa45 | 178 | (progn |
0f965ba3 | 179 | (setq mail-abbrev-modtime |
00bafa45 RS |
180 | (nth 5 (file-attributes mail-personal-alias-file))) |
181 | (build-mail-abbrevs))) | |
182 | (mail-abbrevs-sync-aliases) | |
92182a70 RS |
183 | (add-hook 'pre-abbrev-expand-hook 'sendmail-pre-abbrev-expand-hook |
184 | nil t) | |
b01c3008 RS |
185 | (abbrev-mode 1)) |
186 | ||
7d4c958f | 187 | (defun mail-abbrevs-enable () |
bde4c7ce | 188 | (add-hook 'mail-mode-hook 'mail-abbrevs-setup)) |
7d4c958f KH |
189 | |
190 | (defun mail-abbrevs-disable () | |
191 | "Turn off use of the `mailabbrev' package." | |
bde4c7ce | 192 | (remove-hook 'mail-mode-hook 'mail-abbrevs-setup) |
7d4c958f KH |
193 | (abbrev-mode (if (default-value 'abbrev-mode) 1 -1))) |
194 | ||
22f4ef2e | 195 | ;;;###autoload |
753d16a6 | 196 | (defun build-mail-abbrevs (&optional file recursivep) |
a3039e32 NF |
197 | "Read mail aliases from personal mail alias file and set `mail-abbrevs'. |
198 | By default this is the file specified by `mail-personal-alias-file'." | |
199 | (setq file (expand-file-name (or file mail-personal-alias-file))) | |
753d16a6 | 200 | (if (vectorp mail-abbrevs) |
b01c3008 | 201 | nil |
753d16a6 RS |
202 | (setq mail-abbrevs nil) |
203 | (define-abbrev-table 'mail-abbrevs '())) | |
5aefeeba | 204 | (message "Parsing %s..." file) |
b01c3008 RS |
205 | (let ((buffer nil) |
206 | (obuf (current-buffer))) | |
207 | (unwind-protect | |
208 | (progn | |
4adf062e | 209 | (setq buffer (generate-new-buffer " mailrc")) |
22f4ef2e | 210 | (buffer-disable-undo buffer) |
b01c3008 RS |
211 | (set-buffer buffer) |
212 | (cond ((get-file-buffer file) | |
213 | (insert (save-excursion | |
214 | (set-buffer (get-file-buffer file)) | |
215 | (buffer-substring (point-min) (point-max))))) | |
216 | ((not (file-exists-p file))) | |
217 | (t (insert-file-contents file))) | |
218 | ;; Don't lose if no final newline. | |
219 | (goto-char (point-max)) | |
220 | (or (eq (preceding-char) ?\n) (newline)) | |
221 | (goto-char (point-min)) | |
222 | ;; Delete comments from the file | |
223 | (while (search-forward "# " nil t) | |
224 | (let ((p (- (point) 2))) | |
225 | (end-of-line) | |
226 | (delete-region p (point)))) | |
227 | (goto-char (point-min)) | |
228 | ;; handle "\\\n" continuation lines | |
229 | (while (not (eobp)) | |
230 | (end-of-line) | |
231 | (if (= (preceding-char) ?\\) | |
232 | (progn (delete-char -1) (delete-char 1) (insert ?\ )) | |
233 | (forward-char 1))) | |
234 | (goto-char (point-min)) | |
235 | (while (re-search-forward | |
c540863c | 236 | "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t) |
b01c3008 RS |
237 | (beginning-of-line) |
238 | (if (looking-at "source[ \t]+\\([^ \t\n]+\\)") | |
239 | (progn | |
240 | (end-of-line) | |
753d16a6 | 241 | (build-mail-abbrevs |
94142e78 | 242 | (substitute-in-file-name |
651fca74 RS |
243 | (buffer-substring (match-beginning 1) (match-end 1))) |
244 | t)) | |
b01c3008 RS |
245 | (re-search-forward "[ \t]+\\([^ \t\n]+\\)") |
246 | (let* ((name (buffer-substring | |
247 | (match-beginning 1) (match-end 1))) | |
248 | (start (progn (skip-chars-forward " \t") (point)))) | |
249 | (end-of-line) | |
250 | ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1) | |
51e4ece8 | 251 | (define-mail-abbrev |
b01c3008 | 252 | name |
51e4ece8 RS |
253 | (buffer-substring start (point)) |
254 | t)))) | |
b01c3008 RS |
255 | ;; Resolve forward references in .mailrc file. |
256 | ;; This would happen automatically before the first abbrev was | |
257 | ;; expanded, but why not do it now. | |
258 | (or recursivep (mail-resolve-all-aliases)) | |
753d16a6 | 259 | mail-abbrevs) |
b01c3008 RS |
260 | (if buffer (kill-buffer buffer)) |
261 | (set-buffer obuf))) | |
5aefeeba | 262 | (message "Parsing %s... done" file)) |
b01c3008 | 263 | |
f22cd786 | 264 | (defvar mail-alias-separator-string ", " |
b01c3008 | 265 | "*A string inserted between addresses in multi-address mail aliases. |
0f965ba3 | 266 | This has to contain a comma, so \", \" is a reasonable value. You might |
b01c3008 RS |
267 | also want something like \",\\n \" to get each address on its own line.") |
268 | ||
51e4ece8 | 269 | ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases |
b01c3008 RS |
270 | ;; to be called before expanding abbrevs if it's necessary. |
271 | (defvar mail-abbrev-aliases-need-to-be-resolved t) | |
272 | ||
753d16a6 | 273 | ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with |
b01c3008 RS |
274 | ;; stuff parsed from the .mailrc file. |
275 | ;; | |
aa228418 | 276 | ;;;###autoload |
51e4ece8 | 277 | (defun define-mail-abbrev (name definition &optional from-mailrc-file) |
92182a70 | 278 | "Define NAME as a mail alias abbrev that translates to DEFINITION. |
f22cd786 | 279 | If DEFINITION contains multiple addresses, separate them with commas." |
753d16a6 | 280 | ;; When this is called from build-mail-abbrevs, the third argument is |
b01c3008 RS |
281 | ;; true, and we do some evil space->comma hacking like /bin/mail does. |
282 | (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ") | |
283 | ;; Read the defaults first, if we have not done so. | |
753d16a6 | 284 | (if (vectorp mail-abbrevs) |
b01c3008 | 285 | nil |
753d16a6 RS |
286 | (setq mail-abbrevs nil) |
287 | (define-abbrev-table 'mail-abbrevs '()) | |
a3039e32 | 288 | (if (file-exists-p mail-personal-alias-file) |
753d16a6 | 289 | (build-mail-abbrevs))) |
b01c3008 RS |
290 | ;; strip garbage from front and end |
291 | (if (string-match "\\`[ \t\n,]+" definition) | |
292 | (setq definition (substring definition (match-end 0)))) | |
293 | (if (string-match "[ \t\n,]+\\'" definition) | |
294 | (setq definition (substring definition 0 (match-beginning 0)))) | |
8eb6c7da RS |
295 | (let* ((result '()) |
296 | (L (length definition)) | |
297 | (start (if (> L 0) 0)) | |
298 | end) | |
b01c3008 RS |
299 | (while start |
300 | ;; If we're reading from the mailrc file, then addresses are delimited | |
301 | ;; by spaces, and addresses with embedded spaces must be surrounded by | |
f22cd786 | 302 | ;; double-quotes. Otherwise, addresses are separated by commas. |
b01c3008 RS |
303 | (if from-mailrc-file |
304 | (if (eq ?\" (aref definition start)) | |
305 | (setq start (1+ start) | |
306 | end (string-match "\"[ \t,]*" definition start)) | |
8eb6c7da RS |
307 | (setq end (string-match "[ \t,]+" definition start))) |
308 | (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) | |
b01c3008 RS |
309 | (setq result (cons (substring definition start end) result)) |
310 | (setq start (and end | |
311 | (/= (match-end 0) L) | |
312 | (match-end 0)))) | |
313 | (setq definition (mapconcat (function identity) | |
314 | (nreverse result) | |
f22cd786 | 315 | mail-alias-separator-string))) |
b01c3008 RS |
316 | (setq mail-abbrev-aliases-need-to-be-resolved t) |
317 | (setq name (downcase name)) | |
753d16a6 | 318 | ;; use an abbrev table instead of an alist for mail-abbrevs. |
b01c3008 | 319 | (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed. |
8fad672b | 320 | (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook 0 t))) |
b01c3008 RS |
321 | |
322 | ||
323 | (defun mail-resolve-all-aliases () | |
324 | "Resolve all forward references in the mail aliases table." | |
325 | (if mail-abbrev-aliases-need-to-be-resolved | |
326 | (progn | |
327 | ;; (message "Resolving mail aliases...") | |
753d16a6 RS |
328 | (if (vectorp mail-abbrevs) |
329 | (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs)) | |
b01c3008 RS |
330 | (setq mail-abbrev-aliases-need-to-be-resolved nil) |
331 | ;; (message "Resolving mail aliases... done.") | |
332 | ))) | |
333 | ||
d7c1ec4b RM |
334 | (defun mail-resolve-all-aliases-1 (sym &optional so-far) |
335 | (if (memq sym so-far) | |
336 | (error "mail alias loop detected: %s" | |
337 | (mapconcat 'symbol-name (cons sym so-far) " <- "))) | |
b01c3008 RS |
338 | (let ((definition (and (boundp sym) (symbol-value sym)))) |
339 | (if definition | |
340 | (let ((result '()) | |
341 | (start 0)) | |
342 | (while start | |
343 | (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start))) | |
344 | (setq result (cons (substring definition start end) result) | |
345 | start (and end (match-end 0))))) | |
346 | (setq definition | |
347 | (mapconcat (function (lambda (x) | |
348 | (or (mail-resolve-all-aliases-1 | |
e1d24183 | 349 | (intern-soft (downcase x) mail-abbrevs) |
d7c1ec4b | 350 | (cons sym so-far)) |
b01c3008 RS |
351 | x))) |
352 | (nreverse result) | |
f22cd786 | 353 | mail-alias-separator-string)) |
b01c3008 RS |
354 | (set sym definition)))) |
355 | (symbol-value sym)) | |
356 | ||
357 | ||
f22cd786 | 358 | (defun mail-abbrev-expand-hook () |
92182a70 RS |
359 | "For use as the fourth arg to `define-abbrev'. |
360 | After expanding a mail-abbrev, if Auto Fill mode is on and we're past the | |
361 | fill-column, break the line at the previous comma, and indent the next line." | |
0f965ba3 RM |
362 | ;; Disable abbrev mode to avoid recursion in indent-relative expanding |
363 | ;; part of the abbrev expansion as an abbrev itself. | |
364 | (let ((abbrev-mode nil)) | |
365 | (save-excursion | |
366 | (let ((p (point)) | |
367 | bol comma fp) | |
368 | (beginning-of-line) | |
369 | (setq bol (point)) | |
370 | (goto-char p) | |
371 | (while (and auto-fill-function | |
372 | (>= (current-column) fill-column) | |
373 | (search-backward "," bol t)) | |
374 | (setq comma (point)) | |
375 | (forward-char 1) ; Now we are just past the comma. | |
376 | (insert "\n") | |
377 | (delete-horizontal-space) | |
378 | (setq p (point)) | |
379 | (indent-relative) | |
380 | (setq fp (buffer-substring p (point))) | |
381 | ;; Go to the end of the new line. | |
382 | (end-of-line) | |
383 | (if (> (current-column) fill-column) | |
384 | ;; It's still too long; do normal auto-fill. | |
385 | (let ((fill-prefix (or fp "\t"))) | |
386 | (do-auto-fill))) | |
387 | ;; Resume the search. | |
388 | (goto-char comma) | |
389 | ))))) | |
22f4ef2e RM |
390 | \f |
391 | ;;; Syntax tables and abbrev-expansion | |
b01c3008 | 392 | |
0f965ba3 | 393 | (defvar mail-abbrev-mode-regexp |
450b15a0 | 394 | "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):" |
92182a70 | 395 | "*Regexp to select mail-headers in which mail abbrevs should be expanded. |
fb11ccf6 | 396 | This string will be handed to `looking-at' with point at the beginning |
b01c3008 RS |
397 | of the current line; if it matches, abbrev mode will be turned on, otherwise |
398 | it will be turned off. (You don't need to worry about continuation lines.) | |
399 | This should be set to match those mail fields in which you want abbreviations | |
400 | turned on.") | |
401 | ||
d075ed70 | 402 | (defvar mail-abbrev-syntax-table nil |
4d2795c3 RS |
403 | "The syntax-table used for abbrev-expansion purposes. |
404 | This is not actually made the current syntax table of the buffer, but | |
405 | simply controls the set of characters which may be a part of the name | |
d075ed70 | 406 | of a mail alias. The value is set up, buffer-local, when first needed.") |
22f4ef2e | 407 | |
cf8b484f RS |
408 | (defun mail-abbrev-make-syntax-table () |
409 | (make-local-variable 'mail-abbrev-syntax-table) | |
410 | (unless mail-abbrev-syntax-table | |
f436b1cb | 411 | (let ((tab (copy-syntax-table (syntax-table))) |
cf8b484f RS |
412 | (_ (aref (standard-syntax-table) ?_)) |
413 | (w (aref (standard-syntax-table) ?w))) | |
414 | (map-char-table | |
415 | (function (lambda (key value) | |
6946ae5b AS |
416 | (if (null value) |
417 | ;; Fetch the inherited value | |
418 | (setq value (aref tab key))) | |
cf8b484f RS |
419 | (if (equal value _) |
420 | (set-char-table-range tab key w)))) | |
421 | tab) | |
422 | (modify-syntax-entry ?@ "w" tab) | |
f8aba1ce MR |
423 | (modify-syntax-entry ?% "w" tab) |
424 | (modify-syntax-entry ?! "w" tab) | |
425 | (modify-syntax-entry ?. "w" tab) | |
426 | (modify-syntax-entry ?_ "w" tab) | |
427 | (modify-syntax-entry ?- "w" tab) | |
cf8b484f | 428 | (setq mail-abbrev-syntax-table tab)))) |
22f4ef2e RM |
429 | |
430 | (defun mail-abbrev-in-expansion-header-p () | |
431 | "Whether point is in a mail-address header field." | |
432 | (let ((case-fold-search t)) | |
433 | (and ;; | |
434 | ;; we are on an appropriate header line... | |
435 | (save-excursion | |
349deff4 GM |
436 | (unless (eobp) (forward-char 1)) |
437 | (re-search-backward "^[^ \t]" nil 'move) | |
22f4ef2e RM |
438 | ;; are we at the front of an appropriate header line? |
439 | (looking-at mail-abbrev-mode-regexp)) | |
440 | ;; | |
13aa1cde | 441 | ;; ...and are we in the headers? |
8fad672b RS |
442 | (< (point) |
443 | (save-restriction | |
444 | (widen) | |
445 | (save-excursion | |
446 | (rfc822-goto-eoh) | |
447 | (point))))))) | |
22f4ef2e | 448 | |
f22cd786 | 449 | (defun sendmail-pre-abbrev-expand-hook () |
753d16a6 | 450 | (and (and mail-abbrevs (not (eq mail-abbrevs t))) |
275da787 | 451 | (if (mail-abbrev-in-expansion-header-p) |
d075ed70 RS |
452 | |
453 | ;; We are in a To: (or CC:, or whatever) header, and | |
454 | ;; should use word-abbrevs to expand mail aliases. | |
455 | (let ((local-abbrev-table mail-abbrevs) | |
456 | (old-syntax-table (syntax-table))) | |
275da787 RM |
457 | |
458 | ;; Before anything else, resolve aliases if they need it. | |
459 | (and mail-abbrev-aliases-need-to-be-resolved | |
460 | (mail-resolve-all-aliases)) | |
461 | ||
462 | ;; Now proceed with the abbrev section. | |
d075ed70 | 463 | ;; - We already installed mail-abbrevs as the abbrev table. |
275da787 RM |
464 | ;; - Then install the mail-abbrev-syntax-table, which |
465 | ;; temporarily marks all of the | |
466 | ;; non-alphanumeric-atom-characters (the "_" | |
467 | ;; syntax ones) as being normal word-syntax. We do this | |
468 | ;; because the C code for expand-abbrev only works on words, | |
469 | ;; and we want these characters to be considered words for | |
470 | ;; the purpose of abbrev expansion. | |
471 | ;; - Then we call expand-abbrev again, recursively, to do | |
472 | ;; the abbrev expansion with the above syntax table. | |
d075ed70 | 473 | ;; - Restore the previous syntax table. |
275da787 RM |
474 | ;; - Then we do a trick which tells the expand-abbrev frame |
475 | ;; which invoked us to not continue (and thus not | |
476 | ;; expand twice.) This means that any abbrev expansion | |
477 | ;; will happen as a result of this function's call to | |
478 | ;; expand-abbrev, and not as a result of the call to | |
479 | ;; expand-abbrev which invoked *us*. | |
275da787 | 480 | |
cf8b484f | 481 | (mail-abbrev-make-syntax-table) |
275da787 RM |
482 | |
483 | ;; If the character just typed was non-alpha-symbol-syntax, | |
484 | ;; then don't expand the abbrev now (that is, don't expand | |
485 | ;; when the user types -.) Check the character's syntax in | |
d075ed70 | 486 | ;; the usual syntax table. |
275da787 | 487 | |
25645866 RS |
488 | (or (and (integerp last-command-char) |
489 | (eq (char-syntax last-command-char) ?_)) | |
275da787 RM |
490 | (let ((pre-abbrev-expand-hook nil)) ; That's us; don't loop. |
491 | ;; Use this table so that abbrevs can have hyphens in them. | |
492 | (set-syntax-table mail-abbrev-syntax-table) | |
d075ed70 RS |
493 | (unwind-protect |
494 | (expand-abbrev) | |
495 | ;; Now set it back to what it was before. | |
496 | (set-syntax-table old-syntax-table)))) | |
e1a3a843 | 497 | (setq abbrev-start-location (point-max) ; This is the trick. |
275da787 RM |
498 | abbrev-start-location-buffer (current-buffer))) |
499 | ||
7d4c958f KH |
500 | (if (or (not mail-abbrevs-only) |
501 | (eq this-command 'expand-abbrev)) | |
d075ed70 RS |
502 | ;; We're not in a mail header where mail aliases should |
503 | ;; be expanded, then use the normal mail-mode abbrev table | |
504 | ;; (if any) and the normal mail-mode syntax table. | |
505 | nil | |
7d4c958f KH |
506 | ;; This is not a mail abbrev, and we should not expand it. |
507 | ;; This kludge stops expand-abbrev from doing anything. | |
508 | (setq abbrev-start-location (point-max) | |
509 | abbrev-start-location-buffer (current-buffer)))) | |
275da787 | 510 | )) |
22f4ef2e RM |
511 | \f |
512 | ;;; utilities | |
b01c3008 | 513 | |
753d16a6 | 514 | (defun merge-mail-abbrevs (file) |
b01c3008 RS |
515 | "Merge mail aliases from the given file with existing ones." |
516 | (interactive (list | |
517 | (let ((insert-default-directory t) | |
518 | (default-directory (expand-file-name "~/")) | |
a3039e32 | 519 | (def mail-personal-alias-file)) |
b01c3008 RS |
520 | (read-file-name |
521 | (format "Read additional aliases from file: (default %s) " | |
522 | def) | |
523 | default-directory | |
524 | (expand-file-name def default-directory) | |
525 | t)))) | |
753d16a6 | 526 | (build-mail-abbrevs file)) |
b01c3008 | 527 | |
cf376527 | 528 | (defun rebuild-mail-abbrevs (&optional file) |
b01c3008 RS |
529 | "Rebuild all the mail aliases from the given file." |
530 | (interactive (list | |
531 | (let ((insert-default-directory t) | |
532 | (default-directory (expand-file-name "~/")) | |
a3039e32 | 533 | (def mail-personal-alias-file)) |
b01c3008 RS |
534 | (read-file-name |
535 | (format "Read mail aliases from file: (default %s) " def) | |
536 | default-directory | |
537 | (expand-file-name def default-directory) | |
538 | t)))) | |
cf376527 RS |
539 | (if (null file) |
540 | (setq file buffer-file-name)) | |
753d16a6 RS |
541 | (setq mail-abbrevs nil) |
542 | (build-mail-abbrevs file)) | |
49116ac0 | 543 | |
85d0e9ed | 544 | (defun mail-abbrev-insert-alias (&optional alias) |
22f4ef2e | 545 | "Prompt for and insert a mail alias." |
c540863c | 546 | (interactive (progn |
753d16a6 RS |
547 | (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup)) |
548 | (list (completing-read "Expand alias: " mail-abbrevs nil t)))) | |
549 | (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup)) | |
0eef787b RS |
550 | (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) "")) |
551 | (mail-abbrev-expand-hook)) | |
753d16a6 | 552 | |
85d0e9ed GM |
553 | (defun mail-abbrev-complete-alias () |
554 | "Perform completion on alias preceding point." | |
555 | ;; Based on lisp.el:lisp-complete-symbol | |
556 | (interactive) | |
da00bb18 | 557 | (mail-abbrev-make-syntax-table) |
85d0e9ed GM |
558 | (let* ((end (point)) |
559 | (syntax-table (syntax-table)) | |
560 | (beg (unwind-protect | |
561 | (save-excursion | |
562 | (set-syntax-table mail-abbrev-syntax-table) | |
563 | (backward-word 1) | |
564 | (point)) | |
565 | (set-syntax-table syntax-table))) | |
566 | (alias (buffer-substring beg end)) | |
567 | (completion (try-completion alias mail-abbrevs))) | |
568 | (cond ((eq completion t) | |
569 | (message "%s" alias)) ; confirm | |
570 | ((null completion) | |
571 | (error "[Can't complete \"%s\"]" alias)) ; (message ...) (ding) | |
572 | ((not (string= completion alias)) | |
573 | (delete-region beg end) | |
574 | (insert completion)) | |
575 | (t (with-output-to-temp-buffer "*Completions*" | |
576 | (display-completion-list | |
577 | (prog2 | |
578 | (message "Making completion list...") | |
579 | (all-completions alias mail-abbrevs) | |
580 | (message "Making completion list...done")))))))) | |
581 | ||
753d16a6 RS |
582 | (defun mail-abbrev-next-line (&optional arg) |
583 | "Expand any mail abbrev, then move cursor vertically down ARG lines. | |
584 | If there is no character in the target line exactly under the current column, | |
585 | the cursor is positioned after the character in that line which spans this | |
586 | column, or at the end of the line if it is not long enough. | |
587 | If there is no line in the buffer after this one, | |
588 | a newline character is inserted to create a line | |
589 | and the cursor moves to that line. | |
590 | ||
591 | The command \\[set-goal-column] can be used to create | |
592 | a semipermanent goal column to which this command always moves. | |
593 | Then it does not try to move vertically. This goal column is stored | |
594 | in `goal-column', which is nil when there is none. | |
595 | ||
596 | If you are thinking of using this in a Lisp program, consider | |
597 | using `forward-line' instead. It is usually easier to use | |
598 | and more reliable (no dependence on goal column, etc.)." | |
22f4ef2e | 599 | (interactive "p") |
d7c1ec4b RM |
600 | (if (looking-at "[ \t]*\n") (expand-abbrev)) |
601 | (setq this-command 'next-line) | |
22f4ef2e RM |
602 | (next-line arg)) |
603 | ||
753d16a6 RS |
604 | (defun mail-abbrev-end-of-buffer (&optional arg) |
605 | "Expand any mail abbrev, then move point to end of buffer. | |
606 | Leave mark at previous position. | |
607 | With arg N, put point N/10 of the way from the true end. | |
608 | ||
609 | Don't use this command in Lisp programs! | |
610 | \(goto-char (point-max)) is faster and avoids clobbering the mark." | |
650d6486 | 611 | (interactive "P") |
d7c1ec4b RM |
612 | (if (looking-at "[ \t]*\n") (expand-abbrev)) |
613 | (setq this-command 'end-of-buffer) | |
22f4ef2e RM |
614 | (end-of-buffer arg)) |
615 | ||
f526e65a RS |
616 | (eval-after-load "sendmail" |
617 | '(progn | |
618 | (define-key mail-mode-map "\C-c\C-a" 'mail-abbrev-insert-alias) | |
619 | (define-key mail-mode-map "\e\t" ; like lisp-complete-symbol | |
620 | 'mail-abbrev-complete-alias))) | |
22f4ef2e | 621 | |
753d16a6 RS |
622 | ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line) |
623 | ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer) | |
b01c3008 | 624 | |
3e1b7a46 | 625 | (provide 'mailabbrev) |
b578f267 | 626 | |
7d4c958f KH |
627 | (if mail-abbrevs-mode |
628 | (mail-abbrevs-enable)) | |
629 | ||
55535639 | 630 | ;;; mailabbrev.el ends here |