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