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