1 ;;; em-term.el --- running visual commands
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
27 ;; as "visual" programs. In order to run these programs under Emacs,
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.
36 (eval-when-compile (require 'eshell
))
41 (defgroup eshell-term nil
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"
48 :group
'eshell-module
))
52 (defcustom eshell-term-load-hook nil
53 "A list of functions to call when loading `eshell-term'."
54 :version
"24.1" ; removed eshell-term-initialize
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!!
64 "A list of commands that present their output in a visual fashion."
65 :type
'(repeat string
)
68 (defcustom eshell-visual-subcommands
72 ((COMMAND1 SUBCOMMAND1 SUBCOMMAND2...)
73 (COMMAND2 SUBCOMMAND1 ...))
75 of commands with subcommands that present their output in a
76 visual fashion. A likely entry is
78 (\"git\" \"log\" \"diff\" \"show\")
80 because git shows logs and diffs using a pager by default."
81 :type
'(repeat (cons (string :tag
"Command")
82 (repeat (string :tag
"Subcommand")))))
84 (defcustom eshell-visual-options
88 ((COMMAND1 OPTION1 OPTION2...)
89 (COMMAND2 OPTION1 ...))
91 of commands with options that present their output in a visual
92 fashion. For example, a sensible entry would be
96 because \"git <command> --help\" shows the command's
97 documentation with a pager."
98 :type
'(repeat (cons (string :tag
"Command")
99 (repeat (string :tag
"Option")))))
101 ;; If you change this from term-term-name, you need to ensure that the
102 ;; value you choose exists in the system's terminfo database. (Bug#12485)
103 (defcustom eshell-term-name term-term-name
104 "Name to use for the TERM variable when running visual commands.
105 See `term-term-name' in term.el for more information on how this is
107 :version
"24.3" ; eterm -> term-term-name = eterm-color
111 (defcustom eshell-escape-control-x t
112 "If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
113 See the variables `eshell-visual-commands',
114 `eshell-visual-subcommands', and `eshell-visual-options'. If
115 this variable is set to nil, <C-x> will send that control
116 character to the invoked process."
120 ;;; Internal Variables:
122 (defvar eshell-parent-buffer
)
126 (defun eshell-term-initialize ()
127 "Initialize the `term' interface code."
128 (make-local-variable 'eshell-interpreter-alist
)
129 (setq eshell-interpreter-alist
130 (cons (cons (function
131 (lambda (command args
)
132 (let ((command (file-name-nondirectory command
)))
133 (or (member command eshell-visual-commands
)
135 (cdr (assoc command eshell-visual-subcommands
)))
137 (cdr (assoc command eshell-visual-options
))
140 eshell-interpreter-alist
)))
142 (defun eshell-exec-visual (&rest args
)
143 "Run the specified PROGRAM in a terminal emulation buffer.
144 ARGS are passed to the program. At the moment, no piping of input is
146 (let* (eshell-interpreter-alist
147 (interp (eshell-find-interpreter (car args
) (cdr args
)))
148 (program (car interp
))
149 (args (eshell-flatten-list
150 (eshell-stringify-list (append (cdr interp
)
154 (concat "*" (file-name-nondirectory program
) "*")))
155 (eshell-buf (current-buffer)))
157 (switch-to-buffer term-buf
)
159 (set (make-local-variable 'term-term-name
) eshell-term-name
)
160 (make-local-variable 'eshell-parent-buffer
)
161 (setq eshell-parent-buffer eshell-buf
)
162 (term-exec term-buf program program nil args
)
163 (let ((proc (get-buffer-process term-buf
)))
164 (if (and proc
(eq 'run
(process-status proc
)))
165 (set-process-sentinel proc
'eshell-term-sentinel
)
166 (error "Failed to invoke visual command")))
168 (if eshell-escape-control-x
169 (term-set-escape-char ?\C-x
))))
172 (defun eshell-term-sentinel (proc string
)
173 "Destroy the buffer visiting PROC."
174 (let ((proc-buf (process-buffer proc
)))
175 (when (and proc-buf
(buffer-live-p proc-buf
)
176 (not (eq 'run
(process-status proc
)))
177 (= (process-exit-status proc
) 0))
178 (if (eq (current-buffer) proc-buf
)
179 (let ((buf (and (boundp 'eshell-parent-buffer
)
181 (buffer-live-p eshell-parent-buffer
)
182 eshell-parent-buffer
)))
184 (switch-to-buffer buf
))))
185 (kill-buffer proc-buf
))))
187 ;; jww (1999-09-17): The code below will allow Eshell to send input
188 ;; characters directly to the currently running interactive process.
189 ;; However, since this would introduce other problems that would need
190 ;; solutions, I'm going to let it wait until after 2.1.
192 ; (defvar eshell-term-raw-map nil
193 ; "Keyboard map for sending characters directly to the inferior process.")
194 ; (defvar eshell-term-escape-char nil
195 ; "Escape character for char-sub-mode of term mode.
196 ; Do not change it directly; use term-set-escape-char instead.")
197 ; (defvar eshell-term-raw-escape-map nil)
199 ; (defun eshell-term-send-raw-string (chars)
200 ; (goto-char eshell-last-output-end)
201 ; (process-send-string (eshell-interactive-process) chars))
203 ; (defun eshell-term-send-raw ()
204 ; "Send the last character typed through the terminal-emulator
205 ; without any interpretation."
207 ; ;; Convert `return' to C-m, etc.
208 ; (if (and (symbolp last-input-event)
209 ; (get last-input-event 'ascii-character))
210 ; (setq last-input-event (get last-input-event 'ascii-character)))
211 ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
213 ; (defun eshell-term-send-raw-meta ()
215 ; (if (symbolp last-input-event)
216 ; ;; Convert `return' to C-m, etc.
217 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
219 ; (setq last-input-event (car tmp)))
220 ; (if (symbolp last-input-event)
222 ; (setq tmp (get last-input-event 'ascii-character))
223 ; (if tmp (setq last-input-event tmp))))))
224 ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
225 ; (> last-input-event 127)
226 ; (< last-input-event 256))
227 ; (make-string 1 last-input-event)
228 ; (format "\e%c" last-input-event))))
230 ; (defun eshell-term-mouse-paste (click arg)
231 ; "Insert the last stretch of killed text at the position clicked on."
232 ; (interactive "e\nP")
233 ; (if (boundp 'xemacs-logo)
234 ; (eshell-term-send-raw-string
235 ; (or (condition-case () (x-get-selection) (error ()))
236 ; (error "No selection available")))
237 ; ;; Give temporary modes such as isearch a chance to turn off.
238 ; (run-hooks 'mouse-leave-buffer-hook)
239 ; (setq this-command 'yank)
240 ; (eshell-term-send-raw-string
241 ; (current-kill (cond ((listp arg) 0)
245 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
246 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
247 ; ;; For example: vi works with \eOA while elm wants \e[A ...
248 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
249 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
250 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
251 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
252 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
253 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
254 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
255 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
256 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
257 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
259 ; (defun eshell-term-set-escape-char (c)
260 ; "Change term-escape-char and keymaps that depend on it."
261 ; (if eshell-term-escape-char
262 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
263 ; (setq c (make-string 1 c))
264 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
265 ; ;; Define standard bindings in eshell-term-raw-escape-map
266 ; (define-key eshell-term-raw-escape-map "\C-x"
267 ; (lookup-key (current-global-map) "\C-x"))
268 ; (define-key eshell-term-raw-escape-map "\C-v"
269 ; (lookup-key (current-global-map) "\C-v"))
270 ; (define-key eshell-term-raw-escape-map "\C-u"
271 ; (lookup-key (current-global-map) "\C-u"))
272 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
274 ; (defun eshell-term-char-mode ()
275 ; "Switch to char (\"raw\") sub-mode of term mode.
276 ; Each character you type is sent directly to the inferior without
277 ; intervention from Emacs, except for the escape character (usually C-c)."
279 ; (if (not eshell-term-raw-map)
280 ; (let* ((map (make-keymap))
281 ; (esc-map (make-keymap))
284 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
285 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
287 ; (define-key map "\e" esc-map)
288 ; (setq eshell-term-raw-map map)
289 ; (setq eshell-term-raw-escape-map
290 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
291 ; (if (boundp 'xemacs-logo)
292 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
293 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
294 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
295 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
296 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
297 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
298 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
299 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
300 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
301 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
302 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
303 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
304 ; (eshell-term-set-escape-char ?\C-c))))
306 ; (defun eshell-term-line-mode ()
307 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
308 ; (use-local-map term-old-mode-map))
313 ;; generated-autoload-file: "esh-groups.el"
316 ;;; em-term.el ends here