]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-term.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / eshell / em-term.el
1 ;;; em-term.el --- running visual commands
2
3 ;; Copyright (C) 1999-2011 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 progrem 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 (eshell-defgroup eshell-term nil
39 "This module causes visual commands (e.g., 'vi') to be executed by
40 the `term' package, which comes with Emacs. This package handles most
41 of the ANSI control codes, allowing curses-based applications to run
42 within an Emacs window. The variable `eshell-visual-commands' defines
43 which commands are considered visual in nature."
44 :tag "Running visual commands"
45 :group 'eshell-module)
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.
66 See `term-term-name' in term.el for more information on how this is
67 used."
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.
73 See the variable `eshell-visual-commands'. If this variable is set to
74 nil, <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.
97 ARGS are passed to the program. At the moment, no piping of input is
98 allowed."
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-event)
162 ; (get last-input-event 'ascii-character))
163 ; (setq last-input-event (get last-input-event 'ascii-character)))
164 ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
165
166 ; (defun eshell-term-send-raw-meta ()
167 ; (interactive)
168 ; (if (symbolp last-input-event)
169 ; ;; Convert `return' to C-m, etc.
170 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
171 ; (if tmp
172 ; (setq last-input-event (car tmp)))
173 ; (if (symbolp last-input-event)
174 ; (progn
175 ; (setq tmp (get last-input-event 'ascii-character))
176 ; (if tmp (setq last-input-event tmp))))))
177 ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
178 ; (> last-input-event 127)
179 ; (< last-input-event 256))
180 ; (make-string 1 last-input-event)
181 ; (format "\e%c" last-input-event))))
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 ; (error "No selection available")))
190 ; ;; Give temporary modes such as isearch a chance to turn off.
191 ; (run-hooks 'mouse-leave-buffer-hook)
192 ; (setq this-command 'yank)
193 ; (eshell-term-send-raw-string
194 ; (current-kill (cond ((listp arg) 0)
195 ; ((eq arg '-) -1)
196 ; (t (1- arg)))))))
197
198 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
199 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
200 ; ;; For example: vi works with \eOA while elm wants \e[A ...
201 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
202 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
203 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
204 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
205 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
206 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
207 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
208 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
209 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
210 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
211
212 ; (defun eshell-term-set-escape-char (c)
213 ; "Change term-escape-char and keymaps that depend on it."
214 ; (if eshell-term-escape-char
215 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
216 ; (setq c (make-string 1 c))
217 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
218 ; ;; Define standard bindings in eshell-term-raw-escape-map
219 ; (define-key eshell-term-raw-escape-map "\C-x"
220 ; (lookup-key (current-global-map) "\C-x"))
221 ; (define-key eshell-term-raw-escape-map "\C-v"
222 ; (lookup-key (current-global-map) "\C-v"))
223 ; (define-key eshell-term-raw-escape-map "\C-u"
224 ; (lookup-key (current-global-map) "\C-u"))
225 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
226
227 ; (defun eshell-term-char-mode ()
228 ; "Switch to char (\"raw\") sub-mode of term mode.
229 ; Each character you type is sent directly to the inferior without
230 ; intervention from Emacs, except for the escape character (usually C-c)."
231 ; (interactive)
232 ; (if (not eshell-term-raw-map)
233 ; (let* ((map (make-keymap))
234 ; (esc-map (make-keymap))
235 ; (i 0))
236 ; (while (< i 128)
237 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
238 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
239 ; (setq i (1+ i)))
240 ; (define-key map "\e" esc-map)
241 ; (setq eshell-term-raw-map map)
242 ; (setq eshell-term-raw-escape-map
243 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
244 ; (if (boundp 'xemacs-logo)
245 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
246 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
247 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
248 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
249 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
250 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
251 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
252 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
253 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
254 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
255 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
256 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
257 ; (eshell-term-set-escape-char ?\C-c))))
258
259 ; (defun eshell-term-line-mode ()
260 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
261 ; (use-local-map term-old-mode-map))
262
263 (provide 'em-term)
264
265 ;; Local Variables:
266 ;; generated-autoload-file: "esh-groups.el"
267 ;; End:
268
269 ;;; em-term.el ends here