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