Initial Commit
[packages] / xemacs-packages / mh-e / mh-identity.el
1 ;;; mh-identity.el --- Multiple identify support for MH-E.
2
3 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Peter S. Galbraith <psg@debian.org>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
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 ;; Multiple identity support for MH-E.
30 ;;
31 ;; Used to easily set different fields such as From and Organization, as
32 ;; well as different signature files.
33 ;;
34 ;; Customize the variable `mh-identity-list' and an Identity menu will
35 ;; appear in mh-letter-mode.  The command 'mh-insert-identity can be used
36 ;; from the command line.
37
38 ;;; Change Log:
39
40 ;;; Code:
41
42
43 (require 'cl)
44
45 (eval-when (compile load eval)
46   (defvar mh-comp-loaded nil)
47   (unless mh-comp-loaded
48     (setq mh-comp-loaded t)
49     (require 'mh-comp)))                   ;Since we do this on sending
50
51 (autoload 'mml-insert-tag "mml")
52
53 ;;;###mh-autoload
54 (defun mh-identity-make-menu ()
55   "Build (or rebuild) the Identity menu (e.g. after the list is modified)."
56   (when (and mh-identity-list (boundp 'mh-letter-mode-map))
57     (easy-menu-define mh-identity-menu mh-letter-mode-map
58       "mh-e identity menu"
59       (append
60        '("Identity")
61        ;; Dynamically render :type corresponding to `mh-identity-list'
62        ;; e.g.:
63        ;;  ["home" (mh-insert-identity "home")
64        ;;   :style radio :active (not (equal mh-identity-local "home"))
65        ;;   :selected (equal mh-identity-local "home")]
66        (if mh-auto-fields-list
67            '(["insert auto fields" (mh-insert-auto-fields) t]
68              "--"))
69        (mapcar (function
70                 (lambda (arg)
71                   `[,arg  (mh-insert-identity ,arg) :style radio
72                           :active (not (equal mh-identity-local ,arg))
73                           :selected (equal mh-identity-local ,arg)]))
74                (mapcar 'car mh-identity-list))
75        '("--"
76          ["none" (mh-insert-identity "none") mh-identity-local]
77          ["Set Default for Session"
78           (setq mh-identity-default mh-identity-local) t]
79          ["Save as Default"
80           (customize-save-variable
81            'mh-identity-default mh-identity-local) t]
82          )))))
83
84 ;;;###mh-autoload
85 (defun mh-identity-list-set (symbol value)
86   "Update the `mh-identity-list' variable, and rebuild the menu.
87 Sets the default for SYMBOL (e.g. `mh-identity-list') to VALUE (as set in
88 customization).  This is called after 'customize is used to alter
89 `mh-identity-list'."
90   (set-default symbol value)
91   (mh-identity-make-menu))
92
93 (defvar mh-identity-local nil
94   "Buffer-local variable holding the identity currently in use.")
95 (make-variable-buffer-local 'mh-identity-local)
96
97 (defun mh-header-field-delete (field value-only)
98   "Delete FIELD in the mail header, or only its value if VALUE-ONLY is t.
99 Return t if anything is deleted."
100   (when (mh-goto-header-field field)
101     (if (not value-only)
102         (beginning-of-line)
103       (forward-char))
104     (delete-region (point)
105                    (progn (mh-header-field-end)
106                           (if (not value-only) (forward-char 1))
107                           (point)))
108     t))
109
110 (defvar mh-identity-signature-start nil
111   "Marker for the beginning of a signature inserted by `mh-insert-identity'.")
112 (defvar mh-identity-signature-end nil
113   "Marker for the end of a signature inserted by `mh-insert-identity'.")
114
115 ;;;###mh-autoload
116 (defun mh-insert-identity (identity)
117   "Insert proper fields for given IDENTITY.
118 Edit the `mh-identity-list' variable to define identity."
119   (interactive
120    (list (completing-read
121           "Identity: "
122           (if mh-identity-local
123               (cons '("none")
124                     (mapcar 'list (mapcar 'car mh-identity-list)))
125             (mapcar 'list (mapcar 'car mh-identity-list)))
126           nil t)))
127   (save-excursion
128     ;;First remove old settings, if any.
129     (when mh-identity-local
130       (let ((pers-list (cadr (assoc mh-identity-local mh-identity-list))))
131         (while pers-list
132           (let ((field (concat (caar pers-list) ":")))
133             (cond
134              ((string-equal "signature:" field)
135               (when (and (boundp 'mh-identity-signature-start)
136                          (markerp mh-identity-signature-start))
137                 (goto-char mh-identity-signature-start)
138                 (forward-char -1)
139                 (delete-region (point) mh-identity-signature-end)))
140              ((mh-header-field-delete field nil))))
141           (setq pers-list (cdr pers-list)))))
142     ;; Then insert the replacement
143     (when (not (equal "none" identity))
144       (let ((pers-list (cadr (assoc identity mh-identity-list))))
145         (while pers-list
146           (let ((field (concat (caar pers-list) ":"))
147                 (value (cdar pers-list)))
148             (cond
149              ;; No value, remove field
150              ((or (not value)
151                   (string= value ""))
152               (mh-header-field-delete field nil))
153              ;; Existing field, replace
154              ((mh-header-field-delete field t)
155               (insert value))
156              ;; Handle "signature" special case. Insert file or call function.
157              ((and (string-equal "signature:" field)
158                    (or (and (stringp value)
159                             (file-readable-p value))
160                        (fboundp value)))
161               (goto-char (point-max))
162               (if (not (looking-at "^$"))
163                   (insert "\n"))
164               (insert "\n")
165               (save-restriction
166                 (narrow-to-region (point) (point))
167                 (set (make-local-variable 'mh-identity-signature-start)
168                      (make-marker))
169                 (set-marker mh-identity-signature-start (point))
170                 (cond
171                  ;; If MIME composition done, insert signature at the end as
172                  ;; an inline MIME part.
173                  ((mh-mhn-directive-present-p)
174                   (insert "#\n" "Content-Description: Signature\n"))
175                  ((mh-mml-directive-present-p)
176                   (mml-insert-tag 'part 'type "text/plain"
177                                   'disposition "inline"
178                                   'description "Signature")))
179                 (if (stringp value)
180                     (insert-file-contents value)
181                   (funcall value))
182                 (goto-char (point-min))
183                 (when (not (re-search-forward "^--" nil t))
184                   (cond ((mh-mhn-directive-present-p)
185                          (forward-line 2))
186                         ((mh-mml-directive-present-p)
187                          (forward-line 1)))
188                   (insert "-- \n"))
189                 (set (make-local-variable 'mh-identity-signature-end)
190                      (make-marker))
191                 (set-marker mh-identity-signature-end (point-max))))
192              ;; Handle "From" field differently, adding it at the beginning.
193              ((string-equal "From:" field)
194               (goto-char (point-min))
195               (insert "From: " value "\n"))
196              ;; Skip empty signature (Can't remove what we don't know)
197              ((string-equal "signature:" field))
198              ;; Other field, add at end
199              (t                         ;Otherwise, add the end.
200               (goto-char (point-min))
201               (mh-goto-header-end 0)
202               (mh-insert-fields field value))))
203           (setq pers-list (cdr pers-list))))))
204   ;; Remember what is in use in this buffer
205   (if (equal "none" identity)
206       (setq mh-identity-local nil)
207     (setq mh-identity-local identity)))
208
209 (provide 'mh-identity)
210
211 ;;; Local Variables:
212 ;;; indent-tabs-mode: nil
213 ;;; sentence-end-double-space: nil
214 ;;; End:
215
216 ;;; mh-identity.el ends here