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