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