~reddyuday/vm/virtual

« back to all changes in this revision

Viewing changes to vm-pine.el

  • Committer: Robert Widhopf
  • Date: 2004-05-02 21:49:29 UTC
  • Revision ID: Arch-1:hack@robf.de--testing%vm--robf--7--patch-2
My additional files that should go with VM

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;;; vm-pine.el --- draft handling and other neat functions for VM
 
2
;; 
 
3
;; Copyright (C) 1998-2002 Robert Fenk
 
4
;;
 
5
;; Author:      Robert Fenk
 
6
;; Status:      Tested with XEmacs 21.1.10 & GNU Emacs 20.7 & VM 6.95
 
7
;; Keywords:    vm draft handling
 
8
;; X-URL:       http://www.robf.de/Hacking/elisp
 
9
;; X-RCS:       $Id: vm-pine.el,v 1.50 2004/04/29 21:12:38 fenk Exp $
 
10
 
 
11
;;
 
12
;; This code 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 1, or (at your option)
 
15
;; any later version.
 
16
;;
 
17
;; This program 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 this program; if not, write to the Free Software
 
24
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
25
;;
 
26
;;; Commentary:
 
27
;; 
 
28
;; This package provides the following new features for VM:
 
29
;;
 
30
;;  A Pine-like postpone message function and folder.  There are two new
 
31
;;  functions. `vm-postpone-message' bound to [C-c C-p] in
 
32
;;  the `vm-mail-mode' and the function `vm-continue-postponed-message'
 
33
;;  is bound to [C] in a folder buffer.
 
34
;;  The `vm-continue-what-message' imitates Pines message composing.
 
35
;;  You can set `vm-mode-map' in the following way to get Pine-like.
 
36
;;
 
37
;;  (define-key vm-mode-map "m" 'vm-continue-what-message)
 
38
;;
 
39
;;  If you have postponed messages you will be asked if you want to continue
 
40
;;  their composing, if you say "yes" you will visit the `vm-postponed-folder'
 
41
;;  and you can select the message you would like to continue and press "m"
 
42
;;  again!  However be aware this works currently only if you expunge all
 
43
;;  messages marked for deletion and save the postponed folder.
 
44
;;
 
45
;;  You can also bind it to "C-x m" in order to check for postponed messages
 
46
;;  when composing a message without starting VM.
 
47
;;
 
48
;;  (autoload 'vm-continue-what-message-other-window "vm-pine" "" t)
 
49
;;  (global-set-key "\C-xm" 'vm-continue-what-message-other-window)
 
50
;;
 
51
;;
 
52
;;  Three new mail header insertion functions make life easier. The
 
53
;;  bindings and names are:
 
54
;;     "\C-c\C-f\C-a"  vm-mail-return-receipt-to
 
55
;;     "\C-c\C-f\C-p"  vm-mail-priority
 
56
;;     "\C-c\C-f\C-f"  vm-mail-fcc
 
57
;;  The variables `vm-mail-return-receipt-to' and `vm-mail-priority'
 
58
;;  can be used to configure the inserted headers.
 
59
;;  `vm-mail-fcc' can be configured by setting the variable
 
60
;;  `vm-mail-folder-alist' which has the same syntax and default
 
61
;;  value as `vm-auto-folder-alist'.
 
62
;;  You may also add `vm-mail-auto-fcc' to `vm-reply-hook' in order to
 
63
;;  automatically setup the FCC header according to the variable
 
64
;;  `vm-mail-folder-alist'.
 
65
;;  An there's another fcc-function `vm-mail-to-fcc' which set the FCC
 
66
;;  according to the recipients email-address.
 
67
;;
 
68
;;; Bug reports and feature requests:
 
69
;; Please send a backtrace and the version number of vm-pine.el!
 
70
;; Feature requests are welcome!
 
71
 
 
72
;;; Code:
 
73
 
 
74
;; Attempt to handle older/other emacs.
 
75
(eval-and-compile
 
76
  (require 'vm-version)
 
77
  (require 'vm-message)
 
78
  (require 'vm-macro)
 
79
  (require 'vm-vars))
 
80
 
 
81
(if (not (boundp 'user-mail-address))
 
82
    (if (functionp 'user-mail-address)
 
83
        (setq user-mail-address (user-mail-address))
 
84
      (setq user-mail-address "unknown")
 
85
      (message "Please set the variable `user-mail-address'!!!")
 
86
      (sit-for 2)))
 
87
      
 
88
(defgroup vm nil
 
89
  "VM"
 
90
  :group 'mail)
 
91
 
 
92
(defgroup vm-pine nil
 
93
  "Pine inspired extensions to VM."
 
94
  :group  'vm)
 
95
 
 
96
;;-----------------------------------------------------------------------------
 
97
;;;###autoload
 
98
(defun vm-summary-function-f (m)
 
99
  "Return the recipient or newsgroup for uninteresting senders.
 
100
If the \"From:\" header contains the user login or full name then
 
101
this function returns the \"To:\" or \"Newsgroups:\" header field with a
 
102
\"To:\" as prefix.
 
103
 
 
104
For example the outgoing message box will now list to whom you sent the
 
105
messages.  Use `vm-fix-summary!!!' to update the summary of a folder! With
 
106
loaded BBDB it uses `vm-summary-function-B' to obtain the full name of the
 
107
sender.  The only difference to VMs default behavior is the honoring of
 
108
messages sent to news groups. ;c)
 
109
 
 
110
See also:    `vm-summary-uninteresting-senders'"
 
111
  (interactive)
 
112
  (let ((case-fold-search t)
 
113
        (headers '(("From:" "") 
 
114
                   ("Newsgroups:" "News:")
 
115
                   "To:" "CC:" "BCC:"
 
116
                   "Resent-To:" "Resent-CC:" "Resent-BCC:"
 
117
                   ("Sender:" "")  ("Resent-From:" "Resent:")))
 
118
        header-name arrow
 
119
        addresses
 
120
        address
 
121
        first)
 
122
 
 
123
    (while (and (not address) headers)
 
124
      (if (listp (car headers))
 
125
          (setq header-name (caar headers) arrow (cadar headers))
 
126
        (setq header-name (car headers) arrow (concat header-name " ")))
 
127
      (setq addresses (vm-get-header-contents m header-name))
 
128
      (if addresses
 
129
          (setq addresses (vm-decode-mime-encoded-words-in-string addresses)
 
130
                addresses
 
131
                (or (if (functionp 'bbdb-extract-address-components)
 
132
                        (bbdb-extract-address-components addresses t))
 
133
                    (list (mail-extract-address-components addresses))
 
134
                    addresses)))
 
135
      (if (not first) (setq first (car addresses)))
 
136
      (while addresses
 
137
        (if (or (not vm-summary-uninteresting-senders)
 
138
                (and vm-summary-uninteresting-senders
 
139
                     (not (string-match vm-summary-uninteresting-senders
 
140
                                        (format "%s" (car addresses))))))
 
141
            (setq address (car addresses) addresses nil))
 
142
        (setq addresses (cdr addresses)))
 
143
      (setq headers (cdr headers)))
 
144
 
 
145
    (if (and (null address) (null first))
 
146
        ""
 
147
      (if (and (null address) first)
 
148
          (setq address first))
 
149
      (concat arrow
 
150
              (cond ((functionp 'bbdb/vm-alternate-full-name)
 
151
                     (or (bbdb/vm-alternate-full-name (cadr address))
 
152
                         (car address)
 
153
                         (cadr address)))
 
154
                    (t (or (car address) (cadr address))))))))
 
155
 
 
156
;;-----------------------------------------------------------------------------
 
157
;;;###autoload
 
158
(defcustom vm-postponed-header "X-VM-postponed-data: "
 
159
  "Additional header which is inserted to postponed messages.
 
160
It is used for internal things and should not be modified. 
 
161
It is a lisp list which currently contains the following items:
 
162
 <date of the postponing>
 
163
 <reply references list>
 
164
 <forward references list>
 
165
 <redistribute references list>
 
166
while the last three are set by `vm-get-persistent-message-ids-for'."
 
167
  :type 'string
 
168
  :group 'vm-pine)
 
169
 
 
170
;;;###autoload
 
171
(defcustom vm-postponed-attribute "P"
 
172
  "Summary string that will be inserted by `vm-summary-function-p' for
 
173
postponed messages."
 
174
  :type 'string
 
175
  :group 'vm-pine)
 
176
  
 
177
;;;###autoload
 
178
(defun vm-summary-function-p (m)
 
179
  "Return 'P' for postponed messages."
 
180
  (interactive)
 
181
  (if (vm-get-header-contents m vm-postponed-header)
 
182
      "P"
 
183
    ""))
 
184
 
 
185
;;-----------------------------------------------------------------------------
 
186
;; A Pine-like postponed folder handling
 
187
;;;###autoload
 
188
(defcustom vm-postponed-folder "postponed"
 
189
  "The name of the folder where postponed messages are saved."
 
190
  :type 'string
 
191
  :group 'vm-pine)
 
192
 
 
193
;;;###autoload
 
194
(defcustom vm-postponed-message-headers '("From:" "Organization:"
 
195
                                          "Reply-To:"
 
196
                                          "To:" "Newsgroups:"
 
197
                                          "CC:" "BCC:" "FCC:"
 
198
                                          "In-Reply-To:"
 
199
                                          "References:"
 
200
                                          "Subject:"
 
201
                                          "X-Priority:" "Priority:")
 
202
  "Similar to `vm-forwarded-headers'.
 
203
A list of headers that should be kept, when continuing a postponed message.
 
204
 
 
205
The following mime headers should not be kept, since this breaks things:
 
206
Mime-Version, Content-Type, Content-Transfer-Encoding."
 
207
  :type '(repeat (string))
 
208
  :group 'vm-pine)
 
209
 
 
210
;;;###autoload
 
211
(defcustom vm-postponed-message-discard-header-regexp nil
 
212
  "Similar to `vm-unforwarded-header-regexp'.
 
213
A regular expression matching all headers that should be discard when
 
214
when continuing a postponed message."
 
215
  :type 'regexp
 
216
  :group 'vm-pine)
 
217
 
 
218
;;;###autoload
 
219
(defcustom vm-continue-postponed-message-hook nil
 
220
  "List of hook functions to be run after continuing a postponed message."
 
221
  :type 'hook
 
222
  :group 'vm-pine)
 
223
 
 
224
;;;###autoload
 
225
(defcustom vm-postpone-message-hook nil
 
226
  "List of hook functions to be run before postponing a message."
 
227
  :type 'hook
 
228
  :group 'vm-pine)
 
229
 
 
230
(defvar vm-postponed-message-folder-buffer nil
 
231
  "Buffer of source folder.
 
232
This is only for internal use of vm-pine.el!!!")
 
233
 
 
234
;;-----------------------------------------------------------------------------
 
235
(define-key vm-mode-map [C]      'vm-continue-postponed-message)
 
236
 
 
237
;;-----------------------------------------------------------------------------
 
238
(defun vm-get-persistent-message-ids-for (mlist)
 
239
  "Return a list of message id and folder name of all messages in MLIST."
 
240
  (let (mp midlist folder mid f)
 
241
    (while mlist
 
242
      (setq mp (car mlist)
 
243
            folder (buffer-name (vm-buffer-of mp))
 
244
            mid (vm-message-id-of mp)
 
245
            f (assoc folder midlist))
 
246
      (if mid
 
247
          (if f
 
248
              (setcdr f (cons mid (cdr f)))
 
249
            (add-to-list 'midlist (list folder mid))))
 
250
      (setq mlist (cdr mlist)))
 
251
    midlist))
 
252
  
 
253
(defun vm-get-message-pointers-for (msgidlist)
 
254
  "Return the message pointers belonging to the messages listed in MSGIDLIST.
 
255
MSGIDLIST is a list as returned by `vm-get-persistent-message-ids-for'."
 
256
  (let (folder vm-message-pointers)
 
257
    
 
258
    (while msgidlist
 
259
      (setq folder (caar msgidlist))
 
260
      (if (file-exists-p (expand-file-name folder
 
261
                                           (or vm-folder-directory "~/Mail")))
 
262
          (save-excursion
 
263
            (if folder
 
264
                (cond ((bufferp folder)
 
265
                       (set-buffer folder))
 
266
                      ((stringp folder)
 
267
                       (if (get-buffer folder)
 
268
                           (set-buffer (get-buffer folder))
 
269
                         (vm-visit-folder folder)))))
 
270
            (vm-select-folder-buffer)
 
271
            (save-restriction
 
272
              (widen)
 
273
              (goto-char (point-min))
 
274
              (let ((msgid-regexp (concat "^Message-Id:\\s-*"
 
275
                                          (regexp-opt (cdar msgidlist))))
 
276
                    (point-max (point-max))
 
277
                    (case-fold-search t))
 
278
 
 
279
                (while (re-search-forward msgid-regexp point-max t)
 
280
                  (let ((point (point))
 
281
                        (mp vm-message-list))
 
282
                    (while mp
 
283
                      (if (and (>= point (vm-start-of (car mp)))
 
284
                               (<= point (vm-end-of (car mp))))
 
285
                          (setq vm-message-pointers (cons (car mp)
 
286
                                                          vm-message-pointers)
 
287
                                mp nil)
 
288
                        (setq mp (cdr mp)))))))))
 
289
        (message "The folder %s does not exist.  Maybe it was virtual." folder))
 
290
      (setq msgidlist (cdr msgidlist)))
 
291
    vm-message-pointers))
 
292
 
 
293
;;-----------------------------------------------------------------------------
 
294
;;;###autoload
 
295
(defun vm-continue-postponed-message (&optional silent)
 
296
  "Continue composing of the currently selected message.
 
297
Before continuing the composition you may decode the presentation as
 
298
you like, by pressing [D] and viewing part of the message!
 
299
Then current message is copied to a new buffer and the vm-mail-mode is
 
300
entered.  When every thing is finished the hook functions in
 
301
`vm-mail-mode-hook' and `vm-continue-postponed-message-hook' are
 
302
executed.  When called with a prefix argument it will not switch to
 
303
the composition buffer, this may be used for automatic editing of
 
304
messages.
 
305
 
 
306
The variables `vm-postponed-message-headers' and
 
307
`vm-postponed-message-discard-header-regexp' control which
 
308
headers are copied to the composition buffer.
 
309
 
 
310
In `vm-mail-mode' this is bound to [C].
 
311
Optional argument SILENT is positiv then act in background (no frame
 
312
creation)."
 
313
  (interactive "P")
 
314
 
 
315
  (vm-session-initialization)
 
316
  (vm-follow-summary-cursor)
 
317
  (vm-select-folder-buffer)
 
318
  (vm-check-for-killed-summary)
 
319
  (vm-check-for-killed-presentation)
 
320
  (vm-error-if-folder-empty)
 
321
 
 
322
  (if (eq vm-system-state 'previewing)
 
323
      (vm-show-current-message))
 
324
  
 
325
  (save-restriction
 
326
    (widen)
 
327
    (let* ((folder-buffer (current-buffer))
 
328
           (presentation-buffer vm-presentation-buffer)
 
329
           (vmp vm-message-pointer)
 
330
           (is-decoded vm-mime-decoded)
 
331
           (hstart (vm-headers-of (car vmp)))
 
332
           (tstart (vm-text-of    (car vmp)))
 
333
           (tend   (- (vm-end-of     (car vmp)) 1))
 
334
           (to (format "mail to %s" (vm-get-header-contents (car vmp)
 
335
                                                            "To:" ",")))
 
336
           (vm-pp-data (vm-get-header-contents (car vmp)
 
337
                                               vm-postponed-header)))
 
338
      
 
339
      ;; Prepare the composition buffer
 
340
      (if (and to (string-match "[^,\n<(]+" to))
 
341
          (setq to (match-string 0 to)))
 
342
      
 
343
      (if (not silent)
 
344
          (let ((vm-mail-hook nil)
 
345
                (vm-mail-mode-hook nil)
 
346
                (this-command 'vm-mail))
 
347
            (vm-mail-internal to))
 
348
        (set-buffer (generate-new-buffer to))
 
349
        (setq default-directory (expand-file-name
 
350
                                 (or vm-folder-directory "~/")))
 
351
        (auto-save-mode (if auto-save-default 1 -1))
 
352
        (let ((mail-mode-hook nil)
 
353
              (mail-setup-hook nil))
 
354
          (mail-mode))
 
355
        (setq vm-mail-buffer folder-buffer))
 
356
      
 
357
      (make-local-variable 'vm-postponed-message-folder-buffer)
 
358
      (setq vm-postponed-message-folder-buffer
 
359
            (vm-buffer-of (vm-real-message-of (car vmp))))
 
360
      (make-local-variable 'vm-message-pointer)
 
361
      (setq vm-message-pointer vmp)
 
362
      (make-local-hook 'mail-send-hook)
 
363
      (add-hook 'mail-send-hook 'vm-delete-postponed-message t t)
 
364
      (erase-buffer)
 
365
 
 
366
      ;; set the VM variables for setting source message attributes
 
367
      (when vm-pp-data
 
368
        (make-local-variable 'vm-reply-list)
 
369
        (make-local-variable 'vm-forward-list)
 
370
        (make-local-variable 'vm-redistribute-list)
 
371
        (setq vm-pp-data (read vm-pp-data)
 
372
              vm-reply-list 
 
373
              (vm-get-message-pointers-for (nth 1 vm-pp-data))
 
374
              vm-forward-list
 
375
              (vm-get-message-pointers-for (nth 2 vm-pp-data))
 
376
              vm-redistribute-list
 
377
              (vm-get-message-pointers-for (nth 3 vm-pp-data)))
 
378
        (if vm-reply-list (setq vm-system-state 'replying))
 
379
        (if vm-forward-list (setq vm-system-state 'forwarding))
 
380
        (if vm-redistribute-list (setq vm-system-state 'redistributing)))
 
381
      
 
382
      ;; Prepare headers
 
383
      (insert-buffer-substring folder-buffer hstart tstart)
 
384
      (beginning-of-buffer)
 
385
      (cond ((or (vm-mime-plain-message-p (car vmp)) is-decoded)
 
386
             (vm-reorder-message-headers nil
 
387
                 vm-postponed-message-headers
 
388
                 vm-postponed-message-discard-header-regexp))
 
389
            (t ; copy undecoded messages with mime headers
 
390
             (vm-reorder-message-headers nil
 
391
                  (append '("MIME-Version:" "Content-type:")
 
392
                      vm-postponed-message-headers)
 
393
                  vm-postponed-message-discard-header-regexp)))
 
394
      (vm-decode-mime-encoded-words)
 
395
      (search-forward-regexp "\n\n")
 
396
      (replace-match (concat "\n" mail-header-separator "\n") t t)
 
397
 
 
398
      ;; Add message body as previewed
 
399
      (goto-char (point-max))
 
400
      (if presentation-buffer
 
401
          ;; when using presentation buffer we have to
 
402
          (save-excursion
 
403
            (set-buffer presentation-buffer)
 
404
            (goto-char (point-min))
 
405
            (search-forward-regexp "\n\n")
 
406
            (setq tstart (match-end 0)
 
407
                  tend (point-max)))
 
408
        (setq presentation-buffer folder-buffer))
 
409
            
 
410
      (insert-buffer-substring presentation-buffer tstart tend)
 
411
      ;; in order to show headers hidden by vm-shrunken-headers 
 
412
      (put-text-property (point-min) (point-max) 'invisible nil)
 
413
      
 
414
      ;; and add the buttons for attachments
 
415
      (vm-decode-postponed-mime-message)))
 
416
  (when (not silent)
 
417
    (run-hooks 'vm-mail-hook)
 
418
    (run-hooks 'vm-mail-mode-hook))
 
419
  (run-hooks 'vm-continue-postponed-message-hook))
 
420
 
 
421
;;-----------------------------------------------------------------------------
 
422
(defun vm-delete-postponed-message ()
 
423
  "Delete the source message belonging to the continued composition."
 
424
  (interactive)
 
425
  (if vm-message-pointer
 
426
      (condition-case nil
 
427
          (let* ((msg (car vm-message-pointer))
 
428
                (buffer (vm-buffer-of msg)))
 
429
            ;; only delete messages which have been postponed by us before
 
430
            (when (vm-get-header-contents msg vm-postponed-header)
 
431
              (vm-set-deleted-flag msg t)
 
432
              (vm-update-summary-and-mode-line))
 
433
            ;; in the postponded folder expunge them right now 
 
434
            (when (string= (buffer-name buffer)
 
435
                           (file-name-nondirectory vm-postponed-folder))
 
436
              (if (frames-of-buffer buffer t)
 
437
                  (iconify-frame (car (frames-of-buffer buffer))))
 
438
              (save-excursion
 
439
                (switch-to-buffer buffer)
 
440
                (vm-expunge-folder)
 
441
                (vm-save-folder)
 
442
                (when (not vm-message-list)
 
443
                  (let ((this-command 'vm-quit))
 
444
                    (vm-quit))))))
 
445
        (error "Folder buffer closed before deletion of source message."))))
 
446
 
 
447
;;-----------------------------------------------------------------------------
 
448
(defun vm-decode-postponed-mime-message ()
 
449
  "Replace the mime buttons by attachment buttons."
 
450
  (interactive)
 
451
  (cond (vm-xemacs-p
 
452
         (let ((e-list (extent-list nil (point-min) (point-max))))
 
453
           ;; First collect the extents
 
454
           (setq e-list
 
455
                 (sort (vm-delete
 
456
                        (function (lambda (e)
 
457
                                    (extent-property e 'vm-mime-layout)))
 
458
                        e-list t)
 
459
                       (function (lambda (e1 e2)
 
460
                                   (< (extent-end-position e1)
 
461
                                      (extent-end-position e2))))))
 
462
           ;; Then replace the buttons, because doing it at once will result in
 
463
           ;; problems since the new buttons are from the same extent.
 
464
           (while e-list
 
465
             (vm-decode-postponed-mime-button (car e-list))
 
466
             (setq e-list (cdr e-list)))))
 
467
        (vm-fsfemacs-p
 
468
         (let ((o-list (vm-mime-fake-attachment-overlays (point-min)
 
469
                                                         (point-max))))
 
470
           (setq o-list (sort (vm-delete
 
471
                               (function (lambda (o)
 
472
                                           (overlay-get o 'vm-mime-layout)))
 
473
                               o-list t)
 
474
                              (function
 
475
                               (lambda (e1 e2)
 
476
                                 (< (overlay-end e1)
 
477
                                    (overlay-end e2))))))
 
478
           (while o-list
 
479
             (vm-decode-postponed-mime-button (car o-list))
 
480
             (setq o-list (cdr o-list)))))
 
481
        (t
 
482
         (error "don't know how to MIME dencode composition for %s"
 
483
                (emacs-version)))))
 
484
 
 
485
;;-----------------------------------------------------------------------------
 
486
(defun vm-decode-postponed-mime-button (x)
 
487
  "Replace the mime button specified by X."
 
488
 
 
489
  (save-excursion
 
490
    (let (layout
 
491
          xstart
 
492
          xend)
 
493
 
 
494
      (if vm-fsfemacs-p
 
495
          (setq layout (overlay-get x 'vm-mime-layout)
 
496
                xstart (overlay-start x)
 
497
                xend   (overlay-end x))
 
498
        (setq layout (extent-property x 'vm-mime-layout)
 
499
              xstart (extent-start-position x)
 
500
              xend   (extent-end-position x)))
 
501
               
 
502
      (let* ((start  (vm-mm-layout-header-start layout))
 
503
             (end    (vm-mm-layout-body-end   layout))
 
504
             (b      (marker-buffer start))
 
505
             (desc   (or (vm-mm-layout-description layout)
 
506
                         "message body text"))
 
507
             (disp   (or (vm-mm-layout-disposition layout)
 
508
                         '("inline")))
 
509
             (file   (vm-mime-get-disposition-parameter layout "filename"))
 
510
             filename
 
511
             (type   (vm-mm-layout-type layout)))
 
512
 
 
513
        (if (and type
 
514
                 (string= (car type) "message/external-body")
 
515
                 (string= (cadr type) "access-type=local-file"))
 
516
          (save-excursion
 
517
            (setq filename (substring (caddr type) 5))
 
518
            (vm-select-folder-buffer)
 
519
            (save-restriction
 
520
              (let ((start (vm-mm-layout-body-start layout))
 
521
                    (end   (vm-mm-layout-body-end layout)))
 
522
                (set-buffer (marker-buffer (vm-mm-layout-body-start layout)))
 
523
                (widen)
 
524
                (goto-char start)
 
525
                (if (not (re-search-forward
 
526
                          "Content-Type: \"?\\([^ ;\" \n\t]+\\)\"?;?"
 
527
                          end t))
 
528
                    (error "No `Content-Type' header found in: %s"
 
529
                           (buffer-substring start end))
 
530
                  (setq type (list (match-string 1))))))))
 
531
        
 
532
        ;; delete the mime-button
 
533
        (goto-char xstart)
 
534
        (delete-region xstart xend)
 
535
        ;; and insert an attached-object-button
 
536
        (if filename
 
537
            (vm-mime-attach-file filename (car type))
 
538
          (if file
 
539
              (vm-mime-attach-object (list b start end disp file)
 
540
                                     (car type) nil desc t)
 
541
            (vm-mime-attach-object (list b start end disp)
 
542
                                   (car type) nil desc t)))))))
 
543
 
 
544
;;-----------------------------------------------------------------------------
 
545
(define-key vm-mail-mode-map [(control c) (control p)] 'vm-postpone-message)
 
546
 
 
547
;;-----------------------------------------------------------------------------
 
548
;;;###autoload
 
549
(defun vm-postpone-message (&optional folder dont-kill no-postpone-header)
 
550
  "Save the current composition as a draft.
 
551
Before saving the composition the `vm-postpone-message-hook' functions
 
552
are executed and it is written into the FOLDER `vm-postponed-folder'.
 
553
When called with a prefix argument you will be asked for
 
554
the folder.
 
555
Optional argument DONT-KILL is positive, then do not kill source message."
 
556
  (interactive "P")
 
557
  
 
558
  (let ((message-buffer (current-buffer))
 
559
        folder-buffer
 
560
        target-type)
 
561
 
 
562
    (if (and (boundp 'font-lock-mode) font-lock-mode)
 
563
        (font-lock-mode 0))
 
564
    (if (and vm-fsfemacs-p (boundp 'ispell-minor-mode) ispell-minor-mode)
 
565
        (ispell-minor-mode 0))
 
566
    (if (and vm-fsfemacs-p (boundp  'filladapt-mode) filladapt-mode)
 
567
        (filladapt-mode 0))
 
568
    (if (boundp 'auto-fill-mode)
 
569
        (auto-fill-mode 0))
 
570
 
 
571
    (if (and folder (not (stringp folder)))
 
572
        (setq folder (vm-read-file-name
 
573
                      (format "Postpone to folder (%s): " vm-postponed-folder)
 
574
                      (or vm-folder-directory default-directory)
 
575
                      vm-postponed-folder nil nil
 
576
                      'vm-folder-history)))
 
577
    
 
578
    ;; there is no explicit folder given ...
 
579
    (if (not folder)
 
580
        (if vm-postponed-message-folder-buffer
 
581
            (setq folder (buffer-file-name vm-postponed-message-folder-buffer))
 
582
          (setq folder (expand-file-name vm-postponed-folder
 
583
                                         (or vm-folder-directory
 
584
                                             default-directory)))))
 
585
 
 
586
    (if (not folder)
 
587
        (error "I could not find a folder for postponing messages!"))
 
588
 
 
589
    ;; if it is no absolute folder path then prepend the folder directory
 
590
    (if (not (file-name-absolute-p folder))
 
591
        (setq folder (expand-file-name folder
 
592
                                       (or vm-folder-directory
 
593
                                           default-directory))))
 
594
 
 
595
    ;; Now add possibly missing headers
 
596
    (goto-char (point-min))
 
597
    (if (not (vm-mail-mode-get-header-contents "From:"))
 
598
        (let* ((login user-mail-address)
 
599
               (fullname (user-full-name)))
 
600
          (cond ((and (eq mail-from-style 'angles) login fullname)
 
601
                 (insert (format "From: %s <%s>\n" fullname login)))
 
602
                ((and (eq mail-from-style 'parens) login fullname)
 
603
                 (insert (format "From: %s (%s)\n" login fullname)))
 
604
                (t
 
605
                 (insert (format "From: %s\n" login))))))
 
606
    (if (not (vm-mail-mode-get-header-contents "Date:"))
 
607
        (insert "Date: "
 
608
                (format-time-string "%a, %d %b %Y %H%M%S %Z"
 
609
                                    (current-time))
 
610
                "\n"))
 
611
    
 
612
        ;; mime-encode the message if necessary 
 
613
    (let ((vm-do-fcc-before-mime-encode nil))
 
614
      (condition-case nil (vm-mime-encode-composition) (error t)))
 
615
    
 
616
    ;; add the postponed header
 
617
    (vm-mail-mode-remove-header vm-postponed-header)
 
618
 
 
619
    (if no-postpone-header nil
 
620
      (insert vm-postponed-header " "
 
621
              (format
 
622
               "(\"%s\" %S %S %S)\n"
 
623
               (format-time-string "%a, %d %b %Y %T %Z" (current-time))
 
624
               (vm-get-persistent-message-ids-for vm-reply-list)
 
625
               (vm-get-persistent-message-ids-for vm-forward-list)
 
626
               (vm-get-persistent-message-ids-for vm-redistribute-list))))
 
627
 
 
628
    ;; ensure that the message ends with an empty line!
 
629
    (goto-char (point-max))
 
630
    (skip-chars-backward " \t\n")
 
631
    (delete-region (point) (point-max))
 
632
    (insert "\n\n\n")
 
633
    
 
634
    ;; run the hooks 
 
635
    (run-hooks 'vm-postpone-message-hook)
 
636
 
 
637
    ;; delete mail header separator
 
638
    (beginning-of-buffer)
 
639
    (if (re-search-forward (regexp-quote mail-header-separator) nil t)
 
640
        (delete-region (match-beginning 0) (match-end 0)))
 
641
 
 
642
 
 
643
    (setq folder-buffer (vm-get-file-buffer folder))
 
644
    (if folder-buffer
 
645
        ;; o.k. the folder is already opened
 
646
        (save-excursion
 
647
          (set-buffer folder-buffer)
 
648
          (vm-error-if-folder-read-only)
 
649
          (let ((buffer-read-only nil))
 
650
            (vm-save-restriction
 
651
             (widen)
 
652
             (goto-char (point-max))
 
653
             (vm-write-string (current-buffer) (vm-leading-message-separator))
 
654
             (insert-buffer-substring message-buffer)
 
655
             (vm-write-string (current-buffer) (vm-trailing-message-separator))
 
656
 
 
657
             (cond ((eq major-mode 'vm-mode)
 
658
                    (vm-increment vm-messages-not-on-disk)
 
659
                    (vm-clear-modification-flag-undos)))
 
660
             
 
661
             (vm-check-for-killed-summary)
 
662
             (vm-assimilate-new-messages)
 
663
             (vm-update-summary-and-mode-line))))
 
664
      ;; well the folder is not visited, so we write to the file
 
665
      (setq target-type (or (vm-get-folder-type folder)
 
666
                            vm-default-folder-type))
 
667
      
 
668
      (if (eq target-type 'unknown)
 
669
          (error "Folder `%s' type is unrecognized" folder))
 
670
      
 
671
      (vm-write-string folder (vm-leading-message-separator target-type))
 
672
      (write-region (point-min) (point-max) folder t 'quiet)
 
673
      (vm-write-string folder (vm-trailing-message-separator target-type)))
 
674
    
 
675
    ;; delete source message
 
676
    (vm-delete-postponed-message)
 
677
 
 
678
    ;; mess arounf with the window configuration 
 
679
    (let ((b (current-buffer))
 
680
          (this-command 'vm-mail-send-and-exit))
 
681
      (cond ((null (buffer-name b));; dead buffer
 
682
             ;; This improves window configuration behavior in
 
683
             ;; XEmacs.  It avoids taking the folder buffer from
 
684
             ;; one frame and attaching it to the selected frame.
 
685
             (set-buffer (window-buffer (selected-window)))
 
686
             (vm-display nil nil '(vm-mail-send-and-exit)
 
687
                         '(vm-mail-send-and-exit
 
688
                           reading-message
 
689
                           startup)))
 
690
            (t
 
691
             (vm-display b nil '(vm-mail-send-and-exit)
 
692
                         '(vm-mail-send-and-exit reading-message startup)))))
 
693
    
 
694
    ;; and kill this buffer?
 
695
    (if dont-kill
 
696
        (insert (concat "FCC: " folder "\n" mail-header-separator))
 
697
      (kill-this-buffer))
 
698
 
 
699
    (if (interactive-p)
 
700
        (message "Message postponed to folder `%s'" folder))))
 
701
 
 
702
;;-----------------------------------------------------------------------------
 
703
(defun vm-buffer-in-vm-mode ()
 
704
  (member major-mode '(vm-mode vm-virtual-mode
 
705
                               vm-presentation-mode
 
706
                               vm-summary-mode
 
707
                               vm-mail-mode)))
 
708
 
 
709
(defcustom vm-continue-what-message 'ask
 
710
  "Whether to never continue, ask or always continue postponed messages."
 
711
  :type '(choice (const :tag "never" nil)
 
712
                 (const ask)
 
713
                 (const continue))
 
714
  :group 'vm-pine)
 
715
 
 
716
(defun vm-continue-what-message-composing ()
 
717
  "Decide whether to compose a new message or continue a draft.
 
718
This checks if the postponed folder contains drafts.
 
719
Drafts in other folders are not recognized!"
 
720
  (save-excursion
 
721
    (vm-session-initialization)
 
722
    
 
723
    (let* ((ppfolder (and vm-postponed-folder
 
724
                          (expand-file-name vm-postponed-folder
 
725
                                            (or vm-folder-directory
 
726
                                                default-directory))))
 
727
           action
 
728
           buffer)
 
729
 
 
730
      ;; postponed message in current folder
 
731
      (when (vm-buffer-in-vm-mode)
 
732
        (vm-check-for-killed-folder)
 
733
        (vm-select-folder-buffer)
 
734
                  
 
735
        (if (and vm-message-pointer
 
736
                 (vm-get-header-contents (vm-real-message-of
 
737
                                          (car vm-message-pointer))
 
738
                                         (regexp-quote vm-postponed-header))
 
739
                 (not (vm-deleted-flag (car vm-message-pointer))))
 
740
            (setq action 'continue)))
 
741
 
 
742
      ;; postponed message in postponed folder
 
743
      (when (and (not action) (setq buffer (vm-get-file-buffer ppfolder)))
 
744
        (if (and (get-buffer-window-list buffer nil 0))
 
745
            (when (save-excursion
 
746
                    (set-buffer buffer)
 
747
                    (not (vm-deleted-flag (car vm-message-pointer))))
 
748
              (message "Please select a draft!")
 
749
              (select-window (car (get-buffer-window-list buffer nil 0)))
 
750
              (if (frames-of-buffer buffer)
 
751
                  (deiconify-frame (car (frames-of-buffer buffer))))
 
752
              (setq action 'none))
 
753
          (setq action 'visit)))
 
754
 
 
755
      ;; visit postponed folder 
 
756
      (when (and (not action) (file-exists-p ppfolder)
 
757
                 (> (nth 7 (file-attributes ppfolder)) 0))
 
758
        (setq action 'visit))
 
759
 
 
760
      (if (not action) (setq action 'new))
 
761
 
 
762
      ;; decide what to do
 
763
      (setq action 
 
764
            (cond ((eq vm-continue-what-message nil)
 
765
                   'new)
 
766
                  ((eq vm-continue-what-message 'ask)
 
767
                   (if (equal action 'visit)
 
768
                       (if (y-or-n-p
 
769
                            "Continue composition of postponed messages? ")
 
770
                           'visit
 
771
                         'new)
 
772
                     action))
 
773
                  ((eq vm-continue-what-message 'continue)
 
774
                   action)
 
775
                  (t
 
776
                   action))))))
 
777
              
 
778
;;;###autoload
 
779
(defun vm-continue-what-message (&optional where)
 
780
  "Ask for continuing of postponed messages if there are some."
 
781
  (interactive)
 
782
  (if where (setq where (concat "-" where)))
 
783
  (let ((action (vm-continue-what-message-composing))
 
784
        (visit  (intern (concat "vm-visit-folder" (or where ""))))
 
785
        (mail   (intern (concat "vm-mail" (or where "")))))
 
786
    (cond ((equal action 'continue)
 
787
           (vm-continue-postponed-message))
 
788
          ((equal action 'visit)
 
789
           (funcall visit vm-postponed-folder)
 
790
           (vm-select-folder-buffer)
 
791
           (make-local-hook 'vm-quit-hook)
 
792
           (add-hook 'vm-quit-hook 'vm-expunge-folder)
 
793
           (vm-expunge-folder)
 
794
           (cond ((= (length vm-message-list) 0)
 
795
                  (let ((this-command 'vm-quit))
 
796
                    (vm-quit))
 
797
                  (let ((this-command mail))
 
798
                    (funcall mail)))
 
799
                 ((= (length vm-message-list) 1)
 
800
                  (vm-continue-postponed-message))))
 
801
          ((equal action 'new)
 
802
           (let ((this-command mail))
 
803
             (funcall mail))))))
 
804
 
 
805
;;;###autoload
 
806
  (defun vm-continue-what-message-other-window ()
 
807
    "Ask for continuing of postponed messages if there are some."
 
808
    (interactive)
 
809
    (vm-continue-what-message "other-window"))
 
810
 
 
811
;;;###autoload
 
812
(defun vm-continue-what-message-other-frame ()
 
813
  "Ask for continuing of postponed messages if there are some."
 
814
  (interactive)
 
815
  (vm-continue-what-message "other-frame"))
 
816
 
 
817
;;-----------------------------------------------------------------------------
 
818
;; And now do some cool stuff when killing a mail buffer
 
819
;; This was inspired by Uwe Brauer
 
820
(defcustom vm-save-killed-message
 
821
  'ask
 
822
  "How `vm-save-killed-message-hook' handles saving of a mail as a draft.
 
823
If set to 'ask it will ask whether to save the mail as draft or not.
 
824
If set to 'always it will save without asking.
 
825
If set to nil it will never save them nor it will ask."
 
826
  :type '(choice (const ask)
 
827
                 (const always)
 
828
                 (const :tag "never" nil))
 
829
  :group 'vm-pine)
 
830
 
 
831
(defcustom vm-save-killed-messages-folder
 
832
  vm-postponed-folder
 
833
  "The name of the folder where killed messages are saved."
 
834
  :type 'string
 
835
  :group 'vm-pine)
 
836
 
 
837
(defun vm-add-save-killed-message-hook ()
 
838
  (make-local-hook  'kill-buffer-hook)
 
839
  (add-hook 'kill-buffer-hook 'vm-save-killed-message-hook nil t))
 
840
 
 
841
(defun vm-remove-save-killed-message-hook ()
 
842
  (remove-hook 'kill-buffer-hook 'vm-save-killed-message-hook t))
 
843
 
 
844
(defun vm-save-killed-message-hook ()
 
845
  (if (or (and (equal vm-save-killed-message 'ask)
 
846
               (y-or-n-p (format "Save `%s' as draft in folder `%s'? "
 
847
                                 (buffer-name)
 
848
                                 vm-save-killed-messages-folder)))
 
849
          (equal vm-save-killed-message 'always))
 
850
      (vm-postpone-message vm-save-killed-messages-folder t)
 
851
    (message "`%s' is gone forever!" (buffer-name))))
 
852
 
 
853
(add-hook 'vm-mail-mode-hook 'vm-add-save-killed-message-hook)
 
854
(add-hook 'mail-send-hook 'vm-remove-save-killed-message-hook)
 
855
(add-hook 'vm-postpone-message-hook 'vm-remove-save-killed-message-hook)
 
856
 
 
857
;;-----------------------------------------------------------------------------
 
858
;; New header fields
 
859
(define-key vm-mail-mode-map "\C-c\C-f\C-a" 'vm-mail-return-receipt-to)
 
860
(define-key vm-mail-mode-map "\C-c\C-f\C-p" 'vm-mail-priority)
 
861
(define-key vm-mail-mode-map "\C-c\C-f\C-f" 'vm-mail-fcc)
 
862
(define-key vm-mail-mode-map "\C-c\C-f\C-n" 'vm-mail-notice-requested-upon-delivery-to)
 
863
 
 
864
;;;###autoload
 
865
(defcustom vm-mail-return-receipt-to
 
866
  (concat (user-full-name) " <" user-mail-address ">")
 
867
  "The address where return receipts should be sent to."
 
868
  :type 'string
 
869
  :group 'vm-pine)
 
870
 
 
871
;;;###autoload
 
872
(defun vm-mail-return-receipt-to ()
 
873
  "Insert the \"Return-Receipt-To\" header into a `vm-mail-mode' buffer.
 
874
See the variable `vm-mail-return-receipt-to'."
 
875
  (interactive)
 
876
  (expand-abbrev)
 
877
  (save-excursion
 
878
    (or (mail-position-on-field "Return-Receipt-To" t)
 
879
        (progn (mail-position-on-field "Subject")
 
880
               (insert "\nReturn-Receipt-To: " vm-mail-return-receipt-to
 
881
                       "\nRead-Receipt-To: " vm-mail-return-receipt-to
 
882
                       "\nDelivery-Receipt-To: " vm-mail-return-receipt-to))))
 
883
  (message "Remove those headers you do not require!"))
 
884
 
 
885
;;;###autoload
 
886
(defun vm-mail-notice-requested-upon-delivery-to ()
 
887
  "Notice-Requested-Upon-Delivery-To:"
 
888
  (interactive)
 
889
  (expand-abbrev)
 
890
  (save-excursion
 
891
    (or (mail-position-on-field "Notice-Requested-Upon-Delivery-To" t)
 
892
        (progn (mail-position-on-field "Subject")
 
893
               (insert "\nNotice-Requested-Upon-Delivery-To: "
 
894
                       (let ((to (vm-mail-get-header-contents
 
895
                                  "\\(.*-\\)?To:")))
 
896
                         (if to to "")))))))
 
897
 
 
898
;;;###autoload
 
899
(defcustom vm-mail-priority
 
900
  "Priority: urgent\nImportance: High\nX-Priority: 1"
 
901
  "The priority headers."
 
902
  :type 'string
 
903
  :group 'vm-pine)
 
904
 
 
905
;;;###autoload
 
906
(defun vm-mail-priority ()
 
907
  "Insert priority headers into a `vm-mail-mode' buffer.
 
908
See the variable `vm-mail-priority'."
 
909
  (interactive)
 
910
  (expand-abbrev)
 
911
  (save-excursion
 
912
    (or (mail-position-on-field "Priority" t)
 
913
        (progn (mail-position-on-field "Subject")
 
914
               (insert "\n" vm-mail-priority)))))
 
915
 
 
916
;;-----------------------------------------------------------------------------
 
917
(if (not vm-xemacs-p)
 
918
    (defun user-home-directory ()
 
919
      (getenv "HOME")))
 
920
 
 
921
(defun vm-mail-fcc-file-join (dir file)
 
922
  "Returns a nice path to a folder."
 
923
  (let* ((path (expand-file-name file dir)))
 
924
    (if path
 
925
        (if (not vm-xemacs-p)
 
926
            (abbreviate-file-name path)
 
927
          (abbreviate-file-name path t))
 
928
      dir)))
 
929
 
 
930
;;;###autoload
 
931
(defcustom vm-mail-folder-alist vm-auto-folder-alist
 
932
  "Like `vm-auto-folder-alist' but for outgoing messages.
 
933
It should be fed to `vm-mail-select-folder'!"
 
934
  :type 'list
 
935
  :group 'vm-pine)
 
936
 
 
937
;;;###autoload
 
938
(defcustom vm-mail-fcc-default
 
939
  '(or (vm-mail-select-folder vm-mail-folder-alist)
 
940
       (vm-mail-to-fcc nil t)
 
941
       mail-archive-file-name)
 
942
  "A list which is evaluated to return a folder name.
 
943
By reordering the elements of this list or adding own functions you
 
944
can control the behavior of vm-mail-fcc and `vm-mail-auto-fcc'.
 
945
You may allow a sophisticated decision for the right folder for your
 
946
outgoing message."
 
947
  :type 'list
 
948
  :group 'vm-pine)
 
949
 
 
950
;;;###autoload
 
951
(defun vm-mail-fcc (&optional arg)
 
952
  "Insert the FCC-header into a `vm-mail-mode' buffer.
 
953
Like `mail-fcc', but honors VM variables and offers a default folder
 
954
according to `vm-mail-folder-alist'.
 
955
Called with prefix ARG it just removes the FCC-header."
 
956
  (interactive "P")
 
957
  (expand-abbrev)
 
958
 
 
959
  (let ((dir (or vm-folder-directory default-directory))
 
960
        (fcc nil)
 
961
        (folder (vm-mail-mode-get-header-contents "FCC:"))
 
962
        (prompt nil))
 
963
    
 
964
    (if arg (progn (vm-mail-mode-remove-header "FCC:")
 
965
                   (message "FCC header removed!"))
 
966
      (save-excursion
 
967
        (setq fcc (eval vm-mail-fcc-default))
 
968
 
 
969
        ;; cleanup the name 
 
970
        (setq fcc (if fcc (vm-mail-fcc-file-join dir fcc)))
 
971
 
 
972
        (setq prompt (if fcc
 
973
                         (format "FCC to folder (%s): " fcc)
 
974
                       "FCC to folder: "))
 
975
 
 
976
        (setq folder (if (and folder (not (file-directory-p folder)))
 
977
                         (file-relative-name folder dir)))
 
978
 
 
979
        ;; we got the name so insert it 
 
980
        (vm-mail-mode-remove-header "FCC:")
 
981
        (setq fcc (vm-read-file-name prompt
 
982
                                        dir fcc
 
983
                                        nil folder
 
984
                                        'vm-folder-history))
 
985
        (setq fcc (vm-mail-fcc-file-join dir fcc))
 
986
        (if (file-directory-p fcc)
 
987
            (error "Folder `%s' in no file, but a directory!" fcc)
 
988
          (mail-position-on-field "FCC")
 
989
          (insert fcc))))))
 
990
 
 
991
;;;###autoload
 
992
(defun vm-mail-auto-fcc ()
 
993
  "Add a new FCC field, with file name guessed by `vm-mail-folder-alist'.
 
994
You likely want to add it to `vm-reply-hook' by
 
995
   (add-hook 'vm-reply-hook 'vm-mail-auto-fcc)
 
996
or if sure about what you are doing you can add it to mail-send-hook!"
 
997
  (interactive "")
 
998
  (expand-abbrev)
 
999
  (save-excursion
 
1000
    (let ((dir (or vm-folder-directory default-directory))
 
1001
          (fcc nil))
 
1002
      
 
1003
      (vm-mail-mode-remove-header "FCC:")
 
1004
      (setq fcc (eval vm-mail-fcc-default))
 
1005
      (if fcc
 
1006
          (if (file-directory-p fcc)
 
1007
              (error "Folder `%s' in no file, but a directory!" fcc)
 
1008
            (progn (mail-position-on-field "FCC")
 
1009
                   (insert (vm-mail-fcc-file-join dir fcc))))))))
 
1010
 
 
1011
;;;###autoload
 
1012
(defun vm-mail-get-header-contents (header-name-regexp &optional clump-sep)
 
1013
  "Return the contents of the header(s) matching HEADER-NAME-REGEXP.
 
1014
This function is a slightly changed version of `vm-get-header-contents'.
 
1015
Optional argument CLUMP-SEP usually a \",\"."
 
1016
  (let ((contents nil)
 
1017
        (text-of-message 0)
 
1018
        (regexp (concat "^\\(" header-name-regexp "\\)")))
 
1019
    (save-excursion
 
1020
      (goto-char (point-min))
 
1021
      (if (re-search-forward (regexp-quote mail-header-separator)
 
1022
                             (point-max) t)
 
1023
          (setq text-of-message (match-end 0))
 
1024
        (error "No mail header separator found!"))
 
1025
 
 
1026
      (goto-char (point-min))
 
1027
      (let ((case-fold-search t))
 
1028
        (while (and (or (null contents) clump-sep)
 
1029
                    (re-search-forward regexp text-of-message t)
 
1030
                    (save-excursion (goto-char (match-beginning 0))
 
1031
                                    (vm-match-header)))
 
1032
          (if contents
 
1033
              (setq contents
 
1034
                    (concat contents clump-sep (vm-matched-header-contents)))
 
1035
            (setq contents (vm-matched-header-contents)))))
 
1036
      contents)))
 
1037
 
 
1038
;;;###autoload
 
1039
(defun vm-mail-select-folder (folder-alist)
 
1040
  "Return a folder according to FOLDER-ALIST for the current message.
 
1041
This function is a slightly changed version of `vm-auto-select-folder'."
 
1042
  (interactive)
 
1043
  (condition-case error-data
 
1044
      (catch 'match
 
1045
        (let (header tuple-list)
 
1046
          (while folder-alist
 
1047
            (setq header (vm-mail-get-header-contents
 
1048
                          (car (car folder-alist)) ", "))
 
1049
            (if (null header)
 
1050
                ()
 
1051
              (setq tuple-list (cdr (car folder-alist)))
 
1052
              (while tuple-list
 
1053
                (if (let ((case-fold-search vm-auto-folder-case-fold-search))
 
1054
                      (string-match (car (car tuple-list)) header))
 
1055
                    ;; Don't waste time eval'ing an atom.
 
1056
                    (if (stringp (cdr (car tuple-list)))
 
1057
                        (throw 'match (cdr (car tuple-list)))
 
1058
                      (let* ((match-data (vm-match-data))
 
1059
                             ;; allow this buffer to live forever
 
1060
                             (buf (get-buffer-create " *vm-auto-folder*"))
 
1061
                             (result))
 
1062
                        ;; Set up a buffer that matches our cached
 
1063
                        ;; match data.
 
1064
                        (save-excursion
 
1065
                          (set-buffer buf)
 
1066
                          (if vm-fsfemacs-mule-p
 
1067
                              (set-buffer-multibyte nil))
 
1068
                          (widen)
 
1069
                          (erase-buffer)
 
1070
                          (insert header)
 
1071
                          ;; It appears that get-buffer-create clobbers the
 
1072
                          ;; match-data.
 
1073
                          ;;
 
1074
                          ;; The match data is off by one because we matched
 
1075
                          ;; a string and Emacs indexes strings from 0 and
 
1076
                          ;; buffers from 1.
 
1077
                          ;;
 
1078
                          ;; Also store-match-data only accepts MARKERS!!
 
1079
                          ;; AUGHGHGH!!
 
1080
                          (store-match-data
 
1081
                           (mapcar
 
1082
                            (function (lambda (n) (and n (vm-marker n))))
 
1083
                            (mapcar
 
1084
                             (function (lambda (n) (and n (1+ n))))
 
1085
                             match-data)))
 
1086
                          (setq result (eval (cdr (car tuple-list))))
 
1087
                          (while (consp result)
 
1088
                            (setq result (vm-mail-select-folder result)))
 
1089
                          (if result
 
1090
                              (throw 'match result))))))
 
1091
                (setq tuple-list (cdr tuple-list))))
 
1092
            (setq folder-alist (cdr folder-alist)))
 
1093
          nil ))
 
1094
    (error "Error processing folder-alist: %s"
 
1095
           (prin1-to-string error-data))))
 
1096
 
 
1097
;;;###autoload
 
1098
(defcustom vm-mail-to-regexp "\\([^<\t\n ]+\\)@"
 
1099
  "A regexp matching the part of an email address to use as FCC-folder.
 
1100
The string enclosed in \"\\\\(\\\\)\" is used as folder name."
 
1101
  :type 'regexp
 
1102
  :group 'vm-pine)
 
1103
 
 
1104
;;;###autoload
 
1105
(defcustom vm-mail-to-headers '("To:" "CC:" "BCC:")
 
1106
  "A list of headers for finding the email address to use as FCC-folder."
 
1107
  :type '(repeat (string))
 
1108
  :group 'vm-pine)
 
1109
 
 
1110
;;;###autoload
 
1111
(defun vm-mail-to-fcc (&optional arg return-only)
 
1112
  "Insert a FCC-header into a `vm-mail-mode' buffer.
 
1113
Like `mail-fcc', but honors VM variables and inserts the first email
 
1114
address (or the like matched by `vm-mail-to-regexp') found in the headers
 
1115
listed in `vm-mail-to-headers'.
 
1116
Called with prefix ARG it just removes the FCC-header.
 
1117
If optional argument RETURN-ONLY is t just returns FCC."
 
1118
  (interactive "P")
 
1119
  (expand-abbrev)
 
1120
  (let ((fcc nil)
 
1121
        (headers vm-mail-to-headers))
 
1122
    (if arg (progn (vm-mail-mode-remove-header "FCC:")
 
1123
                   (message "FCC header removed!"))
 
1124
      (progn
 
1125
        (while (and (not fcc) headers)
 
1126
          (setq fcc (vm-mail-get-header-contents (car headers)))
 
1127
          (if (and fcc (string-match vm-mail-to-regexp fcc))
 
1128
              (setq fcc (match-string 1 fcc))
 
1129
            (setq fcc nil))
 
1130
          (setq headers (cdr headers)))
 
1131
        (setq fcc (or fcc mail-archive-file-name))
 
1132
        (if return-only
 
1133
            fcc
 
1134
          (if fcc
 
1135
              (if (file-directory-p fcc)
 
1136
                  (error "Folder `%s' in no file, but a directory!" fcc)
 
1137
                (vm-mail-mode-remove-header "FCC:")
 
1138
                (mail-position-on-field "FCC")
 
1139
                (insert (vm-mail-fcc-file-join (or vm-folder-directory
 
1140
                                                   default-directory)
 
1141
                                               fcc)))))))))
 
1142
 
 
1143
;;-----------------------------------------------------------------------------
 
1144
(provide 'vm-pine)
 
1145
 
 
1146
;;; vm-pine.el ends here