From afc71f47f376d7f685b5994c58f81e4199a34f38 Mon Sep 17 00:00:00 2001 From: MitchMarq42 Date: Sat, 5 Nov 2022 12:52:11 -0800 Subject: [PATCH] Add functions from AwEshell --- lisp/mitch-packages.el | 53 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/lisp/mitch-packages.el b/lisp/mitch-packages.el index f44c90f..2cc0992 100644 --- a/lisp/mitch-packages.el +++ b/lisp/mitch-packages.el @@ -153,18 +153,67 @@ This is taken from a website that I can't remember at the moment." (if (null args) (bury-buffer) (mapc #'find-file-other-window (mapcar #'expand-file-name (flatten-tree (reverse args)))))) + (defun aweshell-sudo-toggle () + "Toggle sudo with current command." + (interactive) + (save-excursion + (let ((commands (buffer-substring-no-properties + (progn (eshell-bol) (point)) (point-max)))) + (if (string-match-p "^sudo " commands) + (progn + (eshell-bol) + (while (re-search-forward "sudo " nil t) + (replace-match "" t nil))) + (progn + (eshell-bol) + (insert "sudo ") + ))))) :general (general-define-key :states 'normal :keymaps 'eshell-mode-map - "I" 'eshell-evil-insert-line)) + "I" 'eshell-evil-insert-line) + (general-define-key + :states 'insert + :keymaps 'eshell-mode-map + "C-L" 'aweshell-sudo-toggle)) (use-package eshell-vterm :after eshell :custom (eshell-destroy-buffer-when-process-dies t) :hook (eshell-mode . eshell-vterm-mode)) (use-package eshell-syntax-highlighting :after eshell - :hook (eshell-mode . eshell-syntax-highlighting-mode)) + :hook (eshell-mode . eshell-syntax-highlighting-mode) + :config + ;; Make cat with syntax highlight. + ;; (defun aweshell-cat-with-syntax-highlight (filename) + (defun aweshell-cat-with-syntax-highlight (filename) + "Like cat(1) but with syntax highlighting. + +Taken from https://github.com/manateelazycat/aweshell/blob/d246df619573ca3f46070cc0ac82d024271ed243/aweshell.el#L775" + (let ((existing-buffer (get-file-buffer filename)) + (buffer (find-file-noselect filename))) + (eshell-print + (with-current-buffer buffer + (if (fboundp 'font-lock-ensure) + (font-lock-ensure) + (with-no-warnings + (font-lock-fontify-buffer))) + (let ((contents (buffer-string))) + (remove-text-properties 0 (length contents) '(read-only nil) contents) + contents))) + (unless existing-buffer + (kill-buffer buffer)) + nil)) + (defun eshell/cat (&rest args) + "Wrapper around `aweshell-cat-with-syntax-highlight' for multiple arguments. +" + (setq args (eshell-stringify-list (flatten-tree args))) + (dolist (file args) + (if (string= file "-") + (throw 'eshell-external + (eshell-external-command "cat" args)) + (aweshell-cat-with-syntax-highlight file))))) (use-package eshell-prompt-extras :after eshell :custom