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