- (multishell-bracket-asterisks got)
- default)))
-
-(defun multishell-derive-target-name-and-path (path-ish)
- "Give tramp-style PATH-ISH, determine target name and default directory.
-
-The name is the part of the string before the initial '/' slash,
-if any. Otherwise, it's either the host-name, domain-name, final
-directory name, or local host name. The path is everything
-besides the string before the initial '/' slash.
-
-Return them as a list (name dir), with dir nil if none given."
- (let (name (path "") dir)
- (cond ((string= path-ish "") (setq dir multishell-primary-name))
- ((string-match "^\\*\\([^/]*\\)\\(/.*\\)\\*" path-ish)
- ;; We have a path, use it
- (let ((overt-name (match-string 1 path-ish)))
- (setq path (match-string 2 path-ish))
- (if (string= overt-name "") (setq overt-name nil))
- (if (string= path "") (setq path nil))
- (setq name
- (multishell-bracket-asterisks
- (or overt-name
- (if (file-remote-p path)
- (let ((vec (tramp-dissect-file-name path)))
- (or (tramp-file-name-host vec)
- (tramp-file-name-domain vec)
- (tramp-file-name-localname vec)
- system-name))
- (multishell-unbracket-asterisks
- multishell-primary-name)))))))
- (t (setq name (multishell-bracket-asterisks path-ish))))
- (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)
-
-(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")))
- 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 empty (homedir) path"
- "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)))
+ got
+ nil)))
+
+(defun multishell-resolve-target-name-and-path (shell-spec)
+ "Given name/tramp-style address shell spec, resolve buffer name and directory.
+
+The name is the part of the string up to the first '/' slash, if
+any. Missing pieces are filled in from remote path elements, if
+any, and multishell history. Given a tramp-style remote address
+and no name part, either the user@host is used for the buffer
+name, if a user is specified, or just the host.
+
+Return them as a list: (name path), with name asterisk-bracketed
+and path nil if none is resolved."
+ (let* ((splat (multishell-split-entry (or shell-spec "")))
+ (path (cadr splat))
+ (name (or (car splat) (multishell-name-from-entry path))))
+ (when (not path)
+ ;; Get path from history, if present.
+ (dolist (entry
+ (multishell-history-entries
+ (multishell-unbracket name)))
+ (when (or (not path) (string= path ""))
+ (setq path (cadr (multishell-split-entry entry))))))
+ (list (multishell-bracket name) path)))
+
+(defun multishell-name-from-entry (entry)
+ "Derive a name for a shell buffer according to ENTRY."
+ (if (not entry)
+ (multishell-unbracket multishell-primary-name)
+ (let* ((splat (multishell-split-entry entry))
+ (name (car splat))
+ (path (cadr splat)))
+ (or name
+ (if (file-remote-p path)
+ (let ((host (file-remote-p path 'host))
+ (user (file-remote-p path 'user)))
+ (cond ((and host user)
+ (format "%s@%s" user host))
+ (host host)
+ (user user)
+ ((system-name))))
+ (multishell-unbracket multishell-primary-name))))))
+
+(declare-function tramp-dissect-file-name "tramp")
+(declare-function tramp-cleanup-connection "tramp")
+
+(defun multishell-start-shell-in-buffer (path)
+ "Start, restart, or continue a shell in BUFFER-NAME on PATH."
+ (let* ((is-active (comint-check-proc (current-buffer))))
+
+ (when (and path (not is-active))
+
+ (when (and (derived-mode-p 'shell-mode) (file-remote-p path))
+ ;; Returning to disconnected remote shell - tidy up:
+ (tramp-cleanup-connection
+ (tramp-dissect-file-name default-directory 'noexpand)
+ 'keep-debug 'keep-password))
+
+ (when (file-remote-p path) (message "Connecting to %s" path))
+ (cd path))
+
+ (shell (current-buffer))))
+
+(defun multishell-homedir-shorthand-p (dirpath)
+ "t if dirpath is an unexpanded remote homedir spec."
+ ;; Workaround to recognize tramp-style homedir shorthand, "...:" and "...:~".
+ (let ((localname (file-remote-p dirpath 'localname)))
+ (and localname
+ (or
+ ;; No directory path and no connection to expand homedir:
+ (string= localname "")
+ ;; Original path doesn't equal expanded homedir:
+ (save-match-data
+ (not (string-match (concat (regexp-quote localname) "/?$")
+ dirpath)))))))
+;; (assert (multishell-homedir-shorthand-p "/ssh:myhost.net:")
+;; (assert (not (multishell-homedir-shorthand-p "/home/klm")))
+;; (assert (not (multishell-homedir-shorthand-p "/ssh:myhost.net:/home/me")))