is there a simple way to bring back mu4e-action-view-with-xwidget?

142 views
Skip to first unread message

AriT93

unread,
Jul 30, 2021, 2:29:09 PM7/30/21
to mu-discuss
prior to the 1.6.0 upgrade I had an action add that called to mu4e-action-view-with-xwidget to display the message.    Now it seem there is a conflict if I require mu4e-view-old  to use that functionality and the new gnus based view.   Is there a way to include view-old and still use the xwidget view?

thank you


Iñigo Serna

unread,
Jul 30, 2021, 2:48:38 PM7/30/21
to mu-di...@googlegroups.com, AriT93
Hi AriT93,
I was fighting with this yesterday… gnus article view uses
=browse-url-of-file= which calls =browse-url=.
Thus we can make the action run a function which temporary set
=browse-url-browser-function= to our own function.

I have different actions which calls next function with different
browsers (firefox, firefox in private window, eww, webkit, etc).

#+begin_src
(defun isr/mu4e--action-view-in-browser (msg &optional browser)
(let ((browse-url-browser-function #'(lambda (url &optional _)
(isr/open-url url browser))))
(mu4e-action-view-in-browser msg)))
#+end_src

Changing the lambda expression above with
=xwidget-webkit-browse-url= should be enough.

Hope it helps,
--
Iñigo Serna

Ari Turetzky

unread,
Jul 30, 2021, 3:05:38 PM7/30/21
to Iñigo Serna, mu-di...@googlegroups.com
Thank you Iñigo!
that seems reasonable, can you had the defun for the
isr/open-url as well please.

inigo...@gmail.com

unread,
Jul 30, 2021, 3:36:16 PM7/30/21
to ari...@gmail.com, mu-di...@googlegroups.com
On 30 July 2021 at 21:03 +02, Ari Turetzky <ari...@gmail.com> wrote:

> Thank you Iñigo!
> that seems reasonable, can you had the defun for the
> isr/open-url as well please.

Well, my use cases are a bit convoluted, I use different browsers
depending on the web page, so I configured a specialized function
to let me select the preferred one in each case.

But note that you don't need all this complexity in the case you
mentioned initially, just use =xwidget-webkit-browse-url=.

Anyway, here you have the code:

#+begin_src emacs-lisp
;;;; open url
(setq browse-url-generic-program "firefox"
browse-url-browser-function 'eww-browse-url)

(defvar isr/open-url-browsers-list '("firefox --private-window" "firefox container"
"firefox" "firefox --new-window"
"eww" "webkit" "external" "qutebrowser"
; "flatpak run org.gnome.Eolie" "vivaldi" "vivaldi --incognito" "xwidget"
"epiphany"
"chromium-freeworld --incognito" "chromium-freeworld")
"Web browsers list.")

(defun isr/open-url (url &optional browser)
"Open URL with BROWSER.
if BROWSER is not present or symbol `external', open with default external configured browser such as firefox
If BROWSER is symbol `eww', open with internal eww browser
if BROWSER is symbol `xwidget', open with Emacs 25+ XWidget
if BROWSER is symbol `webkit', open with Emacs 28+ webkit module
if BROWSER is symbol `select', ask user for browser
if BROWSER is symbol `firefox container', open in firefox container
if BROWSER is a string, open with that browser."
(when (stringp url)
(setq url (isr/sanitize-url url))
(catch 'exit
(when (eq browser 'select)
;; https://emacs.stackexchange.com/questions/8115/make-completing-read-respect-sorting-order-of-a-collection/8122
(setq browser (completing-read "Select web browser to open the url: " isr/open-url-browsers-list))
(unless browser
(message "No browser selected")
(throw 'exit "No browser selected")))
(cond
((or (string= browser "eww") (eq browser 'eww)) (eww-browse-url url t))
((or (string= browser "external") (eq browser 'external)) (browse-url-generic url))
((or (string= browser "xwidget") (eq browser 'xwidget)) (xwidget-webkit-browse-url url t))
((or (string= browser "webkit") (eq browser 'webkit)) (webkit-browse-url url))
((or (string= browser "firefox container") (eq browser 'firefox-container))
(let ((browse-url-generic-program "firefox")
(browse-url-generic-args nil))
(browse-url-generic (concat "ext+container:name=Temp&url=" (url-encode-url url)))))
((stringp browser) (let* ((lst (split-string browser))
(browse-url-generic-program (car lst))
(browse-url-generic-args (cdr lst)))
(browse-url-generic url)))
;; (t (eww-browse-url url)))))
(t (browse-url-generic url))))))

(defun isr/open-url-at-point (&optional browser)
"Open url at point, optionally use BROWSER."
(interactive "P")
(when (equal browser '(4))
(setq browser 'select))
(let ((url (thing-at-point-url-at-point)))
(if (stringp url)
(isr/open-url url browser)
(message "No URL at point"))))

(defun isr/sanitize-url (url)
"Use saner services."
(interactive)
; twitr.gq, nitter.net, nitter.cc, nitter.eu, nitter.fdn.fr
; https://github.com/zedeus/nitter/wiki/Instances
(string-replace "twitter.com" "nitter.eu"
(string-replace "reddit.com" "teddit.net" url)))

(global-unset-key "\C-j")
(global-set-key "\C-j" 'isr/open-url-at-point)
(global-set-key "\M-j" #'(lambda () (interactive) (isr/open-url-at-point "eww")))
#+end_src


Kind regards,
--
Iñigo Serna

Ari Turetzky

unread,
Aug 3, 2021, 12:15:59 PM8/3/21
to inigo...@gmail.com, mu-di...@googlegroups.com
Thanks again for this, I was able to get this working but then decided
to see if I could just extract the xwidget view out of =mu4e-view-old=
library to its own. it turned out to just be 3 functions. I have them
as a separate file that I require currently but will try to get a PR
together for consideration this week as well.

#+BEGIN_SRC elisp
(require 'mu4e-proc)
(require 'mu4e-view-common)
(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))

(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-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) ", "))
#+END_SRC
Reply all
Reply to author
Forward
0 new messages