org-rmail.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ;;; org-rmail.el --- Support for Links to Rmail Messages -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2018 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file implements links to Rmail messages from within Org mode.
  23. ;; Org mode loads this module by default - if this is not what you
  24. ;; want, configure the variable `org-modules'.
  25. ;;; Code:
  26. (require 'org)
  27. ;; Declare external functions and variables
  28. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  29. (declare-function rmail-what-message "rmail" (&optional pos))
  30. (declare-function rmail-toggle-header "rmail" (&optional arg))
  31. (declare-function rmail "rmail" (&optional file-name-arg))
  32. (declare-function rmail-widen "rmail" ())
  33. (defvar rmail-current-message) ; From rmail.el
  34. (defvar rmail-header-style) ; From rmail.el
  35. (defvar rmail-file-name) ; From rmail.el
  36. ;; Install the link type
  37. (org-link-set-parameters "rmail" :follow #'org-rmail-open :store #'org-rmail-store-link)
  38. ;; Implementation
  39. (defun org-rmail-store-link ()
  40. "Store a link to an Rmail folder or message."
  41. (when (or (eq major-mode 'rmail-mode)
  42. (eq major-mode 'rmail-summary-mode))
  43. (save-window-excursion
  44. (save-restriction
  45. (when (eq major-mode 'rmail-summary-mode)
  46. (rmail-show-message rmail-current-message))
  47. (when (fboundp 'rmail-narrow-to-non-pruned-header)
  48. (rmail-narrow-to-non-pruned-header))
  49. (when (eq rmail-header-style 'normal)
  50. (rmail-toggle-header -1))
  51. (let* ((folder buffer-file-name)
  52. (message-id (mail-fetch-field "message-id"))
  53. (from (mail-fetch-field "from"))
  54. (to (mail-fetch-field "to"))
  55. (subject (mail-fetch-field "subject"))
  56. (date (mail-fetch-field "date"))
  57. desc link)
  58. (org-store-link-props
  59. :type "rmail" :from from :to to :date date
  60. :subject subject :message-id message-id)
  61. (setq message-id (org-unbracket-string "<" ">" message-id))
  62. (setq desc (org-email-link-description))
  63. (setq link (concat "rmail:" folder "#" message-id))
  64. (org-add-link-props :link link :description desc)
  65. (rmail-show-message rmail-current-message)
  66. link)))))
  67. (defun org-rmail-open (path)
  68. "Follow an Rmail message link to the specified PATH."
  69. (let (folder article)
  70. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  71. (error "Error in Rmail link"))
  72. (setq folder (match-string 1 path)
  73. article (match-string 3 path))
  74. (org-rmail-follow-link folder article)))
  75. (defun org-rmail-follow-link (folder article)
  76. "Follow an Rmail link to FOLDER and ARTICLE."
  77. (require 'rmail)
  78. (cond ((null article) (setq article ""))
  79. ((stringp article)
  80. (setq article (org-add-angle-brackets article)))
  81. (t (user-error "Wrong RMAIL link format")))
  82. (let (message-number)
  83. (save-excursion
  84. (save-window-excursion
  85. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  86. (setq message-number
  87. (save-restriction
  88. (rmail-widen)
  89. (goto-char (point-max))
  90. (if (re-search-backward
  91. (concat "^Message-ID:\\s-+" (regexp-quote article))
  92. nil t)
  93. (rmail-what-message))))))
  94. (if message-number
  95. (progn
  96. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  97. (rmail-show-message message-number)
  98. message-number)
  99. (error "Message not found"))))
  100. (provide 'org-rmail)
  101. ;;; org-rmail.el ends here