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