* lisp/thingatpt.el (forward-whitespace): Typo.
[emacs.git] / lisp / org / org-rmail.el
blob6e984fda6878035bd20e0905d34581b12e2a7df8
1 ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
3 ;; Copyright (C) 2004-2011 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: 7.4
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 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file implements links to Rmail messages from within Org-mode.
29 ;; Org-mode loads this module by default - if this is not what you want,
30 ;; configure the variable `org-modules'.
32 ;;; Code:
34 (require 'org)
36 ;; Declare external functions and variables
37 (declare-function rmail-show-message "rmail" (&optional n no-summary))
38 (declare-function rmail-what-message "rmail" ())
39 (defvar rmail-current-message)
41 ;; Install the link type
42 (org-add-link-type "rmail" 'org-rmail-open)
43 (add-hook 'org-store-link-functions 'org-rmail-store-link)
45 ;; Implementation
46 (defun org-rmail-store-link ()
47 "Store a link to an Rmail folder or message."
48 (when (or (eq major-mode 'rmail-mode)
49 (eq major-mode 'rmail-summary-mode))
50 (save-window-excursion
51 (save-restriction
52 (when (eq major-mode 'rmail-summary-mode)
53 (rmail-show-message rmail-current-message))
54 (when (fboundp 'rmail-narrow-to-non-pruned-header)
55 (rmail-narrow-to-non-pruned-header))
56 (let* ((folder buffer-file-name)
57 (message-id (mail-fetch-field "message-id"))
58 (from (mail-fetch-field "from"))
59 (to (mail-fetch-field "to"))
60 (subject (mail-fetch-field "subject"))
61 (date (mail-fetch-field "date"))
62 (date-ts (and date (format-time-string
63 (org-time-stamp-format t)
64 (date-to-time date))))
65 (date-ts-ia (and date (format-time-string
66 (org-time-stamp-format t t)
67 (date-to-time date))))
68 desc link)
69 (org-store-link-props
70 :type "rmail" :from from :to to
71 :subject subject :message-id message-id)
72 (when date
73 (org-add-link-props :date date :date-timestamp date-ts
74 :date-timestamp-inactive date-ts-ia))
75 (setq message-id (org-remove-angle-brackets message-id))
76 (setq desc (org-email-link-description))
77 (setq link (org-make-link "rmail:" folder "#" message-id))
78 (org-add-link-props :link link :description desc)
79 (rmail-show-message rmail-current-message)
80 link)))))
82 (defun org-rmail-open (path)
83 "Follow an Rmail message link to the specified PATH."
84 (let (folder article)
85 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
86 (error "Error in Rmail link"))
87 (setq folder (match-string 1 path)
88 article (match-string 3 path))
89 (org-rmail-follow-link folder article)))
91 (defun org-rmail-follow-link (folder article)
92 "Follow an Rmail link to FOLDER and ARTICLE."
93 (require 'rmail)
94 (setq article (org-add-angle-brackets article))
95 (let (message-number)
96 (save-excursion
97 (save-window-excursion
98 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
99 (setq message-number
100 (save-restriction
101 (widen)
102 (goto-char (point-max))
103 (if (re-search-backward
104 (concat "^Message-ID:\\s-+" (regexp-quote
105 (or article "")))
106 nil t)
107 (rmail-what-message))))))
108 (if message-number
109 (progn
110 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
111 (rmail-show-message message-number)
112 message-number)
113 (error "Message not found"))))
115 (provide 'org-rmail)
118 ;;; org-rmail.el ends here