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