From 584a0239d60e5319f70f0c280ed665f4af93ac2a Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Sun, 28 Jun 2020 19:51:45 -0400 Subject: Add configuration for `shell-mode'. --- init.el | 1 + lisp/shell.el | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ lisp/window-manager.el | 4 +- 3 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 lisp/shell.el diff --git a/init.el b/init.el index d25cb9d..63177ca 100644 --- a/init.el +++ b/init.el @@ -149,6 +149,7 @@ (load-module "appearance/theme") (load-module "appearance/modeline") (load-module "quality-of-life") +(load-module "shell") (load-module "window-manager") (load-module "system") (load-module "documents") diff --git a/lisp/shell.el b/lisp/shell.el new file mode 100644 index 0000000..7045880 --- /dev/null +++ b/lisp/shell.el @@ -0,0 +1,156 @@ +;;; shell.el -*- lexical-binding: t; -*- +;;; +;;; Author: Jakob L. Kreuze +;;; URL: https://git.sr.ht/~jakob/.emacs.d/ +;;; +;;; After reading Pierre Neidhardt's article on `shell-mode', I've decided to +;;; give it a try over Alacritty. This is a configuration to make it act a +;;; little bit more like `eshell' -- a notion of "visual" programs that need to +;;; be run in a proper terminal emulator, and popping the shell buffer to the +;;; currently-focused window. +;;; +;;; [1]: https://ambrevar.xyz/emacs-eshell-versus-shell/index.html +;;; + +;; I dislike the default behavior of creating a new window upon invoking +;; `shell', but there isn't a better way of dealing with that. +(defun shell-same-window (&optional buffer) + "Copy-pasted version of `shell' which does not use `pop-to-buffer'." + (interactive) + (let ((buffer-name "*shell*") + (n 1)) + (while (get-buffer buffer-name) + (setq buffer-name (format "*shell-%d*" n)) + (setq n (1+ n))) + (setq buffer (switch-to-buffer buffer-name))) + + (with-connection-local-variables + ;; On remote hosts, the local `shell-file-name' might be useless. + (when (file-remote-p default-directory) + (if (and (called-interactively-p 'any) + (null explicit-shell-file-name) + (null (getenv "ESHELL"))) + (set (make-local-variable 'explicit-shell-file-name) + (file-local-name + (expand-file-name + (read-file-name + "Remote shell path: " default-directory shell-file-name + t shell-file-name)))))) + + ;; Rain or shine, BUFFER must be current by now. + (unless (comint-check-proc buffer) + (let* ((prog (or explicit-shell-file-name + (getenv "ESHELL") shell-file-name)) + (name (file-name-nondirectory prog)) + (startfile (concat "~/.emacs_" name)) + (xargs-name (intern-soft (concat "explicit-" name "-args")))) + (unless (file-exists-p startfile) + (setq startfile (concat user-emacs-directory "init_" name ".sh"))) + (setq-local shell--start-prog (file-name-nondirectory prog)) + (apply #'make-comint-in-buffer "shell" buffer prog + (if (file-exists-p startfile) startfile) + (if (and xargs-name (boundp xargs-name)) + (symbol-value xargs-name) + '("-i"))) + (shell-mode)))) + buffer) + +;; Use ANSI colors in `shell' (and elsewhere!?). +(use-package xterm-color + ;; Don't play 'dumb'. + :init + (setq comint-terminfo-terminal "xterm-256color") + + ;; Use the same colors as Alacritty. + :init + (setq xterm-color-names + ["#333333" "#F2777A" "#99CC99" "#FFCC66" + "#6699CC" "#CC99CC" "#66CCCC" "#DDDDDD"]) + (setq xterm-color-names-bright + ["#5D5D5D" "#CF8BA2" "#90D6A6" "#E0BF80" + "#5F7AB1" "#807793" "#1FB6D3" "#F6F6F6"]) + + :init + (setq comint-output-filter-functions + (remove 'ansi-color-process-output + comint-output-filter-functions)) + + :hook + (shell-mode . + (lambda () + ;; Disable font-locking in this buffer to improve performance + (font-lock-mode -1) + ;; Prevent font-locking from being re-enabled in this buffer + (make-local-variable 'font-lock-function) + (setq font-lock-function (lambda (_) nil)) + (add-hook 'comint-preoutput-filter-functions 'xterm-color-filter nil t)))) + + + +(defvar visual-commands + '("r2" "radare2" "top") + "Shell commands to run in a proper terminal emulator.") + +(defun shell-fake-comint-send () + "Make it seem as though the current line was sent to the inferior." + ;; First, we make the "input" to comint empty. + (goto-char pmark) + (kill-line) + (comint-send-input) ; and send it. + (save-excursion + ;; Then, we copy the input to the previous line, + (previous-line) + (end-of-visual-line) + (yank) + ;; And finish by cleaning the kill ring. + (pop kill-ring) + (when kill-ring-yank-pointer + (setq kill-ring-yank-pointer kill-ring)))) + +(defun shell-screen-command () + "Handle any visual commands before deferring to the inferior." + (interactive) + (let ((proc (get-buffer-process (current-buffer)))) + (if (not proc) (user-error "Current buffer has no process") + (let* ((pmark (process-mark proc)) + (intxt (if (>= (point) (marker-position pmark)) + (progn (if comint-eol-on-send + (if comint-use-prompt-regexp + (end-of-line) + (goto-char (field-end)))) + (buffer-substring pmark (point))) + (let ((copy (funcall comint-get-old-input))) + (goto-char pmark) + (insert copy) + copy))) + (input (if (not (eq comint-input-autoexpand 'input)) + ;; Just whatever's already there. + intxt + ;; Expand and leave it visible in buffer. + (comint-replace-by-expanded-history t pmark) + (buffer-substring pmark (point)))) + (command (cl-first (split-string input " "))) + (args (cl-rest (split-string input " "))) + (visual (cl-member command visual-commands :test #'string=))) + (cond ((string= command "man") (man (string-join args " ")) + (shell-fake-comint-send)) + (visual (apply #'start-process "" nil "alacritty" + "--class" (format "*%s*,*%s*" command command) + "-t" input + "-e" command args) + (shell-fake-comint-send)) + (t (comint-send-input))))))) + +(bind-key "RET" 'shell-screen-command shell-mode-map) + +(defun shell-clear () + "Remove all preceding lines of shell interaction." + (interactive) + (let ((n (* 2 (1- (line-number-at-pos))))) + (goto-char (point-min)) + (while (plusp n) + (delete-visual-line) + (setq n (1- n))) + (end-of-line))) + +(bind-key "C-c C-l" 'shell-clear shell-mode-map) diff --git a/lisp/window-manager.el b/lisp/window-manager.el index fa332d4..b2f7421 100644 --- a/lisp/window-manager.el +++ b/lisp/window-manager.el @@ -66,9 +66,7 @@ ([?\s-f] . (lambda () (interactive) (start-process "" nil "firefox"))) - (,(kbd "s-") . (lambda () - (interactive) - (start-process "" nil "alacritty"))) + (,(kbd "s-") . shell-same-window) ;; Keys are offset such that corresponds to the 0th workspace. ,@(mapcar (lambda (i) -- cgit v1.3