org-rmail.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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: 6.09
  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-narrow-to-non-pruned-header "rmail" ())
  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. (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. desc link)
  52. (org-store-link-props
  53. :type "rmail" :from from :to to
  54. :subject subject :message-id message-id)
  55. (setq message-id (org-remove-angle-brackets message-id))
  56. (setq desc (org-email-link-description))
  57. (setq link (org-make-link "rmail:" folder "#" message-id))
  58. (org-add-link-props :link link :description desc)
  59. (rmail-show-message rmail-current-message)
  60. link)))))
  61. (defun org-rmail-open (path)
  62. "Follow an Rmail message link to the specified PATH."
  63. (let (folder article)
  64. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  65. (error "Error in Rmail link"))
  66. (setq folder (match-string 1 path)
  67. article (match-string 3 path))
  68. (org-rmail-follow-link folder article)))
  69. (defun org-rmail-follow-link (folder article)
  70. "Follow an Rmail link to FOLDER and ARTICLE."
  71. (require 'rmail)
  72. (setq article (org-add-angle-brackets article))
  73. (let (message-number)
  74. (save-excursion
  75. (save-window-excursion
  76. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  77. (setq message-number
  78. (save-restriction
  79. (widen)
  80. (goto-char (point-max))
  81. (if (re-search-backward
  82. (concat "^Message-ID:\\s-+" (regexp-quote
  83. (or article "")))
  84. nil t)
  85. (rmail-what-message))))))
  86. (if message-number
  87. (progn
  88. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  89. (rmail-show-message message-number)
  90. message-number)
  91. (error "Message not found"))))
  92. (provide 'org-rmail)
  93. ;; arch-tag: c6cf4a8b-6639-4b7f-821f-bdf10746b173
  94. ;;; org-rmail.el ends here