ol-mew.el 14 KB

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