1 ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
3 ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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'.
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
"rmail" (&optional file-name-arg
))
40 (declare-function rmail-widen
"rmail" ())
41 (defvar rmail-current-message
) ; From rmail.el
42 (defvar rmail-header-style
) ; From rmail.el
43 (defvar rmail-file-name
) ; From rmail.el
45 ;; Install the link type
46 (org-add-link-type "rmail" 'org-rmail-open
)
47 (add-hook 'org-store-link-functions
'org-rmail-store-link
)
50 (defun org-rmail-store-link ()
51 "Store a link to an Rmail folder or message."
52 (when (or (eq major-mode
'rmail-mode
)
53 (eq major-mode
'rmail-summary-mode
))
54 (save-window-excursion
56 (when (eq major-mode
'rmail-summary-mode
)
57 (rmail-show-message rmail-current-message
))
58 (when (fboundp 'rmail-narrow-to-non-pruned-header
)
59 (rmail-narrow-to-non-pruned-header))
60 (when (eq rmail-header-style
'normal
)
61 (rmail-toggle-header -
1))
62 (let* ((folder buffer-file-name
)
63 (message-id (mail-fetch-field "message-id"))
64 (from (mail-fetch-field "from"))
65 (to (mail-fetch-field "to"))
66 (subject (mail-fetch-field "subject"))
67 (date (mail-fetch-field "date"))
68 (date-ts (and date
(format-time-string
69 (org-time-stamp-format t
)
70 (date-to-time date
))))
71 (date-ts-ia (and date
(format-time-string
72 (org-time-stamp-format t t
)
73 (date-to-time date
))))
76 :type
"rmail" :from from
:to to
77 :subject subject
:message-id message-id
)
79 (org-add-link-props :date date
:date-timestamp date-ts
80 :date-timestamp-inactive date-ts-ia
))
81 (setq message-id
(org-remove-angle-brackets message-id
))
82 (setq desc
(org-email-link-description))
83 (setq link
(concat "rmail:" folder
"#" message-id
))
84 (org-add-link-props :link link
:description desc
)
85 (rmail-show-message rmail-current-message
)
88 (defun org-rmail-open (path)
89 "Follow an Rmail message link to the specified PATH."
91 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
92 (error "Error in Rmail link"))
93 (setq folder
(match-string 1 path
)
94 article
(match-string 3 path
))
95 (org-rmail-follow-link folder article
)))
97 (defun org-rmail-follow-link (folder article
)
98 "Follow an Rmail link to FOLDER and ARTICLE."
100 (cond ((null article
) (setq article
""))
102 (setq article
(org-add-angle-brackets article
)))
103 (t (user-error "Wrong RMAIL link format")))
104 (let (message-number)
106 (save-window-excursion
107 (rmail (if (string= folder
"RMAIL") rmail-file-name folder
))
111 (goto-char (point-max))
112 (if (re-search-backward
113 (concat "^Message-ID:\\s-+" (regexp-quote article
))
115 (rmail-what-message))))))
118 (rmail (if (string= folder
"RMAIL") rmail-file-name folder
))
119 (rmail-show-message message-number
)
121 (error "Message not found"))))
125 ;;; org-rmail.el ends here