notmuch 使用 xwidget (或者其他自定义浏览器)来打开当前的 email。

作为从 mu4e 迁移到 notmuch 的老用户,我很想念 mu4e 的 mu4e-action-view-with-xwidget。而 notmuch 原生只提供了功能有限的 notmuch-view-part 函数,会强制调用系统默认浏览器打开邮件内容。

这在 Emacs 环境下显得尤为不便 —— 我们明明可以用 Emacs 内置浏览器(xwidget 或 EAF)优雅地查看邮件,却不得不忍受系统自动弹出 Safari/Edge/Chromium 这类外部浏览器。

废话不讲了,直接上解决方案:

(defun mg-notmuch-display-email-in-xwidget ()
  "Display the HTML email content in xwidget-webkit.
This function requires the current MIME part to be of type
text/html. If the content is not HTML, it falls back to calling
`notmuch-show-view-part'.  Similarly, if xwidget support is
unavailable in the current Emacs build, it fallbacks to
`notmuch-show-view-part'."
    (interactive)
    (if-let* ((mime-part (ignore-errors (notmuch-show-current-part-handle)))
              (is-html-mime (equal (caadr mime-part) "text/html"))
              (has-xwidget (featurep 'xwidget-internal)))
            (notmuch-show-apply-to-current-part-handle
             (lambda (handle)
                 (let ((tempf (make-temp-file "notmuch"
                                              nil
                                              ".html"
                                              (with-current-buffer (car handle)
                                                  (buffer-string)))))
                     (xwidget-webkit-browse-url (concat "file://" tempf))
                     (run-with-idle-timer 3 nil #'delete-file tempf))))
        (notmuch-show-view-part)))

(define-key notmuch-show-part-map "x" #'mg-notmuch-display-email-in-xwidget)

使用说明:

  1. 在执行过程中,minibuffer 可能会询问编码格式,这是安全机制所致,直接选择 raw-text 即可
  2. 使用 EAF 浏览器的用户只需将 xwidget-webkit-browse-url 替换为对应的 EAF 函数

这样就能继续享受 Emacs 内浏览邮件的丝滑体验了!

1 个赞

notmuch 相对 m4ue 有什么优势吗

我决定进行迁移,主要有两个原因。

首先,mu4e 的向后兼容性非常糟糕,许多依赖 mu4e 的插件经常在大版本更新后出现问题。比如 org-msg (可以在 emacs 里撰写 outlook 格式的邮件,用于非技术类邮件列表的常规邮件交流很有必要)就被 mu4e breaking change 过。但是 notmuch 配合 org-msg 一直都很稳定。再比如 evil-collection,我还提过 PR 来修 breaking change,后来实在是心累了。

其次,notmuch 提供了 thread conversational view,而 mu4e 只提供了 thread tree view。虽然我曾经在 mu4e 中实现 thread folding,但效果并不理想。即使是 mu4e 1.12 中引入的官方 thread folding 功能也存在诸多问题。

在阅读邮件列表时,经常会遇到包含数十甚至上百条消息的长篇讨论。在这种情况下,将所有交流整合到一个会话视图中,比 mu4e 那样每条消息占据阅读列表一行的方式更加简洁明了。

1 个赞

恰恰相反,我刚换到 mu4e 。。。

围城

哈哈哈。家家都有难念的经。

你要是有兴趣,也可以介绍一下你为什么从 notmuch 切换到 mu4e 的心路历程。也可以给新来的小伙伴想要配置 emacs 邮件纠结选择哪个做一个参考。

我这有在浏览器里面看邮件的代码,可以参考下,也处理了 text 格式的邮件。

感谢老哥的分享。不过我没明白你为什么要自己写一个函数来实现呢。mu4e 和 notmuch 都有内置函数支持直接用浏览器打开邮件呀。

mu4e 的 mu4e-action-view-in-browser 有什么局限呢?

纯 text 的邮件没办法预览,会报 html parts not found, 然后公司里面的告警邮件基本都是 text 。

Ubuntu24.04所携带的webkitgtk3版本高于Emacs30.1所要求的版本上限,所以自己编译的时候xwidget无法开启,请问大家是如何解决的?

Basically, the external library that allows most Xwidgets functionality is no longer compatible with Emacs.

See bug#66068: 30.0.50; xwidget-webkit-browse-url makes Emacs abort for the main email discussion on this.

If you do build Emacs with Xwidgets support using the latest webkit library, Emacs will crash whenever you run anything Xwidgets related.

1 个赞

值得一提的是 emacs-plus 在 macOS 上 xwidget 表现一直稳定,从未出现过问题。

So the core members have to intentionally limit the version of webkitgtk3 library that can be used. Got it, thank you!