Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / org / org-vm.el
1 ;;; org-vm.el --- Support for links to VM messages from within Org-mode
2
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
4
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.4
9 ;;
10 ;; This file is part of GNU Emacs.
11 ;;
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;
26 ;;; Commentary:
27 ;; This file implements links to VM messages and folders from within Org-mode.
28 ;; Org-mode loads this module by default - if this is not what you want,
29 ;; configure the variable `org-modules'.
30
31 ;;; Code:
32
33 (require 'org)
34
35 ;; Declare external functions and variables
36 (declare-function vm-preview-current-message "ext:vm-page" ())
37 (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
38 (declare-function vm-get-header-contents "ext:vm-summary"
39 (message header-name-regexp &optional clump-sep))
40 (declare-function vm-isearch-narrow "ext:vm-search" ())
41 (declare-function vm-isearch-update "ext:vm-search" ())
42 (declare-function vm-select-folder-buffer "ext:vm-macro" ())
43 (declare-function vm-su-message-id "ext:vm-summary" (m))
44 (declare-function vm-su-subject "ext:vm-summary" (m))
45 (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
46 (defvar vm-message-pointer)
47 (defvar vm-folder-directory)
48
49 ;; Install the link type
50 (org-add-link-type "vm" 'org-vm-open)
51 (add-hook 'org-store-link-functions 'org-vm-store-link)
52
53 ;; Implementation
54 (defun org-vm-store-link ()
55 "Store a link to a VM folder or message."
56 (when (or (eq major-mode 'vm-summary-mode)
57 (eq major-mode 'vm-presentation-mode))
58 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
59 (vm-follow-summary-cursor)
60 (save-excursion
61 (vm-select-folder-buffer)
62 (let* ((message (car vm-message-pointer))
63 (folder buffer-file-name)
64 (subject (vm-su-subject message))
65 (to (vm-get-header-contents message "To"))
66 (from (vm-get-header-contents message "From"))
67 (message-id (vm-su-message-id message))
68 (date (vm-get-header-contents message "Date"))
69 (date-ts (and date (format-time-string
70 (org-time-stamp-format t)
71 (date-to-time date))))
72 (date-ts-ia (and date (format-time-string
73 (org-time-stamp-format t t)
74 (date-to-time date))))
75 desc link)
76 (org-store-link-props :type "vm" :from from :to to :subject subject
77 :message-id message-id)
78 (when date
79 (org-add-link-props :date date :date-timestamp date-ts
80 :date-timestamp-inactive date-ts-ia))
81 (setq message-id (org-remove-angle-brackets message-id))
82 (setq folder (abbreviate-file-name folder))
83 (if (and vm-folder-directory
84 (string-match (concat "^" (regexp-quote vm-folder-directory))
85 folder))
86 (setq folder (replace-match "" t t folder)))
87 (setq desc (org-email-link-description))
88 (setq link (org-make-link "vm:" folder "#" message-id))
89 (org-add-link-props :link link :description desc)
90 link))))
91
92 (defun org-vm-open (path)
93 "Follow a VM message link specified by PATH."
94 (let (folder article)
95 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
96 (error "Error in VM link"))
97 (setq folder (match-string 1 path)
98 article (match-string 3 path))
99 ;; The prefix argument will be interpreted as read-only
100 (org-vm-follow-link folder article current-prefix-arg)))
101
102 (defun org-vm-follow-link (&optional folder article readonly)
103 "Follow a VM link to FOLDER and ARTICLE."
104 (require 'vm)
105 (setq article (org-add-angle-brackets article))
106 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
107 ;; ange-ftp or efs or tramp access
108 (let ((user (or (match-string 1 folder) (user-login-name)))
109 (host (match-string 2 folder))
110 (file (match-string 3 folder)))
111 (cond
112 ((featurep 'tramp)
113 ;; use tramp to access the file
114 (if (featurep 'xemacs)
115 (setq folder (format "[%s@%s]%s" user host file))
116 (setq folder (format "/%s@%s:%s" user host file))))
117 (t
118 ;; use ange-ftp or efs
119 (require (if (featurep 'xemacs) 'efs 'ange-ftp))
120 (setq folder (format "/%s@%s:%s" user host file))))))
121 (when folder
122 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
123 (sit-for 0.1)
124 (when article
125 (require 'vm-search)
126 (vm-select-folder-buffer)
127 (widen)
128 (let ((case-fold-search t))
129 (goto-char (point-min))
130 (if (not (re-search-forward
131 (concat "^" "message-id: *" (regexp-quote article))))
132 (error "Could not find the specified message in this folder"))
133 (vm-isearch-update)
134 (vm-isearch-narrow)
135 (vm-preview-current-message)
136 (vm-summarize)))))
137
138 (provide 'org-vm)
139
140
141 ;;; org-vm.el ends here