Revamp eshell/emacs, add helper function

This commit is contained in:
MitchMarq42 2023-01-03 16:25:18 -09:00
parent 5334d489ac
commit fb524eea38

View File

@ -154,15 +154,14 @@
(interactive "p")
(eshell-bol)
(evil-insert count vcount))
(defun eshell/emacs (&rest args)
"Basically you can edit ARGS and it will open in a new buffer.
When your shell is Emacs, your Emacs is but an oyster...
This is taken from a website that I can't remember at the moment."
(let* ((exe (if (member "--client" args)
(executable-find "emacsclient")
(executable-find "emacs")))
(args (remove "--client" args))
(defun mitch/edit-file-or-lib (name)
"Edit library called NAME if it exists, otherwise edit FILE.
If the current window occupies the whole frame, split it."
(let* ((old-default-dir default-directory) ; needed to preserve environment
find-fun lib-fun ; scope these so we can bind them later
(openfun (lambda (arg)
"Implicit function for opening a file or library."
(let ((arg-file-name (expand-file-name arg))
(arg-lib-name (locate-library arg)))
(if (file-exists-p arg-file-name)
@ -170,16 +169,26 @@ This is taken from a website that I can't remember at the moment."
(if arg-lib-name
(funcall lib-fun arg)
(funcall find-fun arg-file-name)))))))
(if (and (one-window-p) (not (member "-nw" args)))
(if (one-window-p)
(setq find-fun #'find-file-other-window
lib-fun #'find-library-other-window)
(setq find-fun #'find-file
lib-fun #'find-library))
(setq args (remove "-nw" args))
(if (null args)
(async-shell-command exe)
(mapcar openfun
(flatten-tree (reverse args))))))
(let ((return (funcall openfun name))) ; default-directory needs to reset
(setq default-directory old-default-dir) return))) ; but we return bufs
(defun eshell/emacs (&rest args)
"run external Emacs."
(eshell-eval-using-options ; this is somewhat broken but works okay
"emacs" args
'(
(nil "batch" nil batch "Non-interactive")
(nil "chdir" t change-dir "change to directory DIR")
("nw" nil nil no-window "open in terminal mode, no window")
("fs" "fullscreen" nil full-screen "Open in full screen")
:external "emacs"
:usage "[FILE] etc placeholder text...")
(mapcar #'mitch/edit-file-or-lib
(eshell-stringify-list (flatten-tree (reverse args))))))
(defun mitch/eshell-setup-keys ()
(evil-collection-define-key
'normal