0
|
1 ;;; Abbrev-expansion of mail aliases.
|
|
2 ;;; Copyright (C) 1985-1994 Free Software Foundation, Inc.
|
4
|
3 ;;; Created: 19 oct 90, Jamie Zawinski <jwz@netscape.com>
|
0
|
4 ;;; Modified: 5 apr 92, Roland McGrath <roland@gnu.ai.mit.edu>
|
|
5 ;;; Last change 4-may-94. jwz
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
16
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 ;; Boston, MA 02111-1307, USA.
|
0
|
23
|
|
24 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
|
|
25 ;;; field, word-abbrevs are defined for each of your mail aliases. These
|
|
26 ;;; aliases will be defined from your .mailrc file (or the file specified by
|
|
27 ;;; the MAILRC environment variable) if it exists. Your mail aliases will
|
|
28 ;;; expand any time you type a word-delimiter at the end of an abbreviation.
|
|
29 ;;;
|
|
30 ;;; What you see is what you get: no abbreviations will be expanded after you
|
|
31 ;;; have sent the mail, unlike the old system. This means you don't suffer
|
|
32 ;;; the annoyance of having the system do things behind your back -- if an
|
|
33 ;;; address you typed is going to be rewritten, you know it immediately,
|
|
34 ;;; instead of after the mail has been sent and it's too late to do anything
|
|
35 ;;; about it. You will never again be screwed because you forgot to delete an
|
|
36 ;;; old alias from your .mailrc when a new local user arrives and is given a
|
|
37 ;;; userid which conflicts with one of your aliases, for example.
|
|
38 ;;;
|
|
39 ;;; Your mail alias abbrevs will be in effect only when the point is in an
|
|
40 ;;; appropriate header field. When in the body of the message, or other
|
|
41 ;;; header fields, the mail aliases will not expand. Rather, the normal
|
|
42 ;;; mode-specific abbrev table (mail-mode-abbrev-table) will be used if
|
|
43 ;;; defined. So if you use mail-mode specific abbrevs, this code will not
|
|
44 ;;; adversely affect you. You can control which header fields the abbrevs
|
|
45 ;;; are used in by changing the variable mail-abbrev-mode-regexp.
|
|
46 ;;;
|
|
47 ;;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word
|
|
48 ;;; boundaries; also, header continuation-lines will be properly indented.
|
|
49 ;;;
|
|
50 ;;; You can also insert a mail alias with mail-interactive-insert-alias
|
|
51 ;;; (bound to C-c C-a), which prompts you for an alias (with completion)
|
|
52 ;;; and inserts its expansion at point.
|
|
53 ;;;
|
|
54 ;;; This file fixes a bug in the old system which prohibited your .mailrc
|
|
55 ;;; file from having lines like
|
|
56 ;;;
|
|
57 ;;; alias someone "John Doe <doe@quux.com>"
|
|
58 ;;;
|
|
59 ;;; That is, if you want an address to have embedded spaces, simply surround it
|
|
60 ;;; with quotes. This is necessary because the format of the .mailrc file
|
|
61 ;;; bogusly uses spaces as address delimiters. The following line defines an
|
|
62 ;;; alias which expands to three addresses:
|
|
63 ;;;
|
|
64 ;;; alias foobar addr-1 addr-2 "address three <addr-3>"
|
|
65 ;;;
|
|
66 ;;; (This is bogus because mail-delivery programs want commas, not spaces,
|
|
67 ;;; but that's what the file format is, so we have to live with it.)
|
|
68 ;;;
|
|
69 ;;; If you like, you can call the function define-mail-alias to define your
|
|
70 ;;; mail-aliases instead of using a .mailrc file. When you call it in this
|
|
71 ;;; way, addresses are separated by commas.
|
|
72 ;;;
|
|
73 ;;; CAVEAT: This works on most Sun systems; I have been told that some versions
|
|
74 ;;; of /bin/mail do not understand double-quotes in the .mailrc file. So you
|
|
75 ;;; should make sure your version does before including verbose addresses like
|
|
76 ;;; this. One solution to this, if you are on a system whose /bin/mail doesn't
|
|
77 ;;; work that way, (and you still want to be able to /bin/mail to send mail in
|
|
78 ;;; addition to emacs) is to define minimal aliases (without full names) in
|
|
79 ;;; your .mailrc file, and use define-mail-alias to redefine them when sending
|
|
80 ;;; mail from emacs; this way, mail sent from /bin/mail will work, and mail
|
|
81 ;;; sent from emacs will be pretty.
|
|
82 ;;;
|
|
83 ;;; Aliases in the mailrc file may be nested. If you define aliases like
|
|
84 ;;; alias group1 fred ethel
|
|
85 ;;; alias group2 larry curly moe
|
|
86 ;;; alias everybody group1 group2
|
|
87 ;;; Then when you type "everybody" on the To: line, it will be expanded to
|
|
88 ;;; fred, ethyl, larry, curly, moe
|
|
89 ;;;
|
|
90 ;;; Aliases may also contain forward references; the alias of "everybody" can
|
30
|
91 ;;; precede the aliases of "group1" and "group2".
|
0
|
92 ;;;
|
|
93 ;;; This code also understands the "source" .mailrc command, for reading
|
|
94 ;;; aliases from some other file as well.
|
|
95 ;;;
|
|
96 ;;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs
|
|
97 ;;; normally cannot contain hyphens, but this code works around that for the
|
|
98 ;;; specific case of mail-alias word-abbrevs.
|
|
99 ;;;
|
|
100 ;;; To read in the contents of another .mailrc-type file from emacs, use the
|
|
101 ;;; command Meta-X merge-mail-aliases. The rebuild-mail-aliases command is
|
|
102 ;;; similar, but will delete existing aliases first.
|
|
103 ;;;
|
|
104 ;;; If you want multiple addresses separated by a string other than ", " then
|
|
105 ;;; you can set the variable mail-alias-separator-string to it. This has to
|
|
106 ;;; be a comma bracketed by whitespace if you want any kind of reasonable
|
|
107 ;;; behaviour.
|
|
108 ;;;
|
|
109 ;;; Some versions of /bin/mail append the contents of multiple definitions of
|
|
110 ;;; the same alias together, so that
|
|
111 ;;; alias group one two three
|
|
112 ;;; alias group four five
|
|
113 ;;; would define "group" as "one two three four five" instead of "four five".
|
|
114 ;;; This code does *not* support that syntax, because it's a horrible syntax
|
|
115 ;;; and isn't worth the effort or added code complexity. (So there.)
|
|
116 ;;;
|
|
117 ;;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, Noah
|
|
118 ;;; Friedman, and Michelangelo Grigni for suggestions and bug reports.
|
|
119 ;;;
|
|
120 ;;; INSTALLATION
|
|
121 ;;;
|
|
122 ;;; If you are using Emacs 18, you shouldn't have to do anything at all to
|
|
123 ;;; install this code other than load this file. You might want to do this
|
|
124 ;;; to have this code loaded only when needed:
|
|
125 ;;;
|
|
126 ;;; (setq mail-setup-hook '(lambda () (require 'mail-abbrevs)))
|
|
127 ;;;
|
|
128 ;;; Simply loading this file will redefine and overload the required
|
|
129 ;;; functions.
|
|
130 ;;;
|
|
131 ;;; If you want to install this code more permanently (instead of loading
|
|
132 ;;; it as a patch) you need to do the following:
|
|
133 ;;;
|
|
134 ;;; - Remove the entire file mailalias.el;
|
|
135 ;;; - Remove the definition of mail-aliases from sendmail.el;
|
|
136 ;;; - Add a call to mail-aliases-setup to the front of the function
|
|
137 ;;; mail-setup in the file sendmail.el;
|
|
138 ;;; - Remove the call to expand-mail-aliases from the function
|
|
139 ;;; sendmail-send-it in the file sendmail.el;
|
|
140 ;;; - Remove the autoload of expand-mail-aliases from the file sendmail.el;
|
|
141 ;;; - Remove the autoload of build-mail-aliases from the file sendmail.el;
|
|
142 ;;; - Add an autoload of define-mail-alias to loaddefs.el.
|
|
143
|
|
144 (require 'sendmail)
|
|
145
|
|
146 ;;;###autoload
|
|
147 (defvar mail-abbrev-mailrc-file nil
|
|
148 "Name of file with mail aliases. If nil, ~/.mailrc is used.")
|
|
149
|
|
150 (defmacro mail-abbrev-mailrc-file ()
|
|
151 '(or mail-abbrev-mailrc-file
|
|
152 (setq mail-abbrev-mailrc-file
|
|
153 (or (getenv "MAILRC") "~/.mailrc"))))
|
|
154
|
|
155 ;; originally defined in sendmail.el - used to be an alist, now is a table.
|
|
156 ;;;###autoload
|
|
157 (defvar mail-aliases nil
|
|
158 "Word-abbrev table of mail address aliases.
|
|
159 If this is nil, it means the aliases have not yet been initialized and
|
|
160 should be read from the .mailrc file. (This is distinct from there being
|
|
161 no aliases, which is represented by this being a table with no entries.)")
|
|
162
|
|
163 ;;;###autoload
|
|
164 (defun mail-aliases-setup ()
|
|
165 (if (and (not (vectorp mail-aliases))
|
|
166 (file-exists-p (mail-abbrev-mailrc-file)))
|
|
167 (build-mail-aliases))
|
|
168 (make-local-variable 'pre-abbrev-expand-hook)
|
|
169 (setq pre-abbrev-expand-hook
|
|
170 (cond ((and (listp pre-abbrev-expand-hook)
|
|
171 (not (eq 'lambda (car pre-abbrev-expand-hook))))
|
|
172 (cons 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook))
|
|
173 (t
|
|
174 (list 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook))))
|
|
175 (abbrev-mode 1))
|
|
176
|
|
177 ;;; Originally defined in mailalias.el. Changed to call define-mail-alias
|
|
178 ;;; with an additional argument.
|
|
179 ;;;###autoload
|
|
180 (defun build-mail-aliases (&optional file recursivep)
|
|
181 "Read mail aliases from .mailrc and set mail-aliases."
|
|
182 (setq file (expand-file-name (or file (mail-abbrev-mailrc-file))))
|
|
183 (or (vectorp mail-aliases)
|
|
184 (setq mail-aliases (make-abbrev-table)))
|
|
185 (message "Parsing %s..." file)
|
|
186 (let ((buffer nil)
|
|
187 (obuf (current-buffer)))
|
|
188 (unwind-protect
|
|
189 (progn
|
|
190 (setq buffer (generate-new-buffer "mailrc"))
|
|
191 (buffer-disable-undo buffer)
|
|
192 (set-buffer buffer)
|
|
193 (cond ((get-file-buffer file)
|
|
194 (insert (save-excursion
|
|
195 (set-buffer (get-file-buffer file))
|
|
196 (buffer-substring (point-min) (point-max)))))
|
|
197 ((not (file-exists-p file)))
|
|
198 (t (insert-file-contents file)))
|
|
199 ;; Don't lose if no final newline.
|
|
200 (goto-char (point-max))
|
|
201 (or (eq (preceding-char) ?\n) (newline))
|
|
202 (goto-char (point-min))
|
|
203 ;; Delete comments from the file
|
|
204 (while (search-forward "# " nil t)
|
|
205 (let ((p (- (point) 2)))
|
|
206 (end-of-line)
|
|
207 (delete-region p (point))))
|
|
208 (goto-char (point-min))
|
|
209 ;; handle "\\\n" continuation lines
|
|
210 (while (not (eobp))
|
|
211 (end-of-line)
|
|
212 (if (= (preceding-char) ?\\)
|
|
213 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
|
|
214 (forward-char 1)))
|
|
215 (goto-char (point-min))
|
|
216 (while (re-search-forward
|
|
217 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t)
|
|
218 (beginning-of-line)
|
|
219 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
|
|
220 (progn
|
|
221 (end-of-line)
|
|
222 (build-mail-aliases
|
|
223 (substitute-in-file-name
|
|
224 (buffer-substring (match-beginning 1) (match-end 1)))
|
|
225 t))
|
|
226 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
|
|
227 (let* ((name (buffer-substring
|
|
228 (match-beginning 1) (match-end 1)))
|
|
229 (start (progn (skip-chars-forward " \t") (point))))
|
|
230 (end-of-line)
|
|
231 ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
|
|
232 (define-mail-alias
|
|
233 name
|
|
234 (buffer-substring start (point))
|
|
235 t))))
|
|
236 ;; Resolve forward references in .mailrc file.
|
|
237 ;; This would happen automatically before the first abbrev was
|
|
238 ;; expanded, but why not do it now.
|
|
239 (or recursivep (mail-resolve-all-aliases))
|
|
240 )
|
|
241 (if buffer (kill-buffer buffer))
|
|
242 (set-buffer obuf)))
|
|
243 (message "Parsing %s... done" file))
|
|
244
|
|
245 (defvar mail-alias-separator-string ", "
|
|
246 "*A string inserted between addresses in multi-address mail aliases.
|
|
247 This has to contain a comma, so \", \" is a reasonable value. You might
|
|
248 also want something like \",\\n \" to get each address on its own line.")
|
|
249
|
|
250 ;; define-mail-alias sets this flag, which causes mail-resolve-all-aliases
|
|
251 ;; to be called before expanding abbrevs if it's necessary.
|
|
252 (defvar mail-abbrev-aliases-need-to-be-resolved t)
|
|
253
|
|
254 ;; originally defined in mailalias.el ; build-mail-aliases calls this with
|
|
255 ;; stuff parsed from the .mailrc file.
|
|
256 ;;
|
|
257 ;;;###autoload
|
|
258 (defun define-mail-alias (name definition &optional from-mailrc-file)
|
|
259 "Define NAME as a mail-alias that translates to DEFINITION.
|
|
260 If DEFINITION contains multiple addresses, separate them with commas."
|
|
261 ;; When this is called from build-mail-aliases, the third argument is
|
|
262 ;; true, and we do some evil space->comma hacking like /bin/mail does.
|
|
263 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
|
|
264 ;; Read the defaults first, if we have not done so.
|
|
265 (if (vectorp mail-aliases)
|
|
266 nil
|
|
267 (setq mail-aliases (make-abbrev-table))
|
|
268 (if (file-exists-p (mail-abbrev-mailrc-file))
|
|
269 (build-mail-aliases)))
|
|
270 ;; strip garbage from front and end
|
|
271 (if (string-match "\\`[ \t\n,]+" definition)
|
|
272 (setq definition (substring definition (match-end 0))))
|
|
273 (if (string-match "[ \t\n,]+\\'" definition)
|
|
274 (setq definition (substring definition 0 (match-beginning 0))))
|
|
275 (let ((result '())
|
|
276 (start 0)
|
|
277 (L (length definition))
|
|
278 end)
|
|
279 (while start
|
|
280 ;; If we're reading from the mailrc file, then addresses are delimited
|
|
281 ;; by spaces, and addresses with embedded spaces must be surrounded by
|
|
282 ;; single or double-quotes. Otherwise, addresses are separated by
|
|
283 ;; commas.
|
|
284 (if from-mailrc-file
|
|
285 (cond ((eq ?\" (aref definition start))
|
|
286 (setq start (1+ start)
|
|
287 end (string-match "\"[ \t,]*" definition start)))
|
|
288 ((eq ?\' (aref definition start))
|
|
289 (setq start (1+ start)
|
|
290 end (string-match "\'[ \t,]*" definition start)))
|
|
291 (t
|
|
292 (setq end (string-match "[ \t,]+" definition start))))
|
|
293 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
|
|
294 (setq result (cons (substring definition start end) result))
|
|
295 (setq start (and end
|
|
296 (/= (match-end 0) L)
|
|
297 (match-end 0))))
|
|
298 (setq definition (mapconcat (function identity)
|
|
299 (nreverse result)
|
|
300 mail-alias-separator-string)))
|
|
301 (setq mail-abbrev-aliases-need-to-be-resolved t)
|
|
302 (setq name (downcase name))
|
|
303 ;; use an abbrev table instead of an alist for mail-aliases.
|
|
304 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed.
|
|
305 (define-abbrev mail-aliases name definition 'mail-abbrev-expand-hook)))
|
|
306
|
|
307
|
|
308 (defun mail-resolve-all-aliases ()
|
|
309 "Resolve all forward references in the mail aliases table."
|
|
310 (if mail-abbrev-aliases-need-to-be-resolved
|
|
311 (progn
|
|
312 ;; (message "Resolving mail aliases...")
|
|
313 (if (vectorp mail-aliases)
|
|
314 (mapatoms (function mail-resolve-all-aliases-1) mail-aliases))
|
|
315 (setq mail-abbrev-aliases-need-to-be-resolved nil)
|
|
316 ;; (message "Resolving mail aliases... done.")
|
|
317 )))
|
|
318
|
|
319 (defun mail-resolve-all-aliases-1 (sym &optional so-far)
|
|
320 (if (memq sym so-far)
|
|
321 (error "mail alias loop detected: %s"
|
|
322 (mapconcat 'symbol-name (cons sym so-far) " <- ")))
|
|
323 (let ((definition (and (boundp sym) (symbol-value sym))))
|
|
324 (if definition
|
|
325 (let ((result '())
|
|
326 (start 0))
|
|
327 (while start
|
|
328 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start)))
|
|
329 (setq result (cons (substring definition start end) result)
|
|
330 start (and end (match-end 0)))))
|
|
331 (setq definition
|
|
332 (mapconcat (function (lambda (x)
|
|
333 (or (mail-resolve-all-aliases-1
|
|
334 (intern-soft (downcase x) mail-aliases)
|
|
335 (cons sym so-far))
|
|
336 x)))
|
|
337 (nreverse result)
|
|
338 mail-alias-separator-string))
|
|
339 (set sym definition))))
|
|
340 (symbol-value sym))
|
|
341
|
|
342
|
|
343 (defun mail-abbrev-expand-hook ()
|
|
344 "For use as the fourth arg to define-abbrev.
|
|
345 After expanding a mail-abbrev, if fill-mode is on and we're past the
|
|
346 fill-column, break the line at the previous comma, and indent the next
|
|
347 line."
|
|
348 (save-excursion
|
|
349 (let ((p (point))
|
|
350 bol comma fp)
|
|
351 (beginning-of-line)
|
|
352 (setq bol (point))
|
|
353 (goto-char p)
|
|
354 (while (and auto-fill-function
|
|
355 (>= (current-column) fill-column)
|
|
356 (search-backward "," bol t))
|
|
357 (setq comma (point))
|
|
358 (forward-char 1) ; Now we are just past the comma.
|
|
359 (insert "\n")
|
|
360 (delete-horizontal-space)
|
|
361 (setq p (point))
|
10
|
362 ;; Prevent abbrev expansion from happening again, since
|
|
363 ;; sendmail-pre-abbrev-expand-hook will already have done it.
|
|
364 (let ((abbrev-mode nil))
|
|
365 (indent-relative))
|
0
|
366 (setq fp (buffer-substring p (point)))
|
|
367 ;; Go to the end of the new line.
|
|
368 (end-of-line)
|
|
369 (if (> (current-column) fill-column)
|
|
370 ;; It's still too long; do normal auto-fill.
|
|
371 (let ((fill-prefix (or fp "\t")))
|
|
372 (do-auto-fill)))
|
|
373 ;; Resume the search.
|
|
374 (goto-char comma)
|
|
375 ))))
|
|
376
|
|
377 ;;; Syntax tables and abbrev-expansion
|
|
378
|
|
379 (defvar mail-abbrev-mode-regexp
|
|
380 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
|
|
381 "*Regexp to select mail-headers in which mail aliases should be expanded.
|
|
382 This string it will be handed to `looking-at' with the point at the beginning
|
|
383 of the current line; if it matches, abbrev mode will be turned on, otherwise
|
|
384 it will be turned off. (You don't need to worry about continuation lines.)
|
|
385 This should be set to match those mail fields in which you want abbreviations
|
|
386 turned on.")
|
|
387
|
|
388 (defvar mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table)
|
|
389 "The syntax table which is used in send-mail mode message bodies.")
|
|
390
|
|
391 (defvar mail-mode-header-syntax-table
|
|
392 (let ((tab (copy-syntax-table text-mode-syntax-table)))
|
|
393 ;; This makes the characters "@%!._-" be considered symbol-consituents
|
|
394 ;; but not word-constituents, so forward-sexp will move you over an
|
|
395 ;; entire address, but forward-word will only move you over a sequence
|
|
396 ;; of alphanumerics. (Clearly the right thing.)
|
|
397 (modify-syntax-entry ?@ "_" tab)
|
|
398 (modify-syntax-entry ?% "_" tab)
|
|
399 (modify-syntax-entry ?! "_" tab)
|
|
400 (modify-syntax-entry ?. "_" tab)
|
|
401 (modify-syntax-entry ?_ "_" tab)
|
|
402 (modify-syntax-entry ?- "_" tab)
|
|
403 (modify-syntax-entry ?< "(>" tab)
|
|
404 (modify-syntax-entry ?> ")<" tab)
|
|
405 tab)
|
|
406 "The syntax table used in send-mail mode when in a mail-address header.
|
|
407 mail-mode-syntax-table is used when the cursor is in the message body or in
|
|
408 non-address headers.")
|
|
409
|
|
410 (defvar mail-abbrev-syntax-table
|
4
|
411 (if (fboundp 'map-syntax-table)
|
|
412 (let ((tab (copy-syntax-table mail-mode-header-syntax-table)))
|
|
413 (if (vectorp tab)
|
|
414 (let ((i (1- (length tab)))
|
|
415 (_ (aref (standard-syntax-table) ?_))
|
|
416 (w (aref (standard-syntax-table) ?w)))
|
|
417 (while (>= i 0)
|
|
418 (if (= (aref tab i) _) (aset tab i w))
|
|
419 (setq i (1- i))))
|
|
420 (map-syntax-table
|
|
421 #'(lambda (key val)
|
|
422 (if (eq (char-syntax-from-code val) ?_)
|
|
423 (put-char-table key (set-char-syntax-in-code val ?w) tab)
|
|
424 ))
|
|
425 tab))
|
|
426 tab)
|
|
427 (let* ((tab (copy-syntax-table mail-mode-header-syntax-table))
|
|
428 (i (1- (length tab)))
|
|
429 (_ (aref (standard-syntax-table) ?_))
|
|
430 (w (aref (standard-syntax-table) ?w)))
|
|
431 (while (>= i 0)
|
|
432 (if (= (aref tab i) _) (aset tab i w))
|
|
433 (setq i (1- i)))
|
|
434 tab))
|
0
|
435 "The syntax-table used for abbrev-expansion purposes; this is not actually
|
|
436 made the current syntax table of the buffer, but simply controls the set of
|
|
437 characters which may be a part of the name of a mail-alias.")
|
|
438
|
|
439
|
|
440 (defun mail-abbrev-in-expansion-header-p ()
|
|
441 "Whether point is in a mail-address header field."
|
|
442 (let ((case-fold-search t))
|
|
443 (and ;;
|
|
444 ;; we are on an appropriate header line...
|
|
445 (save-excursion
|
|
446 (beginning-of-line)
|
|
447 ;; skip backwards over continuation lines.
|
|
448 (while (and (looking-at "^[ \t]")
|
|
449 (not (= (point) (point-min))))
|
|
450 (forward-line -1))
|
|
451 ;; are we at the front of an appropriate header line?
|
|
452 (looking-at mail-abbrev-mode-regexp))
|
|
453 ;;
|
|
454 ;; ...and we are before the mail-header-separator
|
|
455 (< (point)
|
|
456 (save-excursion
|
|
457 (goto-char (point-min))
|
|
458 (search-forward (concat "\n" mail-header-separator "\n")
|
|
459 nil 0)
|
|
460 (point))))))
|
|
461
|
|
462 (defvar mail-mode-abbrev-table) ; quiet the compiler
|
|
463
|
|
464 (defun sendmail-pre-abbrev-expand-hook ()
|
|
465 (if mail-abbrev-aliases-need-to-be-resolved
|
|
466 (mail-resolve-all-aliases))
|
|
467 (if (and mail-aliases (not (eq mail-aliases t)))
|
|
468 (if (not (mail-abbrev-in-expansion-header-p))
|
|
469 ;;
|
|
470 ;; If we're not in a mail header in which mail aliases should
|
|
471 ;; be expanded, then use the normal mail-mode abbrev table (if any)
|
|
472 ;; and the normal mail-mode syntax table.
|
|
473 ;;
|
|
474 (progn
|
|
475 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table)
|
|
476 mail-mode-abbrev-table))
|
|
477 (set-syntax-table mail-mode-syntax-table))
|
|
478 ;;
|
|
479 ;; Otherwise, we are in a To: (or CC:, or whatever) header, and
|
|
480 ;; should use word-abbrevs to expand mail aliases.
|
|
481 ;; - First, install the mail-aliases as the word-abbrev table.
|
|
482 ;; - Then install the mail-abbrev-syntax-table, which temporarily
|
|
483 ;; marks all of the non-alphanumeric-atom-characters (the "_"
|
|
484 ;; syntax ones) as being normal word-syntax. We do this because
|
|
485 ;; the C code for expand-abbrev only works on words, and we want
|
|
486 ;; these characters to be considered words for the purpose of
|
|
487 ;; abbrev expansion.
|
|
488 ;; - Then we call expand-abbrev again, recursively, to do the abbrev
|
|
489 ;; expansion with the above syntax table.
|
|
490 ;; - Then we do a trick which tells the expand-abbrev frame which
|
|
491 ;; invoked us to not continue (and thus not expand twice.)
|
|
492 ;; This means that any abbrev expansion will happen as a result
|
|
493 ;; of this function's call to expand-abbrev, and not as a result
|
|
494 ;; of the call to expand-abbrev which invoked *us*.
|
|
495 ;; - Then we set the syntax table to mail-mode-header-syntax-table,
|
|
496 ;; which doesn't have anything to do with abbrev expansion, but
|
|
497 ;; is just for the user's convenience (see its doc string.)
|
|
498 ;;
|
|
499 (setq local-abbrev-table mail-aliases)
|
|
500 ;; If the character just typed was non-alpha-symbol-syntax, then don't
|
|
501 ;; expand the abbrev now (that is, don't expand when the user types -.)
|
|
502 ;; Check the character's syntax in the mail-mode-header-syntax-table.
|
|
503 (set-syntax-table mail-mode-header-syntax-table)
|
|
504 (or (and last-command-char
|
|
505 (eq (char-syntax last-command-char) ?_))
|
|
506 (let ((pre-abbrev-expand-hook nil)) ; That's us; don't loop.
|
|
507 ;; Use this table so that abbrevs can have hyphens in them.
|
|
508 (set-syntax-table mail-abbrev-syntax-table)
|
|
509 (expand-abbrev)
|
|
510 ;; Now set it back to what it was before.
|
|
511 (set-syntax-table mail-mode-header-syntax-table)))
|
|
512 (setq abbrev-start-location (point) ; This is the trick.
|
|
513 abbrev-start-location-buffer (current-buffer))
|
|
514 )))
|
|
515
|
|
516
|
|
517 ;;; Reading addresses from the minibuffer; by David Hughes <djh@Harston.CV.COM>
|
|
518
|
|
519 (defun mail-abbrev-minibuffer-setup-hook ()
|
|
520 ;; Use as the value of minibuffer-setup-hook when reading addresses
|
|
521 ;; from the minibuffer, as in:
|
|
522 ;; (let ((minibuffer-setup-hook 'mail-abbrev-minibuffer-setup-hook))
|
|
523 ;; (read-string "Who: "))
|
|
524 (if (and (not (vectorp mail-aliases))
|
|
525 (file-exists-p (mail-abbrev-mailrc-file)))
|
|
526 (build-mail-aliases))
|
|
527 (make-local-variable 'pre-abbrev-expand-hook)
|
|
528 (setq pre-abbrev-expand-hook
|
|
529 (function
|
|
530 (lambda ()
|
|
531 (setq local-abbrev-table mail-aliases)
|
|
532 (set-syntax-table mail-mode-header-syntax-table)
|
|
533 (or (and last-command-char
|
|
534 (eq (char-syntax last-command-char) ?_))
|
|
535 (let ((pre-abbrev-expand-hook nil)) ; That's us; don't loop.
|
|
536 ;; Use this table so that abbrevs can have hyphens in them.
|
|
537 (set-syntax-table mail-abbrev-syntax-table)
|
|
538 (expand-abbrev)
|
|
539 ;; Now set it back to what it was before.
|
|
540 (set-syntax-table mail-mode-header-syntax-table)))
|
|
541 (setq abbrev-start-location (point) ; This is the trick.
|
|
542 abbrev-start-location-buffer (current-buffer)))))
|
|
543 (abbrev-mode 1))
|
|
544
|
|
545
|
|
546 ;;; utilities
|
|
547
|
|
548 (defun merge-mail-aliases (file)
|
|
549 "Merge mail aliases from the given file with existing ones."
|
|
550 (interactive (list
|
|
551 (let ((insert-default-directory t)
|
|
552 (default-directory (expand-file-name "~/"))
|
|
553 (def (mail-abbrev-mailrc-file)))
|
|
554 (read-file-name
|
|
555 (format "Read additional aliases from file: (default %s) "
|
|
556 def)
|
|
557 default-directory
|
|
558 (expand-file-name def default-directory)
|
|
559 t))))
|
|
560 (build-mail-aliases file))
|
|
561
|
|
562 (defun rebuild-mail-aliases (file)
|
|
563 "Rebuild all the mail aliases from the given file."
|
|
564 (interactive (list
|
|
565 (let ((insert-default-directory t)
|
|
566 (default-directory (expand-file-name "~/"))
|
|
567 (def (mail-abbrev-mailrc-file)))
|
|
568 (read-file-name
|
|
569 (format "Read mail aliases from file: (default %s) " def)
|
|
570 default-directory
|
|
571 (expand-file-name def default-directory)
|
|
572 t))))
|
|
573 (setq mail-aliases nil)
|
|
574 (build-mail-aliases file))
|
|
575
|
|
576 (defun mail-interactive-insert-alias (&optional alias)
|
|
577 "Prompt for and insert a mail alias."
|
|
578 (interactive (progn
|
|
579 (if (not (vectorp mail-aliases)) (mail-aliases-setup))
|
|
580 (list (completing-read "Expand alias: " mail-aliases nil t))))
|
|
581 (if (not (vectorp mail-aliases)) (mail-aliases-setup))
|
|
582 (insert (or (and alias (symbol-value (intern-soft alias mail-aliases))) "")))
|
|
583
|
|
584 ;; call-interactively is so that zmacs-regions gets hacked correctly
|
|
585 ;; without making the interactive specs incompatible with v18.
|
|
586
|
|
587 (defun abbrev-hacking-next-line ()
|
|
588 "Just like `next-line' (\\<global-map>\\[next-line]) but expands abbrevs \
|
|
589 when at end of line."
|
|
590 (interactive)
|
|
591 (if (and (looking-at "[ \t]*\n")
|
|
592 (= (char-syntax (preceding-char)) ?w))
|
|
593 (expand-abbrev))
|
|
594 (setq this-command 'next-line)
|
|
595 (call-interactively 'next-line))
|
|
596
|
|
597 (defun abbrev-hacking-end-of-buffer ()
|
|
598 "Just like `end-of-buffer' (\\<global-map>\\[end-of-buffer]) but expands \
|
|
599 abbrevs when at end of buffer."
|
|
600 (interactive)
|
|
601 (if (and (looking-at "[ \t]*\n")
|
|
602 (= (char-syntax (preceding-char)) ?w))
|
|
603 (expand-abbrev))
|
|
604 (setq this-command 'end-of-buffer)
|
|
605 (call-interactively 'end-of-buffer))
|
|
606
|
|
607 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias)
|
|
608
|
|
609 ;(define-key mail-mode-map "\C-n" 'abbrev-hacking-next-line)
|
|
610 ;(define-key mail-mode-map "\M->" 'abbrev-hacking-end-of-buffer)
|
|
611 (let ((subst '((next-line . abbrev-hacking-next-line)
|
|
612 (fkey-next-line . abbrev-hacking-next-line)
|
|
613 (end-of-buffer . abbrev-hacking-end-of-buffer)
|
|
614 (fkey-end-of-buffer . abbrev-hacking-end-of-buffer)
|
|
615 )))
|
|
616 (while subst
|
|
617 (let ((keys
|
|
618 (delq nil
|
|
619 (nconc (where-is-internal (car (car subst)) mail-mode-map)
|
|
620 (where-is-internal (car (car subst)))))))
|
|
621 (while keys
|
|
622 (define-key mail-mode-map (car keys) (cdr (car subst)))
|
|
623 (setq keys (cdr keys))))
|
|
624 (setq subst (cdr subst))))
|
|
625
|
|
626 (provide 'mail-abbrevs)
|
|
627
|
|
628
|
|
629 ;;; V18 compatibility
|
|
630 ;;;
|
|
631 ;;; All of the Emacs18 stuff is isolated down here so that it will be
|
|
632 ;;; easy to delete once v18 finally bites the dust.
|
|
633 ;;;
|
|
634 ;;; These defuns and defvars aren't inside the cond in deference to
|
|
635 ;;; the intense brokenness of the v18 byte-compiler.
|
|
636 ;;;
|
|
637 ;;; All the code on this page is gross and hidious and awful and might
|
|
638 ;;; not even work all that well. Comfort yourself with knowing that the
|
|
639 ;;; v19 code above works wonderfully.
|
|
640
|
|
641 (defun sendmail-v18-self-insert-command (arg)
|
|
642 "Just like self-insert-command, but runs sendmail-pre-abbrev-expand-hook."
|
|
643 (interactive "p")
|
|
644 (if (not (and last-command-char
|
|
645 (eq (char-syntax last-command-char) ?w)))
|
|
646 (progn
|
|
647 (sendmail-pre-abbrev-expand-hook)
|
|
648 ;; Unhack expand-abbrev, so it will work right next time around.
|
|
649 (setq abbrev-start-location nil)))
|
|
650 ;; this is gross and wasteful.
|
|
651 (let ((abbrev-mode (if (mail-abbrev-in-expansion-header-p)
|
|
652 nil
|
|
653 abbrev-mode)))
|
|
654 (self-insert-command arg)))
|
|
655
|
|
656 (defun abbrev-hacking-next-line-v18 (arg)
|
|
657 (if (looking-at "[ \t]*\n") (sendmail-pre-abbrev-expand-hook))
|
|
658 (setq this-command 'next-line)
|
|
659 (next-line arg))
|
|
660
|
|
661 (defun abbrev-hacking-end-of-buffer-v18 (arg)
|
|
662 (if (looking-at "[ \t]*\n") (sendmail-pre-abbrev-expand-hook))
|
|
663 (setq this-command 'end-of-buffer)
|
|
664 (end-of-buffer arg))
|
|
665
|
|
666 (defvar mail-abbrevs-v18-map-munged nil)
|
|
667
|
|
668 (defun mail-abbrevs-v18-munge-map ()
|
|
669 ;; For every key that is bound to self-insert-command in global-map,
|
|
670 ;; bind that key to sendmail-self-insert-command in mail-mode-map.
|
|
671 ;; We used to do this by making the mail-mode-map be a non-sparse map,
|
|
672 ;; but that made the esc-map be shared in such a way that making a
|
|
673 ;; local meta binding in the mail-mode-map made a *global* binding
|
|
674 ;; instead. Yucko.
|
|
675 (let ((global-map (current-global-map))
|
|
676 new-bindings
|
|
677 (i 0))
|
|
678 (while (< i 128)
|
|
679 (if (eq 'self-insert-command (or (cdr (assq i mail-mode-map))
|
|
680 (aref global-map i)))
|
|
681 (setq new-bindings
|
|
682 (cons (cons i 'sendmail-v18-self-insert-command)
|
|
683 new-bindings)))
|
|
684 (setq i (1+ i)))
|
|
685 (setq mail-mode-map
|
|
686 (nconc (copy-keymap mail-mode-map) (nreverse new-bindings))))
|
|
687 (setq mail-abbrevs-v18-map-munged t))
|
|
688
|
|
689 (defun mail-aliases-setup-v18 ()
|
|
690 "Put this on `mail-setup-hook' to use mail-abbrevs."
|
|
691 (if (not (eq major-mode 'mail-mode))
|
|
692 nil
|
|
693 (or (and mail-mode-map (eq (current-local-map) mail-mode-map))
|
|
694 (error "shut 'er down clancy, she's suckin' mud"))
|
|
695 (if (and (not (vectorp mail-aliases))
|
|
696 (file-exists-p (mail-abbrev-mailrc-file)))
|
|
697 (build-mail-aliases))
|
|
698 (or mail-abbrevs-v18-map-munged (mail-abbrevs-v18-munge-map))
|
|
699 (use-local-map mail-mode-map)
|
|
700 (abbrev-mode 1)))
|
|
701
|
|
702
|
|
703 (cond ((or (string-match "^18\\." emacs-version)
|
|
704 (and (boundp 'epoch::version) epoch::version))
|
|
705 ;;
|
|
706 ;; v19 (and this code) uses a new name for this function.
|
|
707 (or (fboundp 'buffer-disable-undo)
|
|
708 (fset 'buffer-disable-undo 'buffer-flush-undo))
|
|
709 ;;
|
|
710 ;; v19 (and this code) uses a new name for auto-fill-hook (-function).
|
|
711 ;; Encapsulate the function that uses it to bind the new name.
|
|
712 (or (fboundp 'mail-abbrev-expand-hook-v19)
|
|
713 (fset 'mail-abbrev-expand-hook-v19
|
|
714 (symbol-function 'mail-abbrev-expand-hook)))
|
|
715 (fset 'mail-abbrev-expand-hook
|
|
716 (function (lambda ()
|
|
717 (let ((auto-fill-function auto-fill-hook))
|
|
718 (mail-abbrev-expand-hook-v19)))))
|
|
719 ;;
|
|
720 ;; Turn off the broken v18 code (that is still called from sendmail.el)
|
|
721 (fset 'expand-mail-aliases
|
|
722 (function (lambda (&rest args)
|
|
723 "Obsoleted by mail-abbrevs. Does nothing."
|
|
724 nil)))
|
|
725 ;;
|
|
726 ;; Redefine the abbrev-hacking functions. Yuck.
|
|
727 (fset 'abbrev-hacking-next-line
|
|
728 (function (lambda (p) (interactive "p")
|
|
729 (abbrev-hacking-next-line-v18 p))))
|
|
730 (fset 'abbrev-hacking-end-of-buffer
|
|
731 (function (lambda (p) (interactive "P")
|
|
732 (abbrev-hacking-end-of-buffer-v18 p))))
|
|
733 ;;
|
|
734 ;; Encapsulate mail-setup to do the necessary buffer initializations.
|
|
735 (or (fboundp 'mail-setup-v18)
|
|
736 (fset 'mail-setup-v18 (symbol-function 'mail-setup)))
|
|
737 (fset 'mail-setup
|
|
738 (function (lambda (&rest args)
|
|
739 (mail-aliases-setup-v18)
|
|
740 (apply 'mail-setup-v18 args))))
|
|
741
|
|
742 ;;
|
|
743 ;; Encapsulate VM's version of mail-setup as well, if vm-mail is
|
|
744 ;; defined as a function or as an autoload.
|
|
745 (cond ((and (fboundp 'vm-mail)
|
|
746 (if (eq 'autoload (car-safe (symbol-function 'vm-mail)))
|
|
747 (load (nth 1 (symbol-function 'vm-mail)) t)
|
|
748 t))
|
|
749 (or (fboundp 'vm-mail-internal-v18)
|
|
750 (fset 'vm-mail-internal-v18
|
|
751 (symbol-function 'vm-mail-internal)))
|
|
752 (fset 'vm-mail-internal
|
|
753 (function (lambda (&rest args)
|
|
754 (apply 'vm-mail-internal-v18 args)
|
|
755 (mail-aliases-setup-v18))))))
|
|
756
|
|
757 ;; If we're being loaded from mail-setup-hook or mail-mode-hook
|
|
758 ;; as run from inside mail-setup or vm-mail-internal, then install
|
|
759 ;; right now.
|
|
760 (if (eq major-mode 'mail-mode)
|
|
761 (mail-aliases-setup-v18))
|
|
762 )
|
|
763
|
|
764 (t ; v19
|
|
765 (fmakunbound 'expand-mail-aliases)))
|