diff --git a/docs/source/usage.rst b/docs/source/usage.rst index baa4404..bbf6b79 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -49,6 +49,7 @@ regularly, you'll want to bind some of these to global shortcuts: pair: variable; mistty-shell-command pair: variable; explicit-shell-file-name pair: variable; shell-file-name + pair: variable; mistty-buffer-name - :kbd:`M-x mistty-create` launches a new interactive shell in a MisTTY buffer. The shell that is launched is the one that's @@ -66,6 +67,9 @@ regularly, you'll want to bind some of these to global shortcuts: directory. This is particularly useful if you want to run a :ref:`tramp`. + To change the way new buffers are named, :kbd:`M-x + customize-option mistty-buffer-name`. + .. index:: pair: command; mistty-create-other-window - :kbd:`M-x mistty-create-other-window` does the same, but opens the @@ -472,6 +476,10 @@ Example: (connection-local-set-profiles '(:machine "myhost.example") 'profile-usr-local-fish) +By default, the name of TRAMP shells include the user and hostname. If +you don't want that, run :kbd:`M-x customize-option +mistty-buffer-name` to change the way buffers are named. + .. _dirtrack: Directory tracking and TRAMP diff --git a/mistty-project.el b/mistty-project.el index 17fc0c1..43b4708 100644 --- a/mistty-project.el +++ b/mistty-project.el @@ -56,11 +56,10 @@ in another window. (mistty-cycle-or-create (lambda (buf) (memq buf bufs)) (lambda (other-window) - (let ((default-directory (project-root pr))) - (with-current-buffer (mistty-create nil other-window) - (rename-buffer (generate-new-buffer-name - (project-prefixed-buffer-name "mistty"))) - (current-buffer)))) + (let ((default-directory (project-root pr)) + (mistty-buffer-name + (cons (concat (project-name pr) "-") mistty-buffer-name))) + (mistty-create nil other-window))) other-window))) (defun mistty-in-project-other-window () diff --git a/mistty.el b/mistty.el index 2e257f2..5473b58 100644 --- a/mistty.el +++ b/mistty.el @@ -148,6 +148,32 @@ spaces, such as the indentation spaces fish adds." :type '(boolean) :group 'mistty) +(defcustom mistty-buffer-name '("mistty" mistty-buffer-name-user mistty-buffer-name-host) + "Name for MisTTY buffers. + +Elements of this list should be either strings or functions +returning strings. `mistty-create' evaluates each one in order to +create the full buffer name, then adds * around and possible <1> +<2> to make them unique. + +By default, for non-TRAMP buffers, this the default is usually +just \"*mistty*\". If connected to another host, it could become +\"*mistty@myhost.example\". If using the sudo method, it could +become \"*mistty-root*\". + +The functions on this list should look at the current +environment, primarily, `default-directory' and +`mistty-shell-command', set to the current command by +`mistty-create', to make their choice. Note that they're not +called from the mistty buffer, but rather from the environment +the buffer will be created." + :type '(repeat (choice string + (function-item mistty-buffer-name-shell) + (function-item mistty-buffer-name-user) + (function-item mistty-buffer-name-host) + function)) + :group 'mistty) + (defface mistty-fringe-face '((t (:foreground "#1b345a"))) "Color of the left fringe or margin that highlights the synced region. @@ -1091,21 +1117,21 @@ Upon success, the function returns the newly-created buffer." current-prefix-arg (mistty--read-default-directory)) default-directory)) - (buf (generate-new-buffer "*mistty*"))) + (mistty-shell-command (or command + (with-connection-local-variables + (or + mistty-shell-command + explicit-shell-file-name + shell-file-name + (getenv "ESHELL") + (getenv "SHELL"))))) + (buf (generate-new-buffer (mistty-new-buffer-name)))) ;; Note that it's important to attach the buffer to a window ;; before executing the command, so that the shell known the size ;; of the terminal from the very beginning. (mistty--pop-to-buffer buf other-window) (with-current-buffer buf - (mistty--exec - (or command - (with-connection-local-variables - (or - mistty-shell-command - explicit-shell-file-name - shell-file-name - (getenv "ESHELL") - (getenv "SHELL"))))) + (mistty--exec mistty-shell-command) buf))) ;;;###autoload @@ -3487,6 +3513,54 @@ Usage example: (mistty--forbid-edit mistty-forbid-edit-map) (t mistty-prompt-map))) +(defun mistty-new-buffer-name () + "Generate a name for a new MisTTY buffer. + +This function interprets the customization option +`mistty-buffer-name' within the current context and returns the +result." + (with-connection-local-variables + (concat "*" + (mapconcat + (lambda (part) + (if (functionp part) + (funcall part) + part)) + mistty-buffer-name) + "*"))) + +(defun mistty-buffer-name-shell () + "Return the short name of the shell to be run. + +For example, if the command that's run is \"/usr/bin/bash\", this +function returns \"bash\". + +This function is meant to be used in the configuration option +`mistty-buffer-name'. It relies on `mistty-shell-command' being +set by `mistty-create' in the environment the function is called." + (file-name-sans-extension + (file-name-nondirectory (if (consp mistty-shell-command) + (car mistty-shell-command) + mistty-shell-command)))) + +(defun mistty-buffer-name-user () + "TRAMP user, if not the current user, as \"-\". + +This function is meant to be used in the configuration option +`mistty-buffer-name'." + (when-let ((remote-user (file-remote-p default-directory 'user))) + (unless (string= (getenv "USER") remote-user) + (concat "-" remote-user)))) + +(defun mistty-buffer-name-host () + "TRAMP host, if not localhost, as \"@\". + +This function is meant to be used in the configuration option +`mistty-buffer-name'." + (when-let ((remote-host (file-remote-p default-directory 'host))) + (unless (string= remote-host (system-name)) + (concat "@" remote-host)))) + (provide 'mistty) ;;; mistty.el ends here diff --git a/mistty.texi b/mistty.texi index ffde5a4..c46ec02 100644 --- a/mistty.texi +++ b/mistty.texi @@ -218,6 +218,7 @@ regularly, you’ll want to bind some of these to global shortcuts: @geindex variable; mistty-shell-command @geindex variable; explicit-shell-file-name @geindex variable; shell-file-name +@geindex variable; mistty-buffer-name @itemize - @@ -244,6 +245,9 @@ With a prefix argument, this command asks for a directory for the new shell, instead of using the current buffer’s current directory. This is particularly useful if you want to run a @ref{c,,Remote Shells with TRAMP}. + +To change the way new buffers are named, @code{M-x +customize-option mistty-buffer-name}. @end itemize @geindex command; mistty-create-other-window @@ -810,6 +814,10 @@ Example: 'profile-usr-local-fish) @end example +By default, the name of TRAMP shells include the user and hostname. If +you don’t want that, run @code{M-x customize-option +mistty-buffer-name} to change the way buffers are named. + @node Directory tracking and TRAMP,Fancy prompts,Remote Shells with TRAMP,Usage @anchor{usage directory-tracking-and-tramp}@anchor{1e}@anchor{usage dirtrack}@anchor{1f} @subsection Directory tracking and TRAMP diff --git a/test/mistty-project-test.el b/test/mistty-project-test.el index d1fa0ce..cd7ac65 100644 --- a/test/mistty-project-test.el +++ b/test/mistty-project-test.el @@ -34,7 +34,7 @@ ;; create new with a specific name (setq buf (mistty-in-project)) - (should (equal (project-prefixed-buffer-name "mistty") (buffer-name buf))) + (should (equal "*fake-mistty*" (buffer-name buf))) (setcdr (cdr fake-project) (list buf)) (should (equal (list buf) (project-buffers fake-project))) diff --git a/test/mistty-test.el b/test/mistty-test.el index e89b6ab..ac56482 100644 --- a/test/mistty-test.el +++ b/test/mistty-test.el @@ -4060,3 +4060,16 @@ (mistty-send-text "exit 10") (mistty-send-command) (mistty-wait-for-output :test (lambda () called)))))) + +(ert-deftest mistty-test-buffer-name () + (let ((mistty-buffer-name '("mistty"))) + (should (equal "*mistty*" (mistty-new-buffer-name))))) + +(ert-deftest mistty-test-buffer-name-shell () + (let ((mistty-buffer-name '(mistty-buffer-name-shell)) + (mistty-shell-command "/usr/local/bin/zsh")) + (should (equal "*zsh*" (mistty-new-buffer-name)))) + + (let ((mistty-buffer-name '(mistty-buffer-name-shell)) + (mistty-shell-command '("/usr/bin/fish" "-i"))) + (should (equal "*fish*" (mistty-new-buffer-name))))) diff --git a/test/mistty-tramp-test.el b/test/mistty-tramp-test.el index e818e86..a75b222 100644 --- a/test/mistty-tramp-test.el +++ b/test/mistty-tramp-test.el @@ -157,3 +157,12 @@ (setq cols-after (string-to-number (mistty-send-and-capture-command-output))) (should-not (equal cols-before cols-after)) (should (< cols-after cols-before)))))) + +(ert-deftest mistty-tramp-test-buffer-name () + (let ((mistty-buffer-name '("mistty" mistty-buffer-name-user mistty-buffer-name-host))) + (let ((default-directory "/ssh:someuser@test.example:/")) + (should (equal "*mistty-someuser@test.example*" (mistty-new-buffer-name)))) + (let ((default-directory "/ssh:test.example:/")) + (should (equal "*mistty@test.example*" (mistty-new-buffer-name)))) + (let ((default-directory "/sudo::/")) + (should (equal "*mistty-root*" (mistty-new-buffer-name))))))