1 ;;; mh-identity.el --- Multiple Identify support for MH-E.
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
5 ;; Author: Peter S. Galbraith <psg@debian.org>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; Multiple identity support for MH-E.
31 ;; Used to easily set different fields such as From and Organization, as
32 ;; well as different signature files.
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.
40 ;; $Id: mh-identity.el,v 1.2 2003/02/03 20:55:30 wohler Exp $
47 (eval-when (compile load eval)
48 (defvar mh-comp-loaded nil)
49 (unless mh-comp-loaded
50 (setq mh-comp-loaded t)
51 (require 'mh-comp))) ;Since we do this on sending
53 (autoload 'mml-insert-tag "mml")
56 (defun mh-identity-make-menu ()
57 "Build (or rebuild) the Identity menu (e.g. after the list is modified)."
58 (when (and mh-identity-list (boundp 'mh-letter-mode-map))
59 (easy-menu-define mh-identity-menu mh-letter-mode-map
63 ;; Dynamically render :type corresponding to `mh-identity-list'
65 ;; ["home" (mh-insert-identity "home")
66 ;; :style radio :active (not (equal mh-identity-local "home"))
67 ;; :selected (equal mh-identity-local "home")]
70 `[,arg (mh-insert-identity ,arg) :style radio
71 :active (not (equal mh-identity-local ,arg))
72 :selected (equal mh-identity-local ,arg)]))
73 (mapcar 'car mh-identity-list))
75 ["none" (mh-insert-identity "none") mh-identity-local]
76 ["Set Default for Session"
77 (setq mh-identity-default mh-identity-local) t]
79 (customize-save-variable
80 'mh-identity-default mh-identity-local) t]
84 (defun mh-identity-list-set (symbol value)
85 "Update the `mh-identity-list' variable, and rebuild the menu.
86 Sets the default for SYMBOL (e.g. `mh-identity-list') to VALUE (as set in
87 customization). This is called after 'customize is used to alter
89 (set-default symbol value)
90 (mh-identity-make-menu))
92 (defvar mh-identity-local nil
93 "Buffer-local variable holding the identity currently in use.")
94 (make-variable-buffer-local 'mh-identity-local)
96 (defun mh-header-field-delete (field value-only)
97 "Delete FIELD in the mail header, or only its value if VALUE-ONLY is t.
98 Return t if anything is deleted."
99 (when (mh-goto-header-field field)
103 (delete-region (point)
104 (progn (mh-header-field-end)
105 (if (not value-only) (forward-char 1))
109 (defvar mh-identity-signature-start nil
110 "Marker for the beginning of a signature inserted by `mh-insert-identity'.")
111 (defvar mh-identity-signature-end nil
112 "Marker for the end of a signature inserted by `mh-insert-identity'.")
115 (defun mh-insert-identity (identity)
116 "Insert proper fields for given IDENTITY.
117 Edit the `mh-identity-list' variable to define identity."
119 (list (completing-read
121 (if mh-identity-local
123 (mapcar 'list (mapcar 'car mh-identity-list)))
124 (mapcar 'list (mapcar 'car mh-identity-list)))
127 ;;First remove old settings, if any.
128 (when mh-identity-local
129 (let ((pers-list (cadr (assoc mh-identity-local mh-identity-list))))
131 (let ((field (concat (caar pers-list) ":")))
133 ((string-equal "signature:" field)
134 (when (and (boundp 'mh-identity-signature-start)
135 (markerp mh-identity-signature-start))
136 (goto-char mh-identity-signature-start)
138 (delete-region (point) mh-identity-signature-end)))
139 ((mh-header-field-delete field nil))))
140 (setq pers-list (cdr pers-list)))))
141 ;; Then insert the replacement
142 (when (not (equal "none" identity))
143 (let ((pers-list (cadr (assoc identity mh-identity-list))))
145 (let ((field (concat (caar pers-list) ":"))
146 (value (cdar pers-list)))
148 ;; No value, remove field
151 (mh-header-field-delete field nil))
152 ;; Existing field, replace
153 ((mh-header-field-delete field t)
155 ;; Handle "signature" special case. Insert file or call function.
156 ((and (string-equal "signature:" field)
157 (or (and (stringp value)
158 (file-readable-p value))
160 (goto-char (point-max))
161 (if (not (looking-at "^$"))
165 (narrow-to-region (point) (point))
166 (set (make-local-variable 'mh-identity-signature-start)
168 (set-marker mh-identity-signature-start (point))
170 ;; If MIME composition done, insert signature at the end as
171 ;; an inline MIME part.
172 ((and (boundp 'mh-mhn-compose-insert-flag)
173 mh-mhn-compose-insert-flag)
174 (insert "#\n" "Content-Description: Signature\n"))
175 ((and (boundp 'mh-mml-compose-insert-flag)
176 mh-mml-compose-insert-flag)
177 (mml-insert-tag 'part 'type "text/plain"
178 'disposition "inline"
179 'description "Signature")))
181 (insert-file-contents value)
183 (goto-char (point-min))
184 (when (not (re-search-forward "^--" nil t))
185 (if (and (boundp 'mh-mhn-compose-insert-flag)
186 mh-mhn-compose-insert-flag)
188 (if (and (boundp 'mh-mml-compose-insert-flag)
189 mh-mml-compose-insert-flag)
192 (set (make-local-variable 'mh-identity-signature-end)
194 (set-marker mh-identity-signature-end (point-max))))
195 ;; Handle "From" field differently, adding it at the beginning.
196 ((string-equal "From:" field)
197 (goto-char (point-min))
198 (insert "From: " value "\n"))
199 ;; Skip empty signature (Can't remove what we don't know)
200 ((string-equal "signature:" field))
201 ;; Other field, add at end
202 (t ;Otherwise, add the end.
203 (goto-char (point-min))
204 (mh-goto-header-end 0)
205 (mh-insert-fields field value))))
206 (setq pers-list (cdr pers-list))))))
207 ;; Remember what is in use in this buffer
208 (if (equal "none" identity)
209 (setq mh-identity-local nil)
210 (setq mh-identity-local identity)))
212 (provide 'mh-identity)
215 ;;; indent-tabs-mode: nil
216 ;;; sentence-end-double-space: nil
219 ;;; mh-identity.el ends here