home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac_os2 / e31el3.zip / EMACS / 19.31 / LISP / MAILABBR.EL < prev    next >
Text File  |  1996-03-22  |  24KB  |  574 lines

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