1 ;;; em-term.el --- running visual commands
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; At the moment, eshell is stream-based in its interactive input and
28 ;; output. This means that full-screen commands, such as "vi" or
29 ;; "lynx", will not display correctly. These are therefore thought of
30 ;; as "visual" programs. In order to run these progrem under Emacs,
31 ;; Eshell uses the term.el package, and invokes them in a separate
32 ;; buffer, giving the illusion that Eshell itself is allowing these
33 ;; visual processes to execute.
37 (eval-when-compile (require 'eshell
))
40 (defgroup eshell-term nil
41 "This module causes visual commands (e.g., 'vi') to be executed by
42 the `term' package, which comes with Emacs. This package handles most
43 of the ANSI control codes, allowing curses-based applications to run
44 within an Emacs window. The variable `eshell-visual-commands' defines
45 which commands are considered visual in nature."
46 :tag
"Running visual commands"
47 :group
'eshell-module
)
51 (defcustom eshell-term-load-hook
'(eshell-term-initialize)
52 "*A list of functions to call when loading `eshell-term'."
56 (defcustom eshell-visual-commands
57 '("vi" ; what is going on??
58 "screen" "top" ; ok, a valid program...
59 "less" "more" ; M-x view-file
60 "lynx" "ncftp" ; w3.el, ange-ftp
61 "pine" "tin" "trn" "elm") ; GNUS!!
62 "*A list of commands that present their output in a visual fashion."
63 :type
'(repeat string
)
66 (defcustom eshell-term-name
"eterm"
67 "*Name to use for the TERM variable when running visual commands.
68 See `term-term-name' in term.el for more information on how this is
73 (defcustom eshell-escape-control-x t
74 "*If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
75 See the variable `eshell-visual-commands'. If this variable is set to
76 nil, <C-x> will send that control character to the invoked process."
80 ;;; Internal Variables:
82 (defvar eshell-parent-buffer
)
86 (defun eshell-term-initialize ()
87 "Initialize the `term' interface code."
88 (make-local-variable 'eshell-interpreter-alist
)
89 (setq eshell-interpreter-alist
92 (member (file-name-nondirectory command
)
93 eshell-visual-commands
)))
95 eshell-interpreter-alist
)))
97 (defun eshell-exec-visual (&rest args
)
98 "Run the specified PROGRAM in a terminal emulation buffer.
99 ARGS are passed to the program. At the moment, no piping of input is
101 (let* (eshell-interpreter-alist
102 (interp (eshell-find-interpreter (car args
)))
103 (program (car interp
))
104 (args (eshell-flatten-list
105 (eshell-stringify-list (append (cdr interp
)
109 (concat "*" (file-name-nondirectory program
) "*")))
110 (eshell-buf (current-buffer)))
112 (switch-to-buffer term-buf
)
114 (set (make-local-variable 'term-term-name
) eshell-term-name
)
115 (make-local-variable 'eshell-parent-buffer
)
116 (setq eshell-parent-buffer eshell-buf
)
117 (term-exec term-buf program program nil args
)
118 (let ((proc (get-buffer-process term-buf
)))
119 (if (and proc
(eq 'run
(process-status proc
)))
120 (set-process-sentinel proc
'eshell-term-sentinel
)
121 (error "Failed to invoke visual command")))
123 (if eshell-escape-control-x
124 (term-set-escape-char ?\C-x
))))
127 (defun eshell-term-sentinel (proc string
)
128 "Destroy the buffer visiting PROC."
129 (let ((proc-buf (process-buffer proc
)))
130 (when (and proc-buf
(buffer-live-p proc-buf
)
131 (not (eq 'run
(process-status proc
)))
132 (= (process-exit-status proc
) 0))
133 (if (eq (current-buffer) proc-buf
)
134 (let ((buf (and (boundp 'eshell-parent-buffer
)
136 (buffer-live-p eshell-parent-buffer
)
137 eshell-parent-buffer
)))
139 (switch-to-buffer buf
))))
140 (kill-buffer proc-buf
))))
142 ;; jww (1999-09-17): The code below will allow Eshell to send input
143 ;; characters directly to the currently running interactive process.
144 ;; However, since this would introduce other problems that would need
145 ;; solutions, I'm going to let it wait until after 2.1.
147 ; (defvar eshell-term-raw-map nil
148 ; "Keyboard map for sending characters directly to the inferior process.")
149 ; (defvar eshell-term-escape-char nil
150 ; "Escape character for char-sub-mode of term mode.
151 ; Do not change it directly; use term-set-escape-char instead.")
152 ; (defvar eshell-term-raw-escape-map nil)
154 ; (defun eshell-term-send-raw-string (chars)
155 ; (goto-char eshell-last-output-end)
156 ; (process-send-string (eshell-interactive-process) chars))
158 ; (defun eshell-term-send-raw ()
159 ; "Send the last character typed through the terminal-emulator
160 ; without any interpretation."
162 ; ;; Convert `return' to C-m, etc.
163 ; (if (and (symbolp last-input-char)
164 ; (get last-input-char 'ascii-character))
165 ; (setq last-input-char (get last-input-char 'ascii-character)))
166 ; (eshell-term-send-raw-string (make-string 1 last-input-char)))
168 ; (defun eshell-term-send-raw-meta ()
170 ; (if (symbolp last-input-char)
171 ; ;; Convert `return' to C-m, etc.
172 ; (let ((tmp (get last-input-char 'event-symbol-elements)))
174 ; (setq last-input-char (car tmp)))
175 ; (if (symbolp last-input-char)
177 ; (setq tmp (get last-input-char 'ascii-character))
178 ; (if tmp (setq last-input-char tmp))))))
179 ; (eshell-term-send-raw-string (if (and (numberp last-input-char)
180 ; (> last-input-char 127)
181 ; (< last-input-char 256))
182 ; (make-string 1 last-input-char)
183 ; (format "\e%c" last-input-char))))
185 ; (defun eshell-term-mouse-paste (click arg)
186 ; "Insert the last stretch of killed text at the position clicked on."
187 ; (interactive "e\nP")
188 ; (if (boundp 'xemacs-logo)
189 ; (eshell-term-send-raw-string
190 ; (or (condition-case () (x-get-selection) (error ()))
192 ; (error "No selection or cut buffer available")))
193 ; ;; Give temporary modes such as isearch a chance to turn off.
194 ; (run-hooks 'mouse-leave-buffer-hook)
195 ; (setq this-command 'yank)
196 ; (eshell-term-send-raw-string
197 ; (current-kill (cond ((listp arg) 0)
201 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
202 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
203 ; ;; For example: vi works with \eOA while elm wants \e[A ...
204 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
205 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
206 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
207 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
208 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
209 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
210 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
211 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
212 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
213 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
215 ; (defun eshell-term-set-escape-char (c)
216 ; "Change term-escape-char and keymaps that depend on it."
217 ; (if eshell-term-escape-char
218 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
219 ; (setq c (make-string 1 c))
220 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
221 ; ;; Define standard bindings in eshell-term-raw-escape-map
222 ; (define-key eshell-term-raw-escape-map "\C-x"
223 ; (lookup-key (current-global-map) "\C-x"))
224 ; (define-key eshell-term-raw-escape-map "\C-v"
225 ; (lookup-key (current-global-map) "\C-v"))
226 ; (define-key eshell-term-raw-escape-map "\C-u"
227 ; (lookup-key (current-global-map) "\C-u"))
228 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
230 ; (defun eshell-term-char-mode ()
231 ; "Switch to char (\"raw\") sub-mode of term mode.
232 ; Each character you type is sent directly to the inferior without
233 ; intervention from Emacs, except for the escape character (usually C-c)."
235 ; (if (not eshell-term-raw-map)
236 ; (let* ((map (make-keymap))
237 ; (esc-map (make-keymap))
240 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
241 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
243 ; (define-key map "\e" esc-map)
244 ; (setq eshell-term-raw-map map)
245 ; (setq eshell-term-raw-escape-map
246 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
247 ; (if (boundp 'xemacs-logo)
248 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
249 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
250 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
251 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
252 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
253 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
254 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
255 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
256 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
257 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
258 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
259 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
260 ; (eshell-term-set-escape-char ?\C-c))))
262 ; (defun eshell-term-line-mode ()
263 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
264 ; (use-local-map term-old-mode-map))
268 ;;; arch-tag: ab7c8fe4-3101-4257-925b-1354c6b2fe9d
269 ;;; em-term.el ends here