]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-term.el
Update copyright notices for 2013.
[gnu-emacs] / lisp / eshell / em-term.el
1 ;;; em-term.el --- running visual commands
2
3 ;; Copyright (C) 1999-2013 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 ;; If you change this from term-term-name, you need to ensure that the
67 ;; value you choose exists in the system's terminfo database. (Bug#12485)
68 (defcustom eshell-term-name term-term-name
69 "Name to use for the TERM variable when running visual commands.
70 See `term-term-name' in term.el for more information on how this is
71 used."
72 :version "24.3" ; eterm -> term-term-name = eterm-color
73 :type 'string
74 :group 'eshell-term)
75
76 (defcustom eshell-escape-control-x t
77 "If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
78 See the variable `eshell-visual-commands'. If this variable is set to
79 nil, <C-x> will send that control character to the invoked process."
80 :type 'boolean
81 :group 'eshell-term)
82
83 ;;; Internal Variables:
84
85 (defvar eshell-parent-buffer)
86
87 ;;; Functions:
88
89 (defun eshell-term-initialize ()
90 "Initialize the `term' interface code."
91 (make-local-variable 'eshell-interpreter-alist)
92 (setq eshell-interpreter-alist
93 (cons (cons (function
94 (lambda (command)
95 (member (file-name-nondirectory command)
96 eshell-visual-commands)))
97 'eshell-exec-visual)
98 eshell-interpreter-alist)))
99
100 (defun eshell-exec-visual (&rest args)
101 "Run the specified PROGRAM in a terminal emulation buffer.
102 ARGS are passed to the program. At the moment, no piping of input is
103 allowed."
104 (let* (eshell-interpreter-alist
105 (interp (eshell-find-interpreter (car args)))
106 (program (car interp))
107 (args (eshell-flatten-list
108 (eshell-stringify-list (append (cdr interp)
109 (cdr args)))))
110 (term-buf
111 (generate-new-buffer
112 (concat "*" (file-name-nondirectory program) "*")))
113 (eshell-buf (current-buffer)))
114 (save-current-buffer
115 (switch-to-buffer term-buf)
116 (term-mode)
117 (set (make-local-variable 'term-term-name) eshell-term-name)
118 (make-local-variable 'eshell-parent-buffer)
119 (setq eshell-parent-buffer eshell-buf)
120 (term-exec term-buf program program nil args)
121 (let ((proc (get-buffer-process term-buf)))
122 (if (and proc (eq 'run (process-status proc)))
123 (set-process-sentinel proc 'eshell-term-sentinel)
124 (error "Failed to invoke visual command")))
125 (term-char-mode)
126 (if eshell-escape-control-x
127 (term-set-escape-char ?\C-x))))
128 nil)
129
130 (defun eshell-term-sentinel (proc string)
131 "Destroy the buffer visiting PROC."
132 (let ((proc-buf (process-buffer proc)))
133 (when (and proc-buf (buffer-live-p proc-buf)
134 (not (eq 'run (process-status proc)))
135 (= (process-exit-status proc) 0))
136 (if (eq (current-buffer) proc-buf)
137 (let ((buf (and (boundp 'eshell-parent-buffer)
138 eshell-parent-buffer
139 (buffer-live-p eshell-parent-buffer)
140 eshell-parent-buffer)))
141 (if buf
142 (switch-to-buffer buf))))
143 (kill-buffer proc-buf))))
144
145 ;; jww (1999-09-17): The code below will allow Eshell to send input
146 ;; characters directly to the currently running interactive process.
147 ;; However, since this would introduce other problems that would need
148 ;; solutions, I'm going to let it wait until after 2.1.
149
150 ; (defvar eshell-term-raw-map nil
151 ; "Keyboard map for sending characters directly to the inferior process.")
152 ; (defvar eshell-term-escape-char nil
153 ; "Escape character for char-sub-mode of term mode.
154 ; Do not change it directly; use term-set-escape-char instead.")
155 ; (defvar eshell-term-raw-escape-map nil)
156
157 ; (defun eshell-term-send-raw-string (chars)
158 ; (goto-char eshell-last-output-end)
159 ; (process-send-string (eshell-interactive-process) chars))
160
161 ; (defun eshell-term-send-raw ()
162 ; "Send the last character typed through the terminal-emulator
163 ; without any interpretation."
164 ; (interactive)
165 ; ;; Convert `return' to C-m, etc.
166 ; (if (and (symbolp last-input-event)
167 ; (get last-input-event 'ascii-character))
168 ; (setq last-input-event (get last-input-event 'ascii-character)))
169 ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
170
171 ; (defun eshell-term-send-raw-meta ()
172 ; (interactive)
173 ; (if (symbolp last-input-event)
174 ; ;; Convert `return' to C-m, etc.
175 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
176 ; (if tmp
177 ; (setq last-input-event (car tmp)))
178 ; (if (symbolp last-input-event)
179 ; (progn
180 ; (setq tmp (get last-input-event 'ascii-character))
181 ; (if tmp (setq last-input-event tmp))))))
182 ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
183 ; (> last-input-event 127)
184 ; (< last-input-event 256))
185 ; (make-string 1 last-input-event)
186 ; (format "\e%c" last-input-event))))
187
188 ; (defun eshell-term-mouse-paste (click arg)
189 ; "Insert the last stretch of killed text at the position clicked on."
190 ; (interactive "e\nP")
191 ; (if (boundp 'xemacs-logo)
192 ; (eshell-term-send-raw-string
193 ; (or (condition-case () (x-get-selection) (error ()))
194 ; (error "No selection available")))
195 ; ;; Give temporary modes such as isearch a chance to turn off.
196 ; (run-hooks 'mouse-leave-buffer-hook)
197 ; (setq this-command 'yank)
198 ; (eshell-term-send-raw-string
199 ; (current-kill (cond ((listp arg) 0)
200 ; ((eq arg '-) -1)
201 ; (t (1- arg)))))))
202
203 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
204 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
205 ; ;; For example: vi works with \eOA while elm wants \e[A ...
206 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
207 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
208 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
209 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
210 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
211 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
212 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
213 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
214 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
215 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
216
217 ; (defun eshell-term-set-escape-char (c)
218 ; "Change term-escape-char and keymaps that depend on it."
219 ; (if eshell-term-escape-char
220 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
221 ; (setq c (make-string 1 c))
222 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
223 ; ;; Define standard bindings in eshell-term-raw-escape-map
224 ; (define-key eshell-term-raw-escape-map "\C-x"
225 ; (lookup-key (current-global-map) "\C-x"))
226 ; (define-key eshell-term-raw-escape-map "\C-v"
227 ; (lookup-key (current-global-map) "\C-v"))
228 ; (define-key eshell-term-raw-escape-map "\C-u"
229 ; (lookup-key (current-global-map) "\C-u"))
230 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
231
232 ; (defun eshell-term-char-mode ()
233 ; "Switch to char (\"raw\") sub-mode of term mode.
234 ; Each character you type is sent directly to the inferior without
235 ; intervention from Emacs, except for the escape character (usually C-c)."
236 ; (interactive)
237 ; (if (not eshell-term-raw-map)
238 ; (let* ((map (make-keymap))
239 ; (esc-map (make-keymap))
240 ; (i 0))
241 ; (while (< i 128)
242 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
243 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
244 ; (setq i (1+ i)))
245 ; (define-key map "\e" esc-map)
246 ; (setq eshell-term-raw-map map)
247 ; (setq eshell-term-raw-escape-map
248 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
249 ; (if (boundp 'xemacs-logo)
250 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
251 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
252 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
253 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
254 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
255 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
256 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
257 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
258 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
259 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
260 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
261 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
262 ; (eshell-term-set-escape-char ?\C-c))))
263
264 ; (defun eshell-term-line-mode ()
265 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
266 ; (use-local-map term-old-mode-map))
267
268 (provide 'em-term)
269
270 ;; Local Variables:
271 ;; generated-autoload-file: "esh-groups.el"
272 ;; End:
273
274 ;;; em-term.el ends here