+ "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.
+
+;; (when (not (member 'multishell-primary-name
+;; savehist-additional-variables))
+;; (setq savehist-additional-variables
+;; (cons 'multishell-primary-name savehist-additional-variables)))
+
+;; Multiple entries happen because completion also adds name to history.
+(defun multishell-register-name-to-path (name path)
+ "Add or replace entry associating NAME with PATH in `multishell-history'.
+
+If NAME already had a PATH and new PATH is empty, retain the prior one.
+
+Promote added/changed entry to the front of the list."
+ ;; Add or promote to the front, tracking path changes in the process.
+ (let* ((entries (multishell-history-entries name))
+ (path (or path "")))
+ (dolist (entry entries)
+ (when (string= path "")
+ ;; Retain explicit established path.
+ (setq path (cadr (multishell-split-entry entry))))
+ (setq multishell-history (delete entry multishell-history)))
+ (setq multishell-history (push (concat name path)
+ multishell-history))))
+
+(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))