comparison lisp/mail/mailabbrev.el @ 598:75b23846ac96

*** empty log message ***
author Roland McGrath <roland@gnu.org>
date Tue, 24 Mar 1992 04:22:48 +0000
parents 4cd7543be581
children c1e8cc0b3fb5
comparison
equal deleted inserted replaced
597:434e13b1fc95 598:75b23846ac96
1 ;;; Abbrev-expansion of mail aliases. 1 ;;; Abbrev-expansion of mail aliases.
2 ;;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc. 2 ;;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
3 ;;; Created: 19 oct 90, Jamie Zawinski <jwz@lucid.com> 3 ;;; Created: 19 oct 90, Jamie Zawinski <jwz@lucid.com>
4 ;;; Last change 15-dec-91. jwz 4 ;;; Last change 16-mar-92. roland@gnu.ai.mit.edu
5 5
6 ;;; This file is part of GNU Emacs. 6 ;;; This file is part of GNU Emacs.
7 7
8 ;;; GNU Emacs is free software; you can redistribute it and/or modify 8 ;;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;;; it under the terms of the GNU General Public License as published by 9 ;;; it under the terms of the GNU General Public License as published by
20 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 20 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 21
22 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From: 22 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
23 ;;; field, word-abbrevs are defined for each of your mail aliases. These 23 ;;; field, word-abbrevs are defined for each of your mail aliases. These
24 ;;; aliases will be defined from your .mailrc file (or the file specified by 24 ;;; aliases will be defined from your .mailrc file (or the file specified by
25 ;;; the MAILRC environment variable) if it exists. Providing abbrev-mode is 25 ;;; the MAILRC environment variable) if it exists. Your mail aliases will
26 ;;; on in your send-mail buffer, your mail aliases will expand any time you 26 ;;; expand any time you type a word-delimiter at the end of an abbreviation.
27 ;;; type a word-delimiter at the end of an abbreviation.
28 ;;; 27 ;;;
29 ;;; What you see is what you get: no abbreviations will be expanded after you 28 ;;; 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 29 ;;; 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 30 ;;; 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, 31 ;;; address you typed is going to be rewritten, you know it immediately,
47 ;;; boundaries; also, header continuation-lines will be properly indented. 46 ;;; boundaries; also, header continuation-lines will be properly indented.
48 ;;; 47 ;;;
49 ;;; You can also insert a mail alias with mail-interactive-insert-alias 48 ;;; 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) 49 ;;; (bound to C-c C-a), which prompts you for an alias (with completion)
51 ;;; and inserts its expansion at point. 50 ;;; and inserts its expansion at point.
52 ;;;
53 ;;; To use this code, do something like
54 ;;;
55 ;;; (setq mail-mode-hook '(lambda () (require 'mail-abbrevs)))
56 ;;; 51 ;;;
57 ;;; This file fixes a bug in the old system which prohibited your .mailrc 52 ;;; This file fixes a bug in the old system which prohibited your .mailrc
58 ;;; file from having lines like 53 ;;; file from having lines like
59 ;;; 54 ;;;
60 ;;; alias someone "John Doe <doe@quux.com>" 55 ;;; alias someone "John Doe <doe@quux.com>"
94 ;;; preceed the aliases of "group1" and "group2". 89 ;;; preceed the aliases of "group1" and "group2".
95 ;;; 90 ;;;
96 ;;; This code also understands the "source" .mailrc command, for reading 91 ;;; This code also understands the "source" .mailrc command, for reading
97 ;;; aliases from some other file as well. 92 ;;; aliases from some other file as well.
98 ;;; 93 ;;;
94 ;;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs
95 ;;; normally cannot contain hyphens, but this code works around that for the
96 ;;; specific case of mail-alias word-abbrevs.
97 ;;;
99 ;;; To read in the contents of another .mailrc-type file from emacs, use the 98 ;;; 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 99 ;;; command Meta-X merge-mail-aliases. The rebuild-mail-aliases command is
101 ;;; similar, but will delete existing aliases first. 100 ;;; similar, but will delete existing aliases first.
101 ;;;
102 ;;; If you would like your aliases to be expanded when you type M-> or ^N to
103 ;;; move out of the mail-header into the message body (instead of having to
104 ;;; type SPC at the end of the abbrev before moving away) then you can do
105 ;;;
106 ;;; (define-key mail-mode-map "\C-n" 'abbrev-hacking-next-line)
107 ;;; (define-key mail-mode-map "\M->" 'abbrev-hacking-end-of-buffer)
102 ;;; 108 ;;;
103 ;;; If you want multiple addresses seperated by a string other than ", " then 109 ;;; If you want multiple addresses seperated by a string other than ", " then
104 ;;; you can set the variable mail-alias-seperator-string to it. This has to 110 ;;; you can set the variable mail-alias-seperator-string to it. This has to
105 ;;; be a comma bracketed by whitespace if you want any kind of reasonable 111 ;;; be a comma bracketed by whitespace if you want any kind of reasonable
106 ;;; behaviour. 112 ;;; behaviour.
118 (setq mail-abbrev-mailrc-file 124 (setq mail-abbrev-mailrc-file
119 (or (getenv "MAILRC") "~/.mailrc")))) 125 (or (getenv "MAILRC") "~/.mailrc"))))
120 126
121 ;; originally defined in sendmail.el - used to be an alist, now is a table. 127 ;; originally defined in sendmail.el - used to be an alist, now is a table.
122 (defvar mail-aliases nil 128 (defvar mail-aliases nil
123 "Abbrev table of mail address aliases. 129 "Word-abbrev table of mail address aliases.
124 If this is nil, it means the aliases have not yet been initialized and 130 If this is nil, it means the aliases have not yet been initialized and
125 should be read from the .mailrc file. (This is distinct from there being 131 should be read from the .mailrc file. (This is distinct from there being
126 no aliases, which is represented by this being a table with no entries.)") 132 no aliases, which is represented by this being a table with no entries.)")
127 133
128 ;;;###autoload 134 ;;;###autoload
129 (defun mail-aliases-setup () 135 (defun mail-aliases-setup ()
136 "Put on `mail-setup-hook' to use mail-abbrevs."
130 (if (and (not (vectorp mail-aliases)) 137 (if (and (not (vectorp mail-aliases))
131 (file-exists-p (mail-abbrev-mailrc-file))) 138 (file-exists-p (mail-abbrev-mailrc-file)))
132 (build-mail-aliases)) 139 (build-mail-aliases))
133 (make-local-variable 'pre-abbrev-expand-hook) 140 (if (boundp 'pre-abbrev-expand-hook)
134 (setq pre-abbrev-expand-hook 141 (progn
135 (cond ((and (listp pre-abbrev-expand-hook) 142 (make-local-variable 'pre-abbrev-expand-hook)
136 (not (eq 'lambda (car pre-abbrev-expand-hook)))) 143 (setq pre-abbrev-expand-hook
137 (cons 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook)) 144 (cond ((and (listp pre-abbrev-expand-hook)
138 (t 145 (not (eq 'lambda (car pre-abbrev-expand-hook))))
139 (list 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook)))) 146 (cons 'sendmail-pre-abbrev-expand-hook
147 pre-abbrev-expand-hook))
148 (t
149 (list 'sendmail-pre-abbrev-expand-hook
150 pre-abbrev-expand-hook)))))
151 (or mail-abbrevs-map-munged
152 (mail-abbrevs-munge-map))
153 (use-local-map mail-mode-map))
140 (abbrev-mode 1)) 154 (abbrev-mode 1))
155
156 (defvar mail-abbrevs-map-munged nil)
157 (defun mail-abbrevs-munge-map ()
158 ;; If mail-mode-map is a sparse-keymap, convert it to a non-sparse one.
159 ;; If a given key would be bound to self-insert-command in mail-mode (that
160 ;; is, it is bound to it in mail-mode-map or in global-map) then bind it
161 ;; to sendmail-self-insert-command in mail-mode-map.
162 (let* ((sparse-p (consp mail-mode-map))
163 (map (make-keymap))
164 (L (length map))
165 (i 0))
166 (while (< i L)
167 (let ((old (or (if sparse-p
168 (cdr (assq i mail-mode-map))
169 (aref mail-mode-map i))
170 (aref global-map i))))
171 (aset map i (if (eq old 'self-insert-command)
172 'sendmail-self-insert-command
173 old)))
174 (setq i (1+ i)))
175 (setq mail-mode-map map))
176 (setq mail-abbrevs-map-munged t))
177
178 (defun sendmail-self-insert-command (arg)
179 "Just like self-insert-command, except that, if `mail-aliases' is an abbrev
180 table, and point is in an appropriate header field of the message being
181 composed, then the local-abbrev-table will be set to mail-aliases. Otherwise
182 the local-abbrev-table is mail-mode-abbrev-table (the normal state). The
183 variable mail-abbrev-mode-regexp controls which header-fields use the
184 mail-aliases table."
185 (interactive "p")
186 (if (= (char-syntax last-command-char) ? )
187 (progn
188 (sendmail-pre-abbrev-expand-hook)
189 ;; Unhack expand-abbrev, so it will work right next time around.
190 (setq abbrev-start-location nil)))
191 (self-insert-command arg))
192
193 (defun expand-mail-aliases (&rest args)
194 "Obsoleted by mail-abbrevs. Does nothing."
195 nil)
196
197 (or (fboundp 'buffer-disable-undo)
198 (fset 'buffer-disable-undo 'buffer-flush-undo))
141 199
142 ;;; Originally defined in mailalias.el. Changed to call define-mail-alias 200 ;;; Originally defined in mailalias.el. Changed to call define-mail-alias
143 ;;; with an additional argument. 201 ;;; with an additional argument.
202 ;;;###autoload
144 (defun build-mail-aliases (&optional file recursivep) 203 (defun build-mail-aliases (&optional file recursivep)
145 "Read mail aliases from .mailrc and set mail-aliases." 204 "Read mail aliases from .mailrc and set mail-aliases."
146 (setq file (expand-file-name (or file (mail-abbrev-mailrc-file)))) 205 (setq file (expand-file-name (or file (mail-abbrev-mailrc-file))))
147 (if (vectorp mail-aliases) 206 (if (vectorp mail-aliases)
148 nil 207 nil
152 (let ((buffer nil) 211 (let ((buffer nil)
153 (obuf (current-buffer))) 212 (obuf (current-buffer)))
154 (unwind-protect 213 (unwind-protect
155 (progn 214 (progn
156 (setq buffer (generate-new-buffer "mailrc")) 215 (setq buffer (generate-new-buffer "mailrc"))
157 (buffer-flush-undo buffer) 216 (buffer-disable-undo buffer)
158 (set-buffer buffer) 217 (set-buffer buffer)
159 (cond ((get-file-buffer file) 218 (cond ((get-file-buffer file)
160 (insert (save-excursion 219 (insert (save-excursion
161 (set-buffer (get-file-buffer file)) 220 (set-buffer (get-file-buffer file))
162 (buffer-substring (point-min) (point-max))))) 221 (buffer-substring (point-min) (point-max)))))
295 (set sym definition)))) 354 (set sym definition))))
296 (symbol-value sym)) 355 (symbol-value sym))
297 356
298 357
299 (defun mail-abbrev-expand-hook () 358 (defun mail-abbrev-expand-hook ()
300 "For use as the fourth arg to `define-abbrev'. 359 "For use as the fourth arg to define-abbrev.
301 After expanding a mail alias, if Auto Fill mode is on and we're past the 360 After expanding a mail-abbrev, if fill-mode is on and we're past the
302 fill column, break the line at the previous comma, and indent the next line." 361 fill-column, break the line at the previous comma, and indent the next
362 line."
303 (save-excursion 363 (save-excursion
304 (let ((p (point)) 364 (let ((p (point))
305 bol) 365 (bol (save-excursion
366 (re-search-backward mail-abbrev-mode-regexp)
367 (match-end 0))))
306 (if (and (if (boundp 'auto-fill-function) 368 (if (and (if (boundp 'auto-fill-function)
307 auto-fill-function 369 auto-fill-function
308 auto-fill-hook) 370 auto-fill-hook)
309 (>= (current-column) fill-column)) 371 (or (>= (current-column) fill-column)
310 (progn 372 (> (count-lines bol p) 1)))
311 (beginning-of-line) 373 (let (fp)
312 (setq bol (point))
313 (goto-char p) 374 (goto-char p)
314 (if (search-backward "," bol t) 375 (while (search-backward "," bol t)
315 (progn 376 (save-excursion
316 (forward-char 1) 377 (forward-char 1)
317 (insert "\n "))) 378 (insert "\n")
379 (delete-horizontal-space)
380 (setq p (point))
381 (indent-relative)
382 (setq fp (buffer-substring p (point)))))
318 (if (> (current-column) fill-column) 383 (if (> (current-column) fill-column)
319 (let ((fill-prefix " ")) 384 (let ((fill-prefix (or fp "\t")))
320 (do-auto-fill))) 385 (do-auto-fill))))))))
321 ))))) 386
322 387
323 388 ;;; Syntax tables and abbrev-expansion
324 (defun mail-interactive-insert-alias (&optional alias) 389
325 "Prompt for and insert a mail alias." 390 (defvar mail-abbrev-mode-regexp "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\):"
326 (interactive (list (completing-read "Expand alias: " mail-aliases nil t)))
327 (insert (or (and alias (symbol-value (intern-soft alias mail-aliases))) "")))
328
329 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias)
330
331
332 (defvar mail-abbrev-mode-regexp "^\\(To\\|From\\|CC\\|BCC\\):"
333 "*Regexp to select mail-headers in which mail-aliases should be expanded. 391 "*Regexp to select mail-headers in which mail-aliases should be expanded.
334 This string it will be handed to `looking-at' with the point at the beginning 392 This string it will be handed to `looking-at' with the point at the beginning
335 of the current line; if it matches, abbrev mode will be turned on, otherwise 393 of the current line; if it matches, abbrev mode will be turned on, otherwise
336 it will be turned off. (You don't need to worry about continuation lines.) 394 it will be turned off. (You don't need to worry about continuation lines.)
337 This should be set to match those mail fields in which you want abbreviations 395 This should be set to match those mail fields in which you want abbreviations
338 turned on.") 396 turned on.")
339 397
340 (defvar mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table) 398 (defvar mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table)
341 "The syntax table which is current in mail mode.") 399 "The syntax table which is used in send-mail mode message bodies.")
342 400
343 (defvar mail-mode-header-syntax-table 401 (defvar mail-mode-header-syntax-table
344 (let ((tab (copy-syntax-table text-mode-syntax-table))) 402 (let ((tab (copy-syntax-table text-mode-syntax-table)))
345 ;; This makes the caracters "@%!._-" be considered symbol-consituents 403 ;; This makes the caracters "@%!._-" be considered symbol-consituents
346 ;; but not word-constituents, so forward-sexp will move you over an 404 ;; but not word-constituents, so forward-sexp will move you over an
358 ;; Do this if you want to have aliases with hyphens in them. This causes 416 ;; Do this if you want to have aliases with hyphens in them. This causes
359 ;; hyphens to be considered word-syntax, so forward-word will not stop at 417 ;; hyphens to be considered word-syntax, so forward-word will not stop at
360 ;; hyphens. 418 ;; hyphens.
361 ;;(modify-syntax-entry ?- "w" tab) 419 ;;(modify-syntax-entry ?- "w" tab)
362 tab) 420 tab)
363 "The syntax table used when the cursor is in a mail-address header. 421 "The syntax table used in send-mail mode when in a mail-address header.
364 mail-mode-syntax-table is used when the cursor is not in an address header.") 422 mail-mode-syntax-table is used when the cursor is in the message body or in
365 423 non-address headers.")
366 ;; This hook is run before trying to expand an abbrev in a mail buffer. 424
367 ;; It determines whether point is in the header, and chooses which 425 (defvar mail-abbrev-syntax-table
368 ;; abbrev table accordingly. 426 (let* ((tab (copy-syntax-table mail-mode-header-syntax-table))
427 (i (1- (length tab)))
428 (_ (aref (standard-syntax-table) ?_))
429 (w (aref (standard-syntax-table) ?w)))
430 (while (>= i 0)
431 (if (= (aref tab i) _) (aset tab i w))
432 (setq i (1- i)))
433 tab)
434 "The syntax-table used for abbrev-expansion purposes; this is not actually
435 made the current syntax table of the buffer, but simply controls the set of
436 characters which may be a part of the name of a mail-alias.")
437
438
439 (defun mail-abbrev-in-expansion-header-p ()
440 "Whether point is in a mail-address header field."
441 (let ((case-fold-search t))
442 (and ;;
443 ;; we are on an appropriate header line...
444 (save-excursion
445 (beginning-of-line)
446 ;; skip backwards over continuation lines.
447 (while (and (looking-at "^[ \t]")
448 (not (= (point) (point-min))))
449 (forward-line -1))
450 ;; are we at the front of an appropriate header line?
451 (looking-at mail-abbrev-mode-regexp))
452 ;;
453 ;; ...and we are before the mail-header-separator
454 (< (point)
455 (save-excursion
456 (goto-char (point-min))
457 (search-forward (concat "\n" mail-header-separator "\n")
458 nil 0)
459 (point))))))
460
461 (defvar mail-mode-abbrev-table) ; quiet the compiler
462
369 (defun sendmail-pre-abbrev-expand-hook () 463 (defun sendmail-pre-abbrev-expand-hook ()
370 (if mail-abbrev-aliases-need-to-be-resolved 464 (if mail-abbrev-aliases-need-to-be-resolved
371 (mail-resolve-all-aliases)) 465 (mail-resolve-all-aliases))
372 (if (and mail-aliases (not (eq mail-aliases t))) 466 (if (and mail-aliases (not (eq mail-aliases t)))
373 (let ((case-fold-search t)) 467 (if (not (mail-abbrev-in-expansion-header-p))
374 (if (and ;; 468 ;;
375 ;; we are on an appropriate header line... 469 ;; If we're not in a mail header in which mail aliases should
376 (save-excursion 470 ;; be expanded, then use the normal mail-mode abbrev table (if any)
377 (beginning-of-line) 471 ;; and the normal mail-mode syntax table.
378 ;; skip backwards over continuation lines. 472 ;;
379 (while (and (looking-at "^[ \t]")
380 (not (= (point) (point-min))))
381 (forward-line -1))
382 ;; are we at the front of an appropriate header line?
383 (looking-at mail-abbrev-mode-regexp))
384 ;;
385 ;; ...and we are before the mail-header-separator
386 (< (point)
387 (save-excursion
388 (goto-char (point-min))
389 (search-forward (concat "\n" mail-header-separator "\n")
390 nil 0)
391 (point))))
392 ;; install the mail-aliases abbrev and syntax tables...
393 (progn
394 (setq local-abbrev-table mail-aliases)
395 (set-syntax-table mail-mode-header-syntax-table))
396 ;; or install the normal mail-mode abbrev table (likely empty).
397 (progn 473 (progn
398 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table) 474 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table)
399 mail-mode-abbrev-table)) 475 mail-mode-abbrev-table))
400 (set-syntax-table mail-mode-syntax-table)))))) 476 (set-syntax-table mail-mode-syntax-table))
401 477 ;;
478 ;; Otherwise, we are in a To: (or CC:, or whatever) header, and
479 ;; should use word-abbrevs to expand mail aliases.
480 ;; - First, install the mail-aliases as the word-abbrev table.
481 ;; - Then install the mail-abbrev-syntax-table, which temporarily
482 ;; marks all of the non-alphanumeric-atom-characters (the "_"
483 ;; syntax ones) as being normal word-syntax. We do this because
484 ;; the C code for expand-abbrev only works on words, and we want
485 ;; these characters to be considered words for the purpose of
486 ;; abbrev expansion.
487 ;; - Then we call expand-abbrev again, recursively, to do the abbrev
488 ;; expansion with the above syntax table.
489 ;; - Then we do a trick which tells the expand-abbrev frame which
490 ;; invoked us to not continue (and thus not expand twice.)
491 ;; - Then we set the syntax table to mail-mode-header-syntax-table,
492 ;; which doesn't have anything to do with abbrev expansion, but
493 ;; is just for the user's convenience (see its doc string.)
494 ;;
495 (setq local-abbrev-table mail-aliases)
496 ;; If the character just typed was non-alpha-symbol-syntax, then don't
497 ;; expand the abbrev now (that is, don't expand when the user types -.)
498 (or (= (char-syntax last-command-char) ?_)
499 (let ((pre-abbrev-expand-hook nil)) ; that's us; don't loop
500 (set-syntax-table mail-abbrev-syntax-table)
501 (expand-abbrev)))
502 (setq abbrev-start-location (point) ; this is the trick
503 abbrev-start-location-buffer (current-buffer))
504 ;; and do this just because.
505 (set-syntax-table mail-mode-header-syntax-table)
506 )))
507
508
509 ;;; utilities
402 510
403 (defun merge-mail-aliases (file) 511 (defun merge-mail-aliases (file)
404 "Merge mail aliases from the given file with existing ones." 512 "Merge mail aliases from the given file with existing ones."
405 (interactive (list 513 (interactive (list
406 (let ((insert-default-directory t) 514 (let ((insert-default-directory t)
426 (expand-file-name def default-directory) 534 (expand-file-name def default-directory)
427 t)))) 535 t))))
428 (setq mail-aliases nil) 536 (setq mail-aliases nil)
429 (build-mail-aliases file)) 537 (build-mail-aliases file))
430 538
539 (defun mail-interactive-insert-alias (&optional alias)
540 "Prompt for and insert a mail alias."
541 (interactive (list (completing-read "Expand alias: " mail-aliases nil t)))
542 (if alias
543 (insert alias)))
544
545 (defun abbrev-hacking-next-line (&optional arg)
546 "Just like `next-line' (\\[next-line]) but expands abbrevs when at \
547 end of line."
548 (interactive "p")
549 (if (looking-at "[ \t]*\n") (expand-abbrev))
550 (next-line arg))
551
552 (defun abbrev-hacking-end-of-buffer (&optional arg)
553 "Just like `end-of-buffer' (\\[end-of-buffer]) but expands abbrevs when at \
554 end of line."
555 (interactive "P")
556 (if (looking-at "[ \t]*\n") (expand-abbrev))
557 (end-of-buffer arg))
558
559 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias)
560
561 (define-key mail-mode-map "\C-n" 'abbrev-hacking-next-line)
562 (define-key mail-mode-map "\M->" 'abbrev-hacking-end-of-buffer)
563
564
565 ;;; Patching it in:
566 ;;; Remove the entire file mailalias.el
567 ;;; Remove the definition of mail-aliases from sendmail.el
568 ;;; Add a call to mail-aliases-setup to mail-setup in sendmail.el
569 ;;; Remove the call to expand-mail-aliases from sendmail-send-it in sendmail.el
570 ;;; Remove the autoload of expand-mail-aliases from sendmail.el
571 ;;; Remove the autoload of build-mail-aliases from sendmail.el
572 ;;; Add an autoload of define-mail-alias
573
431 (provide 'mail-abbrevs) 574 (provide 'mail-abbrevs)
432