lwlib/Imakefile is removed.
[emacs.git] / lisp / eshell / em-term.el
blobafd8e9596ef1f4b2d7229a77985e6b248c174677
1 ;;; em-term.el --- running visual commands
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
12 ;; 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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 (provide 'em-term)
26 (eval-when-compile (require 'esh-maint))
28 (defgroup eshell-term nil
29 "This module causes visual commands (e.g., 'vi') to be executed by
30 the `term' package, which comes with Emacs. This package handles most
31 of the ANSI control codes, allowing curses-based applications to run
32 within an Emacs window. The variable `eshell-visual-commands' defines
33 which commands are considered visual in nature."
34 :tag "Running visual commands"
35 :group 'eshell-module)
37 ;;; Commentary:
39 ;; At the moment, eshell is stream-based in its interactive input and
40 ;; output. This means that full-screen commands, such as "vi" or
41 ;; "lynx", will not display correctly. These are therefore thought of
42 ;; as "visual" programs. In order to run these progrem under Emacs,
43 ;; Eshell uses the term.el package, and invokes them in a separate
44 ;; buffer, giving the illusion that Eshell itself is allowing these
45 ;; visual processes to execute.
47 (require 'term)
49 ;;; User Variables:
51 (defcustom eshell-term-load-hook '(eshell-term-initialize)
52 "*A list of functions to call when loading `eshell-term'."
53 :type 'hook
54 :group '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)
64 :group 'eshell-term)
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
69 used."
70 :type 'string
71 :group 'eshell-term)
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."
77 :type 'boolean
78 :group 'eshell-term)
80 ;;; Internal Variables:
82 (defvar eshell-parent-buffer)
84 ;;; Functions:
86 (defun eshell-term-initialize ()
87 "Initialize the `term' interface code."
88 (make-local-variable 'eshell-interpreter-alist)
89 (setq eshell-interpreter-alist
90 (cons (cons (function
91 (lambda (command)
92 (member (file-name-nondirectory command)
93 eshell-visual-commands)))
94 'eshell-exec-visual)
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
100 allowed."
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)
106 (cdr args)))))
107 (term-buf
108 (generate-new-buffer
109 (concat "*" (file-name-nondirectory program) "*")))
110 (eshell-buf (current-buffer)))
111 (save-current-buffer
112 (switch-to-buffer term-buf)
113 (term-mode)
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")))
122 (term-char-mode)
123 (if eshell-escape-control-x
124 (term-set-escape-char ?\C-x))))
125 nil)
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)
135 eshell-parent-buffer
136 (buffer-live-p eshell-parent-buffer)
137 eshell-parent-buffer)))
138 (if buf
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."
161 ; (interactive)
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 ()
169 ; (interactive)
170 ; (if (symbolp last-input-char)
171 ; ;; Convert `return' to C-m, etc.
172 ; (let ((tmp (get last-input-char 'event-symbol-elements)))
173 ; (if tmp
174 ; (setq last-input-char (car tmp)))
175 ; (if (symbolp last-input-char)
176 ; (progn
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 ()))
191 ; (x-get-cutbuffer)
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)
198 ; ((eq arg '-) -1)
199 ; (t (1- arg)))))))
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)."
234 ; (interactive)
235 ; (if (not eshell-term-raw-map)
236 ; (let* ((map (make-keymap))
237 ; (esc-map (make-keymap))
238 ; (i 0))
239 ; (while (< i 128)
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)
242 ; (setq i (1+ i)))
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))
266 ;;; Code:
268 ;;; em-term.el ends here