org-wl.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ;;; org-wl.el --- Support for links to Wanderlust messages from within Org-mode
  2. ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
  3. ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
  4. ;; David Maus <dmaus at ictsoc dot de>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 7.7
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  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.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements links to Wanderlust messages from within Org-mode.
  25. ;; Org-mode loads this module by default - if this is not what you want,
  26. ;; configure the variable `org-modules'.
  27. ;;; Code:
  28. (require 'org)
  29. (defgroup org-wl nil
  30. "Options concerning the Wanderlust link."
  31. :tag "Org Startup"
  32. :group 'org-link)
  33. (defcustom org-wl-link-to-refile-destination t
  34. "Create a link to the refile destination if the message is marked as refile."
  35. :group 'org-wl
  36. :type 'boolean)
  37. (defcustom org-wl-link-remove-filter nil
  38. "Remove filter condition if message is filter folder."
  39. :group 'org-wl
  40. :type 'boolean)
  41. (defcustom org-wl-shimbun-prefer-web-links nil
  42. "If non-nil create web links for shimbun messages."
  43. :group 'org-wl
  44. :type 'boolean)
  45. (defcustom org-wl-nntp-prefer-web-links nil
  46. "If non-nil create web links for nntp messages.
  47. When folder name contains string \"gmane\" link to gmane,
  48. googlegroups otherwise."
  49. :type 'boolean
  50. :group 'org-wl)
  51. (defcustom org-wl-disable-folder-check t
  52. "Disable check for new messages when open a link."
  53. :type 'boolean
  54. :group 'org-wl)
  55. (defcustom org-wl-namazu-default-index nil
  56. "Default namazu search index."
  57. :type 'directory
  58. :group 'org-wl)
  59. ;; Declare external functions and variables
  60. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  61. (declare-function elmo-message-entity-field "ext:elmo-msgdb"
  62. (entity field &optional type))
  63. (declare-function elmo-message-field "ext:elmo"
  64. (folder number field &optional type) t)
  65. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (id msgdb) t)
  66. ;; Backward compatibility to old version of wl
  67. (declare-function wl "ext:wl" () t)
  68. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" () t)
  69. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary"
  70. (&optional id))
  71. (declare-function wl-summary-jump-to-msg "ext:wl-summary"
  72. (&optional number beg end))
  73. (declare-function wl-summary-line-from "ext:wl-summary" ())
  74. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  75. (declare-function wl-summary-message-number "ext:wl-summary" ())
  76. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  77. (declare-function wl-summary-registered-temp-mark "ext:wl-action" (number))
  78. (declare-function wl-folder-goto-folder-subr "ext:wl-folder"
  79. (&optional folder sticky))
  80. (declare-function wl-folder-get-petname "ext:wl-folder" (name))
  81. (declare-function wl-folder-get-entity-from-buffer "ext:wl-folder"
  82. (&optional getid))
  83. (declare-function wl-folder-buffer-group-p "ext:wl-folder")
  84. (defvar wl-init)
  85. (defvar wl-summary-buffer-elmo-folder)
  86. (defvar wl-summary-buffer-folder-name)
  87. (defvar wl-folder-group-regexp)
  88. (defvar wl-auto-check-folder-name)
  89. (defvar elmo-nntp-default-server)
  90. (defconst org-wl-folder-types
  91. '(("%" . imap) ("-" . nntp) ("+" . mh) ("=" . spool)
  92. ("$" . archive) ("&" . pop) ("@" . shimbun) ("[" . search)
  93. ("*" . multi) ("/" . filter) ("|" . pipe) ("'" . internal))
  94. "List of folder indicators. See Wanderlust manual, section 3.")
  95. ;; Install the link type
  96. (org-add-link-type "wl" 'org-wl-open)
  97. (add-hook 'org-store-link-functions 'org-wl-store-link)
  98. ;; Implementation
  99. (defun org-wl-folder-type (folder)
  100. "Return symbol that indicates the type of FOLDER.
  101. FOLDER is the wanderlust folder name. The first character of the
  102. folder name determines the folder type."
  103. (let* ((indicator (substring folder 0 1))
  104. (type (cdr (assoc indicator org-wl-folder-types))))
  105. ;; maybe access or file folder
  106. (when (not type)
  107. (setq type
  108. (cond
  109. ((and (>= (length folder) 5)
  110. (string= (substring folder 0 5) "file:"))
  111. 'file)
  112. ((and (>= (length folder) 7)
  113. (string= (substring folder 0 7) "access:"))
  114. 'access)
  115. (t
  116. nil))))
  117. type))
  118. (defun org-wl-message-field (field entity)
  119. "Return content of FIELD in ENTITY.
  120. FIELD is a symbol of a rfc822 message header field.
  121. ENTITY is a message entity."
  122. (let ((content (elmo-message-entity-field entity field 'string)))
  123. (if (listp content) (car content) content)))
  124. (defun org-wl-store-link ()
  125. "Store a link to a WL message or folder."
  126. (unless (eobp)
  127. (cond
  128. ((memq major-mode '(wl-summary-mode mime-view-mode))
  129. (org-wl-store-link-message))
  130. ((eq major-mode 'wl-folder-mode)
  131. (org-wl-store-link-folder))
  132. (t
  133. nil))))
  134. (defun org-wl-store-link-folder ()
  135. "Store a link to a WL folder."
  136. (let* ((folder (wl-folder-get-entity-from-buffer))
  137. (petname (wl-folder-get-petname folder))
  138. (link (org-make-link "wl:" folder)))
  139. (save-excursion
  140. (beginning-of-line)
  141. (unless (and (wl-folder-buffer-group-p)
  142. (looking-at wl-folder-group-regexp))
  143. (org-store-link-props :type "wl" :description petname
  144. :link link)
  145. link))))
  146. (defun org-wl-store-link-message ()
  147. "Store a link to a WL message."
  148. (save-excursion
  149. (let ((buf (if (eq major-mode 'wl-summary-mode)
  150. (current-buffer)
  151. (and (boundp 'wl-message-buffer-cur-summary-buffer)
  152. wl-message-buffer-cur-summary-buffer))))
  153. (when buf
  154. (with-current-buffer buf
  155. (let* ((msgnum (wl-summary-message-number))
  156. (mark-info (wl-summary-registered-temp-mark msgnum))
  157. (folder-name
  158. (if (and org-wl-link-to-refile-destination
  159. mark-info
  160. (equal (nth 1 mark-info) "o")) ; marked as refile
  161. (nth 2 mark-info)
  162. wl-summary-buffer-folder-name))
  163. (folder-type (org-wl-folder-type folder-name))
  164. (wl-message-entity
  165. (if (fboundp 'elmo-message-entity)
  166. (elmo-message-entity
  167. wl-summary-buffer-elmo-folder msgnum)
  168. (elmo-msgdb-overview-get-entity
  169. msgnum (wl-summary-buffer-msgdb))))
  170. (message-id
  171. (org-wl-message-field 'message-id wl-message-entity))
  172. (message-id-no-brackets
  173. (org-remove-angle-brackets message-id))
  174. (from (org-wl-message-field 'from wl-message-entity))
  175. (to (org-wl-message-field 'to wl-message-entity))
  176. (xref (org-wl-message-field 'xref wl-message-entity))
  177. (subject (org-wl-message-field 'subject wl-message-entity))
  178. (date (org-wl-message-field 'date wl-message-entity))
  179. (date-ts (and date (format-time-string
  180. (org-time-stamp-format t)
  181. (date-to-time date))))
  182. (date-ts-ia (and date (format-time-string
  183. (org-time-stamp-format t t)
  184. (date-to-time date))))
  185. desc link)
  186. ;; remove text properties of subject string to avoid possible bug
  187. ;; when formatting the subject
  188. ;; (Emacs bug #5306, fixed)
  189. (set-text-properties 0 (length subject) nil subject)
  190. ;; maybe remove filter condition
  191. (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
  192. (while (eq (org-wl-folder-type folder-name) 'filter)
  193. (setq folder-name
  194. (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
  195. ;; maybe create http link
  196. (cond
  197. ((and (eq folder-type 'shimbun)
  198. org-wl-shimbun-prefer-web-links xref)
  199. (org-store-link-props :type "http" :link xref :description subject
  200. :from from :to to :message-id message-id
  201. :message-id-no-brackets message-id-no-brackets
  202. :subject subject))
  203. ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
  204. (setq link
  205. (format
  206. (if (string-match "gmane\\." folder-name)
  207. "http://mid.gmane.org/%s"
  208. "http://groups.google.com/groups/search?as_umsgid=%s")
  209. (org-fixup-message-id-for-http message-id)))
  210. (org-store-link-props :type "http" :link link :description subject
  211. :from from :to to :message-id message-id
  212. :message-id-no-brackets message-id-no-brackets
  213. :subject subject))
  214. (t
  215. (org-store-link-props :type "wl" :from from :to to
  216. :subject subject :message-id message-id
  217. :message-id-no-brackets message-id-no-brackets)
  218. (setq desc (org-email-link-description))
  219. (setq link (org-make-link "wl:" folder-name "#" message-id-no-brackets))
  220. (org-add-link-props :link link :description desc)))
  221. (when date
  222. (org-add-link-props :date date :date-timestamp date-ts
  223. :date-timestamp-inactive date-ts-ia))
  224. (or link xref)))))))
  225. (defun org-wl-open-nntp (path)
  226. "Follow the nntp: link specified by PATH."
  227. (let* ((spec (split-string path "/"))
  228. (server (split-string (nth 2 spec) "@"))
  229. (group (nth 3 spec))
  230. (article (nth 4 spec)))
  231. (org-wl-open
  232. (concat "-" group ":" (if (cdr server)
  233. (car (split-string (car server) ":"))
  234. "")
  235. (if (string= elmo-nntp-default-server (nth 2 spec))
  236. ""
  237. (concat "@" (or (cdr server) (car server))))
  238. (if article (concat "#" article) "")))))
  239. (defun org-wl-open (path)
  240. "Follow the WL message link specified by PATH.
  241. When called with one prefix, open message in namazu search folder
  242. with `org-wl-namazu-default-index' as search index. When called
  243. with two prefixes or `org-wl-namazu-default-index' is nil, ask
  244. for namazu index."
  245. (require 'wl)
  246. (let ((wl-auto-check-folder-name
  247. (if org-wl-disable-folder-check
  248. 'none
  249. wl-auto-check-folder-name)))
  250. (unless wl-init (wl))
  251. ;; XXX: The imap-uw's MH folder names start with "%#".
  252. (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
  253. (error "Error in Wanderlust link"))
  254. (let ((folder (match-string 1 path))
  255. (article (match-string 3 path)))
  256. ;; maybe open message in namazu search folder
  257. (when current-prefix-arg
  258. (setq folder (concat "[" article "]"
  259. (if (and (equal current-prefix-arg '(4))
  260. org-wl-namazu-default-index)
  261. org-wl-namazu-default-index
  262. (read-directory-name "Namazu index: ")))))
  263. (if (not (elmo-folder-exists-p (org-no-warnings
  264. (wl-folder-get-elmo-folder folder))))
  265. (error "No such folder: %s" folder))
  266. (let ((old-buf (current-buffer))
  267. (old-point (point-marker)))
  268. (wl-folder-goto-folder-subr folder)
  269. (with-current-buffer old-buf
  270. ;; XXX: `wl-folder-goto-folder-subr' moves point to the
  271. ;; beginning of the current line. So, restore the point
  272. ;; in the old buffer.
  273. (goto-char old-point))
  274. (when article
  275. (if (org-string-match-p "@" article)
  276. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
  277. article))
  278. (or (wl-summary-jump-to-msg (string-to-number article))
  279. (error "No such message: %s" article)))
  280. (wl-summary-redisplay))))))
  281. (provide 'org-wl)
  282. ;;; org-wl.el ends here