Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / eshell / em-term.el
CommitLineData
60370d40 1;;; em-term.el --- running visual commands
affbf647 2
f2e3589a 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
8b72699e 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
affbf647 5
7de5b421
GM
6;; Author: John Wiegley <johnw@gnu.org>
7
affbf647
GM
8;; This file is part of GNU Emacs.
9
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
e0085d62 12;; the Free Software Foundation; either version 3, or (at your option)
affbf647
GM
13;; any later version.
14
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.
19
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
3a35cf56
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
affbf647 24
affbf647
GM
25;;; Commentary:
26
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.
34
dbba8a04
GM
35;;; Code:
36
37(eval-when-compile (require 'eshell))
affbf647
GM
38(require 'term)
39
dbba8a04
GM
40(defgroup eshell-term nil
41 "This module causes visual commands (e.g., 'vi') to be executed by
42the `term' package, which comes with Emacs. This package handles most
43of the ANSI control codes, allowing curses-based applications to run
44within an Emacs window. The variable `eshell-visual-commands' defines
45which commands are considered visual in nature."
46 :tag "Running visual commands"
47 :group 'eshell-module)
48
affbf647
GM
49;;; User Variables:
50
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)
55
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)
65
66(defcustom eshell-term-name "eterm"
67 "*Name to use for the TERM variable when running visual commands.
68See `term-term-name' in term.el for more information on how this is
69used."
70 :type 'string
71 :group 'eshell-term)
72
73(defcustom eshell-escape-control-x t
74 "*If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
75See the variable `eshell-visual-commands'. If this variable is set to
76nil, <C-x> will send that control character to the invoked process."
77 :type 'boolean
78 :group 'eshell-term)
79
80;;; Internal Variables:
81
82(defvar eshell-parent-buffer)
83
84;;; Functions:
85
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)))
96
97(defun eshell-exec-visual (&rest args)
98 "Run the specified PROGRAM in a terminal emulation buffer.
99ARGS are passed to the program. At the moment, no piping of input is
100allowed."
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)
126
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))))
141
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.
146
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)
153
154; (defun eshell-term-send-raw-string (chars)
155; (goto-char eshell-last-output-end)
156; (process-send-string (eshell-interactive-process) chars))
157
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)))
167
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))))
184
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)))))))
200
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"))
214
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))
229
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))))
261
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))
265
dbba8a04 266(provide 'em-term)
affbf647 267
cbee283d 268;; arch-tag: ab7c8fe4-3101-4257-925b-1354c6b2fe9d
affbf647 269;;; em-term.el ends here