eshell.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. ;;; eshell.el --- the Emacs command shell
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; Version: 2.4.2
  5. ;; Keywords: processes
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;;_* What does Eshell offer you?
  19. ;;
  20. ;; Despite the sheer fact that running an Emacs shell can be fun, here
  21. ;; are a few of the unique features offered by Eshell:
  22. ;;
  23. ;; @ Integration with the Emacs Lisp programming environment
  24. ;;
  25. ;; @ A high degree of configurability
  26. ;;
  27. ;; @ The ability to have the same shell on every system Emacs has been
  28. ;; ported to. Since Eshell imposes no external requirements, and
  29. ;; relies upon only the Lisp functions exposed by Emacs, it is quite
  30. ;; operating system independent. Several of the common UNIX
  31. ;; commands, such as ls, mv, rm, ln, etc., have been implemented in
  32. ;; Lisp in order to provide a more consistent work environment.
  33. ;;
  34. ;; For those who might be using an older version of Eshell, version
  35. ;; 2.1 represents an entirely new, module-based architecture. It
  36. ;; supports most of the features offered by modern shells. Here is a
  37. ;; brief list of some of its more visible features:
  38. ;;
  39. ;; @ Command argument completion (tcsh, zsh)
  40. ;; @ Input history management (bash)
  41. ;; @ Intelligent output scrolling
  42. ;; @ Pseudo-devices (such as "/dev/clip" for copying to the clipboard)
  43. ;; @ Extended globbing (zsh)
  44. ;; @ Argument and globbing predication (zsh)
  45. ;; @ I/O redirection to buffers, files, symbols, processes, etc.
  46. ;; @ Many niceties otherwise seen only in 4DOS
  47. ;; @ Alias functions, both Lisp and Eshell-syntax
  48. ;; @ Piping, sequenced commands, background jobs, etc...
  49. ;;
  50. ;;;_* How to begin
  51. ;;
  52. ;; To start using Eshell, simply type `M-x eshell'.
  53. ;;
  54. ;;;_* Philosophy
  55. ;;
  56. ;; A shell is a layer which metaphorically surrounds the kernel, or
  57. ;; heart of an operating system. This kernel can be seen as an engine
  58. ;; of pure functionality, waiting to serve, while the user programs
  59. ;; take advantage of that functionality to accomplish their purpose.
  60. ;;
  61. ;; The shell's role is to make that functionality accessible to the
  62. ;; user in an unformed state. Very roughly, it associates kernel
  63. ;; functionality with textual commands, allowing the user to interact
  64. ;; with the operating system via linguistic constructs. Process
  65. ;; invocation is perhaps the most significant form this takes, using
  66. ;; the kernel's `fork' and `exec' functions.
  67. ;;
  68. ;; Other programs also interact with the functionality of the kernel,
  69. ;; but these user applications typically offer a specific range of
  70. ;; functionality, and thus are not classed as "shells" proper.
  71. ;; (What they lose in quiddity, they gain in rigidity).
  72. ;;
  73. ;; Emacs is also a user application, but it does make the
  74. ;; functionality of the kernel accessible through an interpreted
  75. ;; language -- namely, Lisp. For that reason, there is little
  76. ;; preventing Emacs from serving the same role as a modern shell. It
  77. ;; too can manipulate the kernel in an unpredetermined way to cause
  78. ;; system changes. All it's missing is the shell-ish linguistic
  79. ;; model.
  80. ;;
  81. ;; Enter Eshell. Eshell translates "shell-like" syntax into Lisp
  82. ;; in order to exercise the kernel in the same manner as typical
  83. ;; system shells. There is a fundamental difference here, however,
  84. ;; although it may seem subtle at first...
  85. ;;
  86. ;; Shells like csh and Bourne shell were written several decades ago,
  87. ;; in different times, under more restrictive circumstances. This
  88. ;; confined perspective shows itself in the paradigm used by nearly
  89. ;; all command-line shells since. They are linear in conception, byte
  90. ;; stream-based, sequential, and confined to movement within a single
  91. ;; host machine.
  92. ;;
  93. ;; Emacs, on the other hand, is more than just a limited translator
  94. ;; that can invoke subprocesses and redirect file handles. It also
  95. ;; manages character buffers, windowing frames, network connections,
  96. ;; registers, bookmarks, processes, etc. In other words, it's a very
  97. ;; multi-dimensional environment, within which eshell emulates a highly
  98. ;; linear methodology.
  99. ;;
  100. ;; Taking a moment, let's look at how this could affect the future of
  101. ;; a shell allowed to develop in such a wider field of play:
  102. ;;
  103. ;; @ There is no reason why directory movement should be linear, and
  104. ;; confined to a single file-system. Emacs, through w3 and ange-ftp,
  105. ;; has access to the entire Web. Why not allow a user to cd to
  106. ;; multiple directories simultaneously, for example? It might make
  107. ;; some tasks easier, such as diff'ing files separated by very long
  108. ;; pathnames.
  109. ;;
  110. ;; @ Data sources are available from anywhere Emacs can derive
  111. ;; information from: not just from files or the output of other
  112. ;; processes.
  113. ;;
  114. ;; @ Multiple shell invocations all share the same environment -- even
  115. ;; the same process list! It would be possible to have "process
  116. ;; views", so that one buffer is watching standard output, another
  117. ;; standard error, and another the result of standard output grep'd
  118. ;; through a regular expression...
  119. ;;
  120. ;; @ It is not necessary to "leave" the shell, losing all input and
  121. ;; output history, environment variables, directory stack, etc.
  122. ;; Emacs could save the contents of your eshell environment, and
  123. ;; restore all of it (or at least as much as possible) each time you
  124. ;; restart. This could occur automatically, without requiring
  125. ;; complex initialization scripts.
  126. ;;
  127. ;; @ Typos occur all of the time; many of them are repeats of common
  128. ;; errors, such as 'dri' for `dir'. Since executing non-existent
  129. ;; programs is rarely the intention of the user, eshell could prompt
  130. ;; for the replacement string, and then record that in a database of
  131. ;; known misspellings. (Note: The typo at the beginning of this
  132. ;; paragraph wasn't discovered until two months after I wrote the
  133. ;; text; it was not intentional).
  134. ;;
  135. ;; @ Emacs's register and bookmarking facilities can be used for
  136. ;; remembering where you've been, and what you've seen -- to varying
  137. ;; levels of persistence. They could perhaps even be tied to
  138. ;; specific "moments" during eshell execution, which would include
  139. ;; the environment at that time, as well as other variables.
  140. ;; Although this would require functionality orthogonal to Emacs's
  141. ;; own bookmarking facilities, the interface used could be made to
  142. ;; operate very similarly.
  143. ;;
  144. ;; This presents a brief idea of what the fuller dimensionality of an
  145. ;; Emacs shell could offer. It's not just the language of a shell
  146. ;; that determines how it's used, but also the Weltanschauung
  147. ;; underlying its design -- and which is felt behind even the smallest
  148. ;; feature. I would hope the freedom provided by using Emacs as a
  149. ;; parent environment will invite rich ideas from others. It
  150. ;; certainly feels as though all I've done so far is to tie down the
  151. ;; horse, so to speak, so that he will run at a man's pace.
  152. ;;
  153. ;;;_* Influences
  154. ;;
  155. ;; The author of Eshell has been a long-time user of the following
  156. ;; shells, all of which contributed to Eshell's design:
  157. ;;
  158. ;; @ rc
  159. ;; @ bash
  160. ;; @ zsh
  161. ;; @ sh
  162. ;; @ 4nt
  163. ;; @ csh
  164. ;;;_* Speeding up load time
  165. ;;
  166. ;; If you find that Eshell loads too slowly, there is something you
  167. ;; can do to speed it up.
  168. ;;
  169. ;; Create a file, named /tmp/elc, containing this filelist:
  170. ;;
  171. ;; esh-util.elc
  172. ;; eshell.elc
  173. ;; esh-module.elc
  174. ;; esh-var.elc
  175. ;; esh-proc.elc
  176. ;; esh-arg.elc
  177. ;; esh-io.elc
  178. ;; esh-ext.elc
  179. ;; esh-cmd.elc
  180. ;; esh-mode.elc
  181. ;; esh-opt.elc
  182. ;; em-alias.elc
  183. ;; em-banner.elc
  184. ;; em-basic.elc
  185. ;; em-cmpl.elc
  186. ;; em-dirs.elc
  187. ;; em-pred.elc
  188. ;; em-glob.elc
  189. ;; em-hist.elc
  190. ;; em-ls.elc
  191. ;; em-prompt.elc
  192. ;; em-rebind.elc
  193. ;; em-script.elc
  194. ;; em-smart.elc
  195. ;; em-term.elc
  196. ;; em-unix.elc
  197. ;; em-xtra.elc
  198. ;;
  199. ;; The order is very important. Remove from the filelist any features
  200. ;; you don't use. These all begin with "em-". If you don't use
  201. ;; Eshell's key rebinding module, you can remove "em-rebind.elc" from
  202. ;; the filelist. The modules you are currently using are listed in
  203. ;; `eshell-modules-list'.
  204. ;;
  205. ;; Now, concatenating all of the above mentioned .elc files, in that
  206. ;; order, to another file. Here is how to do this on UNIX:
  207. ;;
  208. ;; cat `cat /tmp/elc` > tmp.elc ; mv tmp.elc eshell.elc
  209. ;;
  210. ;; Now your eshell.elc file contains all of the .elc files that make
  211. ;; up Eshell, in the right load order. When you next load Eshell, it
  212. ;; will only have to read in this one file, which will greatly speed
  213. ;; things up.
  214. (eval-when-compile
  215. (require 'cl)
  216. (require 'esh-util))
  217. (require 'esh-util)
  218. (require 'esh-mode)
  219. (defgroup eshell nil
  220. "A command shell implemented entirely in Emacs Lisp.
  221. It invokes no external processes beyond those requested by the
  222. user, and is intended to be a functional replacement for command
  223. shells such as bash, zsh, rc, 4dos."
  224. :tag "The Emacs shell"
  225. :link '(info-link "(eshell)Top")
  226. :version "21.1"
  227. :group 'applications)
  228. ;; This is hack to force make-autoload to put the whole definition
  229. ;; into the autoload file (see esh-module.el).
  230. (defalias 'eshell-defgroup 'defgroup)
  231. ;;;_* User Options
  232. ;;
  233. ;; The following user options modify the behavior of Eshell overall.
  234. (defvar eshell-buffer-name)
  235. (defsubst eshell-add-to-window-buffer-names ()
  236. "Add `eshell-buffer-name' to `same-window-buffer-names'."
  237. (add-to-list 'same-window-buffer-names eshell-buffer-name))
  238. (defsubst eshell-remove-from-window-buffer-names ()
  239. "Remove `eshell-buffer-name' from `same-window-buffer-names'."
  240. (setq same-window-buffer-names
  241. (delete eshell-buffer-name same-window-buffer-names)))
  242. (defcustom eshell-load-hook nil
  243. "A hook run once Eshell has been loaded."
  244. :type 'hook
  245. :group 'eshell)
  246. (defcustom eshell-unload-hook
  247. '(eshell-remove-from-window-buffer-names
  248. eshell-unload-all-modules)
  249. "A hook run when Eshell is unloaded from memory."
  250. :type 'hook
  251. :group 'eshell)
  252. (defcustom eshell-buffer-name "*eshell*"
  253. "The basename used for Eshell buffers."
  254. :set (lambda (symbol value)
  255. ;; remove the old value of `eshell-buffer-name', if present
  256. (if (boundp 'eshell-buffer-name)
  257. (eshell-remove-from-window-buffer-names))
  258. (set symbol value)
  259. ;; add the new value
  260. (eshell-add-to-window-buffer-names)
  261. value)
  262. :type 'string
  263. :group 'eshell)
  264. (defcustom eshell-directory-name
  265. (locate-user-emacs-file "eshell/" ".eshell/")
  266. "The directory where Eshell control files should be kept."
  267. :type 'directory
  268. :group 'eshell)
  269. ;;;_* Running Eshell
  270. ;;
  271. ;; There are only three commands used to invoke Eshell. The first two
  272. ;; are intended for interactive use, while the third is meant for
  273. ;; programmers. They are:
  274. ;;;###autoload
  275. (defun eshell (&optional arg)
  276. "Create an interactive Eshell buffer.
  277. The buffer used for Eshell sessions is determined by the value of
  278. `eshell-buffer-name'. If there is already an Eshell session active in
  279. that buffer, Emacs will simply switch to it. Otherwise, a new session
  280. will begin. A numeric prefix arg (as in `C-u 42 M-x eshell RET')
  281. switches to the session with that number, creating it if necessary. A
  282. nonnumeric prefix arg means to create a new session. Returns the
  283. buffer selected (or created)."
  284. (interactive "P")
  285. (assert eshell-buffer-name)
  286. (let ((buf (cond ((numberp arg)
  287. (get-buffer-create (format "%s<%d>"
  288. eshell-buffer-name
  289. arg)))
  290. (arg
  291. (generate-new-buffer eshell-buffer-name))
  292. (t
  293. (get-buffer-create eshell-buffer-name)))))
  294. ;; Simply calling `pop-to-buffer' will not mimic the way that
  295. ;; shell-mode buffers appear, since they always reuse the same
  296. ;; window that that command was invoked from. To achieve this,
  297. ;; it's necessary to add `eshell-buffer-name' to the variable
  298. ;; `same-window-buffer-names', which is done when Eshell is loaded
  299. (assert (and buf (buffer-live-p buf)))
  300. (pop-to-buffer buf)
  301. (unless (eq major-mode 'eshell-mode)
  302. (eshell-mode))
  303. buf))
  304. (defun eshell-return-exits-minibuffer ()
  305. (define-key eshell-mode-map [(control ?g)] 'abort-recursive-edit)
  306. (define-key eshell-mode-map [return] 'exit-minibuffer)
  307. (define-key eshell-mode-map [(control ?m)] 'exit-minibuffer)
  308. (define-key eshell-mode-map [(control ?j)] 'exit-minibuffer)
  309. (define-key eshell-mode-map [(meta return)] 'exit-minibuffer)
  310. (define-key eshell-mode-map [(meta control ?m)] 'exit-minibuffer))
  311. (defvar eshell-non-interactive-p nil
  312. "A variable which is non-nil when Eshell is not running interactively.
  313. Modules should use this variable so that they don't clutter
  314. non-interactive sessions, such as when using `eshell-command'.")
  315. ;;;###autoload
  316. (defun eshell-command (&optional command arg)
  317. "Execute the Eshell command string COMMAND.
  318. With prefix ARG, insert output into the current buffer at point."
  319. (interactive)
  320. (require 'esh-cmd)
  321. (unless arg
  322. (setq arg current-prefix-arg))
  323. (let ((eshell-non-interactive-p t))
  324. ;; Enable `eshell-mode' only in this minibuffer.
  325. (minibuffer-with-setup-hook #'(lambda ()
  326. (eshell-mode)
  327. (eshell-return-exits-minibuffer))
  328. (unless command
  329. (setq command (read-from-minibuffer "Emacs shell command: "))
  330. (eshell-add-input-to-history command))))
  331. (unless command
  332. (error "No command specified!"))
  333. ;; redirection into the current buffer is achieved by adding an
  334. ;; output redirection to the end of the command, of the form
  335. ;; 'COMMAND >>> #<buffer BUFFER>'. This will not interfere with
  336. ;; other redirections, since multiple redirections merely cause the
  337. ;; output to be copied to multiple target locations
  338. (if arg
  339. (setq command
  340. (concat command
  341. (format " >>> #<buffer %s>"
  342. (buffer-name (current-buffer))))))
  343. (save-excursion
  344. (let ((buf (set-buffer (generate-new-buffer " *eshell cmd*")))
  345. (eshell-non-interactive-p t))
  346. (eshell-mode)
  347. (let* ((proc (eshell-eval-command
  348. (list 'eshell-commands
  349. (eshell-parse-command command))))
  350. intr
  351. (bufname (if (and proc (listp proc))
  352. "*EShell Async Command Output*"
  353. (setq intr t)
  354. "*EShell Command Output*")))
  355. (if (buffer-live-p (get-buffer bufname))
  356. (kill-buffer bufname))
  357. (rename-buffer bufname)
  358. ;; things get a little coarse here, since the desire is to
  359. ;; make the output as attractive as possible, with no
  360. ;; extraneous newlines
  361. (when intr
  362. (if (eshell-interactive-process)
  363. (eshell-wait-for-process (eshell-interactive-process)))
  364. (assert (not (eshell-interactive-process)))
  365. (goto-char (point-max))
  366. (while (and (bolp) (not (bobp)))
  367. (delete-char -1)))
  368. (assert (and buf (buffer-live-p buf)))
  369. (unless arg
  370. (let ((len (if (not intr) 2
  371. (count-lines (point-min) (point-max)))))
  372. (cond
  373. ((= len 0)
  374. (message "(There was no command output)")
  375. (kill-buffer buf))
  376. ((= len 1)
  377. (message "%s" (buffer-string))
  378. (kill-buffer buf))
  379. (t
  380. (save-selected-window
  381. (select-window (display-buffer buf))
  382. (goto-char (point-min))
  383. ;; cause the output buffer to take up as little screen
  384. ;; real-estate as possible, if temp buffer resizing is
  385. ;; enabled
  386. (and intr temp-buffer-resize-mode
  387. (resize-temp-buffer-window)))))))))))
  388. ;;;###autoload
  389. (defun eshell-command-result (command &optional status-var)
  390. "Execute the given Eshell COMMAND, and return the result.
  391. The result might be any Lisp object.
  392. If STATUS-VAR is a symbol, it will be set to the exit status of the
  393. command. This is the only way to determine whether the value returned
  394. corresponding to a successful execution."
  395. ;; a null command produces a null, successful result
  396. (if (not command)
  397. (ignore
  398. (if (and status-var (symbolp status-var))
  399. (set status-var 0)))
  400. (with-temp-buffer
  401. (let ((eshell-non-interactive-p t))
  402. (eshell-mode)
  403. (let ((result (eshell-do-eval
  404. (list 'eshell-commands
  405. (list 'eshell-command-to-value
  406. (eshell-parse-command command))) t)))
  407. (assert (eq (car result) 'quote))
  408. (if (and status-var (symbolp status-var))
  409. (set status-var eshell-last-command-status))
  410. (cadr result))))))
  411. ;;;_* Reporting bugs
  412. ;;
  413. ;; If you do encounter a bug, on any system, please report
  414. ;; it -- in addition to any particular oddities in your configuration
  415. ;; -- so that the problem may be corrected for the benefit of others.
  416. ;;;###autoload
  417. (define-obsolete-function-alias 'eshell-report-bug 'report-emacs-bug "23.1")
  418. ;;; Code:
  419. (defun eshell-unload-all-modules ()
  420. "Unload all modules that were loaded by Eshell, if possible.
  421. If the user has require'd in any of the modules, or customized a
  422. variable with a :require tag (such as `eshell-prefer-to-shell'), it
  423. will be impossible to unload Eshell completely without restarting
  424. Emacs."
  425. ;; if the user set `eshell-prefer-to-shell' to t, but never loaded
  426. ;; Eshell, then `eshell-subgroups' will be unbound
  427. (when (fboundp 'eshell-subgroups)
  428. (dolist (module (eshell-subgroups 'eshell))
  429. ;; this really only unloads as many modules as possible,
  430. ;; since other `require' references (such as by customizing
  431. ;; `eshell-prefer-to-shell' to a non-nil value) might make it
  432. ;; impossible to unload Eshell completely
  433. (if (featurep module)
  434. (ignore-errors
  435. (message "Unloading %s..." (symbol-name module))
  436. (unload-feature module)
  437. (message "Unloading %s...done" (symbol-name module)))))
  438. (message "Unloading eshell...done")))
  439. (run-hooks 'eshell-load-hook)
  440. (provide 'eshell)
  441. ;;; eshell.el ends here