[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Hack to get nicer buffer names for shell buffers that use tramp.
From: |
Michael Albinus |
Subject: |
Re: Hack to get nicer buffer names for shell buffers that use tramp. |
Date: |
Sat, 20 Apr 2019 21:29:02 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
"Ben Hyde" <address@hidden> writes:
Hi Ben,
> The code below will bless this new shell buffer with a nicer name,
> e.g. shell:example.com instead of say shell.
>
> (defun auto-name-shell-buffers (original-shell-fn &optional buffer)
> "Wrap ORIGINAL-SHELL-FN to avoid tramp shell buffers name *shell*, do
> nothing if BUFFER."
> (cond
> ((or buffer
> (not (tramp-tramp-file-p default-directory)))
> (funcall original-shell-fn buffer))
> ((not (zerop (call-process "/usr/bin/ssh-add" nil nil nil "-l")))
> (message "ssh auth socket is bork'd"))
> (t
> (with-parsed-tramp-file-name default-directory tp
> (let* ( ; (tramp-path (tramp-dissect-file-name default-directory))
> (host (tramp-file-name-host tp))
> (user (if (tramp-file-name-user tp)
> (format "%s@" (tramp-file-name-user tp)) ""))
> (new-buffer-name (format "*shell:%s%s*" user host)))
> (funcall original-shell-fn new-buffer-name))))))
>
> (advice-add 'shell :around #'auto-name-shell-buffers)
>
> Feel free to provide feedback.
Thanks for the code snippet.
I always recommend not to use Tramp ionternal functions. They could
change w/o any notice. Use the public API, file-remote-p here. The
function would look then (untested):
--8<---------------cut here---------------start------------->8---
(defun auto-name-shell-buffers (original-shell-fn &optional buffer)
"Wrap ORIGINAL-SHELL-FN to avoid tramp shell buffers name *shell*, do nothing
if BUFFER."
(cond
((or buffer (not (file-remote-p default-directory)))
(funcall original-shell-fn buffer))
((not (zerop (call-process "/usr/bin/ssh-add" nil nil nil "-l")))
(message "ssh auth socket is bork'd"))
(t
(let* ((host (file-remote-p default-directory 'host))
(user (if (file-remote-p default-directory)
(format "%s@" (file-remote-p default-directory 'user)) ""))
(new-buffer-name (format "*shell:%s%s*" user host)))
(funcall original-shell-fn new-buffer-name)))))
--8<---------------cut here---------------end--------------->8---
> * ben
Best regards, Michael.