1 ;;; multishell.el --- manage interaction with multiple local and remote shells
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc. and Ken Manheimer
5 ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
7 ;; Created: 1999 -- first public availability
9 ;; URL: https://github.com/kenmanheimer/EmacsUtils
13 ;; Easily use and manage multiple shell buffers, including remote shells.
14 ;; Fundamentally, multishell is the function `multishell:pop-to-shell - like
15 ;; pop-to-buffer - plus a keybinding. Together, they enable you to:
17 ;; * Get to the input point from wherever you are in a shell buffer,
18 ;; * ... or to a shell buffer if you're not currently in one.
19 ;; * Use universal arguments to launch and choose among alternate shell buffers,
20 ;; * ... and select which is default.
21 ;; * Prepend a path to a new shell name to launch a shell in that directory,
22 ;; * ... and use a path with Emacs tramp syntax to launch a remote shell.
24 ;; Customize-group `multishell` to select and activate a keybinding and set
27 ;; See the pop-to-shell docstring for details.
31 ;; 2016-01-02 Ken Manheimer - initial release
35 ;; * Preserveable (savehist) history that associates names with paths
36 ;; - Using an association list between names and paths
37 ;; - Searched for search backwards/forwards on isearch-like M-r/M-s bindings
38 ;; - *Not* searched for regular completion
40 ;; - Using isearch keybinding M-e
42 ;; - New association overrides previous
43 ;; - Deleting path removes association and history entry
44 ;; * Customize activation of savehist
45 ;; - Customize entry has warning about activating savehist
46 ;; - Adds the name/path association list to savehist-additional-variables
47 ;; - Activates savehist, if inactive
51 (defvar non-interactive-process-buffers '("*compilation*" "*grep*"))
56 (defgroup multishell nil
57 "Allout extension that highlights outline structure graphically.
59 Customize `allout-widgets-auto-activation' to activate allout-widgets
63 (defcustom multishell:non-interactive-process-buffers
64 '("*compilation*" "*grep*")
65 "Names of buffers that have processes but are not for interaction.
66 Add names of buffers that you don't want pop-to-shell to stick around in."
67 :type '(repeat string)
69 (defcustom multishell:command-key "\M- "
70 "The key to use if `multishell:activate-command-key' is true.
72 You can instead bind `pop-to-shell` to your preferred key using emacs
73 lisp, eg: (global-set-key \"\\M- \" 'pop-to-shell)."
77 (defvar multishell:responsible-for-command-key nil
78 "Multishell internal.")
79 (defun multishell:activate-command-key-setter (symbol setting)
80 "Implement `multishell:activate-command-key' choice."
81 (set-default 'multishell:activate-command-key setting)
82 (when (or setting multishell:responsible-for-command-key)
83 (multishell:implement-command-key-choice (not setting))))
84 (defun multishell:implement-command-key-choice (&optional unbind)
85 "If settings dicate, implement binding of multishell command key.
87 If optional UNBIND is true, globally unbind the key.
89 * `multishell:activate-command-key' - Set this to get the binding or not.
90 * `multishell:command-key' - The key to use for the binding, if appropriate."
92 (when (and (boundp 'multishell:command-key) multishell:command-key)
93 (global-unset-key multishell:command-key)))
94 ((not (and (boundp 'multishell:activate-command-key)
95 (boundp 'multishell:command-key)))
97 ((and multishell:activate-command-key multishell:command-key)
98 (setq multishell:responsible-for-command-key t)
99 (global-set-key multishell:command-key 'pop-to-shell))))
101 (defcustom multishell:activate-command-key nil
102 "Set this to impose the `multishell:command-key' binding.
104 You can instead bind `pop-to-shell` to your preferred key using emacs
105 lisp, eg: (global-set-key \"\\M- \" 'pop-to-shell)."
107 :set 'multishell:activate-command-key-setter
110 ;; Assert the customizations whenever the package is loaded:
111 (with-eval-after-load "multishell"
112 (multishell:implement-command-key-choice))
114 (defcustom multishell:pop-to-frame nil
115 "*If non-nil, jump to a frame already showing the shell, if another is.
117 Otherwise, open a new window in the current frame.
119 \(Adjust `pop-up-windows' to change other-buffer vs current-buffer behavior.)"
123 ;; (defcustom multishell:persist-shell-names nil
124 ;; "Remember shell name/path associations across sessions. Note well:
125 ;; This will activate minibuffer history persistence, in general, if it's not
130 (defvar multishell:name-path-assoc nil
131 "Assoc list from name to path")
133 (defvar multishell:primary-name "*shell*"
134 "Shell name to use for un-modified pop-to-shell buffer target.")
135 (defvar multishell:buffer-name-history nil
136 "Distinct pop-to-shell completion history container.")
138 (defun pop-to-shell (&optional arg)
139 "Easily navigate to and within multiple shell buffers, local and remote.
141 Use universal arguments to launch and choose between alternate
142 shell buffers and to select which is default. Prepend a path to
143 a new shell name to launch a shell in that directory, and use
144 Emacs tramp syntax to launch a remote shell.
146 Customize-group `multishell' to set up a key binding and tweak behaviors.
148 ==== Basic operation:
150 - If the current buffer is associated with a subprocess (that is
151 not among those named on `non-interactive-process-buffers'),
152 then focus is moved to the process input point.
154 \(You can use a universal argument go to a different shell
155 buffer when already in a buffer that has a process - see
158 - If not in a shell buffer (or with universal argument), go to a
159 window that is already showing the (a) shell buffer, if any.
161 In this case, the cursor is left in its prior position in the
162 shell buffer. Repeating the command will then go to the
163 process input point, per the first item in this list.
165 We respect `pop-up-windows', so you can adjust it to set the
166 other-buffer/same-buffer behavior.
168 - Otherwise, start a new shell buffer, using the current
169 directory as the working directory.
171 If a buffer with the resulting name exists and its shell process
172 was disconnected or otherwise stopped, it's resumed.
174 ===== Universal arg to start and select between named shell buffers:
176 You can name alternate shell buffers to create or return to using
177 single or doubled universal arguments:
179 - With a single universal argument, prompt for the buffer name
180 to use (without the asterisks that shell mode will put around
181 the name), defaulting to 'shell'.
183 Completion is available.
185 This combination makes it easy to start and switch between
186 multiple shell buffers.
188 - A double universal argument will prompt for the name *and* set
189 the default to that name, so the target shell becomes the
192 ===== Select starting directory and remote host:
194 The shell buffer name you give to the prompt for a universal arg
195 can include a preceding path. That will be used for the startup
196 directory. You can use tramp remote syntax to specify a remote
197 shell. If there is an element after a final '/', that's used for
198 the buffer name. Otherwise, the host, domain, or path is used.
202 * Use '/ssh:example.net:/' for a shell buffer on example.net named
204 * '/ssh:example.net|sudo:root@example.net:/\#ex' for a root shell on
205 example.net named \"#ex\"."
207 ;; I'm leaving the following out of the docstring for now because just
208 ;; saving the buffer names, and not the paths, yields sometimes unwanted
211 ;; ===== Persisting your alternate shell buffer names and paths:
213 ;; You can use emacs builtin SaveHist to preserve your alternate
214 ;; shell buffer names and paths across emacs sessions. To do so,
215 ;; customize the `savehist' group, and:
217 ;; 1. Add `multishell:pop-to-shell-buffer-name-history' to Savehist Additional
219 ;; 2. Activate Savehist Mode, if not already activated.
224 (let* ((from-buffer (current-buffer))
225 (from-buffer-is-shell (eq major-mode 'shell-mode))
226 (doublearg (equal arg '(16)))
228 (multishell:read-bare-shell-buffer-name
229 (format "Shell buffer name [%s]%s "
230 (substring-no-properties
231 multishell:primary-name
232 1 (- (length multishell:primary-name) 1))
233 (if doublearg " <==" ":"))
234 multishell:primary-name)
235 multishell:primary-name))
237 (target-shell-buffer-name
238 ;; Derive target name, and default-dir if any, from temp.
239 (cond ((string= temp "") multishell:primary-name)
240 ((string-match "^\\*\\(/.*/\\)\\(.*\\)\\*" temp)
241 (setq use-default-dir (match-string 1 temp))
242 (multishell:bracket-asterisks
243 (if (string= (match-string 2 temp) "")
244 (let ((v (tramp-dissect-file-name
246 (or (tramp-file-name-host v)
247 (tramp-file-name-domain v)
248 (tramp-file-name-localname v)
250 (match-string 2 temp))))
251 (t (multishell:bracket-asterisks temp))))
252 (curr-buff-proc (get-buffer-process from-buffer))
253 (target-buffer (if (and (or curr-buff-proc from-buffer-is-shell)
254 (not (member (buffer-name from-buffer)
255 non-interactive-process-buffers)))
257 (get-buffer target-shell-buffer-name)))
262 (setq multishell:primary-name target-shell-buffer-name))
268 ((and (or curr-buff-proc from-buffer-is-shell)
270 (eq from-buffer target-buffer)
271 (not (eq target-shell-buffer-name (buffer-name from-buffer))))
272 ;; In a shell buffer, but not named - stay in buffer, but go to end.
273 (setq already-there t))
275 ((string= (buffer-name) target-shell-buffer-name)
276 ;; Already in the specified shell buffer:
277 (setq already-there t))
279 ((or (not target-buffer)
281 (multishell:get-visible-window-for-buffer target-buffer))))
282 ;; No preexisting shell buffer, or not in a visible window:
283 (pop-to-buffer target-shell-buffer-name pop-up-windows))
285 ;; Buffer exists and already has a window - jump to it:
286 (t (if (and multishell:pop-to-frame
288 (not (equal (window-frame (selected-window))
289 (window-frame inwin))))
290 (select-frame-set-input-focus (window-frame inwin)))
291 (if (not (string= (buffer-name (current-buffer))
292 target-shell-buffer-name))
293 (pop-to-buffer target-shell-buffer-name t))))
295 ;; We're in the buffer. Activate:
297 (cond ((not (comint-check-proc (current-buffer)))
298 (multishell:start-shell-in-buffer (buffer-name (current-buffer))
301 (cd use-default-dir)))
303 ;; If the destination buffer has a stopped process, resume it:
304 (let ((process (get-buffer-process (current-buffer))))
305 (if (and process (equal 'stop (process-status process)))
306 (continue-process process)))
307 (when (or already-there
308 (equal (current-buffer) from-buffer))
309 (goto-char (point-max))
310 (and (get-buffer-process from-buffer)
311 (goto-char (process-mark (get-buffer-process from-buffer)))))))
313 (defun multishell:get-visible-window-for-buffer (buffer)
314 "Return visible window containing buffer."
317 (function (lambda (win)
318 (if (and (eq (window-buffer win) buffer)
319 (equal (frame-parameter
320 (selected-frame) 'display)
322 (window-frame win) 'display)))
323 (throw 'got-a-vis win))))
327 (defun multishell:read-bare-shell-buffer-name (prompt default)
328 "PROMPT for shell buffer name, sans asterisks.
330 Return the supplied name bracketed with the asterisks, or specified DEFAULT
332 (let* ((candidates (append
334 (mapcar (lambda (buffer)
335 (let ((name (buffer-name buffer)))
336 (if (with-current-buffer buffer
337 (eq major-mode 'shell-mode))
338 ;; Shell mode buffers.
339 (if (> (length name) 2)
345 (got (completing-read prompt
346 candidates ; COLLECTION
348 'confirm ; REQUIRE-MATCH
350 'multishell:buffer-name-history ; HIST
352 (if (not (string= got "")) (multishell:bracket-asterisks got) default)))
354 (defun multishell:bracket-asterisks (name)
355 "Return a copy of name, ensuring it has an asterisk at the beginning and end."
356 (if (not (string= (substring name 0 1) "*"))
357 (setq name (concat "*" name)))
358 (if (not (string= (substring name -1) "*"))
359 (setq name (concat name "*")))
361 (defun multishell:unbracket-asterisks (name)
362 "Return a copy of name, removing asterisks, if any, at beginning and end."
363 (if (string= (substring name 0 1) "*")
364 (setq name (substring name 1)))
365 (if (string= (substring name -1) "*")
366 (setq name (substring name 0 -1)))
368 (defun multishell:start-shell-in-buffer (buffer-name dir)
369 "Ensure a shell is started, using whatever name we're passed."
370 ;; We work around shell-mode's bracketing of the buffer name, and do
371 ;; some tramp-mode hygiene for remote connections.
373 (let* ((buffer buffer-name)
374 (prog (or explicit-shell-file-name
378 (name (file-name-nondirectory prog))
379 (startfile (concat "~/.emacs_" name))
380 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
381 (set-buffer buffer-name)
382 (when (and (file-remote-p default-directory)
383 (eq major-mode 'shell-mode)
384 (not (comint-check-proc (current-buffer))))
385 ;; We're returning to an already established but disconnected remote
387 (tramp-cleanup-connection
388 (tramp-dissect-file-name default-directory 'noexpand)
389 'keep-debug 'keep-password))
392 (setq buffer (set-buffer (apply 'make-comint
393 (multishell:unbracket-asterisks buffer-name)
395 (if (file-exists-p startfile)
399 (symbol-value xargs-name)
403 (provide 'multishell)
405 ;;; multishell.el ends here