New version number.
[org-mode.git] / lisp / org-rmail.el
blob7f7b2a39781894a7be1f0b1523c114e934b3e1cc
1 ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.02pre-04
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file implements links to Rmail messages from within Org-mode.
31 ;; Org-mode loads this module by default - if this is not what you want,
32 ;; configure the variable `org-modules'.
34 ;;; Code:
36 (require 'org)
38 ;; Declare external functions and variables
39 (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
40 (declare-function rmail-show-message "rmail" (&optional n no-summary))
41 (declare-function rmail-what-message "rmail" ())
42 (defvar rmail-current-message)
44 ;; Install the link type
45 (org-add-link-type "rmail" 'org-rmail-open)
46 (add-hook 'org-store-link-functions 'org-rmail-store-link)
48 ;; Implementation
49 (defun org-rmail-store-link ()
50 "Store a link to an Rmail folder or message."
51 (when (or (eq major-mode 'rmail-mode)
52 (eq major-mode 'rmail-summary-mode))
53 (save-window-excursion
54 (save-restriction
55 (when (eq major-mode 'rmail-summary-mode)
56 (rmail-show-message rmail-current-message))
57 (rmail-narrow-to-non-pruned-header)
58 (let* ((folder buffer-file-name)
59 (message-id (mail-fetch-field "message-id"))
60 (from (mail-fetch-field "from"))
61 (to (mail-fetch-field "to"))
62 (subject (mail-fetch-field "subject"))
63 desc link)
64 (org-store-link-props
65 :type "rmail" :from from :to to
66 :subject subject :message-id message-id)
67 (setq message-id (org-remove-angle-brackets message-id))
68 (setq desc (org-email-link-description))
69 (setq link (org-make-link "rmail:" folder "#" message-id))
70 (org-add-link-props :link link :description desc)
71 (rmail-show-message rmail-current-message)
72 link)))))
74 (defun org-rmail-open (path)
75 "Follow an Rmail message link to the specified PATH."
76 (let (folder article)
77 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
78 (error "Error in Rmail link"))
79 (setq folder (match-string 1 path)
80 article (match-string 3 path))
81 (org-rmail-follow-link folder article)))
83 (defun org-rmail-follow-link (folder article)
84 "Follow an Rmail link to FOLDER and ARTICLE."
85 (require 'rmail)
86 (setq article (org-add-angle-brackets article))
87 (let (message-number)
88 (save-excursion
89 (save-window-excursion
90 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
91 (setq message-number
92 (save-restriction
93 (widen)
94 (goto-char (point-max))
95 (if (re-search-backward
96 (concat "^Message-ID:\\s-+" (regexp-quote
97 (or article "")))
98 nil t)
99 (rmail-what-message))))))
100 (if message-number
101 (progn
102 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
103 (rmail-show-message message-number)
104 message-number)
105 (error "Message not found"))))
107 (provide 'org-rmail)
109 ;;; org-rmail.el ends here