config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ol-rmail.el (4299B)


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