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