1 ;;; org-wl.el --- Support for links to Wanderlust messages from within Org-mode
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
6 ;; David Maus <dmaus at ictsoc dot de>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
10 ;; This file is not part of GNU Emacs.
12 ;; This program is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file implements links to Wanderlust messages from within Org-mode.
29 ;; Org-mode loads this module by default - if this is not what you want,
30 ;; configure the variable `org-modules'.
37 "Options concerning the Wanderlust link."
41 (defcustom org-wl-link-to-refile-destination t
42 "Create a link to the refile destination if the message is marked as refile."
46 (defcustom org-wl-link-remove-filter nil
47 "Remove filter condition if message is filter folder."
51 (defcustom org-wl-shimbun-prefer-web-links nil
52 "If non-nil create web links for shimbun messages."
56 (defcustom org-wl-nntp-prefer-web-links nil
57 "If non-nil create web links for nntp messages.
58 When folder name contains string \"gmane\" link to gmane,
59 googlegroups otherwise."
63 (defcustom org-wl-disable-folder-check t
64 "Disable check for new messages when open a link."
68 (defcustom org-wl-namazu-default-index nil
69 "Default namazu search index."
70 :type
'(choice (const nil
) (directory))
73 ;; Declare external functions and variables
74 (declare-function elmo-folder-exists-p
"ext:elmo" (folder) t
)
75 (declare-function elmo-message-entity-field
"ext:elmo-msgdb"
76 (entity field
&optional type
))
77 (declare-function elmo-message-field
"ext:elmo"
78 (folder number field
&optional type
) t
)
79 (declare-function elmo-msgdb-overview-get-entity
"ext:elmo" (id msgdb
) t
)
80 ;; Backward compatibility to old version of wl
81 (declare-function wl
"ext:wl" () t
)
82 (declare-function wl-summary-buffer-msgdb
"ext:wl-folder" () t
)
83 (declare-function wl-summary-jump-to-msg-by-message-id
"ext:wl-summary"
85 (declare-function wl-summary-jump-to-msg
"ext:wl-summary"
86 (&optional number beg end
))
87 (declare-function wl-summary-line-from
"ext:wl-summary" ())
88 (declare-function wl-summary-line-subject
"ext:wl-summary" ())
89 (declare-function wl-summary-message-number
"ext:wl-summary" ())
90 (declare-function wl-summary-redisplay
"ext:wl-summary" (&optional arg
))
91 (declare-function wl-summary-registered-temp-mark
"ext:wl-action" (number))
92 (declare-function wl-folder-goto-folder-subr
"ext:wl-folder"
93 (&optional folder sticky
))
94 (declare-function wl-folder-get-petname
"ext:wl-folder" (name))
95 (declare-function wl-folder-get-entity-from-buffer
"ext:wl-folder"
97 (declare-function wl-folder-buffer-group-p
"ext:wl-folder")
99 (defvar wl-summary-buffer-elmo-folder
)
100 (defvar wl-summary-buffer-folder-name
)
101 (defvar wl-folder-group-regexp
)
102 (defvar wl-auto-check-folder-name
)
103 (defvar elmo-nntp-default-server
)
105 (defconst org-wl-folder-types
106 '(("%" . imap
) ("-" . nntp
) ("+" . mh
) ("=" . spool
)
107 ("$" . archive
) ("&" . pop
) ("@" . shimbun
) ("[" . search
)
108 ("*" . multi
) ("/" . filter
) ("|" . pipe
) ("'" . internal
))
109 "List of folder indicators. See Wanderlust manual, section 3.")
111 ;; Install the link type
112 (org-add-link-type "wl" 'org-wl-open
)
113 (add-hook 'org-store-link-functions
'org-wl-store-link
)
117 (defun org-wl-folder-type (folder)
118 "Return symbol that indicates the type of FOLDER.
119 FOLDER is the wanderlust folder name. The first character of the
120 folder name determines the folder type."
121 (let* ((indicator (substring folder
0 1))
122 (type (cdr (assoc indicator org-wl-folder-types
))))
123 ;; maybe access or file folder
127 ((and (>= (length folder
) 5)
128 (string= (substring folder
0 5) "file:"))
130 ((and (>= (length folder
) 7)
131 (string= (substring folder
0 7) "access:"))
137 (defun org-wl-message-field (field entity
)
138 "Return content of FIELD in ENTITY.
139 FIELD is a symbol of a rfc822 message header field.
140 ENTITY is a message entity."
141 (let ((content (elmo-message-entity-field entity field
'string
)))
142 (if (listp content
) (car content
) content
)))
144 (defun org-wl-store-link ()
145 "Store a link to a WL message or folder."
148 ((memq major-mode
'(wl-summary-mode mime-view-mode
))
149 (org-wl-store-link-message))
150 ((eq major-mode
'wl-folder-mode
)
151 (org-wl-store-link-folder))
155 (defun org-wl-store-link-folder ()
156 "Store a link to a WL folder."
157 (let* ((folder (wl-folder-get-entity-from-buffer))
158 (petname (wl-folder-get-petname folder
))
159 (link (concat "wl:" folder
)))
162 (unless (and (wl-folder-buffer-group-p)
163 (looking-at wl-folder-group-regexp
))
164 (org-store-link-props :type
"wl" :description petname
168 (defun org-wl-store-link-message ()
169 "Store a link to a WL message."
171 (let ((buf (if (eq major-mode
'wl-summary-mode
)
173 (and (boundp 'wl-message-buffer-cur-summary-buffer
)
174 wl-message-buffer-cur-summary-buffer
))))
176 (with-current-buffer buf
177 (let* ((msgnum (wl-summary-message-number))
178 (mark-info (wl-summary-registered-temp-mark msgnum
))
180 (if (and org-wl-link-to-refile-destination
182 (equal (nth 1 mark-info
) "o")) ; marked as refile
184 wl-summary-buffer-folder-name
))
185 (folder-type (org-wl-folder-type folder-name
))
187 (if (fboundp 'elmo-message-entity
)
189 wl-summary-buffer-elmo-folder msgnum
)
190 (elmo-msgdb-overview-get-entity
191 msgnum
(wl-summary-buffer-msgdb))))
193 (org-wl-message-field 'message-id wl-message-entity
))
194 (message-id-no-brackets
195 (org-remove-angle-brackets message-id
))
196 (from (org-wl-message-field 'from wl-message-entity
))
197 (to (org-wl-message-field 'to wl-message-entity
))
198 (xref (org-wl-message-field 'xref wl-message-entity
))
199 (subject (org-wl-message-field 'subject wl-message-entity
))
200 (date (org-wl-message-field 'date wl-message-entity
))
201 (date-ts (and date
(format-time-string
202 (org-time-stamp-format t
)
203 (date-to-time date
))))
204 (date-ts-ia (and date
(format-time-string
205 (org-time-stamp-format t t
)
206 (date-to-time date
))))
209 ;; remove text properties of subject string to avoid possible bug
210 ;; when formatting the subject
211 ;; (Emacs bug #5306, fixed)
212 (set-text-properties 0 (length subject
) nil subject
)
214 ;; maybe remove filter condition
215 (when (and (eq folder-type
'filter
) org-wl-link-remove-filter
)
216 (while (eq (org-wl-folder-type folder-name
) 'filter
)
218 (replace-regexp-in-string "^/[^/]+/" "" folder-name
))))
220 ;; maybe create http link
222 ((and (eq folder-type
'shimbun
)
223 org-wl-shimbun-prefer-web-links xref
)
224 (org-store-link-props :type
"http" :link xref
:description subject
225 :from from
:to to
:message-id message-id
226 :message-id-no-brackets message-id-no-brackets
228 ((and (eq folder-type
'nntp
) org-wl-nntp-prefer-web-links
)
231 (if (string-match "gmane\\." folder-name
)
232 "http://mid.gmane.org/%s"
233 "http://groups.google.com/groups/search?as_umsgid=%s")
234 (org-fixup-message-id-for-http message-id
)))
235 (org-store-link-props :type
"http" :link link
:description subject
236 :from from
:to to
:message-id message-id
237 :message-id-no-brackets message-id-no-brackets
240 (org-store-link-props :type
"wl" :from from
:to to
241 :subject subject
:message-id message-id
242 :message-id-no-brackets message-id-no-brackets
)
243 (setq desc
(org-email-link-description))
244 (setq link
(concat "wl:" folder-name
"#" message-id-no-brackets
))
245 (org-add-link-props :link link
:description desc
)))
247 (org-add-link-props :date date
:date-timestamp date-ts
248 :date-timestamp-inactive date-ts-ia
))
251 (defun org-wl-open-nntp (path)
252 "Follow the nntp: link specified by PATH."
253 (let* ((spec (split-string path
"/"))
254 (server (split-string (nth 2 spec
) "@"))
256 (article (nth 4 spec
)))
258 (concat "-" group
":" (if (cdr server
)
259 (car (split-string (car server
) ":"))
261 (if (string= elmo-nntp-default-server
(nth 2 spec
))
263 (concat "@" (or (cdr server
) (car server
))))
264 (if article
(concat "#" article
) "")))))
266 (defun org-wl-open (path)
267 "Follow the WL message link specified by PATH.
268 When called with one prefix, open message in namazu search folder
269 with `org-wl-namazu-default-index' as search index. When called
270 with two prefixes or `org-wl-namazu-default-index' is nil, ask
273 (let ((wl-auto-check-folder-name
274 (if org-wl-disable-folder-check
276 wl-auto-check-folder-name
)))
277 (unless wl-init
(wl))
278 ;; XXX: The imap-uw's MH folder names start with "%#".
279 (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path
))
280 (error "Error in Wanderlust link"))
281 (let ((folder (match-string 1 path
))
282 (article (match-string 3 path
)))
283 ;; maybe open message in namazu search folder
284 (when current-prefix-arg
285 (setq folder
(concat "[" article
"]"
286 (if (and (equal current-prefix-arg
'(4))
287 org-wl-namazu-default-index
)
288 org-wl-namazu-default-index
289 (read-directory-name "Namazu index: ")))))
290 (if (not (elmo-folder-exists-p (org-no-warnings
291 (wl-folder-get-elmo-folder folder
))))
292 (error "No such folder: %s" folder
))
293 (let ((old-buf (current-buffer))
294 (old-point (point-marker)))
295 (wl-folder-goto-folder-subr folder
)
296 (with-current-buffer old-buf
297 ;; XXX: `wl-folder-goto-folder-subr' moves point to the
298 ;; beginning of the current line. So, restore the point
299 ;; in the old buffer.
300 (goto-char old-point
))
302 (if (org-string-match-p "@" article
)
303 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
305 (or (wl-summary-jump-to-msg (string-to-number article
))
306 (error "No such message: %s" article
)))
307 (wl-summary-redisplay))))))
311 ;;; org-wl.el ends here