org-agenda: Fix a docstring
[org-mode.git] / lisp / org-rmail.el
blobd737aff4ba93ffa860c086828755ecbcbea06640
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 (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))))
74 desc link)
75 (org-store-link-props
76 :type "rmail" :from from :to to
77 :subject subject :message-id message-id)
78 (when date
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)
86 link)))))
88 (defun org-rmail-open (path)
89 "Follow an Rmail message link to the specified PATH."
90 (let (folder article)
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."
99 (require 'rmail)
100 (cond ((null article) (setq article ""))
101 ((stringp article)
102 (setq article (org-add-angle-brackets article)))
103 (t (user-error "Wrong RMAIL link format")))
104 (let (message-number)
105 (save-excursion
106 (save-window-excursion
107 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
108 (setq message-number
109 (save-restriction
110 (rmail-widen)
111 (goto-char (point-max))
112 (if (re-search-backward
113 (concat "^Message-ID:\\s-+" (regexp-quote article))
114 nil t)
115 (rmail-what-message))))))
116 (if message-number
117 (progn
118 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
119 (rmail-show-message message-number)
120 message-number)
121 (error "Message not found"))))
123 (provide 'org-rmail)
125 ;;; org-rmail.el ends here