Mercurial > emacs
annotate lisp/mail/mailabbrev.el @ 14003:f2bec77fbc58
(regi-interpret): Doc fix.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Thu, 04 Jan 1996 23:47:41 +0000 |
parents | 2a2eaca9d6bb |
children | 83f275dcd93a |
rev | line source |
---|---|
1148 | 1 ;;; mailabbrev.el --- abbrev-expansion of mail aliases. |
2 | |
1832
f852ebc02465
(mail-abbrev-end-of-buffer): Changed interactive spec from "P" to "p".
Roland McGrath <roland@gnu.org>
parents:
1433
diff
changeset
|
3 ;;; Copyright (C) 1985, 1986, 1987, 1992, 1993 Free Software Foundation, Inc. |
1148 | 4 |
5 ;; Author: Jamie Zawinski <jwz@lucid.com> | |
6 ;; Maintainer: Jamie Zawinski <jwz@lucid.com> | |
7 ;; Created: 19 Oct 90 | |
8 ;; Keywords: mail | |
9 | |
459 | 10 ;;; This file is part of GNU Emacs. |
11 | |
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;;; it under the terms of the GNU General Public License as published by | |
717 | 14 ;;; the Free Software Foundation; either version 2, or (at your option) |
459 | 15 ;;; any later version. |
16 | |
17 ;;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;;; GNU General Public License for more details. | |
21 | |
22 ;;; You should have received a copy of the GNU General Public License | |
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to | |
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | |
1148 | 26 ;;; Commentary: |
27 | |
459 | 28 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From: |
29 ;;; field, word-abbrevs are defined for each of your mail aliases. These | |
30 ;;; aliases will be defined from your .mailrc file (or the file specified by | |
598 | 31 ;;; the MAILRC environment variable) if it exists. Your mail aliases will |
32 ;;; expand any time you type a word-delimiter at the end of an abbreviation. | |
459 | 33 ;;; |
13815 | 34 ;;; What you see is what you get: if mailabbrev is in use when you type |
35 ;;; a name, and the name does not expand, you know it is not an abbreviation. | |
36 ;;; However, if you yank abbreviations into the headers | |
37 ;;; in a way that bypasses the check for abbreviations, | |
38 ;;; they are expanded (but not visibly) when you send the message. | |
459 | 39 ;;; |
40 ;;; Your mail alias abbrevs will be in effect only when the point is in an | |
41 ;;; appropriate header field. When in the body of the message, or other | |
42 ;;; header fields, the mail aliases will not expand. Rather, the normal | |
43 ;;; mode-specific abbrev table (mail-mode-abbrev-table) will be used if | |
44 ;;; defined. So if you use mail-mode specific abbrevs, this code will not | |
45 ;;; adversely affect you. You can control which header fields the abbrevs | |
46 ;;; are used in by changing the variable mail-abbrev-mode-regexp. | |
47 ;;; | |
48 ;;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word | |
49 ;;; boundaries; also, header continuation-lines will be properly indented. | |
50 ;;; | |
51 ;;; You can also insert a mail alias with mail-interactive-insert-alias | |
52 ;;; (bound to C-c C-a), which prompts you for an alias (with completion) | |
53 ;;; and inserts its expansion at point. | |
54 ;;; | |
55 ;;; This file fixes a bug in the old system which prohibited your .mailrc | |
56 ;;; file from having lines like | |
57 ;;; | |
58 ;;; alias someone "John Doe <doe@quux.com>" | |
59 ;;; | |
60 ;;; That is, if you want an address to have embedded spaces, simply surround it | |
61 ;;; with double-quotes. This is necessary because the format of the .mailrc | |
62 ;;; file bogusly uses spaces as address delimiters. The following line defines | |
63 ;;; an alias which expands to three addresses: | |
64 ;;; | |
65 ;;; alias foobar addr-1 addr-2 "address three <addr-3>" | |
66 ;;; | |
67 ;;; (This is bogus because mail-delivery programs want commas, not spaces, | |
68 ;;; but that's what the file format is, so we have to live with it.) | |
69 ;;; | |
3742
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
70 ;;; If you like, you can call the function define-mail-abbrev to define your |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
71 ;;; mail aliases instead of using a .mailrc file. When you call it in this |
627 | 72 ;;; way, addresses are separated by commas. |
459 | 73 ;;; |
74 ;;; CAVEAT: This works on most Sun systems; I have been told that some versions | |
75 ;;; of /bin/mail do not understand double-quotes in the .mailrc file. So you | |
76 ;;; should make sure your version does before including verbose addresses like | |
77 ;;; this. One solution to this, if you are on a system whose /bin/mail doesn't | |
78 ;;; work that way, (and you still want to be able to /bin/mail to send mail in | |
79 ;;; addition to emacs) is to define minimal aliases (without full names) in | |
3742
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
80 ;;; your .mailrc file, and use define-mail-abbrev to redefine them when sending |
459 | 81 ;;; mail from emacs; this way, mail sent from /bin/mail will work, and mail |
82 ;;; sent from emacs will be pretty. | |
83 ;;; | |
84 ;;; Aliases in the mailrc file may be nested. If you define aliases like | |
85 ;;; alias group1 fred ethel | |
86 ;;; alias group2 larry curly moe | |
87 ;;; alias everybody group1 group2 | |
88 ;;; Then when you type "everybody" on the To: line, it will be expanded to | |
89 ;;; fred, ethyl, larry, curly, moe | |
90 ;;; | |
91 ;;; Aliases may also contain forward references; the alias of "everybody" can | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2418
diff
changeset
|
92 ;;; precede the aliases of "group1" and "group2". |
459 | 93 ;;; |
94 ;;; This code also understands the "source" .mailrc command, for reading | |
95 ;;; aliases from some other file as well. | |
96 ;;; | |
598 | 97 ;;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs |
98 ;;; normally cannot contain hyphens, but this code works around that for the | |
99 ;;; specific case of mail-alias word-abbrevs. | |
100 ;;; | |
459 | 101 ;;; To read in the contents of another .mailrc-type file from emacs, use the |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
102 ;;; command Meta-X merge-mail-abbrevs. The rebuild-mail-abbrevs command is |
459 | 103 ;;; similar, but will delete existing aliases first. |
104 ;;; | |
598 | 105 ;;; If you would like your aliases to be expanded when you type M-> or ^N to |
106 ;;; move out of the mail-header into the message body (instead of having to | |
107 ;;; type SPC at the end of the abbrev before moving away) then you can do | |
108 ;;; | |
13815 | 109 ;;; (add-hook |
110 ;;; 'mail-setup-hook | |
111 ;;; '(lambda () | |
112 ;;; (substitute-key-definition 'next-line 'mail-abbrev-next-line | |
113 ;;; mail-mode-map global-map) | |
114 ;;; (substitute-key-definition 'end-of-buffer 'mail-abbrev-end-of-buffer | |
115 ;;; mail-mode-map global-map))) | |
598 | 116 ;;; |
627 | 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 | |
459 | 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. | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
124 |
4317 | 125 ;;; To use this package, do (add-hook 'mail-setup-hook 'mail-abbrevs-setup). |
459 | 126 |
1148 | 127 ;;; Code: |
128 | |
459 | 129 (require 'sendmail) |
130 | |
131 ;; originally defined in sendmail.el - used to be an alist, now is a table. | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
132 (defvar mail-abbrevs nil |
598 | 133 "Word-abbrev table of mail address aliases. |
459 | 134 If this is nil, it means the aliases have not yet been initialized and |
135 should be read from the .mailrc file. (This is distinct from there being | |
136 no aliases, which is represented by this being a table with no entries.)") | |
137 | |
474 | 138 ;;;###autoload |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
139 (defun mail-abbrevs-setup () |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
140 (if (and (not (vectorp mail-abbrevs)) |
10328
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
141 (file-exists-p mail-personal-alias-file)) |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
142 (build-mail-abbrevs)) |
11059 | 143 (make-local-hook 'pre-abbrev-expand-hook) |
144 (add-hook 'pre-abbrev-expand-hook 'sendmail-pre-abbrev-expand-hook | |
145 nil t) | |
459 | 146 (abbrev-mode 1)) |
147 | |
598 | 148 ;;;###autoload |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
149 (defun build-mail-abbrevs (&optional file recursivep) |
10328
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
150 "Read mail aliases from personal mail alias file and set `mail-abbrevs'. |
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
151 By default this is the file specified by `mail-personal-alias-file'." |
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
152 (setq file (expand-file-name (or file mail-personal-alias-file))) |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
153 (if (vectorp mail-abbrevs) |
459 | 154 nil |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
155 (setq mail-abbrevs nil) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
156 (define-abbrev-table 'mail-abbrevs '())) |
606 | 157 (message "Parsing %s..." file) |
459 | 158 (let ((buffer nil) |
159 (obuf (current-buffer))) | |
160 (unwind-protect | |
161 (progn | |
162 (setq buffer (generate-new-buffer "mailrc")) | |
598 | 163 (buffer-disable-undo buffer) |
459 | 164 (set-buffer buffer) |
165 (cond ((get-file-buffer file) | |
166 (insert (save-excursion | |
167 (set-buffer (get-file-buffer file)) | |
168 (buffer-substring (point-min) (point-max))))) | |
169 ((not (file-exists-p file))) | |
170 (t (insert-file-contents file))) | |
171 ;; Don't lose if no final newline. | |
172 (goto-char (point-max)) | |
173 (or (eq (preceding-char) ?\n) (newline)) | |
174 (goto-char (point-min)) | |
175 ;; Delete comments from the file | |
176 (while (search-forward "# " nil t) | |
177 (let ((p (- (point) 2))) | |
178 (end-of-line) | |
179 (delete-region p (point)))) | |
180 (goto-char (point-min)) | |
181 ;; handle "\\\n" continuation lines | |
182 (while (not (eobp)) | |
183 (end-of-line) | |
184 (if (= (preceding-char) ?\\) | |
185 (progn (delete-char -1) (delete-char 1) (insert ?\ )) | |
186 (forward-char 1))) | |
187 (goto-char (point-min)) | |
188 (while (re-search-forward | |
907 | 189 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t) |
459 | 190 (beginning-of-line) |
191 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)") | |
192 (progn | |
193 (end-of-line) | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
194 (build-mail-abbrevs |
3945
7a8d0a08eac7
(build-mail-abbrevs): Do substitute-in-file-name on the abbrev, for `source'.
Richard M. Stallman <rms@gnu.org>
parents:
3742
diff
changeset
|
195 (substitute-in-file-name |
7939
338a91733ff9
(build-mail-abbrevs): Pass a recursivep argument in recursive call.
Richard M. Stallman <rms@gnu.org>
parents:
5559
diff
changeset
|
196 (buffer-substring (match-beginning 1) (match-end 1))) |
338a91733ff9
(build-mail-abbrevs): Pass a recursivep argument in recursive call.
Richard M. Stallman <rms@gnu.org>
parents:
5559
diff
changeset
|
197 t)) |
459 | 198 (re-search-forward "[ \t]+\\([^ \t\n]+\\)") |
199 (let* ((name (buffer-substring | |
200 (match-beginning 1) (match-end 1))) | |
201 (start (progn (skip-chars-forward " \t") (point)))) | |
202 (end-of-line) | |
203 ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1) | |
3742
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
204 (define-mail-abbrev |
459 | 205 name |
3742
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
206 (buffer-substring start (point)) |
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
207 t)))) |
459 | 208 ;; Resolve forward references in .mailrc file. |
209 ;; This would happen automatically before the first abbrev was | |
210 ;; expanded, but why not do it now. | |
211 (or recursivep (mail-resolve-all-aliases)) | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
212 mail-abbrevs) |
459 | 213 (if buffer (kill-buffer buffer)) |
214 (set-buffer obuf))) | |
606 | 215 (message "Parsing %s... done" file)) |
459 | 216 |
627 | 217 (defvar mail-alias-separator-string ", " |
459 | 218 "*A string inserted between addresses in multi-address mail aliases. |
219 This has to contain a comma, so \", \" is a reasonable value. You might | |
220 also want something like \",\\n \" to get each address on its own line.") | |
221 | |
3742
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
222 ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases |
459 | 223 ;; to be called before expanding abbrevs if it's necessary. |
224 (defvar mail-abbrev-aliases-need-to-be-resolved t) | |
225 | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
226 ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with |
459 | 227 ;; stuff parsed from the .mailrc file. |
228 ;; | |
474 | 229 ;;;###autoload |
3742
f5bbe69b8bc5
(define-mail-abbrev): Renamed from define-mail-alias.
Richard M. Stallman <rms@gnu.org>
parents:
3741
diff
changeset
|
230 (defun define-mail-abbrev (name definition &optional from-mailrc-file) |
11059 | 231 "Define NAME as a mail alias abbrev that translates to DEFINITION. |
627 | 232 If DEFINITION contains multiple addresses, separate them with commas." |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
233 ;; When this is called from build-mail-abbrevs, the third argument is |
459 | 234 ;; true, and we do some evil space->comma hacking like /bin/mail does. |
235 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ") | |
236 ;; Read the defaults first, if we have not done so. | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
237 (if (vectorp mail-abbrevs) |
459 | 238 nil |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
239 (setq mail-abbrevs nil) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
240 (define-abbrev-table 'mail-abbrevs '()) |
10328
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
241 (if (file-exists-p mail-personal-alias-file) |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
242 (build-mail-abbrevs))) |
459 | 243 ;; strip garbage from front and end |
244 (if (string-match "\\`[ \t\n,]+" definition) | |
245 (setq definition (substring definition (match-end 0)))) | |
246 (if (string-match "[ \t\n,]+\\'" definition) | |
247 (setq definition (substring definition 0 (match-beginning 0)))) | |
8215
767bef8ec35b
(define-mail-abbrev): Don't try to parse empty aliases.
Richard M. Stallman <rms@gnu.org>
parents:
7939
diff
changeset
|
248 (let* ((result '()) |
767bef8ec35b
(define-mail-abbrev): Don't try to parse empty aliases.
Richard M. Stallman <rms@gnu.org>
parents:
7939
diff
changeset
|
249 (L (length definition)) |
767bef8ec35b
(define-mail-abbrev): Don't try to parse empty aliases.
Richard M. Stallman <rms@gnu.org>
parents:
7939
diff
changeset
|
250 (start (if (> L 0) 0)) |
767bef8ec35b
(define-mail-abbrev): Don't try to parse empty aliases.
Richard M. Stallman <rms@gnu.org>
parents:
7939
diff
changeset
|
251 end) |
459 | 252 (while start |
253 ;; If we're reading from the mailrc file, then addresses are delimited | |
254 ;; by spaces, and addresses with embedded spaces must be surrounded by | |
627 | 255 ;; double-quotes. Otherwise, addresses are separated by commas. |
459 | 256 (if from-mailrc-file |
257 (if (eq ?\" (aref definition start)) | |
258 (setq start (1+ start) | |
259 end (string-match "\"[ \t,]*" definition start)) | |
8215
767bef8ec35b
(define-mail-abbrev): Don't try to parse empty aliases.
Richard M. Stallman <rms@gnu.org>
parents:
7939
diff
changeset
|
260 (setq end (string-match "[ \t,]+" definition start))) |
767bef8ec35b
(define-mail-abbrev): Don't try to parse empty aliases.
Richard M. Stallman <rms@gnu.org>
parents:
7939
diff
changeset
|
261 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) |
459 | 262 (setq result (cons (substring definition start end) result)) |
263 (setq start (and end | |
264 (/= (match-end 0) L) | |
265 (match-end 0)))) | |
266 (setq definition (mapconcat (function identity) | |
267 (nreverse result) | |
627 | 268 mail-alias-separator-string))) |
459 | 269 (setq mail-abbrev-aliases-need-to-be-resolved t) |
270 (setq name (downcase name)) | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
271 ;; use an abbrev table instead of an alist for mail-abbrevs. |
459 | 272 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed. |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
273 (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook))) |
459 | 274 |
275 | |
276 (defun mail-resolve-all-aliases () | |
277 "Resolve all forward references in the mail aliases table." | |
278 (if mail-abbrev-aliases-need-to-be-resolved | |
279 (progn | |
280 ;; (message "Resolving mail aliases...") | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
281 (if (vectorp mail-abbrevs) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
282 (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs)) |
459 | 283 (setq mail-abbrev-aliases-need-to-be-resolved nil) |
284 ;; (message "Resolving mail aliases... done.") | |
285 ))) | |
286 | |
717 | 287 (defun mail-resolve-all-aliases-1 (sym &optional so-far) |
288 (if (memq sym so-far) | |
289 (error "mail alias loop detected: %s" | |
290 (mapconcat 'symbol-name (cons sym so-far) " <- "))) | |
459 | 291 (let ((definition (and (boundp sym) (symbol-value sym)))) |
292 (if definition | |
293 (let ((result '()) | |
294 (start 0)) | |
295 (while start | |
296 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start))) | |
297 (setq result (cons (substring definition start end) result) | |
298 start (and end (match-end 0))))) | |
299 (setq definition | |
300 (mapconcat (function (lambda (x) | |
301 (or (mail-resolve-all-aliases-1 | |
12444
0f96d53b31b7
(mail-resolve-all-aliases-1):
Richard M. Stallman <rms@gnu.org>
parents:
11504
diff
changeset
|
302 (intern-soft (downcase x) mail-abbrevs) |
717 | 303 (cons sym so-far)) |
459 | 304 x))) |
305 (nreverse result) | |
627 | 306 mail-alias-separator-string)) |
459 | 307 (set sym definition)))) |
308 (symbol-value sym)) | |
309 | |
310 | |
627 | 311 (defun mail-abbrev-expand-hook () |
11059 | 312 "For use as the fourth arg to `define-abbrev'. |
313 After expanding a mail-abbrev, if Auto Fill mode is on and we're past the | |
314 fill-column, break the line at the previous comma, and indent the next line." | |
608 | 315 (save-excursion |
316 (let ((p (point)) | |
317 bol comma fp) | |
318 (beginning-of-line) | |
319 (setq bol (point)) | |
320 (goto-char p) | |
606 | 321 (while (and auto-fill-function |
608 | 322 (>= (current-column) fill-column) |
323 (search-backward "," bol t)) | |
324 (setq comma (point)) | |
325 (forward-char 1) ; Now we are just past the comma. | |
326 (insert "\n") | |
327 (delete-horizontal-space) | |
627 | 328 (setq p (point)) |
608 | 329 (indent-relative) |
330 (setq fp (buffer-substring p (point))) | |
331 ;; Go to the end of the new line. | |
332 (end-of-line) | |
333 (if (> (current-column) fill-column) | |
334 ;; It's still too long; do normal auto-fill. | |
335 (let ((fill-prefix (or fp "\t"))) | |
336 (do-auto-fill))) | |
337 ;; Resume the search. | |
338 (goto-char comma) | |
339 )))) | |
598 | 340 |
341 ;;; Syntax tables and abbrev-expansion | |
459 | 342 |
5559
e976a52296fc
(mail-abbrev-mode-regexp): Add Reply-to.
Richard M. Stallman <rms@gnu.org>
parents:
4805
diff
changeset
|
343 (defvar mail-abbrev-mode-regexp |
e976a52296fc
(mail-abbrev-mode-regexp): Add Reply-to.
Richard M. Stallman <rms@gnu.org>
parents:
4805
diff
changeset
|
344 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):" |
11059 | 345 "*Regexp to select mail-headers in which mail abbrevs should be expanded. |
10776
52b3af3cb71a
(mail-abbrev-mode-regexp): Doc fix.
Karl Heuer <kwzh@gnu.org>
parents:
10328
diff
changeset
|
346 This string will be handed to `looking-at' with point at the beginning |
459 | 347 of the current line; if it matches, abbrev mode will be turned on, otherwise |
348 it will be turned off. (You don't need to worry about continuation lines.) | |
349 This should be set to match those mail fields in which you want abbreviations | |
350 turned on.") | |
351 | |
352 (defvar mail-mode-header-syntax-table | |
353 (let ((tab (copy-syntax-table text-mode-syntax-table))) | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2418
diff
changeset
|
354 ;; This makes the characters "@%!._-" be considered symbol-constituents |
459 | 355 ;; but not word-constituents, so forward-sexp will move you over an |
356 ;; entire address, but forward-word will only move you over a sequence | |
357 ;; of alphanumerics. (Clearly the right thing.) | |
358 (modify-syntax-entry ?@ "_" tab) | |
359 (modify-syntax-entry ?% "_" tab) | |
360 (modify-syntax-entry ?! "_" tab) | |
361 (modify-syntax-entry ?. "_" tab) | |
362 (modify-syntax-entry ?_ "_" tab) | |
363 (modify-syntax-entry ?- "_" tab) | |
364 (modify-syntax-entry ?< "(>" tab) | |
365 (modify-syntax-entry ?> ")<" tab) | |
366 tab) | |
598 | 367 "The syntax table used in send-mail mode when in a mail-address header. |
11059 | 368 `mail-mode-syntax-table' is used when the cursor is in the message body or in |
598 | 369 non-address headers.") |
370 | |
371 (defvar mail-abbrev-syntax-table | |
372 (let* ((tab (copy-syntax-table mail-mode-header-syntax-table)) | |
373 (_ (aref (standard-syntax-table) ?_)) | |
374 (w (aref (standard-syntax-table) ?w))) | |
13258
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
375 (map-char-table |
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
376 (function (lambda (key value) |
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
377 (if (equal value _) |
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
378 (set-char-table-range tab key w)))) |
13362
1220026cd3a3
(mail-abbrev-syntax-table): Fix initialization.
Richard M. Stallman <rms@gnu.org>
parents:
13258
diff
changeset
|
379 tab) |
1220026cd3a3
(mail-abbrev-syntax-table): Fix initialization.
Richard M. Stallman <rms@gnu.org>
parents:
13258
diff
changeset
|
380 tab) |
13258
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
381 "The syntax-table used for abbrev-expansion purposes. |
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
382 This is not actually made the current syntax table of the buffer, but |
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
383 simply controls the set of characters which may be a part of the name |
2a96bf08bccb
(mail-abbrev-syntax-table): Rewrite using map-char-table.
Richard M. Stallman <rms@gnu.org>
parents:
13161
diff
changeset
|
384 of a mail alias.") |
598 | 385 |
459 | 386 |
598 | 387 (defun mail-abbrev-in-expansion-header-p () |
388 "Whether point is in a mail-address header field." | |
389 (let ((case-fold-search t)) | |
390 (and ;; | |
391 ;; we are on an appropriate header line... | |
392 (save-excursion | |
393 (beginning-of-line) | |
394 ;; skip backwards over continuation lines. | |
395 (while (and (looking-at "^[ \t]") | |
396 (not (= (point) (point-min)))) | |
397 (forward-line -1)) | |
398 ;; are we at the front of an appropriate header line? | |
399 (looking-at mail-abbrev-mode-regexp)) | |
400 ;; | |
401 ;; ...and we are before the mail-header-separator | |
402 (< (point) | |
403 (save-excursion | |
404 (goto-char (point-min)) | |
405 (search-forward (concat "\n" mail-header-separator "\n") | |
406 nil 0) | |
407 (point)))))) | |
408 | |
409 (defvar mail-mode-abbrev-table) ; quiet the compiler | |
410 | |
627 | 411 (defun sendmail-pre-abbrev-expand-hook () |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
412 (and (and mail-abbrevs (not (eq mail-abbrevs t))) |
1148 | 413 (if (mail-abbrev-in-expansion-header-p) |
414 (progn | |
415 ;; | |
416 ;; We are in a To: (or CC:, or whatever) header, and | |
417 ;; should use word-abbrevs to expand mail aliases. | |
418 | |
419 ;; Before anything else, resolve aliases if they need it. | |
420 (and mail-abbrev-aliases-need-to-be-resolved | |
421 (mail-resolve-all-aliases)) | |
422 | |
423 ;; Now proceed with the abbrev section. | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
424 ;; - First, install the mail-abbrevs as the word-abbrev table. |
1148 | 425 ;; - Then install the mail-abbrev-syntax-table, which |
426 ;; temporarily marks all of the | |
427 ;; non-alphanumeric-atom-characters (the "_" | |
428 ;; syntax ones) as being normal word-syntax. We do this | |
429 ;; because the C code for expand-abbrev only works on words, | |
430 ;; and we want these characters to be considered words for | |
431 ;; the purpose of abbrev expansion. | |
432 ;; - Then we call expand-abbrev again, recursively, to do | |
433 ;; the abbrev expansion with the above syntax table. | |
434 ;; - Then we do a trick which tells the expand-abbrev frame | |
435 ;; which invoked us to not continue (and thus not | |
436 ;; expand twice.) This means that any abbrev expansion | |
437 ;; will happen as a result of this function's call to | |
438 ;; expand-abbrev, and not as a result of the call to | |
439 ;; expand-abbrev which invoked *us*. | |
440 ;; - Then we set the syntax table to | |
441 ;; mail-mode-header-syntax-table, which doesn't have | |
442 ;; anything to do with abbrev expansion, but | |
443 ;; is just for the user's convenience (see its doc string.) | |
444 ;; | |
445 | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
446 (setq local-abbrev-table mail-abbrevs) |
1148 | 447 |
448 ;; If the character just typed was non-alpha-symbol-syntax, | |
449 ;; then don't expand the abbrev now (that is, don't expand | |
450 ;; when the user types -.) Check the character's syntax in | |
451 ;; the mail-mode-header-syntax-table. | |
452 | |
453 (set-syntax-table mail-mode-header-syntax-table) | |
3668
87f7ff6bba51
(sendmail-pre-abbrev-expand-hook):
Richard M. Stallman <rms@gnu.org>
parents:
3664
diff
changeset
|
454 (or (and (integerp last-command-char) |
87f7ff6bba51
(sendmail-pre-abbrev-expand-hook):
Richard M. Stallman <rms@gnu.org>
parents:
3664
diff
changeset
|
455 (eq (char-syntax last-command-char) ?_)) |
1148 | 456 (let ((pre-abbrev-expand-hook nil)) ; That's us; don't loop. |
457 ;; Use this table so that abbrevs can have hyphens in them. | |
458 (set-syntax-table mail-abbrev-syntax-table) | |
459 (expand-abbrev) | |
460 ;; Now set it back to what it was before. | |
461 (set-syntax-table mail-mode-header-syntax-table))) | |
11504
ae5b033ad31f
(sendmail-pre-abbrev-expand-hook): When setting the
Karl Heuer <kwzh@gnu.org>
parents:
11059
diff
changeset
|
462 (setq abbrev-start-location (point-max) ; This is the trick. |
1148 | 463 abbrev-start-location-buffer (current-buffer))) |
464 | |
465 ;; We're not in a mail header where mail aliases should | |
466 ;; be expanded, then use the normal mail-mode abbrev table | |
467 ;; (if any) and the normal mail-mode syntax table. | |
468 | |
469 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table) | |
470 mail-mode-abbrev-table)) | |
471 (set-syntax-table mail-mode-syntax-table)) | |
472 )) | |
598 | 473 |
474 ;;; utilities | |
459 | 475 |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
476 (defun merge-mail-abbrevs (file) |
459 | 477 "Merge mail aliases from the given file with existing ones." |
478 (interactive (list | |
479 (let ((insert-default-directory t) | |
480 (default-directory (expand-file-name "~/")) | |
10328
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
481 (def mail-personal-alias-file)) |
459 | 482 (read-file-name |
483 (format "Read additional aliases from file: (default %s) " | |
484 def) | |
485 default-directory | |
486 (expand-file-name def default-directory) | |
487 t)))) | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
488 (build-mail-abbrevs file)) |
459 | 489 |
12945
14e16370799d
(rebuild-mail-abbrevs): FILE defaults to visited file.
Richard M. Stallman <rms@gnu.org>
parents:
12444
diff
changeset
|
490 (defun rebuild-mail-abbrevs (&optional file) |
459 | 491 "Rebuild all the mail aliases from the given file." |
492 (interactive (list | |
493 (let ((insert-default-directory t) | |
494 (default-directory (expand-file-name "~/")) | |
10328
edd33989fb23
(mail-abbrev-mailrc-file): Variable and macro deleted.
Noah Friedman <friedman@splode.com>
parents:
8215
diff
changeset
|
495 (def mail-personal-alias-file)) |
459 | 496 (read-file-name |
497 (format "Read mail aliases from file: (default %s) " def) | |
498 default-directory | |
499 (expand-file-name def default-directory) | |
500 t)))) | |
12945
14e16370799d
(rebuild-mail-abbrevs): FILE defaults to visited file.
Richard M. Stallman <rms@gnu.org>
parents:
12444
diff
changeset
|
501 (if (null file) |
14e16370799d
(rebuild-mail-abbrevs): FILE defaults to visited file.
Richard M. Stallman <rms@gnu.org>
parents:
12444
diff
changeset
|
502 (setq file buffer-file-name)) |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
503 (setq mail-abbrevs nil) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
504 (build-mail-abbrevs file)) |
584 | 505 |
598 | 506 (defun mail-interactive-insert-alias (&optional alias) |
507 "Prompt for and insert a mail alias." | |
907 | 508 (interactive (progn |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
509 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup)) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
510 (list (completing-read "Expand alias: " mail-abbrevs nil t)))) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
511 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup)) |
13000
a86ad056a1cd
(mail-interactive-insert-alias): Call mail-abbrev-expand-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12956
diff
changeset
|
512 (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) "")) |
a86ad056a1cd
(mail-interactive-insert-alias): Call mail-abbrev-expand-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12956
diff
changeset
|
513 (mail-abbrev-expand-hook)) |
598 | 514 |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
515 (defun mail-abbrev-next-line (&optional arg) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
516 "Expand any mail abbrev, then move cursor vertically down ARG lines. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
517 If there is no character in the target line exactly under the current column, |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
518 the cursor is positioned after the character in that line which spans this |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
519 column, or at the end of the line if it is not long enough. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
520 If there is no line in the buffer after this one, |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
521 a newline character is inserted to create a line |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
522 and the cursor moves to that line. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
523 |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
524 The command \\[set-goal-column] can be used to create |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
525 a semipermanent goal column to which this command always moves. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
526 Then it does not try to move vertically. This goal column is stored |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
527 in `goal-column', which is nil when there is none. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
528 |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
529 If you are thinking of using this in a Lisp program, consider |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
530 using `forward-line' instead. It is usually easier to use |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
531 and more reliable (no dependence on goal column, etc.)." |
598 | 532 (interactive "p") |
717 | 533 (if (looking-at "[ \t]*\n") (expand-abbrev)) |
534 (setq this-command 'next-line) | |
598 | 535 (next-line arg)) |
536 | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
537 (defun mail-abbrev-end-of-buffer (&optional arg) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
538 "Expand any mail abbrev, then move point to end of buffer. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
539 Leave mark at previous position. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
540 With arg N, put point N/10 of the way from the true end. |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
541 |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
542 Don't use this command in Lisp programs! |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
543 \(goto-char (point-max)) is faster and avoids clobbering the mark." |
4034
9e585b42806d
(mail-abbrev-end-of-buffer): Fix interactive spec to "P", same as
Roland McGrath <roland@gnu.org>
parents:
3945
diff
changeset
|
544 (interactive "P") |
717 | 545 (if (looking-at "[ \t]*\n") (expand-abbrev)) |
546 (setq this-command 'end-of-buffer) | |
598 | 547 (end-of-buffer arg)) |
548 | |
549 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias) | |
550 | |
1432
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
551 ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line) |
7446ab662993
Delete version 18 compatibility stuff.
Richard M. Stallman <rms@gnu.org>
parents:
1150
diff
changeset
|
552 ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer) |
598 | 553 |
2418
3af5c7b9f4a1
Provide mailabbrev, not mail-abbrevs.
Richard M. Stallman <rms@gnu.org>
parents:
1832
diff
changeset
|
554 (provide 'mailabbrev) |