org-vm.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; org-vm.el --- Support for links to VM messages from within Org-mode
  2. ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; Support for IMAP folders added
  8. ;; by Konrad Hinsen <konrad dot hinsen at fastmail dot net>
  9. ;; Requires VM 8.2.0a or later.
  10. ;;
  11. ;; This file is not part of GNU Emacs.
  12. ;;
  13. ;; This program 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.
  17. ;; This program 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. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  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. ;;; Code:
  30. (require 'org)
  31. ;; Declare external functions and variables
  32. (declare-function vm-preview-current-message "ext:vm-page" ())
  33. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  34. (declare-function vm-get-header-contents "ext:vm-summary"
  35. (message header-name-regexp &optional clump-sep))
  36. (declare-function vm-isearch-narrow "ext:vm-search" ())
  37. (declare-function vm-isearch-update "ext:vm-search" ())
  38. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  39. (declare-function vm-su-message-id "ext:vm-summary" (m))
  40. (declare-function vm-su-subject "ext:vm-summary" (m))
  41. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  42. (declare-function vm-imap-folder-p "ext:vm-save" ())
  43. (declare-function vm-imap-find-spec-for-buffer "ext:vm-imap" (buffer))
  44. (declare-function vm-imap-folder-for-spec "ext:vm-imap" (spec))
  45. (declare-function vm-imap-parse-spec-to-list "ext:vm-imap" (spec))
  46. (declare-function vm-imap-spec-for-account "ext:vm-imap" (account))
  47. (defvar vm-message-pointer)
  48. (defvar vm-folder-directory)
  49. ;; Install the link type
  50. (org-add-link-type "vm" 'org-vm-open)
  51. (org-add-link-type "vm-imap" 'org-vm-imap-open)
  52. (add-hook 'org-store-link-functions 'org-vm-store-link)
  53. ;; Implementation
  54. (defun org-vm-store-link ()
  55. "Store a link to a VM folder or message."
  56. (when (and (or (eq major-mode 'vm-summary-mode)
  57. (eq major-mode 'vm-presentation-mode))
  58. (save-window-excursion
  59. (vm-select-folder-buffer) buffer-file-name))
  60. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  61. (vm-follow-summary-cursor)
  62. (save-excursion
  63. (vm-select-folder-buffer)
  64. (let* ((message (car vm-message-pointer))
  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))
  69. (link-type (if (vm-imap-folder-p) "vm-imap" "vm"))
  70. (date (vm-get-header-contents message "Date"))
  71. (date-ts (and date (format-time-string
  72. (org-time-stamp-format t)
  73. (date-to-time date))))
  74. (date-ts-ia (and date (format-time-string
  75. (org-time-stamp-format t t)
  76. (date-to-time date))))
  77. folder desc link)
  78. (if (vm-imap-folder-p)
  79. (let ((spec (vm-imap-find-spec-for-buffer (current-buffer))))
  80. (setq folder (vm-imap-folder-for-spec spec)))
  81. (progn
  82. (setq folder (abbreviate-file-name buffer-file-name))
  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 message-id (org-remove-angle-brackets message-id))
  88. (org-store-link-props :type link-type :from from :to to :subject subject
  89. :message-id message-id)
  90. (when date
  91. (org-add-link-props :date date :date-timestamp date-ts
  92. :date-timestamp-inactive date-ts-ia))
  93. (setq desc (org-email-link-description))
  94. (setq link (concat (concat link-type ":") folder "#" message-id))
  95. (org-add-link-props :link link :description desc)
  96. 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)))
  106. (defun org-vm-follow-link (&optional folder article readonly)
  107. "Follow a VM link to FOLDER and ARTICLE."
  108. (require 'vm)
  109. (setq article (org-add-angle-brackets article))
  110. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  111. ;; ange-ftp or efs or tramp access
  112. (let ((user (or (match-string 1 folder) (user-login-name)))
  113. (host (match-string 2 folder))
  114. (file (match-string 3 folder)))
  115. (cond
  116. ((featurep 'tramp)
  117. ;; use tramp to access the file
  118. (if (featurep 'xemacs)
  119. (setq folder (format "[%s@%s]%s" user host file))
  120. (setq folder (format "/%s@%s:%s" user host file))))
  121. (t
  122. ;; use ange-ftp or efs
  123. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  124. (setq folder (format "/%s@%s:%s" user host file))))))
  125. (when folder
  126. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  127. (when article
  128. (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))))))
  147. (defun org-vm-select-message (message-id)
  148. "Go to the message with message-id in the current folder."
  149. (require 'vm-search)
  150. (sit-for 0.1)
  151. (vm-select-folder-buffer)
  152. (widen)
  153. (let ((case-fold-search t))
  154. (goto-char (point-min))
  155. (if (not (re-search-forward
  156. (concat "^" "message-id: *" (regexp-quote message-id))))
  157. (error "Could not find the specified message in this folder"))
  158. (vm-isearch-update)
  159. (vm-isearch-narrow)
  160. (vm-preview-current-message)
  161. (vm-summarize)))
  162. (provide 'org-vm)
  163. ;;; org-vm.el ends here