org-mew.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
  2. ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
  3. ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  18. ;;
  19. ;;; Commentary:
  20. ;; This file implements links to Mew messages from within Org-mode.
  21. ;; Org-mode loads this module by default - if this is not what you want,
  22. ;; configure the variable `org-modules'.
  23. ;;
  24. ;; Here is an example of workflow:
  25. ;; In your ~/.mew.el configuration file:
  26. ;;
  27. ;; (define-key mew-summary-mode-map "'" 'org-mew-search)
  28. ;; (eval-after-load "mew-summary"
  29. ;; '(define-key mew-summary-mode-map "\C-o" 'org-mew-capture))
  30. ;; 1. In the Mew's inbox folder, take a glance at new messages to find
  31. ;; a message that requires any action.
  32. ;; 2. If the message is a reply from somebody and associated with the
  33. ;; existing orgmode entry, type M-x `org-mew-search' RET (or press
  34. ;; the ' key simply) to find the entry. If you can find the entry
  35. ;; successfully and think you should start the task right now,
  36. ;; start the task by M-x `org-agenda-clock-in' RET.
  37. ;; 3. If the message is a new message, type M-x `org-mew-capture' RET,
  38. ;; enter the refile folder, and the buffer to capture the message
  39. ;; is shown up (without selecting the template by hand). Then you
  40. ;; can fill the template and type C-c C-c to complete the capture.
  41. ;; Note that you can configure `org-capture-templates' so that the
  42. ;; captured entry has a link to the message.
  43. ;;; Code:
  44. (require 'org)
  45. (defgroup org-mew nil
  46. "Options concerning the Mew link."
  47. :tag "Org Startup"
  48. :group 'org-link)
  49. (defcustom org-mew-link-to-refile-destination t
  50. "Create a link to the refile destination if the message is marked as refile."
  51. :group 'org-mew
  52. :type 'boolean)
  53. (defcustom org-mew-inbox-folder nil
  54. "The folder where new messages are incorporated.
  55. If `org-mew-inbox-folder' is non-nil, `org-mew-open' locates the message
  56. in this inbox folder as well as the folder specified by the link."
  57. :group 'org-mew
  58. :version "24.4"
  59. :package-version '(Org . "8.0")
  60. :type 'string)
  61. (defcustom org-mew-use-id-db t
  62. "Use ID database to locate the message if id.db is created."
  63. :group 'org-mew
  64. :version "24.4"
  65. :package-version '(Org . "8.0")
  66. :type 'boolean)
  67. (defcustom org-mew-subject-alist
  68. (list (cons (concat "^\\(?:\\(?:re\\|fwd?\\): *\\)*"
  69. "\\(?:[[(][a-z0-9._-]+[:,]? [0-9]+[])]\\)? *"
  70. "\\(?:\\(?:re\\|fwd?\\): *\\)*"
  71. "\\(.*\\)[ \t]*")
  72. 1))
  73. "Alist of subject regular expression and matched group number for search."
  74. :group 'org-mew
  75. :version "24.4"
  76. :package-version '(Org . "8.0")
  77. :type '(repeat (cons (regexp) (integer))))
  78. (defcustom org-mew-capture-inbox-folders nil
  79. "List of inbox folders whose messages need refile marked before capture.
  80. `org-mew-capture' will ask you to put the refile mark on the
  81. message if the message's folder is any of these folders and the
  82. message is not marked. Nil means `org-mew-capture' never ask you
  83. destination folders before capture."
  84. :group 'org-mew
  85. :version "24.4"
  86. :package-version '(Org . "8.0")
  87. :type '(repeat string))
  88. (defcustom org-mew-capture-guess-alist nil
  89. "Alist of the regular expression of the folder name and the capture
  90. template selection keys.
  91. For example,
  92. '((\"^%emacs-orgmode$\" . \"o\")
  93. (\"\" . \"t\"))
  94. the messages in \"%emacs-orgmode\" folder will be captured with
  95. the capture template associated with \"o\" key, and any other
  96. messages will be captured with the capture template associated
  97. with \"t\" key."
  98. :group 'org-mew
  99. :version "24.4"
  100. :package-version '(Org . "8.0")
  101. :type '(repeat (cons regexp string)))
  102. ;; Declare external functions and variables
  103. (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
  104. (declare-function mew-case-folder "ext:mew-func" (case folder))
  105. (declare-function mew-folder-path-to-folder
  106. "ext:mew-func" (path &optional has-proto))
  107. (declare-function mew-idstr-to-id-list "ext:mew-header" (idstr &optional rev))
  108. (declare-function mew-folder-remotep "ext:mew-func" (folder))
  109. (declare-function mew-folder-virtualp "ext:mew-func" (folder))
  110. (declare-function mew-header-get-value "ext:mew-header"
  111. (field &optional as-list))
  112. (declare-function mew-init "ext:mew" ())
  113. (declare-function mew-refile-get "ext:mew-refile" (msg))
  114. (declare-function mew-sinfo-get-case "ext:mew-summary" ())
  115. (declare-function mew-summary-diag-global "ext:mew-thread" (id opt who))
  116. (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
  117. (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
  118. (declare-function mew-summary-get-mark "ext:mew-mark" ())
  119. (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
  120. (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
  121. (pattern folder src-msgs))
  122. (declare-function mew-summary-refile "ext:mew-refile" (&optional report))
  123. (declare-function mew-summary-search-msg "ext:mew-const" (msg))
  124. (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
  125. (declare-function mew-summary-visit-folder "ext:mew-summary4"
  126. (folder &optional goend no-ls))
  127. (declare-function mew-window-push "ext:mew" ())
  128. (declare-function mew-expand-folder "ext:mew-func" (folder))
  129. (declare-function mew-case:folder-folder "ext:mew-func" (case:folder))
  130. (declare-function mew "ext:mew" (&optional arg))
  131. (declare-function mew-message-goto-summary "ext:mew-message" ())
  132. (declare-function mew-summary-mode "ext:mew-summary" ())
  133. (defvar mew-init-p)
  134. (defvar mew-mark-afterstep-spec)
  135. (defvar mew-summary-goto-line-then-display)
  136. ;; Install the link type
  137. (org-add-link-type "mew" 'org-mew-open)
  138. (add-hook 'org-store-link-functions 'org-mew-store-link)
  139. ;; Implementation
  140. (defun org-mew-store-link ()
  141. "Store a link to a Mew folder or message."
  142. (save-window-excursion
  143. (if (eq major-mode 'mew-message-mode)
  144. (mew-message-goto-summary))
  145. (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
  146. (let ((msgnum (mew-summary-message-number2))
  147. (folder-name (org-mew-folder-name)))
  148. (if (fboundp 'mew-summary-set-message-buffer)
  149. (mew-summary-set-message-buffer folder-name msgnum)
  150. (set-buffer (mew-cache-hit folder-name msgnum t)))
  151. (let* ((message-id (mew-header-get-value "Message-Id:"))
  152. (from (mew-header-get-value "From:"))
  153. (to (mew-header-get-value "To:"))
  154. (date (mew-header-get-value "Date:"))
  155. (date-ts (and date (format-time-string
  156. (org-time-stamp-format t)
  157. (date-to-time date))))
  158. (date-ts-ia (and date (format-time-string
  159. (org-time-stamp-format t t)
  160. (date-to-time date))))
  161. (subject (mew-header-get-value "Subject:"))
  162. desc link)
  163. (org-store-link-props :type "mew" :from from :to to
  164. :subject subject :message-id message-id)
  165. (when date
  166. (org-add-link-props :date date :date-timestamp date-ts
  167. :date-timestamp-inactive date-ts-ia))
  168. (setq message-id (org-remove-angle-brackets message-id))
  169. (setq desc (org-email-link-description))
  170. (setq link (concat "mew:" folder-name "#" message-id))
  171. (org-add-link-props :link link :description desc)
  172. link)))))
  173. (defun org-mew-folder-name ()
  174. "Return the folder name of the current message."
  175. (save-window-excursion
  176. (if (eq major-mode 'mew-message-mode)
  177. (mew-message-goto-summary))
  178. (let* ((msgnum (mew-summary-message-number2))
  179. (mark-info (mew-summary-get-mark)))
  180. (if (and org-mew-link-to-refile-destination
  181. (eq mark-info ?o)) ; marked as refile
  182. (nth 1 (mew-refile-get msgnum))
  183. (let ((folder-or-path (mew-summary-folder-name)))
  184. (mew-folder-path-to-folder folder-or-path t))))))
  185. (defun org-mew-open (path)
  186. "Follow the Mew message link specified by PATH."
  187. (let (folder message-id)
  188. (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
  189. (setq folder (match-string 1 path))
  190. (setq message-id (match-string 2 path)))
  191. ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
  192. (setq folder (match-string 1 path))
  193. (setq message-id (match-string 4 path)))
  194. ((and org-mew-use-id-db (string-match "\\`#\\(.+\\)" path))
  195. (setq folder nil)
  196. (setq message-id (match-string 1 path)))
  197. (t (error "Error in Mew link")))
  198. (require 'mew)
  199. (mew-window-push)
  200. (unless mew-init-p (mew-init))
  201. (if (null folder)
  202. (progn
  203. (mew t)
  204. (org-mew-open-by-message-id message-id))
  205. (or (org-mew-follow-link folder message-id)
  206. (and org-mew-inbox-folder (not (string= org-mew-inbox-folder folder))
  207. (org-mew-follow-link org-mew-inbox-folder message-id))
  208. (and org-mew-use-id-db
  209. (org-mew-open-by-message-id message-id))
  210. (error "Message not found")))))
  211. (defun org-mew-follow-link (folder message-id)
  212. (unless (org-mew-folder-exists-p folder)
  213. (error "No such folder or wrong folder %s" folder))
  214. (mew-summary-visit-folder folder)
  215. (when message-id
  216. (let ((msgnum (org-mew-get-msgnum folder message-id)))
  217. (when (mew-summary-search-msg msgnum)
  218. (if mew-summary-goto-line-then-display
  219. (mew-summary-display))
  220. t))))
  221. (defun org-mew-folder-exists-p (folder)
  222. (let ((dir (mew-expand-folder folder)))
  223. (cond
  224. ((mew-folder-virtualp folder) (get-buffer folder))
  225. ((null dir) nil)
  226. ((mew-folder-remotep (mew-case:folder-folder folder)) t)
  227. (t (file-directory-p dir)))))
  228. (defun org-mew-get-msgnum (folder message-id)
  229. (if (string-match "\\`[0-9]+\\'" message-id)
  230. message-id
  231. (let* ((pattern (concat "message-id=" message-id))
  232. (msgs (mew-summary-pick-with-mewl pattern folder nil)))
  233. (car msgs))))
  234. (defun org-mew-open-by-message-id (message-id)
  235. "Open message using ID database."
  236. (let ((result (mew-summary-diag-global (format "<%s>" message-id)
  237. "-p" "Message")))
  238. (unless (eq result t)
  239. (error "Message not found"))))
  240. ;; In ~/.mew.el, add the following line:
  241. ;; (define-key mew-summary-mode-map "'" 'org-mew-search)
  242. (defun org-mew-search (&optional arg)
  243. "Show all entries related to the message using `org-search-view'.
  244. It shows entries which contains the message ID, the reference
  245. IDs, or the subject of the message.
  246. With C-u prefix, search for the entries that contains the message
  247. ID or any of the reference IDs. With C-u C-u prefix, search for
  248. the message ID or the last reference ID.
  249. The search phase for the subject is extracted with
  250. `org-mew-subject-alist', which defines the regular expression of
  251. the subject and the group number to extract. You can get rid of
  252. \"Re:\" and some other prefix from the subject text."
  253. (interactive "P")
  254. (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
  255. (let ((last-reference-only (equal arg '(16)))
  256. (by-subject (null arg))
  257. (msgnum (mew-summary-message-number2))
  258. (folder-name (mew-summary-folder-name))
  259. subject message-id references id-list)
  260. (save-window-excursion
  261. (if (fboundp 'mew-summary-set-message-buffer)
  262. (mew-summary-set-message-buffer folder-name msgnum)
  263. (set-buffer (mew-cache-hit folder-name msgnum t)))
  264. (setq subject (mew-header-get-value "Subject:"))
  265. (setq message-id (mew-header-get-value "Message-Id:"))
  266. (setq references (mew-header-get-value "References:")))
  267. (setq id-list (mapcar (lambda (id) (org-remove-angle-brackets id))
  268. (mew-idstr-to-id-list references)))
  269. (if last-reference-only
  270. (setq id-list (last id-list))
  271. (if message-id
  272. (setq id-list (cons (org-remove-angle-brackets message-id)
  273. id-list))))
  274. (when (and by-subject (stringp subject))
  275. (catch 'matched
  276. (mapc (lambda (elem)
  277. (let ((regexp (car elem))
  278. (num (cdr elem)))
  279. (when (string-match regexp subject)
  280. (setq subject (match-string num subject))
  281. (throw 'matched t))))
  282. org-mew-subject-alist))
  283. (setq id-list (cons subject id-list)))
  284. (cond ((null id-list)
  285. (error "No message ID to search."))
  286. ((equal (length id-list) 1)
  287. (org-search-view nil (car id-list)))
  288. (t
  289. (org-search-view nil (format "{\\(%s\\)}"
  290. (mapconcat 'regexp-quote
  291. id-list "\\|"))))))
  292. (delete-other-windows)))
  293. (defun org-mew-capture (arg)
  294. "Guess the capture template from the folder name and invoke `org-capture'.
  295. This selects a capture template in `org-capture-templates' by
  296. searching for capture template selection keys defined in
  297. `org-mew-capture-guess-alist' which are associated with the
  298. regular expression that matches the message's folder name, and
  299. then invokes `org-capture'.
  300. If the message's folder is a inbox folder, you are prompted to
  301. put the refile mark on the message and the capture template is
  302. guessed from the refile destination folder. You can customize
  303. the inbox folders by `org-mew-capture-inbox-folders'.
  304. If ARG is non-nil, this does not guess the capture template but
  305. asks you to select the capture template."
  306. (interactive "P")
  307. (or (not (member (org-mew-folder-name)
  308. org-mew-capture-inbox-folders))
  309. (eq (mew-summary-get-mark) ?o)
  310. (save-window-excursion
  311. (if (eq major-mode 'mew-message-mode)
  312. (mew-message-goto-summary))
  313. (let ((mew-mark-afterstep-spec '((?o 0 0 0 0 0 0 0))))
  314. (mew-summary-refile)))
  315. (error "No refile folder selected."))
  316. (let* ((org-mew-link-to-refile-destination t)
  317. (folder-name (org-mew-folder-name))
  318. (keys (if arg
  319. nil
  320. (org-mew-capture-guess-selection-keys folder-name))))
  321. (org-capture nil keys)))
  322. (defun org-mew-capture-guess-selection-keys (folder-name)
  323. (catch 'found
  324. (let ((alist org-mew-capture-guess-alist))
  325. (while alist
  326. (let ((elem (car alist)))
  327. (if (string-match (car elem) folder-name)
  328. (throw 'found (cdr elem))))
  329. (setq alist (cdr alist))))))
  330. (provide 'org-mew)
  331. ;;; org-mew.el ends here