]> code.delx.au - gnu-emacs-elpa/blob - multishell.el
multishell - Note need for local-path-only fix
[gnu-emacs-elpa] / multishell.el
1 ;;; multishell.el --- manage interaction with multiple local and remote shells
2
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc. and Ken Manheimer
4
5 ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
6 ;; Version: 1.0.0
7 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
8 ;; Created: 1999 -- first public availability
9 ;; Keywords: processes
10 ;; URL: https://github.com/kenmanheimer/EmacsUtils
11 ;;
12 ;;; Commentary:
13 ;;
14 ;; Easily use and manage multiple shell buffers, including remote shells.
15 ;; Fundamentally, multishell is the function `multishell:pop-to-shell - like
16 ;; pop-to-buffer - plus a keybinding. Together, they enable you to:
17 ;;
18 ;; * Get to the input point from wherever you are in a shell buffer,
19 ;; * ... or to a shell buffer if you're not currently in one.
20 ;; * Use universal arguments to launch and choose among alternate shell buffers,
21 ;; * ... and select which is default.
22 ;; * Prepend a path to a new shell name to launch a shell in that directory,
23 ;; * ... and use a path with Emacs tramp syntax to launch a remote shell.
24 ;;
25 ;; Customize-group `multishell` to select and activate a keybinding and set
26 ;; various behaviors.
27 ;;
28 ;; See the pop-to-shell docstring for details.
29 ;;
30 ;;; Change Log:
31 ;;
32 ;; 2016-01-02 Ken Manheimer - initial release
33 ;;
34 ;;; TODO:
35 ;;
36 ;; * Fix operation given local path without specified name
37 ;; * Preserveable (savehist) history that associates names with paths
38 ;; - Using an association list between names and paths
39 ;; - Searched for search backwards/forwards on isearch-like M-r/M-s bindings
40 ;; - *Not* searched for regular completion
41 ;; - Editible
42 ;; - During confirmation for new buffers - to use historical one
43 ;; - Or with minibuffer setup created key binding (isearch-like) M-e
44 ;; - M-e in empty initial provides completion on historicals
45 ;; - User can edit the entire path, changing the association
46 ;; - New association overrides previous
47 ;; - Deleting path removes association and history entry
48 ;; - Tracks buffer name changes
49 ;; - Using buffer-list-update-hook
50 ;; * Customize activation of savehist
51 ;; - Customize entry has warning about activating savehist
52 ;; - Adds the name/path association list to savehist-additional-variables
53 ;; - Activates savehist, if inactive
54
55 ;;; Code:
56
57 (defvar non-interactive-process-buffers '("*compilation*" "*grep*"))
58
59 (require 'comint)
60 (require 'shell)
61
62 (defgroup multishell nil
63 "Allout extension that highlights outline structure graphically.
64
65 Customize `allout-widgets-auto-activation' to activate allout-widgets
66 with allout-mode."
67 :group 'shell)
68
69 (defcustom multishell:non-interactive-process-buffers
70 '("*compilation*" "*grep*")
71 "Names of buffers that have processes but are not for interaction.
72 Add names of buffers that you don't want pop-to-shell to stick around in."
73 :type '(repeat string)
74 :group 'multishell)
75 (defcustom multishell:command-key "\M- "
76 "The key to use if `multishell:activate-command-key' is true.
77
78 You can instead bind `pop-to-shell` to your preferred key using emacs
79 lisp, eg: (global-set-key \"\\M- \" 'pop-to-shell)."
80 :type 'key-sequence
81 :group 'multishell)
82
83 (defvar multishell:responsible-for-command-key nil
84 "Multishell internal.")
85 (defun multishell:activate-command-key-setter (symbol setting)
86 "Implement `multishell:activate-command-key' choice."
87 (set-default 'multishell:activate-command-key setting)
88 (when (or setting multishell:responsible-for-command-key)
89 (multishell:implement-command-key-choice (not setting))))
90 (defun multishell:implement-command-key-choice (&optional unbind)
91 "If settings dicate, implement binding of multishell command key.
92
93 If optional UNBIND is true, globally unbind the key.
94
95 * `multishell:activate-command-key' - Set this to get the binding or not.
96 * `multishell:command-key' - The key to use for the binding, if appropriate."
97 (cond (unbind
98 (when (and (boundp 'multishell:command-key) multishell:command-key)
99 (global-unset-key multishell:command-key)))
100 ((not (and (boundp 'multishell:activate-command-key)
101 (boundp 'multishell:command-key)))
102 nil)
103 ((and multishell:activate-command-key multishell:command-key)
104 (setq multishell:responsible-for-command-key t)
105 (global-set-key multishell:command-key 'pop-to-shell))))
106
107 (defcustom multishell:activate-command-key nil
108 "Set this to impose the `multishell:command-key' binding.
109
110 You can instead bind `pop-to-shell` to your preferred key using emacs
111 lisp, eg: (global-set-key \"\\M- \" 'pop-to-shell)."
112 :type 'boolean
113 :set 'multishell:activate-command-key-setter
114 :group 'multishell)
115
116 ;; Assert the customizations whenever the package is loaded:
117 (with-eval-after-load "multishell"
118 (multishell:implement-command-key-choice))
119
120 (defcustom multishell:pop-to-frame nil
121 "*If non-nil, jump to a frame already showing the shell, if another is.
122
123 Otherwise, open a new window in the current frame.
124
125 \(Adjust `pop-up-windows' to change other-buffer vs current-buffer behavior.)"
126 :type 'boolean
127 :group 'multishell)
128
129 ;; (defcustom multishell:persist-shell-names nil
130 ;; "Remember shell name/path associations across sessions. Note well:
131 ;; This will activate minibuffer history persistence, in general, if it's not
132 ;; already active."
133 ;; :type 'boolean
134 ;; :group 'shell)
135
136 (defvar multishell:name-path-assoc nil
137 "Assoc list from name to path")
138
139 (defvar multishell:primary-name "*shell*"
140 "Shell name to use for un-modified pop-to-shell buffer target.")
141 (defvar multishell:buffer-name-history nil
142 "Distinct pop-to-shell completion history container.")
143
144 (defun pop-to-shell (&optional arg)
145 "Easily navigate to and within multiple shell buffers, local and remote.
146
147 Use universal arguments to launch and choose between alternate
148 shell buffers and to select which is default. Prepend a path to
149 a new shell name to launch a shell in that directory, and use
150 Emacs tramp syntax to launch a remote shell.
151
152 Customize-group `multishell' to set up a key binding and tweak behaviors.
153
154 ==== Basic operation:
155
156 - If the current buffer is associated with a subprocess (that is
157 not among those named on `non-interactive-process-buffers'),
158 then focus is moved to the process input point.
159
160 \(You can use a universal argument go to a different shell
161 buffer when already in a buffer that has a process - see
162 below.)
163
164 - If not in a shell buffer (or with universal argument), go to a
165 window that is already showing the (a) shell buffer, if any.
166
167 In this case, the cursor is left in its prior position in the
168 shell buffer. Repeating the command will then go to the
169 process input point, per the first item in this list.
170
171 We respect `pop-up-windows', so you can adjust it to set the
172 other-buffer/same-buffer behavior.
173
174 - Otherwise, start a new shell buffer, using the current
175 directory as the working directory.
176
177 If a buffer with the resulting name exists and its shell process
178 was disconnected or otherwise stopped, it's resumed.
179
180 ===== Universal arg to start and select between named shell buffers:
181
182 You can name alternate shell buffers to create or return to using
183 single or doubled universal arguments:
184
185 - With a single universal argument, prompt for the buffer name
186 to use (without the asterisks that shell mode will put around
187 the name), defaulting to 'shell'.
188
189 Completion is available.
190
191 This combination makes it easy to start and switch between
192 multiple shell buffers.
193
194 - A double universal argument will prompt for the name *and* set
195 the default to that name, so the target shell becomes the
196 primary.
197
198 ===== Select starting directory and remote host:
199
200 The shell buffer name you give to the prompt for a universal arg
201 can include a preceding path. That will be used for the startup
202 directory. You can use tramp remote syntax to specify a remote
203 shell. If there is an element after a final '/', that's used for
204 the buffer name. Otherwise, the host, domain, or path is used.
205
206 For example:
207
208 * Use '/ssh:example.net:/' for a shell buffer on example.net named
209 \"example.net\".
210 * '/ssh:example.net|sudo:root@example.net:/\#ex' for a root shell on
211 example.net named \"#ex\"."
212
213 ;; I'm leaving the following out of the docstring for now because just
214 ;; saving the buffer names, and not the paths, yields sometimes unwanted
215 ;; behavior.
216
217 ;; ===== Persisting your alternate shell buffer names and paths:
218
219 ;; You can use emacs builtin SaveHist to preserve your alternate
220 ;; shell buffer names and paths across emacs sessions. To do so,
221 ;; customize the `savehist' group, and:
222
223 ;; 1. Add `multishell:pop-to-shell-buffer-name-history' to Savehist Additional
224 ;; Variables.
225 ;; 2. Activate Savehist Mode, if not already activated.
226 ;; 3. Save.
227
228 (interactive "P")
229
230 (let* ((from-buffer (current-buffer))
231 (from-buffer-is-shell (eq major-mode 'shell-mode))
232 (doublearg (equal arg '(16)))
233 (temp (if arg
234 (multishell:read-bare-shell-buffer-name
235 (format "Shell buffer name [%s]%s "
236 (substring-no-properties
237 multishell:primary-name
238 1 (- (length multishell:primary-name) 1))
239 (if doublearg " <==" ":"))
240 multishell:primary-name)
241 multishell:primary-name))
242 use-default-dir
243 (target-shell-buffer-name
244 ;; Derive target name, and default-dir if any, from temp.
245 (cond ((string= temp "") multishell:primary-name)
246 ((string-match "^\\*\\(/.*/\\)\\(.*\\)\\*" temp)
247 (setq use-default-dir (match-string 1 temp))
248 (multishell:bracket-asterisks
249 (if (string= (match-string 2 temp) "")
250 (let ((v (tramp-dissect-file-name
251 use-default-dir)))
252 (or (tramp-file-name-host v)
253 (tramp-file-name-domain v)
254 (tramp-file-name-localname v)
255 use-default-dir))
256 (match-string 2 temp))))
257 (t (multishell:bracket-asterisks temp))))
258 (curr-buff-proc (get-buffer-process from-buffer))
259 (target-buffer (if (and (or curr-buff-proc from-buffer-is-shell)
260 (not (member (buffer-name from-buffer)
261 non-interactive-process-buffers)))
262 from-buffer
263 (get-buffer target-shell-buffer-name)))
264 inwin
265 already-there)
266
267 (when doublearg
268 (setq multishell:primary-name target-shell-buffer-name))
269
270 ;; Situate:
271
272 (cond
273
274 ((and (or curr-buff-proc from-buffer-is-shell)
275 (not arg)
276 (eq from-buffer target-buffer)
277 (not (eq target-shell-buffer-name (buffer-name from-buffer))))
278 ;; In a shell buffer, but not named - stay in buffer, but go to end.
279 (setq already-there t))
280
281 ((string= (buffer-name) target-shell-buffer-name)
282 ;; Already in the specified shell buffer:
283 (setq already-there t))
284
285 ((or (not target-buffer)
286 (not (setq inwin
287 (multishell:get-visible-window-for-buffer target-buffer))))
288 ;; No preexisting shell buffer, or not in a visible window:
289 (pop-to-buffer target-shell-buffer-name pop-up-windows))
290
291 ;; Buffer exists and already has a window - jump to it:
292 (t (if (and multishell:pop-to-frame
293 inwin
294 (not (equal (window-frame (selected-window))
295 (window-frame inwin))))
296 (select-frame-set-input-focus (window-frame inwin)))
297 (if (not (string= (buffer-name (current-buffer))
298 target-shell-buffer-name))
299 (pop-to-buffer target-shell-buffer-name t))))
300
301 ;; We're in the buffer.
302
303 ;; If we have a use-default-dir, impose it:
304 (when use-default-dir
305 (cd use-default-dir))
306
307 ;; Activate:
308
309 (if (not (comint-check-proc (current-buffer)))
310 (multishell:start-shell-in-buffer (buffer-name (current-buffer))))
311
312 ;; If the destination buffer has a stopped process, resume it:
313 (let ((process (get-buffer-process (current-buffer))))
314 (if (and process (equal 'stop (process-status process)))
315 (continue-process process)))
316 (when (or already-there
317 (equal (current-buffer) from-buffer))
318 (goto-char (point-max))
319 (and (get-buffer-process from-buffer)
320 (goto-char (process-mark (get-buffer-process from-buffer)))))))
321
322 (defun multishell:get-visible-window-for-buffer (buffer)
323 "Return visible window containing buffer."
324 (catch 'got-a-vis
325 (walk-windows
326 (function (lambda (win)
327 (if (and (eq (window-buffer win) buffer)
328 (equal (frame-parameter
329 (selected-frame) 'display)
330 (frame-parameter
331 (window-frame win) 'display)))
332 (throw 'got-a-vis win))))
333 nil 'visible)
334 nil))
335
336 (defun multishell:read-bare-shell-buffer-name (prompt default)
337 "PROMPT for shell buffer name, sans asterisks.
338
339 Return the supplied name bracketed with the asterisks, or specified DEFAULT
340 on empty input."
341 (let* ((candidates (append
342 (remq nil
343 (mapcar (lambda (buffer)
344 (let ((name (buffer-name buffer)))
345 (if (with-current-buffer buffer
346 (eq major-mode 'shell-mode))
347 ;; Shell mode buffers.
348 (if (> (length name) 2)
349 ;; Strip asterisks.
350 (substring name 1
351 (1- (length name)))
352 name))))
353 (buffer-list)))))
354 (got (completing-read prompt
355 candidates ; COLLECTION
356 nil ; PREDICATE
357 'confirm ; REQUIRE-MATCH
358 nil ; INITIAL-INPUT
359 'multishell:buffer-name-history ; HIST
360 )))
361 (if (not (string= got "")) (multishell:bracket-asterisks got) default)))
362
363 (defun multishell:bracket-asterisks (name)
364 "Return a copy of name, ensuring it has an asterisk at the beginning and end."
365 (if (not (string= (substring name 0 1) "*"))
366 (setq name (concat "*" name)))
367 (if (not (string= (substring name -1) "*"))
368 (setq name (concat name "*")))
369 name)
370 (defun multishell:unbracket-asterisks (name)
371 "Return a copy of name, removing asterisks, if any, at beginning and end."
372 (if (string= (substring name 0 1) "*")
373 (setq name (substring name 1)))
374 (if (string= (substring name -1) "*")
375 (setq name (substring name 0 -1)))
376 name)
377 (defun multishell:start-shell-in-buffer (buffer-name)
378 "Ensure a shell is started, using whatever name we're passed."
379 ;; We work around shell-mode's bracketing of the buffer name, and do
380 ;; some tramp-mode hygiene for remote connections.
381
382 (require 'comint)
383 (require 'shell)
384
385 (let* ((buffer buffer-name)
386 (prog (or explicit-shell-file-name
387 (getenv "ESHELL")
388 (getenv "SHELL")
389 "/bin/sh"))
390 (name (file-name-nondirectory prog))
391 (startfile (concat "~/.emacs_" name))
392 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
393 (set-buffer buffer-name)
394 (when (and (file-remote-p default-directory)
395 (eq major-mode 'shell-mode)
396 (not (comint-check-proc (current-buffer))))
397 ;; We're returning to an already established but disconnected remote
398 ;; shell, tidy it:
399 (tramp-cleanup-connection
400 (tramp-dissect-file-name default-directory 'noexpand)
401 'keep-debug 'keep-password))
402 (setq buffer (set-buffer (apply 'make-comint
403 (multishell:unbracket-asterisks buffer-name)
404 prog
405 (if (file-exists-p startfile)
406 startfile)
407 (if (and xargs-name
408 (boundp xargs-name))
409 (symbol-value xargs-name)
410 '("-i")))))
411 (shell-mode)))
412
413 (provide 'multishell)
414
415 ;;; multishell.el ends here