org-mew.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
  2. ;; Copyright (C) 2008-2018 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 not part of GNU Emacs.
  7. ;; This program 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. ;; This program 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. :type 'string)
  59. (defcustom org-mew-use-id-db t
  60. "Use ID database to locate the message if id.db is created."
  61. :group 'org-mew
  62. :type 'boolean)
  63. (defcustom org-mew-subject-alist
  64. (list (cons (concat "^\\(?:\\(?:re\\|fwd?\\): *\\)*"
  65. "\\(?:[[(][a-z0-9._-]+[:,]? [0-9]+[])]\\)? *"
  66. "\\(?:\\(?:re\\|fwd?\\): *\\)*"
  67. "\\(.*\\)[ \t]*")
  68. 1))
  69. "Alist of subject regular expression and matched group number for search."
  70. :group 'org-mew
  71. :type '(repeat (cons (regexp) (integer))))
  72. (defcustom org-mew-capture-inbox-folders nil
  73. "List of inbox folders whose messages need refile marked before capture.
  74. `org-mew-capture' will ask you to put the refile mark on the
  75. message if the message's folder is any of these folders and the
  76. message is not marked. Nil means `org-mew-capture' never ask you
  77. destination folders before capture."
  78. :group 'org-mew
  79. :type '(repeat string))
  80. (defcustom org-mew-capture-guess-alist nil
  81. "Alist of the regular expression of the folder name and the capture
  82. template selection keys.
  83. For example,
  84. '((\"^%emacs-orgmode$\" . \"o\")
  85. (\"\" . \"t\"))
  86. the messages in \"%emacs-orgmode\" folder will be captured with
  87. the capture template associated with \"o\" key, and any other
  88. messages will be captured with the capture template associated
  89. with \"t\" key."
  90. :group 'org-mew
  91. :type '(repeat (cons regexp string)))
  92. ;; Declare external functions and variables
  93. (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
  94. (declare-function mew-case-folder "ext:mew-func" (case folder))
  95. (declare-function mew-folder-path-to-folder
  96. "ext:mew-func" (path &optional has-proto))
  97. (declare-function mew-idstr-to-id-list "ext:mew-header" (idstr &optional rev))
  98. (declare-function mew-folder-remotep "ext:mew-func" (folder))
  99. (declare-function mew-folder-virtualp "ext:mew-func" (folder))
  100. (declare-function mew-header-get-value "ext:mew-header"
  101. (field &optional as-list))
  102. (declare-function mew-init "ext:mew" ())
  103. (declare-function mew-refile-get "ext:mew-refile" (msg))
  104. (declare-function mew-sinfo-get-case "ext:mew-summary" ())
  105. (declare-function mew-summary-diag-global "ext:mew-thread" (id opt who))
  106. (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
  107. (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
  108. (declare-function mew-summary-get-mark "ext:mew-mark" ())
  109. (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
  110. (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
  111. (pattern folder src-msgs))
  112. (declare-function mew-summary-refile "ext:mew-refile" (&optional report))
  113. (declare-function mew-summary-search-msg "ext:mew-const" (msg))
  114. (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
  115. (declare-function mew-summary-visit-folder "ext:mew-summary4"
  116. (folder &optional goend no-ls))
  117. (declare-function mew-window-push "ext:mew" ())
  118. (declare-function mew-expand-folder "ext:mew-func" (folder))
  119. (declare-function mew-case:folder-folder "ext:mew-func" (case:folder))
  120. (declare-function mew "ext:mew" (&optional arg))
  121. (declare-function mew-message-goto-summary "ext:mew-message" ())
  122. (declare-function mew-summary-mode "ext:mew-summary" ())
  123. (defvar mew-init-p)
  124. (defvar mew-mark-afterstep-spec)
  125. (defvar mew-summary-goto-line-then-display)
  126. ;; Install the link type
  127. (org-link-set-parameters "mew" :follow #'org-mew-open :store #'org-mew-store-link)
  128. ;; Implementation
  129. (defun org-mew-store-link ()
  130. "Store a link to a Mew folder or message."
  131. (save-window-excursion
  132. (if (eq major-mode 'mew-message-mode)
  133. (mew-message-goto-summary))
  134. (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
  135. (let ((msgnum (mew-summary-message-number2))
  136. (folder-name (org-mew-folder-name)))
  137. (if (fboundp 'mew-summary-set-message-buffer)
  138. (mew-summary-set-message-buffer folder-name msgnum)
  139. (set-buffer (mew-cache-hit folder-name msgnum t)))
  140. (let* ((message-id (mew-header-get-value "Message-Id:"))
  141. (from (mew-header-get-value "From:"))
  142. (to (mew-header-get-value "To:"))
  143. (date (mew-header-get-value "Date:"))
  144. (subject (mew-header-get-value "Subject:"))
  145. desc link)
  146. (org-store-link-props :type "mew" :from from :to to :date date
  147. :subject subject :message-id message-id)
  148. (setq message-id (org-unbracket-string "<" ">" message-id))
  149. (setq desc (org-email-link-description))
  150. (setq link (concat "mew:" folder-name "#" message-id))
  151. (org-add-link-props :link link :description desc)
  152. link)))))
  153. (defun org-mew-folder-name ()
  154. "Return the folder name of the current message."
  155. (save-window-excursion
  156. (if (eq major-mode 'mew-message-mode)
  157. (mew-message-goto-summary))
  158. (let* ((msgnum (mew-summary-message-number2))
  159. (mark-info (mew-summary-get-mark)))
  160. (if (and org-mew-link-to-refile-destination
  161. (eq mark-info ?o)) ; marked as refile
  162. (mew-case-folder (mew-sinfo-get-case)
  163. (nth 1 (mew-refile-get msgnum)))
  164. (let ((folder-or-path (mew-summary-folder-name)))
  165. (mew-folder-path-to-folder folder-or-path t))))))
  166. (defun org-mew-open (path)
  167. "Follow the Mew message link specified by PATH."
  168. (let (folder message-id)
  169. (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
  170. (setq folder (match-string 1 path))
  171. (setq message-id (match-string 2 path)))
  172. ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
  173. (setq folder (match-string 1 path))
  174. (setq message-id (match-string 4 path)))
  175. ((and org-mew-use-id-db (string-match "\\`#\\(.+\\)" path))
  176. (setq folder nil)
  177. (setq message-id (match-string 1 path)))
  178. (t (error "Error in Mew link")))
  179. (require 'mew)
  180. (mew-window-push)
  181. (unless mew-init-p (mew-init))
  182. (if (null folder)
  183. (progn
  184. (mew t)
  185. (org-mew-open-by-message-id message-id))
  186. (or (org-mew-follow-link folder message-id)
  187. (and org-mew-inbox-folder (not (string= org-mew-inbox-folder folder))
  188. (org-mew-follow-link org-mew-inbox-folder message-id))
  189. (and org-mew-use-id-db
  190. (org-mew-open-by-message-id message-id))
  191. (error "Message not found")))))
  192. (defun org-mew-follow-link (folder message-id)
  193. (unless (org-mew-folder-exists-p folder)
  194. (error "No such folder or wrong folder %s" folder))
  195. (mew-summary-visit-folder folder)
  196. (when message-id
  197. (let ((msgnum (org-mew-get-msgnum folder message-id)))
  198. (when (mew-summary-search-msg msgnum)
  199. (if mew-summary-goto-line-then-display
  200. (mew-summary-display))
  201. t))))
  202. (defun org-mew-folder-exists-p (folder)
  203. (let ((dir (mew-expand-folder folder)))
  204. (cond
  205. ((mew-folder-virtualp folder) (get-buffer folder))
  206. ((null dir) nil)
  207. ((mew-folder-remotep (mew-case:folder-folder folder)) t)
  208. (t (file-directory-p dir)))))
  209. (defun org-mew-get-msgnum (folder message-id)
  210. (if (string-match "\\`[0-9]+\\'" message-id)
  211. message-id
  212. (let* ((pattern (concat "message-id=" message-id))
  213. (msgs (mew-summary-pick-with-mewl pattern folder nil)))
  214. (car msgs))))
  215. (defun org-mew-open-by-message-id (message-id)
  216. "Open message using ID database."
  217. (let ((result (mew-summary-diag-global (format "<%s>" message-id)
  218. "-p" "Message")))
  219. (unless (eq result t)
  220. (error "Message not found"))))
  221. ;; In ~/.mew.el, add the following line:
  222. ;; (define-key mew-summary-mode-map "'" 'org-mew-search)
  223. (defun org-mew-search (&optional arg)
  224. "Show all entries related to the message using `org-search-view'.
  225. It shows entries which contains the message ID, the reference
  226. IDs, or the subject of the message.
  227. With C-u prefix, search for the entries that contains the message
  228. ID or any of the reference IDs. With C-u C-u prefix, search for
  229. the message ID or the last reference ID.
  230. The search phase for the subject is extracted with
  231. `org-mew-subject-alist', which defines the regular expression of
  232. the subject and the group number to extract. You can get rid of
  233. \"Re:\" and some other prefix from the subject text."
  234. (interactive "P")
  235. (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
  236. (let ((last-reference-only (equal arg '(16)))
  237. (by-subject (null arg))
  238. (msgnum (mew-summary-message-number2))
  239. (folder-name (mew-summary-folder-name))
  240. subject message-id references id-list)
  241. (save-window-excursion
  242. (if (fboundp 'mew-summary-set-message-buffer)
  243. (mew-summary-set-message-buffer folder-name msgnum)
  244. (set-buffer (mew-cache-hit folder-name msgnum t)))
  245. (setq subject (mew-header-get-value "Subject:"))
  246. (setq message-id (mew-header-get-value "Message-Id:"))
  247. (setq references (mew-header-get-value "References:")))
  248. (setq id-list (mapcar (lambda (id) (org-unbracket-string "<" ">" id))
  249. (mew-idstr-to-id-list references)))
  250. (if last-reference-only
  251. (setq id-list (last id-list))
  252. (if message-id
  253. (setq id-list (cons (org-unbracket-string "<" ">" message-id)
  254. id-list))))
  255. (when (and by-subject (stringp subject))
  256. (catch 'matched
  257. (mapc (lambda (elem)
  258. (let ((regexp (car elem))
  259. (num (cdr elem)))
  260. (when (string-match regexp subject)
  261. (setq subject (match-string num subject))
  262. (throw 'matched t))))
  263. org-mew-subject-alist))
  264. (setq id-list (cons subject id-list)))
  265. (cond ((null id-list)
  266. (error "No message ID to search"))
  267. ((equal (length id-list) 1)
  268. (org-search-view nil (car id-list)))
  269. (t
  270. (org-search-view nil (format "{\\(%s\\)}"
  271. (mapconcat 'regexp-quote
  272. id-list "\\|"))))))
  273. (delete-other-windows)))
  274. (defun org-mew-capture (arg)
  275. "Guess the capture template from the folder name and invoke `org-capture'.
  276. This selects a capture template in `org-capture-templates' by
  277. searching for capture template selection keys defined in
  278. `org-mew-capture-guess-alist' which are associated with the
  279. regular expression that matches the message's folder name, and
  280. then invokes `org-capture'.
  281. If the message's folder is a inbox folder, you are prompted to
  282. put the refile mark on the message and the capture template is
  283. guessed from the refile destination folder. You can customize
  284. the inbox folders by `org-mew-capture-inbox-folders'.
  285. If ARG is non-nil, this does not guess the capture template but
  286. asks you to select the capture template."
  287. (interactive "P")
  288. (or (not (member (org-mew-folder-name)
  289. org-mew-capture-inbox-folders))
  290. (eq (mew-summary-get-mark) ?o)
  291. (save-window-excursion
  292. (if (eq major-mode 'mew-message-mode)
  293. (mew-message-goto-summary))
  294. (let ((mew-mark-afterstep-spec '((?o 0 0 0 0 0 0 0))))
  295. (mew-summary-refile)))
  296. (error "No refile folder selected"))
  297. (let* ((org-mew-link-to-refile-destination t)
  298. (folder-name (org-mew-folder-name))
  299. (keys (if arg
  300. nil
  301. (org-mew-capture-guess-selection-keys folder-name))))
  302. (org-capture nil keys)))
  303. (defun org-mew-capture-guess-selection-keys (folder-name)
  304. (catch 'found
  305. (let ((alist org-mew-capture-guess-alist))
  306. (while alist
  307. (let ((elem (car alist)))
  308. (if (string-match (car elem) folder-name)
  309. (throw 'found (cdr elem))))
  310. (setq alist (cdr alist))))))
  311. (provide 'org-mew)
  312. ;;; org-mew.el ends here