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