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