Merge branch 'maint'
[org-mode.git] / lisp / org-rmail.el
blobe2f9fbeef606ce0e1f04e51e6cc895b6ebdee6b5
1 ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
3 ;; Copyright (C) 2004-2013 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 ;;
9 ;; This file is part of GNU Emacs.
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements links to Rmail messages from within Org-mode.
28 ;; Org-mode loads this module by default - if this is not what you want,
29 ;; configure the variable `org-modules'.
31 ;;; Code:
33 (require 'org)
35 ;; Declare external functions and variables
36 (declare-function rmail-show-message "rmail" (&optional n no-summary))
37 (declare-function rmail-what-message "rmail" (&optional pos))
38 (declare-function rmail-toggle-header "rmail" (&optional arg))
39 (declare-function rmail-widen "rmail" ())
40 (defvar rmail-current-message) ; From rmail.el
41 (defvar rmail-header-style) ; From rmail.el
43 ;; Install the link type
44 (org-add-link-type "rmail" 'org-rmail-open)
45 (add-hook 'org-store-link-functions 'org-rmail-store-link)
47 ;; Implementation
48 (defun org-rmail-store-link ()
49 "Store a link to an Rmail folder or message."
50 (when (or (eq major-mode 'rmail-mode)
51 (eq major-mode 'rmail-summary-mode))
52 (save-window-excursion
53 (save-restriction
54 (when (eq major-mode 'rmail-summary-mode)
55 (rmail-show-message rmail-current-message))
56 (when (fboundp 'rmail-narrow-to-non-pruned-header)
57 (rmail-narrow-to-non-pruned-header))
58 (when (eq rmail-header-style 'normal)
59 (rmail-toggle-header -1))
60 (let* ((folder buffer-file-name)
61 (message-id (mail-fetch-field "message-id"))
62 (from (mail-fetch-field "from"))
63 (to (mail-fetch-field "to"))
64 (subject (mail-fetch-field "subject"))
65 (date (mail-fetch-field "date"))
66 (date-ts (and date (format-time-string
67 (org-time-stamp-format t)
68 (date-to-time date))))
69 (date-ts-ia (and date (format-time-string
70 (org-time-stamp-format t t)
71 (date-to-time date))))
72 desc link)
73 (org-store-link-props
74 :type "rmail" :from from :to to
75 :subject subject :message-id message-id)
76 (when date
77 (org-add-link-props :date date :date-timestamp date-ts
78 :date-timestamp-inactive date-ts-ia))
79 (setq message-id (org-remove-angle-brackets message-id))
80 (setq desc (org-email-link-description))
81 (setq link (concat "rmail:" folder "#" message-id))
82 (org-add-link-props :link link :description desc)
83 (rmail-show-message rmail-current-message)
84 link)))))
86 (defun org-rmail-open (path)
87 "Follow an Rmail message link to the specified PATH."
88 (let (folder article)
89 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
90 (error "Error in Rmail link"))
91 (setq folder (match-string 1 path)
92 article (match-string 3 path))
93 (org-rmail-follow-link folder article)))
95 (defun org-rmail-follow-link (folder article)
96 "Follow an Rmail link to FOLDER and ARTICLE."
97 (require 'rmail)
98 (setq article (org-add-angle-brackets article))
99 (let (message-number)
100 (save-excursion
101 (save-window-excursion
102 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
103 (setq message-number
104 (save-restriction
105 (rmail-widen)
106 (goto-char (point-max))
107 (if (re-search-backward
108 (concat "^Message-ID:\\s-+" (regexp-quote
109 (or article "")))
110 nil t)
111 (rmail-what-message))))))
112 (if message-number
113 (progn
114 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
115 (rmail-show-message message-number)
116 message-number)
117 (error "Message not found"))))
119 (provide 'org-rmail)
121 ;;; org-rmail.el ends here