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