org-rmail.el 4.0 KB

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