;; a la `pop-to-buffer' - plus a keybinding. Together, they enable you to:
;;
;; * Get to the input point from wherever you are in a shell buffer,
-;; * ... or to a shell buffer if you're not currently in one.
+;; * ... or to one of your shell buffers if you're not currently in one,
+;; * ... with just a keystroke.
;; * Use universal arguments to launch and choose among alternate shell buffers,
;; * ... and select which is default.
;; * Append a path to a new shell name to launch a shell in that directory,
;; * ... and use a path with Emacs tramp syntax to launch a remote shell.
;;
-;; Customize-group `multishell` to select and activate a keybinding and set
-;; various behaviors.
+;; For example:
;;
-;; See the multishell-pop-to-shell docstring for details.
+;; * '/ssh:example.net:/' for a shell buffer in / 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*".
+;;
+;; (NOTE that you can try to use, eg, '/ssh:example.net:' to get to your
+;; home dir on example.net (if you had one), but that sometimes fails -
+;; particularly remote+sudo to homedir - on an obscure bug. Until that's
+;; fixed, you may need to start remote+sudo shells with an explicit path,
+;; then cd to the homedir.)
+;;
+;; Customize-group `multishell' to select and activate a keybinding and set
+;; various behaviors. Customize-group `savehist' to preserve buffer
+;; names/paths across emacs sessions.
+;;
+;; See the `multishell-pop-to-shell' docstring for details.
;;
;;; Change Log:
;;
+;; 2016-01-16 1.0.5 Ken Manheimer:
+;; - Fix - recognize and respect tramp path syntax to start in home dir
+;; - Offer to user to remove shell's history entry when buffer is killed
+;; - Fix - prevent duplicate entries for same name but different paths
+;; - Simplify history var name, migrate existing history from old name if any
+;; 2016-01-06 Ken Manheimer - Released
;; 2016-01-02 Ken Manheimer - working on this in public, but not yet released.
;;
;;; TODO:
;;
-;; * Preserveable (savehist) history that associates names with paths
-;; - Using an association list between names and paths
-;; - Searched for search backwards/forwards on isearch-like M-r/M-s bindings
-;; - *Not* searched for regular completion
-;; - Editible
-;; - New shell prompts for confirmation
-;; - Including path from history, if any
-;; - which offers opportunity to edit association
-;; - New association overrides previous
-;; - History tracks buffer disposition
-;; - Track buffer name change using buffer-list-update-hook
-;; - Deleting buffer removes history entry!
-;; - Option to track last directory - multishell-remember-last-dir
-;; - Include note about tramp not tracking remote dirs well
-;; - use `M-x shell-resync-dirs'; I bind to M-return
-;; * Customize activation of savehist
-;; - Customize entry has warning about activating savehist
-;; - Adds the name/path association list to savehist-additional-variables
-;; - Activates savehist, if inactive
+;; * Isolate frequent failure with remote tramp home-dir syntax (`/host.dom:')
+;; * Track the current directory in each buffer's history entry.
+;; * Provide toggle to see completions buffer with just buffer names or + paths
;;; Code:
:group 'multishell)
(defvar multishell--responsible-for-command-key nil
- "Multishell internal.")
+ "Coordination for multishell key assignment.")
(defun multishell-activate-command-key-setter (symbol setting)
"Implement `multishell-activate-command-key' choice."
(set-default 'multishell-activate-command-key setting)
:type 'boolean
:group 'multishell)
-;; (defcustom multishell-persist-shell-names nil
-;; "Remember shell name/path associations across sessions. Note well:
-;; This will activate minibuffer history persistence, in general, if it's not
-;; already active."
-;; :type 'boolean
-;; :set 'multishell-activate-persistence
-;; :group 'shell)
-
-(defvar multishell-name-to-path-history nil
- "Associations from shell buffer names to their default paths")
+(defcustom multishell-history-entry-tracks-current-directory t
+ "Modify shell buffer's multishell entry to track the current directory.
+
+When set, the path part of the name/path entry for each shell
+will track the current directory of the shell with emacs. If
+`savehist' is active, the directory tracking will extend across
+emacs sessions."
+ :type 'boolean
+ :group 'multishell)
+
+(defvar multishell-history nil
+ "Name/path entries, most recent first.")
+(when (and (not multishell-history)
+ (boundp 'multishell-buffer-name-history)
+ multishell-buffer-name-history)
+ ;; Migrate few users who had old var to new.
+ (setq multishell-history multishell-buffer-name-history)
+ )
(defvar multishell-primary-name "*shell*"
"Shell name to use for un-modified multishell-pop-to-shell buffer target.")
-(defvar multishell-buffer-name-history nil
- "Distinct multishell-pop-to-shell completion history container.")
-(defvar multishell-names-to-paths nil
- "Multishell buffer name/path associations.")
+
+;; There is usually only one entry per name, but disruptions happen.
(defun multishell-register-name-to-path (name path)
- "Associate NAME with PATH in `multishell-name-to-path-history'.
-
-Remove registration for NAME if PATH is nil (but not the empty string)."
- (if path
- (let* ((got (assoc name multishell-name-to-path-history)))
- (cond ((or (not path)(string= path ""))
- ;; Remove entry, if present:
- (if got
- (setq multishell-name-to-path-history
- (delete got multishell-name-to-path-history))))
- (got
- ;; Replace the path of the existing entry:
- (setcdr got path))
- ;; Add a new entry:
- (t (setq multishell-name-to-path-history
- (cons (cons name path) multishell-name-to-path-history)))))))
+ "Add or replace entry associating NAME with PATH in `multishell-history'.
+
+Promote to 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))
+ (becomes (concat name path)))
+ (dolist (entry entries)
+ (setq multishell-history (delete entry multishell-history)))
+ (setq multishell-history (push becomes 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))
(defun multishell-pop-to-shell (&optional arg)
"Easily navigate to and within multiple shell buffers, local and remote.
For example:
-* Use '/ssh:example.net:/' for a shell buffer on example.net named
- \"example.net\".
-* '\#ex/ssh:example.net|sudo:root@example.net:/' for a root shell on
- example.net named \"#ex\"."
+* Use '/ssh:example.net:/home/myaccount' for a shell buffer in
+ /home/myaccount on example.net; the buffer will be named
+ \"*example.net*\".
+* '\#ex/ssh:example.net|sudo:root@example.net:/etc' for a root
+ shell in /etc on example.net named \"*#ex*\".
-;; I'm leaving the following out of the docstring for now because just
-;; saving the buffer names, and not the paths, yields sometimes unwanted
-;; behavior.
+\(NOTE that you can specify a remote homedir using tramp syntax,
+eg '/ssh:example.net:'. However that sometimes fails on an
+obscure bug - particularly for remote+sudo with homedir
+syntax. Until fixed, you may need to start remote+sudo shells
+with an explicit path, then cd ~.)
-;; ===== Persisting your alternate shell buffer names and paths:
+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.
-;; You can use emacs builtin SaveHist to preserve your alternate
-;; shell buffer names and paths across emacs sessions. To do so,
-;; customize the `savehist' group, and:
+To remove a shell buffer's history entry, kill the buffer and
+affirm removal of the entry when prompted.
-;; 1. Add `multishell-name-to-path-history' to Savehist Additional
-;; Variables.
-;; 2. Activate Savehist Mode, if not already activated.
-;; 3. Save.
+===== Activate savehist to persisting your shell buffer names and paths:
+
+To have emacs maintain your history of shell buffer names and paths,
+customize the savehist group to activate savehist."
(interactive "P")
(curr-buff-proc (get-buffer-process from-buffer))
(target-buffer (if from-buffer-is-shell
from-buffer
- (get-buffer target-shell-buffer-name)))
+ (let ((got (get-buffer target-shell-buffer-name)))
+ (if (buffer-live-p got)
+ got
+ (kill-buffer got)
+ (get-buffer target-shell-buffer-name)))))
inwin
already-there)
;; We're in the buffer. Activate:
- (cond ((not (comint-check-proc (current-buffer)))
- (multishell-start-shell-in-buffer (buffer-name (current-buffer))
- use-default-dir))
- (use-default-dir
- (cd use-default-dir)))
+ (if (not (comint-check-proc (current-buffer)))
+ (multishell-start-shell-in-buffer (buffer-name (current-buffer))
+ use-default-dir))
;; If the destination buffer has a stopped process, resume it:
(let ((process (get-buffer-process (current-buffer))))
(if (and process (equal 'stop (process-status process)))
(continue-process process)))
- (multishell-register-name-to-path target-shell-buffer-name
+ (multishell-register-name-to-path (multishell-unbracket-asterisks
+ target-shell-buffer-name)
use-default-dir)
(when (or already-there
(equal (current-buffer) from-buffer))
(and (get-buffer-process from-buffer)
(goto-char (process-mark (get-buffer-process from-buffer)))))))
+(defun multishell-kill-buffer-query-function ()
+ "Offer to remove multishell-history entry for buffer."
+ ;; Removal choice is crucial, so users can, eg, kill and a runaway shell
+ ;; and keep the history entry to easily restart it.
+ ;;
+ ;; We use kill-buffer-query-functions instead of kill-buffer-hook because:
+ ;;
+ ;; 1. It enables the user to remove the history without killing the buffer,
+ ;; by cancelling the kill-buffer process after affirming history removal.
+ ;; 2. kill-buffer-hooks often fails to run when killing shell buffers!
+ ;; I've failed to resolve that, and like the first reason well enough.
+
+ ;; (Use condition-case to avoid inadvertant disruption of kill-buffer
+ ;; activity. kill-buffer happens behind the scenes a whole lot.)
+ (condition-case anyerr
+ (let ((entries (and (derived-mode-p 'shell-mode)
+ (multishell-history-entries
+ (multishell-unbracket-asterisks (buffer-name))))))
+ (dolist (entry entries)
+ (when (and entry
+ (y-or-n-p (format "Remove multishell history entry `%s'? "
+ entry)))
+ (setq multishell-history
+ (delete entry multishell-history)))))
+ (error nil))
+ t)
+(add-hook 'kill-buffer-query-functions 'multishell-kill-buffer-query-function)
+
(defun multishell-get-visible-window-for-buffer (buffer)
"Return visible window containing buffer."
(catch 'got-a-vis
Return the supplied name bracketed with the asterisks, or specified DEFAULT
on empty input."
- (let* ((ntph multishell-name-to-path-history)
- (candidates
+ (let* ((candidates
(append
+ ;; Plain shell buffer names appended with names from name/path hist:
(remq nil
(mapcar (lambda (buffer)
- (let* ((name (buffer-name buffer))
- (already
- (assoc (multishell-unbracket-asterisks name)
- ntph)))
- (if already
- nil
- (when (with-current-buffer buffer
- (derived-mode-p 'shell-mode))
- ;; Shell mode buffers.
- (setq name (if (> (length name) 2)
- ;; Strip asterisks.
- (substring name 1
- (1- (length name)))
- name))))))
+ (let* ((name (multishell-unbracket-asterisks
+ (buffer-name buffer))))
+ (and (buffer-live-p buffer)
+ (with-current-buffer buffer
+ ;; Shell mode buffers.
+ (derived-mode-p 'shell-mode))
+ (not (multishell-history-entries name))
+ name)))
(buffer-list)))
- (mapcar #'(lambda (assoc)
- (concat (car assoc) (cdr assoc)))
- multishell-name-to-path-history)))
+ multishell-history))
(got (completing-read prompt
;; COLLECTION:
- candidates
+ (reverse candidates)
;; PREDICATE:
nil
;; REQUIRE-MATCH:
'confirm
- ;; INITIAL-INPUT:
+ ;; INITIAL-INPUT
nil
;; HIST:
- 'multishell-buffer-name-history
- )))
- (if (not (string= got "")) (multishell-bracket-asterisks got) default)))
+ 'multishell-history)))
+ (if (not (string= got ""))
+ (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.
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)
+ ((string-match "^\\*\\([^/]*\\)\\(/.*\\)\\*" path-ish)
;; We have a path, use it
- (let ((overt-name (match-string 1 path-ish))
- (overt-path (match-string 2 path-ish))
- (trailing-name (match-string 3 path-ish)))
+ (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= overt-path "") (setq overt-path nil))
- (if (string= trailing-name "") (setq trailing-name nil))
- (setq path (concat overt-path trailing-name))
+ (if (string= path "") (setq path nil))
(setq name
(multishell-bracket-asterisks
(or overt-name
(or (tramp-file-name-host vec)
(tramp-file-name-domain vec)
(tramp-file-name-localname vec)
- trailing-name
system-name))
(multishell-unbracket-asterisks
multishell-primary-name)))))))
"/bin/sh"))
(name (file-name-nondirectory prog))
(startfile (concat "~/.emacs_" name))
- (xargs-name (intern-soft (concat "explicit-" name "-args"))))
+ (xargs-name (intern-soft (concat "explicit-" name "-args")))
+ is-remote)
(set-buffer buffer-name)
(if (and path (not (string= path "")))
(setq default-directory path))
- (when (and (file-remote-p default-directory)
+ (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
(tramp-cleanup-connection
(tramp-dissect-file-name default-directory 'noexpand)
'keep-debug 'keep-password))
- ;; (cd default-directory) will reconnect a disconnected remote:
- (cd default-directory)
+ ;; (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.
+ (when (and (stringp (cadr err))
+ (string-equal (cadr err)
+ "Selecting deleted buffer"))
+ (signal (car err)
+ (list
+ (format "Tramp shell can fail on homedir paths, %s (\"%s\")"
+ "please try with an explicit path"
+ (cadr err)))))))
(setq buffer (set-buffer (apply 'make-comint
(multishell-unbracket-asterisks buffer-name)
prog
'("-i")))))
(shell-mode)))
+(defun multishell-track-dirchange (name newpath)
+ "Change multishell history entry to track current directory."
+ (let* ((entries (multishell-history-entries name)))
+ (dolist (entry entries)
+ (let* ((name-path (multishell-split-entry-name-and-tramp entry))
+ (name (car name-path))
+ (path (cadr name-path))
+ (is-remote (file-remote-p path))
+ (vec (and is-remote (tramp-dissect-file-name path nil)))
+ (localname (if is-remote
+ (tramp-file-name-localname vec)
+ path))
+ (newlocalname
+ (replace-regexp-in-string (if (string= localname "")
+ "$"
+ (regexp-quote localname))
+ ;; REP
+ newpath
+ ;; STRING
+ localname
+ ;; FIXEDCASE
+ t
+ ;; LITERAL
+ t
+ ))
+ (newpath (if is-remote
+ (tramp-make-tramp-file-name (aref vec 0)
+ (aref vec 1)
+ (aref vec 2)
+ newlocalname
+ (aref vec 4))
+ newlocalname))
+ (newentry (concat name newpath))
+ (membership (member entry multishell-history)))
+ (when membership
+ (setcar membership newentry))))))
+(defvar multishell-was-default-directory ()
+ "Provide for tracking directory changes.")
+(make-variable-buffer-local 'multishell-was-default-directory)
+(defun multishell-post-command-business ()
+ "Do multishell bookkeeping."
+ ;; Update multishell-history with dir changes.
+ (condition-case err
+ (when (and multishell-history-entry-tracks-current-directory
+ (derived-mode-p 'shell-mode))
+ (let ((curdir (if (file-remote-p default-directory)
+ (tramp-file-name-localname
+ (tramp-dissect-file-name default-directory))
+ default-directory)))
+ (when (and multishell-was-default-directory
+ (not (string= curdir multishell-was-default-directory)))
+ (multishell-track-dirchange (multishell-unbracket-asterisks
+ (buffer-name))
+ curdir))
+ (setq multishell-was-default-directory curdir)))
+ ;; To avoid disruption as a pervasive hook function, swallow all errors:
+ (error nil)))
+(add-hook 'post-command-hook 'multishell-post-command-business)
+
+(defun multishell-split-entry-name-and-tramp (entry)
+ "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)))
+
(provide 'multishell)
;;; multishell.el ends here