org-wl.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. :version "24.1"
  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. :version "24.1"
  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. :version "24.1"
  52. :group 'org-wl)
  53. (defcustom org-wl-disable-folder-check t
  54. "Disable check for new messages when open a link."
  55. :type 'boolean
  56. :version "24.1"
  57. :group 'org-wl)
  58. (defcustom org-wl-namazu-default-index nil
  59. "Default namazu search index."
  60. :type 'directory
  61. :version "24.1"
  62. :group 'org-wl)
  63. ;; Declare external functions and variables
  64. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  65. (declare-function elmo-message-entity-field "ext:elmo-msgdb"
  66. (entity field &optional type))
  67. (declare-function elmo-message-field "ext:elmo"
  68. (folder number field &optional type) t)
  69. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (id msgdb) t)
  70. ;; Backward compatibility to old version of wl
  71. (declare-function wl "ext:wl" () t)
  72. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" () t)
  73. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary"
  74. (&optional id))
  75. (declare-function wl-summary-jump-to-msg "ext:wl-summary"
  76. (&optional number beg end))
  77. (declare-function wl-summary-line-from "ext:wl-summary" ())
  78. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  79. (declare-function wl-summary-message-number "ext:wl-summary" ())
  80. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  81. (declare-function wl-summary-registered-temp-mark "ext:wl-action" (number))
  82. (declare-function wl-folder-goto-folder-subr "ext:wl-folder"
  83. (&optional folder sticky))
  84. (declare-function wl-folder-get-petname "ext:wl-folder" (name))
  85. (declare-function wl-folder-get-entity-from-buffer "ext:wl-folder"
  86. (&optional getid))
  87. (declare-function wl-folder-buffer-group-p "ext:wl-folder")
  88. (defvar wl-init)
  89. (defvar wl-summary-buffer-elmo-folder)
  90. (defvar wl-summary-buffer-folder-name)
  91. (defvar wl-folder-group-regexp)
  92. (defvar wl-auto-check-folder-name)
  93. (defvar elmo-nntp-default-server)
  94. (defconst org-wl-folder-types
  95. '(("%" . imap) ("-" . nntp) ("+" . mh) ("=" . spool)
  96. ("$" . archive) ("&" . pop) ("@" . shimbun) ("[" . search)
  97. ("*" . multi) ("/" . filter) ("|" . pipe) ("'" . internal))
  98. "List of folder indicators. See Wanderlust manual, section 3.")
  99. ;; Install the link type
  100. (org-add-link-type "wl" 'org-wl-open)
  101. (add-hook 'org-store-link-functions 'org-wl-store-link)
  102. ;; Implementation
  103. (defun org-wl-folder-type (folder)
  104. "Return symbol that indicates the type of FOLDER.
  105. FOLDER is the wanderlust folder name. The first character of the
  106. folder name determines the folder type."
  107. (let* ((indicator (substring folder 0 1))
  108. (type (cdr (assoc indicator org-wl-folder-types))))
  109. ;; maybe access or file folder
  110. (when (not type)
  111. (setq type
  112. (cond
  113. ((and (>= (length folder) 5)
  114. (string= (substring folder 0 5) "file:"))
  115. 'file)
  116. ((and (>= (length folder) 7)
  117. (string= (substring folder 0 7) "access:"))
  118. 'access)
  119. (t
  120. nil))))
  121. type))
  122. (defun org-wl-message-field (field entity)
  123. "Return content of FIELD in ENTITY.
  124. FIELD is a symbol of a rfc822 message header field.
  125. ENTITY is a message entity."
  126. (let ((content (elmo-message-entity-field entity field 'string)))
  127. (if (listp content) (car content) content)))
  128. (defun org-wl-store-link ()
  129. "Store a link to a WL message or folder."
  130. (unless (eobp)
  131. (cond
  132. ((memq major-mode '(wl-summary-mode mime-view-mode))
  133. (org-wl-store-link-message))
  134. ((eq major-mode 'wl-folder-mode)
  135. (org-wl-store-link-folder))
  136. (t
  137. nil))))
  138. (defun org-wl-store-link-folder ()
  139. "Store a link to a WL folder."
  140. (let* ((folder (wl-folder-get-entity-from-buffer))
  141. (petname (wl-folder-get-petname folder))
  142. (link (org-make-link "wl:" folder)))
  143. (save-excursion
  144. (beginning-of-line)
  145. (unless (and (wl-folder-buffer-group-p)
  146. (looking-at wl-folder-group-regexp))
  147. (org-store-link-props :type "wl" :description petname
  148. :link link)
  149. link))))
  150. (defun org-wl-store-link-message ()
  151. "Store a link to a WL message."
  152. (save-excursion
  153. (let ((buf (if (eq major-mode 'wl-summary-mode)
  154. (current-buffer)
  155. (and (boundp 'wl-message-buffer-cur-summary-buffer)
  156. wl-message-buffer-cur-summary-buffer))))
  157. (when buf
  158. (with-current-buffer buf
  159. (let* ((msgnum (wl-summary-message-number))
  160. (mark-info (wl-summary-registered-temp-mark msgnum))
  161. (folder-name
  162. (if (and org-wl-link-to-refile-destination
  163. mark-info
  164. (equal (nth 1 mark-info) "o")) ; marked as refile
  165. (nth 2 mark-info)
  166. wl-summary-buffer-folder-name))
  167. (folder-type (org-wl-folder-type folder-name))
  168. (wl-message-entity
  169. (if (fboundp 'elmo-message-entity)
  170. (elmo-message-entity
  171. wl-summary-buffer-elmo-folder msgnum)
  172. (elmo-msgdb-overview-get-entity
  173. msgnum (wl-summary-buffer-msgdb))))
  174. (message-id
  175. (org-wl-message-field 'message-id wl-message-entity))
  176. (message-id-no-brackets
  177. (org-remove-angle-brackets message-id))
  178. (from (org-wl-message-field 'from wl-message-entity))
  179. (to (org-wl-message-field 'to wl-message-entity))
  180. (xref (org-wl-message-field 'xref wl-message-entity))
  181. (subject (org-wl-message-field 'subject wl-message-entity))
  182. (date (org-wl-message-field 'date wl-message-entity))
  183. (date-ts (and date (format-time-string
  184. (org-time-stamp-format t)
  185. (date-to-time date))))
  186. (date-ts-ia (and date (format-time-string
  187. (org-time-stamp-format t t)
  188. (date-to-time date))))
  189. desc link)
  190. ;; remove text properties of subject string to avoid possible bug
  191. ;; when formatting the subject
  192. ;; (Emacs bug #5306, fixed)
  193. (set-text-properties 0 (length subject) nil subject)
  194. ;; maybe remove filter condition
  195. (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
  196. (while (eq (org-wl-folder-type folder-name) 'filter)
  197. (setq folder-name
  198. (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
  199. ;; maybe create http link
  200. (cond
  201. ((and (eq folder-type 'shimbun)
  202. org-wl-shimbun-prefer-web-links xref)
  203. (org-store-link-props :type "http" :link xref :description subject
  204. :from from :to to :message-id message-id
  205. :message-id-no-brackets message-id-no-brackets
  206. :subject subject))
  207. ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
  208. (setq link
  209. (format
  210. (if (string-match "gmane\\." folder-name)
  211. "http://mid.gmane.org/%s"
  212. "http://groups.google.com/groups/search?as_umsgid=%s")
  213. (org-fixup-message-id-for-http message-id)))
  214. (org-store-link-props :type "http" :link link :description subject
  215. :from from :to to :message-id message-id
  216. :message-id-no-brackets message-id-no-brackets
  217. :subject subject))
  218. (t
  219. (org-store-link-props :type "wl" :from from :to to
  220. :subject subject :message-id message-id
  221. :message-id-no-brackets message-id-no-brackets)
  222. (setq desc (org-email-link-description))
  223. (setq link (org-make-link "wl:" folder-name "#" message-id-no-brackets))
  224. (org-add-link-props :link link :description desc)))
  225. (when date
  226. (org-add-link-props :date date :date-timestamp date-ts
  227. :date-timestamp-inactive date-ts-ia))
  228. (or link xref)))))))
  229. (defun org-wl-open-nntp (path)
  230. "Follow the nntp: link specified by PATH."
  231. (let* ((spec (split-string path "/"))
  232. (server (split-string (nth 2 spec) "@"))
  233. (group (nth 3 spec))
  234. (article (nth 4 spec)))
  235. (org-wl-open
  236. (concat "-" group ":" (if (cdr server)
  237. (car (split-string (car server) ":"))
  238. "")
  239. (if (string= elmo-nntp-default-server (nth 2 spec))
  240. ""
  241. (concat "@" (or (cdr server) (car server))))
  242. (if article (concat "#" article) "")))))
  243. (defun org-wl-open (path)
  244. "Follow the WL message link specified by PATH.
  245. When called with one prefix, open message in namazu search folder
  246. with `org-wl-namazu-default-index' as search index. When called
  247. with two prefixes or `org-wl-namazu-default-index' is nil, ask
  248. for namazu index."
  249. (require 'wl)
  250. (let ((wl-auto-check-folder-name
  251. (if org-wl-disable-folder-check
  252. 'none
  253. wl-auto-check-folder-name)))
  254. (unless wl-init (wl))
  255. ;; XXX: The imap-uw's MH folder names start with "%#".
  256. (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
  257. (error "Error in Wanderlust link"))
  258. (let ((folder (match-string 1 path))
  259. (article (match-string 3 path)))
  260. ;; maybe open message in namazu search folder
  261. (when current-prefix-arg
  262. (setq folder (concat "[" article "]"
  263. (if (and (equal current-prefix-arg '(4))
  264. org-wl-namazu-default-index)
  265. org-wl-namazu-default-index
  266. (read-directory-name "Namazu index: ")))))
  267. (if (not (elmo-folder-exists-p (org-no-warnings
  268. (wl-folder-get-elmo-folder folder))))
  269. (error "No such folder: %s" folder))
  270. (let ((old-buf (current-buffer))
  271. (old-point (point-marker)))
  272. (wl-folder-goto-folder-subr folder)
  273. (with-current-buffer old-buf
  274. ;; XXX: `wl-folder-goto-folder-subr' moves point to the
  275. ;; beginning of the current line. So, restore the point
  276. ;; in the old buffer.
  277. (goto-char old-point))
  278. (when article
  279. (if (org-string-match-p "@" article)
  280. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
  281. article))
  282. (or (wl-summary-jump-to-msg (string-to-number article))
  283. (error "No such message: %s" article)))
  284. (wl-summary-redisplay))))))
  285. (provide 'org-wl)
  286. ;;; org-wl.el ends here