Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-mew.el
blob6dbc67cc01e21ae2564fcd7e2767f37bc2198106
1 ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
3 ;; Copyright (C) 2008-2017 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 not part of GNU Emacs.
11 ;; This program 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 ;; This program 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 :type 'string)
76 (defcustom org-mew-use-id-db t
77 "Use ID database to locate the message if id.db is created."
78 :group 'org-mew
79 :type 'boolean)
81 (defcustom org-mew-subject-alist
82 (list (cons (concat "^\\(?:\\(?:re\\|fwd?\\): *\\)*"
83 "\\(?:[[(][a-z0-9._-]+[:,]? [0-9]+[])]\\)? *"
84 "\\(?:\\(?:re\\|fwd?\\): *\\)*"
85 "\\(.*\\)[ \t]*")
86 1))
87 "Alist of subject regular expression and matched group number for search."
88 :group 'org-mew
89 :type '(repeat (cons (regexp) (integer))))
91 (defcustom org-mew-capture-inbox-folders nil
92 "List of inbox folders whose messages need refile marked before capture.
93 `org-mew-capture' will ask you to put the refile mark on the
94 message if the message's folder is any of these folders and the
95 message is not marked. Nil means `org-mew-capture' never ask you
96 destination folders before capture."
97 :group 'org-mew
98 :type '(repeat string))
100 (defcustom org-mew-capture-guess-alist nil
101 "Alist of the regular expression of the folder name and the capture
102 template selection keys.
104 For example,
105 '((\"^%emacs-orgmode$\" . \"o\")
106 (\"\" . \"t\"))
107 the messages in \"%emacs-orgmode\" folder will be captured with
108 the capture template associated with \"o\" key, and any other
109 messages will be captured with the capture template associated
110 with \"t\" key."
111 :group 'org-mew
112 :type '(repeat (cons regexp string)))
114 ;; Declare external functions and variables
115 (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
116 (declare-function mew-case-folder "ext:mew-func" (case folder))
117 (declare-function mew-folder-path-to-folder
118 "ext:mew-func" (path &optional has-proto))
119 (declare-function mew-idstr-to-id-list "ext:mew-header" (idstr &optional rev))
120 (declare-function mew-folder-remotep "ext:mew-func" (folder))
121 (declare-function mew-folder-virtualp "ext:mew-func" (folder))
122 (declare-function mew-header-get-value "ext:mew-header"
123 (field &optional as-list))
124 (declare-function mew-init "ext:mew" ())
125 (declare-function mew-refile-get "ext:mew-refile" (msg))
126 (declare-function mew-sinfo-get-case "ext:mew-summary" ())
127 (declare-function mew-summary-diag-global "ext:mew-thread" (id opt who))
128 (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
129 (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
130 (declare-function mew-summary-get-mark "ext:mew-mark" ())
131 (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
132 (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
133 (pattern folder src-msgs))
134 (declare-function mew-summary-refile "ext:mew-refile" (&optional report))
135 (declare-function mew-summary-search-msg "ext:mew-const" (msg))
136 (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
137 (declare-function mew-summary-visit-folder "ext:mew-summary4"
138 (folder &optional goend no-ls))
139 (declare-function mew-window-push "ext:mew" ())
140 (declare-function mew-expand-folder "ext:mew-func" (folder))
141 (declare-function mew-case:folder-folder "ext:mew-func" (case:folder))
142 (declare-function mew "ext:mew" (&optional arg))
143 (declare-function mew-message-goto-summary "ext:mew-message" ())
144 (declare-function mew-summary-mode "ext:mew-summary" ())
146 (defvar mew-init-p)
147 (defvar mew-mark-afterstep-spec)
148 (defvar mew-summary-goto-line-then-display)
150 ;; Install the link type
151 (org-link-set-parameters "mew" :follow #'org-mew-open :store #'org-mew-store-link)
153 ;; Implementation
154 (defun org-mew-store-link ()
155 "Store a link to a Mew folder or message."
156 (save-window-excursion
157 (if (eq major-mode 'mew-message-mode)
158 (mew-message-goto-summary))
159 (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
160 (let ((msgnum (mew-summary-message-number2))
161 (folder-name (org-mew-folder-name)))
162 (if (fboundp 'mew-summary-set-message-buffer)
163 (mew-summary-set-message-buffer folder-name msgnum)
164 (set-buffer (mew-cache-hit folder-name msgnum t)))
165 (let* ((message-id (mew-header-get-value "Message-Id:"))
166 (from (mew-header-get-value "From:"))
167 (to (mew-header-get-value "To:"))
168 (date (mew-header-get-value "Date:"))
169 (subject (mew-header-get-value "Subject:"))
170 desc link)
171 (org-store-link-props :type "mew" :from from :to to :date date
172 :subject subject :message-id message-id)
173 (setq message-id (org-unbracket-string "<" ">" message-id))
174 (setq desc (org-email-link-description))
175 (setq link (concat "mew:" folder-name "#" message-id))
176 (org-add-link-props :link link :description desc)
177 link)))))
179 (defun org-mew-folder-name ()
180 "Return the folder name of the current message."
181 (save-window-excursion
182 (if (eq major-mode 'mew-message-mode)
183 (mew-message-goto-summary))
184 (let* ((msgnum (mew-summary-message-number2))
185 (mark-info (mew-summary-get-mark)))
186 (if (and org-mew-link-to-refile-destination
187 (eq mark-info ?o)) ; marked as refile
188 (mew-case-folder (mew-sinfo-get-case)
189 (nth 1 (mew-refile-get msgnum)))
190 (let ((folder-or-path (mew-summary-folder-name)))
191 (mew-folder-path-to-folder folder-or-path t))))))
193 (defun org-mew-open (path)
194 "Follow the Mew message link specified by PATH."
195 (let (folder message-id)
196 (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
197 (setq folder (match-string 1 path))
198 (setq message-id (match-string 2 path)))
199 ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
200 (setq folder (match-string 1 path))
201 (setq message-id (match-string 4 path)))
202 ((and org-mew-use-id-db (string-match "\\`#\\(.+\\)" path))
203 (setq folder nil)
204 (setq message-id (match-string 1 path)))
205 (t (error "Error in Mew link")))
206 (require 'mew)
207 (mew-window-push)
208 (unless mew-init-p (mew-init))
209 (if (null folder)
210 (progn
211 (mew t)
212 (org-mew-open-by-message-id message-id))
213 (or (org-mew-follow-link folder message-id)
214 (and org-mew-inbox-folder (not (string= org-mew-inbox-folder folder))
215 (org-mew-follow-link org-mew-inbox-folder message-id))
216 (and org-mew-use-id-db
217 (org-mew-open-by-message-id message-id))
218 (error "Message not found")))))
220 (defun org-mew-follow-link (folder message-id)
221 (unless (org-mew-folder-exists-p folder)
222 (error "No such folder or wrong folder %s" folder))
223 (mew-summary-visit-folder folder)
224 (when message-id
225 (let ((msgnum (org-mew-get-msgnum folder message-id)))
226 (when (mew-summary-search-msg msgnum)
227 (if mew-summary-goto-line-then-display
228 (mew-summary-display))
229 t))))
231 (defun org-mew-folder-exists-p (folder)
232 (let ((dir (mew-expand-folder folder)))
233 (cond
234 ((mew-folder-virtualp folder) (get-buffer folder))
235 ((null dir) nil)
236 ((mew-folder-remotep (mew-case:folder-folder folder)) t)
237 (t (file-directory-p dir)))))
239 (defun org-mew-get-msgnum (folder message-id)
240 (if (string-match "\\`[0-9]+\\'" message-id)
241 message-id
242 (let* ((pattern (concat "message-id=" message-id))
243 (msgs (mew-summary-pick-with-mewl pattern folder nil)))
244 (car msgs))))
246 (defun org-mew-open-by-message-id (message-id)
247 "Open message using ID database."
248 (let ((result (mew-summary-diag-global (format "<%s>" message-id)
249 "-p" "Message")))
250 (unless (eq result t)
251 (error "Message not found"))))
253 ;; In ~/.mew.el, add the following line:
254 ;; (define-key mew-summary-mode-map "'" 'org-mew-search)
255 (defun org-mew-search (&optional arg)
256 "Show all entries related to the message using `org-search-view'.
258 It shows entries which contains the message ID, the reference
259 IDs, or the subject of the message.
261 With C-u prefix, search for the entries that contains the message
262 ID or any of the reference IDs. With C-u C-u prefix, search for
263 the message ID or the last reference ID.
265 The search phase for the subject is extracted with
266 `org-mew-subject-alist', which defines the regular expression of
267 the subject and the group number to extract. You can get rid of
268 \"Re:\" and some other prefix from the subject text."
269 (interactive "P")
270 (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
271 (let ((last-reference-only (equal arg '(16)))
272 (by-subject (null arg))
273 (msgnum (mew-summary-message-number2))
274 (folder-name (mew-summary-folder-name))
275 subject message-id references id-list)
276 (save-window-excursion
277 (if (fboundp 'mew-summary-set-message-buffer)
278 (mew-summary-set-message-buffer folder-name msgnum)
279 (set-buffer (mew-cache-hit folder-name msgnum t)))
280 (setq subject (mew-header-get-value "Subject:"))
281 (setq message-id (mew-header-get-value "Message-Id:"))
282 (setq references (mew-header-get-value "References:")))
283 (setq id-list (mapcar (lambda (id) (org-unbracket-string "<" ">" id))
284 (mew-idstr-to-id-list references)))
285 (if last-reference-only
286 (setq id-list (last id-list))
287 (if message-id
288 (setq id-list (cons (org-unbracket-string "<" ">" message-id)
289 id-list))))
290 (when (and by-subject (stringp subject))
291 (catch 'matched
292 (mapc (lambda (elem)
293 (let ((regexp (car elem))
294 (num (cdr elem)))
295 (when (string-match regexp subject)
296 (setq subject (match-string num subject))
297 (throw 'matched t))))
298 org-mew-subject-alist))
299 (setq id-list (cons subject id-list)))
300 (cond ((null id-list)
301 (error "No message ID to search"))
302 ((equal (length id-list) 1)
303 (org-search-view nil (car id-list)))
305 (org-search-view nil (format "{\\(%s\\)}"
306 (mapconcat 'regexp-quote
307 id-list "\\|"))))))
308 (delete-other-windows)))
310 (defun org-mew-capture (arg)
311 "Guess the capture template from the folder name and invoke `org-capture'.
313 This selects a capture template in `org-capture-templates' by
314 searching for capture template selection keys defined in
315 `org-mew-capture-guess-alist' which are associated with the
316 regular expression that matches the message's folder name, and
317 then invokes `org-capture'.
319 If the message's folder is a inbox folder, you are prompted to
320 put the refile mark on the message and the capture template is
321 guessed from the refile destination folder. You can customize
322 the inbox folders by `org-mew-capture-inbox-folders'.
324 If ARG is non-nil, this does not guess the capture template but
325 asks you to select the capture template."
326 (interactive "P")
327 (or (not (member (org-mew-folder-name)
328 org-mew-capture-inbox-folders))
329 (eq (mew-summary-get-mark) ?o)
330 (save-window-excursion
331 (if (eq major-mode 'mew-message-mode)
332 (mew-message-goto-summary))
333 (let ((mew-mark-afterstep-spec '((?o 0 0 0 0 0 0 0))))
334 (mew-summary-refile)))
335 (error "No refile folder selected"))
336 (let* ((org-mew-link-to-refile-destination t)
337 (folder-name (org-mew-folder-name))
338 (keys (if arg
340 (org-mew-capture-guess-selection-keys folder-name))))
341 (org-capture nil keys)))
343 (defun org-mew-capture-guess-selection-keys (folder-name)
344 (catch 'found
345 (let ((alist org-mew-capture-guess-alist))
346 (while alist
347 (let ((elem (car alist)))
348 (if (string-match (car elem) folder-name)
349 (throw 'found (cdr elem))))
350 (setq alist (cdr alist))))))
352 (provide 'org-mew)
354 ;;; org-mew.el ends here