1 ;;; mh-identity.el --- Multiple identify support for MH-E.
3 ;; Copyright (C) 2002, 2003 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.
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
51 (autoload 'mml-insert-tag "mml")
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
61 ;; Dynamically render :type corresponding to `mh-identity-list'
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]
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))
76 ["none" (mh-insert-identity "none") mh-identity-local]
77 ["Set Default for Session"
78 (setq mh-identity-default mh-identity-local) t]
80 (customize-save-variable
81 'mh-identity-default mh-identity-local) t]
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
90 (set-default symbol value)
91 (mh-identity-make-menu))
93 (defvar mh-identity-local nil
94 "Buffer-local variable holding the identity currently in use.")
95 (make-variable-buffer-local 'mh-identity-local)
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)
104 (delete-region (point)
105 (progn (mh-header-field-end)
106 (if (not value-only) (forward-char 1))
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'.")
116 (defun mh-insert-identity (identity)
117 "Insert proper fields for given IDENTITY.
118 Edit the `mh-identity-list' variable to define identity."
120 (list (completing-read
122 (if mh-identity-local
124 (mapcar 'list (mapcar 'car mh-identity-list)))
125 (mapcar 'list (mapcar 'car mh-identity-list)))
128 ;;First remove old settings, if any.
129 (when mh-identity-local
130 (let ((pers-list (cadr (assoc mh-identity-local mh-identity-list))))
132 (let ((field (concat (caar pers-list) ":")))
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)
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))))
146 (let ((field (concat (caar pers-list) ":"))
147 (value (cdar pers-list)))
149 ;; No value, remove field
152 (mh-header-field-delete field nil))
153 ;; Existing field, replace
154 ((mh-header-field-delete field t)
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))
161 (goto-char (point-max))
162 (if (not (looking-at "^$"))
166 (narrow-to-region (point) (point))
167 (set (make-local-variable 'mh-identity-signature-start)
169 (set-marker mh-identity-signature-start (point))
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")))
180 (insert-file-contents value)
182 (goto-char (point-min))
183 (when (not (re-search-forward "^--" nil t))
184 (cond ((mh-mhn-directive-present-p)
186 ((mh-mml-directive-present-p)
189 (set (make-local-variable 'mh-identity-signature-end)
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)))
209 (provide 'mh-identity)
212 ;;; indent-tabs-mode: nil
213 ;;; sentence-end-double-space: nil
216 ;;; mh-identity.el ends here