Commit | Line | Data |
---|---|---|
20908596 CD |
1 | ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2004-2014 Free Software Foundation, Inc. |
20908596 CD |
4 | |
5 | ;; Author: Carsten Dominik <carsten at orgmode dot org> | |
6 | ;; Keywords: outlines, hypermedia, calendar, wp | |
7 | ;; Homepage: http://orgmode.org | |
20908596 CD |
8 | ;; |
9 | ;; This file is part of GNU Emacs. | |
10 | ;; | |
b1fc2b50 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
20908596 | 12 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
20908596 CD |
15 | |
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
20908596 CD |
23 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
24 | ;; | |
25 | ;;; Commentary: | |
26 | ||
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'. | |
30 | ||
31 | ;;; Code: | |
32 | ||
33 | (require 'org) | |
34 | ||
35 | ;; Declare external functions and variables | |
8223b1d2 BG |
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 | |
20908596 CD |
42 | |
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) | |
46 | ||
47 | ;; Implementation | |
48 | (defun org-rmail-store-link () | |
49 | "Store a link to an Rmail folder or message." | |
8d642074 CD |
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)) | |
8223b1d2 BG |
58 | (when (eq rmail-header-style 'normal) |
59 | (rmail-toggle-header -1)) | |
8d642074 CD |
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")) | |
afe98dfa CD |
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)))) | |
8d642074 CD |
72 | desc link) |
73 | (org-store-link-props | |
74 | :type "rmail" :from from :to to | |
75 | :subject subject :message-id message-id) | |
afe98dfa CD |
76 | (when date |
77 | (org-add-link-props :date date :date-timestamp date-ts | |
78 | :date-timestamp-inactive date-ts-ia)) | |
8d642074 CD |
79 | (setq message-id (org-remove-angle-brackets message-id)) |
80 | (setq desc (org-email-link-description)) | |
8223b1d2 | 81 | (setq link (concat "rmail:" folder "#" message-id)) |
8d642074 CD |
82 | (org-add-link-props :link link :description desc) |
83 | (rmail-show-message rmail-current-message) | |
84 | link))))) | |
20908596 CD |
85 | |
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))) | |
94 | ||
95 | (defun org-rmail-follow-link (folder article) | |
96 | "Follow an Rmail link to FOLDER and ARTICLE." | |
97 | (require 'rmail) | |
3c8b09ca BG |
98 | (cond ((null article) (setq article "")) |
99 | ((stringp article) | |
100 | (setq article (org-add-angle-brackets article))) | |
101 | (t (user-error "Wrong RMAIL link format"))) | |
8d642074 | 102 | (let (message-number) |
20908596 CD |
103 | (save-excursion |
104 | (save-window-excursion | |
105 | (rmail (if (string= folder "RMAIL") rmail-file-name folder)) | |
8d642074 CD |
106 | (setq message-number |
107 | (save-restriction | |
8223b1d2 | 108 | (rmail-widen) |
8d642074 CD |
109 | (goto-char (point-max)) |
110 | (if (re-search-backward | |
3c8b09ca | 111 | (concat "^Message-ID:\\s-+" (regexp-quote article)) |
8d642074 CD |
112 | nil t) |
113 | (rmail-what-message)))))) | |
20908596 CD |
114 | (if message-number |
115 | (progn | |
116 | (rmail (if (string= folder "RMAIL") rmail-file-name folder)) | |
117 | (rmail-show-message message-number) | |
118 | message-number) | |
119 | (error "Message not found")))) | |
120 | ||
121 | (provide 'org-rmail) | |
122 | ||
123 | ;;; org-rmail.el ends here |