org-rmail.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 7.5
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements links to Rmail messages from within Org-mode.
  25. ;; Org-mode loads this module by default - if this is not what you want,
  26. ;; configure the variable `org-modules'.
  27. ;;; Code:
  28. (require 'org)
  29. ;; Declare external functions and variables
  30. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  31. (declare-function rmail-what-message "rmail" ())
  32. (defvar rmail-current-message)
  33. ;; Install the link type
  34. (org-add-link-type "rmail" 'org-rmail-open)
  35. (add-hook 'org-store-link-functions 'org-rmail-store-link)
  36. ;; Implementation
  37. (defun org-rmail-store-link ()
  38. "Store a link to an Rmail folder or message."
  39. (when (or (eq major-mode 'rmail-mode)
  40. (eq major-mode 'rmail-summary-mode))
  41. (save-window-excursion
  42. (save-restriction
  43. (when (eq major-mode 'rmail-summary-mode)
  44. (rmail-show-message rmail-current-message))
  45. (when (fboundp 'rmail-narrow-to-non-pruned-header)
  46. (rmail-narrow-to-non-pruned-header))
  47. (let* ((folder buffer-file-name)
  48. (message-id (mail-fetch-field "message-id"))
  49. (from (mail-fetch-field "from"))
  50. (to (mail-fetch-field "to"))
  51. (subject (mail-fetch-field "subject"))
  52. (date (mail-fetch-field "date"))
  53. (date-ts (and date (format-time-string
  54. (org-time-stamp-format t)
  55. (date-to-time date))))
  56. (date-ts-ia (and date (format-time-string
  57. (org-time-stamp-format t t)
  58. (date-to-time date))))
  59. desc link)
  60. (org-store-link-props
  61. :type "rmail" :from from :to to
  62. :subject subject :message-id message-id)
  63. (when date
  64. (org-add-link-props :date date :date-timestamp date-ts
  65. :date-timestamp-inactive date-ts-ia))
  66. (setq message-id (org-remove-angle-brackets message-id))
  67. (setq desc (org-email-link-description))
  68. (setq link (org-make-link "rmail:" folder "#" message-id))
  69. (org-add-link-props :link link :description desc)
  70. (rmail-show-message rmail-current-message)
  71. link)))))
  72. (defun org-rmail-open (path)
  73. "Follow an Rmail message link to the specified PATH."
  74. (let (folder article)
  75. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  76. (error "Error in Rmail link"))
  77. (setq folder (match-string 1 path)
  78. article (match-string 3 path))
  79. (org-rmail-follow-link folder article)))
  80. (defun org-rmail-follow-link (folder article)
  81. "Follow an Rmail link to FOLDER and ARTICLE."
  82. (require 'rmail)
  83. (setq article (org-add-angle-brackets article))
  84. (let (message-number)
  85. (save-excursion
  86. (save-window-excursion
  87. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  88. (setq message-number
  89. (save-restriction
  90. (widen)
  91. (goto-char (point-max))
  92. (if (re-search-backward
  93. (concat "^Message-ID:\\s-+" (regexp-quote
  94. (or article "")))
  95. nil t)
  96. (rmail-what-message))))))
  97. (if message-number
  98. (progn
  99. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  100. (rmail-show-message message-number)
  101. message-number)
  102. (error "Message not found"))))
  103. (provide 'org-rmail)
  104. ;; arch-tag: c6cf4a8b-6639-4b7f-821f-bdf10746b173
  105. ;;; org-rmail.el ends here