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