org-rmail.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
  2. ;; Copyright (C) 2004-2013 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 Rmail messages 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 rmail-show-message "rmail" (&optional n no-summary))
  29. (declare-function rmail-what-message "rmail" (&optional pos))
  30. (declare-function rmail-toggle-header "rmail" (&optional arg))
  31. (declare-function rmail-widen "rmail" ())
  32. (defvar rmail-current-message) ; From rmail.el
  33. (defvar rmail-header-style) ; From rmail.el
  34. ;; Install the link type
  35. (org-add-link-type "rmail" 'org-rmail-open)
  36. (add-hook 'org-store-link-functions 'org-rmail-store-link)
  37. ;; Implementation
  38. (defun org-rmail-store-link ()
  39. "Store a link to an Rmail folder or message."
  40. (when (or (eq major-mode 'rmail-mode)
  41. (eq major-mode 'rmail-summary-mode))
  42. (save-window-excursion
  43. (save-restriction
  44. (when (eq major-mode 'rmail-summary-mode)
  45. (rmail-show-message rmail-current-message))
  46. (when (fboundp 'rmail-narrow-to-non-pruned-header)
  47. (rmail-narrow-to-non-pruned-header))
  48. (when (eq rmail-header-style 'normal)
  49. (rmail-toggle-header -1))
  50. (let* ((folder buffer-file-name)
  51. (message-id (mail-fetch-field "message-id"))
  52. (from (mail-fetch-field "from"))
  53. (to (mail-fetch-field "to"))
  54. (subject (mail-fetch-field "subject"))
  55. (date (mail-fetch-field "date"))
  56. (date-ts (and date (format-time-string
  57. (org-time-stamp-format t)
  58. (date-to-time date))))
  59. (date-ts-ia (and date (format-time-string
  60. (org-time-stamp-format t t)
  61. (date-to-time date))))
  62. desc link)
  63. (org-store-link-props
  64. :type "rmail" :from from :to to
  65. :subject subject :message-id message-id)
  66. (when date
  67. (org-add-link-props :date date :date-timestamp date-ts
  68. :date-timestamp-inactive date-ts-ia))
  69. (setq message-id (org-remove-angle-brackets message-id))
  70. (setq desc (org-email-link-description))
  71. (setq link (concat "rmail:" folder "#" message-id))
  72. (org-add-link-props :link link :description desc)
  73. (rmail-show-message rmail-current-message)
  74. link)))))
  75. (defun org-rmail-open (path)
  76. "Follow an Rmail message link to the specified PATH."
  77. (let (folder article)
  78. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  79. (error "Error in Rmail link"))
  80. (setq folder (match-string 1 path)
  81. article (match-string 3 path))
  82. (org-rmail-follow-link folder article)))
  83. (defun org-rmail-follow-link (folder article)
  84. "Follow an Rmail link to FOLDER and ARTICLE."
  85. (require 'rmail)
  86. (setq article (org-add-angle-brackets article))
  87. (let (message-number)
  88. (save-excursion
  89. (save-window-excursion
  90. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  91. (setq message-number
  92. (save-restriction
  93. (rmail-widen)
  94. (goto-char (point-max))
  95. (if (re-search-backward
  96. (concat "^Message-ID:\\s-+" (regexp-quote
  97. (or article "")))
  98. nil t)
  99. (rmail-what-message))))))
  100. (if message-number
  101. (progn
  102. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  103. (rmail-show-message message-number)
  104. message-number)
  105. (error "Message not found"))))
  106. (provide 'org-rmail)
  107. ;;; org-rmail.el ends here