em-term.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ;;; em-term.el --- running visual commands
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; At the moment, eshell is stream-based in its interactive input and
  17. ;; output. This means that full-screen commands, such as "vi" or
  18. ;; "lynx", will not display correctly. These are therefore thought of
  19. ;; as "visual" programs. In order to run these programs under Emacs,
  20. ;; Eshell uses the term.el package, and invokes them in a separate
  21. ;; buffer, giving the illusion that Eshell itself is allowing these
  22. ;; visual processes to execute.
  23. ;;; Code:
  24. (eval-when-compile (require 'eshell))
  25. (require 'term)
  26. ;;;###autoload
  27. (eshell-defgroup eshell-term nil
  28. "This module causes visual commands (e.g., 'vi') to be executed by
  29. the `term' package, which comes with Emacs. This package handles most
  30. of the ANSI control codes, allowing curses-based applications to run
  31. within an Emacs window. The variable `eshell-visual-commands' defines
  32. which commands are considered visual in nature."
  33. :tag "Running visual commands"
  34. :group 'eshell-module)
  35. ;;; User Variables:
  36. (defcustom eshell-term-load-hook nil
  37. "A list of functions to call when loading `eshell-term'."
  38. :version "24.1" ; removed eshell-term-initialize
  39. :type 'hook
  40. :group 'eshell-term)
  41. (defcustom eshell-visual-commands
  42. '("vi" ; what is going on??
  43. "screen" "top" ; ok, a valid program...
  44. "less" "more" ; M-x view-file
  45. "lynx" "ncftp" ; w3.el, ange-ftp
  46. "pine" "tin" "trn" "elm") ; GNUS!!
  47. "A list of commands that present their output in a visual fashion."
  48. :type '(repeat string)
  49. :group 'eshell-term)
  50. (defcustom eshell-term-name "eterm"
  51. "Name to use for the TERM variable when running visual commands.
  52. See `term-term-name' in term.el for more information on how this is
  53. used."
  54. :type 'string
  55. :group 'eshell-term)
  56. (defcustom eshell-escape-control-x t
  57. "If non-nil, allow <C-x> to be handled by Emacs key in visual buffers.
  58. See the variable `eshell-visual-commands'. If this variable is set to
  59. nil, <C-x> will send that control character to the invoked process."
  60. :type 'boolean
  61. :group 'eshell-term)
  62. ;;; Internal Variables:
  63. (defvar eshell-parent-buffer)
  64. ;;; Functions:
  65. (defun eshell-term-initialize ()
  66. "Initialize the `term' interface code."
  67. (make-local-variable 'eshell-interpreter-alist)
  68. (setq eshell-interpreter-alist
  69. (cons (cons (function
  70. (lambda (command)
  71. (member (file-name-nondirectory command)
  72. eshell-visual-commands)))
  73. 'eshell-exec-visual)
  74. eshell-interpreter-alist)))
  75. (defun eshell-exec-visual (&rest args)
  76. "Run the specified PROGRAM in a terminal emulation buffer.
  77. ARGS are passed to the program. At the moment, no piping of input is
  78. allowed."
  79. (let* (eshell-interpreter-alist
  80. (interp (eshell-find-interpreter (car args)))
  81. (program (car interp))
  82. (args (eshell-flatten-list
  83. (eshell-stringify-list (append (cdr interp)
  84. (cdr args)))))
  85. (term-buf
  86. (generate-new-buffer
  87. (concat "*" (file-name-nondirectory program) "*")))
  88. (eshell-buf (current-buffer)))
  89. (save-current-buffer
  90. (switch-to-buffer term-buf)
  91. (term-mode)
  92. (set (make-local-variable 'term-term-name) eshell-term-name)
  93. (make-local-variable 'eshell-parent-buffer)
  94. (setq eshell-parent-buffer eshell-buf)
  95. (term-exec term-buf program program nil args)
  96. (let ((proc (get-buffer-process term-buf)))
  97. (if (and proc (eq 'run (process-status proc)))
  98. (set-process-sentinel proc 'eshell-term-sentinel)
  99. (error "Failed to invoke visual command")))
  100. (term-char-mode)
  101. (if eshell-escape-control-x
  102. (term-set-escape-char ?\C-x))))
  103. nil)
  104. (defun eshell-term-sentinel (proc string)
  105. "Destroy the buffer visiting PROC."
  106. (let ((proc-buf (process-buffer proc)))
  107. (when (and proc-buf (buffer-live-p proc-buf)
  108. (not (eq 'run (process-status proc)))
  109. (= (process-exit-status proc) 0))
  110. (if (eq (current-buffer) proc-buf)
  111. (let ((buf (and (boundp 'eshell-parent-buffer)
  112. eshell-parent-buffer
  113. (buffer-live-p eshell-parent-buffer)
  114. eshell-parent-buffer)))
  115. (if buf
  116. (switch-to-buffer buf))))
  117. (kill-buffer proc-buf))))
  118. ;; jww (1999-09-17): The code below will allow Eshell to send input
  119. ;; characters directly to the currently running interactive process.
  120. ;; However, since this would introduce other problems that would need
  121. ;; solutions, I'm going to let it wait until after 2.1.
  122. ; (defvar eshell-term-raw-map nil
  123. ; "Keyboard map for sending characters directly to the inferior process.")
  124. ; (defvar eshell-term-escape-char nil
  125. ; "Escape character for char-sub-mode of term mode.
  126. ; Do not change it directly; use term-set-escape-char instead.")
  127. ; (defvar eshell-term-raw-escape-map nil)
  128. ; (defun eshell-term-send-raw-string (chars)
  129. ; (goto-char eshell-last-output-end)
  130. ; (process-send-string (eshell-interactive-process) chars))
  131. ; (defun eshell-term-send-raw ()
  132. ; "Send the last character typed through the terminal-emulator
  133. ; without any interpretation."
  134. ; (interactive)
  135. ; ;; Convert `return' to C-m, etc.
  136. ; (if (and (symbolp last-input-event)
  137. ; (get last-input-event 'ascii-character))
  138. ; (setq last-input-event (get last-input-event 'ascii-character)))
  139. ; (eshell-term-send-raw-string (make-string 1 last-input-event)))
  140. ; (defun eshell-term-send-raw-meta ()
  141. ; (interactive)
  142. ; (if (symbolp last-input-event)
  143. ; ;; Convert `return' to C-m, etc.
  144. ; (let ((tmp (get last-input-event 'event-symbol-elements)))
  145. ; (if tmp
  146. ; (setq last-input-event (car tmp)))
  147. ; (if (symbolp last-input-event)
  148. ; (progn
  149. ; (setq tmp (get last-input-event 'ascii-character))
  150. ; (if tmp (setq last-input-event tmp))))))
  151. ; (eshell-term-send-raw-string (if (and (numberp last-input-event)
  152. ; (> last-input-event 127)
  153. ; (< last-input-event 256))
  154. ; (make-string 1 last-input-event)
  155. ; (format "\e%c" last-input-event))))
  156. ; (defun eshell-term-mouse-paste (click arg)
  157. ; "Insert the last stretch of killed text at the position clicked on."
  158. ; (interactive "e\nP")
  159. ; (if (boundp 'xemacs-logo)
  160. ; (eshell-term-send-raw-string
  161. ; (or (condition-case () (x-get-selection) (error ()))
  162. ; (error "No selection available")))
  163. ; ;; Give temporary modes such as isearch a chance to turn off.
  164. ; (run-hooks 'mouse-leave-buffer-hook)
  165. ; (setq this-command 'yank)
  166. ; (eshell-term-send-raw-string
  167. ; (current-kill (cond ((listp arg) 0)
  168. ; ((eq arg '-) -1)
  169. ; (t (1- arg)))))))
  170. ; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
  171. ; ;; For my configuration it's definitely better \eOA but YMMV. -mm
  172. ; ;; For example: vi works with \eOA while elm wants \e[A ...
  173. ; (defun eshell-term-send-up () (interactive) (eshell-term-send-raw-string "\eOA"))
  174. ; (defun eshell-term-send-down () (interactive) (eshell-term-send-raw-string "\eOB"))
  175. ; (defun eshell-term-send-right () (interactive) (eshell-term-send-raw-string "\eOC"))
  176. ; (defun eshell-term-send-left () (interactive) (eshell-term-send-raw-string "\eOD"))
  177. ; (defun eshell-term-send-home () (interactive) (eshell-term-send-raw-string "\e[1~"))
  178. ; (defun eshell-term-send-end () (interactive) (eshell-term-send-raw-string "\e[4~"))
  179. ; (defun eshell-term-send-prior () (interactive) (eshell-term-send-raw-string "\e[5~"))
  180. ; (defun eshell-term-send-next () (interactive) (eshell-term-send-raw-string "\e[6~"))
  181. ; (defun eshell-term-send-del () (interactive) (eshell-term-send-raw-string "\C-?"))
  182. ; (defun eshell-term-send-backspace () (interactive) (eshell-term-send-raw-string "\C-H"))
  183. ; (defun eshell-term-set-escape-char (c)
  184. ; "Change term-escape-char and keymaps that depend on it."
  185. ; (if eshell-term-escape-char
  186. ; (define-key eshell-term-raw-map eshell-term-escape-char 'eshell-term-send-raw))
  187. ; (setq c (make-string 1 c))
  188. ; (define-key eshell-term-raw-map c eshell-term-raw-escape-map)
  189. ; ;; Define standard bindings in eshell-term-raw-escape-map
  190. ; (define-key eshell-term-raw-escape-map "\C-x"
  191. ; (lookup-key (current-global-map) "\C-x"))
  192. ; (define-key eshell-term-raw-escape-map "\C-v"
  193. ; (lookup-key (current-global-map) "\C-v"))
  194. ; (define-key eshell-term-raw-escape-map "\C-u"
  195. ; (lookup-key (current-global-map) "\C-u"))
  196. ; (define-key eshell-term-raw-escape-map c 'eshell-term-send-raw))
  197. ; (defun eshell-term-char-mode ()
  198. ; "Switch to char (\"raw\") sub-mode of term mode.
  199. ; Each character you type is sent directly to the inferior without
  200. ; intervention from Emacs, except for the escape character (usually C-c)."
  201. ; (interactive)
  202. ; (if (not eshell-term-raw-map)
  203. ; (let* ((map (make-keymap))
  204. ; (esc-map (make-keymap))
  205. ; (i 0))
  206. ; (while (< i 128)
  207. ; (define-key map (make-string 1 i) 'eshell-term-send-raw)
  208. ; (define-key esc-map (make-string 1 i) 'eshell-term-send-raw-meta)
  209. ; (setq i (1+ i)))
  210. ; (define-key map "\e" esc-map)
  211. ; (setq eshell-term-raw-map map)
  212. ; (setq eshell-term-raw-escape-map
  213. ; (copy-keymap (lookup-key (current-global-map) "\C-x")))
  214. ; (if (boundp 'xemacs-logo)
  215. ; (define-key eshell-term-raw-map [button2] 'eshell-term-mouse-paste)
  216. ; (define-key eshell-term-raw-map [mouse-2] 'eshell-term-mouse-paste))
  217. ; (define-key eshell-term-raw-map [up] 'eshell-term-send-up)
  218. ; (define-key eshell-term-raw-map [down] 'eshell-term-send-down)
  219. ; (define-key eshell-term-raw-map [right] 'eshell-term-send-right)
  220. ; (define-key eshell-term-raw-map [left] 'eshell-term-send-left)
  221. ; (define-key eshell-term-raw-map [delete] 'eshell-term-send-del)
  222. ; (define-key eshell-term-raw-map [backspace] 'eshell-term-send-backspace)
  223. ; (define-key eshell-term-raw-map [home] 'eshell-term-send-home)
  224. ; (define-key eshell-term-raw-map [end] 'eshell-term-send-end)
  225. ; (define-key eshell-term-raw-map [prior] 'eshell-term-send-prior)
  226. ; (define-key eshell-term-raw-map [next] 'eshell-term-send-next)
  227. ; (eshell-term-set-escape-char ?\C-c))))
  228. ; (defun eshell-term-line-mode ()
  229. ; "Switch to line (\"cooked\") sub-mode of eshell-term mode."
  230. ; (use-local-map term-old-mode-map))
  231. (provide 'em-term)
  232. ;; Local Variables:
  233. ;; generated-autoload-file: "esh-groups.el"
  234. ;; End:
  235. ;;; em-term.el ends here