TranslateProject/sources/tech/20180831 Get desktop notifications from Emacs shell commands .md
darksun 667a7a9811 选题: 20180831 Get desktop notifications from Emacs shell commands ·
sources/tech/20180831 Get desktop notifications from Emacs shell commands .md
2019-04-27 22:22:55 +08:00

2.6 KiB

Get desktop notifications from Emacs shell commands ·

When interacting with the operating systems I always use Eshell because it integrates seamlessly with Emacs, supports (remote) TRAMP file names and also works nice on Windows.

After starting shell commands (like long running build jobs) I often lose track the task when switching buffers.

Thanks to Emacs hooks mechanism you can customize Emacs to call a elisp function when an external command finishes.

I use John Wiegleys excellent alert package to send desktop notifications:

(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 rules can be setup programmatically. In my case I only want to get notified if the corresponding buffer is not visible:

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

This even works on TRAMP buffers. Below is a screenshot showing a Gnome desktop notification of a failed make command.

../../img/eshell.png


via: https://blog.hoetzel.info/post/eshell-notifications/

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

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