ol-vm.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; ol-vm.el --- Links to VM messages
  2. ;; Copyright (C) 2004-2021 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 'ol)
  31. (require 'org)
  32. ;; Declare external functions and variables
  33. (declare-function vm-preview-current-message "ext:vm-page" ())
  34. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  35. (declare-function vm-get-header-contents "ext:vm-summary"
  36. (message header-name-regexp &optional clump-sep))
  37. (declare-function vm-isearch-narrow "ext:vm-search" ())
  38. (declare-function vm-isearch-update "ext:vm-search" ())
  39. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  40. (declare-function vm-su-message-id "ext:vm-summary" (m))
  41. (declare-function vm-su-subject "ext:vm-summary" (m))
  42. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  43. (declare-function vm-imap-folder-p "ext:vm-save" ())
  44. (declare-function vm-imap-find-spec-for-buffer "ext:vm-imap" (buffer))
  45. (declare-function vm-imap-folder-for-spec "ext:vm-imap" (spec))
  46. (declare-function vm-imap-parse-spec-to-list "ext:vm-imap" (spec))
  47. (declare-function vm-imap-spec-for-account "ext:vm-imap" (account))
  48. (defvar vm-message-pointer)
  49. (defvar vm-folder-directory)
  50. ;; Install the link type
  51. (org-link-set-parameters "vm" :follow #'org-vm-open :store #'org-vm-store-link)
  52. (org-link-set-parameters "vm-imap" :follow #'org-vm-imap-open)
  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-unbracket-string "<" ">" 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-link-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. (setq folder (format "/%s@%s:%s" user host file)))
  110. (t
  111. ;; use ange-ftp or efs
  112. (require 'ange-ftp)
  113. (setq folder (format "/%s@%s:%s" user host file))))))
  114. (when folder
  115. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  116. (when article
  117. (org-vm-select-message (org-link-add-angle-brackets article)))))
  118. (defun org-vm-imap-open (path _)
  119. "Follow a VM link to an IMAP folder."
  120. (require 'vm-imap)
  121. (when (string-match "\\([^:]+\\):\\([^#]+\\)#?\\(.+\\)?" path)
  122. (let* ((account-name (match-string 1 path))
  123. (mailbox-name (match-string 2 path))
  124. (message-id (match-string 3 path))
  125. (account-spec (vm-imap-parse-spec-to-list
  126. (vm-imap-spec-for-account account-name)))
  127. (mailbox-spec (mapconcat 'identity
  128. (append (butlast account-spec 4)
  129. (cons mailbox-name
  130. (last account-spec 3)))
  131. ":")))
  132. (funcall (cdr (assq 'vm-imap org-link-frame-setup))
  133. mailbox-spec)
  134. (when message-id
  135. (org-vm-select-message (org-link-add-angle-brackets message-id))))))
  136. (defun org-vm-select-message (message-id)
  137. "Go to the message with message-id in the current folder."
  138. (require 'vm-search)
  139. (sit-for 0.1)
  140. (vm-select-folder-buffer)
  141. (widen)
  142. (let ((case-fold-search t))
  143. (goto-char (point-min))
  144. (if (not (re-search-forward
  145. (concat "^" "message-id: *" (regexp-quote message-id))))
  146. (error "Could not find the specified message in this folder"))
  147. (vm-isearch-update)
  148. (vm-isearch-narrow)
  149. (vm-preview-current-message)
  150. (vm-summarize)))
  151. (provide 'ol-vm)
  152. ;;; ol-vm.el ends here