mu4e: clean up creating html files

Clean up the creation of html files a bit, and automatically clean them
up after a short while, so we don't clutter /tmp.

Refactor the html-generating actions, so we don't repeat ourselves too
much.
This commit is contained in:
djcb 2016-02-20 11:21:24 +02:00
parent 392880233f
commit ace2f2ad2c
3 changed files with 42 additions and 40 deletions

View File

@ -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 "<head><meta charset=\"UTF-8\"></head>\n")
(insert (or html (concat "<pre>" txt "</pre>")))
(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 "<head><meta charset=\"UTF-8\"></head>\n")
(insert (or html (concat "<pre>" txt "</pre>")))
(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 "<head><meta charset=\"UTF-8\"></head>\n")
(insert (or html (concat "<pre>" txt "</pre>")))
(write-file tmpfile)
(xwidget-webkit-browse-url (concat "file://" tmpfile) t))))
(xwidget-webkit-browse-url
(concat "file://" (mu4e~write-body-to-html msg)) t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -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'")))

View File

@ -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