I want to make a function that stops the ESS inferior process (e.g. an R or Julia shell), kills its buffer and deletes its window. ess-inf.el
provides the function ess-quit
to exit the inferior process, so my first attempt was
(defun kill-ess-inf-buffer-and-window ()
"Exit the inferior ESS process, kill its buffer and delete its
window."
(interactive)
(let ((ess-process-buffer (ess-get-process-buffer)))
(with-current-buffer ess-process-buffer ; Otherwise ESS inserts the command for quitting in the source code buffer, if this function is invoked from there.
(ess-quit)
(delete-window (get-buffer-window ess-process-buffer))
(kill-buffer ess-process-buffer))))
but Emacs asks me about killing the buffer because it has a running process. Apparently kill-buffer
runs before ess-quit
has finished its job, so I tried adding a (sleep-for 0.1)
before (kill-buffer ess-process-buffer)
and Emacs didn't ask anymore, but I guess there is a more robust way of doing it.
gnuplot.el
has a function that does for gnuplot shells exactly what I'm trying to do for ESS shells, gnuplot-kill-gnuplot-buffer
:
(defun gnuplot-kill-gnuplot-buffer ()
"Kill the gnuplot process and its display buffers."
(interactive)
(if (and gnuplot-process
(eq (process-status gnuplot-process) 'run)) ;; <SE>
(kill-process gnuplot-process))
(if (and gnuplot-buffer (get-buffer gnuplot-buffer))
(progn
(if (one-window-p) ()
(delete-window (get-buffer-window gnuplot-buffer)))
(kill-buffer gnuplot-buffer)))
(setq gnuplot-process nil
gnuplot-buffer nil))
It uses kill-process
, but I don't know how that function works. Is it a request for a graceful shutdown or a SIGDIEMOTHERF**ER
? I don't want to brutally murder my inferior ESS processes.
(when-let ((proc (get-buffer-process (current-buffer)))) (while (buffer-live-p proc) (sit-for 0.1)))
C-h i g (elisp)Signals to Processes
regardingkill-process
and friends.