]> code.delx.au - gnu-emacs-elpa/blob - packages/multishell/multishell.el
multishell - merge 1.0.8.
[gnu-emacs-elpa] / packages / multishell / multishell.el
1 ;;; multishell.el --- facilitate multiple local and remote shell buffers
2
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc. and Ken Manheimer
4
5 ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
6 ;; Version: 1.0.8
7 ;; Created: 1999 -- first public availability
8 ;; Keywords: processes
9 ;; URL: https://github.com/kenmanheimer/EmacsMultishell
10 ;;
11 ;;; Commentary:
12 ;;
13 ;; Easily use and navigate multiple shell buffers, including remote shells.
14 ;; Fundamentally, multishell is the function `multishell-pop-to-shell' -
15 ;; a la `pop-to-buffer' - plus a keybinding. Together, they enable you to:
16 ;;
17 ;; * Get to the input point from wherever you are in a shell buffer,
18 ;; ... or to any of your shell buffers, from anywhere inside emacs.
19 ;;
20 ;; * Use universal arguments to launch and choose among alternate shell buffers,
21 ;; ... and change which is the current default.
22 ;;
23 ;; * Easily restart disconnected shells, or shells from prior sessions
24 ;; ... the latter from Emacs builtin savehist minibuf history persistence
25 ;;
26 ;; * Append a path to a new shell name to launch a shell in that directory,
27 ;; ... and use a path with Emacs tramp syntax to launch a remote shell -
28 ;; for example:
29 ;;
30 ;; * `#root/sudo:root@localhost:/etc` for a buffer named "#root" with a
31 ;; root shell starting in /etc.
32 ;;
33 ;; * `/ssh:example.net:/` for a shell buffer in / on example.net.
34 ;; The buffer will be named "*example.net*".
35 ;;
36 ;; * `#ex/ssh:example.net|sudo:root@example.net:/etc` for a root shell
37 ;; starting in /etc on example.net named "*#ex*".
38 ;;
39 ;; * 'interior/ssh:gateway.corp.com|ssh:interior.corp.com:' to go via
40 ;; gateway.corp.com to your homedir on interior.corp.com. The buffer
41 ;; will be named "*interior*". You could append a sudo hop, and so on.
42 ;;
43 ;; * Thanks to tramp, file visits from the shell will seamlessly be in
44 ;; the auspices of the target account, and relative to the current
45 ;; directory, on the host where the shell is running.
46 ;;
47 ;; See the `multishell-pop-to-shell` docstring for details.
48 ;;
49 ;; Customize-group `multishell' to select and activate a keybinding and set
50 ;; various behaviors. Customize-group `savehist' to preserve buffer
51 ;; names/paths across emacs restarts.
52 ;;
53 ;; Please use
54 ;; [the multishell repository](https://github.com/kenmanheimer/EmacsMultishell)
55 ;; issue tracker to report problems, suggestions, etc, and see that
56 ;; repository for a bit more documentation.
57 ;;
58 ;; Change Log:
59 ;;
60 ;; * 2016-01-24 1.0.8 Ken Manheimer:
61 ;; - Work around the shell/tramp mishandling of remote+sudo+homedir problem!
62 ;; The work around is clean and simple, basically using high-level `cd'
63 ;; API and not messing with the low-level default-directory setting.
64 ;; (Turns out the problem was not in my local config. Good riddance to the
65 ;; awkward failure handler!)
66 ;; - Clean up code resolving the destination shell, starting to document the
67 ;; decision tree in the process. See getting-to-a-shell.md in the
68 ;; multishell repository, https://github.com/kenmanheimer/EmacsMultishell
69 ;; - There may be some shake-out on resolving the destination shell, but
70 ;; this release gets the fundamental functionality soundly in place.
71 ;; * 2016-01-23 1.0.7 Ken Manheimer:
72 ;; - Remove notes about tramp remote+sudo+homedir problem. Apparently it's
73 ;; due to something in my local site configuration (happens with -q but
74 ;; not -Q).
75 ;; * 2016-01-22 1.0.6 Ken Manheimer:
76 ;; - Add multishell-version function.
77 ;; - Tweak commentary/comments/docstrings.
78 ;; - Null old multishell-buffer-name-history var, if present.
79 ;; * 2016-01-16 1.0.5 Ken Manheimer:
80 ;; - History now includes paths, when designated.
81 ;; - Actively track current directory in history entries that have a path.
82 ;; Custom control: multishell-history-entry-tracks-current-directory
83 ;; - Offer to remove shell's history entry when buffer is killed.
84 ;; (Currently the only UI mechanism to remove history entries.)
85 ;; - Fix - prevent duplicate entries for same name but different paths
86 ;; - Fix - recognize and respect tramp path syntax to start in home dir
87 ;; - Simplify history var name, migrate existing history if any from old name
88 ;; * 2016-01-04 1.0.4 Ken Manheimer - Released to ELPA
89 ;; * 2016-01-02 Ken Manheimer - working on this in public, but not yet released.
90 ;;
91 ;; TODO:
92 ;;
93 ;; * Find suitable, internally consistent ways to tidy completions, eg:
94 ;; - first list completions for active shells, then present but inactive,
95 ;; then historical
96 ;; - some way for user to toggle between presenting just buffer names vs
97 ;; full buffer/path
98 ;; - without cutting user off from easy editing of path
99 ;; * Try minibuffer field boundary at beginning of tramp path, to see whether
100 ;; the field boundary magically enables tramp path completion.
101 ;; * Assess whether deletion of history entry via kill-buffer is sufficient.
102
103 ;;; Code:
104
105 (require 'comint)
106 (require 'shell)
107 (require 'savehist)
108
109 (defvar multishell-version "1.0.8")
110 (defun multishell-version (&optional here)
111 "Return string describing the loaded multishell version."
112 (interactive "P")
113 (let ((msg (concat "Multishell " multishell-version)))
114 (if here (insert msg)
115 (if (called-interactively-p 'interactive)
116 (message "%s" msg)
117 msg))))
118
119 (defgroup multishell nil
120 "Allout extension that highlights outline structure graphically.
121
122 Customize `allout-widgets-auto-activation' to activate allout-widgets
123 with allout-mode."
124 :group 'shell)
125
126 (defcustom multishell-command-key "\M- "
127 "The key to use if `multishell-activate-command-key' is true.
128
129 You can instead manually bind `multishell-pop-to-shell` using emacs
130 lisp, eg: (global-set-key \"\\M- \" 'multishell-pop-to-shell)."
131 :type 'key-sequence
132 :group 'multishell)
133
134 (defvar multishell--responsible-for-command-key nil
135 "Coordination for multishell key assignment.")
136 (defun multishell-activate-command-key-setter (symbol setting)
137 "Implement `multishell-activate-command-key' choice."
138 (set-default 'multishell-activate-command-key setting)
139 (when (or setting multishell--responsible-for-command-key)
140 (multishell-implement-command-key-choice (not setting))))
141 (defun multishell-implement-command-key-choice (&optional unbind)
142 "If settings dicate, implement binding of multishell command key.
143
144 If optional UNBIND is true, globally unbind the key.
145
146 * `multishell-activate-command-key' - Set this to get the binding or not.
147 * `multishell-command-key' - The key to use for the binding, if appropriate."
148 (cond (unbind
149 (when (and (boundp 'multishell-command-key) multishell-command-key)
150 (global-unset-key multishell-command-key)))
151 ((not (and (boundp 'multishell-activate-command-key)
152 (boundp 'multishell-command-key)))
153 nil)
154 ((and multishell-activate-command-key multishell-command-key)
155 (setq multishell--responsible-for-command-key t)
156 (global-set-key multishell-command-key 'multishell-pop-to-shell))))
157
158 (defcustom multishell-activate-command-key nil
159 "Set this to impose the `multishell-command-key' binding.
160
161 You can instead manually bind `multishell-pop-to-shell` using emacs
162 lisp, eg: (global-set-key \"\\M- \" 'multishell-pop-to-shell)."
163 :type 'boolean
164 :set 'multishell-activate-command-key-setter
165 :group 'multishell)
166
167 ;; Implement the key customization whenever the package is loaded:
168 (with-eval-after-load "multishell"
169 (multishell-implement-command-key-choice))
170
171 (defcustom multishell-pop-to-frame nil
172 "*If non-nil, jump to a frame already showing the shell, if another one is.
173
174 Otherwise, disregard already-open windows on the shell if they're
175 in another frame, and open a new window on the shell in the
176 current frame.
177
178 \(Use `pop-up-windows' to change multishell other-window vs
179 current-window behavior.)"
180 :type 'boolean
181 :group 'multishell)
182
183 (defcustom multishell-history-entry-tracks-current-directory t
184 "Maintain shell's current directory in its multishell history entry.
185
186 When set, the history entry for shells started with explicit
187 paths will track the shell's current working directory. (Explicit
188 paths will not be added to local shells started without one,
189 however.)
190
191 If `savehist-save-minibuffer-history' is enabled, the current
192 working directory of shells \(that were started with an explicit
193 path) will be conveyed between emacs sessions."
194 :type 'boolean
195 :group 'multishell)
196
197 (defvar multishell-history nil
198 "Name/path entries, most recent first.")
199 ;; Migrate the few pre 1.0.5 users to changed history var:
200 (when (and (not multishell-history)
201 (boundp 'multishell-buffer-name-history)
202 multishell-buffer-name-history)
203 (setq multishell-history multishell-buffer-name-history
204 multishell-buffer-name-history nil))
205
206 (defvar multishell-primary-name "*shell*"
207 "Default shell name for un-modified multishell-pop-to-shell buffer target.
208
209 This is adjusted by `multishell-pop-to-shell' when it is
210 invoked (with doubled universal argument) to set the default.
211
212 To preserve changes to this setting across emacs restarts, add it
213 to `savehist-additional-variables' by customizing the latter.")
214
215 ;;; Can't just add multishell-primary-name to savehist-additional-variables
216 ;;; - it'll be lost any time the user runs emacs without loading
217 ;;; multishell. So instead, inform the user that they can customize
218 ;;; savehist-additional-variables.
219 ;;;
220 ;;; I suspect that including savehist-additional-variables *on*
221 ;;; savehist-additional-variables could avoid this problem, as long as it
222 ;;; doesn't conflict with user customizations. However, even if that works,
223 ;;; doing so from multishell would change a behavior (for the better, but)
224 ;;; beyond multishell's scope, making the change hard to track down.
225
226 ;; (when (not (member 'multishell-primary-name
227 ;; savehist-additional-variables))
228 ;; (setq savehist-additional-variables
229 ;; (cons 'multishell-primary-name savehist-additional-variables)))
230
231 ;; Multiple entries happen because completion also adds name to history.
232 (defun multishell-register-name-to-path (name path)
233 "Add or replace entry associating NAME with PATH in `multishell-history'.
234
235 If NAME already had a PATH and new PATH is empty, retain the prior one.
236
237 Promote added/changed entry to the front of the list."
238 ;; Add or promote to the front, tracking path changes in the process.
239 (let* ((entries (multishell-history-entries name))
240 (path (or path "")))
241 (dolist (entry entries)
242 (when (string= path "")
243 ;; Retain explicit established path.
244 (setq path (cadr (multishell-split-entry entry))))
245 (setq multishell-history (delete entry multishell-history)))
246 (setq multishell-history (push (concat name path)
247 multishell-history))))
248
249 (defun multishell-history-entries (name)
250 "Return `multishell-history' entry that starts with NAME, or nil if none."
251 (let ((match-expr (concat "^" name "\\\(/.*$\\\)?$"))
252 got)
253 (dolist (entry multishell-history)
254 (when (and (string-match match-expr entry)
255 (not (member entry got)))
256 (setq got (cons entry got))))
257 got))
258
259 (defun multishell-pop-to-shell (&optional arg)
260 "Easily navigate to and within multiple shell buffers, local and remote.
261
262 Use universal arguments to launch and choose between alternate
263 shell buffers and to select which is default. Append a path to
264 a new shell name to launch a shell in that directory, and use
265 Emacs tramp syntax to launch a remote shell.
266
267 Customize-group `multishell' to set up a key binding and tweak behaviors.
268
269 ==== Basic operation:
270
271 - If the current buffer is shell-mode (or shell-mode derived)
272 buffer then focus is moved to the process input point.
273
274 \(You can use a universal argument go to a different shell
275 buffer when already in a buffer that has a process - see
276 below.)
277
278 - If not in a shell buffer (or with universal argument), go to a
279 window that is already showing the (a) shell buffer, if any.
280
281 In this case, the cursor is left in its prior position in the
282 shell buffer. Repeating the command will then go to the
283 process input point, per the first item in this list.
284
285 We respect `pop-up-windows', so you can adjust it to set the
286 other-buffer/same-buffer behavior.
287
288 - Otherwise, start a new shell buffer, using the current
289 directory as the working directory.
290
291 If a buffer with the resulting name exists and its shell process
292 was disconnected or otherwise stopped, it's resumed.
293
294 ===== Universal arg to start and select between named shell buffers:
295
296 You can name alternate shell buffers to create or return to using
297 single or doubled universal arguments:
298
299 - With a single universal argument, prompt for the buffer name
300 to use (without the asterisks that shell mode will put around
301 the name), defaulting to 'shell'.
302
303 Completion is available.
304
305 This combination makes it easy to start and switch across
306 multiple shell restarts.
307
308 - A double universal argument will prompt for the name *and* set
309 the default to that name, so the target shell becomes the
310 primary.
311
312 See `multishell-primary-name' for info about preserving the
313 setting across emacs restarts.
314
315 ===== Select starting directory and remote host:
316
317 The shell buffer name you give to the prompt for a universal arg
318 can include an appended path. That will be used for the startup
319 directory. You can use tramp remote syntax to specify a remote
320 shell. If there is an element after a final '/', that's used for
321 the buffer name. Otherwise, the host, domain, or path is used.
322
323 For example:
324
325 * '#root/sudo:root@localhost:/etc' for a buffer named \"#root\" with a
326 root shell starting in /etc.
327
328 * '/ssh:example.net:/' for a shell buffer in / on example.net; the buffer
329 will be named \"*example.net*\".
330
331 * '#ex/ssh:example.net|sudo:root@example.net:/etc' for a root shell
332 starting in /etc on example.net named \"*#ex*\".
333
334 * 'interior/ssh:gateway.corp.com|ssh:interior.corp.com:' to go
335 via gateway.corp.com to your homedir on interior.corp.com. The
336 buffer will be named \"*interior*\". You could append a sudo
337 hop to the path, combining the previous example, and so on.
338
339 Thanks to tramp, file visits from the shell, and many common
340 emacs activities, like dired, will seamlessly be in the auspices
341 of the target account, and relative to the current directory, on
342 the host where the shell is running.
343
344 You can change the startup path for a shell buffer by editing it
345 at the completion prompt. The new path will be preserved in
346 history but will not take effect for an already-running shell.
347
348 To remove a shell buffer's history entry, kill the buffer and
349 affirm removal of the entry when prompted.
350
351 ===== Activate savehist to retain shell buffer names and paths across Emacs restarts:
352
353 To have emacs maintain your history of shell buffer names and paths,
354 customize the savehist group to activate savehist."
355
356 (interactive "P")
357
358 (let* ((from-buffer (current-buffer))
359 (from-buffer-is-shell (derived-mode-p 'shell-mode))
360 (primary-name-unbracketed (multishell-unbracket-asterisks
361 multishell-primary-name))
362 (fallthrough-name (if from-buffer-is-shell
363 (buffer-name from-buffer)
364 primary-name-unbracketed))
365 (doublearg (equal arg '(16)))
366 (target-name-and-path
367 (multishell-resolve-target-name-and-path
368 (if arg
369 (or (multishell-read-bare-shell-buffer-name
370 (format "Shell buffer name [%s]%s "
371 primary-name-unbracketed
372 (if doublearg " <==" ":"))
373 primary-name-unbracketed)
374 primary-name-unbracketed)
375 fallthrough-name)))
376 (use-path (cadr target-name-and-path))
377 (target-shell-buffer-name (car target-name-and-path))
378 (target-buffer (get-buffer target-shell-buffer-name))
379 (curr-buff-proc (get-buffer-process from-buffer))
380 inwin
381 already-there)
382
383 ;; Register early so the entry is pushed to the front:
384 (multishell-register-name-to-path (multishell-unbracket-asterisks
385 target-shell-buffer-name)
386 use-path)
387
388 (when doublearg
389 (setq multishell-primary-name target-shell-buffer-name))
390
391 ;; Situate:
392
393 (cond
394
395 ((and (or curr-buff-proc from-buffer-is-shell)
396 (not arg)
397 (eq from-buffer target-buffer)
398 (not (eq target-shell-buffer-name (buffer-name from-buffer))))
399 ;; In a shell buffer, but not named - stay in buffer, but go to end.
400 (setq already-there t))
401
402 ((string= (buffer-name) target-shell-buffer-name)
403 ;; Already in the specified shell buffer:
404 (setq already-there t))
405
406 ((or (not target-buffer)
407 (not (setq inwin
408 (multishell-get-visible-window-for-buffer target-buffer))))
409 ;; No preexisting shell buffer, or not in a visible window:
410 (pop-to-buffer target-shell-buffer-name pop-up-windows))
411
412 ;; Buffer exists and already has a window - jump to it:
413 (t (if (and multishell-pop-to-frame
414 inwin
415 (not (equal (window-frame (selected-window))
416 (window-frame inwin))))
417 (select-frame-set-input-focus (window-frame inwin)))
418 (if (not (string= (buffer-name (current-buffer))
419 target-shell-buffer-name))
420 (pop-to-buffer target-shell-buffer-name t))))
421
422 ;; We're in the buffer. Activate:
423
424 (if (not (comint-check-proc (current-buffer)))
425 (multishell-start-shell-in-buffer (buffer-name (current-buffer))
426 use-path))
427
428 ;; If the destination buffer has a stopped process, resume it:
429 (let ((process (get-buffer-process (current-buffer))))
430 (if (and process (equal 'stop (process-status process)))
431 (continue-process process)))
432
433 (when (or already-there
434 (equal (current-buffer) from-buffer))
435 (goto-char (point-max))
436 (and (get-buffer-process from-buffer)
437 (goto-char (process-mark (get-buffer-process from-buffer)))))))
438
439 (defun multishell-kill-buffer-query-function ()
440 "Offer to remove multishell-history entry for buffer."
441 ;; Removal choice is crucial, so users can, eg, kill a shell with huge
442 ;; output backlog, while keeping the history entry to easily restart it.
443 ;;
444 ;; We use kill-buffer-query-functions instead of kill-buffer-hook because:
445 ;;
446 ;; 1. It enables the user to remove the history without actually killing a
447 ;; running buffer, by not confirming the subsequent running-proc query.
448 ;; 2. kill-buffer-hooks often fails to run when killing shell buffers!
449 ;; It's probably due to failures in other hooks - beyond our control -
450 ;; and anyway, I like the first reason well enough.
451
452 ;; (Use condition-case to avoid inadvertant disruption of kill-buffer
453 ;; activity. kill-buffer happens behind the scenes a whole lot.)
454 (condition-case err
455 (dolist (entry (and (derived-mode-p 'shell-mode)
456 (multishell-history-entries
457 (multishell-unbracket-asterisks (buffer-name)))))
458 (when (and entry
459 (y-or-n-p (format "Remove multishell history entry `%s'? "
460 entry)))
461 (setq multishell-history
462 (delete entry multishell-history))))
463 (error
464 (message "multishell-kill-buffer-query-function error: %s" err)))
465 t)
466 (add-hook 'kill-buffer-query-functions 'multishell-kill-buffer-query-function)
467
468 (defun multishell-get-visible-window-for-buffer (buffer)
469 "Return visible window containing buffer."
470 (catch 'got-a-vis
471 (walk-windows
472 (function (lambda (win)
473 (if (and (eq (window-buffer win) buffer)
474 (equal (frame-parameter
475 (selected-frame) 'display)
476 (frame-parameter
477 (window-frame win) 'display)))
478 (throw 'got-a-vis win))))
479 nil 'visible)
480 nil))
481
482 (defun multishell-read-bare-shell-buffer-name (prompt default)
483 "PROMPT for shell buffer name, sans asterisks. Indicate DEFAULT in prompt.
484
485 Return the supplied name, if provided, else return nil."
486 (let* ((candidates
487 (append
488 ;; Plain shell buffer names appended with names from name/path hist:
489 (remq nil
490 (mapcar (lambda (buffer)
491 (let* ((name (multishell-unbracket-asterisks
492 (buffer-name buffer))))
493 (and (buffer-live-p buffer)
494 (with-current-buffer buffer
495 ;; Shell mode buffers.
496 (derived-mode-p 'shell-mode))
497 (not (multishell-history-entries name))
498 name)))
499 (buffer-list)))
500 multishell-history))
501 (got (completing-read prompt
502 ;; COLLECTION:
503 (reverse candidates)
504 ;; PREDICATE:
505 nil
506 ;; REQUIRE-MATCH:
507 'confirm
508 ;; INITIAL-INPUT
509 nil
510 ;; HIST:
511 'multishell-history)))
512 (if (not (string= got ""))
513 got
514 nil)))
515
516 (defun multishell-resolve-target-name-and-path (path-ish)
517 "Given name/tramp-path PATH-ISH, resolve buffer name and initial directory.
518
519 The name is the part of the string up to the first '/' slash, if
520 any. Missing pieces are filled in from remote path elements, if
521 any, and multishell history. Given a path and no name, either the
522 host-name, domain-name, final directory name, or local host name
523 is used.
524
525 Return them as a list (name path), with name asterisk-bracketed
526 and path nil if none resolved."
527 (let* ((splat (multishell-split-entry (or path-ish "")))
528 (name (car splat))
529 (path (cadr splat)))
530 (if path
531 (if (not name)
532 (setq name
533 (if (file-remote-p path)
534 (let ((vec (tramp-dissect-file-name path)))
535 (or (tramp-file-name-host vec)
536 (tramp-file-name-domain vec)
537 (tramp-file-name-localname vec)
538 system-name))
539 multishell-primary-name)))
540 ;; No path - get one from history, if present.
541 (when (not name)
542 (setq name multishell-primary-name))
543 (mapcar #'(lambda (entry)
544 (when (or (not path) (string= path ""))
545 (setq path (cadr (multishell-split-entry entry)))))
546 (multishell-history-entries
547 (multishell-unbracket-asterisks name))))
548 (list (multishell-bracket-asterisks name) path)))
549
550 (defun multishell-bracket-asterisks (name)
551 "Return a copy of name, ensuring it has an asterisk at the beginning and end."
552 (if (not (string= (substring name 0 1) "*"))
553 (setq name (concat "*" name)))
554 (if (not (string= (substring name -1) "*"))
555 (setq name (concat name "*")))
556 name)
557 (defun multishell-unbracket-asterisks (name)
558 "Return a copy of name, removing asterisks, if any, at beginning and end."
559 (if (string= (substring name 0 1) "*")
560 (setq name (substring name 1)))
561 (if (string= (substring name -1) "*")
562 (setq name (substring name 0 -1)))
563 name)
564
565 (defun multishell-start-shell-in-buffer (buffer-name path)
566 "Ensure a shell is started, with name NAME and PATH."
567 ;; We work around shell-mode's bracketing of the buffer name, and do
568 ;; some tramp-mode hygiene for remote connections.
569
570 (let* ((buffer buffer-name)
571 (prog (or explicit-shell-file-name
572 (getenv "ESHELL")
573 (getenv "SHELL")
574 "/bin/sh"))
575 (name (file-name-nondirectory prog))
576 (startfile (concat "~/.emacs_" name))
577 (xargs-name (intern-soft (concat "explicit-" name "-args")))
578 is-remote)
579 (set-buffer buffer-name)
580 (setq is-remote (and path (file-remote-p path)))
581 (when (and is-remote
582 (derived-mode-p 'shell-mode)
583 (not (comint-check-proc (current-buffer))))
584 ;; We're returning to an already established but disconnected remote
585 ;; shell, tidy it:
586 (tramp-cleanup-connection
587 (tramp-dissect-file-name default-directory 'noexpand)
588 'keep-debug 'keep-password))
589 (when is-remote
590 (message "Connecting to %s" path))
591 (if (and path (not (string= path "")))
592 (cd path))
593 (setq buffer (set-buffer (apply 'make-comint
594 (multishell-unbracket-asterisks buffer-name)
595 prog
596 (if (file-exists-p startfile)
597 startfile)
598 (if (and xargs-name
599 (boundp xargs-name))
600 (symbol-value xargs-name)
601 '("-i")))))
602 (shell-mode)))
603
604 (defun multishell-track-dirchange (name newpath)
605 "Change multishell history entry to track current directory."
606 (let* ((entries (multishell-history-entries name)))
607 (dolist (entry entries)
608 (let* ((name-path (multishell-split-entry entry))
609 (name (car name-path))
610 (path (cadr name-path)))
611 (when path
612 (let* ((is-remote (file-remote-p path))
613 (vec (and is-remote (tramp-dissect-file-name path nil)))
614 (localname (if is-remote
615 (tramp-file-name-localname vec)
616 path))
617 (newlocalname
618 (replace-regexp-in-string (if (string= localname "")
619 "$"
620 (regexp-quote localname))
621 ;; REP
622 newpath
623 ;; STRING
624 localname
625 ;; FIXEDCASE
626 t
627 ;; LITERAL
628 t
629 ))
630 (newpath (if is-remote
631 (tramp-make-tramp-file-name (aref vec 0)
632 (aref vec 1)
633 (aref vec 2)
634 newlocalname
635 (aref vec 4))
636 newlocalname))
637 (newentry (concat name newpath))
638 (membership (member entry multishell-history)))
639 (when membership
640 (setcar membership newentry))))))))
641 (defvar multishell-was-default-directory ()
642 "Provide for tracking directory changes.")
643 (make-variable-buffer-local 'multishell-was-default-directory)
644 (defun multishell-post-command-business ()
645 "Do multishell bookkeeping."
646 ;; Update multishell-history with dir changes.
647 (condition-case err
648 (when (and multishell-history-entry-tracks-current-directory
649 (derived-mode-p 'shell-mode))
650 (let ((curdir (if (file-remote-p default-directory)
651 (tramp-file-name-localname
652 (tramp-dissect-file-name default-directory))
653 default-directory)))
654 (when (and multishell-was-default-directory
655 (not (string= curdir multishell-was-default-directory)))
656 (multishell-track-dirchange (multishell-unbracket-asterisks
657 (buffer-name))
658 curdir))
659 (setq multishell-was-default-directory curdir)))
660 ;; To avoid disruption as a pervasive hook function, swallow all errors:
661 (error
662 (message "multishell-post-command-business error: %s" err))))
663 (add-hook 'post-command-hook 'multishell-post-command-business)
664
665 (defun multishell-split-entry (entry)
666 "Given multishell name/path ENTRY, return the separated name and path pair.
667
668 Returns nil for empty parts, rather than the empty string."
669 (string-match "^\\([^/]*\\)\\(/?.*\\)?" entry)
670 (let ((name (match-string 1 entry))
671 (path (match-string 2 entry)))
672 (and (string= name "") (setq name nil))
673 (and (string= path "") (setq path nil))
674 (list name path)))
675
676 (provide 'multishell)
677
678 ;;; multishell.el ends here