1 ;;; em-term.el --- running visual commands
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
34 (eval-when-compile (require 'eshell
))
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
))
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
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
)
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
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."
80 ;;; Internal Variables:
82 (defvar eshell-parent-buffer
)
86 (defun eshell-term-initialize ()
87 "Initialize the `term' interface code."
88 (make-local-variable 'eshell-interpreter-alist
)
89 (setq eshell-interpreter-alist
92 (member (file-name-nondirectory command
)
93 eshell-visual-commands
)))
95 eshell-interpreter-alist
)))
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
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
)
109 (concat "*" (file-name-nondirectory program
) "*")))
110 (eshell-buf (current-buffer)))
112 (switch-to-buffer term-buf
)
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")))
123 (if eshell-escape-control-x
124 (term-set-escape-char ?\C-x
))))
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
)
136 (buffer-live-p eshell-parent-buffer
)
137 eshell-parent-buffer
)))
139 (switch-to-buffer buf
))))
140 (kill-buffer proc-buf
))))
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.
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)
154 ; (defun eshell-term-send-raw-string (chars)
155 ; (goto-char eshell-last-output-end)
156 ; (process-send-string (eshell-interactive-process) chars))
158 ; (defun eshell-term-send-raw ()
159 ; "Send the last character typed through the terminal-emulator
160 ; without any interpretation."
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)))
168 ; (defun eshell-term-send-raw-meta ()
170 ; (if (symbolp last-input-event)
171 ; ;; Convert `return' to C-m, etc.
172 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
174 ; (setq last-input-event (car tmp)))
175 ; (if (symbolp last-input-event)
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))))
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)
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"))
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))
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)."
234 ; (if (not eshell-term-raw-map)
235 ; (let* ((map (make-keymap))
236 ; (esc-map (make-keymap))
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)
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))))
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))
268 ;; generated-autoload-file: "esh-groups.el"
271 ;;; em-term.el ends here