root/trunk/lisp/eshell/eshell.el

Revision 4220, 20.9 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

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