Bury shell buffers


Last time I refactored my windows restoring configuration in order to pass the prefix argument around. This is a nice solution to leverage my wrappers for commands like shell, but I can push it a little further.

Here I showed you a couple of functions to restore my windows configuration when I exit a fullframed buffer.

(defun mu-restore-window-configuration (config)
  "Kill current buffer and restore window configuration in CONFIG."
  (interactive)
  (kill-this-buffer)
  (set-window-configuration config))

(defun mu-pop-window-configuration ()
  "Restore previous window configuration and clear current window."
  (interactive)
  (let ((config (pop mu-saved-window-configuration)))
    (if config
        (mu-restore-window-configuration config)
      (if (> (length (window-list)) 1)
          (delete-window)
        (bury-buffer)))))

However, with these functions the buffer I am leaving gets killed. This is fine when moving away from, say, Ibuffer or Paradox, but it’s less than optimal for shell buffers which may be running a long-run process.

Once again, the prefix argument can help:

(defun mu--restore-window-configuration (config &optional bury-buffer)
  "Kill current buffer and restore the window configuration in CONFIG.
With BURY-BUFFER bury the buffer instead of killing it."
  (if bury-buffer
      (bury-buffer)
    (kill-this-buffer))
  (set-window-configuration config))

(defun mu-pop-window-configuration (&optional bury-buffer)
  "Restore the previous window configuration and clear current window.
With BURY-BUFFER bury the buffer instead of killing it."
  (interactive "P")
  (let ((config (pop mu--saved-window-configuration)))
    (if config
        (mu--restore-window-configuration config bury-buffer)
      (if (> (length (window-list)) 1)
          (delete-window)
        (bury-buffer)))))

Now that the functions can make use of the prefix argument, I just need to call them properly from a shell buffer:

(defun mu-shell-bury ()
  "Bury the shell window."
  (interactive)
  (let ((current-prefix-arg 4))
    (call-interactively #'mu-pop-window-configuration)))

I’ve bound this to C-c C-q, so I can still kill a shell buffer using C-x k (kill-this-buffer in my configuration) if needed.