diff --git a/mu4e/mu4e-actions.el b/mu4e/mu4e-actions.el index dc5ee985..6109d48f 100644 --- a/mu4e/mu4e-actions.el +++ b/mu4e/mu4e-actions.el @@ -30,6 +30,7 @@ (require 'cl) (require 'ido) +(require 'mu4e-utils) (require 'mu4e-message) (require 'mu4e-meta) @@ -46,7 +47,6 @@ Works for headers view and message-view." - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defvar mu4e-msg2pdf (concat mu4e-builddir "/toys/msg2pdf/msg2pdf") @@ -68,47 +68,36 @@ Works for the message view." (mu4e-warn "Failed to create PDF file")) (find-file pdf))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defun mu4e~write-body-to-html (msg) + "Write the 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"))) + (unless (or html txt) + (mu4e-error "No body part for this message")) + (with-temp-buffer + (insert "\n") + (insert (or html (concat "
" txt "
"))) + (write-file tmpfile) + tmpfile))) + (defun mu4e-action-view-in-browser (msg) - "View the body of the message in a web browser. + "View the body of the message in a browser. You can influence the browser to use with the variable `browse-url-generic-program'." - (let* ((html (mu4e-message-field msg :body-html)) - (txt (mu4e-message-field msg :body-txt)) - (tmpfile (format "%s%x.html" temporary-file-directory (random t)))) - (unless (or html txt) - (mu4e-error "No body part for this message")) - (with-temp-buffer - ;; simplistic -- but note that it's only an example... - (insert "\n") - (insert (or html (concat "
" txt "
"))) - (write-file tmpfile) - (browse-url (concat "file://" tmpfile))))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (browse-url (concat "file://" + (mu4e~write-body-to-html msg)))) - - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun mu4e-action-view-with-xwidget (msg) - "View the body of the message inside xwidget-webkit." + "View the body of the message inside xwidget-webkit. This is +only available in emacs 25+." (unless (fboundp 'xwidget-webkit-browse-url) (mu4e-error "No xwidget support available")) - (let* ((html (mu4e-message-field msg :body-html)) - (txt (mu4e-message-field msg :body-txt)) - (tmpfile (format "%s%x.html" temporary-file-directory (random t)))) - (unless (or html txt) - (mu4e-error "No body part for this message")) - (with-temp-buffer - ;; simplistic -- but note that it's only an example... - (insert "\n") - (insert (or html (concat "
" txt "
"))) - (write-file tmpfile) - (xwidget-webkit-browse-url (concat "file://" tmpfile) t)))) + (xwidget-webkit-browse-url + (concat "file://" (mu4e~write-body-to-html msg)) t)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/mu4e/mu4e-message.el b/mu4e/mu4e-message.el index 9a3cc11b..128eaf7f 100644 --- a/mu4e/mu4e-message.el +++ b/mu4e/mu4e-message.el @@ -178,15 +178,16 @@ be changed by setting `mu4e-view-prefer-html'." txt) ;; otherwise, it there some html? (html + (message "%S" html) (with-temp-buffer (insert html) (cond - ((stringp mu4e-html2text-command) - (let* ((tmp-file (make-temp-file "mu4e-html"))) - (write-region (point-min) (point-max) tmp-file) - (erase-buffer) - (call-process-shell-command mu4e-html2text-command tmp-file t t) - (delete-file tmp-file))) + ((stringp mu4e-html2text-command) + (let* ((tmp-file (mu4e-make-temp-file "html"))) + (write-region (point-min) (point-max) tmp-file) + (erase-buffer) + (call-process-shell-command mu4e-html2text-command tmp-file t t) + (delete-file tmp-file))) ((functionp mu4e-html2text-command) (funcall mu4e-html2text-command)) (t (mu4e-error "Invalid `mu4e-html2text-command'"))) diff --git a/mu4e/mu4e-utils.el b/mu4e/mu4e-utils.el index 6059e3a7..6bb9af5f 100644 --- a/mu4e/mu4e-utils.el +++ b/mu4e/mu4e-utils.el @@ -140,6 +140,18 @@ return the result." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defun mu4e-make-temp-file (ext) + "Create a temporary file with extension EXT. The file will +self-destruct in a few seconds, enough to open it in another +program." + (let ((tmpfile (make-temp-file "mu4e-" nil (concat "." ext)))) + (run-at-time "10 sec" nil + (lambda (fname) (ignore-errors (delete-file fname))) tmpfile) + tmpfile)) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; mu4e-attachment-dir is either a string or a function that takes a filename ;; and the mime-type as argument, either (or both) which can be nil