(Emacs/config|elisp)~rework Eshell

Now I have separate modules for the additional new functions I
introduced for eshell and for the prompt function I made.  Cleans up
the configuration a bit and makes it easier to examine those files on
their own, which I expect to grow.
This commit is contained in:
2024-06-13 00:55:25 +01:00
parent 6fa811691e
commit f418d17001
4 changed files with 213 additions and 121 deletions

View File

@@ -5,4 +5,4 @@ alias gs magit-status
alias clear clear-scrollback alias clear clear-scrollback
alias d dired-other-window $1 alias d dired-other-window $1
alias gt goto alias gt goto
alias p~ project-root alias pr project-root

View File

@@ -1686,16 +1686,27 @@ expression it tries to evaluate it by testing against these conditions:
- it's an external command (bash evaluator) - it's an external command (bash evaluator)
Essentially, you get the best of both Emacs and external shell Essentially, you get the best of both Emacs and external shell
programs *ALL WITHIN* Emacs for free. programs *ALL WITHIN* Emacs for free.
*** Eshell functionality *** Eshell keymaps, display and variables
Bind some evil-like movements for easy shell usage, and a toggle Bind some evil-like movements for easy shell usage, a display record
function to pull up the eshell quickly. so when you call eshell it kinda looks like VSCode's terminal popup.
NOTE: This mode doesn't allow you to set maps the normal way; you need
to set keybindings on eshell-mode-hook, otherwise it'll just overwrite
them.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(use-package eshell (use-package eshell
:defer t :defer t
:general :general
(shell-leader (shell-leader
"t" #'eshell) "t" #'eshell)
:display
("\\*e?shell\\*"
(display-buffer-at-bottom)
(window-height . 0.33))
:init :init
(setq eshell-cmpl-ignore-case t
eshell-cd-on-directory t
eshell-highlight-prompt nil)
(add-hook (add-hook
'eshell-mode-hook 'eshell-mode-hook
(proc (proc
@@ -1703,6 +1714,7 @@ function to pull up the eshell quickly.
(general-def (general-def
:states '(normal insert) :states '(normal insert)
:keymaps 'eshell-mode-map :keymaps 'eshell-mode-map
"0" #'eshell-bol
"M-j" #'eshell-next-matching-input-from-input "M-j" #'eshell-next-matching-input-from-input
"M-k" #'eshell-previous-matching-input-from-input) "M-k" #'eshell-previous-matching-input-from-input)
(local-leader (local-leader
@@ -1711,134 +1723,45 @@ function to pull up the eshell quickly.
(recenter)) (recenter))
"k" #'eshell-kill-process)))) "k" #'eshell-kill-process))))
#+end_src #+end_src
*** Eshell pretty symbols and display *** Eshell prompt
Pretty symbols and a display record. Here I use my external library
#+begin_src emacs-lisp [[file:elisp/eshell-prompt.el][eshell-prompt]], which provides a more
(use-package eshell dynamic prompt for Eshell. Current features include:
:defer t + Git (with difference from remote and number of modified files)
:display + Current date and time
("\\*e?shell\\*" ; for general shells as well + A coloured prompt which changes colour based on the exit status of
(display-buffer-at-bottom) the previous command
(window-height . 0.33)))
#+end_src
*** Eshell variables and aliases
Set some sane defaults, a banner and a prompt. The prompt checks for
a git repo in the current directory and provides some extra
information in that case (in particular, branch name and if there any
changes that haven't been committed).
NOTE: I don't defer this package because it doesn't use any eshell
internals, just standard old Emacs packages.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(use-package eshell (use-package eshell-prompt
:defer t :load-path "elisp/"
:config :config
(defun +eshell/--git-get-remote-status ()
(let* ((branch-status (split-string
(shell-command-to-string "git status | grep 'Your branch is'")))
(status (nth 3 branch-status))
(diff (cl-position "by" branch-status :test #'string=)))
(if (null diff)
(propertize "=" 'font-lock-face '(:foreground "green"))
(let ((n (nth (+ 1 diff) branch-status)))
(concat
(cond
((string= status "ahead")
(propertize "" 'font-lock-face '(:foreground "dodger blue")))
((string= status "behind")
(propertize "" 'font-lock-face '(:foreground "orange red"))))
n)))))
(defun +eshell/--git-get-change-status ()
(let ((changed-files (- (length (split-string (shell-command-to-string "git status -s" ) "\n")) 1)))
(if (= changed-files 0)
(propertize "" 'font-lock-face '(:foreground "green"))
(propertize (number-to-string changed-files) 'font-lock-face '(:foreground "red")))))
(defun +eshell/get-git-properties ()
(let ((git-branch (shell-command-to-string "git branch")))
(if (or (string= git-branch "")
(not (string= "*" (substring git-branch 0 1))))
""
(format
"(%s<%s>[%s])"
(nth 2 (split-string git-branch "\n\\|\\*\\| "))
(+eshell/--git-get-change-status)
(+eshell/--git-get-remote-status)))))
(defun +eshell/prompt-function ()
(let ((git (+eshell/get-git-properties)))
(mapconcat
(lambda (item)
(if (listp item)
(propertize (car item)
'read-only t
'font-lock-face (cdr item)
'front-sticky '(font-lock-face read-only)
'rear-nonsticky '(font-lock-face read-only))
item))
(list
'("[")
`(,(abbreviate-file-name (eshell/pwd)) :foreground "LimeGreen")
'("]")
(if (string= git "")
""
(concat "-" git ""))
"\n"
`(,(format-time-string "[%H:%M:%S]") :foreground "purple")
"\n"
'("𝜆> " :foreground "DeepSkyBlue")))))
(defun +eshell/banner-message () (defun +eshell/banner-message ()
(concat (shell-command-to-string "~/.local/scripts/cowfortune") (concat (shell-command-to-string "~/.local/scripts/cowfortune")
"\n")) "\n"))
(setq eshell-prompt-regexp (format "^%s" +eshell-prompt/user-prompt)
(setq eshell-cmpl-ignore-case t eshell-prompt-function #'+eshell-prompt/make-prompt
eshell-cd-on-directory t eshell-banner-message '(+eshell/banner-message)))
eshell-banner-message '(+eshell/banner-message)
eshell-highlight-prompt nil
eshell-prompt-function #'+eshell/prompt-function
eshell-prompt-regexp "^𝜆> "))
#+end_src #+end_src
*** Eshell change directory quickly *** Eshell additions
Add ~eshell/goto~, which is actually a command accessible from within Using my external library
eshell (this is because ~eshell/*~ creates an accessible function [[file:elisp/eshell-additions.el][eshell-additions]], I get a few new
within eshell with name ~*~). ~eshell/goto~ makes it easier to change eshell internal commands and a surface command to open eshell at the
directories by using Emacs' find-file interface (which is much faster current working directory.
than ~cd ..; ls -l~).
~eshell/goto~ is a better ~cd~ for eshell. However it is really just NOTE: I don't defer this package because it autoloads any eshell
a plaster over a bigger issue for my workflow; many times I want internals that it uses so I'm only loading what I need to. Any
eshell to be present in the current directory of the buffer I am ~eshell/*~ functions need to be known by eshell before launching, so
using. So here's also a command for opening eshell with the current if I loaded this ~:after~ eshell then the first instance has no
directory. knowledge of the new additions.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(use-package eshell (use-package eshell-additions
:defer t :load-path "elisp/"
:general :general
(leader (leader
"T" #'+eshell/current-buffer) "T" #'+eshell/at-cwd))
:config
(defun eshell/goto (&rest args)
"Use `read-directory-name' to change directories."
(eshell/cd (list (read-directory-name "Directory?: "))))
(defun eshell/project-root (&rest args)
"Change to directory `project-root'"
(if (project-current)
(eshell/cd (list (project-root (project-current))))
(eshell/echo (format "[%s]: No project in current directory"
(propertize "Error" 'font-lock-face '(:foreground "red"))))))
(defun +eshell/current-buffer ()
(interactive)
(let ((dir (if buffer-file-name
(file-name-directory buffer-file-name)
default-directory))
(buf (eshell)))
(if dir
(with-current-buffer buf
(eshell/cd dir)
(eshell-send-input))
(message "Could not switch eshell: buffer is not real file")))))
#+end_src #+end_src
** WAIT Elfeed ** WAIT Elfeed
:PROPERTIES: :PROPERTIES:

View File

@@ -0,0 +1,57 @@
;;; eshell-additions.el --- Some aliases for Eshell -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Aryadev Chavali
;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation version 2 of the License
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(autoload #'eshell/cd "eshell")
(autoload #'eshell/echo "eshell")
(autoload #'eshell/send-input "eshell")
;; Aliases
(defun eshell/goto (&rest args)
"Use `read-directory-name' to change directories"
(eshell/cd (list (read-directory-name "Directory?: "))))
(defun eshell/project-root (&rest args)
"Change to directory `project-root'"
(if (project-current)
(eshell/cd (list (project-root (project-current))))
(let ((error-msg (propertize "Error" 'font-lock-face
'(:foreground "red"))))
(eshell/echo
(format "[%s]: No project in current directory" error-msg)))))
;; Additional functions
(defun +eshell/at-cwd ()
"Open an instance of eshell at the current working directory."
(interactive)
(let ((dir (if buffer-file-name
(file-name-directory buffer-file-name)
default-directory))
(buf (eshell)))
(with-current-buffer buf
(eshell/cd dir)
(eshell-send-input))))
(provide 'eshell-additions)
;;; eshell-additions.el ends here

View File

@@ -0,0 +1,112 @@
;;; eshell-prompt.el --- Generating a good prompt for Eshell -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Aryadev Chavali
;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation version 2 of the License.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; We provide a function +eshell-prompt which generates a prompt on
;; demand.
;;; Code:
(defvar +eshell-prompt/user-prompt "𝜆> "
"Prompt for user to input.")
(defun +eshell-prompt/--colour-on-last-command ()
"Returns an Emacs colour based on ESHELL-LAST-COMMAND-STATUS."
(if (zerop eshell-last-command-status)
"forestgreen"
"darkred"))
(defun +eshell-prompt/--git-remote-status ()
"Returns a propertized string for the status of a repository
in comparison to its remote. 3 differing strings are returned
dependent on:
- Is it equivalent to the remote?
- Is it ahead of the remote?
- Is it behind the remote?
The latter 2 also have a number for exactly how many commits
behind or ahead the local repository is."
(let* ((git-cmd "git status | grep 'Your branch is'")
(branch-status (split-string (shell-command-to-string git-cmd)))
(status (nth 3 branch-status))
(diff (cl-position "by" branch-status :test #'string=)))
(if (null diff)
(propertize "=" 'font-lock-face '(:foreground "green"))
(let ((n (nth (+ 1 diff) branch-status)))
(concat
(cond
((string= status "ahead")
(propertize "" 'font-lock-face '(:foreground "dodger blue")))
((string= status "behind")
(propertize "" 'font-lock-face '(:foreground "orange red"))))
n)))))
(defun +eshell-prompt/--git-change-status ()
"Returns a propertized string for the condition of the worktree in
a repository. If there are no changes i.e. the worktree is clean
then a green tick is returned, but if there are changes then the
number of files affected are returned in red."
(let* ((git-cmd "git status -s")
(command-output (split-string git-cmd))
(changed-files (- (length command-output) 1)))
(if (= changed-files 0)
(propertize "" 'font-lock-face '(:foreground "green"))
(propertize (number-to-string changed-files) 'font-lock-face '(:foreground "red")))))
(defun +eshell-prompt/--git-status ()
"Returns a completely formatted string of
form (BRANCH-NAME<CHANGES>[REMOTE-STATUS])."
(let ((git-branch (shell-command-to-string "git brnach")))
(if (or (string= git-branch "")
(not (string= "*" (substring git-branch 0 1))))
""
(format
"(%s<%s>[%s])"
(nth 2 (split-string git-branch "\n\\|\\*\\| "))
(+eshell-prompt/--git-change-status)
(+eshell-prompt/--git-remote-status)))))
(defun +eshell-prompt/make-prompt ()
(let ((git (+eshell-prompt/--git-status)))
(mapconcat
(lambda (item)
(if (listp item)
(propertize (car item)
'read-only t
'font-lock-face (cdr item)
'front-sticky '(font-lock-face read-only)
'rear-nonsticky '(font-lock-face read-only))
item))
(list
"["
`(,(abbreviate-file-name (eshell/pwd)) :foreground "LimeGreen")
"]"
(if (string= git "")
""
(concat "-" git ""))
"\n"
`(,(format-time-string "[%H:%M:%S]") :foreground "purple")
"\n"
(list "𝜆> " ':foreground (+eshell-prompt/--colour-on-last-command))))))
(provide 'eshell-prompt)
;;; eshell-prompt.el ends here