Merge branch 'master' into comment-cache
[emacs.git] / lisp / eshell / em-term.el
blobea38f12124ae2526f1ee119cca708ea28a85fbb9
1 ;;; em-term.el --- running visual commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2017 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/>.
22 ;;; Commentary:
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.
32 ;;; Code:
34 (require 'cl-lib)
35 (require 'esh-util)
36 (require 'esh-ext)
37 (eval-when-compile (require 'eshell))
38 (require 'term)
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))
51 ;;; User Variables:
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)
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.
67 Commands listed here are run in a term buffer.
69 See also `eshell-visual-subcommands' and `eshell-visual-options'."
70 :type '(repeat string)
71 :group 'eshell-term)
73 (defcustom eshell-visual-subcommands
74 nil
75 "An alist of subcommands that present their output in a visual fashion.
77 An alist of the form
79 ((COMMAND1 SUBCOMMAND1 SUBCOMMAND2...)
80 (COMMAND2 SUBCOMMAND1 ...))
82 of commands with subcommands that present their output in a
83 visual fashion. A likely entry is
85 (\"git\" \"log\" \"diff\" \"show\")
87 because git shows logs and diffs using a pager by default.
89 See also `eshell-visual-commands' and `eshell-visual-options'."
90 :type '(repeat (cons (string :tag "Command")
91 (repeat (string :tag "Subcommand"))))
92 :version "24.4"
93 :group 'eshell-term)
95 (defcustom eshell-visual-options
96 nil
97 "An alist of the form
99 ((COMMAND1 OPTION1 OPTION2...)
100 (COMMAND2 OPTION1 ...))
102 of commands with options that present their output in a visual
103 fashion. For example, a sensible entry would be
105 (\"git\" \"--help\" \"--paginate\")
107 because \"git <command> --help\" shows the command's
108 documentation with a pager and \"git --paginate <command>\"
109 always uses a pager for output.
111 See also `eshell-visual-commands' and `eshell-visual-subcommands'."
112 :type '(repeat (cons (string :tag "Command")
113 (repeat (string :tag "Option"))))
114 :version "24.4"
115 :group 'eshell-term)
117 ;; If you change this from term-term-name, you need to ensure that the
118 ;; value you choose exists in the system's terminfo database. (Bug#12485)
119 (defcustom eshell-term-name term-term-name
120 "Name to use for the TERM variable when running visual commands.
121 See `term-term-name' in term.el for more information on how this is
122 used."
123 :version "24.3" ; eterm -> term-term-name = eterm-color
124 :type 'string
125 :group 'eshell-term)
127 (defcustom eshell-escape-control-x t
128 "If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
129 See the variables `eshell-visual-commands',
130 `eshell-visual-subcommands', and `eshell-visual-options'. If
131 this variable is set to nil, <C-x> will send that control
132 character to the invoked process."
133 :type 'boolean
134 :group 'eshell-term)
136 (defcustom eshell-destroy-buffer-when-process-dies nil
137 "If non-nil, term buffers are destroyed after their processes die.
138 WARNING: Setting this to non-nil may result in unexpected
139 behavior for short-lived processes, see bug#18108."
140 :version "25.1"
141 :type 'boolean
142 :group 'eshell-term)
144 ;;; Internal Variables:
146 (defvar eshell-parent-buffer)
148 ;;; Functions:
150 (defun eshell-term-initialize ()
151 "Initialize the `term' interface code."
152 (make-local-variable 'eshell-interpreter-alist)
153 (setq eshell-interpreter-alist
154 (cons (cons #'eshell-visual-command-p
155 'eshell-exec-visual)
156 eshell-interpreter-alist)))
158 (defun eshell-visual-command-p (command args)
159 "Returns non-nil when given a visual command.
160 If either COMMAND or a subcommand in ARGS (e.g. git log) is a
161 visual command, returns non-nil."
162 (let ((command (file-name-nondirectory command)))
163 (and (eshell-interactive-output-p)
164 (or (member command eshell-visual-commands)
165 (member (car args)
166 (cdr (assoc command eshell-visual-subcommands)))
167 (cl-intersection args
168 (cdr (assoc command eshell-visual-options))
169 :test 'string=)))))
171 (defun eshell-exec-visual (&rest args)
172 "Run the specified PROGRAM in a terminal emulation buffer.
173 ARGS are passed to the program. At the moment, no piping of input is
174 allowed."
175 (let* (eshell-interpreter-alist
176 (interp (eshell-find-interpreter (car args) (cdr args)))
177 (program (car interp))
178 (args (eshell-flatten-list
179 (eshell-stringify-list (append (cdr interp)
180 (cdr args)))))
181 (term-buf
182 (generate-new-buffer
183 (concat "*" (file-name-nondirectory program) "*")))
184 (eshell-buf (current-buffer)))
185 (save-current-buffer
186 (switch-to-buffer term-buf)
187 (term-mode)
188 (set (make-local-variable 'term-term-name) eshell-term-name)
189 (make-local-variable 'eshell-parent-buffer)
190 (setq eshell-parent-buffer eshell-buf)
191 (term-exec term-buf program program nil args)
192 (let ((proc (get-buffer-process term-buf)))
193 (if (and proc (eq 'run (process-status proc)))
194 (set-process-sentinel proc 'eshell-term-sentinel)
195 (error "Failed to invoke visual command")))
196 (term-char-mode)
197 (if eshell-escape-control-x
198 (term-set-escape-char ?\C-x))))
199 nil)
201 ;; Process sentinels receive two arguments.
202 (defun eshell-term-sentinel (proc msg)
203 "Clean up the buffer visiting PROC.
204 If `eshell-destroy-buffer-when-process-dies' is non-nil, destroy
205 the buffer."
206 (term-sentinel proc msg) ;; First call the normal term sentinel.
207 (when eshell-destroy-buffer-when-process-dies
208 (let ((proc-buf (process-buffer proc)))
209 (when (and proc-buf (buffer-live-p proc-buf)
210 (not (eq 'run (process-status proc)))
211 (= (process-exit-status proc) 0))
212 (if (eq (current-buffer) proc-buf)
213 (let ((buf (and (boundp 'eshell-parent-buffer)
214 eshell-parent-buffer
215 (buffer-live-p eshell-parent-buffer)
216 eshell-parent-buffer)))
217 (if buf
218 (switch-to-buffer buf))))
219 (kill-buffer proc-buf)))))
221 ;; jww (1999-09-17): The code below will allow Eshell to send input
222 ;; characters directly to the currently running interactive process.
223 ;; However, since this would introduce other problems that would need
224 ;; solutions, I'm going to let it wait until after 2.1.
226 ; (defvar eshell-term-raw-map nil
227 ; "Keyboard map for sending characters directly to the inferior process.")
228 ; (defvar eshell-term-escape-char nil
229 ; "Escape character for char-sub-mode of term mode.
230 ; Do not change it directly; use term-set-escape-char instead.")
231 ; (defvar eshell-term-raw-escape-map nil)
233 ; (defun eshell-term-send-raw-string (chars)
234 ; (goto-char eshell-last-output-end)
235 ; (process-send-string (eshell-interactive-process) chars))
237 ; (defun eshell-term-send-raw ()
238 ; "Send the last character typed through the terminal-emulator
239 ; without any interpretation."
240 ; (interactive)
241 ; ;; Convert `return' to C-m, etc.
242 ; (if (and (symbolp last-input-event)
243 ; (get last-input-event 'ascii-character))
244 ; (setq last-input-event (get last-input-event 'ascii-character)))
245 ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
247 ; (defun eshell-term-send-raw-meta ()
248 ; (interactive)
249 ; (if (symbolp last-input-event)
250 ; ;; Convert `return' to C-m, etc.
251 ; (let ((tmp (get last-input-event 'event-symbol-elements)))
252 ; (if tmp
253 ; (setq last-input-event (car tmp)))
254 ; (if (symbolp last-input-event)
255 ; (progn
256 ; (setq tmp (get last-input-event 'ascii-character))
257 ; (if tmp (setq last-input-event tmp))))))
258 ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
259 ; (> last-input-event 127)
260 ; (< last-input-event 256))
261 ; (make-string 1 last-input-event)
262 ; (format "\e%c" last-input-event))))
264 ; (defun eshell-term-mouse-paste (click arg)
265 ; "Insert the last stretch of killed text at the position clicked on."
266 ; (interactive "e\nP")
267 ; (if (boundp 'xemacs-logo)
268 ; (eshell-term-send-raw-string
269 ; (or (condition-case () (x-get-selection) (error ()))
270 ; (error "No selection available")))
271 ; ;; Give temporary modes such as isearch a chance to turn off.
272 ; (run-hooks 'mouse-leave-buffer-hook)
273 ; (setq this-command 'yank)
274 ; (eshell-term-send-raw-string
275 ; (current-kill (cond ((listp arg) 0)
276 ; ((eq arg '-) -1)
277 ; (t (1- arg)))))))
279 ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
280 ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
281 ; ;; For example: vi works with \eOA while elm wants \e[A ...
282 ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
283 ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
284 ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
285 ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
286 ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
287 ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
288 ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
289 ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
290 ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
291 ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
293 ; (defun eshell-term-set-escape-char (c)
294 ; "Change term-escape-char and keymaps that depend on it."
295 ; (if eshell-term-escape-char
296 ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
297 ; (setq c (make-string 1 c))
298 ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
299 ; ;; Define standard bindings in eshell-term-raw-escape-map
300 ; (define-key eshell-term-raw-escape-map "\C-x"
301 ; (lookup-key (current-global-map) "\C-x"))
302 ; (define-key eshell-term-raw-escape-map "\C-v"
303 ; (lookup-key (current-global-map) "\C-v"))
304 ; (define-key eshell-term-raw-escape-map "\C-u"
305 ; (lookup-key (current-global-map) "\C-u"))
306 ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
308 ; (defun eshell-term-char-mode ()
309 ; "Switch to char (\"raw\") sub-mode of term mode.
310 ; Each character you type is sent directly to the inferior without
311 ; intervention from Emacs, except for the escape character (usually C-c)."
312 ; (interactive)
313 ; (if (not eshell-term-raw-map)
314 ; (let* ((map (make-keymap))
315 ; (esc-map (make-keymap))
316 ; (i 0))
317 ; (while (< i 128)
318 ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
319 ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
320 ; (setq i (1+ i)))
321 ; (define-key map "\e" esc-map)
322 ; (setq eshell-term-raw-map map)
323 ; (setq eshell-term-raw-escape-map
324 ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
325 ; (if (boundp 'xemacs-logo)
326 ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
327 ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
328 ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
329 ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
330 ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
331 ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
332 ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
333 ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
334 ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
335 ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
336 ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
337 ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
338 ; (eshell-term-set-escape-char ?\C-c))))
340 ; (defun eshell-term-line-mode ()
341 ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
342 ; (use-local-map term-old-mode-map))
344 (provide 'em-term)
346 ;; Local Variables:
347 ;; generated-autoload-file: "esh-groups.el"
348 ;; End:
350 ;;; em-term.el ends here