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