From 930d35965741524ce9ae30668c5d89559d23ce1a Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 16 Oct 2024 16:31:49 +0100 Subject: Rework +eshell/open to work better with multiple instances. --- Emacs/.config/emacs/elisp/eshell-additions.el | 42 +++++++++++++++++---------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'Emacs/.config/emacs/elisp/eshell-additions.el') diff --git a/Emacs/.config/emacs/elisp/eshell-additions.el b/Emacs/.config/emacs/elisp/eshell-additions.el index f22f05e..0abaf6a 100644 --- a/Emacs/.config/emacs/elisp/eshell-additions.el +++ b/Emacs/.config/emacs/elisp/eshell-additions.el @@ -68,23 +68,33 @@ collect (cons (buffer-name buffer) buffer))) -(defun +eshell/open (&optional ARG) - "If no arg is given, run EShell as per usual. -If an arg is given, then interactively open a new Eshell instance -or a currently opened one, naming it in the process." +(defun +eshell/open (&optional arg) + "Open an instance of EShell, displaying it. + +If there exists only one instance of EShell, display it. Otherwise, prompt with +a list of open instances. If user selects an instance, display that instance. +Otherwise, create an instance with the name given. + +If `arg' is non nil, then always prompt user to select an instance." (interactive "P") - (if (null ARG) - (eshell) - (let* ((current-instances (+eshell/--current-instances)) - (answer (completing-read "Enter name: " (mapcar #'car current-instances))) - (result (assoc answer current-instances))) - (cond - (result (switch-to-buffer (cdr result))) - ((not (string= answer "")) - (let ((eshell-buffer-name (format "*%s-eshell*" answer))) - (eshell t))) - (t - (eshell)))))) + (let ((current-instances (+eshell/--current-instances))) + (cond + ((and (null current-instances) + (null arg)) + (eshell)) + ((and (= (length current-instances) 1) + (null arg)) + (switch-to-buffer (cdar current-instances))) + (t + (let* ((answer (completing-read "Enter name: " (mapcar #'car current-instances))) + (result (assoc answer current-instances))) + (cond + (result (switch-to-buffer (cdr result))) + ((not (string= answer "")) + (let ((eshell-buffer-name (format "*%s-eshell*" answer))) + (eshell nil))) + (t + (eshell)))))))) (provide 'eshell-additions) ;;; eshell-additions.el ends here -- cgit v1.2.3-13-gbd6f