开发者

Emacs automatically close async output buffers on command completion

I'm currently running an asynchronous emacs command with a fair degree of regularity like this:

(save-window-excursion
  (async-shell-command
    cmd
    (generate-new-buffer "async")))

This works well and all, but it clutters up my emacs 开发者_JAVA百科instance with a whole ton of async<5> and async<11> buffers. How can I automatically kill these buffers when their corresponding asynchronous command finishes executing?


While it won't kill them when the command completes, you can have the buffers killed after a period of time - this assumes that the async commands are shotr-lived ( or have a fairly-known runtime). Something like:

(save-window-excursion
  (let ((buf (generate-new-buffer "async")))
    (async-shell-command cmd buf)
    (run-with-timer 10 nil (lambda (buf) (kill-buffer buf)) buf)))


Please have a look at http://news.gmane.org/find-root.php?message_id=%3cloom.20120517T145957%2d51%40post.gmane.org%3e

The second proposal there starts a sentinel along with the shell process. When this sentinel detects the process status 'exit you can kill the process buffer right away or you can start dying-mode for the process buffer as it is proposed in the cited posting.

Within the dying time you can inspect the process output, cancel dying or prolong the life-time of the buffer.

Best regards, Tobias


I'm assuming that (for this particular use-case) you are rarely interested in looking at the output that is put in the "async" buffer and that you just want to prevent the creation of the extraneous buffers. If so, you could do:

(save-window-excursion
  (when (get-buffer "async")
    (kill-buffer "async"))
  (async-shell-command
    cmd
    (generate-new-buffer "async")))

This will kill the "async" buffer prior to running the "async-shell-command" and thus prevent the additional "async" buffers from being created.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜