shell.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. ;; Run subshell under Emacs
  2. ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (provide 'shell)
  18. ;; For old Emacs versions.
  19. (or (fboundp 'process-send-string)
  20. (progn
  21. (fset 'process-send-string 'send-string)
  22. (fset 'process-send-region 'send-region)))
  23. (defvar last-input-start nil
  24. "In a shell-mode buffer, marker for start of last unit of input.")
  25. (defvar last-input-end nil
  26. "In a shell-mode buffer, marker for start of last unit of input.")
  27. (defvar shell-mode-map nil)
  28. (defvar shell-directory-stack nil
  29. "List of directories saved by pushd in this buffer's shell.")
  30. (defvar shell-popd-regexp "popd"
  31. "*Regexp to match subshell commands equivalent to popd.")
  32. (defvar shell-pushd-regexp "pushd"
  33. "*Regexp to match subshell commands equivalent to pushd.")
  34. (defvar shell-cd-regexp "cd"
  35. "*Regexp to match subshell commands equivalent to cd.")
  36. (defvar explicit-shell-file-name nil
  37. "*If non-nil, is file name to use for explicitly requested inferior shell.")
  38. ;In loaddefs.el now.
  39. ;(defconst shell-prompt-pattern
  40. ; "^[^#$%>]*[#$%>] *"
  41. ; "*Regexp used by Newline command to match subshell prompts.
  42. ;Anything from beginning of line up to the end of what this pattern matches
  43. ;is deemed to be prompt, and is not reexecuted.")
  44. (defun shell-mode ()
  45. "Major mode for interacting with an inferior shell.
  46. Shell name is same as buffer name, sans the asterisks.
  47. Return at end of buffer sends line as input.
  48. Return not at end copies rest of line to end and sends it.
  49. The following commands imitate the usual Unix interrupt and
  50. editing control characters:
  51. \\{shell-mode-map}
  52. Entry to this mode calls the value of shell-mode-hook with no args,
  53. if that value is non-nil.
  54. cd, pushd and popd commands given to the shell are watched
  55. by Emacs to keep this buffer's default directory
  56. the same as the shell's working directory.
  57. Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp
  58. are used to match these command names.
  59. You can send text to the shell (or its subjobs) from other buffers
  60. using the commands process-send-region, process-send-string
  61. and lisp-send-defun."
  62. (interactive)
  63. (kill-all-local-variables)
  64. (setq major-mode 'shell-mode)
  65. (setq mode-name "Shell")
  66. (setq mode-line-process '(": %s"))
  67. (use-local-map shell-mode-map)
  68. (make-local-variable 'shell-directory-stack)
  69. (setq shell-directory-stack nil)
  70. (make-local-variable 'last-input-start)
  71. (setq last-input-start (make-marker))
  72. (make-local-variable 'last-input-end)
  73. (setq last-input-end (make-marker))
  74. (run-hooks 'shell-mode-hook))
  75. (if shell-mode-map
  76. nil
  77. (setq shell-mode-map (make-sparse-keymap))
  78. (define-key shell-mode-map "\C-m" 'shell-send-input)
  79. (define-key shell-mode-map "\C-c\C-d" 'shell-send-eof)
  80. (define-key shell-mode-map "\C-c\C-u" 'kill-shell-input)
  81. (define-key shell-mode-map "\C-c\C-w" 'backward-kill-word)
  82. (define-key shell-mode-map "\C-c\C-c" 'interrupt-shell-subjob)
  83. (define-key shell-mode-map "\C-c\C-z" 'stop-shell-subjob)
  84. (define-key shell-mode-map "\C-c\C-\\" 'quit-shell-subjob)
  85. (define-key shell-mode-map "\C-c\C-o" 'kill-output-from-shell)
  86. (define-key shell-mode-map "\C-c\C-r" 'show-output-from-shell)
  87. (define-key shell-mode-map "\C-c\C-y" 'copy-last-shell-input))
  88. (defvar explicit-csh-args
  89. (if (eq system-type 'hpux)
  90. ;; -T persuades HP's csh not to think it is smarter
  91. ;; than us about what terminal modes to use.
  92. '("-i" "-T")
  93. '("-i"))
  94. "Args passed to inferior shell by M-x shell, if the shell is csh.
  95. Value is a list of strings, which may be nil.")
  96. (defun shell ()
  97. "Run an inferior shell, with I/O through buffer *shell*.
  98. If buffer exists but shell process is not running, make new shell.
  99. Program used comes from variable explicit-shell-file-name,
  100. or (if that is nil) from the ESHELL environment variable,
  101. or else from SHELL if there is no ESHELL.
  102. If a file ~/.emacs_SHELLNAME exists, it is given as initial input
  103. (Note that this may lose due to a timing error if the shell
  104. discards input when it starts up.)
  105. The buffer is put in shell-mode, giving commands for sending input
  106. and controlling the subjobs of the shell. See shell-mode.
  107. See also variable shell-prompt-pattern.
  108. The shell file name (sans directories) is used to make a symbol name
  109. such as `explicit-csh-arguments'. If that symbol is a variable,
  110. its value is used as a list of arguments when invoking the shell.
  111. Otherwise, one argument `-i' is passed to the shell.
  112. Note that many people's .cshrc files unconditionally clear the prompt.
  113. If yours does, you will probably want to change it."
  114. (interactive)
  115. (let* ((prog (or explicit-shell-file-name
  116. (getenv "ESHELL")
  117. (getenv "SHELL")
  118. "/bin/sh"))
  119. (name (file-name-nondirectory prog)))
  120. (switch-to-buffer
  121. (apply 'make-shell "shell" prog
  122. (if (file-exists-p (concat "~/.emacs_" name))
  123. (concat "~/.emacs_" name))
  124. (let ((symbol (intern-soft (concat "explicit-" name "-args"))))
  125. (if (and symbol (boundp symbol))
  126. (symbol-value symbol)
  127. '("-i")))))))
  128. (defun make-shell (name program &optional startfile &rest switches)
  129. (let ((buffer (get-buffer-create (concat "*" name "*")))
  130. proc status size)
  131. (setq proc (get-buffer-process buffer))
  132. (if proc (setq status (process-status proc)))
  133. (save-excursion
  134. (set-buffer buffer)
  135. ;; (setq size (buffer-size))
  136. (if (memq status '(run stop))
  137. nil
  138. (if proc (delete-process proc))
  139. (setq proc (apply 'start-process name buffer
  140. (concat exec-directory "env")
  141. (format "TERMCAP=emacs:co#%d:tc=unknown:"
  142. (screen-width))
  143. "TERM=emacs"
  144. "EMACS=t"
  145. "-" program switches))
  146. (cond (startfile
  147. ;;This is guaranteed to wait long enough
  148. ;;but has bad results if the shell does not prompt at all
  149. ;; (while (= size (buffer-size))
  150. ;; (sleep-for 1))
  151. ;;I hope 1 second is enough!
  152. (sleep-for 1)
  153. (goto-char (point-max))
  154. (insert-file-contents startfile)
  155. (setq startfile (buffer-substring (point) (point-max)))
  156. (delete-region (point) (point-max))
  157. (process-send-string proc startfile)))
  158. (setq name (process-name proc)))
  159. (goto-char (point-max))
  160. (set-marker (process-mark proc) (point))
  161. (shell-mode))
  162. buffer))
  163. (defvar shell-set-directory-error-hook 'ignore
  164. "Function called with no arguments when shell-send-input
  165. recognizes a change-directory command but gets an error
  166. trying to change Emacs's default directory.")
  167. (defun shell-send-input ()
  168. "Send input to subshell.
  169. At end of buffer, sends all text after last output
  170. as input to the subshell, including a newline inserted at the end.
  171. Not at end, copies current line to the end of the buffer and sends it,
  172. after first attempting to discard any prompt at the beginning of the line
  173. by matching the regexp that is the value of shell-prompt-pattern if possible.
  174. This regexp should start with \"^\"."
  175. (interactive)
  176. (end-of-line)
  177. (if (eobp)
  178. (progn
  179. (move-marker last-input-start
  180. (process-mark (get-buffer-process (current-buffer))))
  181. (insert ?\n)
  182. (move-marker last-input-end (point)))
  183. (beginning-of-line)
  184. (re-search-forward shell-prompt-pattern nil t)
  185. (let ((copy (buffer-substring (point)
  186. (progn (forward-line 1) (point)))))
  187. (goto-char (point-max))
  188. (move-marker last-input-start (point))
  189. (insert copy)
  190. (move-marker last-input-end (point))))
  191. ;; Even if we get an error trying to hack the working directory,
  192. ;; still send the input to the subshell.
  193. (condition-case ()
  194. (save-excursion
  195. (goto-char last-input-start)
  196. (shell-set-directory))
  197. (error (funcall shell-set-directory-error-hook)))
  198. (let ((process (get-buffer-process (current-buffer))))
  199. (process-send-region process last-input-start last-input-end)
  200. (set-marker (process-mark process) (point))))
  201. ;;; If this code changes (shell-send-input and shell-set-directory),
  202. ;;; the customization tutorial in
  203. ;;; info/customizing-tutorial must also change, since it explains this
  204. ;;; code. Please let marick@gswd-vms.arpa know of any changes you
  205. ;;; make.
  206. (defun shell-set-directory ()
  207. (cond ((and (looking-at shell-popd-regexp)
  208. (memq (char-after (match-end 0)) '(?\; ?\n)))
  209. (if shell-directory-stack
  210. (progn
  211. (cd (car shell-directory-stack))
  212. (setq shell-directory-stack (cdr shell-directory-stack)))))
  213. ((looking-at shell-pushd-regexp)
  214. (cond ((memq (char-after (match-end 0)) '(?\; ?\n))
  215. (if shell-directory-stack
  216. (let ((old default-directory))
  217. (cd (car shell-directory-stack))
  218. (setq shell-directory-stack
  219. (cons old (cdr shell-directory-stack))))))
  220. ((memq (char-after (match-end 0)) '(?\ ?\t))
  221. (let (dir)
  222. (skip-chars-forward "^ ")
  223. (skip-chars-forward " \t")
  224. (if (file-directory-p
  225. (setq dir
  226. (expand-file-name
  227. (substitute-in-file-name
  228. (buffer-substring
  229. (point)
  230. (progn
  231. (skip-chars-forward "^\n \t;")
  232. (point)))))))
  233. (progn
  234. (setq shell-directory-stack
  235. (cons default-directory shell-directory-stack))
  236. (cd dir)))))))
  237. ((looking-at shell-cd-regexp)
  238. (cond ((memq (char-after (match-end 0)) '(?\; ?\n))
  239. (cd (getenv "HOME")))
  240. ((memq (char-after (match-end 0)) '(?\ ?\t))
  241. (let (dir)
  242. (forward-char 3)
  243. (skip-chars-forward " \t")
  244. (if (file-directory-p
  245. (setq dir
  246. (expand-file-name
  247. (substitute-in-file-name
  248. (buffer-substring
  249. (point)
  250. (progn
  251. (skip-chars-forward "^\n \t;")
  252. (point)))))))
  253. (cd dir))))))))
  254. (defun shell-send-eof ()
  255. "Send eof to subshell (or to the program running under it)."
  256. (interactive)
  257. (process-send-eof))
  258. (defun kill-output-from-shell ()
  259. "Kill all output from shell since last input."
  260. (interactive)
  261. (goto-char (point-max))
  262. (kill-region last-input-end (point))
  263. (insert "> output flushed ***\n"))
  264. (defun show-output-from-shell ()
  265. "Display start of this batch of shell output at top of window.
  266. Also put cursor there."
  267. (interactive)
  268. (set-window-start (selected-window) last-input-end)
  269. (goto-char last-input-end))
  270. (defun copy-last-shell-input ()
  271. "Copy previous shell input, sans newline, and insert before point."
  272. (interactive)
  273. (insert (buffer-substring last-input-end last-input-start))
  274. (delete-char -1))
  275. (defun interrupt-shell-subjob ()
  276. "Interrupt this shell's current subjob."
  277. (interactive)
  278. (interrupt-process nil t))
  279. (defun kill-shell-subjob ()
  280. "Send kill signal to this shell's current subjob."
  281. (interactive)
  282. (kill-process nil t))
  283. (defun quit-shell-subjob ()
  284. "Send quit signal to this shell's current subjob."
  285. (interactive)
  286. (quit-process nil t))
  287. (defun stop-shell-subjob ()
  288. "Stop this shell's current subjob."
  289. (interactive)
  290. (stop-process nil t))
  291. (defun kill-shell-input ()
  292. "Kill all text since last stuff output by the shell or its subjobs."
  293. (interactive)
  294. (kill-region (process-mark (get-buffer-process (current-buffer)))
  295. (point)))
  296. (defvar inferior-lisp-mode-map nil)
  297. (if inferior-lisp-mode-map
  298. nil
  299. (setq inferior-lisp-mode-map (copy-alist shell-mode-map))
  300. (lisp-mode-commands inferior-lisp-mode-map)
  301. (define-key inferior-lisp-mode-map "\e\C-x" 'lisp-send-defun))
  302. (defvar inferior-lisp-program "lisp"
  303. "*Program name for invoking an inferior Lisp with `run-lisp'.")
  304. (defvar inferior-lisp-load-command "(load \"%s\")\n"
  305. "*Format-string for building a Lisp expression to load a file.
  306. This format string should use %s to substitute a file name
  307. and should result in a Lisp expression that will command the inferior Lisp
  308. to load that file. The default works acceptably on most Lisps.
  309. The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\\n\"
  310. produces cosmetically superior output for this application,
  311. but it works only in Common Lisp.")
  312. (defvar inferior-lisp-prompt "^.*>:? *$"
  313. "*Regexp to recognize prompts from the inferior Lisp.
  314. Default is right for Franz Lisp and kcl.")
  315. (defun inferior-lisp-mode ()
  316. "Major mode for interacting with an inferior Lisp process.
  317. The following commands are available:
  318. \\{inferior-lisp-mode-map}
  319. Entry to this mode calls the value of lisp-mode-hook with no arguments,
  320. if that value is non-nil. Likewise with the value of shell-mode-hook.
  321. lisp-mode-hook is called after shell-mode-hook.
  322. You can send text to the inferior Lisp from other buffers
  323. using the commands process-send-region, process-send-string
  324. and \\[lisp-send-defun].
  325. Commands:
  326. Delete converts tabs to spaces as it moves back.
  327. Tab indents for Lisp; with argument, shifts rest
  328. of expression rigidly with the current line.
  329. Meta-Control-Q does Tab on each line starting within following expression.
  330. Paragraphs are separated only by blank lines. Semicolons start comments.
  331. Return at end of buffer sends line as input.
  332. Return not at end copies rest of line to end and sends it.
  333. C-d at end of buffer sends end-of-file as input.
  334. C-d not at end or with arg deletes or kills characters.
  335. C-u and C-w are kill commands, imitating normal Unix input editing.
  336. C-c interrupts the shell or its current subjob if any.
  337. C-z stops, likewise. C-\\ sends quit signal, likewise.
  338. C-x C-k deletes last batch of output from shell.
  339. C-x C-v puts top of last batch of output at top of window."
  340. (interactive)
  341. (kill-all-local-variables)
  342. (setq major-mode 'inferior-lisp-mode)
  343. (setq mode-name "Inferior Lisp")
  344. (setq mode-line-process '(": %s"))
  345. (lisp-mode-variables)
  346. (use-local-map inferior-lisp-mode-map)
  347. (make-local-variable 'last-input-start)
  348. (setq last-input-start (make-marker))
  349. (make-local-variable 'last-input-end)
  350. (setq last-input-end (make-marker))
  351. (run-hooks 'shell-mode-hook 'lisp-mode-hook))
  352. (defun run-lisp ()
  353. "Run an inferior Lisp process, input and output via buffer *lisp*."
  354. (interactive)
  355. (switch-to-buffer (make-shell "lisp" inferior-lisp-program))
  356. (inferior-lisp-mode))
  357. (defun lisp-send-defun (display-flag)
  358. "Send the current defun to the Lisp process made by M-x run-lisp.
  359. With argument, force redisplay and scrolling of the *lisp* buffer.
  360. Variable `inferior-lisp-load-command' controls formatting of
  361. the `load' form that is set to the Lisp process."
  362. (interactive "P")
  363. (save-excursion
  364. (end-of-defun)
  365. (let ((end (point))
  366. (filename (format "/tmp/emlisp%d" (process-id (get-process "lisp")))))
  367. (beginning-of-defun)
  368. (write-region (point) end filename nil 'nomessage)
  369. (process-send-string "lisp" (format inferior-lisp-load-command filename)))
  370. (if display-flag
  371. (let* ((process (get-process "lisp"))
  372. (buffer (process-buffer process))
  373. (w (or (get-buffer-window buffer) (display-buffer buffer)))
  374. (height (window-height w))
  375. (end))
  376. (save-excursion
  377. (set-buffer buffer)
  378. (setq end (point-max))
  379. (while (progn
  380. (accept-process-output process)
  381. (goto-char (point-max))
  382. (beginning-of-line)
  383. (or (= (point-max) end)
  384. (not (looking-at inferior-lisp-prompt)))))
  385. (setq end (point-max))
  386. (vertical-motion (- 4 height))
  387. (set-window-start w (point)))
  388. (set-window-point w end)))))
  389. (defun lisp-send-defun-and-go ()
  390. "Send the current defun to the inferior Lisp, and switch to *lisp* buffer."
  391. (interactive)
  392. (lisp-send-defun)
  393. (switch-to-buffer "*lisp*"))