org-wl.el 11 KB

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