ox-html.el (org-html-headline): Add comment
[org-mode.git] / lisp / org-mew.el
blobab1b279d40409d39058f015670016d49b6a253ed
1 ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
3 ;; Copyright (C) 2008-2013 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 ;; Here is an example of workflow:
33 ;; In your ~/.mew.el configuration file:
35 ;; (define-key mew-summary-mode-map "'" 'org-mew-search)
36 ;; (eval-after-load "mew-summary"
37 ;; '(define-key mew-summary-mode-map "\C-o" 'org-mew-capture))
39 ;; 1. In the Mew's inbox folder, take a glance at new messages to find
40 ;; a message that requires any action.
42 ;; 2. If the message is a reply from somebody and associated with the
43 ;; existing orgmode entry, type M-x `org-mew-search' RET (or press
44 ;; the ' key simply) to find the entry. If you can find the entry
45 ;; successfully and think you should start the task right now,
46 ;; start the task by M-x `org-agenda-clock-in' RET.
48 ;; 3. If the message is a new message, type M-x `org-mew-capture' RET,
49 ;; enter the refile folder, and the buffer to capture the message
50 ;; is shown up (without selecting the template by hand). Then you
51 ;; can fill the template and type C-c C-c to complete the capture.
52 ;; Note that you can configure `org-capture-templates' so that the
53 ;; captured entry has a link to the message.
55 ;;; Code:
57 (require 'org)
59 (defgroup org-mew nil
60 "Options concerning the Mew link."
61 :tag "Org Startup"
62 :group 'org-link)
64 (defcustom org-mew-link-to-refile-destination t
65 "Create a link to the refile destination if the message is marked as refile."
66 :group 'org-mew
67 :type 'boolean)
69 (defcustom org-mew-inbox-folder nil
70 "The folder where new messages are incorporated.
71 If `org-mew-inbox-folder' is non-nil, `org-mew-open' locates the message
72 in this inbox folder as well as the folder specified by the link."
73 :group 'org-mew
74 :version "24.4"
75 :package-version '(Org . "8.0")
76 :type 'string)
78 (defcustom org-mew-use-id-db t
79 "Use ID database to locate the message if id.db is created."
80 :group 'org-mew
81 :version "24.4"
82 :package-version '(Org . "8.0")
83 :type 'boolean)
85 (defcustom org-mew-subject-alist
86 (list (cons (concat "^\\(?:\\(?:re\\|fwd?\\): *\\)*"
87 "\\(?:[[(][a-z0-9._-]+[:,]? [0-9]+[])]\\)? *"
88 "\\(?:\\(?:re\\|fwd?\\): *\\)*"
89 "\\(.*\\)[ \t]*")
90 1))
91 "Alist of subject regular expression and matched group number for search."
92 :group 'org-mew
93 :version "24.4"
94 :package-version '(Org . "8.0")
95 :type '(repeat (cons (regexp) (integer))))
97 (defcustom org-mew-capture-inbox-folders nil
98 "List of inbox folders whose messages need refile marked before capture.
99 `org-mew-capture' will ask you to put the refile mark on the
100 message if the message's folder is any of these folders and the
101 message is not marked. Nil means `org-mew-capture' never ask you
102 destination folders before capture."
103 :group 'org-mew
104 :version "24.4"
105 :package-version '(Org . "8.0")
106 :type '(repeat string))
108 (defcustom org-mew-capture-guess-alist nil
109 "Alist of the regular expression of the folder name and the capture
110 template selection keys.
112 For example,
113 '((\"^%emacs-orgmode$\" . \"o\")
114 (\"\" . \"t\"))
115 the messages in \"%emacs-orgmode\" folder will be captured with
116 the capture template associated with \"o\" key, and any other
117 messages will be captured with the capture template associated
118 with \"t\" key."
119 :group 'org-mew
120 :version "24.4"
121 :package-version '(Org . "8.0")
122 :type '(repeat (cons regexp string)))
124 ;; Declare external functions and variables
125 (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
126 (declare-function mew-case-folder "ext:mew-func" (case folder))
127 (declare-function mew-folder-path-to-folder
128 "ext:mew-func" (path &optional has-proto))
129 (declare-function mew-idstr-to-id-list "ext:mew-header" (idstr &optional rev))
130 (declare-function mew-folder-remotep "ext:mew-func" (folder))
131 (declare-function mew-folder-virtualp "ext:mew-func" (folder))
132 (declare-function mew-header-get-value "ext:mew-header"
133 (field &optional as-list))
134 (declare-function mew-init "ext:mew" ())
135 (declare-function mew-refile-get "ext:mew-refile" (msg))
136 (declare-function mew-sinfo-get-case "ext:mew-summary" ())
137 (declare-function mew-summary-diag-global "ext:mew-thread" (id opt who))
138 (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
139 (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
140 (declare-function mew-summary-get-mark "ext:mew-mark" ())
141 (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
142 (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
143 (pattern folder src-msgs))
144 (declare-function mew-summary-refile "ext:mew-refile" (&optional report))
145 (declare-function mew-summary-search-msg "ext:mew-const" (msg))
146 (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
147 (declare-function mew-summary-visit-folder "ext:mew-summary4"
148 (folder &optional goend no-ls))
149 (declare-function mew-window-push "ext:mew" ())
150 (declare-function mew-expand-folder "ext:mew-func" (folder))
151 (declare-function mew-case:folder-folder "ext:mew-func" (case:folder))
152 (declare-function mew "ext:mew" (&optional arg))
153 (declare-function mew-message-goto-summary "ext:mew-message" ())
154 (declare-function mew-summary-mode "ext:mew-summary" ())
156 (defvar mew-init-p)
157 (defvar mew-mark-afterstep-spec)
158 (defvar mew-summary-goto-line-then-display)
160 ;; Install the link type
161 (org-add-link-type "mew" 'org-mew-open)
162 (add-hook 'org-store-link-functions 'org-mew-store-link)
164 ;; Implementation
165 (defun org-mew-store-link ()
166 "Store a link to a Mew folder or message."
167 (save-window-excursion
168 (if (eq major-mode 'mew-message-mode)
169 (mew-message-goto-summary))
170 (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
171 (let ((msgnum (mew-summary-message-number2))
172 (folder-name (org-mew-folder-name)))
173 (if (fboundp 'mew-summary-set-message-buffer)
174 (mew-summary-set-message-buffer folder-name msgnum)
175 (set-buffer (mew-cache-hit folder-name msgnum t)))
176 (let* ((message-id (mew-header-get-value "Message-Id:"))
177 (from (mew-header-get-value "From:"))
178 (to (mew-header-get-value "To:"))
179 (date (mew-header-get-value "Date:"))
180 (date-ts (and date (format-time-string
181 (org-time-stamp-format t)
182 (date-to-time date))))
183 (date-ts-ia (and date (format-time-string
184 (org-time-stamp-format t t)
185 (date-to-time date))))
186 (subject (mew-header-get-value "Subject:"))
187 desc link)
188 (org-store-link-props :type "mew" :from from :to to
189 :subject subject :message-id message-id)
190 (when date
191 (org-add-link-props :date date :date-timestamp date-ts
192 :date-timestamp-inactive date-ts-ia))
193 (setq message-id (org-remove-angle-brackets message-id))
194 (setq desc (org-email-link-description))
195 (setq link (concat "mew:" folder-name "#" message-id))
196 (org-add-link-props :link link :description desc)
197 link)))))
199 (defun org-mew-folder-name ()
200 "Return the folder name of the current message."
201 (save-window-excursion
202 (if (eq major-mode 'mew-message-mode)
203 (mew-message-goto-summary))
204 (let* ((msgnum (mew-summary-message-number2))
205 (mark-info (mew-summary-get-mark)))
206 (if (and org-mew-link-to-refile-destination
207 (eq mark-info ?o)) ; marked as refile
208 (nth 1 (mew-refile-get msgnum))
209 (let ((folder-or-path (mew-summary-folder-name)))
210 (mew-folder-path-to-folder folder-or-path t))))))
212 (defun org-mew-open (path)
213 "Follow the Mew message link specified by PATH."
214 (let (folder message-id)
215 (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
216 (setq folder (match-string 1 path))
217 (setq message-id (match-string 2 path)))
218 ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
219 (setq folder (match-string 1 path))
220 (setq message-id (match-string 4 path)))
221 ((and org-mew-use-id-db (string-match "\\`#\\(.+\\)" path))
222 (setq folder nil)
223 (setq message-id (match-string 1 path)))
224 (t (error "Error in Mew link")))
225 (require 'mew)
226 (mew-window-push)
227 (unless mew-init-p (mew-init))
228 (if (null folder)
229 (progn
230 (mew t)
231 (org-mew-open-by-message-id message-id))
232 (or (org-mew-follow-link folder message-id)
233 (and org-mew-inbox-folder (not (string= org-mew-inbox-folder folder))
234 (org-mew-follow-link org-mew-inbox-folder message-id))
235 (and org-mew-use-id-db
236 (org-mew-open-by-message-id message-id))
237 (error "Message not found")))))
239 (defun org-mew-follow-link (folder message-id)
240 (unless (org-mew-folder-exists-p folder)
241 (error "No such folder or wrong folder %s" folder))
242 (mew-summary-visit-folder folder)
243 (when message-id
244 (let ((msgnum (org-mew-get-msgnum folder message-id)))
245 (when (mew-summary-search-msg msgnum)
246 (if mew-summary-goto-line-then-display
247 (mew-summary-display))
248 t))))
250 (defun org-mew-folder-exists-p (folder)
251 (let ((dir (mew-expand-folder folder)))
252 (cond
253 ((mew-folder-virtualp folder) (get-buffer folder))
254 ((null dir) nil)
255 ((mew-folder-remotep (mew-case:folder-folder folder)) t)
256 (t (file-directory-p dir)))))
258 (defun org-mew-get-msgnum (folder message-id)
259 (if (string-match "\\`[0-9]+\\'" message-id)
260 message-id
261 (let* ((pattern (concat "message-id=" message-id))
262 (msgs (mew-summary-pick-with-mewl pattern folder nil)))
263 (car msgs))))
265 (defun org-mew-open-by-message-id (message-id)
266 "Open message using ID database."
267 (let ((result (mew-summary-diag-global (format "<%s>" message-id)
268 "-p" "Message")))
269 (unless (eq result t)
270 (error "Message not found"))))
272 ;; In ~/.mew.el, add the following line:
273 ;; (define-key mew-summary-mode-map "'" 'org-mew-search)
274 (defun org-mew-search (&optional arg)
275 "Show all entries related to the message using `org-search-view'.
277 It shows entries which contains the message ID, the reference
278 IDs, or the subject of the message.
280 With C-u prefix, search for the entries that contains the message
281 ID or any of the reference IDs. With C-u C-u prefix, search for
282 the message ID or the last reference ID.
284 The search phase for the subject is extracted with
285 `org-mew-subject-alist', which defines the regular expression of
286 the subject and the group number to extract. You can get rid of
287 \"Re:\" and some other prefix from the subject text."
288 (interactive "P")
289 (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
290 (let ((last-reference-only (equal arg '(16)))
291 (by-subject (null arg))
292 (msgnum (mew-summary-message-number2))
293 (folder-name (mew-summary-folder-name))
294 subject message-id references id-list)
295 (save-window-excursion
296 (if (fboundp 'mew-summary-set-message-buffer)
297 (mew-summary-set-message-buffer folder-name msgnum)
298 (set-buffer (mew-cache-hit folder-name msgnum t)))
299 (setq subject (mew-header-get-value "Subject:"))
300 (setq message-id (mew-header-get-value "Message-Id:"))
301 (setq references (mew-header-get-value "References:")))
302 (setq id-list (mapcar (lambda (id) (org-remove-angle-brackets id))
303 (mew-idstr-to-id-list references)))
304 (if last-reference-only
305 (setq id-list (last id-list))
306 (if message-id
307 (setq id-list (cons (org-remove-angle-brackets message-id)
308 id-list))))
309 (when (and by-subject (stringp subject))
310 (catch 'matched
311 (mapc (lambda (elem)
312 (let ((regexp (car elem))
313 (num (cdr elem)))
314 (when (string-match regexp subject)
315 (setq subject (match-string num subject))
316 (throw 'matched t))))
317 org-mew-subject-alist))
318 (setq id-list (cons subject id-list)))
319 (cond ((null id-list)
320 (error "No message ID to search."))
321 ((equal (length id-list) 1)
322 (org-search-view nil (car id-list)))
324 (org-search-view nil (format "{\\(%s\\)}"
325 (mapconcat 'regexp-quote
326 id-list "\\|"))))))
327 (delete-other-windows)))
329 (defun org-mew-capture (arg)
330 "Guess the capture template from the folder name and invoke `org-capture'.
332 This selects a capture template in `org-capture-templates' by
333 searching for capture template selection keys defined in
334 `org-mew-capture-guess-alist' which are associated with the
335 regular expression that matches the message's folder name, and
336 then invokes `org-capture'.
338 If the message's folder is a inbox folder, you are prompted to
339 put the refile mark on the message and the capture template is
340 guessed from the refile destination folder. You can customize
341 the inbox folders by `org-mew-capture-inbox-folders'.
343 If ARG is non-nil, this does not guess the capture template but
344 asks you to select the capture template."
345 (interactive "P")
346 (or (not (member (org-mew-folder-name)
347 org-mew-capture-inbox-folders))
348 (eq (mew-summary-get-mark) ?o)
349 (save-window-excursion
350 (if (eq major-mode 'mew-message-mode)
351 (mew-message-goto-summary))
352 (let ((mew-mark-afterstep-spec '((?o 0 0 0 0 0 0 0))))
353 (mew-summary-refile)))
354 (error "No refile folder selected."))
355 (let* ((org-mew-link-to-refile-destination t)
356 (folder-name (org-mew-folder-name))
357 (keys (if arg
359 (org-mew-capture-guess-selection-keys folder-name))))
360 (org-capture nil keys)))
362 (defun org-mew-capture-guess-selection-keys (folder-name)
363 (catch 'found
364 (let ((alist org-mew-capture-guess-alist))
365 (while alist
366 (let ((elem (car alist)))
367 (if (string-match (car elem) folder-name)
368 (throw 'found (cdr elem))))
369 (setq alist (cdr alist))))))
371 (provide 'org-mew)
373 ;;; org-mew.el ends here