org-rmail.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  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: 6.32trans
  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. 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 to the specified PATH."
  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. ;; arch-tag: c6cf4a8b-6639-4b7f-821f-bdf10746b173
  95. ;;; org-rmail.el ends here