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.00pre-1
  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, or (at your option)
  13. ;; 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; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file implements links to Rmail messages from within Org-mode.
  26. ;; Org-mode loads this module by default - if this is not what you want,
  27. ;; configure the variable `org-modules'.
  28. ;;; Code:
  29. (require 'org)
  30. ;; Declare external functions and variables
  31. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  32. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  33. (declare-function rmail-what-message "rmail" ())
  34. (defvar rmail-current-message)
  35. ;; Install the link type
  36. (org-add-link-type "rmail" 'org-rmail-open)
  37. (add-hook 'org-store-link-functions 'org-rmail-store-link)
  38. ;; Implementation
  39. (defun org-rmail-store-link ()
  40. "Store a link to an Rmail folder or message."
  41. (when (or (eq major-mode 'rmail-mode)
  42. (eq major-mode 'rmail-summary-mode))
  43. (save-window-excursion
  44. (save-restriction
  45. (when (eq major-mode 'rmail-summary-mode)
  46. (rmail-show-message rmail-current-message))
  47. (rmail-narrow-to-non-pruned-header)
  48. (let* ((folder buffer-file-name)
  49. (message-id (mail-fetch-field "message-id"))
  50. (from (mail-fetch-field "from"))
  51. (to (mail-fetch-field "to"))
  52. (subject (mail-fetch-field "subject"))
  53. desc link)
  54. (org-store-link-props
  55. :type "rmail" :from from :to to
  56. :subject subject :message-id message-id)
  57. (setq message-id (org-remove-angle-brackets message-id))
  58. (setq desc (org-email-link-description))
  59. (setq link (org-make-link "rmail:" folder "#" message-id))
  60. (org-add-link-props :link link :description desc)
  61. (rmail-show-message rmail-current-message)
  62. link)))))
  63. (defun org-rmail-open (path)
  64. "Follow an Rmail message link to the specified PATH."
  65. (let (folder article)
  66. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  67. (error "Error in Rmail link"))
  68. (setq folder (match-string 1 path)
  69. article (match-string 3 path))
  70. (org-rmail-follow-link folder article)))
  71. (defun org-rmail-follow-link (folder article)
  72. "Follow an Rmail link to FOLDER and ARTICLE."
  73. (require 'rmail)
  74. (setq article (org-add-angle-brackets article))
  75. (let (message-number)
  76. (save-excursion
  77. (save-window-excursion
  78. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  79. (setq message-number
  80. (save-restriction
  81. (widen)
  82. (goto-char (point-max))
  83. (if (re-search-backward
  84. (concat "^Message-ID:\\s-+" (regexp-quote
  85. (or article "")))
  86. nil t)
  87. (rmail-what-message))))))
  88. (if message-number
  89. (progn
  90. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  91. (rmail-show-message message-number)
  92. message-number)
  93. (error "Message not found"))))
  94. (provide 'org-rmail)
  95. ;;; org-rmail.el ends here