org-mew.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
  2. ;; Copyright (C) 2008-2012 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. ;;; Code:
  24. (require 'org)
  25. (defgroup org-mew nil
  26. "Options concerning the Mew link."
  27. :tag "Org Startup"
  28. :group 'org-link)
  29. (defcustom org-mew-link-to-refile-destination t
  30. "Create a link to the refile destination if the message is marked as refile."
  31. :group 'org-mew
  32. :type 'boolean)
  33. ;; Declare external functions and variables
  34. (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
  35. (declare-function mew-case-folder "ext:mew-func" (case folder))
  36. (declare-function mew-header-get-value "ext:mew-header"
  37. (field &optional as-list))
  38. (declare-function mew-init "ext:mew" ())
  39. (declare-function mew-refile-get "ext:mew-refile" (msg))
  40. (declare-function mew-sinfo-get-case "ext:mew-summary" ())
  41. (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
  42. (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
  43. (declare-function mew-summary-get-mark "ext:mew-mark" ())
  44. (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
  45. (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
  46. (pattern folder src-msgs))
  47. (declare-function mew-summary-search-msg "ext:mew-const" (msg))
  48. (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
  49. (declare-function mew-summary-visit-folder "ext:mew-summary4"
  50. (folder &optional goend no-ls))
  51. (declare-function mew-window-push "ext:mew" ())
  52. (defvar mew-init-p)
  53. (defvar mew-summary-goto-line-then-display)
  54. ;; Install the link type
  55. (org-add-link-type "mew" 'org-mew-open)
  56. (add-hook 'org-store-link-functions 'org-mew-store-link)
  57. ;; Implementation
  58. (defun org-mew-store-link ()
  59. "Store a link to a Mew folder or message."
  60. (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
  61. (let* ((msgnum (mew-summary-message-number2))
  62. (mark-info (mew-summary-get-mark))
  63. (folder-name
  64. (if (and org-mew-link-to-refile-destination
  65. (eq mark-info ?o)) ; marked as refile
  66. (mew-case-folder (mew-sinfo-get-case)
  67. (nth 1 (mew-refile-get msgnum)))
  68. (mew-summary-folder-name)))
  69. message-id from to subject desc link date date-ts date-ts-ia)
  70. (save-window-excursion
  71. (if (fboundp 'mew-summary-set-message-buffer)
  72. (mew-summary-set-message-buffer folder-name msgnum)
  73. (set-buffer (mew-cache-hit folder-name msgnum t)))
  74. (setq message-id (mew-header-get-value "Message-Id:"))
  75. (setq from (mew-header-get-value "From:"))
  76. (setq to (mew-header-get-value "To:"))
  77. (setq date (mew-header-get-value "Date:"))
  78. (setq date-ts (and date (format-time-string
  79. (org-time-stamp-format t)
  80. (date-to-time date))))
  81. (setq date-ts-ia (and date (format-time-string
  82. (org-time-stamp-format t t)
  83. (date-to-time date))))
  84. (setq subject (mew-header-get-value "Subject:")))
  85. (org-store-link-props :type "mew" :from from :to to
  86. :subject subject :message-id message-id)
  87. (when date
  88. (org-add-link-props :date date :date-timestamp date-ts
  89. :date-timestamp-inactive date-ts-ia))
  90. (setq message-id (org-remove-angle-brackets message-id))
  91. (setq desc (org-email-link-description))
  92. (setq link (org-make-link "mew:" folder-name
  93. "#" message-id))
  94. (org-add-link-props :link link :description desc)
  95. link)))
  96. (defun org-mew-open (path)
  97. "Follow the Mew message link specified by PATH."
  98. (let (folder msgnum)
  99. (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
  100. (setq folder (match-string 1 path))
  101. (setq msgnum (match-string 2 path)))
  102. ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
  103. (setq folder (match-string 1 path))
  104. (setq msgnum (match-string 4 path)))
  105. (t (error "Error in Mew link")))
  106. (require 'mew)
  107. (mew-window-push)
  108. (unless mew-init-p (mew-init))
  109. (mew-summary-visit-folder folder)
  110. (when msgnum
  111. (if (not (string-match "\\`[0-9]+\\'" msgnum))
  112. (let* ((pattern (concat "message-id=" msgnum))
  113. (msgs (mew-summary-pick-with-mewl pattern folder nil)))
  114. (setq msgnum (car msgs))))
  115. (if (mew-summary-search-msg msgnum)
  116. (if mew-summary-goto-line-then-display
  117. (mew-summary-display))
  118. (error "Message not found")))))
  119. (provide 'org-mew)
  120. ;;; org-mew.el ends here