ox-html: Fix link handling
[org-mode/org-kjn.git] / contrib / lisp / org-wl.el
blob632c9e37b02cc852a0e54bb74197c90192ef5487
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
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
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'.
32 ;;; Code:
34 (require 'org)
36 (defgroup org-wl nil
37 "Options concerning the Wanderlust link."
38 :tag "Org Startup"
39 :group 'org-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."
43 :group 'org-wl
44 :type 'boolean)
46 (defcustom org-wl-link-remove-filter nil
47 "Remove filter condition if message is filter folder."
48 :group 'org-wl
49 :type 'boolean)
51 (defcustom org-wl-shimbun-prefer-web-links nil
52 "If non-nil create web links for shimbun messages."
53 :group 'org-wl
54 :type 'boolean)
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."
60 :type 'boolean
61 :group 'org-wl)
63 (defcustom org-wl-disable-folder-check t
64 "Disable check for new messages when open a link."
65 :type 'boolean
66 :group 'org-wl)
68 (defcustom org-wl-namazu-default-index nil
69 "Default namazu search index."
70 :type '(choice (const nil) (directory))
71 :group 'org-wl)
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"
84 (&optional id))
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"
96 (&optional getid))
97 (declare-function wl-folder-buffer-group-p "ext:wl-folder")
98 (defvar wl-init)
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)
115 ;; Implementation
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
124 (when (not type)
125 (setq type
126 (cond
127 ((and (>= (length folder) 5)
128 (string= (substring folder 0 5) "file:"))
129 'file)
130 ((and (>= (length folder) 7)
131 (string= (substring folder 0 7) "access:"))
132 'access)
134 nil))))
135 type))
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."
146 (unless (eobp)
147 (cond
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))
153 nil))))
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)))
160 (save-excursion
161 (beginning-of-line)
162 (unless (and (wl-folder-buffer-group-p)
163 (looking-at wl-folder-group-regexp))
164 (org-store-link-props :type "wl" :description petname
165 :link link)
166 link))))
168 (defun org-wl-store-link-message ()
169 "Store a link to a WL message."
170 (save-excursion
171 (let ((buf (if (eq major-mode 'wl-summary-mode)
172 (current-buffer)
173 (and (boundp 'wl-message-buffer-cur-summary-buffer)
174 wl-message-buffer-cur-summary-buffer))))
175 (when buf
176 (with-current-buffer buf
177 (let* ((msgnum (wl-summary-message-number))
178 (mark-info (wl-summary-registered-temp-mark msgnum))
179 (folder-name
180 (if (and org-wl-link-to-refile-destination
181 mark-info
182 (equal (nth 1 mark-info) "o")) ; marked as refile
183 (nth 2 mark-info)
184 wl-summary-buffer-folder-name))
185 (folder-type (org-wl-folder-type folder-name))
186 (wl-message-entity
187 (if (fboundp 'elmo-message-entity)
188 (elmo-message-entity
189 wl-summary-buffer-elmo-folder msgnum)
190 (elmo-msgdb-overview-get-entity
191 msgnum (wl-summary-buffer-msgdb))))
192 (message-id
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))))
207 desc link)
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)
217 (setq folder-name
218 (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
220 ;; maybe create http link
221 (cond
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
227 :subject subject))
228 ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
229 (setq link
230 (format
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
238 :subject subject))
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)))
246 (when date
247 (org-add-link-props :date date :date-timestamp date-ts
248 :date-timestamp-inactive date-ts-ia))
249 (or link xref)))))))
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) "@"))
255 (group (nth 3 spec))
256 (article (nth 4 spec)))
257 (org-wl-open
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
271 for namazu index."
272 (require 'wl)
273 (let ((wl-auto-check-folder-name
274 (if org-wl-disable-folder-check
275 'none
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))
301 (when article
302 (if (org-string-match-p "@" article)
303 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
304 article))
305 (or (wl-summary-jump-to-msg (string-to-number article))
306 (error "No such message: %s" article)))
307 (wl-summary-redisplay))))))
309 (provide 'org-wl)
311 ;;; org-wl.el ends here