org-vm.el 5.3 KB

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