org-rmail.el 4.1 KB

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