Merge branch 'refactor/org-macros'
[org-mode.git] / lisp / org-rmail.el
blobcbb63365985b7a0f6063bd777f8a9bc177f72570
1 ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.7
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file implements links to Rmail messages from within Org-mode.
30 ;; Org-mode loads this module by default - if this is not what you want,
31 ;; configure the variable `org-modules'.
33 ;;; Code:
35 (require 'org)
37 ;; Declare external functions and variables
38 (declare-function rmail-show-message "rmail" (&optional n no-summary))
39 (declare-function rmail-what-message "rmail" ())
40 (defvar rmail-current-message)
42 ;; Install the link type
43 (org-add-link-type "rmail" 'org-rmail-open)
44 (add-hook 'org-store-link-functions 'org-rmail-store-link)
46 ;; Implementation
47 (defun org-rmail-store-link ()
48 "Store a link to an Rmail folder or message."
49 (when (or (eq major-mode 'rmail-mode)
50 (eq major-mode 'rmail-summary-mode))
51 (save-window-excursion
52 (save-restriction
53 (when (eq major-mode 'rmail-summary-mode)
54 (rmail-show-message rmail-current-message))
55 (when (fboundp 'rmail-narrow-to-non-pruned-header)
56 (rmail-narrow-to-non-pruned-header))
57 (let* ((folder buffer-file-name)
58 (message-id (mail-fetch-field "message-id"))
59 (from (mail-fetch-field "from"))
60 (to (mail-fetch-field "to"))
61 (subject (mail-fetch-field "subject"))
62 (date (mail-fetch-field "date"))
63 (date-ts (and date (format-time-string
64 (org-time-stamp-format t)
65 (date-to-time date))))
66 (date-ts-ia (and date (format-time-string
67 (org-time-stamp-format t t)
68 (date-to-time date))))
69 desc link)
70 (org-store-link-props
71 :type "rmail" :from from :to to
72 :subject subject :message-id message-id)
73 (when date
74 (org-add-link-props :date date :date-timestamp date-ts
75 :date-timestamp-inactive date-ts-ia))
76 (setq message-id (org-remove-angle-brackets message-id))
77 (setq desc (org-email-link-description))
78 (setq link (org-make-link "rmail:" folder "#" message-id))
79 (org-add-link-props :link link :description desc)
80 (rmail-show-message rmail-current-message)
81 link)))))
83 (defun org-rmail-open (path)
84 "Follow an Rmail message link to the specified PATH."
85 (let (folder article)
86 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
87 (error "Error in Rmail link"))
88 (setq folder (match-string 1 path)
89 article (match-string 3 path))
90 (org-rmail-follow-link folder article)))
92 (defun org-rmail-follow-link (folder article)
93 "Follow an Rmail link to FOLDER and ARTICLE."
94 (require 'rmail)
95 (setq article (org-add-angle-brackets article))
96 (let (message-number)
97 (save-excursion
98 (save-window-excursion
99 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
100 (setq message-number
101 (save-restriction
102 (widen)
103 (goto-char (point-max))
104 (if (re-search-backward
105 (concat "^Message-ID:\\s-+" (regexp-quote
106 (or article "")))
107 nil t)
108 (rmail-what-message))))))
109 (if message-number
110 (progn
111 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
112 (rmail-show-message message-number)
113 message-number)
114 (error "Message not found"))))
116 (provide 'org-rmail)
118 ;; arch-tag: c6cf4a8b-6639-4b7f-821f-bdf10746b173
120 ;;; org-rmail.el ends here