Commit | Line | Data |
---|---|---|
ae5e4c48 | 1 | ;;; eshell.el --- the Emacs command shell -*- lexical-binding:t -*- |
affbf647 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1999-2014 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 | ||
d736dd56 | 173 | ;;; Code: |
bb155908 | 174 | |
5477308b | 175 | (eval-when-compile |
f87b1284 | 176 | (require 'cl-lib)) |
c6d48e47 | 177 | (require 'esh-util) |
5477308b | 178 | (require 'esh-mode) |
c6d48e47 GM |
179 | |
180 | (defgroup eshell nil | |
cf20dee0 | 181 | "Command shell implemented entirely in Emacs Lisp. |
b660eb70 CY |
182 | It invokes no external processes beyond those requested by the |
183 | user, and is intended to be a functional replacement for command | |
184 | shells such as bash, zsh, rc, 4dos." | |
c6d48e47 GM |
185 | :link '(info-link "(eshell)Top") |
186 | :version "21.1" | |
187 | :group 'applications) | |
188 | ||
affbf647 GM |
189 | ;;;_* User Options |
190 | ;; | |
191 | ;; The following user options modify the behavior of Eshell overall. | |
5477308b | 192 | (defvar eshell-buffer-name) |
affbf647 | 193 | |
1530c98e | 194 | (defun eshell-add-to-window-buffer-names () |
affbf647 | 195 | "Add `eshell-buffer-name' to `same-window-buffer-names'." |
59f7af81 | 196 | (declare (obsolete nil "24.3")) |
affbf647 GM |
197 | (add-to-list 'same-window-buffer-names eshell-buffer-name)) |
198 | ||
1530c98e | 199 | (defun eshell-remove-from-window-buffer-names () |
affbf647 | 200 | "Remove `eshell-buffer-name' from `same-window-buffer-names'." |
59f7af81 | 201 | (declare (obsolete nil "24.3")) |
affbf647 GM |
202 | (setq same-window-buffer-names |
203 | (delete eshell-buffer-name same-window-buffer-names))) | |
204 | ||
205 | (defcustom eshell-load-hook nil | |
c6d48e47 | 206 | "A hook run once Eshell has been loaded." |
affbf647 GM |
207 | :type 'hook |
208 | :group 'eshell) | |
209 | ||
1530c98e | 210 | (defcustom eshell-unload-hook '(eshell-unload-all-modules) |
c6d48e47 | 211 | "A hook run when Eshell is unloaded from memory." |
affbf647 GM |
212 | :type 'hook |
213 | :group 'eshell) | |
214 | ||
215 | (defcustom eshell-buffer-name "*eshell*" | |
c6d48e47 | 216 | "The basename used for Eshell buffers." |
affbf647 GM |
217 | :type 'string |
218 | :group 'eshell) | |
219 | ||
bfc49a5b LL |
220 | (defcustom eshell-directory-name |
221 | (locate-user-emacs-file "eshell/" ".eshell/") | |
c6d48e47 | 222 | "The directory where Eshell control files should be kept." |
affbf647 GM |
223 | :type 'directory |
224 | :group 'eshell) | |
225 | ||
affbf647 GM |
226 | ;;;_* Running Eshell |
227 | ;; | |
228 | ;; There are only three commands used to invoke Eshell. The first two | |
229 | ;; are intended for interactive use, while the third is meant for | |
230 | ;; programmers. They are: | |
231 | ||
232 | ;;;###autoload | |
233 | (defun eshell (&optional arg) | |
234 | "Create an interactive Eshell buffer. | |
235 | The buffer used for Eshell sessions is determined by the value of | |
236 | `eshell-buffer-name'. If there is already an Eshell session active in | |
237 | that buffer, Emacs will simply switch to it. Otherwise, a new session | |
818001cc KG |
238 | will begin. A numeric prefix arg (as in `C-u 42 M-x eshell RET') |
239 | switches to the session with that number, creating it if necessary. A | |
240 | nonnumeric prefix arg means to create a new session. Returns the | |
241 | buffer selected (or created)." | |
affbf647 | 242 | (interactive "P") |
a464a6c7 | 243 | (cl-assert eshell-buffer-name) |
818001cc KG |
244 | (let ((buf (cond ((numberp arg) |
245 | (get-buffer-create (format "%s<%d>" | |
246 | eshell-buffer-name | |
247 | arg))) | |
248 | (arg | |
249 | (generate-new-buffer eshell-buffer-name)) | |
250 | (t | |
251 | (get-buffer-create eshell-buffer-name))))) | |
a464a6c7 | 252 | (cl-assert (and buf (buffer-live-p buf))) |
1530c98e | 253 | (pop-to-buffer-same-window buf) |
170266d0 | 254 | (unless (derived-mode-p 'eshell-mode) |
c6d48e47 | 255 | (eshell-mode)) |
affbf647 GM |
256 | buf)) |
257 | ||
258 | (defun eshell-return-exits-minibuffer () | |
259 | (define-key eshell-mode-map [(control ?g)] 'abort-recursive-edit) | |
260 | (define-key eshell-mode-map [return] 'exit-minibuffer) | |
261 | (define-key eshell-mode-map [(control ?m)] 'exit-minibuffer) | |
262 | (define-key eshell-mode-map [(control ?j)] 'exit-minibuffer) | |
263 | (define-key eshell-mode-map [(meta return)] 'exit-minibuffer) | |
264 | (define-key eshell-mode-map [(meta control ?m)] 'exit-minibuffer)) | |
265 | ||
9c6a6a5a JW |
266 | (defvar eshell-non-interactive-p nil |
267 | "A variable which is non-nil when Eshell is not running interactively. | |
268 | Modules should use this variable so that they don't clutter | |
269 | non-interactive sessions, such as when using `eshell-command'.") | |
270 | ||
f87b1284 GM |
271 | (declare-function eshell-add-input-to-history "em-hist" (input)) |
272 | ||
affbf647 GM |
273 | ;;;###autoload |
274 | (defun eshell-command (&optional command arg) | |
275 | "Execute the Eshell command string COMMAND. | |
276 | With prefix ARG, insert output into the current buffer at point." | |
277 | (interactive) | |
278 | (require 'esh-cmd) | |
9c6a6a5a JW |
279 | (unless arg |
280 | (setq arg current-prefix-arg)) | |
61a57ef4 | 281 | (let ((eshell-non-interactive-p t)) |
282 | ;; Enable `eshell-mode' only in this minibuffer. | |
283 | (minibuffer-with-setup-hook #'(lambda () | |
284 | (eshell-mode) | |
285 | (eshell-return-exits-minibuffer)) | |
93e616fd | 286 | (unless command |
287 | (setq command (read-from-minibuffer "Emacs shell command: ")) | |
f87b1284 GM |
288 | (if (eshell-using-module 'eshell-hist) |
289 | (eshell-add-input-to-history command))))) | |
affbf647 GM |
290 | (unless command |
291 | (error "No command specified!")) | |
292 | ;; redirection into the current buffer is achieved by adding an | |
293 | ;; output redirection to the end of the command, of the form | |
294 | ;; 'COMMAND >>> #<buffer BUFFER>'. This will not interfere with | |
295 | ;; other redirections, since multiple redirections merely cause the | |
296 | ;; output to be copied to multiple target locations | |
297 | (if arg | |
298 | (setq command | |
299 | (concat command | |
300 | (format " >>> #<buffer %s>" | |
301 | (buffer-name (current-buffer)))))) | |
302 | (save-excursion | |
affbf647 GM |
303 | (let ((buf (set-buffer (generate-new-buffer " *eshell cmd*"))) |
304 | (eshell-non-interactive-p t)) | |
305 | (eshell-mode) | |
306 | (let* ((proc (eshell-eval-command | |
307 | (list 'eshell-commands | |
308 | (eshell-parse-command command)))) | |
309 | intr | |
310 | (bufname (if (and proc (listp proc)) | |
311 | "*EShell Async Command Output*" | |
312 | (setq intr t) | |
313 | "*EShell Command Output*"))) | |
314 | (if (buffer-live-p (get-buffer bufname)) | |
315 | (kill-buffer bufname)) | |
316 | (rename-buffer bufname) | |
317 | ;; things get a little coarse here, since the desire is to | |
318 | ;; make the output as attractive as possible, with no | |
319 | ;; extraneous newlines | |
320 | (when intr | |
321 | (if (eshell-interactive-process) | |
322 | (eshell-wait-for-process (eshell-interactive-process))) | |
a464a6c7 | 323 | (cl-assert (not (eshell-interactive-process))) |
affbf647 GM |
324 | (goto-char (point-max)) |
325 | (while (and (bolp) (not (bobp))) | |
d355a0b7 | 326 | (delete-char -1))) |
a464a6c7 | 327 | (cl-assert (and buf (buffer-live-p buf))) |
affbf647 GM |
328 | (unless arg |
329 | (let ((len (if (not intr) 2 | |
330 | (count-lines (point-min) (point-max))))) | |
331 | (cond | |
332 | ((= len 0) | |
333 | (message "(There was no command output)") | |
334 | (kill-buffer buf)) | |
335 | ((= len 1) | |
ed942deb | 336 | (message "%s" (buffer-string)) |
affbf647 GM |
337 | (kill-buffer buf)) |
338 | (t | |
339 | (save-selected-window | |
340 | (select-window (display-buffer buf)) | |
341 | (goto-char (point-min)) | |
342 | ;; cause the output buffer to take up as little screen | |
343 | ;; real-estate as possible, if temp buffer resizing is | |
344 | ;; enabled | |
345 | (and intr temp-buffer-resize-mode | |
346 | (resize-temp-buffer-window))))))))))) | |
347 | ||
348 | ;;;###autoload | |
349 | (defun eshell-command-result (command &optional status-var) | |
350 | "Execute the given Eshell COMMAND, and return the result. | |
351 | The result might be any Lisp object. | |
352 | If STATUS-VAR is a symbol, it will be set to the exit status of the | |
353 | command. This is the only way to determine whether the value returned | |
354 | corresponding to a successful execution." | |
355 | ;; a null command produces a null, successful result | |
356 | (if (not command) | |
357 | (ignore | |
358 | (if (and status-var (symbolp status-var)) | |
359 | (set status-var 0))) | |
360 | (with-temp-buffer | |
affbf647 GM |
361 | (let ((eshell-non-interactive-p t)) |
362 | (eshell-mode) | |
363 | (let ((result (eshell-do-eval | |
364 | (list 'eshell-commands | |
365 | (list 'eshell-command-to-value | |
366 | (eshell-parse-command command))) t))) | |
a464a6c7 | 367 | (cl-assert (eq (car result) 'quote)) |
affbf647 GM |
368 | (if (and status-var (symbolp status-var)) |
369 | (set status-var eshell-last-command-status)) | |
370 | (cadr result)))))) | |
371 | ||
affbf647 GM |
372 | ;;;_* Reporting bugs |
373 | ;; | |
abf1a55b | 374 | ;; If you do encounter a bug, on any system, please report |
affbf647 GM |
375 | ;; it -- in addition to any particular oddities in your configuration |
376 | ;; -- so that the problem may be corrected for the benefit of others. | |
377 | ||
affbf647 | 378 | ;;;###autoload |
538f2308 | 379 | (define-obsolete-function-alias 'eshell-report-bug 'report-emacs-bug "23.1") |
affbf647 GM |
380 | |
381 | ;;; Code: | |
382 | ||
383 | (defun eshell-unload-all-modules () | |
384 | "Unload all modules that were loaded by Eshell, if possible. | |
385 | If the user has require'd in any of the modules, or customized a | |
386 | variable with a :require tag (such as `eshell-prefer-to-shell'), it | |
387 | will be impossible to unload Eshell completely without restarting | |
388 | Emacs." | |
389 | ;; if the user set `eshell-prefer-to-shell' to t, but never loaded | |
390 | ;; Eshell, then `eshell-subgroups' will be unbound | |
391 | (when (fboundp 'eshell-subgroups) | |
a9eeff78 | 392 | (dolist (module (eshell-subgroups 'eshell)) |
affbf647 GM |
393 | ;; this really only unloads as many modules as possible, |
394 | ;; since other `require' references (such as by customizing | |
395 | ;; `eshell-prefer-to-shell' to a non-nil value) might make it | |
396 | ;; impossible to unload Eshell completely | |
397 | (if (featurep module) | |
398 | (ignore-errors | |
399 | (message "Unloading %s..." (symbol-name module)) | |
400 | (unload-feature module) | |
401 | (message "Unloading %s...done" (symbol-name module))))) | |
402 | (message "Unloading eshell...done"))) | |
403 | ||
404 | (run-hooks 'eshell-load-hook) | |
405 | ||
c6d48e47 GM |
406 | (provide 'eshell) |
407 | ||
affbf647 | 408 | ;;; eshell.el ends here |