让 Emacs shell 命令发送桌面通知

我总是使用 Eshell 来与操作系统进行交互,因为它与 Emacs 无缝整合、支持处理 (远程) TRAMP 文件,而且在 Windows 上也能工作得很好。

启动 shell 命令后 (比如耗时严重的构建任务) 我经常会由于切换缓冲区而忘了追踪任务的运行状态。

多亏了 Emacs 的 钩子 机制,你可以配置 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 的规则可以用程序来设置。就我这个情况来看,我只需要当对应的缓冲区不可见时得到通知:

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

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


via: blog.hoetzel.info/post/eshell…

作者:Jürgen Hötzel 选题:lujun9972 译者:lujun9972 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

转载于:https://juejin.im/post/5d06563e6fb9a07ee63f6486

猜你喜欢

转载自blog.csdn.net/weixin_34102807/article/details/93180958