Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-rmail.el
blob5cd85bd40091e8c5d0c39d947a1707a802508320
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
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 "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)
49 ;; Implementation
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
55 (save-restriction
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 desc link)
69 (org-store-link-props
70 :type "rmail" :from from :to to :date date
71 :subject subject :message-id message-id)
72 (setq message-id (org-remove-angle-brackets message-id))
73 (setq desc (org-email-link-description))
74 (setq link (concat "rmail:" folder "#" message-id))
75 (org-add-link-props :link link :description desc)
76 (rmail-show-message rmail-current-message)
77 link)))))
79 (defun org-rmail-open (path)
80 "Follow an Rmail message link to the specified PATH."
81 (let (folder article)
82 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
83 (error "Error in Rmail link"))
84 (setq folder (match-string 1 path)
85 article (match-string 3 path))
86 (org-rmail-follow-link folder article)))
88 (defun org-rmail-follow-link (folder article)
89 "Follow an Rmail link to FOLDER and ARTICLE."
90 (require 'rmail)
91 (cond ((null article) (setq article ""))
92 ((stringp article)
93 (setq article (org-add-angle-brackets article)))
94 (t (user-error "Wrong RMAIL link format")))
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 (rmail-widen)
102 (goto-char (point-max))
103 (if (re-search-backward
104 (concat "^Message-ID:\\s-+" (regexp-quote article))
105 nil t)
106 (rmail-what-message))))))
107 (if message-number
108 (progn
109 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
110 (rmail-show-message message-number)
111 message-number)
112 (error "Message not found"))))
114 (provide 'org-rmail)
116 ;;; org-rmail.el ends here