(describe-function-1): Don't print extra newline
[bpt/emacs.git] / lisp / mail / mailabbrev.el
CommitLineData
55535639 1;;; mailabbrev.el --- abbrev-expansion of mail aliases
275da787 2
f2e3589a 3;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1996, 1997, 2000, 2001,
2f043267 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
275da787 5
fcc31755
DL
6;; Author: Jamie Zawinski <jwz@lucid.com>, now <jwz@jwz.org>
7;; Maintainer: FSF
275da787
RM
8;; Created: 19 Oct 90
9;; Keywords: mail
10
b578f267 11;; This file is part of GNU Emacs.
b01c3008 12
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
b01c3008 17
b578f267
EN
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
b01c3008 22
b578f267 23;; You should have received a copy of the GNU General Public License
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b01c3008 25
275da787
RM
26;;; Commentary:
27
0f965ba3 28;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
b578f267
EN
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
f6babbac 31;; `mail-personal-alias-file') if it exists. Your mail aliases will
b578f267
EN
32;; expand any time you type a word-delimiter at the end of an abbreviation.
33;;
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.
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
fba75b6b 43;; mode-specific abbrev table will be used if
b578f267
EN
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;;
85d0e9ed 51;; You can also insert a mail alias with mail-abbrev-insert-alias
b578f267
EN
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;;
70;; If you like, you can call the function define-mail-abbrev to define your
71;; mail aliases instead of using a .mailrc file. When you call it in this
72;; way, addresses are separated by commas.
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
80;; your .mailrc file, and use define-mail-abbrev to redefine them when sending
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
92;; precede the aliases of "group1" and "group2".
93;;
94;; This code also understands the "source" .mailrc command, for reading
95;; aliases from some other file as well.
96;;
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;;
101;; To read in the contents of another .mailrc-type file from emacs, use the
102;; command Meta-X merge-mail-abbrevs. The rebuild-mail-abbrevs command is
103;; similar, but will delete existing aliases first.
104;;
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;;
109;; (add-hook
bde4c7ce 110;; 'mail-mode-hook
c0df1972 111;; (lambda ()
6b823692
AS
112;; (define-key mail-mode-map [remap next-line] 'mail-abbrev-next-line)
113;; (define-key mail-mode-map [remap end-of-buffer] 'mail-abbrev-end-of-buffer)))
b578f267
EN
114;;
115;; If you want multiple addresses separated by a string other than ", " then
116;; you can set the variable mail-alias-separator-string to it. This has to
117;; be a comma bracketed by whitespace if you want any kind of reasonable
fffa137c 118;; behavior.
b578f267
EN
119;;
120;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
121;; Noah Friedman for suggestions and bug reports.
122
bde4c7ce 123;; To use this package, do (add-hook 'mail-mode-hook 'mail-abbrevs-setup).
b01c3008 124
275da787
RM
125;;; Code:
126
d075ed70
RS
127(eval-when-compile
128 (require 'sendmail))
b01c3008 129
7d4c958f
KH
130(defgroup mail-abbrev nil
131 "Expand mail aliases as abbrevs, in certain mail headers."
132 :group 'abbrev-mode)
133
dcbb251e
SM
134;;;###autoload
135(define-minor-mode mail-abbrevs-mode
136 "Non-nil means expand mail aliases as abbrevs, in certain message headers."
137 :global t
7d4c958f 138 :group 'mail-abbrev
dcbb251e
SM
139 :version "20.3"
140 (if mail-abbrevs-mode (mail-abbrevs-enable) (mail-abbrevs-disable)))
7d4c958f
KH
141
142(defcustom mail-abbrevs-only nil
dcbb251e 143 "Non-nil means only mail abbrevs should expand automatically.
7d4c958f
KH
144Other abbrevs expand only when you explicitly use `expand-abbrev'."
145 :type 'boolean
146 :group 'mail-abbrev)
147
b01c3008 148;; originally defined in sendmail.el - used to be an alist, now is a table.
753d16a6 149(defvar mail-abbrevs nil
22f4ef2e 150 "Word-abbrev table of mail address aliases.
b01c3008
RS
151If this is nil, it means the aliases have not yet been initialized and
152should be read from the .mailrc file. (This is distinct from there being
153no aliases, which is represented by this being a table with no entries.)")
154
00bafa45
RS
155(defvar mail-abbrev-modtime nil
156 "The modification time of your mail alias file when it was last examined.")
157
158(defun mail-abbrevs-sync-aliases ()
f7e85dcf
RS
159 (when mail-personal-alias-file
160 (if (file-exists-p mail-personal-alias-file)
161 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
162 (if (not (equal mail-abbrev-modtime modtime))
163 (progn
164 (setq mail-abbrev-modtime modtime)
165 (build-mail-abbrevs)))))))
00bafa45 166
aa228418 167;;;###autoload
753d16a6 168(defun mail-abbrevs-setup ()
17804728 169 "Initialize use of the `mailabbrev' package."
753d16a6 170 (if (and (not (vectorp mail-abbrevs))
a3039e32 171 (file-exists-p mail-personal-alias-file))
00bafa45 172 (progn
0f965ba3 173 (setq mail-abbrev-modtime
00bafa45
RS
174 (nth 5 (file-attributes mail-personal-alias-file)))
175 (build-mail-abbrevs)))
176 (mail-abbrevs-sync-aliases)
dcbb251e 177 (add-hook 'abbrev-expand-functions 'mail-abbrev-expand-wrapper nil t)
b01c3008
RS
178 (abbrev-mode 1))
179
7d4c958f 180(defun mail-abbrevs-enable ()
bde4c7ce 181 (add-hook 'mail-mode-hook 'mail-abbrevs-setup))
7d4c958f
KH
182
183(defun mail-abbrevs-disable ()
184 "Turn off use of the `mailabbrev' package."
bde4c7ce 185 (remove-hook 'mail-mode-hook 'mail-abbrevs-setup)
7d4c958f
KH
186 (abbrev-mode (if (default-value 'abbrev-mode) 1 -1)))
187
22f4ef2e 188;;;###autoload
753d16a6 189(defun build-mail-abbrevs (&optional file recursivep)
a3039e32
NF
190 "Read mail aliases from personal mail alias file and set `mail-abbrevs'.
191By default this is the file specified by `mail-personal-alias-file'."
192 (setq file (expand-file-name (or file mail-personal-alias-file)))
753d16a6 193 (if (vectorp mail-abbrevs)
b01c3008 194 nil
753d16a6
RS
195 (setq mail-abbrevs nil)
196 (define-abbrev-table 'mail-abbrevs '()))
5aefeeba 197 (message "Parsing %s..." file)
dcbb251e
SM
198 (with-temp-buffer
199 (buffer-disable-undo)
200 (cond ((get-file-buffer file)
201 (insert (with-current-buffer (get-file-buffer file)
202 (buffer-substring (point-min) (point-max)))))
203 ((not (file-exists-p file)))
204 (t (insert-file-contents file)))
205 ;; Don't lose if no final newline.
206 (goto-char (point-max))
207 (or (eq (preceding-char) ?\n) (newline))
208 (goto-char (point-min))
209 ;; Delete comments from the file
210 (while (search-forward "# " nil t)
211 (let ((p (- (point) 2)))
212 (end-of-line)
213 (delete-region p (point))))
214 (goto-char (point-min))
215 ;; handle "\\\n" continuation lines
216 (while (not (eobp))
217 (end-of-line)
218 (if (= (preceding-char) ?\\)
219 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
220 (forward-char 1)))
221 (goto-char (point-min))
222 (while (re-search-forward
223 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t)
224 (beginning-of-line)
225 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
226 (progn
227 (end-of-line)
228 (build-mail-abbrevs
229 (substitute-in-file-name
230 (buffer-substring (match-beginning 1) (match-end 1)))
231 t))
232 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
233 (let* ((name (buffer-substring
234 (match-beginning 1) (match-end 1)))
235 (start (progn (skip-chars-forward " \t") (point))))
236 (end-of-line)
237 ;; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
238 (define-mail-abbrev
239 name
240 (buffer-substring start (point))
241 t))))
242 ;; Resolve forward references in .mailrc file.
243 ;; This would happen automatically before the first abbrev was
244 ;; expanded, but why not do it now.
245 (or recursivep (mail-resolve-all-aliases))
246 mail-abbrevs)
247 (message "Parsing %s... done" file))
b01c3008 248
f22cd786 249(defvar mail-alias-separator-string ", "
b01c3008 250 "*A string inserted between addresses in multi-address mail aliases.
0f965ba3 251This has to contain a comma, so \", \" is a reasonable value. You might
b01c3008
RS
252also want something like \",\\n \" to get each address on its own line.")
253
51e4ece8 254;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases
b01c3008
RS
255;; to be called before expanding abbrevs if it's necessary.
256(defvar mail-abbrev-aliases-need-to-be-resolved t)
257
753d16a6 258;; originally defined in mailalias.el ; build-mail-abbrevs calls this with
b01c3008
RS
259;; stuff parsed from the .mailrc file.
260;;
aa228418 261;;;###autoload
51e4ece8 262(defun define-mail-abbrev (name definition &optional from-mailrc-file)
92182a70 263 "Define NAME as a mail alias abbrev that translates to DEFINITION.
f22cd786 264If DEFINITION contains multiple addresses, separate them with commas."
753d16a6 265 ;; When this is called from build-mail-abbrevs, the third argument is
b01c3008
RS
266 ;; true, and we do some evil space->comma hacking like /bin/mail does.
267 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
268 ;; Read the defaults first, if we have not done so.
dcbb251e 269 (unless (vectorp mail-abbrevs) (build-mail-abbrevs))
b01c3008
RS
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))))
8eb6c7da
RS
275 (let* ((result '())
276 (L (length definition))
277 (start (if (> L 0) 0))
278 end)
b01c3008
RS
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
f22cd786 282 ;; double-quotes. Otherwise, addresses are separated by commas.
b01c3008
RS
283 (if from-mailrc-file
284 (if (eq ?\" (aref definition start))
285 (setq start (1+ start)
286 end (string-match "\"[ \t,]*" definition start))
8eb6c7da
RS
287 (setq end (string-match "[ \t,]+" definition start)))
288 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
69ba53f7
RS
289 (let ((tem (substring definition start end)))
290 ;; Advance the loop past this address.
291 (setq start (and end
292 (/= (match-end 0) L)
293 (match-end 0)))
294 ;; If the full name contains a problem character, quote it.
295 (when (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" tem)
296 (if (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
297 (match-string 1 tem))
298 (setq tem (replace-regexp-in-string
299 "\\(.+?\\)[ \t]*\\(<.*>\\)" "\"\\1\" \\2"
300 tem))))
301 (push tem result)))
b01c3008
RS
302 (setq definition (mapconcat (function identity)
303 (nreverse result)
f22cd786 304 mail-alias-separator-string)))
b01c3008
RS
305 (setq mail-abbrev-aliases-need-to-be-resolved t)
306 (setq name (downcase name))
753d16a6 307 ;; use an abbrev table instead of an alist for mail-abbrevs.
b01c3008 308 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed.
8fad672b 309 (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook 0 t)))
b01c3008
RS
310
311
312(defun mail-resolve-all-aliases ()
313 "Resolve all forward references in the mail aliases table."
314 (if mail-abbrev-aliases-need-to-be-resolved
315 (progn
316;; (message "Resolving mail aliases...")
753d16a6
RS
317 (if (vectorp mail-abbrevs)
318 (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs))
b01c3008
RS
319 (setq mail-abbrev-aliases-need-to-be-resolved nil)
320;; (message "Resolving mail aliases... done.")
321 )))
322
d7c1ec4b
RM
323(defun mail-resolve-all-aliases-1 (sym &optional so-far)
324 (if (memq sym so-far)
325 (error "mail alias loop detected: %s"
326 (mapconcat 'symbol-name (cons sym so-far) " <- ")))
b01c3008
RS
327 (let ((definition (and (boundp sym) (symbol-value sym))))
328 (if definition
329 (let ((result '())
330 (start 0))
331 (while start
332 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start)))
333 (setq result (cons (substring definition start end) result)
334 start (and end (match-end 0)))))
335 (setq definition
336 (mapconcat (function (lambda (x)
337 (or (mail-resolve-all-aliases-1
e1d24183 338 (intern-soft (downcase x) mail-abbrevs)
d7c1ec4b 339 (cons sym so-far))
b01c3008
RS
340 x)))
341 (nreverse result)
f22cd786 342 mail-alias-separator-string))
b01c3008
RS
343 (set sym definition))))
344 (symbol-value sym))
345
346
f22cd786 347(defun mail-abbrev-expand-hook ()
92182a70
RS
348 "For use as the fourth arg to `define-abbrev'.
349After expanding a mail-abbrev, if Auto Fill mode is on and we're past the
350fill-column, break the line at the previous comma, and indent the next line."
0f965ba3
RM
351 ;; Disable abbrev mode to avoid recursion in indent-relative expanding
352 ;; part of the abbrev expansion as an abbrev itself.
353 (let ((abbrev-mode nil))
354 (save-excursion
355 (let ((p (point))
356 bol comma fp)
357 (beginning-of-line)
358 (setq bol (point))
359 (goto-char p)
360 (while (and auto-fill-function
361 (>= (current-column) fill-column)
362 (search-backward "," bol t))
363 (setq comma (point))
364 (forward-char 1) ; Now we are just past the comma.
365 (insert "\n")
366 (delete-horizontal-space)
367 (setq p (point))
368 (indent-relative)
369 (setq fp (buffer-substring p (point)))
370 ;; Go to the end of the new line.
371 (end-of-line)
372 (if (> (current-column) fill-column)
373 ;; It's still too long; do normal auto-fill.
374 (let ((fill-prefix (or fp "\t")))
375 (do-auto-fill)))
376 ;; Resume the search.
377 (goto-char comma)
378 )))))
22f4ef2e
RM
379\f
380;;; Syntax tables and abbrev-expansion
b01c3008 381
0f965ba3 382(defvar mail-abbrev-mode-regexp
450b15a0 383 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
92182a70 384 "*Regexp to select mail-headers in which mail abbrevs should be expanded.
fb11ccf6 385This string will be handed to `looking-at' with point at the beginning
b01c3008
RS
386of the current line; if it matches, abbrev mode will be turned on, otherwise
387it will be turned off. (You don't need to worry about continuation lines.)
388This should be set to match those mail fields in which you want abbreviations
389turned on.")
390
d075ed70 391(defvar mail-abbrev-syntax-table nil
4d2795c3
RS
392 "The syntax-table used for abbrev-expansion purposes.
393This is not actually made the current syntax table of the buffer, but
394simply controls the set of characters which may be a part of the name
d075ed70 395of a mail alias. The value is set up, buffer-local, when first needed.")
22f4ef2e 396
cf8b484f
RS
397(defun mail-abbrev-make-syntax-table ()
398 (make-local-variable 'mail-abbrev-syntax-table)
399 (unless mail-abbrev-syntax-table
f436b1cb 400 (let ((tab (copy-syntax-table (syntax-table)))
cf8b484f
RS
401 (_ (aref (standard-syntax-table) ?_))
402 (w (aref (standard-syntax-table) ?w)))
403 (map-char-table
404 (function (lambda (key value)
6946ae5b
AS
405 (if (null value)
406 ;; Fetch the inherited value
407 (setq value (aref tab key)))
cf8b484f
RS
408 (if (equal value _)
409 (set-char-table-range tab key w))))
410 tab)
411 (modify-syntax-entry ?@ "w" tab)
f8aba1ce
MR
412 (modify-syntax-entry ?% "w" tab)
413 (modify-syntax-entry ?! "w" tab)
414 (modify-syntax-entry ?. "w" tab)
415 (modify-syntax-entry ?_ "w" tab)
416 (modify-syntax-entry ?- "w" tab)
cf8b484f 417 (setq mail-abbrev-syntax-table tab))))
22f4ef2e
RM
418
419(defun mail-abbrev-in-expansion-header-p ()
420 "Whether point is in a mail-address header field."
421 (let ((case-fold-search t))
422 (and ;;
423 ;; we are on an appropriate header line...
424 (save-excursion
349deff4
GM
425 (unless (eobp) (forward-char 1))
426 (re-search-backward "^[^ \t]" nil 'move)
22f4ef2e
RM
427 ;; are we at the front of an appropriate header line?
428 (looking-at mail-abbrev-mode-regexp))
429 ;;
13aa1cde 430 ;; ...and are we in the headers?
8fad672b
RS
431 (< (point)
432 (save-restriction
433 (widen)
434 (save-excursion
435 (rfc822-goto-eoh)
436 (point)))))))
22f4ef2e 437
dcbb251e
SM
438(defun mail-abbrev-expand-wrapper (expand)
439 (if (and mail-abbrevs (not (eq mail-abbrevs t)))
440 (if (mail-abbrev-in-expansion-header-p)
441
442 ;; We are in a To: (or CC:, or whatever) header, and
443 ;; should use word-abbrevs to expand mail aliases.
444 (let ((local-abbrev-table mail-abbrevs))
445
446 ;; Before anything else, resolve aliases if they need it.
447 (and mail-abbrev-aliases-need-to-be-resolved
448 (mail-resolve-all-aliases))
449
450 ;; Now proceed with the abbrev section.
451 ;; - We already installed mail-abbrevs as the abbrev table.
452 ;; - Then install the mail-abbrev-syntax-table, which
453 ;; temporarily marks all of the
454 ;; non-alphanumeric-atom-characters (the "_"
455 ;; syntax ones) as being normal word-syntax. We do this
456 ;; because the C code for expand-abbrev only works on words,
457 ;; and we want these characters to be considered words for
458 ;; the purpose of abbrev expansion.
459 ;; - Then we call the expand function, to do
460 ;; the abbrev expansion with the above syntax table.
461
462 (mail-abbrev-make-syntax-table)
463
464 ;; If the character just typed was non-alpha-symbol-syntax,
465 ;; then don't expand the abbrev now (that is, don't expand
466 ;; when the user types -.) Check the character's syntax in
467 ;; the usual syntax table.
468
469 (or (and (integerp last-command-char)
470 ;; Some commands such as M-> may want to expand first.
471 (equal this-command 'self-insert-command)
472 (or (eq (char-syntax last-command-char) ?_)
473 ;; Don't expand on @.
474 (memq last-command-char '(?@ ?. ?% ?! ?_ ?-))))
475 ;; Use this table so that abbrevs can have hyphens in them.
476 (with-syntax-table mail-abbrev-syntax-table
477 (funcall expand))))
478
479 (if (or (not mail-abbrevs-only)
480 (eq this-command 'expand-abbrev))
481 ;; We're not in a mail header where mail aliases should
482 ;; be expanded, then use the normal mail-mode abbrev table
483 ;; (if any) and the normal mail-mode syntax table.
484 (funcall expand)
485 ;; This is not a mail abbrev, and we should not expand it.
486 ;; Don't expand anything.
487 nil))
488 ;; No mail-abbrevs at all, do the normal thing.
489 (funcall expand)))
22f4ef2e
RM
490\f
491;;; utilities
b01c3008 492
753d16a6 493(defun merge-mail-abbrevs (file)
b01c3008
RS
494 "Merge mail aliases from the given file with existing ones."
495 (interactive (list
496 (let ((insert-default-directory t)
497 (default-directory (expand-file-name "~/"))
a3039e32 498 (def mail-personal-alias-file))
b01c3008 499 (read-file-name
5b76833f 500 (format "Read additional aliases from file (default %s): "
b01c3008
RS
501 def)
502 default-directory
503 (expand-file-name def default-directory)
504 t))))
753d16a6 505 (build-mail-abbrevs file))
b01c3008 506
cf376527 507(defun rebuild-mail-abbrevs (&optional file)
b01c3008
RS
508 "Rebuild all the mail aliases from the given file."
509 (interactive (list
510 (let ((insert-default-directory t)
511 (default-directory (expand-file-name "~/"))
a3039e32 512 (def mail-personal-alias-file))
b01c3008 513 (read-file-name
5b76833f 514 (format "Read mail aliases from file (default %s): " def)
b01c3008
RS
515 default-directory
516 (expand-file-name def default-directory)
517 t))))
cf376527
RS
518 (if (null file)
519 (setq file buffer-file-name))
753d16a6
RS
520 (setq mail-abbrevs nil)
521 (build-mail-abbrevs file))
49116ac0 522
85d0e9ed 523(defun mail-abbrev-insert-alias (&optional alias)
22f4ef2e 524 "Prompt for and insert a mail alias."
c540863c 525 (interactive (progn
753d16a6
RS
526 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
527 (list (completing-read "Expand alias: " mail-abbrevs nil t))))
528 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
0eef787b
RS
529 (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) ""))
530 (mail-abbrev-expand-hook))
753d16a6 531
85d0e9ed
GM
532(defun mail-abbrev-complete-alias ()
533 "Perform completion on alias preceding point."
534 ;; Based on lisp.el:lisp-complete-symbol
535 (interactive)
da00bb18 536 (mail-abbrev-make-syntax-table)
85d0e9ed 537 (let* ((end (point))
dcbb251e
SM
538 (beg (with-syntax-table mail-abbrev-syntax-table
539 (save-excursion
540 (backward-word 1)
541 (point))))
542 (alias (buffer-substring beg end))
85d0e9ed
GM
543 (completion (try-completion alias mail-abbrevs)))
544 (cond ((eq completion t)
545 (message "%s" alias)) ; confirm
546 ((null completion)
547 (error "[Can't complete \"%s\"]" alias)) ; (message ...) (ding)
548 ((not (string= completion alias))
549 (delete-region beg end)
550 (insert completion))
551 (t (with-output-to-temp-buffer "*Completions*"
552 (display-completion-list
553 (prog2
554 (message "Making completion list...")
555 (all-completions alias mail-abbrevs)
f5fab556
MY
556 (message "Making completion list...done"))
557 alias))))))
85d0e9ed 558
753d16a6
RS
559(defun mail-abbrev-next-line (&optional arg)
560 "Expand any mail abbrev, then move cursor vertically down ARG lines.
561If there is no character in the target line exactly under the current column,
562the cursor is positioned after the character in that line which spans this
563column, or at the end of the line if it is not long enough.
564If there is no line in the buffer after this one,
565a newline character is inserted to create a line
566and the cursor moves to that line.
567
568The command \\[set-goal-column] can be used to create
569a semipermanent goal column to which this command always moves.
570Then it does not try to move vertically. This goal column is stored
571in `goal-column', which is nil when there is none.
572
573If you are thinking of using this in a Lisp program, consider
574using `forward-line' instead. It is usually easier to use
575and more reliable (no dependence on goal column, etc.)."
22f4ef2e 576 (interactive "p")
d7c1ec4b
RM
577 (if (looking-at "[ \t]*\n") (expand-abbrev))
578 (setq this-command 'next-line)
91dd4dc4 579 (with-no-warnings (next-line arg)))
22f4ef2e 580
753d16a6
RS
581(defun mail-abbrev-end-of-buffer (&optional arg)
582 "Expand any mail abbrev, then move point to end of buffer.
583Leave mark at previous position.
584With arg N, put point N/10 of the way from the true end.
585
586Don't use this command in Lisp programs!
587\(goto-char (point-max)) is faster and avoids clobbering the mark."
650d6486 588 (interactive "P")
d7c1ec4b
RM
589 (if (looking-at "[ \t]*\n") (expand-abbrev))
590 (setq this-command 'end-of-buffer)
69ba53f7
RS
591 (with-no-warnings
592 (end-of-buffer arg)))
22f4ef2e 593
f526e65a
RS
594(eval-after-load "sendmail"
595 '(progn
596 (define-key mail-mode-map "\C-c\C-a" 'mail-abbrev-insert-alias)
597 (define-key mail-mode-map "\e\t" ; like lisp-complete-symbol
598 'mail-abbrev-complete-alias)))
22f4ef2e 599
753d16a6
RS
600;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
601;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
b01c3008 602
3e1b7a46 603(provide 'mailabbrev)
b578f267 604
dcbb251e 605;; arch-tag: 5aa2d901-73f8-4ad7-b73c-4802282ad2ff
55535639 606;;; mailabbrev.el ends here