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") (interactive "p")
(eshell-bol) (eshell-bol)
(evil-insert count vcount)) (evil-insert count vcount))
(defun eshell/emacs (&rest args) (defun mitch/edit-file-or-lib (name)
"Basically you can edit ARGS and it will open in a new buffer. "Edit library called NAME if it exists, otherwise edit FILE.
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." If the current window occupies the whole frame, split it."
(let* ((exe (if (member "--client" args) (let* ((old-default-dir default-directory) ; needed to preserve environment
(executable-find "emacsclient") find-fun lib-fun ; scope these so we can bind them later
(executable-find "emacs")))
(args (remove "--client" args))
(openfun (lambda (arg) (openfun (lambda (arg)
"Implicit function for opening a file or library."
(let ((arg-file-name (expand-file-name arg)) (let ((arg-file-name (expand-file-name arg))
(arg-lib-name (locate-library arg))) (arg-lib-name (locate-library arg)))
(if (file-exists-p arg-file-name) (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 (if arg-lib-name
(funcall lib-fun arg) (funcall lib-fun arg)
(funcall find-fun arg-file-name))))))) (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 (setq find-fun #'find-file-other-window
lib-fun #'find-library-other-window) lib-fun #'find-library-other-window)
(setq find-fun #'find-file (setq find-fun #'find-file
lib-fun #'find-library)) lib-fun #'find-library))
(setq args (remove "-nw" args)) (let ((return (funcall openfun name))) ; default-directory needs to reset
(if (null args) (setq default-directory old-default-dir) return))) ; but we return bufs
(async-shell-command exe) (defun eshell/emacs (&rest args)
(mapcar openfun "run external Emacs."
(flatten-tree (reverse args)))))) (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 () (defun mitch/eshell-setup-keys ()
(evil-collection-define-key (evil-collection-define-key
'normal 'normal