org-wl.el 11 KB

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