From bd44bea8ead7c9c5d071d48a57073498b9e9b332 Mon Sep 17 00:00:00 2001 From: Jessie Hildebrandt Date: Thu, 16 Mar 2023 11:37:31 -0400 Subject: [PATCH] Adjust eshell configuration --- init.el | 89 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/init.el b/init.el index d427c1c..0b1d65c 100644 --- a/init.el +++ b/init.el @@ -440,8 +440,41 @@ DOCSTRING is an optional form that is discarded upon expansion." ;; eshell ;; ---------------------------------- ;; -(defun user/abbrev-path (path) - "Return a PATH with all but the last directory name abbreviated." +(defun user/open-eshell (&optional force-new-session) + "Focus the last active `eshell' session, or start a new one if none available. + +When FORCE-NEW-SESSION is non-nil, a new session will be started even if there +is already an active session to bring into focus." + (interactive) + (let ((eshell-buffer (cl-find-if (lambda (buffer) + (eq (buffer-local-value 'major-mode buffer) + 'eshell-mode)) + (buffer-list)))) + (if eshell-buffer + (if force-new-session + (let ((current-buffer-directory default-directory)) + (pop-to-buffer eshell-buffer) + (let ((default-directory current-buffer-directory)) + (eshell :new-session))) + (pop-to-buffer eshell-buffer)) + (eshell)))) + +(defun user/auto-toggle-eshell-buffer-tabs () + "Enable `tab-line-mode' in all `eshell-mode' buffers if multiple are open. + +Also automatically disables `tab-line-mode' if all but one `eshell-mode' buffer +have been closed." + (let ((eshell-buffers (cl-remove-if-not (lambda (buffer) + (eq (buffer-local-value 'major-mode buffer) + 'eshell-mode)) + (buffer-list)))) + (mapc (lambda (buffer) + (with-current-buffer buffer + (tab-line-mode (and (length< eshell-buffers 2) -1)))) + eshell-buffers))) + +(defun user/prettify-eshell-pwd (path) + "Return PATH formatted and with all but the last directory name abbreviated." (let* ((components (split-string (abbreviate-file-name path) "/")) (str "")) (while (cdr components) @@ -456,38 +489,13 @@ DOCSTRING is an optional form that is discarded upon expansion." (string (elt (car components) 0) ?/))))) components (cdr components))) (concat (propertize str 'face 'font-lock-comment-face) - (propertize (cl-reduce (lambda (a b) - (concat a "/" b)) + (propertize (cl-reduce (lambda (a b) (concat a "/" b)) components) 'face 'font-lock-doc-face)))) -(defun user/open-eshell (&optional force-new-session) - "Focus the last active `eshell' session, or start a new one if none unavaiable. - -When FORCE-NEW-SESSION is non-nil, a new session will be started even if there -is already an active session to switch to or focus." - (interactive) - (let ((eshell-buffer (cl-find-if (lambda (buffer) - (eq (buffer-local-value 'major-mode buffer) - 'eshell-mode)) - (buffer-list)))) - (if eshell-buffer - (if force-new-session - (let ((current-buffer-directory default-directory)) - (pop-to-buffer eshell-buffer '((display-buffer-reuse-window display-buffer-same-window))) - (let ((default-directory current-buffer-directory)) - (eshell :new-session))) - (pop-to-buffer eshell-buffer '((display-buffer-reuse-window display-buffer-same-window)))) - (eshell)))) - (editor-feature eshell "Provides a shell-like interpreter that can process shell or Lisp commands." :custom - (eshell-banner-message '(concat "\n" (if (executable-find "fortune") - (shell-command-to-string "fortune -s computers") - "Hello, commander."))) - (eshell-prompt-function (lambda () - (concat "\n" (user/abbrev-path (eshell/pwd)) (if (= (user-uid) 0) " # " " ❱ ")))) (eshell-prompt-regexp "^[^#❱\n]* [#❱] ") (eshell-visual-commands '("fish" "bash" "ssh" "mosh" @@ -495,22 +503,29 @@ is already an active session to switch to or focus." (eshell-destroy-buffer-when-process-dies t "Destroys child buffers after their process returns") (eshell-error-if-no-glob t "Produces an error if a glob pattern fails to match, like zsh") (eshell-hist-ignoredups t "Treat multiple repeating history entries as a single entry") + (eshell-banner-message '(if (executable-find "fortune") + (shell-command-to-string "fortune -s computers") + "Hello, commander.")) + (eshell-prompt-function (lambda () + (concat "\n" + (user/prettify-eshell-pwd (eshell/pwd)) + (if (= (user-uid) 0) + " # " + " ❱ ")))) :hook (eshell-post-command-hook . (lambda () (rename-buffer (concat eshell-buffer-name " " - (user/abbrev-path (eshell/pwd))) + (user/prettify-eshell-pwd (eshell/pwd))) :unique))) (eshell-mode-hook . (lambda () (setq-local line-spacing 0.15 global-hl-line-mode nil - tab-line-tabs-function #'tab-line-tabs-mode-buffers) - (tab-line-mode))) - :bind + tab-line-tabs-function #'tab-line-tabs-mode-buffers))) + (buffer-list-update-hook . user/auto-toggle-eshell-buffer-tabs) + :bind* ("C-c RET" . user/open-eshell) - ("C-c C-" . (lambda () - (interactive) - (user/open-eshell :force-new-session)))) + ("C-c C-" . (lambda () (interactive) (user/open-eshell :force-new-session)))) ;; `eshell-mode-map' is not available until the `esh-mode' module is loaded, so ;; the local keymap bindings are set up here @@ -519,7 +534,6 @@ is already an active session to switch to or focus." "Handles input from the user and provides `eshell-mode' for eshell." :bind (:map eshell-mode-map - ("C-c RET" . nil) ("C-" . eshell-copy-old-input) ("C-c j" . tab-line-switch-to-prev-tab) ("C-c l" . tab-line-switch-to-next-tab))) @@ -530,8 +544,7 @@ is already an active session to switch to or focus." (editor-feature em-alias "Handles creation and management of command aliases for eshell." :config - (eval-after-load 'em-alias (lambda () - (add-to-list 'eshell-command-aliases-list '("lsl" "ls -lh $1"))))) + (eval-after-load 'em-alias (lambda () (add-to-list 'eshell-command-aliases-list '("lsl" "ls -lh $1"))))) ;; ---------------------------------- ;; ;; files