ol-rmail.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; ol-rmail.el --- Links to Rmail Messages -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; URL: 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-macs)
  27. (org-assert-version)
  28. (require 'ol)
  29. ;; Declare external functions and variables
  30. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  31. (declare-function rmail-what-message "rmail" (&optional pos))
  32. (declare-function rmail-toggle-header "rmail" (&optional arg))
  33. (declare-function rmail "rmail" (&optional file-name-arg))
  34. (declare-function rmail-widen "rmail" ())
  35. (defvar rmail-current-message) ; From rmail.el
  36. (defvar rmail-header-style) ; From rmail.el
  37. (defvar rmail-file-name) ; From rmail.el
  38. ;; Install the link type
  39. (org-link-set-parameters "rmail"
  40. :follow #'org-rmail-open
  41. :store #'org-rmail-store-link)
  42. ;; Implementation
  43. (defun org-rmail-store-link ()
  44. "Store a link to an Rmail folder or message."
  45. (when (or (eq major-mode 'rmail-mode)
  46. (eq major-mode 'rmail-summary-mode))
  47. (save-window-excursion
  48. (save-restriction
  49. (when (eq major-mode 'rmail-summary-mode)
  50. (rmail-show-message rmail-current-message))
  51. (when (fboundp 'rmail-narrow-to-non-pruned-header)
  52. (rmail-narrow-to-non-pruned-header))
  53. (when (eq rmail-header-style 'normal)
  54. (rmail-toggle-header -1))
  55. (let* ((folder buffer-file-name)
  56. (message-id (mail-fetch-field "message-id"))
  57. (from (mail-fetch-field "from"))
  58. (to (mail-fetch-field "to"))
  59. (subject (mail-fetch-field "subject"))
  60. (date (mail-fetch-field "date"))
  61. desc link)
  62. (org-link-store-props
  63. :type "rmail" :from from :to to :date date
  64. :subject subject :message-id message-id)
  65. (setq message-id (org-unbracket-string "<" ">" message-id))
  66. (setq desc (org-link-email-description))
  67. (setq link (concat "rmail:" folder "#" message-id))
  68. (org-link-add-props :link link :description desc)
  69. (rmail-show-message rmail-current-message)
  70. link)))))
  71. (defun org-rmail-open (path _)
  72. "Follow an Rmail message link to the specified PATH."
  73. (let (folder article)
  74. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  75. (error "Error in Rmail link"))
  76. (setq folder (match-string 1 path)
  77. article (match-string 3 path))
  78. (org-rmail-follow-link folder article)))
  79. (defun org-rmail-follow-link (folder article)
  80. "Follow an Rmail link to FOLDER and ARTICLE."
  81. (require 'rmail)
  82. (cond ((null article) (setq article ""))
  83. ((stringp article)
  84. (setq article (org-link-add-angle-brackets article)))
  85. (t (user-error "Wrong RMAIL link format")))
  86. (let (message-number)
  87. (save-excursion
  88. (save-window-excursion
  89. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  90. (setq message-number
  91. (save-restriction
  92. (rmail-widen)
  93. (goto-char (point-max))
  94. (if (re-search-backward
  95. (concat "^Message-ID:\\s-+" (regexp-quote article))
  96. nil t)
  97. (rmail-what-message))))))
  98. (if message-number
  99. (progn
  100. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  101. (rmail-show-message message-number)
  102. message-number)
  103. (error "Message not found"))))
  104. (provide 'ol-rmail)
  105. ;;; ol-rmail.el ends here