Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; em-term.el --- running visual commands |
affbf647 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1999-2013 Free Software Foundation, Inc. |
affbf647 | 4 | |
7de5b421 GM |
5 | ;; Author: John Wiegley <johnw@gnu.org> |
6 | ||
affbf647 GM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
4ee57b2a | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
affbf647 | 10 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
affbf647 GM |
13 | |
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
4ee57b2a | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
affbf647 | 21 | |
affbf647 GM |
22 | ;;; Commentary: |
23 | ||
24 | ;; At the moment, eshell is stream-based in its interactive input and | |
25 | ;; output. This means that full-screen commands, such as "vi" or | |
26 | ;; "lynx", will not display correctly. These are therefore thought of | |
e1dbe924 | 27 | ;; as "visual" programs. In order to run these programs under Emacs, |
affbf647 GM |
28 | ;; Eshell uses the term.el package, and invokes them in a separate |
29 | ;; buffer, giving the illusion that Eshell itself is allowing these | |
30 | ;; visual processes to execute. | |
31 | ||
dbba8a04 GM |
32 | ;;; Code: |
33 | ||
f87b1284 GM |
34 | (require 'esh-util) |
35 | (require 'esh-ext) | |
dbba8a04 | 36 | (eval-when-compile (require 'eshell)) |
affbf647 GM |
37 | (require 'term) |
38 | ||
3146b070 | 39 | ;;;###autoload |
35ff222c GM |
40 | (progn |
41 | (defgroup eshell-term nil | |
dbba8a04 GM |
42 | "This module causes visual commands (e.g., 'vi') to be executed by |
43 | the `term' package, which comes with Emacs. This package handles most | |
44 | of the ANSI control codes, allowing curses-based applications to run | |
45 | within an Emacs window. The variable `eshell-visual-commands' defines | |
46 | which commands are considered visual in nature." | |
47 | :tag "Running visual commands" | |
35ff222c | 48 | :group 'eshell-module)) |
dbba8a04 | 49 | |
affbf647 GM |
50 | ;;; User Variables: |
51 | ||
d783d303 | 52 | (defcustom eshell-term-load-hook nil |
ec60da52 | 53 | "A list of functions to call when loading `eshell-term'." |
d783d303 | 54 | :version "24.1" ; removed eshell-term-initialize |
affbf647 GM |
55 | :type 'hook |
56 | :group 'eshell-term) | |
57 | ||
58 | (defcustom eshell-visual-commands | |
59 | '("vi" ; what is going on?? | |
60 | "screen" "top" ; ok, a valid program... | |
61 | "less" "more" ; M-x view-file | |
62 | "lynx" "ncftp" ; w3.el, ange-ftp | |
63 | "pine" "tin" "trn" "elm") ; GNUS!! | |
ec60da52 | 64 | "A list of commands that present their output in a visual fashion." |
affbf647 GM |
65 | :type '(repeat string) |
66 | :group 'eshell-term) | |
67 | ||
d105b0e2 TH |
68 | (defcustom eshell-visual-subcommands |
69 | nil | |
70 | "An alist of the form | |
71 | ||
72 | ((COMMAND1 SUBCOMMAND1 SUBCOMMAND2...) | |
73 | (COMMAND2 SUBCOMMAND1 ...)) | |
74 | ||
75 | of commands with subcommands that present their output in a | |
76 | visual fashion. A likely entry is | |
77 | ||
78 | (\"git\" \"log\" \"diff\" \"show\") | |
79 | ||
80 | because git shows logs and diffs using a pager by default." | |
81 | :type '(repeat (cons (string :tag "Command") | |
a2d98946 GM |
82 | (repeat (string :tag "Subcommand")))) |
83 | :version "24.4" | |
84 | :group 'eshell-term) | |
d105b0e2 TH |
85 | |
86 | (defcustom eshell-visual-options | |
87 | nil | |
88 | "An alist of the form | |
89 | ||
90 | ((COMMAND1 OPTION1 OPTION2...) | |
91 | (COMMAND2 OPTION1 ...)) | |
92 | ||
93 | of commands with options that present their output in a visual | |
94 | fashion. For example, a sensible entry would be | |
95 | ||
96 | (\"git\" \"--help\") | |
97 | ||
98 | because \"git <command> --help\" shows the command's | |
99 | documentation with a pager." | |
100 | :type '(repeat (cons (string :tag "Command") | |
a2d98946 GM |
101 | (repeat (string :tag "Option")))) |
102 | :version "24.4" | |
103 | :group 'eshell-term) | |
d105b0e2 | 104 | |
cca96c97 GM |
105 | ;; If you change this from term-term-name, you need to ensure that the |
106 | ;; value you choose exists in the system's terminfo database. (Bug#12485) | |
107 | (defcustom eshell-term-name term-term-name | |
ec60da52 | 108 | "Name to use for the TERM variable when running visual commands. |
affbf647 GM |
109 | See `term-term-name' in term.el for more information on how this is |
110 | used." | |
cca96c97 | 111 | :version "24.3" ; eterm -> term-term-name = eterm-color |
affbf647 GM |
112 | :type 'string |
113 | :group 'eshell-term) | |
114 | ||
115 | (defcustom eshell-escape-control-x t | |
ec60da52 | 116 | "If non-nil, allow <C-x> to be handled by Emacs key in visual buffers. |
d105b0e2 TH |
117 | See the variables `eshell-visual-commands', |
118 | `eshell-visual-subcommands', and `eshell-visual-options'. If | |
119 | this variable is set to nil, <C-x> will send that control | |
120 | character to the invoked process." | |
affbf647 GM |
121 | :type 'boolean |
122 | :group 'eshell-term) | |
123 | ||
124 | ;;; Internal Variables: | |
125 | ||
126 | (defvar eshell-parent-buffer) | |
127 | ||
128 | ;;; Functions: | |
129 | ||
130 | (defun eshell-term-initialize () | |
131 | "Initialize the `term' interface code." | |
132 | (make-local-variable 'eshell-interpreter-alist) | |
133 | (setq eshell-interpreter-alist | |
134 | (cons (cons (function | |
d105b0e2 TH |
135 | (lambda (command args) |
136 | (let ((command (file-name-nondirectory command))) | |
137 | (or (member command eshell-visual-commands) | |
138 | (member (car args) | |
139 | (cdr (assoc command eshell-visual-subcommands))) | |
140 | (intersection args | |
141 | (cdr (assoc command eshell-visual-options)) | |
142 | :test 'string=))))) | |
affbf647 GM |
143 | 'eshell-exec-visual) |
144 | eshell-interpreter-alist))) | |
145 | ||
146 | (defun eshell-exec-visual (&rest args) | |
147 | "Run the specified PROGRAM in a terminal emulation buffer. | |
148 | ARGS are passed to the program. At the moment, no piping of input is | |
149 | allowed." | |
150 | (let* (eshell-interpreter-alist | |
d105b0e2 | 151 | (interp (eshell-find-interpreter (car args) (cdr args))) |
affbf647 GM |
152 | (program (car interp)) |
153 | (args (eshell-flatten-list | |
154 | (eshell-stringify-list (append (cdr interp) | |
155 | (cdr args))))) | |
156 | (term-buf | |
157 | (generate-new-buffer | |
158 | (concat "*" (file-name-nondirectory program) "*"))) | |
159 | (eshell-buf (current-buffer))) | |
160 | (save-current-buffer | |
161 | (switch-to-buffer term-buf) | |
162 | (term-mode) | |
163 | (set (make-local-variable 'term-term-name) eshell-term-name) | |
164 | (make-local-variable 'eshell-parent-buffer) | |
165 | (setq eshell-parent-buffer eshell-buf) | |
166 | (term-exec term-buf program program nil args) | |
167 | (let ((proc (get-buffer-process term-buf))) | |
168 | (if (and proc (eq 'run (process-status proc))) | |
169 | (set-process-sentinel proc 'eshell-term-sentinel) | |
170 | (error "Failed to invoke visual command"))) | |
171 | (term-char-mode) | |
172 | (if eshell-escape-control-x | |
173 | (term-set-escape-char ?\C-x)))) | |
174 | nil) | |
175 | ||
176 | (defun eshell-term-sentinel (proc string) | |
177 | "Destroy the buffer visiting PROC." | |
178 | (let ((proc-buf (process-buffer proc))) | |
179 | (when (and proc-buf (buffer-live-p proc-buf) | |
180 | (not (eq 'run (process-status proc))) | |
181 | (= (process-exit-status proc) 0)) | |
182 | (if (eq (current-buffer) proc-buf) | |
183 | (let ((buf (and (boundp 'eshell-parent-buffer) | |
184 | eshell-parent-buffer | |
185 | (buffer-live-p eshell-parent-buffer) | |
186 | eshell-parent-buffer))) | |
187 | (if buf | |
188 | (switch-to-buffer buf)))) | |
189 | (kill-buffer proc-buf)))) | |
190 | ||
191 | ;; jww (1999-09-17): The code below will allow Eshell to send input | |
192 | ;; characters directly to the currently running interactive process. | |
193 | ;; However, since this would introduce other problems that would need | |
194 | ;; solutions, I'm going to let it wait until after 2.1. | |
195 | ||
196 | ; (defvar eshell-term-raw-map nil | |
197 | ; "Keyboard map for sending characters directly to the inferior process.") | |
198 | ; (defvar eshell-term-escape-char nil | |
199 | ; "Escape character for char-sub-mode of term mode. | |
200 | ; Do not change it directly; use term-set-escape-char instead.") | |
201 | ; (defvar eshell-term-raw-escape-map nil) | |
202 | ||
203 | ; (defun eshell-term-send-raw-string (chars) | |
204 | ; (goto-char eshell-last-output-end) | |
205 | ; (process-send-string (eshell-interactive-process) chars)) | |
206 | ||
207 | ; (defun eshell-term-send-raw () | |
208 | ; "Send the last character typed through the terminal-emulator | |
209 | ; without any interpretation." | |
210 | ; (interactive) | |
211 | ; ;; Convert `return' to C-m, etc. | |
cfa29281 GM |
212 | ; (if (and (symbolp last-input-event) |
213 | ; (get last-input-event 'ascii-character)) | |
214 | ; (setq last-input-event (get last-input-event 'ascii-character))) | |
215 | ; (eshell-term-send-raw-string (make-string 1 last-input-event))) | |
affbf647 GM |
216 | |
217 | ; (defun eshell-term-send-raw-meta () | |
218 | ; (interactive) | |
cfa29281 | 219 | ; (if (symbolp last-input-event) |
affbf647 | 220 | ; ;; Convert `return' to C-m, etc. |
cfa29281 | 221 | ; (let ((tmp (get last-input-event 'event-symbol-elements))) |
affbf647 | 222 | ; (if tmp |
cfa29281 GM |
223 | ; (setq last-input-event (car tmp))) |
224 | ; (if (symbolp last-input-event) | |
affbf647 | 225 | ; (progn |
cfa29281 GM |
226 | ; (setq tmp (get last-input-event 'ascii-character)) |
227 | ; (if tmp (setq last-input-event tmp)))))) | |
228 | ; (eshell-term-send-raw-string (if (and (numberp last-input-event) | |
229 | ; (> last-input-event 127) | |
230 | ; (< last-input-event 256)) | |
231 | ; (make-string 1 last-input-event) | |
232 | ; (format "\e%c" last-input-event)))) | |
affbf647 GM |
233 | |
234 | ; (defun eshell-term-mouse-paste (click arg) | |
235 | ; "Insert the last stretch of killed text at the position clicked on." | |
236 | ; (interactive "e\nP") | |
237 | ; (if (boundp 'xemacs-logo) | |
238 | ; (eshell-term-send-raw-string | |
239 | ; (or (condition-case () (x-get-selection) (error ())) | |
45240125 | 240 | ; (error "No selection available"))) |
affbf647 GM |
241 | ; ;; Give temporary modes such as isearch a chance to turn off. |
242 | ; (run-hooks 'mouse-leave-buffer-hook) | |
243 | ; (setq this-command 'yank) | |
244 | ; (eshell-term-send-raw-string | |
245 | ; (current-kill (cond ((listp arg) 0) | |
246 | ; ((eq arg '-) -1) | |
247 | ; (t (1- arg))))))) | |
248 | ||
249 | ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either. | |
250 | ; ;; For my configuration it's definitely better \eOA but YMMV. -mm | |
251 | ; ;; For example: vi works with \eOA while elm wants \e[A ... | |
252 | ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA")) | |
253 | ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB")) | |
254 | ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC")) | |
255 | ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD")) | |
256 | ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~")) | |
257 | ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~")) | |
258 | ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~")) | |
259 | ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~")) | |
260 | ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?")) | |
261 | ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H")) | |
262 | ||
263 | ; (defun eshell-term-set-escape-char (c) | |
264 | ; "Change term-escape-char and keymaps that depend on it." | |
265 | ; (if eshell-term-escape-char | |
266 | ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw)) | |
267 | ; (setq c (make-string 1 c)) | |
268 | ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map) | |
269 | ; ;; Define standard bindings in eshell-term-raw-escape-map | |
270 | ; (define-key eshell-term-raw-escape-map "\C-x" | |
271 | ; (lookup-key (current-global-map) "\C-x")) | |
272 | ; (define-key eshell-term-raw-escape-map "\C-v" | |
273 | ; (lookup-key (current-global-map) "\C-v")) | |
274 | ; (define-key eshell-term-raw-escape-map "\C-u" | |
275 | ; (lookup-key (current-global-map) "\C-u")) | |
276 | ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw)) | |
277 | ||
278 | ; (defun eshell-term-char-mode () | |
279 | ; "Switch to char (\"raw\") sub-mode of term mode. | |
280 | ; Each character you type is sent directly to the inferior without | |
281 | ; intervention from Emacs, except for the escape character (usually C-c)." | |
282 | ; (interactive) | |
283 | ; (if (not eshell-term-raw-map) | |
284 | ; (let* ((map (make-keymap)) | |
285 | ; (esc-map (make-keymap)) | |
286 | ; (i 0)) | |
287 | ; (while (< i 128) | |
288 | ; (define-key map (make-string 1 i) 'eshell-term-send-raw) | |
289 | ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta) | |
290 | ; (setq i (1+ i))) | |
291 | ; (define-key map "\e" esc-map) | |
292 | ; (setq eshell-term-raw-map map) | |
293 | ; (setq eshell-term-raw-escape-map | |
294 | ; (copy-keymap (lookup-key (current-global-map) "\C-x"))) | |
295 | ; (if (boundp 'xemacs-logo) | |
296 | ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste) | |
297 | ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste)) | |
298 | ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up) | |
299 | ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down) | |
300 | ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right) | |
301 | ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left) | |
302 | ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del) | |
303 | ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace) | |
304 | ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home) | |
305 | ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end) | |
306 | ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior) | |
307 | ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next) | |
308 | ; (eshell-term-set-escape-char ?\C-c)))) | |
309 | ||
310 | ; (defun eshell-term-line-mode () | |
311 | ; "Switch to line (\"cooked\") sub-mode of eshell-term mode." | |
312 | ; (use-local-map term-old-mode-map)) | |
313 | ||
dbba8a04 | 314 | (provide 'em-term) |
affbf647 | 315 | |
3146b070 GM |
316 | ;; Local Variables: |
317 | ;; generated-autoload-file: "esh-groups.el" | |
318 | ;; End: | |
319 | ||
affbf647 | 320 | ;;; em-term.el ends here |