eshell: Setup some helper functions in eshell-additions, move bindings to there.

- A helper function to instantly perform commands
- eshell-goto-latest-prompt to remove any artifacts when performing
  these instant commands

Bindings needed to be moved over to that section cos they utilise
these helpers.
This commit is contained in:
2025-11-04 16:44:28 +00:00
parent 088ce4a865
commit 58cc74e096
2 changed files with 54 additions and 39 deletions

View File

@@ -1825,44 +1825,13 @@ interpreter.
(evil-set-initial-state 'eshell-mode 'normal)
(defun +eshell/banner-message ()
(concat (shell-command-to-string "fortune") "\n"))
(concat (shell-command-to-string "fortune | cowsay -r") "\n"))
(setq eshell-cmpl-ignore-case t
eshell-cd-on-directory t
eshell-cd-shows-directory nil
eshell-highlight-prompt nil
eshell-banner-message '(+eshell/banner-message))
(defun +eshell/good-clear ()
(interactive)
(eshell/clear-scrollback)
(eshell-send-input))
(add-hook
'eshell-mode-hook
(defun +eshell/--setup-keymap nil
(interactive)
(general-def
:states '(normal insert visual)
:keymaps 'eshell-mode-map
"M-j" #'eshell-next-prompt
"M-k" #'eshell-previous-prompt
"C-j" #'eshell-next-matching-input-from-input
"C-k" #'eshell-previous-matching-input-from-input)
(local-leader
:keymaps 'eshell-mode-map
"g" (proc-int
(let ((buffer (current-buffer)))
(eshell/goto)
(with-current-buffer buffer
(eshell-send-input))))
"l" (proc-int
(eshell-return-to-prompt)
(insert "ls")
(eshell-send-input))
"c" #'+eshell/good-clear
"k" #'eshell-kill-process))))
eshell-banner-message '(+eshell/banner-message)))
#+end_src
*** EShell prompt
Here I use my external library
@@ -1888,6 +1857,10 @@ Using my external library
internal EShell commands and a command to open EShell at the current
working directory.
I use these commands in my local bindings for EShell so I also setup
those here - they need to be hooked into the mode initialisation
because of how EShell works unfortunately.
NOTE: I don't defer this package because it must be loaded *before*
EShell is. This is because any ~eshell/*~ functions need to be loaded
before launching it.
@@ -1899,6 +1872,30 @@ before launching it.
;; FIXME: Why do I need to double load this? Otherwise +eshell/open doesn't
;; work as intended when using universal argument.
(load-file (concat user-emacs-directory "elisp/eshell-additions.el"))
(add-hook
'eshell-mode-hook
(defun +eshell/--setup-keymap nil
(interactive)
(general-def
:states '(normal insert visual)
:keymaps 'eshell-mode-map
"M-j" #'eshell-next-prompt
"M-k" #'eshell-previous-prompt
"C-j" #'eshell-next-matching-input-from-input
"C-k" #'eshell-previous-matching-input-from-input)
(local-leader
:keymaps 'eshell-mode-map
"g" (proc-int
(let ((buffer (current-buffer)))
(eshell/goto)
(with-current-buffer buffer
(eshell-send-input))))
"l" (proc-int (eshell-send-command "ls"))
"c" (proc-int (eshell-send-command "clear"))
"k" #'eshell-kill-process)))
:general
(shell-leader
"t" #'+eshell/open)

View File

@@ -28,23 +28,41 @@
(autoload #'eshell/echo "eshell")
(autoload #'eshell/send-input "eshell")
;; General helpers
(defun eshell-goto-latest-prompt ()
"Move point to the most recent eshell prompt and clear anything before it."
(interactive)
(goto-char (point-max))
(eshell-bol)
(delete-region (point) (point-max)))
(defun eshell-send-command (cmd)
(interactive "sCommand: ")
(eshell-goto-latest-prompt)
(insert cmd)
(eshell-send-input))
;; Aliases
(defun eshell/goto (&rest args)
"Use `read-directory-name' to change directories"
(let* ((name (read-file-name "Choose file: "))
"Use `read-file-name' to change directories"
(let* ((name (read-file-name "Choose file: " (thing-at-point 'filename t)))
(dir (file-name-directory name)))
(eshell-goto-latest-prompt)
(eshell/cd (list dir))
(if (not (file-directory-p name))
(find-file name))))
(defun eshell/project-root (&rest args)
"Change to directory `project-root'"
(if (project-current)
(eshell/cd (list (project-root (project-current))))
(cond
((project-current)
(eshell-goto-latest-prompt)
(eshell/cd (list (project-root (project-current)))))
(t
(setq eshell-last-command-status 1)
(eshell/echo
(format "[%s]: No project in current directory"
(propertize "Error" 'font-lock-face '(:foreground "red"))))))
(propertize "Error" 'font-lock-face '(:foreground "red")))))))
(defun eshell/sudo-switch (&rest args)
"Switch to and from administrative (sudo) mode in Eshell.
@@ -57,7 +75,7 @@ Uses tramp to figure out if we're in sudo mode or not. "
((string= user "root")
(eshell/cd (file-remote-p default-directory 'localname))))))
;; Additional functions
;; +eshell/open and +eshell/at-cwd
(defun +eshell/--current-instances ()
(cl-loop for buffer being the buffers
if (with-current-buffer buffer