Created
January 12, 2015 18:01
-
-
Save andreasjansson/405128cf65effc1430f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require 'cl) | |
| (defun new-shell () | |
| "Create new shells with consecutive names *shell-1*, *shell-2*, etc. | |
| Remove any stale shells (shells without a live process)" | |
| (interactive) | |
| (let* ((regex "^\\*shell-\\([0-9]+\\)\\*$") | |
| (shell-buffers (loop for buffer in (buffer-list) | |
| if (string-match regex (buffer-name buffer)) | |
| collect buffer)) | |
| (max-buffer-num) (active-shell-buffers) (new-num)) | |
| (loop for buffer in shell-buffers | |
| if (not (get-buffer-process buffer)) | |
| do (kill-buffer buffer)) | |
| (setq active-shell-buffers (loop for buffer in shell-buffers | |
| if (buffer-live-p buffer) | |
| collect buffer)) | |
| (setq max-buffer-num (loop for buffer in active-shell-buffers | |
| for buffer-name = (buffer-name buffer) | |
| for buffer-num-s = (progn | |
| (string-match regex buffer-name) | |
| (match-string 1 buffer-name)) | |
| for buffer-num = (string-to-number buffer-num-s) | |
| maximize buffer-num)) | |
| (setq new-num (1+ (or max-buffer-num 0))) | |
| (shell) | |
| (rename-buffer (format "*shell-%d*" new-num)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment