org-vm.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;;; org-vm.el --- Support for links to VM messages from within Org-mode
  2. ;; Copyright (C) 2004-2016 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. folder desc link)
  72. (if (vm-imap-folder-p)
  73. (let ((spec (vm-imap-find-spec-for-buffer (current-buffer))))
  74. (setq folder (vm-imap-folder-for-spec spec)))
  75. (progn
  76. (setq folder (abbreviate-file-name buffer-file-name))
  77. (if (and vm-folder-directory
  78. (string-match (concat "^" (regexp-quote vm-folder-directory))
  79. folder))
  80. (setq folder (replace-match "" t t folder)))))
  81. (setq message-id (org-remove-angle-brackets message-id))
  82. (org-store-link-props :type link-type :from from :to to :subject subject
  83. :message-id message-id :date date)
  84. (setq desc (org-email-link-description))
  85. (setq link (concat (concat link-type ":") folder "#" message-id))
  86. (org-add-link-props :link link :description desc)
  87. link))))
  88. (defun org-vm-open (path)
  89. "Follow a VM message link specified by PATH."
  90. (let (folder article)
  91. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  92. (error "Error in VM link"))
  93. (setq folder (match-string 1 path)
  94. article (match-string 3 path))
  95. ;; The prefix argument will be interpreted as read-only
  96. (org-vm-follow-link folder article current-prefix-arg)))
  97. (defun org-vm-follow-link (&optional folder article readonly)
  98. "Follow a VM link to FOLDER and ARTICLE."
  99. (require 'vm)
  100. (setq article (org-add-angle-brackets article))
  101. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  102. ;; ange-ftp or efs or tramp access
  103. (let ((user (or (match-string 1 folder) (user-login-name)))
  104. (host (match-string 2 folder))
  105. (file (match-string 3 folder)))
  106. (cond
  107. ((featurep 'tramp)
  108. ;; use tramp to access the file
  109. (if (featurep 'xemacs)
  110. (setq folder (format "[%s@%s]%s" user host file))
  111. (setq folder (format "/%s@%s:%s" user host file))))
  112. (t
  113. ;; use ange-ftp or efs
  114. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  115. (setq folder (format "/%s@%s:%s" user host file))))))
  116. (when folder
  117. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  118. (when article
  119. (org-vm-select-message (org-add-angle-brackets article)))))
  120. (defun org-vm-imap-open (path)
  121. "Follow a VM link to an IMAP folder."
  122. (require 'vm-imap)
  123. (when (string-match "\\([^:]+\\):\\([^#]+\\)#?\\(.+\\)?" path)
  124. (let* ((account-name (match-string 1 path))
  125. (mailbox-name (match-string 2 path))
  126. (message-id (match-string 3 path))
  127. (account-spec (vm-imap-parse-spec-to-list
  128. (vm-imap-spec-for-account account-name)))
  129. (mailbox-spec (mapconcat 'identity
  130. (append (butlast account-spec 4)
  131. (cons mailbox-name
  132. (last account-spec 3)))
  133. ":")))
  134. (funcall (cdr (assq 'vm-imap org-link-frame-setup))
  135. mailbox-spec)
  136. (when message-id
  137. (org-vm-select-message (org-add-angle-brackets message-id))))))
  138. (defun org-vm-select-message (message-id)
  139. "Go to the message with message-id in the current folder."
  140. (require 'vm-search)
  141. (sit-for 0.1)
  142. (vm-select-folder-buffer)
  143. (widen)
  144. (let ((case-fold-search t))
  145. (goto-char (point-min))
  146. (if (not (re-search-forward
  147. (concat "^" "message-id: *" (regexp-quote message-id))))
  148. (error "Could not find the specified message in this folder"))
  149. (vm-isearch-update)
  150. (vm-isearch-narrow)
  151. (vm-preview-current-message)
  152. (vm-summarize)))
  153. (provide 'org-vm)
  154. ;;; org-vm.el ends here