blob: 70458807f1522fc36affc540a37c41d0a8e67e56 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
;;; shell.el -*- lexical-binding: t; -*-
;;;
;;; Author: Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;; 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)
|
© 2015 - 2026 Jakob L. Kreuze