ol-rmail.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ;; 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 'ol)
  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"
  38. :follow #'org-rmail-open
  39. :store #'org-rmail-store-link)
  40. ;; Implementation
  41. (defun org-rmail-store-link ()
  42. "Store a link to an Rmail folder or message."
  43. (when (or (eq major-mode 'rmail-mode)
  44. (eq major-mode 'rmail-summary-mode))
  45. (save-window-excursion
  46. (save-restriction
  47. (when (eq major-mode 'rmail-summary-mode)
  48. (rmail-show-message rmail-current-message))
  49. (when (fboundp 'rmail-narrow-to-non-pruned-header)
  50. (rmail-narrow-to-non-pruned-header))
  51. (when (eq rmail-header-style 'normal)
  52. (rmail-toggle-header -1))
  53. (let* ((folder buffer-file-name)
  54. (message-id (mail-fetch-field "message-id"))
  55. (from (mail-fetch-field "from"))
  56. (to (mail-fetch-field "to"))
  57. (subject (mail-fetch-field "subject"))
  58. (date (mail-fetch-field "date"))
  59. desc link)
  60. (org-link-store-props
  61. :type "rmail" :from from :to to :date date
  62. :subject subject :message-id message-id)
  63. (setq message-id (org-unbracket-string "<" ">" message-id))
  64. (setq desc (org-link-email-description))
  65. (setq link (concat "rmail:" folder "#" message-id))
  66. (org-link-add-props :link link :description desc)
  67. (rmail-show-message rmail-current-message)
  68. link)))))
  69. (defun org-rmail-open (path _)
  70. "Follow an Rmail message link to the specified PATH."
  71. (let (folder article)
  72. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  73. (error "Error in Rmail link"))
  74. (setq folder (match-string 1 path)
  75. article (match-string 3 path))
  76. (org-rmail-follow-link folder article)))
  77. (defun org-rmail-follow-link (folder article)
  78. "Follow an Rmail link to FOLDER and ARTICLE."
  79. (require 'rmail)
  80. (cond ((null article) (setq article ""))
  81. ((stringp article)
  82. (setq article (org-link-add-angle-brackets article)))
  83. (t (user-error "Wrong RMAIL link format")))
  84. (let (message-number)
  85. (save-excursion
  86. (save-window-excursion
  87. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  88. (setq message-number
  89. (save-restriction
  90. (rmail-widen)
  91. (goto-char (point-max))
  92. (if (re-search-backward
  93. (concat "^Message-ID:\\s-+" (regexp-quote article))
  94. nil t)
  95. (rmail-what-message))))))
  96. (if message-number
  97. (progn
  98. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  99. (rmail-show-message message-number)
  100. message-number)
  101. (error "Message not found"))))
  102. (provide 'ol-rmail)
  103. ;;; ol-rmail.el ends here