Merge branch 'maint'
[org-mode.git] / lisp / org-mew.el
blob9cc767eaec490bf1777ac979c9a8b289ac95c447
1 ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements links to Mew 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 (defgroup org-mew nil
36 "Options concerning the Mew link."
37 :tag "Org Startup"
38 :group 'org-link)
40 (defcustom org-mew-link-to-refile-destination t
41 "Create a link to the refile destination if the message is marked as refile."
42 :group 'org-mew
43 :type 'boolean)
45 ;; Declare external functions and variables
46 (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
47 (declare-function mew-case-folder "ext:mew-func" (case folder))
48 (declare-function mew-header-get-value "ext:mew-header"
49 (field &optional as-list))
50 (declare-function mew-init "ext:mew" ())
51 (declare-function mew-refile-get "ext:mew-refile" (msg))
52 (declare-function mew-sinfo-get-case "ext:mew-summary" ())
53 (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
54 (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
55 (declare-function mew-summary-get-mark "ext:mew-mark" ())
56 (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
57 (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
58 (pattern folder src-msgs))
59 (declare-function mew-summary-search-msg "ext:mew-const" (msg))
60 (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
61 (declare-function mew-summary-visit-folder "ext:mew-summary4"
62 (folder &optional goend no-ls))
63 (declare-function mew-window-push "ext:mew" ())
64 (defvar mew-init-p)
65 (defvar mew-summary-goto-line-then-display)
67 ;; Install the link type
68 (org-add-link-type "mew" 'org-mew-open)
69 (add-hook 'org-store-link-functions 'org-mew-store-link)
71 ;; Implementation
72 (defun org-mew-store-link ()
73 "Store a link to a Mew folder or message."
74 (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
75 (let* ((msgnum (mew-summary-message-number2))
76 (mark-info (mew-summary-get-mark))
77 (folder-name
78 (if (and org-mew-link-to-refile-destination
79 (eq mark-info ?o)) ; marked as refile
80 (mew-case-folder (mew-sinfo-get-case)
81 (nth 1 (mew-refile-get msgnum)))
82 (mew-summary-folder-name)))
83 message-id from to subject desc link date date-ts date-ts-ia)
84 (save-window-excursion
85 (if (fboundp 'mew-summary-set-message-buffer)
86 (mew-summary-set-message-buffer folder-name msgnum)
87 (set-buffer (mew-cache-hit folder-name msgnum t)))
88 (setq message-id (mew-header-get-value "Message-Id:"))
89 (setq from (mew-header-get-value "From:"))
90 (setq to (mew-header-get-value "To:"))
91 (setq date (mew-header-get-value "Date:"))
92 (setq date-ts (and date (format-time-string
93 (org-time-stamp-format t)
94 (date-to-time date))))
95 (setq date-ts-ia (and date (format-time-string
96 (org-time-stamp-format t t)
97 (date-to-time date))))
98 (setq subject (mew-header-get-value "Subject:")))
99 (org-store-link-props :type "mew" :from from :to to
100 :subject subject :message-id message-id)
101 (when date
102 (org-add-link-props :date date :date-timestamp date-ts
103 :date-timestamp-inactive date-ts-ia))
104 (setq message-id (org-remove-angle-brackets message-id))
105 (setq desc (org-email-link-description))
106 (setq link (org-make-link "mew:" folder-name
107 "#" message-id))
108 (org-add-link-props :link link :description desc)
109 link)))
111 (defun org-mew-open (path)
112 "Follow the Mew message link specified by PATH."
113 (let (folder msgnum)
114 (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
115 (setq folder (match-string 1 path))
116 (setq msgnum (match-string 2 path)))
117 ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
118 (setq folder (match-string 1 path))
119 (setq msgnum (match-string 4 path)))
120 (t (error "Error in Mew link")))
121 (require 'mew)
122 (mew-window-push)
123 (unless mew-init-p (mew-init))
124 (mew-summary-visit-folder folder)
125 (when msgnum
126 (if (not (string-match "\\`[0-9]+\\'" msgnum))
127 (let* ((pattern (concat "message-id=" msgnum))
128 (msgs (mew-summary-pick-with-mewl pattern folder nil)))
129 (setq msgnum (car msgs))))
130 (if (mew-summary-search-msg msgnum)
131 (if mew-summary-goto-line-then-display
132 (mew-summary-display))
133 (error "Message not found")))))
135 (provide 'org-mew)
137 ;;; org-mew.el ends here