mu4e-actions/view: Reorganize example actions a bit

The ones that require the "old" view (now or in the near future) are
move to mu4e-view-old.el.
This commit is contained in:
Dirk-Jan C. Binnema 2021-03-22 23:54:13 +02:00
parent 57510dd8a0
commit 33fb6c58cf
2 changed files with 100 additions and 119 deletions

View File

@ -1,6 +1,6 @@
;;; mu4e-actions.el -- part of mu4e, the mu mail user agent -*- lexical-binding: t -*-
;; Copyright (C) 2011-2019 Dirk-Jan C. Binnema
;; Copyright (C) 2011-2021 Dirk-Jan C. Binnema
;; Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;; Maintainer: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
@ -52,124 +52,6 @@ Works for headers view and message-view."
(shell-command-to-string
(concat "wc -l < " (shell-quote-argument (mu4e-message-field msg :path))))))
;;; To PDF
(defvar mu4e-msg2pdf
(let ((exec-path (cons (concat mu4e-builddir "/toys/msg2pdf/") exec-path)))
(locate-file "msg2pdf" exec-path exec-suffixes))
"Path to the msg2pdf toy.")
(defun mu4e-action-view-as-pdf (msg)
"Convert MSG to pdf, then show it.
Works for the message view."
(unless (file-executable-p mu4e-msg2pdf)
(mu4e-error "Program msg2pdf not found; please set `mu4e-msg2pdf'"))
(let* ((pdf
(shell-command-to-string
(concat mu4e-msg2pdf " "
(shell-quote-argument (mu4e-message-field msg :path))
" 2> /dev/null")))
(pdf (and pdf (> (length pdf) 5)
(substring pdf 0 -1)))) ;; chop \n
(unless (and pdf (file-exists-p pdf))
(mu4e-warn "Failed to create PDF file"))
(find-file pdf)))
;;; To HTML
(defun mu4e~action-header-to-html (msg field)
"Convert the FIELD of MSG to an HTML string."
(mapconcat
(lambda(c)
(let* ((name (when (car c)
(replace-regexp-in-string "[[:cntrl:]]" "" (car c))))
(email (when (cdr c)
(replace-regexp-in-string "[[:cntrl:]]" "" (cdr c))))
(addr (if mu4e-view-show-addresses
(if name (format "%s <%s>" name email) email)
(or name email))) ;; name may be nil
;; Escape HTML entities
(addr (replace-regexp-in-string "&" "&amp;" addr))
(addr (replace-regexp-in-string "<" "&lt;" addr))
(addr (replace-regexp-in-string ">" "&gt;" addr)))
addr))
(mu4e-message-field msg field) ", "))
(defun mu4e~write-body-to-html (msg)
"Write MSG's body (either html or text) to a temporary file;
return the filename."
(let* ((html (mu4e-message-field msg :body-html))
(txt (mu4e-message-field msg :body-txt))
(tmpfile (mu4e-make-temp-file "html"))
(attachments (cl-remove-if (lambda (part)
(or (null (plist-get part :attachment))
(null (plist-get part :cid))))
(mu4e-message-field msg :parts))))
(unless (or html txt)
(mu4e-error "No body part for this message"))
(with-temp-buffer
(insert "<head><meta charset=\"UTF-8\"></head>\n")
(insert (concat "<p><strong>From</strong>: "
(mu4e~action-header-to-html msg :from) "</br>"))
(insert (concat "<strong>To</strong>: "
(mu4e~action-header-to-html msg :to) "</br>"))
(insert (concat "<strong>Date</strong>: "
(format-time-string mu4e-view-date-format (mu4e-message-field msg :date)) "</br>"))
(insert (concat "<strong>Subject</strong>: " (mu4e-message-field msg :subject) "</p>"))
(insert (or html (concat "<pre>" txt "</pre>")))
(write-file tmpfile)
;; rewrite attachment urls
(mapc (lambda (attachment)
(goto-char (point-min))
(while (re-search-forward (format "src=\"cid:%s\""
(plist-get attachment :cid)) nil t)
(if (plist-get attachment :temp)
(replace-match (format "src=\"%s\""
(plist-get attachment :temp)))
(replace-match (format "src=\"%s%s\"" temporary-file-directory
(plist-get attachment :name)))
(let ((tmp-attachment-name
(format "%s%s" temporary-file-directory
(plist-get attachment :name))))
(mu4e~proc-extract 'save (mu4e-message-field msg :docid)
(plist-get attachment :index)
mu4e-decryption-policy tmp-attachment-name)
(mu4e-remove-file-later tmp-attachment-name)))))
attachments)
(save-buffer)
tmpfile)))
(defun mu4e-action-view-in-browser (msg)
"View the body of MSG in a web browser.
You can influence the browser to use with the variable
`browse-url-generic-program', and see the discussion of privacy
aspects in `(mu4e) Displaying rich-text messages'."
(browse-url (concat "file://"
(mu4e~write-body-to-html msg))))
(defun mu4e-action-view-with-xwidget (msg)
"View the body of MSG inside xwidget-webkit.
This is only available in Emacs 25+; also see the discussion of
privacy aspects in `(mu4e) Displaying rich-text messages'."
(unless (fboundp 'xwidget-webkit-browse-url)
(mu4e-error "No xwidget support available"))
(xwidget-webkit-browse-url
(concat "file://" (mu4e~write-body-to-html msg)) t))
;;; To speech
(defconst mu4e-text2speech-command "festival --tts"
"Program that speaks out text it receives on standard input.")
(defun mu4e-action-message-to-speech (msg)
"Pronounce MSG's body text using `mu4e-text2speech-command'."
(unless (mu4e-message-field msg :body-txt)
(mu4e-warn "No text body for this message"))
(with-temp-buffer
(insert (mu4e-message-field msg :body-txt))
(shell-command-on-region (point-min) (point-max)
mu4e-text2speech-command)))
;;; Org Helpers
(defvar mu4e-captured-message nil

View File

@ -995,6 +995,105 @@ If MSG is nil, use the message at point."
(local-set-key "q" 'kill-buffer-and-window))
(setq buffer-read-only t))
(select-window win)))
;; Actions that are only available for the old view
;;; To HTML
(defun mu4e~action-header-to-html (msg field)
"Convert the FIELD of MSG to an HTML string."
(mapconcat
(lambda(c)
(let* ((name (when (car c)
(replace-regexp-in-string "[[:cntrl:]]" "" (car c))))
(email (when (cdr c)
(replace-regexp-in-string "[[:cntrl:]]" "" (cdr c))))
(addr (if mu4e-view-show-addresses
(if name (format "%s <%s>" name email) email)
(or name email))) ;; name may be nil
;; Escape HTML entities
(addr (replace-regexp-in-string "&" "&amp;" addr))
(addr (replace-regexp-in-string "<" "&lt;" addr))
(addr (replace-regexp-in-string ">" "&gt;" addr)))
addr))
(mu4e-message-field msg field) ", "))
(defun mu4e~write-body-to-html (msg)
"Write MSG's body (either html or text) to a temporary file;
return the filename."
(let* ((html (mu4e-message-field msg :body-html))
(txt (mu4e-message-field msg :body-txt))
(tmpfile (mu4e-make-temp-file "html"))
(attachments (cl-remove-if (lambda (part)
(or (null (plist-get part :attachment))
(null (plist-get part :cid))))
(mu4e-message-field msg :parts))))
(unless (or html txt)
(mu4e-error "No body part for this message"))
(with-temp-buffer
(insert "<head><meta charset=\"UTF-8\"></head>\n")
(insert (concat "<p><strong>From</strong>: "
(mu4e~action-header-to-html msg :from) "</br>"))
(insert (concat "<strong>To</strong>: "
(mu4e~action-header-to-html msg :to) "</br>"))
(insert (concat "<strong>Date</strong>: "
(format-time-string mu4e-view-date-format (mu4e-message-field msg :date)) "</br>"))
(insert (concat "<strong>Subject</strong>: " (mu4e-message-field msg :subject) "</p>"))
(insert (or html (concat "<pre>" txt "</pre>")))
(write-file tmpfile)
;; rewrite attachment urls
(mapc (lambda (attachment)
(goto-char (point-min))
(while (re-search-forward (format "src=\"cid:%s\""
(plist-get attachment :cid)) nil t)
(if (plist-get attachment :temp)
(replace-match (format "src=\"%s\""
(plist-get attachment :temp)))
(replace-match (format "src=\"%s%s\"" temporary-file-directory
(plist-get attachment :name)))
(let ((tmp-attachment-name
(format "%s%s" temporary-file-directory
(plist-get attachment :name))))
(mu4e~proc-extract 'save (mu4e-message-field msg :docid)
(plist-get attachment :index)
mu4e-decryption-policy tmp-attachment-name)
(mu4e-remove-file-later tmp-attachment-name)))))
attachments)
(save-buffer)
tmpfile)))
(defun mu4e-action-view-in-browser (msg)
"View the body of MSG in a web browser.
You can influence the browser to use with the variable
`browse-url-generic-program', and see the discussion of privacy
aspects in `(mu4e) Displaying rich-text messages'. This is only
available for the old view."
(browse-url (concat "file://" (mu4e~write-body-to-html msg))))
(defun mu4e-action-view-with-xwidget (msg)
"View the body of MSG inside xwidget-webkit.
This is only available in Emacs 25+; also see the discussion of
privacy aspects in `(mu4e) Displaying rich-text messages'."
(unless (fboundp 'xwidget-webkit-browse-url)
(mu4e-error "No xwidget support available"))
(xwidget-webkit-browse-url
(concat "file://" (mu4e~write-body-to-html msg)) t))
;;; To speech
(defconst mu4e-text2speech-command "festival --tts"
"Program that speaks out text it receives on standard input.")
(defun mu4e-action-message-to-speech (msg)
"Pronounce MSG's body text using `mu4e-text2speech-command'."
(unless (mu4e-message-field msg :body-txt)
(mu4e-warn "No text body for this message"))
(with-temp-buffer
(insert (mu4e-message-field msg :body-txt))
(shell-command-on-region (point-min) (point-max)
mu4e-text2speech-command)))
;;;
(provide 'mu4e-view-old)
;;; mu4e-view-old.el ends here