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