Fix Eshell bug
[bpt/emacs.git] / 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 (require 'cl-lib)
35 (require 'esh-util)
36 (require 'esh-ext)
37 (eval-when-compile (require 'eshell))
38 (require 'term)
39
40 ;;;###autoload
41 (progn
42 (defgroup eshell-term nil
43 "This module causes visual commands (e.g., 'vi') to be executed by
44 the `term' package, which comes with Emacs. This package handles most
45 of the ANSI control codes, allowing curses-based applications to run
46 within an Emacs window. The variable `eshell-visual-commands' defines
47 which commands are considered visual in nature."
48 :tag "Running visual commands"
49 :group 'eshell-module))
50
51 ;;; User Variables:
52
53 (defcustom eshell-term-load-hook nil
54 "A list of functions to call when loading `eshell-term'."
55 :version "24.1" ; removed eshell-term-initialize
56 :type 'hook
57 :group 'eshell-term)
58
59 (defcustom eshell-visual-commands
60 '("vi" ; what is going on??
61 "screen" "top" ; ok, a valid program...
62 "less" "more" ; M-x view-file
63 "lynx" "ncftp" ; w3.el, ange-ftp
64 "pine" "tin" "trn" "elm") ; GNUS!!
65 "A list of commands that present their output in a visual fashion."
66 :type '(repeat string)
67 :group 'eshell-term)
68
69 (defcustom eshell-visual-subcommands
70 nil
71 "An alist of the form
72
73 ((COMMAND1 SUBCOMMAND1 SUBCOMMAND2...)
74 (COMMAND2 SUBCOMMAND1 ...))
75
76 of commands with subcommands that present their output in a
77 visual fashion. A likely entry is
78
79 (\"git\" \"log\" \"diff\" \"show\")
80
81 because git shows logs and diffs using a pager by default."
82 :type '(repeat (cons (string :tag "Command")
83 (repeat (string :tag "Subcommand"))))
84 :version "24.4"
85 :group 'eshell-term)
86
87 (defcustom eshell-visual-options
88 nil
89 "An alist of the form
90
91 ((COMMAND1 OPTION1 OPTION2...)
92 (COMMAND2 OPTION1 ...))
93
94 of commands with options that present their output in a visual
95 fashion. For example, a sensible entry would be
96
97 (\"git\" \"--help\")
98
99 because \"git <command> --help\" shows the command's
100 documentation with a pager."
101 :type '(repeat (cons (string :tag "Command")
102 (repeat (string :tag "Option"))))
103 :version "24.4"
104 :group 'eshell-term)
105
106 ;; If you change this from term-term-name, you need to ensure that the
107 ;; value you choose exists in the system's terminfo database. (Bug#12485)
108 (defcustom eshell-term-name term-term-name
109 "Name to use for the TERM variable when running visual commands.
110 See `term-term-name' in term.el for more information on how this is
111 used."
112 :version "24.3" ; eterm -> term-term-name = eterm-color
113 :type 'string
114 :group 'eshell-term)
115
116 (defcustom eshell-escape-control-x t
117 "If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
118 See the variables `eshell-visual-commands',
119 `eshell-visual-subcommands', and `eshell-visual-options'. If
120 this variable is set to nil, <C-x> will send that control
121 character to the invoked process."
122 :type 'boolean
123 :group 'eshell-term)
124
125 ;;; Internal Variables:
126
127 (defvar eshell-parent-buffer)
128
129 ;;; Functions:
130
131 (defun eshell-term-initialize ()
132 "Initialize the `term' interface code."
133 (make-local-variable 'eshell-interpreter-alist)
134 (setq eshell-interpreter-alist
135 (cons (cons #'eshell-visual-command-p
136 'eshell-exec-visual)
137 eshell-interpreter-alist)))
138
139 (defun eshell-visual-command-p (command args)
140 "Returns non-nil when given a visual command.
141 If either COMMAND or a subcommand in ARGS (e.g. git log) is a
142 visual command, returns non-nil."
143 (let ((command (file-name-nondirectory command)))
144 (and (eshell-interactive-output-p)
145 (or (member command eshell-visual-commands)
146 (member (car args)
147 (cdr (assoc command eshell-visual-subcommands)))
148 (cl-intersection args
149 (cdr (assoc command eshell-visual-options))
150 :test 'string=)))))
151
152 (defun eshell-exec-visual (&rest args)
153 "Run the specified PROGRAM in a terminal emulation buffer.
154 ARGS are passed to the program. At the moment, no piping of input is
155 allowed."
156 (let* (eshell-interpreter-alist
157 (interp (eshell-find-interpreter (car args) (cdr args)))
158 (program (car interp))
159 (args (eshell-flatten-list
160 (eshell-stringify-list (append (cdr interp)
161 (cdr args)))))
162 (term-buf
163 (generate-new-buffer
164 (concat "*" (file-name-nondirectory program) "*")))
165 (eshell-buf (current-buffer)))
166 (save-current-buffer
167 (switch-to-buffer term-buf)
168 (term-mode)
169 (set (make-local-variable 'term-term-name) eshell-term-name)
170 (make-local-variable 'eshell-parent-buffer)
171 (setq eshell-parent-buffer eshell-buf)
172 (term-exec term-buf program program nil args)
173 (let ((proc (get-buffer-process term-buf)))
174 (if (and proc (eq 'run (process-status proc)))
175 (set-process-sentinel proc 'eshell-term-sentinel)
176 (error "Failed to invoke visual command")))
177 (term-char-mode)
178 (if eshell-escape-control-x
179 (term-set-escape-char ?\C-x))))
180 nil)
181
182 (defun eshell-term-sentinel (proc string)
183 "Destroy the buffer visiting PROC."
184 (let ((proc-buf (process-buffer proc)))
185 (when (and proc-buf (buffer-live-p proc-buf)
186 (not (eq 'run (process-status proc)))
187 (= (process-exit-status proc) 0))
188 (if (eq (current-buffer) proc-buf)
189 (let ((buf (and (boundp 'eshell-parent-buffer)
190 eshell-parent-buffer
191 (buffer-live-p eshell-parent-buffer)
192 eshell-parent-buffer)))
193 (if buf
194 (switch-to-buffer buf))))
195 (kill-buffer proc-buf))))
196
197 ;; jww (1999-09-17): The code below will allow Eshell to send input
198 ;; characters directly to the currently running interactive process.
199 ;; However, since this would introduce other problems that would need
200 ;; solutions, I'm going to let it wait until after 2.1.
201
202 ; (defvar eshell-term-raw-map nil
203 ; "Keyboard map for sending characters directly to the inferior process.")
204 ; (defvar eshell-term-escape-char nil
205 ; "Escape character for char-sub-mode of term mode.
206 ; Do not change it directly; use term-set-escape-char instead.")
207 ; (defvar eshell-term-raw-escape-map nil)
208
209 ; (defun eshell-term-send-raw-string (chars)
210 ; (goto-char eshell-last-output-end)
211 ; (process-send-string (eshell-interactive-process) chars))
212
213 ; (defun eshell-term-send-raw ()
214 ; "Send the last character typed through the terminal-emulator
215 ; without any interpretation."
216 ; (interactive)
217 ; ;; Convert `return' to C-m, etc.
218 ; (if (and (symbolp last-input-event)
219 ; (get last-input-event 'ascii-character))
220 ; (setq last-input-event (get last-input-event 'ascii-character)))
221 ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
222
223 ; (defun eshell-term-send-raw-meta ()
224 ; (interactive)
225 ; (if (symbolp last-input-event)
226 ; ;; Convert `return' to C-m, etc.
227 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
228 ; (if tmp
229 ; (setq last-input-event (car tmp)))
230 ; (if (symbolp last-input-event)
231 ; (progn
232 ; (setq tmp (get last-input-event 'ascii-character))
233 ; (if tmp (setq last-input-event tmp))))))
234 ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
235 ; (> last-input-event 127)
236 ; (< last-input-event 256))
237 ; (make-string 1 last-input-event)
238 ; (format "\e%c" last-input-event))))
239
240 ; (defun eshell-term-mouse-paste (click arg)
241 ; "Insert the last stretch of killed text at the position clicked on."
242 ; (interactive "e\nP")
243 ; (if (boundp 'xemacs-logo)
244 ; (eshell-term-send-raw-string
245 ; (or (condition-case () (x-get-selection) (error ()))
246 ; (error "No selection available")))
247 ; ;; Give temporary modes such as isearch a chance to turn off.
248 ; (run-hooks 'mouse-leave-buffer-hook)
249 ; (setq this-command 'yank)
250 ; (eshell-term-send-raw-string
251 ; (current-kill (cond ((listp arg) 0)
252 ; ((eq arg '-) -1)
253 ; (t (1- arg)))))))
254
255 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
256 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
257 ; ;; For example: vi works with \eOA while elm wants \e[A ...
258 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
259 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
260 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
261 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
262 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
263 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
264 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
265 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
266 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
267 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
268
269 ; (defun eshell-term-set-escape-char (c)
270 ; "Change term-escape-char and keymaps that depend on it."
271 ; (if eshell-term-escape-char
272 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
273 ; (setq c (make-string 1 c))
274 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
275 ; ;; Define standard bindings in eshell-term-raw-escape-map
276 ; (define-key eshell-term-raw-escape-map "\C-x"
277 ; (lookup-key (current-global-map) "\C-x"))
278 ; (define-key eshell-term-raw-escape-map "\C-v"
279 ; (lookup-key (current-global-map) "\C-v"))
280 ; (define-key eshell-term-raw-escape-map "\C-u"
281 ; (lookup-key (current-global-map) "\C-u"))
282 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
283
284 ; (defun eshell-term-char-mode ()
285 ; "Switch to char (\"raw\") sub-mode of term mode.
286 ; Each character you type is sent directly to the inferior without
287 ; intervention from Emacs, except for the escape character (usually C-c)."
288 ; (interactive)
289 ; (if (not eshell-term-raw-map)
290 ; (let* ((map (make-keymap))
291 ; (esc-map (make-keymap))
292 ; (i 0))
293 ; (while (< i 128)
294 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
295 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
296 ; (setq i (1+ i)))
297 ; (define-key map "\e" esc-map)
298 ; (setq eshell-term-raw-map map)
299 ; (setq eshell-term-raw-escape-map
300 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
301 ; (if (boundp 'xemacs-logo)
302 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
303 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
304 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
305 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
306 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
307 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
308 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
309 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
310 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
311 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
312 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
313 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
314 ; (eshell-term-set-escape-char ?\C-c))))
315
316 ; (defun eshell-term-line-mode ()
317 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
318 ; (use-local-map term-old-mode-map))
319
320 (provide 'em-term)
321
322 ;; Local Variables:
323 ;; generated-autoload-file: "esh-groups.el"
324 ;; End:
325
326 ;;; em-term.el ends here