Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-wl.el
blob4f74d479c33c0bb014bbc59baaf3ab82517070d8
1 ;;; org-wl.el --- Support for links to Wanderlust messages from within Org-mode
3 ;; Copyright (C) 2004-2017 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-link-set-parameters "wl" :follow #'org-wl-open :store #'org-wl-store-link)
114 ;; Implementation
116 (defun org-wl-folder-type (folder)
117 "Return symbol that indicates the type of FOLDER.
118 FOLDER is the wanderlust folder name. The first character of the
119 folder name determines the folder type."
120 (let* ((indicator (substring folder 0 1))
121 (type (cdr (assoc indicator org-wl-folder-types))))
122 ;; maybe access or file folder
123 (when (not type)
124 (setq type
125 (cond
126 ((and (>= (length folder) 5)
127 (string= (substring folder 0 5) "file:"))
128 'file)
129 ((and (>= (length folder) 7)
130 (string= (substring folder 0 7) "access:"))
131 'access)
133 nil))))
134 type))
136 (defun org-wl-message-field (field entity)
137 "Return content of FIELD in ENTITY.
138 FIELD is a symbol of a rfc822 message header field.
139 ENTITY is a message entity."
140 (let ((content (elmo-message-entity-field entity field 'string)))
141 (if (listp content) (car content) content)))
143 (defun org-wl-store-link ()
144 "Store a link to a WL message or folder."
145 (unless (eobp)
146 (cond
147 ((memq major-mode '(wl-summary-mode mime-view-mode))
148 (org-wl-store-link-message))
149 ((eq major-mode 'wl-folder-mode)
150 (org-wl-store-link-folder))
152 nil))))
154 (defun org-wl-store-link-folder ()
155 "Store a link to a WL folder."
156 (let* ((folder (wl-folder-get-entity-from-buffer))
157 (petname (wl-folder-get-petname folder))
158 (link (concat "wl:" folder)))
159 (save-excursion
160 (beginning-of-line)
161 (unless (and (wl-folder-buffer-group-p)
162 (looking-at wl-folder-group-regexp))
163 (org-store-link-props :type "wl" :description petname
164 :link link)
165 link))))
167 (defun org-wl-store-link-message ()
168 "Store a link to a WL message."
169 (save-excursion
170 (let ((buf (if (eq major-mode 'wl-summary-mode)
171 (current-buffer)
172 (and (boundp 'wl-message-buffer-cur-summary-buffer)
173 wl-message-buffer-cur-summary-buffer))))
174 (when buf
175 (with-current-buffer buf
176 (let* ((msgnum (wl-summary-message-number))
177 (mark-info (wl-summary-registered-temp-mark msgnum))
178 (folder-name
179 (if (and org-wl-link-to-refile-destination
180 mark-info
181 (equal (nth 1 mark-info) "o")) ; marked as refile
182 (nth 2 mark-info)
183 wl-summary-buffer-folder-name))
184 (folder-type (org-wl-folder-type folder-name))
185 (wl-message-entity
186 (if (fboundp 'elmo-message-entity)
187 (elmo-message-entity
188 wl-summary-buffer-elmo-folder msgnum)
189 (elmo-msgdb-overview-get-entity
190 msgnum (wl-summary-buffer-msgdb))))
191 (message-id
192 (org-wl-message-field 'message-id wl-message-entity))
193 (message-id-no-brackets
194 (org-unbracket-string "<" ">" message-id))
195 (from (org-wl-message-field 'from wl-message-entity))
196 (to (org-wl-message-field 'to wl-message-entity))
197 (xref (org-wl-message-field 'xref wl-message-entity))
198 (subject (org-wl-message-field 'subject wl-message-entity))
199 (date (org-wl-message-field 'date wl-message-entity))
200 desc link)
202 ;; remove text properties of subject string to avoid possible bug
203 ;; when formatting the subject
204 ;; (Emacs bug #5306, fixed)
205 (set-text-properties 0 (length subject) nil subject)
207 ;; maybe remove filter condition
208 (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
209 (while (eq (org-wl-folder-type folder-name) 'filter)
210 (setq folder-name
211 (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
213 ;; maybe create http link
214 (cond
215 ((and (eq folder-type 'shimbun)
216 org-wl-shimbun-prefer-web-links xref)
217 (org-store-link-props :type "http" :link xref :description subject
218 :from from :to to :message-id message-id
219 :message-id-no-brackets message-id-no-brackets
220 :subject subject))
221 ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
222 (setq link
223 (format
224 (if (string-match "gmane\\." folder-name)
225 "http://mid.gmane.org/%s"
226 "http://groups.google.com/groups/search?as_umsgid=%s")
227 (org-fixup-message-id-for-http message-id)))
228 (org-store-link-props :type "http" :link link :description subject
229 :from from :to to :message-id message-id
230 :message-id-no-brackets message-id-no-brackets
231 :subject subject))
233 (org-store-link-props :type "wl" :from from :to to
234 :subject subject :message-id message-id
235 :message-id-no-brackets message-id-no-brackets)
236 (setq desc (org-email-link-description))
237 (setq link (concat "wl:" folder-name "#" message-id-no-brackets))
238 (org-add-link-props :link link :description desc)))
239 (org-add-link-props :date date)
240 (or link xref)))))))
242 (defun org-wl-open-nntp (path)
243 "Follow the nntp: link specified by PATH."
244 (let* ((spec (split-string path "/"))
245 (server (split-string (nth 2 spec) "@"))
246 (group (nth 3 spec))
247 (article (nth 4 spec)))
248 (org-wl-open
249 (concat "-" group ":" (if (cdr server)
250 (car (split-string (car server) ":"))
252 (if (string= elmo-nntp-default-server (nth 2 spec))
254 (concat "@" (or (cdr server) (car server))))
255 (if article (concat "#" article) "")))))
257 (defun org-wl-open (path)
258 "Follow the WL message link specified by PATH.
259 When called with one prefix, open message in namazu search folder
260 with `org-wl-namazu-default-index' as search index. When called
261 with two prefixes or `org-wl-namazu-default-index' is nil, ask
262 for namazu index."
263 (require 'wl)
264 (let ((wl-auto-check-folder-name
265 (if org-wl-disable-folder-check
266 'none
267 wl-auto-check-folder-name)))
268 (unless wl-init (wl))
269 ;; XXX: The imap-uw's MH folder names start with "%#".
270 (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
271 (error "Error in Wanderlust link"))
272 (let ((folder (match-string 1 path))
273 (article (match-string 3 path)))
274 ;; maybe open message in namazu search folder
275 (when current-prefix-arg
276 (setq folder (concat "[" article "]"
277 (if (and (equal current-prefix-arg '(4))
278 org-wl-namazu-default-index)
279 org-wl-namazu-default-index
280 (read-directory-name "Namazu index: ")))))
281 (if (not (elmo-folder-exists-p (with-no-warnings
282 (wl-folder-get-elmo-folder folder))))
283 (error "No such folder: %s" folder))
284 (let ((old-buf (current-buffer))
285 (old-point (point-marker)))
286 (wl-folder-goto-folder-subr folder)
287 (with-current-buffer old-buf
288 ;; XXX: `wl-folder-goto-folder-subr' moves point to the
289 ;; beginning of the current line. So, restore the point
290 ;; in the old buffer.
291 (goto-char old-point))
292 (when article
293 (if (string-match-p "@" article)
294 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
295 article))
296 (or (wl-summary-jump-to-msg (string-to-number article))
297 (error "No such message: %s" article)))
298 (wl-summary-redisplay))))))
300 (provide 'org-wl)
302 ;;; org-wl.el ends here