让 Emacs shell 命令发送桌面通知

发布于 2024-10-27 07:56:48 字数 1633 浏览 22 评论 0

我总是使用 Eshell 来与操作系统进行交互,因为它与 Emacs 无缝整合、支持处理 (远程) TRAMP 文件 而且在 Windows 上也能工作得很好。启动 shell 命令后 (比如耗时严重的构建任务) 我经常会由于切换 buffer 而忘了追踪任务的运行状态。

多亏了 Emacs 的 hooks 机制,你可以配置 Emacs 在某个外部命令完成后调用一个 elisp 函数。我使用 John Wiegleys 所编写的超棒的 alert 包来发送桌面通知:

  (require 'alert)

  (defun eshell-command-alert (process status)
   "Send `alert' with severity based on STATUS when PROCESS finished."
   (let* ((cmd (process-command process))
     (buffer (process-buffer process))
     (msg (format "%s: %s" (mapconcat 'identity cmd " ") status)))
   (if (string-prefix-p "finished" status)
     (alert msg :buffer buffer :severity 'normal)
    (alert msg :buffer buffer :severity 'urgent))))

  (add-hook 'eshell-kill-hook #'eshell-command-alert)

alert 的规则可以用程序来设置. 就我这个情况来看,我只需要当对应的 buffer 不可见时被通知:

  (alert-add-rule :status '(buried) ;only send alert when buffer not visible
       :mode 'eshell-mode
       :style 'notifications)

这甚至对于 TRAMP 也一样生效. 下面这个截屏展示了失败的 make 命令产生的 Gnome 桌面通知。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

別甾虛僞

暂无简介

文章
评论
28 人气
更多

推荐作者

笑脸一如从前

文章 0 评论 0

mnbvcxz

文章 0 评论 0

真是无聊啊

文章 0 评论 0

旧城空念

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文