Skip to content

Commit

Permalink
feat: Make buffer name configurable, include user and host.
Browse files Browse the repository at this point in the history
This change introduce the customization option mistty-buffer-name, which
allows configuring what a new MisTTY buffer is going to be called.

The default value of mistty-buffer-name includes function that'll
include the user and host of a remote shell started with TRAMP - or just
the user when using something like the sudo method. That is, with this
change, mistty buffers might have names like *[email protected]*
or *mistty-root*.
  • Loading branch information
szermatt committed Oct 10, 2024
1 parent 619a122 commit ad05710
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
8 changes: 8 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions mistty-project.el
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
94 changes: 84 additions & 10 deletions mistty.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
\"*[email protected]\". 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \"-<user>\".
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 \"@<host>\".
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
8 changes: 8 additions & 0 deletions mistty.texi
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/mistty-project-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
13 changes: 13 additions & 0 deletions test/mistty-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
9 changes: 9 additions & 0 deletions test/mistty-tramp-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:/"))
(should (equal "*[email protected]*" (mistty-new-buffer-name))))
(let ((default-directory "/ssh:test.example:/"))
(should (equal "*[email protected]*" (mistty-new-buffer-name))))
(let ((default-directory "/sudo::/"))
(should (equal "*mistty-root*" (mistty-new-buffer-name))))))

0 comments on commit ad05710

Please sign in to comment.