1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
;;; multi-eshell.el --- multi-eshell -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Thanos Apollo
;; Author: Thanos Apollo <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(defgroup multi-eshell nil
"Simple support for having multiple shells open."
:group 'languages)
(defcustom multi-eshell-shell-function '(eshell)
"Command called to create shell"
:group 'multi-eshell)
(defcustom multi-eshell-name "*eshell*" "The name of the buffer opened by the shell command."
:type 'string
:group 'multi-eshell)
(defun multi-eshell-function ()
"This function opens the appropriate shell."
(eval multi-eshell-shell-function))
(defvar multi-eshell-ring (make-ring 100)
"This stores a bunch of buffers, which are shells created by multi-eshell." )
(setq multi-eshell-index 0 )
(defvar multi-eshell-last-buffer nil)
(defun multi-eshell-is-current-buffer-current-multi-eshell (&optional ignored)
"Checks if current buffer is the current multi-eshell."
(eq (current-buffer) (ring-ref multi-eshell-ring multi-eshell-index)))
(defun multi-eshell-switch-to-current-shell (&optional ignored)
"Switch to shell buffer."
(if (buffer-live-p (ring-ref multi-eshell-ring multi-eshell-index))
(switch-to-buffer (ring-ref multi-eshell-ring multi-eshell-index))))
(defun multi-eshell-current-shell (&optional ignored)
"Returns the current multi-eshell."
(ring-ref multi-eshell-ring multi-eshell-index))
(defun multi-eshell-switch-to-next-live-shell (&optional ignored)
"Switches to the next live shell. Creates one if none exists."
(interactive "p")
(let ((still-looking t)
(empty nil))
(while (and still-looking (not empty))
(if (ring-empty-p multi-eshell-ring)
(progn
(setq empty t)
(multi-eshell 1))
(progn
(if (buffer-live-p (ring-ref multi-eshell-ring multi-eshell-index))
(progn
(setq multi-eshell-index (+ multi-eshell-index 1))
(switch-to-buffer (ring-ref multi-eshell-ring multi-eshell-index))
(setq still-looking nil))
(ring-remove multi-eshell-ring multi-eshell-index)))))))
;;;###autoload
(defun multi-eshell-go-back (&optional ignored)
"Switch to buffer multi-eshell-last-buffer."
(interactive "p")
(if (buffer-live-p multi-eshell-last-buffer)
(switch-to-buffer multi-eshell-last-buffer)
(message "Last buffer visited before multi-eshell is gone. Nothing to go back to..")))
;;;###autoload
(defun multi-eshell-switch (&optional ignored)
"If current buffer is not an multi-eshell, switch to current multi-eshell buffer. Otherwise, switch to next multi-eshell buffer."
(interactive "p")
(progn
(setq multi-eshell-last-buffer (current-buffer))
(let ((still-looking t)
(empty nil))
(if (ring-empty-p multi-eshell-ring)
(multi-eshell 1)
(if (and (buffer-live-p (multi-eshell-current-shell) )
(not (eq (multi-eshell-current-shell) (current-buffer))))
(switch-to-buffer (multi-eshell-current-shell))
(multi-eshell-switch-to-next-live-shell))))))
;;;###autoload
(defun multi-eshell (&optional numshells)
"Creates a shell buffer. If one already exists, this creates a new buffer, with the name '*shell*<n>', where n is chosen by the function generate-new-buffer-name."
(interactive "p")
(progn
(setq multi-eshell-last-buffer (current-buffer))
(dotimes (i (if-void 'numshells 1) nil)
(let ( (tempname (generate-new-buffer-name "*tempshell*"))
(new-buff-name (generate-new-buffer-name multi-eshell-name))
(localdir default-directory))
(if (eq (get-buffer multi-eshell-name) nil)
(progn
(multi-eshell-function)
(ring-insert multi-eshell-ring (current-buffer))
(setq multi-eshell-index (+ multi-eshell-index 1)))
(progn
(interactive)
(multi-eshell-function)
(rename-buffer tempname)
(multi-eshell-function)
(rename-buffer new-buff-name )
(switch-to-buffer tempname)
(rename-buffer multi-eshell-name)
(switch-to-buffer new-buff-name)
(ring-insert multi-eshell-ring (current-buffer) )
(setq multi-eshell-index (+ multi-eshell-index 1))))))))
(defun shell-with-name (name)
"Creates a shell with name given by the first argument, and switches to it. If a buffer with name already exists, we simply switch to it."
(let ((buffer-of-name (get-buffer name))
(tempname (generate-new-buffer-name "*tempshell*") ) )
(cond ((bufferp buffer-of-name) ;If the buffer exists, switch to it (assume it is a shell)
(switch-to-buffer name))
((bufferp (get-buffer multi-eshell-name))
(progn
(multi-eshell-function)
(rename-buffer tempname)
(multi-eshell-function)
(rename-buffer name)
(switch-to-buffer tempname)
(rename-buffer multi-eshell-name)
(switch-to-buffer name)))
(t
(progn
(multi-eshell-function)
(rename-buffer name))))))
(provide 'multi-eshell)
;;; multi-eshell.el ends here
|