org-vm.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ;;; org-vm.el --- Support for links to VM messages from within Org-mode
  2. ;; Copyright (C) 2004-2018 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://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-link-set-parameters "vm" :follow #'org-vm-open :store #'org-vm-store-link)
  51. (org-link-set-parameters "vm-imap" :follow #'org-vm-imap-open)
  52. ;; Implementation
  53. (defun org-vm-store-link ()
  54. "Store a link to a VM folder or message."
  55. (when (and (or (eq major-mode 'vm-summary-mode)
  56. (eq major-mode 'vm-presentation-mode))
  57. (save-window-excursion
  58. (vm-select-folder-buffer) buffer-file-name))
  59. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  60. (vm-follow-summary-cursor)
  61. (save-excursion
  62. (vm-select-folder-buffer)
  63. (let* ((message (car vm-message-pointer))
  64. (subject (vm-su-subject message))
  65. (to (vm-get-header-contents message "To"))
  66. (from (vm-get-header-contents message "From"))
  67. (message-id (vm-su-message-id message))
  68. (link-type (if (vm-imap-folder-p) "vm-imap" "vm"))
  69. (date (vm-get-header-contents message "Date"))
  70. folder desc link)
  71. (if (vm-imap-folder-p)
  72. (let ((spec (vm-imap-find-spec-for-buffer (current-buffer))))
  73. (setq folder (vm-imap-folder-for-spec spec)))
  74. (progn
  75. (setq folder (abbreviate-file-name buffer-file-name))
  76. (if (and vm-folder-directory
  77. (string-match (concat "^" (regexp-quote vm-folder-directory))
  78. folder))
  79. (setq folder (replace-match "" t t folder)))))
  80. (setq message-id (org-unbracket-string "<" ">" message-id))
  81. (org-store-link-props :type link-type :from from :to to :subject subject
  82. :message-id message-id :date date)
  83. (setq desc (org-email-link-description))
  84. (setq link (concat (concat link-type ":") folder "#" message-id))
  85. (org-add-link-props :link link :description desc)
  86. link))))
  87. (defun org-vm-open (path)
  88. "Follow a VM message link specified by PATH."
  89. (let (folder article)
  90. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  91. (error "Error in VM link"))
  92. (setq folder (match-string 1 path)
  93. article (match-string 3 path))
  94. ;; The prefix argument will be interpreted as read-only
  95. (org-vm-follow-link folder article current-prefix-arg)))
  96. (defun org-vm-follow-link (&optional folder article readonly)
  97. "Follow a VM link to FOLDER and ARTICLE."
  98. (require 'vm)
  99. (setq article (org-add-angle-brackets article))
  100. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  101. ;; ange-ftp or efs or tramp access
  102. (let ((user (or (match-string 1 folder) (user-login-name)))
  103. (host (match-string 2 folder))
  104. (file (match-string 3 folder)))
  105. (cond
  106. ((featurep 'tramp)
  107. ;; use tramp to access the file
  108. (setq folder (format "/%s@%s:%s" user host file)))
  109. (t
  110. ;; use ange-ftp or efs
  111. (require '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. (when article
  116. (org-vm-select-message (org-add-angle-brackets article)))))
  117. (defun org-vm-imap-open (path)
  118. "Follow a VM link to an IMAP folder."
  119. (require 'vm-imap)
  120. (when (string-match "\\([^:]+\\):\\([^#]+\\)#?\\(.+\\)?" path)
  121. (let* ((account-name (match-string 1 path))
  122. (mailbox-name (match-string 2 path))
  123. (message-id (match-string 3 path))
  124. (account-spec (vm-imap-parse-spec-to-list
  125. (vm-imap-spec-for-account account-name)))
  126. (mailbox-spec (mapconcat 'identity
  127. (append (butlast account-spec 4)
  128. (cons mailbox-name
  129. (last account-spec 3)))
  130. ":")))
  131. (funcall (cdr (assq 'vm-imap org-link-frame-setup))
  132. mailbox-spec)
  133. (when message-id
  134. (org-vm-select-message (org-add-angle-brackets message-id))))))
  135. (defun org-vm-select-message (message-id)
  136. "Go to the message with message-id in the current folder."
  137. (require 'vm-search)
  138. (sit-for 0.1)
  139. (vm-select-folder-buffer)
  140. (widen)
  141. (let ((case-fold-search t))
  142. (goto-char (point-min))
  143. (if (not (re-search-forward
  144. (concat "^" "message-id: *" (regexp-quote message-id))))
  145. (error "Could not find the specified message in this folder"))
  146. (vm-isearch-update)
  147. (vm-isearch-narrow)
  148. (vm-preview-current-message)
  149. (vm-summarize)))
  150. (provide 'org-vm)
  151. ;;; org-vm.el ends here