org-rmail.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ;;; org-rmail.el - Support for links to RMAIL messages in 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: 1.0
  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 for Org-mode.
  26. ;; Org-mode loads this module by default - if this is not what you want,
  27. ;; configure the variable `org-modules'.
  28. (require 'org)
  29. ;; Declare external functions and variables
  30. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  31. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  32. (declare-function rmail-what-message "rmail" ())
  33. (defvar rmail-current-message)
  34. ;; Install the link type
  35. (org-add-link-type "rmail" 'org-rmail-open)
  36. (add-hook 'org-store-link-functions 'org-rmail-store-link)
  37. ;; Implementation
  38. (defun org-rmail-store-link ()
  39. "Store a link to an RMAIL folder or message."
  40. (when (or (eq major-mode 'rmail-mode)
  41. (eq major-mode 'rmail-summary-mode))
  42. (save-window-excursion
  43. (save-restriction
  44. (when (eq major-mode 'rmail-summary-mode)
  45. (rmail-show-message rmail-current-message))
  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. desc link)
  53. (org-store-link-props
  54. :type "rmail" :from from :to to
  55. :subject subject :message-id message-id)
  56. (setq message-id (org-remove-angle-brackets message-id))
  57. (setq desc (org-email-link-description))
  58. (setq link (org-make-link "rmail:" folder "#" message-id))
  59. (org-add-link-props :link link :description desc)
  60. (rmail-show-message rmail-current-message)
  61. link)))))
  62. (defun org-rmail-open (path)
  63. "Follow an RMAIL message link."
  64. (let (folder article)
  65. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  66. (error "Error in RMAIL link"))
  67. (setq folder (match-string 1 path)
  68. article (match-string 3 path))
  69. (org-rmail-follow-link folder article)))
  70. (defun org-rmail-follow-link (folder article)
  71. "Follow an RMAIL link to FOLDER and ARTICLE."
  72. (require 'rmail)
  73. (setq article (org-add-angle-brackets article))
  74. (let (message-number)
  75. (save-excursion
  76. (save-window-excursion
  77. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  78. (setq message-number
  79. (save-restriction
  80. (widen)
  81. (goto-char (point-max))
  82. (if (re-search-backward
  83. (concat "^Message-ID:\\s-+" (regexp-quote
  84. (or article "")))
  85. nil t)
  86. (rmail-what-message))))))
  87. (if message-number
  88. (progn
  89. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  90. (rmail-show-message message-number)
  91. message-number)
  92. (error "Message not found"))))
  93. (provide 'org-rmail)
  94. ;;; org-rmail.el ends here