2

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 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?

Derek Thurn
  • 14,953
  • 9
  • 42
  • 64

3 Answers3

2

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)))
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
  • I guess if this is the only way... better than nothing. – Derek Thurn Aug 02 '11 at 21:23
  • With this solution, the buffer is deleted before the user has the opportunity to examine the output of the buffer. Is this the desired behavior? – zev Aug 02 '11 at 21:37
1

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

Tobias
  • 11
  • 1
0

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.

zev
  • 3,480
  • 18
  • 13
  • Sadly, this doesn't work for cases when there are multiple async commands running at the same time, because it will attempt to kill a buffer with a running command, and at any rate new buffers will not be named "async" but "async<2>" or something. – Derek Thurn Aug 02 '11 at 21:07
  • I've modified my answer with an alternative approach that you might prefer. – zev Aug 02 '11 at 21:34
  • Never mind, my alternative approach wasn't any better. I've removed it. – zev Aug 02 '11 at 22:01