Merge from emacs-24; up to 2012-05-07T14:57:18Z!michael.albinus@gmx.de
[bpt/emacs.git] / lisp / eshell / em-term.el
1 ;;; em-term.el --- running visual commands
2
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
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.
31
32 ;;; Code:
33
34 (eval-when-compile (require 'eshell))
35 (require 'term)
36
37 ;;;###autoload
38 (progn
39 (defgroup eshell-term nil
40 "This module causes visual commands (e.g., 'vi') to be executed by
41 the `term' package, which comes with Emacs. This package handles most
42 of the ANSI control codes, allowing curses-based applications to run
43 within an Emacs window. The variable `eshell-visual-commands' defines
44 which commands are considered visual in nature."
45 :tag "Running visual commands"
46 :group 'eshell-module))
47
48 ;;; User Variables:
49
50 (defcustom eshell-term-load-hook nil
51 "A list of functions to call when loading `eshell-term'."
52 :version "24.1" ; removed eshell-term-initialize
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.
68 See `term-term-name' in term.el for more information on how this is
69 used."
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.
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)
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.
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)
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-event)
164 ; (get last-input-event 'ascii-character))
165 ; (setq last-input-event (get last-input-event 'ascii-character)))
166 ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
167
168 ; (defun eshell-term-send-raw-meta ()
169 ; (interactive)
170 ; (if (symbolp last-input-event)
171 ; ;; Convert `return' to C-m, etc.
172 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
173 ; (if tmp
174 ; (setq last-input-event (car tmp)))
175 ; (if (symbolp last-input-event)
176 ; (progn
177 ; (setq tmp (get last-input-event 'ascii-character))
178 ; (if tmp (setq last-input-event tmp))))))
179 ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
180 ; (> last-input-event 127)
181 ; (< last-input-event 256))
182 ; (make-string 1 last-input-event)
183 ; (format "\e%c" last-input-event))))
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 ; (error "No selection available")))
192 ; ;; Give temporary modes such as isearch a chance to turn off.
193 ; (run-hooks 'mouse-leave-buffer-hook)
194 ; (setq this-command 'yank)
195 ; (eshell-term-send-raw-string
196 ; (current-kill (cond ((listp arg) 0)
197 ; ((eq arg '-) -1)
198 ; (t (1- arg)))))))
199
200 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
201 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
202 ; ;; For example: vi works with \eOA while elm wants \e[A ...
203 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
204 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
205 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
206 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
207 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
208 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
209 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
210 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
211 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
212 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
213
214 ; (defun eshell-term-set-escape-char (c)
215 ; "Change term-escape-char and keymaps that depend on it."
216 ; (if eshell-term-escape-char
217 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
218 ; (setq c (make-string 1 c))
219 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
220 ; ;; Define standard bindings in eshell-term-raw-escape-map
221 ; (define-key eshell-term-raw-escape-map "\C-x"
222 ; (lookup-key (current-global-map) "\C-x"))
223 ; (define-key eshell-term-raw-escape-map "\C-v"
224 ; (lookup-key (current-global-map) "\C-v"))
225 ; (define-key eshell-term-raw-escape-map "\C-u"
226 ; (lookup-key (current-global-map) "\C-u"))
227 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
228
229 ; (defun eshell-term-char-mode ()
230 ; "Switch to char (\"raw\") sub-mode of term mode.
231 ; Each character you type is sent directly to the inferior without
232 ; intervention from Emacs, except for the escape character (usually C-c)."
233 ; (interactive)
234 ; (if (not eshell-term-raw-map)
235 ; (let* ((map (make-keymap))
236 ; (esc-map (make-keymap))
237 ; (i 0))
238 ; (while (< i 128)
239 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
240 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
241 ; (setq i (1+ i)))
242 ; (define-key map "\e" esc-map)
243 ; (setq eshell-term-raw-map map)
244 ; (setq eshell-term-raw-escape-map
245 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
246 ; (if (boundp 'xemacs-logo)
247 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
248 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
249 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
250 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
251 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
252 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
253 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
254 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
255 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
256 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
257 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
258 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
259 ; (eshell-term-set-escape-char ?\C-c))))
260
261 ; (defun eshell-term-line-mode ()
262 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
263 ; (use-local-map term-old-mode-map))
264
265 (provide 'em-term)
266
267 ;; Local Variables:
268 ;; generated-autoload-file: "esh-groups.el"
269 ;; End:
270
271 ;;; em-term.el ends here