org-vm.el 5.4 KB

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