1 ;;; vm-pine.el --- draft handling and other neat functions for VM
3 ;; Copyright (C) 1998-2006 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
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)
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.
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.
27 ;; This package provides the following new features for VM:
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.
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
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
46 ;; (define-key vm-mode-map "m" 'vm-continue-what-message)
47 ;; (setq vm-zero-drafts-start-compose t)
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.
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.
58 ;; (autoload 'vm-continue-what-message-other-window "vm-pine" "" t)
59 ;; (global-set-key "\C-xm" 'vm-continue-what-message-other-window)
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.
78 ;;; Bug reports and feature requests:
79 ;; Please send a backtrace and the version number of vm-pine.el!
80 ;; Feature requests are welcome!
84 ;; Attempt to handle older/other emacs.
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'!!!")
105 (defgroup vm-pine nil
106 "Pine inspired extensions to VM."
109 ;;-----------------------------------------------------------------------------
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
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)
123 See also: `vm-summary-uninteresting-senders'"
125 (let ((case-fold-search t)
126 (headers '(("From:" "")
127 ("Newsgroups:" "News:")
129 "Resent-To:" "Resent-CC:" "Resent-BCC:"
130 ("Sender:" "") ("Resent-From:" "Resent:")))
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))
142 (setq addresses (vm-decode-mime-encoded-words-in-string addresses)
144 (or (if (functionp 'bbdb-extract-address-components)
145 (bbdb-extract-address-components addresses t))
146 (list (mail-extract-address-components addresses))
148 (if (not first) (setq first (car 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)))
158 (if (and (null address) (null first))
160 (if (and (null address) first)
161 (setq address first))
163 (cond ((functionp 'bbdb/vm-alternate-full-name)
164 (or (bbdb/vm-alternate-full-name (cadr address))
167 (t (or (car address) (cadr address))))))))
169 ;;-----------------------------------------------------------------------------
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'."
184 (defcustom vm-postponed-attribute "P"
185 "Summary string that will be inserted by `vm-summary-function-p' for
191 (defun vm-summary-function-p (m)
192 "Return 'P' for postponed messages."
194 (if (vm-get-header-contents m vm-postponed-header)
198 ;;-----------------------------------------------------------------------------
199 ;; A Pine-like postponed folder handling
201 (defcustom vm-postponed-folder "postponed"
202 "The name of the folder where postponed messages are saved."
207 (defcustom vm-postponed-message-headers '("From:" "Organization:"
214 "X-Priority:" "Priority:")
215 "Similar to `vm-forwarded-headers'.
216 A list of headers that should be kept, when continuing a postponed message.
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))
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."
232 (defcustom vm-continue-postponed-message-hook nil
233 "List of hook functions to be run after continuing a postponed message."
238 (defcustom vm-postpone-message-hook nil
239 "List of hook functions to be run before postponing a message."
243 (defvar vm-postponed-message-folder-buffer nil
244 "Buffer of source folder.
245 This is only for internal use of vm-pine.el!!!")
247 ;;-----------------------------------------------------------------------------
248 (define-key vm-mode-map "C" 'vm-continue-what-message)
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)
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))
261 (setcdr f (cons mid (cdr f)))
262 (add-to-list 'midlist (list folder mid))))
263 (setq mlist (cdr mlist)))
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)
271 (setq folder (caar msgidlist))
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))
280 (message "The folder '%s' does not exist anymore. Maybe it was virtual or closed before postponing." folder)
282 (vm-select-folder-buffer)
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))
291 (while (re-search-forward msgid-regexp point-max t)
292 (let ((point (point))
293 (mp vm-message-list))
295 (if (and (>= point (vm-start-of (car mp)))
296 (<= point (vm-end-of (car mp))))
297 (setq vm-message-pointers (cons (car mp)
300 (setq mp (cdr mp)))))))))
301 (setq msgidlist (cdr msgidlist))))
302 vm-message-pointers))
304 ;;-----------------------------------------------------------------------------
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
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.
321 In `vm-mail-mode' this is bound to [C].
322 If optional argument SILENT is positive then act in background (no frame
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)
333 (if (eq vm-system-state 'previewing)
334 (vm-show-current-message))
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)
347 (vm-pp-data (vm-get-header-contents (car vmp)
348 vm-postponed-header)))
350 ;; Prepare the composition buffer
351 (if (and to (string-match "[^,\n<(]+" to))
352 (setq to (match-string 0 to)))
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))
366 (setq vm-mail-buffer folder-buffer))
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)
377 ;; set the VM variables for setting source message attributes
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)
384 (and (nth 1 vm-pp-data) (vm-get-message-pointers-for (nth 1 vm-pp-data)))
386 (and (nth 2 vm-pp-data) (vm-get-message-pointers-for (nth 2 vm-pp-data)))
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)))
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)
409 ;; Add message body as previewed
410 (goto-char (point-max))
411 (if presentation-buffer
412 ;; when using presentation buffer we have to
414 (set-buffer presentation-buffer)
415 (goto-char (point-min))
416 (search-forward-regexp "\n\n")
417 (setq tstart (match-end 0)
419 (setq presentation-buffer folder-buffer))
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)
425 ;; and add the buttons for attachments
426 (vm-decode-postponed-mime-message)))
429 (run-hooks 'mail-setup-hook)
430 (run-hooks 'vm-mail-hook)
431 (run-hooks 'vm-mail-mode-hook))
433 (run-hooks 'vm-continue-postponed-message-hook))
435 ;;-----------------------------------------------------------------------------
437 (defun vm-reply-by-continue-postponed-message ()
438 "Like `vm-reply' but preserves attachments."
440 (let ((vm-continue-postponed-message-hook)
442 (vm-mail-mode-hook nil)
443 (mail-setup-hook nil)
449 (vm-continue-postponed-message t)
450 (goto-char (point-min))
451 (re-search-forward (regexp-quote mail-header-separator) (point-max))
453 (setq reply-buffer (current-buffer)
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))
464 ;;-----------------------------------------------------------------------------
465 (defun vm-delete-postponed-message ()
466 "Delete the source message belonging to the continued composition."
468 (if vm-message-pointer
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))))
482 (switch-to-buffer buffer)
485 (when (not vm-message-list)
486 (let ((this-command 'vm-quit))
488 (error "Folder buffer closed before deletion of source message."))))
490 ;;-----------------------------------------------------------------------------
491 (defun vm-decode-postponed-mime-message ()
492 "Replace the mime buttons by attachment buttons."
495 (let ((e-list (extent-list nil (point-min) (point-max))))
496 ;; First collect the extents
499 (function (lambda (e)
500 (extent-property e 'vm-mime-layout)))
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.
508 (vm-decode-postponed-mime-button (car e-list))
509 (setq e-list (cdr e-list)))))
511 (let ((o-list (vm-pine-fake-attachment-overlays (point-min)
513 (setq o-list (sort (vm-delete
514 (function (lambda (o)
515 (overlay-get o 'vm-mime-layout)))
520 (overlay-end e2))))))
522 (vm-decode-postponed-mime-button (car o-list))
523 (setq o-list (cdr o-list)))))
525 (error "don't know how to MIME dencode composition for %s"
528 (defun vm-pine-fake-attachment-overlays (start end)
535 (narrow-to-region start end)
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))
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)))
550 (overlay-put o (car props) (car (cdr props)))
551 (setq props (cdr (cdr props))))
552 (setq o-list (cons o o-list))))
556 ;;-----------------------------------------------------------------------------
557 (defun vm-decode-postponed-mime-button (x)
558 "Replace the mime button specified by X."
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)))
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)
580 (file (vm-mime-get-disposition-parameter layout "filename"))
582 (type (vm-mm-layout-type layout)))
585 (string= (car type) "message/external-body")
586 (string= (cadr type) "access-type=local-file"))
588 (setq filename (substring (caddr type) 5))
589 (vm-select-folder-buffer)
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)))
596 (if (not (re-search-forward
597 "Content-Type: \"?\\([^ ;\" \n\t]+\\)\"?;?"
599 (error "No `Content-Type' header found in: %s"
600 (buffer-substring start end))
601 (setq type (list (match-string 1))))))))
603 ;; delete the mime-button
605 (delete-region xstart xend)
606 ;; and insert an attached-object-button
608 (vm-mime-attach-file filename (car type))
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)))))))
615 ;;-----------------------------------------------------------------------------
616 (define-key vm-mail-mode-map "\C-c\C-d" 'vm-postpone-message)
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.")
622 ;;-----------------------------------------------------------------------------
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
630 Optional argument DONT-KILL is positive, then do not kill source message."
633 (let ((message-buffer (current-buffer))
637 (let (m (modes vm-postpone-message-modes-to-disable))
639 (setq m (car modes) modes (cdr modes))
640 (if (and (boundp m) (symbol-value m))
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)))
650 ;; there is no explicit folder given ...
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)))))
659 (error "I could not find a folder for postponing messages!"))
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))))
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)))
677 (insert (format "From: %s\n" login))))))
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)))
683 ;; add the current date
684 (if (not (vm-mail-mode-get-header-contents "Date:"))
686 (format-time-string "%a, %d %b %Y %H:%M:%S %Z"
689 ;; add the postponed header
690 (vm-mail-mode-remove-header vm-postponed-header)
692 (if no-postpone-header nil
693 (insert vm-postponed-header " "
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))))
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))
708 (run-hooks 'vm-postpone-message-hook)
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)))
716 (setq folder-buffer (vm-get-file-buffer folder))
718 ;; o.k. the folder is already opened
720 (set-buffer folder-buffer)
721 (vm-error-if-folder-read-only)
722 (let ((buffer-read-only nil))
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))
730 (cond ((eq major-mode 'vm-mode)
731 (vm-increment vm-messages-not-on-disk)
732 (vm-clear-modification-flag-undos)))
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))
741 (if (eq target-type 'unknown)
742 (error "Folder `%s' type is unrecognized" folder))
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)))
748 ;; delete source message
749 (vm-delete-postponed-message)
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
764 (vm-display b nil '(vm-mail-send-and-exit)
765 '(vm-mail-send-and-exit reading-message startup)))))
767 ;; and kill this buffer?
769 (insert (concat "FCC: " folder "\n" mail-header-separator))
773 (message "Message postponed to folder `%s'" folder))))
775 ;;-----------------------------------------------------------------------------
776 (defun vm-buffer-in-vm-mode ()
777 (member major-mode '(vm-mode vm-virtual-mode
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)
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))
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!"
800 (vm-session-initialization)
802 (let* ((ppfolder (and vm-postponed-folder
803 (expand-file-name vm-postponed-folder
804 (or vm-folder-directory
805 default-directory))))
809 (when current-prefix-arg
810 (setq action 'force-continue))
812 (when (vm-find-composition-buffer)
813 (setq action 'continue))
815 ;; postponed message in current folder
816 (when (vm-buffer-in-vm-mode)
817 (vm-check-for-killed-folder)
818 (vm-select-folder-buffer)
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)))
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
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))))
838 (setq action 'visit)))
840 ;; visit postponed folder
841 (when (and (not action) (file-exists-p ppfolder)
842 (> (nth 7 (file-attributes ppfolder)) 0))
843 (setq action 'visit))
845 (if (not action) (setq action 'new))
849 (cond ((eq vm-continue-what-message nil)
851 ((eq vm-continue-what-message 'ask)
852 (if (equal action 'visit)
854 "Continue composition of postponed messages? ")
858 ((eq vm-continue-what-message 'continue)
864 (defun vm-continue-what-message (&optional where)
865 "Continue compositions or postponed messages if there are some.
867 With a prefix arg, call `vm-continue-postponed-message', i.e. continue the
868 currently selected message.
870 See `vm-continue-what-message' and `vm-zero-drafts-start-compose' for
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)
889 (cond ((= (length vm-message-list) 0)
890 (let ((this-command 'vm-quit))
892 (let ((this-command 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))
900 (message "There are no known drafts.")))))
903 (defun vm-continue-what-message-other-window ()
904 "Ask for continuing of postponed messages if there are some."
906 (vm-continue-what-message "other-window"))
909 (defun vm-continue-what-message-other-frame ()
910 "Ask for continuing of postponed messages if there are some."
912 (vm-continue-what-message "other-frame"))
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
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)
925 (const :tag "never" nil))
928 (defcustom vm-save-killed-messages-folder
930 "The name of the folder where killed messages are saved."
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))
938 (defun vm-remove-save-killed-message-hook ()
939 (remove-hook 'kill-buffer-hook 'vm-save-killed-message-hook t))
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'? "
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))))
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)
954 ;;-----------------------------------------------------------------------------
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)
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."
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'."
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!"))
983 (defun vm-mail-notice-requested-upon-delivery-to ()
984 "Notice-Requested-Upon-Delivery-To:"
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
996 (defcustom vm-mail-priority
997 "Priority: urgent\nImportance: High\nX-Priority: 1"
998 "The priority headers."
1003 (defun vm-mail-priority ()
1004 "Insert priority headers into a `vm-mail-mode' buffer.
1005 See the variable `vm-mail-priority'."
1009 (or (mail-position-on-field "Priority" t)
1010 (progn (mail-position-on-field "Subject")
1011 (insert "\n" vm-mail-priority)))))
1013 ;;-----------------------------------------------------------------------------
1014 (if (not vm-xemacs-p)
1015 (defun user-home-directory ()
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)))
1023 (abbreviate-file-name path t)
1024 (abbreviate-file-name path))
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'!"
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
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."
1057 (let ((dir (or vm-folder-directory default-directory))
1059 (folder (vm-mail-mode-get-header-contents "FCC:"))
1062 (if arg (progn (vm-mail-mode-remove-header "FCC:")
1063 (message "FCC header removed!"))
1065 (setq fcc (eval vm-mail-fcc-default))
1068 (setq fcc (if fcc (vm-mail-fcc-file-join dir fcc)))
1070 (setq prompt (if fcc
1071 (format "FCC to folder (%s): " fcc)
1074 (setq folder (if (and folder (not (file-directory-p folder)))
1075 (file-relative-name folder dir)))
1077 ;; we got the name so insert it
1078 (vm-mail-mode-remove-header "FCC:")
1079 (setq fcc (vm-read-file-name prompt
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")
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!"
1098 (let ((dir (or vm-folder-directory default-directory))
1101 (vm-mail-mode-remove-header "FCC:")
1102 (setq fcc (eval vm-mail-fcc-default))
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))))))))
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)
1116 (regexp (concat "^\\(" header-name-regexp "\\)")))
1118 (goto-char (point-min))
1119 (if (re-search-forward (regexp-quote mail-header-separator)
1121 (setq text-of-message (match-end 0))
1122 (error "No mail header separator found!"))
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))
1132 (concat contents clump-sep (vm-matched-header-contents)))
1133 (setq contents (vm-matched-header-contents)))))
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'."
1141 (condition-case error-data
1143 (let (header tuple-list)
1145 (setq header (vm-mail-get-header-contents
1146 (car (car folder-alist)) ", "))
1149 (setq tuple-list (cdr (car folder-alist)))
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*"))
1160 ;; Set up a buffer that matches our cached
1164 (if vm-fsfemacs-mule-p
1165 (set-buffer-multibyte nil))
1169 ;; It appears that get-buffer-create clobbers the
1172 ;; The match data is off by one because we matched
1173 ;; a string and Emacs indexes strings from 0 and
1176 ;; Also store-match-data only accepts MARKERS!!
1180 (function (lambda (n) (and n (vm-marker n))))
1182 (function (lambda (n) (and n (1+ n))))
1184 (setq result (eval (cdr (car tuple-list))))
1185 (while (consp result)
1186 (setq result (vm-mail-select-folder result)))
1188 (throw 'match result))))))
1189 (setq tuple-list (cdr tuple-list))))
1190 (setq folder-alist (cdr folder-alist)))
1192 (error "Error processing folder-alist: %s"
1193 (prin1-to-string error-data))))
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."
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))
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."
1219 (headers vm-mail-to-headers))
1220 (if arg (progn (vm-mail-mode-remove-header "FCC:")
1221 (message "FCC header removed!"))
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))
1228 (setq headers (cdr headers)))
1229 (setq fcc (or fcc mail-archive-file-name))
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
1241 ;;-----------------------------------------------------------------------------
1244 ;;; vm-pine.el ends here