;; Copyright (C) 1999-2016 Free Software Foundation, Inc. and Ken Manheimer
;; Author: Ken Manheimer <ken.manheimer@gmail.com>
-;; Version: 1.0.6
+;; Version: 1.0.8
;; Created: 1999 -- first public availability
;; Keywords: processes
;; URL: https://github.com/kenmanheimer/EmacsMultishell
;; ... and use a path with Emacs tramp syntax to launch a remote shell -
;; for example:
;;
-;; * `#root/sudo:root@localhost:/etc` for a buffer named "#root" with a
+;; * `#root/sudo:root@localhost:/etc` for a buffer named "*#root*" with a
;; root shell starting in /etc.
;;
-;; * `/ssh:example.net:/` for a shell buffer in / on example.net.
+;; * `/ssh:example.net:` for a shell buffer in your homedir on example.net.
;; The buffer will be named "*example.net*".
;;
-;; * `#ex/ssh:example.net|sudo:root@example.net:/etc` for a root shell
-;; starting in /etc on example.net named "*#ex*".
+;; * `#ex/ssh:example.net|sudo:root@example.net:/var/log` for a root shell
+;; starting in /var/log on example.net named "*#ex*".
;;
;; * 'interior/ssh:gateway.corp.com|ssh:interior.corp.com:' to go via
;; gateway.corp.com to your homedir on interior.corp.com. The buffer
;;
;; Please use
;; [the multishell repository](https://github.com/kenmanheimer/EmacsMultishell)
-;; issue tracker to report problems, suggestions, etc.
-;;
-;; (NOTE - tramp sometimes fails to open specifically a remote shell to
-;; sudo to a homedir, eg `/ssh:example.net|sudo:root:` or
-;; `/ssh:example.net|sudo:root:~`. Once it fails for a specific path, that
-;; path won't work for the rest of the session. Non-homedir remote access
-;; isn't disrupted. You can always work around this by switching to an
-;; explicit, non-homedir remote path when the problem occurs, and then
-;; cd'ing to wherever, including your homedir, in the remote shell.)
+;; issue tracker to report problems, suggestions, etc, and see that
+;; repository for a bit more documentation.
;;
;; Change Log:
;;
+;; * 2016-01-24 1.0.8 Ken Manheimer:
+;; - Work around the shell/tramp mishandling of remote+sudo+homedir problem!
+;; The work around is clean and simple, basically using high-level `cd'
+;; API and not messing with the low-level default-directory setting.
+;; (Turns out the problem was not in my local config. Good riddance to the
+;; awkward failure handler!)
+;; - Clean up code resolving the destination shell, starting to document the
+;; decision tree in the process. See getting-to-a-shell.md in the
+;; multishell repository, https://github.com/kenmanheimer/EmacsMultishell
+;; - There may be some shake-out on resolving the destination shell, but
+;; this release gets the fundamental functionality soundly in place.
+;; * 2016-01-23 1.0.7 Ken Manheimer:
+;; - Remove notes about tramp remote+sudo+homedir problem. Apparently it's
+;; due to something in my local site configuration (happens with -q but
+;; not -Q).
;; * 2016-01-22 1.0.6 Ken Manheimer:
;; - Add multishell-version function.
;; - Tweak commentary/comments/docstrings.
;; (Currently the only UI mechanism to remove history entries.)
;; - Fix - prevent duplicate entries for same name but different paths
;; - Fix - recognize and respect tramp path syntax to start in home dir
-;; - But tramp bug, remote|sudo to homedir, often fails, gets wedged.
;; - Simplify history var name, migrate existing history if any from old name
;; * 2016-01-04 1.0.4 Ken Manheimer - Released to ELPA
;; * 2016-01-02 Ken Manheimer - working on this in public, but not yet released.
;;
;; TODO:
;;
-;; * Isolate tramp's sporadic failure to connect to remote|sudo+homedir
-;; syntax
-;; (eg, /ssh:xyz.com|sudo:root@xyz.com: or /ssh:xyz.com|sudo:root@xyz.com:~)
;; * Find suitable, internally consistent ways to tidy completions, eg:
;; - first list completions for active shells, then present but inactive,
;; then historical
;; - some way for user to toggle between presenting just buffer names vs
;; full buffer/path
;; - without cutting user off from easy editing of path
+;; - maybe use keybindings that wrap minibuffer completion keys
+;; - minibuffer-local-completion-map, minibuffer-local-must-match-map
+;; - setup minibuffer with these vars just before doing completions
+;; - minibuffer exit reverts these vars, if necessary
+;; - toggles between name and name/path if last command was one of them
+;; - and an instruction in the completion buffer
+;; - "complete again immediately to toggle name vs name/path completions"
+;; * Add custom shell launch prep actions
+;; - shell commands to execute when shell name or path matches a regexp
+;; - list of [regexp, which (name, path, or both), command]
+;; - for, eg, knock commands or interface activations, whatever
;; * Try minibuffer field boundary at beginning of tramp path, to see whether
;; the field boundary magically enables tramp path completion.
;; * Assess whether deletion of history entry via kill-buffer is sufficient.
(require 'shell)
(require 'savehist)
-(defvar multishell-version "1.0.6")
+(defvar multishell-version "1.0.8")
(defun multishell-version (&optional here)
"Return string describing the loaded multishell version."
(interactive "P")
:group 'multishell)
;; Implement the key customization whenever the package is loaded:
-(with-eval-after-load "multishell"
- (multishell-implement-command-key-choice))
+(if (fboundp 'with-eval-after-load)
+ (with-eval-after-load "multishell"
+ (multishell-implement-command-key-choice))
+ (eval-after-load "multishell"
+ (multishell-implement-command-key-choice)))
(defcustom multishell-pop-to-frame nil
"*If non-nil, jump to a frame already showing the shell, if another one is.
(defvar multishell-primary-name "*shell*"
"Default shell name for un-modified multishell-pop-to-shell buffer target.
-This is adjusted by `multishell-pop-to-shell' when it is
-invoked (with doubled universal argument) to set the default.
-
-To preserve changes to this setting across emacs restarts, add it
-to `savehist-additional-variables' by customizing the latter.")
-
-;;; Can't just add multishell-primary-name to savehist-additional-variables
-;;; - it'll be lost any time the user runs emacs without loading
-;;; multishell. So instead, inform the user that they can customize
-;;; savehist-additional-variables.
-;;;
-;;; I suspect that including savehist-additional-variables *on*
-;;; savehist-additional-variables could avoid this problem, as long as it
-;;; doesn't conflict with user customizations. However, even if that works,
-;;; doing so from multishell would change a behavior (for the better, but)
-;;; beyond multishell's scope, making the change hard to track down.
+This is set by `multishell-pop-to-shell' as the current default,
+when invoked with doubled universal argument.
-;; (when (not (member 'multishell-primary-name
-;; savehist-additional-variables))
-;; (setq savehist-additional-variables
-;; (cons 'multishell-primary-name savehist-additional-variables)))
+If you want the designated primary that you have at the end of
+one emacs session to be resumed at the next, customize
+`savehist-additional-variables' to include the
+`multishell-primary-name'.")
;; Multiple entries happen because completion also adds name to history.
(defun multishell-register-name-to-path (name path)
(defun multishell-history-entries (name)
"Return `multishell-history' entry that starts with NAME, or nil if none."
- (let ((match-expr (concat "^" name "\\\(/.*$\\\)?$"))
- got)
- (dolist (entry multishell-history)
- (when (and (string-match match-expr entry)
- (not (member entry got)))
- (setq got (cons entry got))))
- got))
+ (save-match-data
+ (let ((match-expr (concat "^" name "\\\(/.*$\\\)?$"))
+ got)
+ (dolist (entry multishell-history)
+ (when (and (string-match match-expr entry)
+ (not (member entry got)))
+ (setq got (cons entry got))))
+ got)))
(defun multishell-pop-to-shell (&optional arg)
"Easily navigate to and within multiple shell buffers, local and remote.
For example:
-* '#root/sudo:root@localhost:/etc' for a buffer named \"#root\" with a
+* '#root/sudo:root@localhost:/etc' for a buffer named \"*#root*\" with a
root shell starting in /etc.
-* '/ssh:example.net:/' for a shell buffer in / on example.net; the buffer
- will be named \"*example.net*\".
+* '/ssh:example.net:' for a shell buffer in your homedir on example.net.
+ The buffer will be named \"*example.net*\".
-* '#ex/ssh:example.net|sudo:root@example.net:/etc' for a root shell
- starting in /etc on example.net named \"*#ex*\".
+* '#ex/ssh:example.net|sudo:root@example.net:/var/log' for a root shell
+ starting in /var/log on example.net named \"*#ex*\".
* 'interior/ssh:gateway.corp.com|ssh:interior.corp.com:' to go
via gateway.corp.com to your homedir on interior.corp.com. The
of the target account, and relative to the current directory, on
the host where the shell is running.
-\(NOTE - tramp sometimes fails to open specifically a remote shell
-to sudo to a homedir, eg `/ssh:example.net|sudo:root:` or
-`/ssh:example.net|sudo:root:~`. Once it fails for a specific
-path, that path won't work for the rest of the
-session. Non-homedir remote access isn't disrupted. You can
-always work around this by switching to an explicit, non-homedir
-remote path when the problem occurs, and then cd'ing to wherever,
-including your homedir, in the remote shell. Non-homedir initial
-paths aren't disrupted.)
-
You can change the startup path for a shell buffer by editing it
at the completion prompt. The new path will be preserved in
history but will not take effect for an already-running shell.
(let* ((from-buffer (current-buffer))
(from-buffer-is-shell (derived-mode-p 'shell-mode))
+ (primary-name-unbracketed (multishell-unbracket-asterisks
+ multishell-primary-name))
+ (fallthrough-name (if from-buffer-is-shell
+ (buffer-name from-buffer)
+ primary-name-unbracketed))
(doublearg (equal arg '(16)))
- (target-name-and-path (multishell-resolve-target-name-and-path
+ (target-name-and-path
+ (multishell-resolve-target-name-and-path
(if arg
- (multishell-read-bare-shell-buffer-name
- (format "Shell buffer name [%s]%s "
- (substring-no-properties
- multishell-primary-name
- 1 (- (length multishell-primary-name) 1))
- (if doublearg " <==" ":"))
- (multishell-unbracket-asterisks multishell-primary-name))
- (multishell-unbracket-asterisks multishell-primary-name))))
- (use-default-dir (cadr target-name-and-path))
+ (or (multishell-read-bare-shell-buffer-name
+ (format "Shell buffer name [%s]%s "
+ primary-name-unbracketed
+ (if doublearg " <==" ":"))
+ primary-name-unbracketed)
+ primary-name-unbracketed)
+ fallthrough-name)))
+ (use-path (cadr target-name-and-path))
(target-shell-buffer-name (car target-name-and-path))
+ (target-buffer (get-buffer target-shell-buffer-name))
(curr-buff-proc (get-buffer-process from-buffer))
- (target-buffer (if from-buffer-is-shell
- from-buffer
- (get-buffer target-shell-buffer-name)))
inwin
already-there)
;; Register early so the entry is pushed to the front:
(multishell-register-name-to-path (multishell-unbracket-asterisks
target-shell-buffer-name)
- use-default-dir)
+ use-path)
(when doublearg
(setq multishell-primary-name target-shell-buffer-name))
(if (not (comint-check-proc (current-buffer)))
(multishell-start-shell-in-buffer (buffer-name (current-buffer))
- use-default-dir))
+ use-path))
;; If the destination buffer has a stopped process, resume it:
(let ((process (get-buffer-process (current-buffer))))
nil))
(defun multishell-read-bare-shell-buffer-name (prompt default)
- "PROMPT for shell buffer name, sans asterisks.
+ "PROMPT for shell buffer name, sans asterisks. Indicate DEFAULT in prompt.
-Return the supplied name not bracketed with the asterisks, or specified
-DEFAULT on empty input."
+Return the supplied name, if provided, else return nil."
(let* ((candidates
(append
;; Plain shell buffer names appended with names from name/path hist:
'multishell-history)))
(if (not (string= got ""))
got
- default)))
+ nil)))
(defun multishell-resolve-target-name-and-path (path-ish)
"Given name/tramp-path PATH-ISH, resolve buffer name and initial directory.
Return them as a list (name path), with name asterisk-bracketed
and path nil if none resolved."
- (let* ((splat (multishell-split-entry path-ish))
+ (let* ((splat (multishell-split-entry (or path-ish "")))
(name (car splat))
(path (cadr splat)))
(if path
(multishell-unbracket-asterisks name))))
(list (multishell-bracket-asterisks name) path)))
-(defun multishell-bracket-asterisks (name)
- "Return a copy of name, ensuring it has an asterisk at the beginning and end."
- (if (not (string= (substring name 0 1) "*"))
- (setq name (concat "*" name)))
- (if (not (string= (substring name -1) "*"))
- (setq name (concat name "*")))
- name)
-(defun multishell-unbracket-asterisks (name)
- "Return a copy of name, removing asterisks, if any, at beginning and end."
- (if (string= (substring name 0 1) "*")
- (setq name (substring name 1)))
- (if (string= (substring name -1) "*")
- (setq name (substring name 0 -1)))
- name)
-
(defun multishell-start-shell-in-buffer (buffer-name path)
- "Ensure a shell is started, with name NAME and PATH."
- ;; We work around shell-mode's bracketing of the buffer name, and do
- ;; some tramp-mode hygiene for remote connections.
-
- (let* ((buffer buffer-name)
- (prog (or explicit-shell-file-name
- (getenv "ESHELL")
- (getenv "SHELL")
- "/bin/sh"))
- (name (file-name-nondirectory prog))
- (startfile (concat "~/.emacs_" name))
- (xargs-name (intern-soft (concat "explicit-" name "-args")))
+ "Start, restart, or continue a shell in BUFFER-NAME on PATH."
+ (let* ((buffer (get-buffer buffer-name))
is-remote)
- (set-buffer buffer-name)
- (if (and path (not (string= path "")))
- (setq default-directory path))
- (setq is-remote (file-remote-p default-directory))
- (when (and is-remote
- (derived-mode-p 'shell-mode)
- (not (comint-check-proc (current-buffer))))
- ;; We're returning to an already established but disconnected remote
- ;; shell, tidy it:
- (tramp-cleanup-connection
- (tramp-dissect-file-name default-directory 'noexpand)
- 'keep-debug 'keep-password))
- ;; (cd default-directory) will connect if remote:
- (when is-remote
- (message "Connecting to %s" default-directory))
- (condition-case err
- (cd default-directory)
- (error
- ;; Aargh. Need to isolate this tramp bug.
- (if (and (stringp (cadr err))
- (string-equal (cadr err)
- "Selecting deleted buffer"))
- (signal (car err)
- (list
- (format "%s, %s (\"%s\")"
- "Tramp shell can fail on remote|sudo to homedir"
- "please try again with an explicit path"
- (cadr err))))
- (signal (car err)(cdr err)))))
- (setq buffer (set-buffer (apply 'make-comint
- (multishell-unbracket-asterisks buffer-name)
- prog
- (if (file-exists-p startfile)
- startfile)
- (if (and xargs-name
- (boundp xargs-name))
- (symbol-value xargs-name)
- '("-i")))))
- (shell-mode)))
+
+ (set-buffer buffer)
+
+ (when (and path (file-remote-p path))
+
+ (when (and (derived-mode-p 'shell-mode)
+ (not (comint-check-proc (current-buffer))))
+ ;; Returning to disconnected remote shell. Do some tidying:
+ (tramp-cleanup-connection
+ (tramp-dissect-file-name default-directory 'noexpand)
+ 'keep-debug 'keep-password))
+
+ (message "Connecting to %s" path)
+ (cd path))
+
+ (shell buffer)))
(defun multishell-track-dirchange (name newpath)
"Change multishell history entry to track current directory."
"Given multishell name/path ENTRY, return the separated name and path pair.
Returns nil for empty parts, rather than the empty string."
- (string-match "^\\([^/]*\\)\\(/?.*\\)?" entry)
- (let ((name (match-string 1 entry))
- (path (match-string 2 entry)))
- (and (string= name "") (setq name nil))
- (and (string= path "") (setq path nil))
- (list name path)))
+ (save-match-data
+ (string-match "^\\([^/]*\\)\\(/?.*\\)?" entry)
+ (let ((name (match-string 1 entry))
+ (path (match-string 2 entry)))
+ (and (string= name "") (setq name nil))
+ (and (string= path "") (setq path nil))
+ (list name path))))
+(defun multishell-bracket-asterisks (name)
+ "Return a copy of name, ensuring it has an asterisk at the beginning and end."
+ (if (not (string= (substring name 0 1) "*"))
+ (setq name (concat "*" name)))
+ (if (not (string= (substring name -1) "*"))
+ (setq name (concat name "*")))
+ name)
+(defun multishell-unbracket-asterisks (name)
+ "Return a copy of name, removing asterisks, if any, at beginning and end."
+ (if (string= (substring name 0 1) "*")
+ (setq name (substring name 1)))
+ (if (string= (substring name -1) "*")
+ (setq name (substring name 0 -1)))
+ name)
(provide 'multishell)