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