1 ;;; mh-identity.el --- multiple identify support for MH-E
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Peter S. Galbraith <psg@debian.org>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Multiple identity support for MH-E.
30 ;; Used to easily set different fields such as From and Organization,
31 ;; as well as different signature files.
33 ;; Customize the variable `mh-identity-list' and see the Identity menu
34 ;; in MH-Letter mode. The command `mh-insert-identity' can be used
35 ;; to manually insert an identity.
43 (autoload 'mml-insert-tag
"mml")
45 (defvar mh-identity-pgg-default-user-id nil
46 "Holds the GPG key ID to be used by pgg.el.
47 This is normally set as part of an Identity in
49 (make-variable-buffer-local 'mh-identity-pgg-default-user-id
)
51 (defvar mh-identity-menu nil
54 (defalias 'mh-identity-make-menu-no-autoload
'mh-identity-make-menu
)
57 (defun mh-identity-make-menu ()
58 "Build the Identity menu.
59 This should be called any time `mh-identity-list' or
60 `mh-auto-fields-list' change.
61 See `mh-identity-add-menu'."
62 (easy-menu-define mh-identity-menu mh-letter-mode-map
66 ;; Dynamically render :type corresponding to `mh-identity-list'
68 ;; ["Home" (mh-insert-identity "Home")
69 ;; :style radio :active (not (equal mh-identity-local "Home"))
70 ;; :selected (equal mh-identity-local "Home")]
71 '(["Insert Auto Fields"
72 (mh-insert-auto-fields) mh-auto-fields-list
]
77 `[,arg
(mh-insert-identity ,arg
) :style radio
78 :selected
(equal mh-identity-local
,arg
)]))
79 (mapcar 'car mh-identity-list
))
81 (mh-insert-identity "None") :style radio
82 :selected
(not mh-identity-local
)]
84 ["Set Default for Session"
85 (setq mh-identity-default mh-identity-local
) t
]
87 (customize-save-variable 'mh-identity-default mh-identity-local
) t
]
88 ["Customize Identities" (customize-variable 'mh-identity-list
) t
]
92 (defun mh-identity-add-menu ()
93 "Add the current Identity menu.
94 See `mh-identity-make-menu'."
96 (easy-menu-add mh-identity-menu
)))
98 (defvar mh-identity-local nil
99 "Buffer-local variable that holds the identity currently in use.")
100 (make-variable-buffer-local 'mh-identity-local
)
102 (defun mh-header-field-delete (field value-only
)
103 "Delete header FIELD, or only its value if VALUE-ONLY is t.
104 Return t if anything is deleted."
105 (let ((field-colon (if (string-match "^.*:$" field
)
107 (concat field
":"))))
108 (when (mh-goto-header-field field-colon
)
112 (delete-region (point)
113 (progn (mh-header-field-end)
114 (if (not value-only
) (forward-char 1))
118 (defvar mh-identity-signature-start nil
119 "Marker for the beginning of a signature inserted by `mh-insert-identity'.")
120 (defvar mh-identity-signature-end nil
121 "Marker for the end of a signature inserted by `mh-insert-identity'.")
123 (defun mh-identity-field-handler (field)
124 "Return the handler for header FIELD or nil if none set.
125 The field name is downcased. If the FIELD begins with the
126 character \":\", then it must have a special handler defined in
127 `mh-identity-handlers', else return an error since it is not a
129 (or (cdr (mh-assoc-string field mh-identity-handlers t
))
130 (and (eq (aref field
0) ?
:)
131 (error "Field %s not found in `mh-identity-handlers'" field
))
132 (cdr (assoc ":default" mh-identity-handlers
))
133 'mh-identity-handler-default
))
136 (defun mh-insert-identity (identity &optional maybe-insert
)
137 "Insert fields specified by given IDENTITY.
139 In a program, do not insert fields if MAYBE-INSERT is non-nil,
140 `mh-identity-default' is non-nil, and fields have already been
143 See `mh-identity-list'."
145 (list (completing-read
147 (if mh-identity-local
149 (mapcar 'list
(mapcar 'car mh-identity-list
)))
150 (mapcar 'list
(mapcar 'car mh-identity-list
)))
154 (when (or (not maybe-insert
)
155 (and (boundp 'mh-identity-default
)
157 (not mh-identity-local
)))
159 ;;First remove old settings, if any.
160 (when mh-identity-local
161 (let ((pers-list (cadr (assoc mh-identity-local mh-identity-list
))))
163 (let* ((field (caar pers-list
))
164 (handler (mh-identity-field-handler field
)))
165 (funcall handler field
'remove
))
166 (setq pers-list
(cdr pers-list
)))))
167 ;; Then insert the replacement
168 (when (not (equal "None" identity
))
169 (let ((pers-list (cadr (assoc identity mh-identity-list
))))
171 (let* ((field (caar pers-list
))
172 (value (cdar pers-list
))
173 (handler (mh-identity-field-handler field
)))
174 (funcall handler field
'add value
))
175 (setq pers-list
(cdr pers-list
))))))
176 ;; Remember what is in use in this buffer
177 (if (equal "None" identity
)
178 (setq mh-identity-local nil
)
179 (setq mh-identity-local identity
))))
182 (defun mh-identity-handler-gpg-identity (field action
&optional value
)
183 "Process header FIELD \":pgg-default-user-id\".
184 The ACTION is one of 'remove or 'add. If 'add, the VALUE is added.
185 The buffer-local variable `mh-identity-pgg-default-user-id' is set to
186 VALUE when action 'add is selected."
188 ((or (equal action
'remove
)
191 (setq mh-identity-pgg-default-user-id nil
))
193 (setq mh-identity-pgg-default-user-id value
))))
196 (defun mh-identity-handler-signature (field action
&optional value
)
197 "Process header FIELD \":signature\".
198 The ACTION is one of 'remove or 'add. If 'add, the VALUE is
201 ((equal action
'remove
)
202 (when (and (markerp mh-identity-signature-start
)
203 (markerp mh-identity-signature-end
))
204 (delete-region mh-identity-signature-start
205 mh-identity-signature-end
)))
207 ;; Insert "signature". Nil value means to use `mh-signature-file-name'.
208 (when (not (mh-signature-separator-p)) ;...unless already present
209 (goto-char (point-max))
211 (narrow-to-region (point) (point))
213 (mh-insert-signature)
214 (mh-insert-signature value
))
215 (set (make-local-variable 'mh-identity-signature-start
)
217 (set-marker-insertion-type mh-identity-signature-start t
)
218 (set (make-local-variable 'mh-identity-signature-end
)
219 (point-max-marker)))))))
221 (defvar mh-identity-attribution-verb-start nil
222 "Marker for the beginning of the attribution verb.")
223 (defvar mh-identity-attribution-verb-end nil
224 "Marker for the end of the attribution verb.")
227 (defun mh-identity-handler-attribution-verb (field action
&optional value
)
228 "Process header FIELD \":attribution-verb\".
229 The ACTION is one of 'remove or 'add. If 'add, the VALUE is
231 (when (and (markerp mh-identity-attribution-verb-start
)
232 (markerp mh-identity-attribution-verb-end
))
233 (delete-region mh-identity-attribution-verb-start
234 mh-identity-attribution-verb-end
)
235 (goto-char mh-identity-attribution-verb-start
)
237 ((equal action
'remove
) ; Replace with default
238 (mh-identity-insert-attribution-verb nil
))
239 (t ; Insert attribution verb.
240 (mh-identity-insert-attribution-verb value
)))))
243 (defun mh-identity-insert-attribution-verb (value)
244 "Insert VALUE as attribution verb, setting up delimiting markers.
245 If VALUE is nil, use `mh-extract-from-attribution-verb'."
247 (narrow-to-region (point) (point))
249 (insert mh-extract-from-attribution-verb
)
251 (set (make-local-variable 'mh-identity-attribution-verb-start
)
253 (set-marker-insertion-type mh-identity-attribution-verb-start t
)
254 (set (make-local-variable 'mh-identity-attribution-verb-end
)
255 (point-max-marker))))
257 (defun mh-identity-handler-default (field action top
&optional value
)
258 "Process header FIELD.
259 The ACTION is one of 'remove or 'add. If TOP is non-nil, add the
260 field and its VALUE at the top of the header, else add it at the
261 bottom of the header. If action is 'add, the VALUE is added."
262 (let ((field-colon (if (string-match "^.*:$" field
)
264 (concat field
":"))))
266 ((equal action
'remove
)
267 (mh-header-field-delete field-colon nil
))
270 ;; No value, remove field
273 (mh-header-field-delete field-colon nil
))
274 ;; Existing field, replace
275 ((mh-header-field-delete field-colon t
)
277 ;; Other field, add at end or top
279 (goto-char (point-min))
281 (mh-goto-header-end 0))
282 (insert field-colon
" " value
"\n")))))))
285 (defun mh-identity-handler-top (field action
&optional value
)
286 "Process header FIELD.
287 The ACTION is one of 'remove or 'add. If 'add, the VALUE is
288 added. If the field wasn't present, it is added to the top of the
290 (mh-identity-handler-default field action t value
))
293 (defun mh-identity-handler-bottom (field action
&optional value
)
294 "Process header FIELD.
295 The ACTION is one of 'remove or 'add. If 'add, the VALUE is
296 added. If the field wasn't present, it is added to the bottom of
298 (mh-identity-handler-default field action nil value
))
300 (provide 'mh-identity
)
303 ;; indent-tabs-mode: nil
304 ;; sentence-end-double-space: nil
307 ;; arch-tag: 07d66ef6-8726-4ac6-9ecf-e566cd5bfb45
308 ;;; mh-identity.el ends here