Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; esh-cmd.el --- command invocation |
25fffb31 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1999-2011 Free Software Foundation, Inc. |
25fffb31 | 4 | |
7de5b421 GM |
5 | ;; Author: John Wiegley <johnw@gnu.org> |
6 | ||
25fffb31 GM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
4ee57b2a | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
25fffb31 | 10 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
25fffb31 GM |
13 | |
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
4ee57b2a | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
25fffb31 | 21 | |
25fffb31 GM |
22 | ;;; Commentary: |
23 | ||
24 | ;;;_* Invoking external commands | |
25 | ;; | |
26 | ;; External commands cause processes to be created, by loading | |
27 | ;; external executables into memory. This is what most normal shells | |
28 | ;; do, most of the time. For more information, see [External commands]. | |
29 | ;; | |
30 | ;;;_* Invoking Lisp functions | |
31 | ;; | |
32 | ;; A Lisp function can be invoked using Lisp syntax, or command shell | |
33 | ;; syntax. For example, to run `dired' to edit the current directory: | |
34 | ;; | |
35 | ;; /tmp $ (dired ".") | |
36 | ;; | |
37 | ;; Or: | |
38 | ;; | |
39 | ;; /tmp $ dired . | |
40 | ;; | |
41 | ;; The latter form is preferable, but the former is more precise, | |
42 | ;; since it involves no translations. See [Argument parsing], to | |
43 | ;; learn more about how arguments are transformed before passing them | |
44 | ;; to commands. | |
45 | ;; | |
46 | ;; Ordinarily, if 'dired' were also available as an external command, | |
47 | ;; the external version would be called in preference to any Lisp | |
48 | ;; function of the same name. To change this behavior so that Lisp | |
49 | ;; functions always take precedence, set | |
50 | ;; `eshell-prefer-lisp-functions' to t. | |
51 | ||
25fffb31 GM |
52 | ;;;_* Alias functions |
53 | ;; | |
54 | ;; Whenever a command is specified using a simple name, such as 'ls', | |
55 | ;; Eshell will first look for a Lisp function of the name `eshell/ls'. | |
56 | ;; If it exists, it will be called in preference to any other command | |
57 | ;; which might have matched the name 'ls' (such as command aliases, | |
58 | ;; external commands, Lisp functions of that name, etc). | |
59 | ;; | |
60 | ;; This is the most flexible mechanism for creating new commands, | |
61 | ;; since it does not pollute the global namespace, yet allows you to | |
62 | ;; use all of Lisp's facilities to define that piece of functionality. | |
63 | ;; Most of Eshell's "builtin" commands are defined as alias functions. | |
64 | ;; | |
65 | ;;;_* Lisp arguments | |
66 | ;; | |
67 | ;; It is possible to invoke a Lisp form as an argument. This can be | |
68 | ;; done either by specifying the form as you might in Lisp, or by | |
69 | ;; using the '$' character to introduce a value-interpolation: | |
70 | ;; | |
71 | ;; echo (+ 1 2) | |
72 | ;; | |
73 | ;; Or | |
74 | ;; | |
75 | ;; echo $(+ 1 2) | |
76 | ;; | |
77 | ;; The two forms are equivalent. The second is required only if the | |
78 | ;; form being interpolated is within a string, or is a subexpression | |
79 | ;; of a larger argument: | |
80 | ;; | |
81 | ;; echo x$(+ 1 2) "String $(+ 1 2)" | |
82 | ;; | |
83 | ;; To pass a Lisp symbol as a argument, use the alternate quoting | |
84 | ;; syntax, since the single quote character is far too overused in | |
85 | ;; shell syntax: | |
86 | ;; | |
87 | ;; echo #'lisp-symbol | |
88 | ;; | |
89 | ;; Backquote can also be used: | |
90 | ;; | |
91 | ;; echo `(list ,lisp-symbol) | |
92 | ;; | |
93 | ;; Lisp arguments are identified using the following regexp: | |
94 | ||
56590d2f GM |
95 | ;;;_* Command hooks |
96 | ;; | |
97 | ;; There are several hooks involved with command execution, which can | |
98 | ;; be used either to change or augment Eshell's behavior. | |
99 | ||
100 | ||
101 | ;;; Code: | |
102 | ||
103 | (require 'esh-util) | |
104 | (unless (featurep 'xemacs) | |
105 | (require 'eldoc)) | |
106 | (require 'esh-arg) | |
107 | (require 'esh-proc) | |
108 | (require 'esh-ext) | |
109 | ||
110 | (eval-when-compile | |
fc17acd1 | 111 | (require 'cl) |
56590d2f GM |
112 | (require 'pcomplete)) |
113 | ||
114 | ||
115 | (defgroup eshell-cmd nil | |
116 | "Executing an Eshell command is as simple as typing it in and | |
117 | pressing <RET>. There are several different kinds of commands, | |
118 | however." | |
119 | :tag "Command invocation" | |
120 | ;; :link '(info-link "(eshell)Command invocation") | |
121 | :group 'eshell) | |
122 | ||
123 | (defcustom eshell-prefer-lisp-functions nil | |
ec60da52 | 124 | "If non-nil, prefer Lisp functions to external commands." |
56590d2f GM |
125 | :type 'boolean |
126 | :group 'eshell-cmd) | |
127 | ||
25fffb31 | 128 | (defcustom eshell-lisp-regexp "\\([(`]\\|#'\\)" |
ec60da52 | 129 | "A regexp which, if matched at beginning of an argument, means Lisp. |
25fffb31 GM |
130 | Such arguments will be passed to `read', and then evaluated." |
131 | :type 'regexp | |
132 | :group 'eshell-cmd) | |
133 | ||
25fffb31 | 134 | (defcustom eshell-pre-command-hook nil |
ec60da52 | 135 | "A hook run before each interactive command is invoked." |
25fffb31 GM |
136 | :type 'hook |
137 | :group 'eshell-cmd) | |
138 | ||
139 | (defcustom eshell-post-command-hook nil | |
ec60da52 | 140 | "A hook run after each interactive command is invoked." |
25fffb31 GM |
141 | :type 'hook |
142 | :group 'eshell-cmd) | |
143 | ||
144 | (defcustom eshell-prepare-command-hook nil | |
ec60da52 | 145 | "A set of functions called to prepare a named command. |
25fffb31 GM |
146 | The command name and its argument are in `eshell-last-command-name' |
147 | and `eshell-last-arguments'. The functions on this hook can change | |
148 | the value of these symbols if necessary. | |
149 | ||
150 | To prevent a command from executing at all, set | |
151 | `eshell-last-command-name' to nil." | |
152 | :type 'hook | |
153 | :group 'eshell-cmd) | |
154 | ||
155 | (defcustom eshell-named-command-hook nil | |
ec60da52 | 156 | "A set of functions called before a named command is invoked. |
25fffb31 GM |
157 | Each function will be passed the command name and arguments that were |
158 | passed to `eshell-named-command'. | |
159 | ||
160 | If any of the functions returns a non-nil value, the named command | |
161 | will not be invoked, and that value will be returned from | |
162 | `eshell-named-command'. | |
163 | ||
164 | In order to substitute an alternate command form for execution, the | |
165 | hook function should throw it using the tag `eshell-replace-command'. | |
166 | For example: | |
167 | ||
168 | (add-hook 'eshell-named-command-hook 'subst-with-cd) | |
169 | (defun subst-with-cd (command args) | |
170 | (throw 'eshell-replace-command | |
171 | (eshell-parse-command \"cd\" args))) | |
172 | ||
173 | Although useless, the above code will cause any non-glob, non-Lisp | |
174 | command (i.e., 'ls' as opposed to '*ls' or '(ls)') to be replaced by a | |
175 | call to `cd' using the arguments that were passed to the function." | |
176 | :type 'hook | |
177 | :group 'eshell-cmd) | |
178 | ||
179 | (defcustom eshell-pre-rewrite-command-hook | |
180 | '(eshell-no-command-conversion | |
181 | eshell-subcommand-arg-values) | |
ec60da52 | 182 | "A hook run before command rewriting begins. |
25fffb31 GM |
183 | The terms of the command to be rewritten is passed as arguments, and |
184 | may be modified in place. Any return value is ignored." | |
185 | :type 'hook | |
186 | :group 'eshell-cmd) | |
187 | ||
188 | (defcustom eshell-rewrite-command-hook | |
189 | '(eshell-rewrite-for-command | |
190 | eshell-rewrite-while-command | |
191 | eshell-rewrite-if-command | |
192 | eshell-rewrite-sexp-command | |
193 | eshell-rewrite-initial-subcommand | |
194 | eshell-rewrite-named-command) | |
ec60da52 | 195 | "A set of functions used to rewrite the command argument. |
25fffb31 GM |
196 | Once parsing of a command line is completed, the next step is to |
197 | rewrite the initial argument into something runnable. | |
198 | ||
199 | A module may wish to associate special behavior with certain argument | |
200 | syntaxes at the beginning of a command line. They are welcome to do | |
201 | so by adding a function to this hook. The first function to return a | |
202 | substitute command form is the one used. Each function is passed the | |
203 | command's full argument list, which is a list of sexps (typically | |
204 | forms or strings)." | |
205 | :type 'hook | |
206 | :group 'eshell-cmd) | |
207 | ||
208 | (defcustom eshell-post-rewrite-command-hook nil | |
ec60da52 | 209 | "A hook run after command rewriting is finished. |
25fffb31 GM |
210 | Each function is passed the symbol containing the rewritten command, |
211 | which may be modified directly. Any return value is ignored." | |
212 | :type 'hook | |
213 | :group 'eshell-cmd) | |
214 | ||
820b9143 | 215 | (defcustom eshell-complex-commands '("ls") |
ec60da52 | 216 | "A list of commands names or functions, that determine complexity. |
dace60cf JW |
217 | That is, if a command is defined by a function named eshell/NAME, |
218 | and NAME is part of this list, it is invoked as a complex command. | |
219 | Complex commands are always correct, but run much slower. If a | |
220 | command works fine without being part of this list, then it doesn't | |
221 | need to be. | |
222 | ||
223 | If an entry is a function, it will be called with the name, and should | |
224 | return non-nil if the command is complex." | |
225 | :type '(repeat :tag "Commands" | |
226 | (choice (string :tag "Name") | |
227 | (function :tag "Predicate"))) | |
228 | :group 'eshell-cmd) | |
229 | ||
25fffb31 GM |
230 | ;;; User Variables: |
231 | ||
d783d303 | 232 | (defcustom eshell-cmd-load-hook nil |
ec60da52 | 233 | "A hook that gets run when `eshell-cmd' is loaded." |
d783d303 | 234 | :version "24.1" ; removed eshell-cmd-initialize |
25fffb31 GM |
235 | :type 'hook |
236 | :group 'eshell-cmd) | |
237 | ||
238 | (defcustom eshell-debug-command nil | |
ec60da52 | 239 | "If non-nil, enable debugging code. SSLLOOWW. |
25fffb31 GM |
240 | This option is only useful for reporting bugs. If you enable it, you |
241 | will have to visit the file 'eshell-cmd.el' and run the command | |
242 | \\[eval-buffer]." | |
243 | :type 'boolean | |
244 | :group 'eshell-cmd) | |
245 | ||
246 | (defcustom eshell-deferrable-commands | |
247 | '(eshell-named-command | |
248 | eshell-lisp-command | |
249 | eshell-process-identity) | |
91af3942 | 250 | "A list of functions which might return an asynchronous process. |
25fffb31 GM |
251 | If they return a process object, execution of the calling Eshell |
252 | command will wait for completion (in the background) before finishing | |
253 | the command." | |
254 | :type '(repeat function) | |
255 | :group 'eshell-cmd) | |
256 | ||
257 | (defcustom eshell-subcommand-bindings | |
258 | '((eshell-in-subcommand-p t) | |
259 | (default-directory default-directory) | |
260 | (process-environment (eshell-copy-environment))) | |
ec60da52 | 261 | "A list of `let' bindings for subcommand environments." |
25fffb31 GM |
262 | :type 'sexp |
263 | :group 'eshell-cmd) | |
264 | ||
265 | (put 'risky-local-variable 'eshell-subcommand-bindings t) | |
266 | ||
267 | (defvar eshell-ensure-newline-p nil | |
268 | "If non-nil, ensure that a newline is emitted after a Lisp form. | |
269 | This can be changed by Lisp forms that are evaluated from the Eshell | |
270 | command line.") | |
271 | ||
272 | ;;; Internal Variables: | |
273 | ||
274 | (defvar eshell-current-command nil) | |
275 | (defvar eshell-command-name nil) | |
276 | (defvar eshell-command-arguments nil) | |
5101a9dc GM |
277 | (defvar eshell-in-pipeline-p nil |
278 | "Internal Eshell variable, non-nil inside a pipeline. | |
279 | Has the value 'first, 'last for the first/last commands in the pipeline, | |
280 | otherwise t.") | |
25fffb31 GM |
281 | (defvar eshell-in-subcommand-p nil) |
282 | (defvar eshell-last-arguments nil) | |
283 | (defvar eshell-last-command-name nil) | |
284 | (defvar eshell-last-async-proc nil | |
285 | "When this foreground process completes, resume command evaluation.") | |
286 | ||
287 | ;;; Functions: | |
288 | ||
289 | (defsubst eshell-interactive-process () | |
290 | "Return currently running command process, if non-Lisp." | |
291 | eshell-last-async-proc) | |
292 | ||
293 | (defun eshell-cmd-initialize () | |
294 | "Initialize the Eshell command processing module." | |
295 | (set (make-local-variable 'eshell-current-command) nil) | |
296 | (set (make-local-variable 'eshell-command-name) nil) | |
297 | (set (make-local-variable 'eshell-command-arguments) nil) | |
298 | (set (make-local-variable 'eshell-last-arguments) nil) | |
299 | (set (make-local-variable 'eshell-last-command-name) nil) | |
300 | (set (make-local-variable 'eshell-last-async-proc) nil) | |
301 | ||
25fffb31 GM |
302 | (add-hook 'eshell-kill-hook 'eshell-resume-command nil t) |
303 | ||
304 | ;; make sure that if a command is over, and no process is being | |
305 | ;; waited for, that `eshell-current-command' is set to nil. This | |
306 | ;; situation can occur, for example, if a Lisp function results in | |
307 | ;; `debug' being called, and the user then types \\[top-level] | |
25fffb31 GM |
308 | (add-hook 'eshell-post-command-hook |
309 | (function | |
310 | (lambda () | |
311 | (setq eshell-current-command nil | |
312 | eshell-last-async-proc nil))) nil t) | |
313 | ||
25fffb31 GM |
314 | (add-hook 'eshell-parse-argument-hook |
315 | 'eshell-parse-subcommand-argument nil t) | |
316 | (add-hook 'eshell-parse-argument-hook | |
317 | 'eshell-parse-lisp-argument nil t) | |
318 | ||
319 | (when (eshell-using-module 'eshell-cmpl) | |
25fffb31 GM |
320 | (add-hook 'pcomplete-try-first-hook |
321 | 'eshell-complete-lisp-symbols nil t))) | |
322 | ||
25fffb31 GM |
323 | (defun eshell-complete-lisp-symbols () |
324 | "If there is a user reference, complete it." | |
325 | (let ((arg (pcomplete-actual-arg))) | |
326 | (when (string-match (concat "\\`" eshell-lisp-regexp) arg) | |
327 | (setq pcomplete-stub (substring arg (match-end 0)) | |
328 | pcomplete-last-completion-raw t) | |
329 | (throw 'pcomplete-completions | |
330 | (all-completions pcomplete-stub obarray 'boundp))))) | |
331 | ||
332 | ;; Command parsing | |
333 | ||
334 | (defun eshell-parse-command (command &optional args top-level) | |
335 | "Parse the COMMAND, adding ARGS if given. | |
336 | COMMAND can either be a string, or a cons cell demarcating a buffer | |
337 | region. TOP-LEVEL, if non-nil, means that the outermost command (the | |
338 | user's input command) is being parsed, and that pre and post command | |
339 | hooks should be run before and after the command." | |
340 | (let* (sep-terms | |
341 | (terms | |
342 | (append | |
343 | (if (consp command) | |
344 | (eshell-parse-arguments (car command) (cdr command)) | |
345 | (let ((here (point)) | |
6e13206c SM |
346 | (inhibit-point-motion-hooks t)) |
347 | (with-silent-modifications | |
348 | ;; FIXME: Why not use a temporary buffer and avoid this | |
349 | ;; "insert&delete" business? --Stef | |
350 | (insert command) | |
351 | (prog1 | |
352 | (eshell-parse-arguments here (point)) | |
353 | (delete-region here (point)))))) | |
25fffb31 GM |
354 | args)) |
355 | (commands | |
356 | (mapcar | |
357 | (function | |
358 | (lambda (cmd) | |
1bc4c3ae SM |
359 | (setq cmd |
360 | (if (or (not (car sep-terms)) | |
361 | (string= (car sep-terms) ";")) | |
362 | (eshell-parse-pipeline cmd (not (car sep-terms))) | |
363 | `(eshell-do-subjob | |
364 | (list ,(eshell-parse-pipeline cmd))))) | |
25fffb31 GM |
365 | (setq sep-terms (cdr sep-terms)) |
366 | (if eshell-in-pipeline-p | |
367 | cmd | |
1bc4c3ae | 368 | `(eshell-trap-errors ,cmd)))) |
25fffb31 GM |
369 | (eshell-separate-commands terms "[&;]" nil 'sep-terms)))) |
370 | (let ((cmd commands)) | |
371 | (while cmd | |
372 | (if (cdr cmd) | |
1bc4c3ae | 373 | (setcar cmd `(eshell-commands ,(car cmd)))) |
25fffb31 GM |
374 | (setq cmd (cdr cmd)))) |
375 | (setq commands | |
1bc4c3ae SM |
376 | `(progn |
377 | ,@(if top-level | |
378 | '((run-hooks 'eshell-pre-command-hook))) | |
379 | ,@(if (not top-level) | |
380 | commands | |
381 | `((catch 'top-level (progn ,@commands)) | |
382 | (run-hooks 'eshell-post-command-hook))))) | |
25fffb31 | 383 | (if top-level |
1bc4c3ae | 384 | `(eshell-commands ,commands) |
25fffb31 GM |
385 | commands))) |
386 | ||
56590d2f GM |
387 | (defun eshell-debug-command (tag subform) |
388 | "Output a debugging message to '*eshell last cmd*'." | |
389 | (let ((buf (get-buffer-create "*eshell last cmd*")) | |
390 | (text (eshell-stringify eshell-current-command))) | |
ee5b7365 | 391 | (with-current-buffer buf |
56590d2f GM |
392 | (if (not tag) |
393 | (erase-buffer) | |
394 | (insert "\n\C-l\n" tag "\n\n" text | |
395 | (if subform | |
396 | (concat "\n\n" (eshell-stringify subform)) "")))))) | |
397 | ||
25fffb31 GM |
398 | (defun eshell-debug-show-parsed-args (terms) |
399 | "Display parsed arguments in the debug buffer." | |
400 | (ignore | |
401 | (if eshell-debug-command | |
402 | (eshell-debug-command "parsed arguments" terms)))) | |
403 | ||
404 | (defun eshell-no-command-conversion (terms) | |
405 | "Don't convert the command argument." | |
406 | (ignore | |
407 | (if (and (listp (car terms)) | |
408 | (eq (caar terms) 'eshell-convert)) | |
409 | (setcar terms (cadr (car terms)))))) | |
410 | ||
411 | (defun eshell-subcommand-arg-values (terms) | |
412 | "Convert subcommand arguments {x} to ${x}, in order to take their values." | |
413 | (setq terms (cdr terms)) ; skip command argument | |
414 | (while terms | |
415 | (if (and (listp (car terms)) | |
416 | (eq (caar terms) 'eshell-as-subcommand)) | |
1bc4c3ae SM |
417 | (setcar terms `(eshell-convert |
418 | (eshell-command-to-value ,(car terms))))) | |
25fffb31 GM |
419 | (setq terms (cdr terms)))) |
420 | ||
421 | (defun eshell-rewrite-sexp-command (terms) | |
422 | "Rewrite a sexp in initial position, such as '(+ 1 2)'." | |
423 | ;; this occurs when a Lisp expression is in first position | |
424 | (if (and (listp (car terms)) | |
425 | (eq (caar terms) 'eshell-command-to-value)) | |
426 | (car (cdar terms)))) | |
427 | ||
25fffb31 GM |
428 | (defun eshell-rewrite-initial-subcommand (terms) |
429 | "Rewrite a subcommand in initial position, such as '{+ 1 2}'." | |
430 | (if (and (listp (car terms)) | |
431 | (eq (caar terms) 'eshell-as-subcommand)) | |
432 | (car terms))) | |
433 | ||
25fffb31 GM |
434 | (defun eshell-rewrite-named-command (terms) |
435 | "If no other rewriting rule transforms TERMS, assume a named command." | |
f4dd1361 JW |
436 | (let ((sym (if eshell-in-pipeline-p |
437 | 'eshell-named-command* | |
438 | 'eshell-named-command)) | |
439 | (cmd (car terms)) | |
440 | (args (cdr terms))) | |
441 | (if args | |
1bc4c3ae | 442 | (list sym cmd `(list ,@(cdr terms))) |
f4dd1361 | 443 | (list sym cmd)))) |
25fffb31 | 444 | |
1a32899d GM |
445 | (defvar eshell-command-body) |
446 | (defvar eshell-test-body) | |
25fffb31 GM |
447 | |
448 | (defsubst eshell-invokify-arg (arg &optional share-output silent) | |
449 | "Change ARG so it can be invoked from a structured command. | |
450 | ||
451 | SHARE-OUTPUT, if non-nil, means this invocation should share the | |
452 | current output stream, which is separately redirectable. SILENT | |
453 | means the user and/or any redirections shouldn't see any output | |
454 | from this command. If both SHARE-OUTPUT and SILENT are non-nil, | |
455 | the second is ignored." | |
456 | ;; something that begins with `eshell-convert' means that it | |
457 | ;; intends to return a Lisp value. We want to get past this, | |
458 | ;; but if it's not _actually_ a value interpolation -- in which | |
459 | ;; we leave it alone. In fact, the only time we muck with it | |
460 | ;; is in the case of a {subcommand} that has been turned into | |
461 | ;; the interpolation, ${subcommand}, by the parser because it | |
462 | ;; didn't know better. | |
463 | (if (and (listp arg) | |
464 | (eq (car arg) 'eshell-convert) | |
465 | (eq (car (cadr arg)) 'eshell-command-to-value)) | |
466 | (if share-output | |
467 | (cadr (cadr arg)) | |
1bc4c3ae | 468 | `(eshell-commands ,(cadr (cadr arg)) ,silent)) |
25fffb31 GM |
469 | arg)) |
470 | ||
1bc4c3ae SM |
471 | (defvar eshell-last-command-status) ;Define in esh-io.el. |
472 | ||
25fffb31 GM |
473 | (defun eshell-rewrite-for-command (terms) |
474 | "Rewrite a `for' command into its equivalent Eshell command form. | |
475 | Because the implementation of `for' relies upon conditional evaluation | |
31096fe8 | 476 | of its argument (i.e., use of a Lisp special form), it must be |
25fffb31 | 477 | implemented via rewriting, rather than as a function." |
1bc4c3ae SM |
478 | (if (and (equal (car terms) "for") |
479 | (equal (nth 2 terms) "in")) | |
25fffb31 GM |
480 | (let ((body (car (last terms)))) |
481 | (setcdr (last terms 2) nil) | |
1bc4c3ae | 482 | `(let ((for-items |
c1e2f5fa SM |
483 | ;; Apparently, eshell-do-eval only works for immutable |
484 | ;; let-bindings, i.e. we cannot use `setq' on `for-items'. | |
485 | ;; Instead we store the list in the car of a cons-cell (which | |
486 | ;; acts as a ref-cell) so we can setcar instead of setq. | |
487 | (list | |
488 | (append | |
489 | ,@(mapcar | |
490 | (lambda (elem) | |
491 | (if (listp elem) | |
492 | elem | |
493 | `(list ,elem))) | |
494 | (cdr (cddr terms)))))) | |
1bc4c3ae SM |
495 | (eshell-command-body '(nil)) |
496 | (eshell-test-body '(nil))) | |
c1e2f5fa SM |
497 | (while (consp (car for-items)) |
498 | (let ((,(intern (cadr terms)) (caar for-items))) | |
499 | (eshell-protect | |
500 | ,(eshell-invokify-arg body t))) | |
501 | (setcar for-items (cdar for-items))) | |
1bc4c3ae SM |
502 | (eshell-close-handles |
503 | eshell-last-command-status | |
504 | (list 'quote eshell-last-command-result)))))) | |
25fffb31 GM |
505 | |
506 | (defun eshell-structure-basic-command (func names keyword test body | |
507 | &optional else vocal-test) | |
508 | "With TERMS, KEYWORD, and two NAMES, structure a basic command. | |
509 | The first of NAMES should be the positive form, and the second the | |
510 | negative. It's not likely that users should ever need to call this | |
511 | function. | |
512 | ||
513 | If VOCAL-TEST is non-nil, it means output from the test should be | |
514 | shown, as well as output from the body." | |
515 | ;; If the test form begins with `eshell-convert', it means | |
516 | ;; something data-wise will be returned, and we should let | |
517 | ;; that determine the truth of the statement. | |
518 | (unless (eq (car test) 'eshell-convert) | |
519 | (setq test | |
1bc4c3ae SM |
520 | `(progn ,test |
521 | (eshell-exit-success-p)))) | |
25fffb31 GM |
522 | |
523 | ;; should we reverse the sense of the test? This depends | |
524 | ;; on the `names' parameter. If it's the symbol nil, yes. | |
525 | ;; Otherwise, it can be a pair of strings; if the keyword | |
526 | ;; we're using matches the second member of that pair (a | |
527 | ;; list), we should reverse it. | |
528 | (if (or (eq names nil) | |
529 | (and (listp names) | |
530 | (string= keyword (cadr names)))) | |
1bc4c3ae | 531 | (setq test `(not ,test))) |
25fffb31 GM |
532 | |
533 | ;; finally, create the form that represents this structured | |
534 | ;; command | |
1bc4c3ae SM |
535 | `(let ((eshell-command-body '(nil)) |
536 | (eshell-test-body '(nil))) | |
537 | (,func ,test ,body ,else) | |
538 | (eshell-close-handles | |
539 | eshell-last-command-status | |
540 | (list 'quote eshell-last-command-result)))) | |
25fffb31 GM |
541 | |
542 | (defun eshell-rewrite-while-command (terms) | |
543 | "Rewrite a `while' command into its equivalent Eshell command form. | |
544 | Because the implementation of `while' relies upon conditional | |
545 | evaluation of its argument (i.e., use of a Lisp special form), it | |
546 | must be implemented via rewriting, rather than as a function." | |
547 | (if (and (stringp (car terms)) | |
548 | (member (car terms) '("while" "until"))) | |
549 | (eshell-structure-basic-command | |
550 | 'while '("while" "until") (car terms) | |
551 | (eshell-invokify-arg (cadr terms) nil t) | |
1bc4c3ae SM |
552 | `(eshell-protect |
553 | ,(eshell-invokify-arg (car (last terms)) t))))) | |
25fffb31 GM |
554 | |
555 | (defun eshell-rewrite-if-command (terms) | |
556 | "Rewrite an `if' command into its equivalent Eshell command form. | |
557 | Because the implementation of `if' relies upon conditional | |
558 | evaluation of its argument (i.e., use of a Lisp special form), it | |
559 | must be implemented via rewriting, rather than as a function." | |
560 | (if (and (stringp (car terms)) | |
561 | (member (car terms) '("if" "unless"))) | |
562 | (eshell-structure-basic-command | |
563 | 'if '("if" "unless") (car terms) | |
564 | (eshell-invokify-arg (cadr terms) nil t) | |
1bc4c3ae SM |
565 | `(eshell-protect |
566 | ,(eshell-invokify-arg (car (last terms (if (= (length terms) 4) 2))) | |
567 | t)) | |
05a68572 | 568 | (if (= (length terms) 4) |
1bc4c3ae SM |
569 | `(eshell-protect |
570 | ,(eshell-invokify-arg (car (last terms)))) t)))) | |
571 | ||
572 | (defvar eshell-last-command-result) ;Defined in esh-io.el. | |
25fffb31 GM |
573 | |
574 | (defun eshell-exit-success-p () | |
575 | "Return non-nil if the last command was \"successful\". | |
576 | For a bit of Lisp code, this means a return value of non-nil. | |
577 | For an external command, it means an exit code of 0." | |
05a68572 JW |
578 | (if (save-match-data |
579 | (string-match "#<\\(Lisp object\\|function .*\\)>" | |
580 | eshell-last-command-name)) | |
25fffb31 GM |
581 | eshell-last-command-result |
582 | (= eshell-last-command-status 0))) | |
583 | ||
584 | (defun eshell-parse-pipeline (terms &optional final-p) | |
585 | "Parse a pipeline from TERMS, return the appropriate Lisp forms." | |
586 | (let* (sep-terms | |
587 | (bigpieces (eshell-separate-commands terms "\\(&&\\|||\\)" | |
588 | nil 'sep-terms)) | |
589 | (bp bigpieces) | |
590 | (results (list t)) | |
591 | final) | |
592 | (while bp | |
593 | (let ((subterms (car bp))) | |
594 | (let* ((pieces (eshell-separate-commands subterms "|")) | |
595 | (p pieces)) | |
596 | (while p | |
597 | (let ((cmd (car p))) | |
598 | (run-hook-with-args 'eshell-pre-rewrite-command-hook cmd) | |
599 | (setq cmd (run-hook-with-args-until-success | |
600 | 'eshell-rewrite-command-hook cmd)) | |
601 | (run-hook-with-args 'eshell-post-rewrite-command-hook 'cmd) | |
602 | (setcar p cmd)) | |
603 | (setq p (cdr p))) | |
604 | (nconc results | |
605 | (list | |
606 | (if (<= (length pieces) 1) | |
607 | (car pieces) | |
608 | (assert (not eshell-in-pipeline-p)) | |
1bc4c3ae | 609 | `(eshell-execute-pipeline (quote ,pieces)))))) |
25fffb31 GM |
610 | (setq bp (cdr bp)))) |
611 | ;; `results' might be empty; this happens in the case of | |
612 | ;; multi-line input | |
613 | (setq results (cdr results) | |
614 | results (nreverse results) | |
615 | final (car results) | |
616 | results (cdr results) | |
617 | sep-terms (nreverse sep-terms)) | |
618 | (while results | |
619 | (assert (car sep-terms)) | |
620 | (setq final (eshell-structure-basic-command | |
621 | 'if (string= (car sep-terms) "&&") "if" | |
1bc4c3ae SM |
622 | `(eshell-protect ,(car results)) |
623 | `(eshell-protect ,final) | |
25fffb31 GM |
624 | nil t) |
625 | results (cdr results) | |
626 | sep-terms (cdr sep-terms))) | |
627 | final)) | |
628 | ||
629 | (defun eshell-parse-subcommand-argument () | |
630 | "Parse a subcommand argument of the form '{command}'." | |
631 | (if (and (not eshell-current-argument) | |
632 | (not eshell-current-quoted) | |
633 | (eq (char-after) ?\{) | |
634 | (or (= (point-max) (1+ (point))) | |
635 | (not (eq (char-after (1+ (point))) ?\})))) | |
636 | (let ((end (eshell-find-delimiter ?\{ ?\}))) | |
637 | (if (not end) | |
638 | (throw 'eshell-incomplete ?\{) | |
639 | (when (eshell-arg-delimiter (1+ end)) | |
640 | (prog1 | |
1bc4c3ae SM |
641 | `(eshell-as-subcommand |
642 | ,(eshell-parse-command (cons (1+ (point)) end))) | |
25fffb31 GM |
643 | (goto-char (1+ end)))))))) |
644 | ||
645 | (defun eshell-parse-lisp-argument () | |
646 | "Parse a Lisp expression which is specified as an argument." | |
647 | (if (and (not eshell-current-argument) | |
648 | (not eshell-current-quoted) | |
649 | (looking-at eshell-lisp-regexp)) | |
650 | (let* ((here (point)) | |
651 | (obj | |
652 | (condition-case err | |
653 | (read (current-buffer)) | |
654 | (end-of-file | |
655 | (throw 'eshell-incomplete ?\())))) | |
656 | (if (eshell-arg-delimiter) | |
1bc4c3ae SM |
657 | `(eshell-command-to-value |
658 | (eshell-lisp-command (quote ,obj))) | |
25fffb31 GM |
659 | (ignore (goto-char here)))))) |
660 | ||
dace60cf JW |
661 | (defun eshell-separate-commands (terms separator &optional |
662 | reversed last-terms-sym) | |
25fffb31 GM |
663 | "Separate TERMS using SEPARATOR. |
664 | If REVERSED is non-nil, the list of separated term groups will be | |
d4469517 | 665 | returned in reverse order. If LAST-TERMS-SYM is a symbol, its value |
25fffb31 GM |
666 | will be set to a list of all the separator operators found (or '(list |
667 | nil)' if none)." | |
668 | (let ((sub-terms (list t)) | |
669 | (eshell-sep-terms (list t)) | |
670 | subchains) | |
671 | (while terms | |
672 | (if (and (consp (car terms)) | |
673 | (eq (caar terms) 'eshell-operator) | |
674 | (string-match (concat "^" separator "$") | |
675 | (nth 1 (car terms)))) | |
676 | (progn | |
677 | (nconc eshell-sep-terms (list (nth 1 (car terms)))) | |
678 | (setq subchains (cons (cdr sub-terms) subchains) | |
679 | sub-terms (list t))) | |
680 | (nconc sub-terms (list (car terms)))) | |
681 | (setq terms (cdr terms))) | |
682 | (if (> (length sub-terms) 1) | |
683 | (setq subchains (cons (cdr sub-terms) subchains))) | |
684 | (if reversed | |
685 | (progn | |
686 | (if last-terms-sym | |
687 | (set last-terms-sym (reverse (cdr eshell-sep-terms)))) | |
688 | subchains) ; already reversed | |
689 | (if last-terms-sym | |
690 | (set last-terms-sym (cdr eshell-sep-terms))) | |
691 | (nreverse subchains)))) | |
692 | ||
693 | ;;_* Command evaluation macros | |
694 | ;; | |
695 | ;; The structure of the following macros is very important to | |
696 | ;; `eshell-do-eval' [Iterative evaluation]: | |
697 | ;; | |
698 | ;; @ Don't use forms that conditionally evaluate their arguments, such | |
699 | ;; as `setq', `if', `while', `let*', etc. The only special forms | |
700 | ;; that can be used are `let', `condition-case' and | |
701 | ;; `unwind-protect'. | |
702 | ;; | |
703 | ;; @ The main body of a `let' can contain only one form. Use `progn' | |
704 | ;; if necessary. | |
705 | ;; | |
706 | ;; @ The two `special' variables are `eshell-current-handles' and | |
707 | ;; `eshell-current-subjob-p'. Bind them locally with a `let' if you | |
708 | ;; need to change them. Change them directly only if your intention | |
709 | ;; is to change the calling environment. | |
710 | ||
711 | (defmacro eshell-do-subjob (object) | |
712 | "Evaluate a command OBJECT as a subjob. | |
6b6f91b3 | 713 | We indicate that the process was run in the background by returning it |
25fffb31 GM |
714 | ensconced in a list." |
715 | `(let ((eshell-current-subjob-p t)) | |
716 | ,object)) | |
717 | ||
718 | (defmacro eshell-commands (object &optional silent) | |
719 | "Place a valid set of handles, and context, around command OBJECT." | |
720 | `(let ((eshell-current-handles | |
721 | (eshell-create-handles ,(not silent) 'append)) | |
722 | eshell-current-subjob-p) | |
723 | ,object)) | |
724 | ||
725 | (defmacro eshell-trap-errors (object) | |
726 | "Trap any errors that occur, so they are not entirely fatal. | |
727 | Also, the variable `eshell-this-command-hook' is available for the | |
728 | duration of OBJECT's evaluation. Note that functions should be added | |
729 | to this hook using `nconc', and *not* `add-hook'. | |
730 | ||
731 | Someday, when Scheme will become the dominant Emacs language, all of | |
732 | this grossness will be made to disappear by using `call/cc'..." | |
1bc4c3ae | 733 | `(let ((eshell-this-command-hook '(ignore))) |
25fffb31 GM |
734 | (eshell-condition-case err |
735 | (prog1 | |
736 | ,object | |
737 | (run-hooks 'eshell-this-command-hook)) | |
738 | (error | |
739 | (run-hooks 'eshell-this-command-hook) | |
740 | (eshell-errorn (error-message-string err)) | |
741 | (eshell-close-handles 1))))) | |
742 | ||
1bc4c3ae SM |
743 | (defvar eshell-output-handle) ;Defined in esh-io.el. |
744 | (defvar eshell-error-handle) ;Defined in esh-io.el. | |
745 | ||
ca7aae91 JW |
746 | (defmacro eshell-copy-handles (object) |
747 | "Duplicate current I/O handles, so OBJECT works with its own copy." | |
748 | `(let ((eshell-current-handles | |
749 | (eshell-create-handles | |
750 | (car (aref eshell-current-handles | |
751 | eshell-output-handle)) nil | |
752 | (car (aref eshell-current-handles | |
753 | eshell-error-handle)) nil))) | |
754 | ,object)) | |
755 | ||
25fffb31 GM |
756 | (defmacro eshell-protect (object) |
757 | "Protect I/O handles, so they aren't get closed after eval'ing OBJECT." | |
758 | `(progn | |
759 | (eshell-protect-handles eshell-current-handles) | |
760 | ,object)) | |
761 | ||
5101a9dc GM |
762 | (defmacro eshell-do-pipelines (pipeline &optional notfirst) |
763 | "Execute the commands in PIPELINE, connecting each to one another. | |
764 | This macro calls itself recursively, with NOTFIRST non-nil." | |
25fffb31 | 765 | (when (setq pipeline (cadr pipeline)) |
ca7aae91 JW |
766 | `(eshell-copy-handles |
767 | (progn | |
768 | ,(when (cdr pipeline) | |
769 | `(let (nextproc) | |
1bc4c3ae SM |
770 | (setq nextproc |
771 | (eshell-do-pipelines (quote ,(cdr pipeline)) t)) | |
772 | (eshell-set-output-handle ,eshell-output-handle | |
773 | 'append nextproc) | |
774 | (eshell-set-output-handle ,eshell-error-handle | |
775 | 'append nextproc) | |
776 | (setq tailproc (or tailproc nextproc)))) | |
ca7aae91 JW |
777 | ,(let ((head (car pipeline))) |
778 | (if (memq (car head) '(let progn)) | |
779 | (setq head (car (last head)))) | |
780 | (when (memq (car head) eshell-deferrable-commands) | |
781 | (ignore | |
782 | (setcar head | |
783 | (intern-soft | |
784 | (concat (symbol-name (car head)) "*")))))) | |
5101a9dc GM |
785 | ;; First and last elements in a pipeline may need special treatment. |
786 | ;; (Currently only eshell-ls-files uses 'last.) | |
787 | ;; Affects process-connection-type in eshell-gather-process-output. | |
788 | (let ((eshell-in-pipeline-p | |
789 | ,(cond ((not notfirst) (quote 'first)) | |
790 | ((cdr pipeline) t) | |
791 | (t (quote 'last))))) | |
3fe3fd2c | 792 | ,(car pipeline)))))) |
ca7aae91 JW |
793 | |
794 | (defmacro eshell-do-pipelines-synchronously (pipeline) | |
795 | "Execute the commands in PIPELINE in sequence synchronously. | |
796 | Output of each command is passed as input to the next one in the pipeline. | |
797 | This is used on systems where `start-process' is not supported." | |
798 | (when (setq pipeline (cadr pipeline)) | |
799 | `(let (result) | |
1bc4c3ae SM |
800 | ,(when (cdr pipeline) |
801 | `(let (output-marker) | |
802 | (setq output-marker ,(point-marker)) | |
803 | (eshell-set-output-handle ,eshell-output-handle | |
804 | 'append output-marker) | |
805 | (eshell-set-output-handle ,eshell-error-handle | |
806 | 'append output-marker))) | |
807 | ,(let ((head (car pipeline))) | |
808 | (if (memq (car head) '(let progn)) | |
809 | (setq head (car (last head)))) | |
810 | ;; FIXME: is deferrable significant here? | |
811 | (when (memq (car head) eshell-deferrable-commands) | |
812 | (ignore | |
813 | (setcar head | |
c1e2f5fa SM |
814 | (intern-soft |
815 | (concat (symbol-name (car head)) "*")))))) | |
816 | ;; The last process in the pipe should get its handles | |
1bc4c3ae SM |
817 | ;; redirected as we found them before running the pipe. |
818 | ,(if (null (cdr pipeline)) | |
819 | `(progn | |
820 | (setq eshell-current-handles tail-handles) | |
821 | (setq eshell-in-pipeline-p nil))) | |
822 | (setq result ,(car pipeline)) | |
823 | ;; tailproc gets the result of the last successful process in | |
824 | ;; the pipeline. | |
825 | (setq tailproc (or result tailproc)) | |
826 | ,(if (cdr pipeline) | |
827 | `(eshell-do-pipelines-synchronously (quote ,(cdr pipeline)))) | |
828 | result))) | |
25fffb31 GM |
829 | |
830 | (defalias 'eshell-process-identity 'identity) | |
831 | ||
832 | (defmacro eshell-execute-pipeline (pipeline) | |
833 | "Execute the commands in PIPELINE, connecting each to one another." | |
834 | `(let ((eshell-in-pipeline-p t) tailproc) | |
835 | (progn | |
ca7aae91 JW |
836 | ,(if (fboundp 'start-process) |
837 | `(eshell-do-pipelines ,pipeline) | |
838 | `(let ((tail-handles (eshell-create-handles | |
839 | (car (aref eshell-current-handles | |
840 | ,eshell-output-handle)) nil | |
841 | (car (aref eshell-current-handles | |
842 | ,eshell-error-handle)) nil))) | |
843 | (eshell-do-pipelines-synchronously ,pipeline))) | |
25fffb31 GM |
844 | (eshell-process-identity tailproc)))) |
845 | ||
846 | (defmacro eshell-as-subcommand (command) | |
847 | "Execute COMMAND using a temp buffer. | |
848 | This is used so that certain Lisp commands, such as `cd', when | |
849 | executed in a subshell, do not disturb the environment of the main | |
850 | Eshell buffer." | |
851 | `(let ,eshell-subcommand-bindings | |
852 | ,command)) | |
853 | ||
854 | (defmacro eshell-do-command-to-value (object) | |
855 | "Run a subcommand prepared by `eshell-command-to-value'. | |
856 | This avoids the need to use `let*'." | |
857 | `(let ((eshell-current-handles | |
858 | (eshell-create-handles value 'overwrite))) | |
859 | (progn | |
860 | ,object | |
861 | (symbol-value value)))) | |
862 | ||
863 | (defmacro eshell-command-to-value (object) | |
864 | "Run OBJECT synchronously, returning its result as a string. | |
865 | Returns a string comprising the output from the command." | |
866 | `(let ((value (make-symbol "eshell-temp"))) | |
867 | (eshell-do-command-to-value ,object))) | |
868 | ||
869 | ;;;_* Iterative evaluation | |
870 | ;; | |
871 | ;; Eshell runs all of its external commands asynchronously, so that | |
872 | ;; Emacs is not blocked while the operation is being performed. | |
873 | ;; However, this introduces certain synchronization difficulties, | |
874 | ;; since the Lisp code, once it returns, will not "go back" to finish | |
875 | ;; executing the commands which haven't yet been started. | |
876 | ;; | |
877 | ;; What Eshell does to work around this problem (basically, the lack | |
878 | ;; of threads in Lisp), is that it evaluates the command sequence | |
879 | ;; iteratively. Whenever an asynchronous process is begun, evaluation | |
880 | ;; terminates and control is given back to Emacs. When that process | |
881 | ;; finishes, it will resume the evaluation using the remainder of the | |
882 | ;; command tree. | |
883 | ||
884 | (defun eshell/eshell-debug (&rest args) | |
885 | "A command for toggling certain debug variables." | |
886 | (ignore | |
887 | (cond | |
888 | ((not args) | |
889 | (if eshell-handle-errors | |
890 | (eshell-print "errors\n")) | |
891 | (if eshell-debug-command | |
892 | (eshell-print "commands\n"))) | |
893 | ((or (string= (car args) "-h") | |
894 | (string= (car args) "--help")) | |
895 | (eshell-print "usage: eshell-debug [kinds] | |
896 | ||
897 | This command is used to aid in debugging problems related to Eshell | |
898 | itself. It is not useful for anything else. The recognized `kinds' | |
899 | at the moment are: | |
900 | ||
901 | errors stops Eshell from trapping errors | |
902 | commands shows command execution progress in `*eshell last cmd*' | |
903 | ")) | |
904 | (t | |
905 | (while args | |
906 | (cond | |
907 | ((string= (car args) "errors") | |
908 | (setq eshell-handle-errors (not eshell-handle-errors))) | |
909 | ((string= (car args) "commands") | |
910 | (setq eshell-debug-command (not eshell-debug-command)))) | |
911 | (setq args (cdr args))))))) | |
912 | ||
913 | (defun pcomplete/eshell-mode/eshell-debug () | |
914 | "Completion for the `debug' command." | |
915 | (while (pcomplete-here '("errors" "commands")))) | |
916 | ||
dace60cf JW |
917 | (defun eshell-invoke-directly (command input) |
918 | (let ((base (cadr (nth 2 (nth 2 (cadr command))))) name) | |
919 | (if (and (eq (car base) 'eshell-trap-errors) | |
920 | (eq (car (cadr base)) 'eshell-named-command)) | |
921 | (setq name (cadr (cadr base)))) | |
922 | (and name (stringp name) | |
923 | (not (member name eshell-complex-commands)) | |
924 | (catch 'simple | |
925 | (progn | |
a9eeff78 | 926 | (dolist (pred eshell-complex-commands) |
dace60cf JW |
927 | (if (and (functionp pred) |
928 | (funcall pred name)) | |
929 | (throw 'simple nil))) | |
930 | t)) | |
931 | (fboundp (intern-soft (concat "eshell/" name)))))) | |
932 | ||
25fffb31 GM |
933 | (defun eshell-eval-command (command &optional input) |
934 | "Evaluate the given COMMAND iteratively." | |
935 | (if eshell-current-command | |
936 | ;; we can just stick the new command at the end of the current | |
937 | ;; one, and everything will happen as it should | |
938 | (setcdr (last (cdr eshell-current-command)) | |
1bc4c3ae SM |
939 | (list `(let ((here (and (eobp) (point)))) |
940 | ,(and input | |
941 | `(insert-and-inherit ,(concat input "\n"))) | |
942 | (if here | |
943 | (eshell-update-markers here)) | |
944 | (eshell-do-eval ',command)))) | |
25fffb31 | 945 | (and eshell-debug-command |
ee5b7365 SM |
946 | (with-current-buffer (get-buffer-create "*eshell last cmd*") |
947 | (erase-buffer) | |
948 | (insert "command: \"" input "\"\n"))) | |
25fffb31 | 949 | (setq eshell-current-command command) |
ca7aae91 JW |
950 | (let ((delim (catch 'eshell-incomplete |
951 | (eshell-resume-eval)))) | |
4c334f5b EZ |
952 | ;; On systems that don't support async subprocesses, eshell-resume |
953 | ;; can return t. Don't treat that as an error. | |
6b6f91b3 JW |
954 | (if (listp delim) |
955 | (setq delim (car delim))) | |
4c334f5b | 956 | (if (and delim (not (eq delim t))) |
6b6f91b3 | 957 | (error "Unmatched delimiter: %c" delim))))) |
25fffb31 GM |
958 | |
959 | (defun eshell-resume-command (proc status) | |
960 | "Resume the current command when a process ends." | |
961 | (when proc | |
ca7aae91 JW |
962 | (unless (or (not (stringp status)) |
963 | (string= "stopped" status) | |
25fffb31 GM |
964 | (string-match eshell-reset-signals status)) |
965 | (if (eq proc (eshell-interactive-process)) | |
966 | (eshell-resume-eval))))) | |
967 | ||
968 | (defun eshell-resume-eval () | |
969 | "Destructively evaluate a form which may need to be deferred." | |
970 | (eshell-condition-case err | |
971 | (progn | |
972 | (setq eshell-last-async-proc nil) | |
973 | (when eshell-current-command | |
974 | (let* (retval | |
975 | (proc (catch 'eshell-defer | |
976 | (ignore | |
977 | (setq retval | |
978 | (eshell-do-eval | |
979 | eshell-current-command)))))) | |
ca7aae91 | 980 | (if (eshell-processp proc) |
25fffb31 GM |
981 | (ignore (setq eshell-last-async-proc proc)) |
982 | (cadr retval))))) | |
983 | (error | |
984 | (error (error-message-string err))))) | |
985 | ||
986 | (defmacro eshell-manipulate (tag &rest commands) | |
987 | "Manipulate a COMMAND form, with TAG as a debug identifier." | |
1bc4c3ae | 988 | (declare (indent 1)) |
ee5b7365 SM |
989 | ;; Check `bound'ness since at compile time the code until here has not |
990 | ;; executed yet. | |
991 | (if (not (and (boundp 'eshell-debug-command) eshell-debug-command)) | |
25fffb31 GM |
992 | `(progn ,@commands) |
993 | `(progn | |
994 | (eshell-debug-command ,(eval tag) form) | |
995 | ,@commands | |
996 | (eshell-debug-command ,(concat "done " (eval tag)) form)))) | |
997 | ||
25fffb31 GM |
998 | (defsubst eshell-macrop (object) |
999 | "Return t if OBJECT is a macro or nil otherwise." | |
1bc4c3ae SM |
1000 | (and (symbolp object) (fboundp object) |
1001 | (setq object (indirect-function object)) | |
1002 | (listp object) | |
1003 | (eq 'macro (car object)) | |
1004 | (functionp (cdr object)))) | |
25fffb31 GM |
1005 | |
1006 | (defun eshell-do-eval (form &optional synchronous-p) | |
1007 | "Evaluate form, simplifying it as we go. | |
1008 | Unless SYNCHRONOUS-P is non-nil, throws `eshell-defer' if it needs to | |
1009 | be finished later after the completion of an asynchronous subprocess." | |
1010 | (cond | |
1011 | ((not (listp form)) | |
1012 | (list 'quote (eval form))) | |
1013 | ((memq (car form) '(quote function)) | |
1014 | form) | |
1015 | (t | |
1016 | ;; skip past the call to `eshell-do-eval' | |
1017 | (when (eq (car form) 'eshell-do-eval) | |
1018 | (setq form (cadr (cadr form)))) | |
1019 | ;; expand any macros directly into the form. This is done so that | |
1020 | ;; we can modify any `let' forms to evaluate only once. | |
1021 | (if (eshell-macrop (car form)) | |
1022 | (let ((exp (eshell-copy-tree (macroexpand form)))) | |
1023 | (eshell-manipulate (format "expanding macro `%s'" | |
1024 | (symbol-name (car form))) | |
1025 | (setcar form (car exp)) | |
1026 | (setcdr form (cdr exp))))) | |
1027 | (let ((args (cdr form))) | |
1028 | (cond | |
1029 | ((eq (car form) 'while) | |
1030 | ;; `eshell-copy-tree' is needed here so that the test argument | |
1031 | ;; doesn't get modified and thus always yield the same result. | |
1032 | (when (car eshell-command-body) | |
1033 | (assert (not synchronous-p)) | |
1034 | (eshell-do-eval (car eshell-command-body)) | |
ca7aae91 JW |
1035 | (setcar eshell-command-body nil) |
1036 | (setcar eshell-test-body nil)) | |
25fffb31 GM |
1037 | (unless (car eshell-test-body) |
1038 | (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
ca7aae91 | 1039 | (while (cadr (eshell-do-eval (car eshell-test-body))) |
c1e2f5fa SM |
1040 | (setcar eshell-command-body |
1041 | (if (cddr args) | |
1042 | `(progn ,@(eshell-copy-tree (cdr args))) | |
1043 | (eshell-copy-tree (cadr args)))) | |
ca7aae91 JW |
1044 | (eshell-do-eval (car eshell-command-body) synchronous-p) |
1045 | (setcar eshell-command-body nil) | |
1046 | (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
25fffb31 GM |
1047 | (setcar eshell-command-body nil)) |
1048 | ((eq (car form) 'if) | |
1049 | ;; `eshell-copy-tree' is needed here so that the test argument | |
1050 | ;; doesn't get modified and thus always yield the same result. | |
ca7aae91 JW |
1051 | (if (car eshell-command-body) |
1052 | (progn | |
1053 | (assert (not synchronous-p)) | |
1054 | (eshell-do-eval (car eshell-command-body))) | |
1055 | (unless (car eshell-test-body) | |
1056 | (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
1bc4c3ae SM |
1057 | (setcar eshell-command-body |
1058 | (eshell-copy-tree | |
1059 | (if (cadr (eshell-do-eval (car eshell-test-body))) | |
1060 | (cadr args) | |
1061 | (car (cddr args))))) | |
ca7aae91 JW |
1062 | (eshell-do-eval (car eshell-command-body) synchronous-p)) |
1063 | (setcar eshell-command-body nil) | |
1064 | (setcar eshell-test-body nil)) | |
25fffb31 GM |
1065 | ((eq (car form) 'setcar) |
1066 | (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)) | |
1067 | (eval form)) | |
1068 | ((eq (car form) 'setcdr) | |
1069 | (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)) | |
1070 | (eval form)) | |
1071 | ((memq (car form) '(let catch condition-case unwind-protect)) | |
1072 | ;; `let', `condition-case' and `unwind-protect' have to be | |
1073 | ;; handled specially, because we only want to call | |
1074 | ;; `eshell-do-eval' on their first form. | |
1075 | ;; | |
1076 | ;; NOTE: This requires obedience by all forms which this | |
1077 | ;; function might encounter, that they do not contain | |
1078 | ;; other special forms. | |
1079 | (if (and (eq (car form) 'let) | |
1080 | (not (eq (car (cadr args)) 'eshell-do-eval))) | |
1081 | (eshell-manipulate "evaluating let args" | |
a9eeff78 | 1082 | (dolist (letarg (car args)) |
25fffb31 GM |
1083 | (if (and (listp letarg) |
1084 | (not (eq (cadr letarg) 'quote))) | |
1085 | (setcdr letarg | |
1086 | (list (eshell-do-eval | |
1087 | (cadr letarg) synchronous-p))))))) | |
1088 | (unless (eq (car form) 'unwind-protect) | |
1089 | (setq args (cdr args))) | |
1090 | (unless (eq (caar args) 'eshell-do-eval) | |
1091 | (eshell-manipulate "handling special form" | |
1bc4c3ae | 1092 | (setcar args `(eshell-do-eval ',(car args) ,synchronous-p)))) |
25fffb31 GM |
1093 | (eval form)) |
1094 | (t | |
1095 | (if (and args (not (memq (car form) '(run-hooks)))) | |
1096 | (eshell-manipulate | |
1097 | (format "evaluating arguments to `%s'" | |
1098 | (symbol-name (car form))) | |
1099 | (while args | |
1100 | (setcar args (eshell-do-eval (car args) synchronous-p)) | |
1101 | (setq args (cdr args))))) | |
1102 | (cond | |
1103 | ((eq (car form) 'progn) | |
1104 | (car (last form))) | |
1105 | ((eq (car form) 'prog1) | |
1106 | (cadr form)) | |
1107 | (t | |
dace60cf JW |
1108 | ;; If a command desire to replace its execution form with |
1109 | ;; another command form, all it needs to do is throw the new | |
1110 | ;; form using the exception tag `eshell-replace-command'. | |
1111 | ;; For example, let's say that the form currently being | |
1112 | ;; eval'd is: | |
1113 | ;; | |
1114 | ;; (eshell-named-command "hello") | |
1115 | ;; | |
1116 | ;; Now, let's assume the 'hello' command is an Eshell alias, | |
1117 | ;; the definition of which yields the command: | |
1118 | ;; | |
1119 | ;; (eshell-named-command "echo" (list "Hello" "world")) | |
1120 | ;; | |
1121 | ;; What the alias code would like to do is simply substitute | |
1122 | ;; the alias form for the original form. To accomplish | |
1123 | ;; this, all it needs to do is to throw the substitution | |
1124 | ;; form with the `eshell-replace-command' tag, and the form | |
1125 | ;; will be replaced within the current command, and | |
1126 | ;; execution will then resume (iteratively) as before. | |
1127 | ;; Thus, aliases can even contain references to asynchronous | |
1128 | ;; sub-commands, and things will still work out as they | |
1129 | ;; should. | |
25fffb31 | 1130 | (let (result new-form) |
25fffb31 GM |
1131 | (if (setq new-form |
1132 | (catch 'eshell-replace-command | |
1133 | (ignore | |
1134 | (setq result (eval form))))) | |
1135 | (progn | |
1136 | (eshell-manipulate "substituting replacement form" | |
1137 | (setcar form (car new-form)) | |
1138 | (setcdr form (cdr new-form))) | |
1139 | (eshell-do-eval form synchronous-p)) | |
1140 | (if (and (memq (car form) eshell-deferrable-commands) | |
1141 | (not eshell-current-subjob-p) | |
1142 | result | |
ca7aae91 | 1143 | (eshell-processp result)) |
25fffb31 GM |
1144 | (if synchronous-p |
1145 | (eshell/wait result) | |
1146 | (eshell-manipulate "inserting ignore form" | |
1147 | (setcar form 'ignore) | |
1148 | (setcdr form nil)) | |
1149 | (throw 'eshell-defer result)) | |
1150 | (list 'quote result)))))))))))) | |
1151 | ||
1152 | ;; command invocation | |
1153 | ||
1154 | (defun eshell/which (command &rest names) | |
1155 | "Identify the COMMAND, and where it is located." | |
a9eeff78 | 1156 | (dolist (name (cons command names)) |
25fffb31 | 1157 | (let (program alias direct) |
94d13633 | 1158 | (if (eq (aref name 0) eshell-explicit-command-char) |
25fffb31 GM |
1159 | (setq name (substring name 1) |
1160 | direct t)) | |
1161 | (if (and (not direct) | |
1162 | (eshell-using-module 'eshell-alias) | |
1163 | (setq alias | |
1164 | (funcall (symbol-function 'eshell-lookup-alias) | |
1165 | name))) | |
1166 | (setq program | |
1167 | (concat name " is an alias, defined as \"" | |
1168 | (cadr alias) "\""))) | |
1169 | (unless program | |
1170 | (setq program (eshell-search-path name)) | |
1171 | (let* ((esym (eshell-find-alias-function name)) | |
1172 | (sym (or esym (intern-soft name)))) | |
ac03c474 JW |
1173 | (if (and (or esym (and sym (fboundp sym))) |
1174 | (or eshell-prefer-lisp-functions (not direct))) | |
25fffb31 GM |
1175 | (let ((desc (let ((inhibit-redisplay t)) |
1176 | (save-window-excursion | |
1177 | (prog1 | |
1178 | (describe-function sym) | |
1179 | (message nil)))))) | |
c1043701 GM |
1180 | (setq desc (if desc (substring desc 0 |
1181 | (1- (or (string-match "\n" desc) | |
1182 | (length desc)))) | |
1183 | ;; This should not happen. | |
1184 | (format "%s is defined, \ | |
1185 | but no documentation was found" name))) | |
ca7aae91 JW |
1186 | (if (buffer-live-p (get-buffer "*Help*")) |
1187 | (kill-buffer "*Help*")) | |
25fffb31 GM |
1188 | (setq program (or desc name)))))) |
1189 | (if (not program) | |
1190 | (eshell-error (format "which: no %s in (%s)\n" | |
1191 | name (getenv "PATH"))) | |
1192 | (eshell-printn program))))) | |
1193 | ||
3cb27fd7 JW |
1194 | (put 'eshell/which 'eshell-no-numeric-conversions t) |
1195 | ||
25fffb31 GM |
1196 | (defun eshell-named-command (command &optional args) |
1197 | "Insert output from a plain COMMAND, using ARGS. | |
1198 | COMMAND may result in an alias being executed, or a plain command." | |
1199 | (setq eshell-last-arguments args | |
1200 | eshell-last-command-name (eshell-stringify command)) | |
1201 | (run-hook-with-args 'eshell-prepare-command-hook) | |
1202 | (assert (stringp eshell-last-command-name)) | |
1203 | (if eshell-last-command-name | |
1204 | (or (run-hook-with-args-until-success | |
1205 | 'eshell-named-command-hook eshell-last-command-name | |
1206 | eshell-last-arguments) | |
1207 | (eshell-plain-command eshell-last-command-name | |
1208 | eshell-last-arguments)))) | |
1209 | ||
1210 | (defalias 'eshell-named-command* 'eshell-named-command) | |
1211 | ||
1212 | (defun eshell-find-alias-function (name) | |
1213 | "Check whether a function called `eshell/NAME' exists." | |
1214 | (let* ((sym (intern-soft (concat "eshell/" name))) | |
5cb345c1 | 1215 | (file (symbol-file sym 'defun))) |
c7b1b508 JW |
1216 | ;; If the function exists, but is defined in an eshell module |
1217 | ;; that's not currently enabled, don't report it as found | |
25fffb31 GM |
1218 | (if (and file |
1219 | (string-match "\\(em\\|esh\\)-\\(.*\\)\\(\\.el\\)?\\'" file)) | |
c7b1b508 | 1220 | (let ((module-sym |
25fffb31 | 1221 | (intern (file-name-sans-extension |
c7b1b508 JW |
1222 | (file-name-nondirectory |
1223 | (concat "eshell-" (match-string 2 file))))))) | |
2e88b53c JW |
1224 | (if (and (functionp sym) |
1225 | (or (null module-sym) | |
1226 | (eshell-using-module module-sym) | |
1227 | (memq module-sym (eshell-subgroups 'eshell)))) | |
c7b1b508 JW |
1228 | sym)) |
1229 | ;; Otherwise, if it's bound, return it. | |
1230 | (if (functionp sym) | |
1231 | sym)))) | |
25fffb31 GM |
1232 | |
1233 | (defun eshell-plain-command (command args) | |
1234 | "Insert output from a plain COMMAND, using ARGS. | |
1235 | COMMAND may result in either a Lisp function being executed by name, | |
1236 | or an external command." | |
1237 | (let* ((esym (eshell-find-alias-function command)) | |
1238 | (sym (or esym (intern-soft command)))) | |
1239 | (if (and sym (fboundp sym) | |
1240 | (or esym eshell-prefer-lisp-functions | |
1241 | (not (eshell-search-path command)))) | |
1242 | (eshell-lisp-command sym args) | |
1243 | (eshell-external-command command args)))) | |
1244 | ||
1245 | (defun eshell-exec-lisp (printer errprint func-or-form args form-p) | |
1246 | "Execute a lisp FUNC-OR-FORM, maybe passing ARGS. | |
1247 | PRINTER and ERRPRINT are functions to use for printing regular | |
1248 | messages, and errors. FORM-P should be non-nil if FUNC-OR-FORM | |
1249 | represent a lisp form; ARGS will be ignored in that case." | |
1250 | (let (result) | |
1251 | (eshell-condition-case err | |
1252 | (progn | |
1253 | (setq result | |
1254 | (save-current-buffer | |
1255 | (if form-p | |
1256 | (eval func-or-form) | |
1257 | (apply func-or-form args)))) | |
1258 | (and result (funcall printer result)) | |
1259 | result) | |
1260 | (error | |
1261 | (let ((msg (error-message-string err))) | |
1262 | (if (and (not form-p) | |
1263 | (string-match "^Wrong number of arguments" msg) | |
1264 | (fboundp 'eldoc-get-fnsym-args-string)) | |
1265 | (let ((func-doc (eldoc-get-fnsym-args-string func-or-form))) | |
1266 | (setq msg (format "usage: %s" func-doc)))) | |
1267 | (funcall errprint msg)) | |
1268 | nil)))) | |
1269 | ||
1270 | (defsubst eshell-apply* (printer errprint func args) | |
1271 | "Call FUNC, with ARGS, trapping errors and return them as output. | |
1272 | PRINTER and ERRPRINT are functions to use for printing regular | |
1273 | messages, and errors." | |
1274 | (eshell-exec-lisp printer errprint func args nil)) | |
1275 | ||
1276 | (defsubst eshell-funcall* (printer errprint func &rest args) | |
1277 | "Call FUNC, with ARGS, trapping errors and return them as output." | |
1278 | (eshell-apply* printer errprint func args)) | |
1279 | ||
1280 | (defsubst eshell-eval* (printer errprint form) | |
1281 | "Evaluate FORM, trapping errors and returning them." | |
1282 | (eshell-exec-lisp printer errprint form nil t)) | |
1283 | ||
1284 | (defsubst eshell-apply (func args) | |
1285 | "Call FUNC, with ARGS, trapping errors and return them as output. | |
1286 | PRINTER and ERRPRINT are functions to use for printing regular | |
1287 | messages, and errors." | |
1288 | (eshell-apply* 'eshell-print 'eshell-error func args)) | |
1289 | ||
1290 | (defsubst eshell-funcall (func &rest args) | |
1291 | "Call FUNC, with ARGS, trapping errors and return them as output." | |
1292 | (eshell-apply func args)) | |
1293 | ||
1294 | (defsubst eshell-eval (form) | |
1295 | "Evaluate FORM, trapping errors and returning them." | |
1296 | (eshell-eval* 'eshell-print 'eshell-error form)) | |
1297 | ||
1298 | (defsubst eshell-applyn (func args) | |
1299 | "Call FUNC, with ARGS, trapping errors and return them as output. | |
1300 | PRINTER and ERRPRINT are functions to use for printing regular | |
1301 | messages, and errors." | |
1302 | (eshell-apply* 'eshell-printn 'eshell-errorn func args)) | |
1303 | ||
1304 | (defsubst eshell-funcalln (func &rest args) | |
1305 | "Call FUNC, with ARGS, trapping errors and return them as output." | |
1306 | (eshell-applyn func args)) | |
1307 | ||
1308 | (defsubst eshell-evaln (form) | |
1309 | "Evaluate FORM, trapping errors and returning them." | |
1310 | (eshell-eval* 'eshell-printn 'eshell-errorn form)) | |
1311 | ||
1bc4c3ae SM |
1312 | (defvar eshell-last-output-end) ;Defined in esh-mode.el. |
1313 | ||
25fffb31 GM |
1314 | (defun eshell-lisp-command (object &optional args) |
1315 | "Insert Lisp OBJECT, using ARGS if a function." | |
25fffb31 GM |
1316 | (catch 'eshell-external ; deferred to an external command |
1317 | (let* ((eshell-ensure-newline-p (eshell-interactive-output-p)) | |
1318 | (result | |
1319 | (if (functionp object) | |
3cb27fd7 JW |
1320 | (progn |
1321 | (setq eshell-last-arguments args | |
1322 | eshell-last-command-name | |
1323 | (concat "#<function " (symbol-name object) ">")) | |
1324 | ;; if any of the arguments are flagged as numbers | |
1325 | ;; waiting for conversion, convert them now | |
1326 | (unless (get object 'eshell-no-numeric-conversions) | |
1327 | (while args | |
1328 | (let ((arg (car args))) | |
1329 | (if (and (stringp arg) | |
1330 | (> (length arg) 0) | |
175acc2d JW |
1331 | (not (text-property-not-all |
1332 | 0 (length arg) 'number t arg))) | |
ea7974a6 | 1333 | (setcar args (string-to-number arg)))) |
3cb27fd7 JW |
1334 | (setq args (cdr args)))) |
1335 | (eshell-apply object eshell-last-arguments)) | |
1336 | (setq eshell-last-arguments args | |
1337 | eshell-last-command-name "#<Lisp object>") | |
25fffb31 GM |
1338 | (eshell-eval object)))) |
1339 | (if (and eshell-ensure-newline-p | |
1340 | (save-excursion | |
1341 | (goto-char eshell-last-output-end) | |
1342 | (not (bolp)))) | |
1343 | (eshell-print "\n")) | |
1344 | (eshell-close-handles 0 (list 'quote result))))) | |
1345 | ||
1346 | (defalias 'eshell-lisp-command* 'eshell-lisp-command) | |
1347 | ||
56590d2f GM |
1348 | (provide 'esh-cmd) |
1349 | ||
25fffb31 | 1350 | ;;; esh-cmd.el ends here |