Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; esh-cmd.el --- command invocation |
25fffb31 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1999-2013 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 | |
a464a6c7 | 111 | (require 'cl-lib) |
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 | |
beb83b5a GM |
239 | "If non-nil, enable Eshell debugging code. |
240 | This is slow, and only useful for debugging problems with Eshell. | |
241 | If you change this without using customize after Eshell has loaded, | |
aac7ba08 | 242 | you must re-load 'esh-cmd.el'." |
beb83b5a GM |
243 | :initialize 'custom-initialize-default |
244 | :set (lambda (symbol value) | |
b499d8d0 | 245 | (set symbol value) |
beb83b5a | 246 | (load-library "esh-cmd")) |
25fffb31 GM |
247 | :type 'boolean |
248 | :group 'eshell-cmd) | |
249 | ||
250 | (defcustom eshell-deferrable-commands | |
251 | '(eshell-named-command | |
252 | eshell-lisp-command | |
253 | eshell-process-identity) | |
91af3942 | 254 | "A list of functions which might return an asynchronous process. |
25fffb31 GM |
255 | If they return a process object, execution of the calling Eshell |
256 | command will wait for completion (in the background) before finishing | |
257 | the command." | |
258 | :type '(repeat function) | |
259 | :group 'eshell-cmd) | |
260 | ||
261 | (defcustom eshell-subcommand-bindings | |
262 | '((eshell-in-subcommand-p t) | |
263 | (default-directory default-directory) | |
264 | (process-environment (eshell-copy-environment))) | |
ec60da52 | 265 | "A list of `let' bindings for subcommand environments." |
25fffb31 GM |
266 | :type 'sexp |
267 | :group 'eshell-cmd) | |
268 | ||
269 | (put 'risky-local-variable 'eshell-subcommand-bindings t) | |
270 | ||
271 | (defvar eshell-ensure-newline-p nil | |
272 | "If non-nil, ensure that a newline is emitted after a Lisp form. | |
273 | This can be changed by Lisp forms that are evaluated from the Eshell | |
274 | command line.") | |
275 | ||
276 | ;;; Internal Variables: | |
277 | ||
278 | (defvar eshell-current-command nil) | |
279 | (defvar eshell-command-name nil) | |
280 | (defvar eshell-command-arguments nil) | |
5101a9dc GM |
281 | (defvar eshell-in-pipeline-p nil |
282 | "Internal Eshell variable, non-nil inside a pipeline. | |
283 | Has the value 'first, 'last for the first/last commands in the pipeline, | |
284 | otherwise t.") | |
25fffb31 GM |
285 | (defvar eshell-in-subcommand-p nil) |
286 | (defvar eshell-last-arguments nil) | |
287 | (defvar eshell-last-command-name nil) | |
288 | (defvar eshell-last-async-proc nil | |
289 | "When this foreground process completes, resume command evaluation.") | |
290 | ||
291 | ;;; Functions: | |
292 | ||
293 | (defsubst eshell-interactive-process () | |
294 | "Return currently running command process, if non-Lisp." | |
295 | eshell-last-async-proc) | |
296 | ||
297 | (defun eshell-cmd-initialize () | |
298 | "Initialize the Eshell command processing module." | |
299 | (set (make-local-variable 'eshell-current-command) nil) | |
300 | (set (make-local-variable 'eshell-command-name) nil) | |
301 | (set (make-local-variable 'eshell-command-arguments) nil) | |
302 | (set (make-local-variable 'eshell-last-arguments) nil) | |
303 | (set (make-local-variable 'eshell-last-command-name) nil) | |
304 | (set (make-local-variable 'eshell-last-async-proc) nil) | |
305 | ||
25fffb31 GM |
306 | (add-hook 'eshell-kill-hook 'eshell-resume-command nil t) |
307 | ||
308 | ;; make sure that if a command is over, and no process is being | |
309 | ;; waited for, that `eshell-current-command' is set to nil. This | |
310 | ;; situation can occur, for example, if a Lisp function results in | |
311 | ;; `debug' being called, and the user then types \\[top-level] | |
25fffb31 GM |
312 | (add-hook 'eshell-post-command-hook |
313 | (function | |
314 | (lambda () | |
315 | (setq eshell-current-command nil | |
316 | eshell-last-async-proc nil))) nil t) | |
317 | ||
25fffb31 GM |
318 | (add-hook 'eshell-parse-argument-hook |
319 | 'eshell-parse-subcommand-argument nil t) | |
320 | (add-hook 'eshell-parse-argument-hook | |
321 | 'eshell-parse-lisp-argument nil t) | |
322 | ||
323 | (when (eshell-using-module 'eshell-cmpl) | |
25fffb31 GM |
324 | (add-hook 'pcomplete-try-first-hook |
325 | 'eshell-complete-lisp-symbols nil t))) | |
326 | ||
25fffb31 GM |
327 | (defun eshell-complete-lisp-symbols () |
328 | "If there is a user reference, complete it." | |
329 | (let ((arg (pcomplete-actual-arg))) | |
330 | (when (string-match (concat "\\`" eshell-lisp-regexp) arg) | |
331 | (setq pcomplete-stub (substring arg (match-end 0)) | |
332 | pcomplete-last-completion-raw t) | |
333 | (throw 'pcomplete-completions | |
334 | (all-completions pcomplete-stub obarray 'boundp))))) | |
335 | ||
336 | ;; Command parsing | |
337 | ||
338 | (defun eshell-parse-command (command &optional args top-level) | |
339 | "Parse the COMMAND, adding ARGS if given. | |
340 | COMMAND can either be a string, or a cons cell demarcating a buffer | |
341 | region. TOP-LEVEL, if non-nil, means that the outermost command (the | |
342 | user's input command) is being parsed, and that pre and post command | |
343 | hooks should be run before and after the command." | |
344 | (let* (sep-terms | |
345 | (terms | |
346 | (append | |
347 | (if (consp command) | |
348 | (eshell-parse-arguments (car command) (cdr command)) | |
349 | (let ((here (point)) | |
6e13206c SM |
350 | (inhibit-point-motion-hooks t)) |
351 | (with-silent-modifications | |
352 | ;; FIXME: Why not use a temporary buffer and avoid this | |
353 | ;; "insert&delete" business? --Stef | |
354 | (insert command) | |
355 | (prog1 | |
356 | (eshell-parse-arguments here (point)) | |
357 | (delete-region here (point)))))) | |
25fffb31 GM |
358 | args)) |
359 | (commands | |
360 | (mapcar | |
361 | (function | |
362 | (lambda (cmd) | |
1bc4c3ae SM |
363 | (setq cmd |
364 | (if (or (not (car sep-terms)) | |
365 | (string= (car sep-terms) ";")) | |
366 | (eshell-parse-pipeline cmd (not (car sep-terms))) | |
367 | `(eshell-do-subjob | |
368 | (list ,(eshell-parse-pipeline cmd))))) | |
25fffb31 GM |
369 | (setq sep-terms (cdr sep-terms)) |
370 | (if eshell-in-pipeline-p | |
371 | cmd | |
1bc4c3ae | 372 | `(eshell-trap-errors ,cmd)))) |
25fffb31 GM |
373 | (eshell-separate-commands terms "[&;]" nil 'sep-terms)))) |
374 | (let ((cmd commands)) | |
375 | (while cmd | |
376 | (if (cdr cmd) | |
1bc4c3ae | 377 | (setcar cmd `(eshell-commands ,(car cmd)))) |
25fffb31 GM |
378 | (setq cmd (cdr cmd)))) |
379 | (setq commands | |
1bc4c3ae SM |
380 | `(progn |
381 | ,@(if top-level | |
382 | '((run-hooks 'eshell-pre-command-hook))) | |
383 | ,@(if (not top-level) | |
384 | commands | |
385 | `((catch 'top-level (progn ,@commands)) | |
386 | (run-hooks 'eshell-post-command-hook))))) | |
25fffb31 | 387 | (if top-level |
1bc4c3ae | 388 | `(eshell-commands ,commands) |
25fffb31 GM |
389 | commands))) |
390 | ||
56590d2f GM |
391 | (defun eshell-debug-command (tag subform) |
392 | "Output a debugging message to '*eshell last cmd*'." | |
393 | (let ((buf (get-buffer-create "*eshell last cmd*")) | |
394 | (text (eshell-stringify eshell-current-command))) | |
ee5b7365 | 395 | (with-current-buffer buf |
56590d2f GM |
396 | (if (not tag) |
397 | (erase-buffer) | |
398 | (insert "\n\C-l\n" tag "\n\n" text | |
399 | (if subform | |
400 | (concat "\n\n" (eshell-stringify subform)) "")))))) | |
401 | ||
25fffb31 GM |
402 | (defun eshell-debug-show-parsed-args (terms) |
403 | "Display parsed arguments in the debug buffer." | |
404 | (ignore | |
405 | (if eshell-debug-command | |
406 | (eshell-debug-command "parsed arguments" terms)))) | |
407 | ||
408 | (defun eshell-no-command-conversion (terms) | |
409 | "Don't convert the command argument." | |
410 | (ignore | |
411 | (if (and (listp (car terms)) | |
412 | (eq (caar terms) 'eshell-convert)) | |
413 | (setcar terms (cadr (car terms)))))) | |
414 | ||
415 | (defun eshell-subcommand-arg-values (terms) | |
416 | "Convert subcommand arguments {x} to ${x}, in order to take their values." | |
417 | (setq terms (cdr terms)) ; skip command argument | |
418 | (while terms | |
419 | (if (and (listp (car terms)) | |
420 | (eq (caar terms) 'eshell-as-subcommand)) | |
1bc4c3ae SM |
421 | (setcar terms `(eshell-convert |
422 | (eshell-command-to-value ,(car terms))))) | |
25fffb31 GM |
423 | (setq terms (cdr terms)))) |
424 | ||
425 | (defun eshell-rewrite-sexp-command (terms) | |
426 | "Rewrite a sexp in initial position, such as '(+ 1 2)'." | |
427 | ;; this occurs when a Lisp expression is in first position | |
428 | (if (and (listp (car terms)) | |
429 | (eq (caar terms) 'eshell-command-to-value)) | |
430 | (car (cdar terms)))) | |
431 | ||
25fffb31 GM |
432 | (defun eshell-rewrite-initial-subcommand (terms) |
433 | "Rewrite a subcommand in initial position, such as '{+ 1 2}'." | |
434 | (if (and (listp (car terms)) | |
435 | (eq (caar terms) 'eshell-as-subcommand)) | |
436 | (car terms))) | |
437 | ||
25fffb31 GM |
438 | (defun eshell-rewrite-named-command (terms) |
439 | "If no other rewriting rule transforms TERMS, assume a named command." | |
f4dd1361 JW |
440 | (let ((sym (if eshell-in-pipeline-p |
441 | 'eshell-named-command* | |
442 | 'eshell-named-command)) | |
443 | (cmd (car terms)) | |
444 | (args (cdr terms))) | |
445 | (if args | |
1bc4c3ae | 446 | (list sym cmd `(list ,@(cdr terms))) |
f4dd1361 | 447 | (list sym cmd)))) |
25fffb31 | 448 | |
1a32899d GM |
449 | (defvar eshell-command-body) |
450 | (defvar eshell-test-body) | |
25fffb31 GM |
451 | |
452 | (defsubst eshell-invokify-arg (arg &optional share-output silent) | |
453 | "Change ARG so it can be invoked from a structured command. | |
454 | ||
455 | SHARE-OUTPUT, if non-nil, means this invocation should share the | |
456 | current output stream, which is separately redirectable. SILENT | |
457 | means the user and/or any redirections shouldn't see any output | |
458 | from this command. If both SHARE-OUTPUT and SILENT are non-nil, | |
459 | the second is ignored." | |
460 | ;; something that begins with `eshell-convert' means that it | |
461 | ;; intends to return a Lisp value. We want to get past this, | |
462 | ;; but if it's not _actually_ a value interpolation -- in which | |
463 | ;; we leave it alone. In fact, the only time we muck with it | |
464 | ;; is in the case of a {subcommand} that has been turned into | |
465 | ;; the interpolation, ${subcommand}, by the parser because it | |
466 | ;; didn't know better. | |
467 | (if (and (listp arg) | |
468 | (eq (car arg) 'eshell-convert) | |
469 | (eq (car (cadr arg)) 'eshell-command-to-value)) | |
470 | (if share-output | |
471 | (cadr (cadr arg)) | |
1bc4c3ae | 472 | `(eshell-commands ,(cadr (cadr arg)) ,silent)) |
25fffb31 GM |
473 | arg)) |
474 | ||
1bc4c3ae SM |
475 | (defvar eshell-last-command-status) ;Define in esh-io.el. |
476 | ||
25fffb31 GM |
477 | (defun eshell-rewrite-for-command (terms) |
478 | "Rewrite a `for' command into its equivalent Eshell command form. | |
479 | Because the implementation of `for' relies upon conditional evaluation | |
31096fe8 | 480 | of its argument (i.e., use of a Lisp special form), it must be |
25fffb31 | 481 | implemented via rewriting, rather than as a function." |
1bc4c3ae SM |
482 | (if (and (equal (car terms) "for") |
483 | (equal (nth 2 terms) "in")) | |
25fffb31 GM |
484 | (let ((body (car (last terms)))) |
485 | (setcdr (last terms 2) nil) | |
1bc4c3ae | 486 | `(let ((for-items |
045ef729 CY |
487 | (copy-tree |
488 | (append | |
489 | ,@(mapcar | |
490 | (lambda (elem) | |
491 | (if (listp elem) | |
492 | elem | |
493 | `(list ,elem))) | |
494 | (cdr (cddr terms)))))) | |
495 | (eshell-command-body '(nil)) | |
1bc4c3ae | 496 | (eshell-test-body '(nil))) |
045ef729 CY |
497 | (while (car for-items) |
498 | (let ((,(intern (cadr terms)) (car for-items))) | |
499 | (eshell-protect | |
500 | ,(eshell-invokify-arg body t))) | |
501 | (setcar for-items (cadr for-items)) | |
502 | (setcdr for-items (cddr for-items))) | |
1bc4c3ae SM |
503 | (eshell-close-handles |
504 | eshell-last-command-status | |
505 | (list 'quote eshell-last-command-result)))))) | |
25fffb31 GM |
506 | |
507 | (defun eshell-structure-basic-command (func names keyword test body | |
508 | &optional else vocal-test) | |
509 | "With TERMS, KEYWORD, and two NAMES, structure a basic command. | |
510 | The first of NAMES should be the positive form, and the second the | |
511 | negative. It's not likely that users should ever need to call this | |
512 | function. | |
513 | ||
514 | If VOCAL-TEST is non-nil, it means output from the test should be | |
515 | shown, as well as output from the body." | |
516 | ;; If the test form begins with `eshell-convert', it means | |
517 | ;; something data-wise will be returned, and we should let | |
518 | ;; that determine the truth of the statement. | |
519 | (unless (eq (car test) 'eshell-convert) | |
520 | (setq test | |
1bc4c3ae SM |
521 | `(progn ,test |
522 | (eshell-exit-success-p)))) | |
25fffb31 GM |
523 | |
524 | ;; should we reverse the sense of the test? This depends | |
525 | ;; on the `names' parameter. If it's the symbol nil, yes. | |
526 | ;; Otherwise, it can be a pair of strings; if the keyword | |
527 | ;; we're using matches the second member of that pair (a | |
528 | ;; list), we should reverse it. | |
529 | (if (or (eq names nil) | |
530 | (and (listp names) | |
531 | (string= keyword (cadr names)))) | |
1bc4c3ae | 532 | (setq test `(not ,test))) |
25fffb31 GM |
533 | |
534 | ;; finally, create the form that represents this structured | |
535 | ;; command | |
1bc4c3ae SM |
536 | `(let ((eshell-command-body '(nil)) |
537 | (eshell-test-body '(nil))) | |
538 | (,func ,test ,body ,else) | |
539 | (eshell-close-handles | |
540 | eshell-last-command-status | |
541 | (list 'quote eshell-last-command-result)))) | |
25fffb31 GM |
542 | |
543 | (defun eshell-rewrite-while-command (terms) | |
544 | "Rewrite a `while' command into its equivalent Eshell command form. | |
545 | Because the implementation of `while' relies upon conditional | |
546 | evaluation of its argument (i.e., use of a Lisp special form), it | |
547 | must be implemented via rewriting, rather than as a function." | |
548 | (if (and (stringp (car terms)) | |
549 | (member (car terms) '("while" "until"))) | |
550 | (eshell-structure-basic-command | |
551 | 'while '("while" "until") (car terms) | |
552 | (eshell-invokify-arg (cadr terms) nil t) | |
1bc4c3ae SM |
553 | `(eshell-protect |
554 | ,(eshell-invokify-arg (car (last terms)) t))))) | |
25fffb31 GM |
555 | |
556 | (defun eshell-rewrite-if-command (terms) | |
557 | "Rewrite an `if' command into its equivalent Eshell command form. | |
558 | Because the implementation of `if' relies upon conditional | |
559 | evaluation of its argument (i.e., use of a Lisp special form), it | |
560 | must be implemented via rewriting, rather than as a function." | |
561 | (if (and (stringp (car terms)) | |
562 | (member (car terms) '("if" "unless"))) | |
563 | (eshell-structure-basic-command | |
564 | 'if '("if" "unless") (car terms) | |
565 | (eshell-invokify-arg (cadr terms) nil t) | |
1bc4c3ae SM |
566 | `(eshell-protect |
567 | ,(eshell-invokify-arg (car (last terms (if (= (length terms) 4) 2))) | |
568 | t)) | |
05a68572 | 569 | (if (= (length terms) 4) |
1bc4c3ae SM |
570 | `(eshell-protect |
571 | ,(eshell-invokify-arg (car (last terms)))) t)))) | |
572 | ||
573 | (defvar eshell-last-command-result) ;Defined in esh-io.el. | |
25fffb31 GM |
574 | |
575 | (defun eshell-exit-success-p () | |
576 | "Return non-nil if the last command was \"successful\". | |
577 | For a bit of Lisp code, this means a return value of non-nil. | |
578 | For an external command, it means an exit code of 0." | |
05a68572 JW |
579 | (if (save-match-data |
580 | (string-match "#<\\(Lisp object\\|function .*\\)>" | |
581 | eshell-last-command-name)) | |
25fffb31 GM |
582 | eshell-last-command-result |
583 | (= eshell-last-command-status 0))) | |
584 | ||
585 | (defun eshell-parse-pipeline (terms &optional final-p) | |
586 | "Parse a pipeline from TERMS, return the appropriate Lisp forms." | |
587 | (let* (sep-terms | |
588 | (bigpieces (eshell-separate-commands terms "\\(&&\\|||\\)" | |
589 | nil 'sep-terms)) | |
590 | (bp bigpieces) | |
591 | (results (list t)) | |
592 | final) | |
593 | (while bp | |
594 | (let ((subterms (car bp))) | |
595 | (let* ((pieces (eshell-separate-commands subterms "|")) | |
596 | (p pieces)) | |
597 | (while p | |
598 | (let ((cmd (car p))) | |
599 | (run-hook-with-args 'eshell-pre-rewrite-command-hook cmd) | |
600 | (setq cmd (run-hook-with-args-until-success | |
601 | 'eshell-rewrite-command-hook cmd)) | |
602 | (run-hook-with-args 'eshell-post-rewrite-command-hook 'cmd) | |
603 | (setcar p cmd)) | |
604 | (setq p (cdr p))) | |
605 | (nconc results | |
606 | (list | |
607 | (if (<= (length pieces) 1) | |
608 | (car pieces) | |
a464a6c7 | 609 | (cl-assert (not eshell-in-pipeline-p)) |
1bc4c3ae | 610 | `(eshell-execute-pipeline (quote ,pieces)))))) |
25fffb31 GM |
611 | (setq bp (cdr bp)))) |
612 | ;; `results' might be empty; this happens in the case of | |
613 | ;; multi-line input | |
614 | (setq results (cdr results) | |
615 | results (nreverse results) | |
616 | final (car results) | |
617 | results (cdr results) | |
618 | sep-terms (nreverse sep-terms)) | |
619 | (while results | |
a464a6c7 | 620 | (cl-assert (car sep-terms)) |
25fffb31 GM |
621 | (setq final (eshell-structure-basic-command |
622 | 'if (string= (car sep-terms) "&&") "if" | |
1bc4c3ae SM |
623 | `(eshell-protect ,(car results)) |
624 | `(eshell-protect ,final) | |
25fffb31 GM |
625 | nil t) |
626 | results (cdr results) | |
627 | sep-terms (cdr sep-terms))) | |
628 | final)) | |
629 | ||
630 | (defun eshell-parse-subcommand-argument () | |
631 | "Parse a subcommand argument of the form '{command}'." | |
632 | (if (and (not eshell-current-argument) | |
633 | (not eshell-current-quoted) | |
634 | (eq (char-after) ?\{) | |
635 | (or (= (point-max) (1+ (point))) | |
636 | (not (eq (char-after (1+ (point))) ?\})))) | |
637 | (let ((end (eshell-find-delimiter ?\{ ?\}))) | |
638 | (if (not end) | |
639 | (throw 'eshell-incomplete ?\{) | |
640 | (when (eshell-arg-delimiter (1+ end)) | |
641 | (prog1 | |
1bc4c3ae SM |
642 | `(eshell-as-subcommand |
643 | ,(eshell-parse-command (cons (1+ (point)) end))) | |
25fffb31 GM |
644 | (goto-char (1+ end)))))))) |
645 | ||
646 | (defun eshell-parse-lisp-argument () | |
647 | "Parse a Lisp expression which is specified as an argument." | |
648 | (if (and (not eshell-current-argument) | |
649 | (not eshell-current-quoted) | |
650 | (looking-at eshell-lisp-regexp)) | |
651 | (let* ((here (point)) | |
652 | (obj | |
170266d0 | 653 | (condition-case nil |
25fffb31 GM |
654 | (read (current-buffer)) |
655 | (end-of-file | |
656 | (throw 'eshell-incomplete ?\())))) | |
657 | (if (eshell-arg-delimiter) | |
1bc4c3ae SM |
658 | `(eshell-command-to-value |
659 | (eshell-lisp-command (quote ,obj))) | |
25fffb31 GM |
660 | (ignore (goto-char here)))))) |
661 | ||
dace60cf JW |
662 | (defun eshell-separate-commands (terms separator &optional |
663 | reversed last-terms-sym) | |
25fffb31 GM |
664 | "Separate TERMS using SEPARATOR. |
665 | If REVERSED is non-nil, the list of separated term groups will be | |
d4469517 | 666 | returned in reverse order. If LAST-TERMS-SYM is a symbol, its value |
25fffb31 GM |
667 | will be set to a list of all the separator operators found (or '(list |
668 | nil)' if none)." | |
669 | (let ((sub-terms (list t)) | |
670 | (eshell-sep-terms (list t)) | |
671 | subchains) | |
672 | (while terms | |
673 | (if (and (consp (car terms)) | |
674 | (eq (caar terms) 'eshell-operator) | |
675 | (string-match (concat "^" separator "$") | |
676 | (nth 1 (car terms)))) | |
677 | (progn | |
678 | (nconc eshell-sep-terms (list (nth 1 (car terms)))) | |
679 | (setq subchains (cons (cdr sub-terms) subchains) | |
680 | sub-terms (list t))) | |
681 | (nconc sub-terms (list (car terms)))) | |
682 | (setq terms (cdr terms))) | |
683 | (if (> (length sub-terms) 1) | |
684 | (setq subchains (cons (cdr sub-terms) subchains))) | |
685 | (if reversed | |
686 | (progn | |
687 | (if last-terms-sym | |
688 | (set last-terms-sym (reverse (cdr eshell-sep-terms)))) | |
689 | subchains) ; already reversed | |
690 | (if last-terms-sym | |
691 | (set last-terms-sym (cdr eshell-sep-terms))) | |
692 | (nreverse subchains)))) | |
693 | ||
694 | ;;_* Command evaluation macros | |
695 | ;; | |
696 | ;; The structure of the following macros is very important to | |
697 | ;; `eshell-do-eval' [Iterative evaluation]: | |
698 | ;; | |
699 | ;; @ Don't use forms that conditionally evaluate their arguments, such | |
700 | ;; as `setq', `if', `while', `let*', etc. The only special forms | |
701 | ;; that can be used are `let', `condition-case' and | |
702 | ;; `unwind-protect'. | |
703 | ;; | |
704 | ;; @ The main body of a `let' can contain only one form. Use `progn' | |
705 | ;; if necessary. | |
706 | ;; | |
707 | ;; @ The two `special' variables are `eshell-current-handles' and | |
708 | ;; `eshell-current-subjob-p'. Bind them locally with a `let' if you | |
709 | ;; need to change them. Change them directly only if your intention | |
710 | ;; is to change the calling environment. | |
711 | ||
712 | (defmacro eshell-do-subjob (object) | |
713 | "Evaluate a command OBJECT as a subjob. | |
6b6f91b3 | 714 | We indicate that the process was run in the background by returning it |
25fffb31 GM |
715 | ensconced in a list." |
716 | `(let ((eshell-current-subjob-p t)) | |
717 | ,object)) | |
718 | ||
719 | (defmacro eshell-commands (object &optional silent) | |
720 | "Place a valid set of handles, and context, around command OBJECT." | |
721 | `(let ((eshell-current-handles | |
722 | (eshell-create-handles ,(not silent) 'append)) | |
723 | eshell-current-subjob-p) | |
724 | ,object)) | |
725 | ||
726 | (defmacro eshell-trap-errors (object) | |
727 | "Trap any errors that occur, so they are not entirely fatal. | |
728 | Also, the variable `eshell-this-command-hook' is available for the | |
729 | duration of OBJECT's evaluation. Note that functions should be added | |
730 | to this hook using `nconc', and *not* `add-hook'. | |
731 | ||
732 | Someday, when Scheme will become the dominant Emacs language, all of | |
733 | this grossness will be made to disappear by using `call/cc'..." | |
1bc4c3ae | 734 | `(let ((eshell-this-command-hook '(ignore))) |
25fffb31 GM |
735 | (eshell-condition-case err |
736 | (prog1 | |
737 | ,object | |
738 | (run-hooks 'eshell-this-command-hook)) | |
739 | (error | |
740 | (run-hooks 'eshell-this-command-hook) | |
741 | (eshell-errorn (error-message-string err)) | |
742 | (eshell-close-handles 1))))) | |
743 | ||
1bc4c3ae SM |
744 | (defvar eshell-output-handle) ;Defined in esh-io.el. |
745 | (defvar eshell-error-handle) ;Defined in esh-io.el. | |
746 | ||
ca7aae91 JW |
747 | (defmacro eshell-copy-handles (object) |
748 | "Duplicate current I/O handles, so OBJECT works with its own copy." | |
749 | `(let ((eshell-current-handles | |
750 | (eshell-create-handles | |
751 | (car (aref eshell-current-handles | |
752 | eshell-output-handle)) nil | |
753 | (car (aref eshell-current-handles | |
754 | eshell-error-handle)) nil))) | |
755 | ,object)) | |
756 | ||
25fffb31 GM |
757 | (defmacro eshell-protect (object) |
758 | "Protect I/O handles, so they aren't get closed after eval'ing OBJECT." | |
759 | `(progn | |
760 | (eshell-protect-handles eshell-current-handles) | |
761 | ,object)) | |
762 | ||
5101a9dc GM |
763 | (defmacro eshell-do-pipelines (pipeline &optional notfirst) |
764 | "Execute the commands in PIPELINE, connecting each to one another. | |
765 | This macro calls itself recursively, with NOTFIRST non-nil." | |
25fffb31 | 766 | (when (setq pipeline (cadr pipeline)) |
ca7aae91 JW |
767 | `(eshell-copy-handles |
768 | (progn | |
769 | ,(when (cdr pipeline) | |
6dbe3e96 SM |
770 | `(let ((nextproc |
771 | (eshell-do-pipelines (quote ,(cdr pipeline)) t))) | |
1bc4c3ae SM |
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)) | |
6dbe3e96 | 799 | `(progn |
1bc4c3ae | 800 | ,(when (cdr pipeline) |
6dbe3e96 | 801 | `(let ((output-marker ,(point-marker))) |
1bc4c3ae SM |
802 | (eshell-set-output-handle ,eshell-output-handle |
803 | 'append output-marker) | |
804 | (eshell-set-output-handle ,eshell-error-handle | |
805 | 'append output-marker))) | |
806 | ,(let ((head (car pipeline))) | |
807 | (if (memq (car head) '(let progn)) | |
808 | (setq head (car (last head)))) | |
809 | ;; FIXME: is deferrable significant here? | |
810 | (when (memq (car head) eshell-deferrable-commands) | |
811 | (ignore | |
812 | (setcar head | |
6dbe3e96 SM |
813 | (intern-soft |
814 | (concat (symbol-name (car head)) "*")))))) | |
815 | ;; The last process in the pipe should get its handles | |
1bc4c3ae SM |
816 | ;; redirected as we found them before running the pipe. |
817 | ,(if (null (cdr pipeline)) | |
818 | `(progn | |
819 | (setq eshell-current-handles tail-handles) | |
820 | (setq eshell-in-pipeline-p nil))) | |
6dbe3e96 SM |
821 | (let ((result ,(car pipeline))) |
822 | ;; tailproc gets the result of the last successful process in | |
823 | ;; the pipeline. | |
824 | (setq tailproc (or result tailproc)) | |
825 | ,(if (cdr pipeline) | |
826 | `(eshell-do-pipelines-synchronously (quote ,(cdr pipeline)))) | |
827 | result)))) | |
25fffb31 GM |
828 | |
829 | (defalias 'eshell-process-identity 'identity) | |
830 | ||
831 | (defmacro eshell-execute-pipeline (pipeline) | |
832 | "Execute the commands in PIPELINE, connecting each to one another." | |
833 | `(let ((eshell-in-pipeline-p t) tailproc) | |
834 | (progn | |
ca7aae91 JW |
835 | ,(if (fboundp 'start-process) |
836 | `(eshell-do-pipelines ,pipeline) | |
837 | `(let ((tail-handles (eshell-create-handles | |
838 | (car (aref eshell-current-handles | |
839 | ,eshell-output-handle)) nil | |
840 | (car (aref eshell-current-handles | |
841 | ,eshell-error-handle)) nil))) | |
842 | (eshell-do-pipelines-synchronously ,pipeline))) | |
25fffb31 GM |
843 | (eshell-process-identity tailproc)))) |
844 | ||
845 | (defmacro eshell-as-subcommand (command) | |
846 | "Execute COMMAND using a temp buffer. | |
847 | This is used so that certain Lisp commands, such as `cd', when | |
848 | executed in a subshell, do not disturb the environment of the main | |
849 | Eshell buffer." | |
850 | `(let ,eshell-subcommand-bindings | |
851 | ,command)) | |
852 | ||
853 | (defmacro eshell-do-command-to-value (object) | |
854 | "Run a subcommand prepared by `eshell-command-to-value'. | |
855 | This avoids the need to use `let*'." | |
856 | `(let ((eshell-current-handles | |
857 | (eshell-create-handles value 'overwrite))) | |
858 | (progn | |
859 | ,object | |
860 | (symbol-value value)))) | |
861 | ||
862 | (defmacro eshell-command-to-value (object) | |
863 | "Run OBJECT synchronously, returning its result as a string. | |
864 | Returns a string comprising the output from the command." | |
865 | `(let ((value (make-symbol "eshell-temp"))) | |
866 | (eshell-do-command-to-value ,object))) | |
867 | ||
868 | ;;;_* Iterative evaluation | |
869 | ;; | |
870 | ;; Eshell runs all of its external commands asynchronously, so that | |
871 | ;; Emacs is not blocked while the operation is being performed. | |
872 | ;; However, this introduces certain synchronization difficulties, | |
873 | ;; since the Lisp code, once it returns, will not "go back" to finish | |
874 | ;; executing the commands which haven't yet been started. | |
875 | ;; | |
876 | ;; What Eshell does to work around this problem (basically, the lack | |
877 | ;; of threads in Lisp), is that it evaluates the command sequence | |
878 | ;; iteratively. Whenever an asynchronous process is begun, evaluation | |
879 | ;; terminates and control is given back to Emacs. When that process | |
880 | ;; finishes, it will resume the evaluation using the remainder of the | |
881 | ;; command tree. | |
882 | ||
883 | (defun eshell/eshell-debug (&rest args) | |
884 | "A command for toggling certain debug variables." | |
885 | (ignore | |
886 | (cond | |
887 | ((not args) | |
888 | (if eshell-handle-errors | |
889 | (eshell-print "errors\n")) | |
890 | (if eshell-debug-command | |
891 | (eshell-print "commands\n"))) | |
6dbe3e96 | 892 | ((member (car args) '("-h" "--help")) |
25fffb31 GM |
893 | (eshell-print "usage: eshell-debug [kinds] |
894 | ||
895 | This command is used to aid in debugging problems related to Eshell | |
896 | itself. It is not useful for anything else. The recognized `kinds' | |
897 | at the moment are: | |
898 | ||
899 | errors stops Eshell from trapping errors | |
900 | commands shows command execution progress in `*eshell last cmd*' | |
901 | ")) | |
902 | (t | |
903 | (while args | |
904 | (cond | |
905 | ((string= (car args) "errors") | |
906 | (setq eshell-handle-errors (not eshell-handle-errors))) | |
907 | ((string= (car args) "commands") | |
908 | (setq eshell-debug-command (not eshell-debug-command)))) | |
909 | (setq args (cdr args))))))) | |
910 | ||
911 | (defun pcomplete/eshell-mode/eshell-debug () | |
912 | "Completion for the `debug' command." | |
913 | (while (pcomplete-here '("errors" "commands")))) | |
914 | ||
dace60cf JW |
915 | (defun eshell-invoke-directly (command input) |
916 | (let ((base (cadr (nth 2 (nth 2 (cadr command))))) name) | |
917 | (if (and (eq (car base) 'eshell-trap-errors) | |
918 | (eq (car (cadr base)) 'eshell-named-command)) | |
919 | (setq name (cadr (cadr base)))) | |
920 | (and name (stringp name) | |
921 | (not (member name eshell-complex-commands)) | |
922 | (catch 'simple | |
923 | (progn | |
a9eeff78 | 924 | (dolist (pred eshell-complex-commands) |
dace60cf JW |
925 | (if (and (functionp pred) |
926 | (funcall pred name)) | |
927 | (throw 'simple nil))) | |
928 | t)) | |
929 | (fboundp (intern-soft (concat "eshell/" name)))))) | |
930 | ||
25fffb31 GM |
931 | (defun eshell-eval-command (command &optional input) |
932 | "Evaluate the given COMMAND iteratively." | |
933 | (if eshell-current-command | |
934 | ;; we can just stick the new command at the end of the current | |
935 | ;; one, and everything will happen as it should | |
936 | (setcdr (last (cdr eshell-current-command)) | |
1bc4c3ae SM |
937 | (list `(let ((here (and (eobp) (point)))) |
938 | ,(and input | |
939 | `(insert-and-inherit ,(concat input "\n"))) | |
940 | (if here | |
941 | (eshell-update-markers here)) | |
942 | (eshell-do-eval ',command)))) | |
25fffb31 | 943 | (and eshell-debug-command |
ee5b7365 SM |
944 | (with-current-buffer (get-buffer-create "*eshell last cmd*") |
945 | (erase-buffer) | |
946 | (insert "command: \"" input "\"\n"))) | |
25fffb31 | 947 | (setq eshell-current-command command) |
ca7aae91 JW |
948 | (let ((delim (catch 'eshell-incomplete |
949 | (eshell-resume-eval)))) | |
4c334f5b EZ |
950 | ;; On systems that don't support async subprocesses, eshell-resume |
951 | ;; can return t. Don't treat that as an error. | |
6b6f91b3 JW |
952 | (if (listp delim) |
953 | (setq delim (car delim))) | |
4c334f5b | 954 | (if (and delim (not (eq delim t))) |
6b6f91b3 | 955 | (error "Unmatched delimiter: %c" delim))))) |
25fffb31 GM |
956 | |
957 | (defun eshell-resume-command (proc status) | |
958 | "Resume the current command when a process ends." | |
959 | (when proc | |
ca7aae91 JW |
960 | (unless (or (not (stringp status)) |
961 | (string= "stopped" status) | |
25fffb31 GM |
962 | (string-match eshell-reset-signals status)) |
963 | (if (eq proc (eshell-interactive-process)) | |
964 | (eshell-resume-eval))))) | |
965 | ||
966 | (defun eshell-resume-eval () | |
967 | "Destructively evaluate a form which may need to be deferred." | |
968 | (eshell-condition-case err | |
969 | (progn | |
970 | (setq eshell-last-async-proc nil) | |
971 | (when eshell-current-command | |
972 | (let* (retval | |
973 | (proc (catch 'eshell-defer | |
974 | (ignore | |
975 | (setq retval | |
976 | (eshell-do-eval | |
977 | eshell-current-command)))))) | |
ca7aae91 | 978 | (if (eshell-processp proc) |
25fffb31 GM |
979 | (ignore (setq eshell-last-async-proc proc)) |
980 | (cadr retval))))) | |
981 | (error | |
982 | (error (error-message-string err))))) | |
983 | ||
984 | (defmacro eshell-manipulate (tag &rest commands) | |
985 | "Manipulate a COMMAND form, with TAG as a debug identifier." | |
1bc4c3ae | 986 | (declare (indent 1)) |
ee5b7365 SM |
987 | ;; Check `bound'ness since at compile time the code until here has not |
988 | ;; executed yet. | |
989 | (if (not (and (boundp 'eshell-debug-command) eshell-debug-command)) | |
25fffb31 GM |
990 | `(progn ,@commands) |
991 | `(progn | |
992 | (eshell-debug-command ,(eval tag) form) | |
993 | ,@commands | |
994 | (eshell-debug-command ,(concat "done " (eval tag)) form)))) | |
995 | ||
25fffb31 GM |
996 | (defun eshell-do-eval (form &optional synchronous-p) |
997 | "Evaluate form, simplifying it as we go. | |
998 | Unless SYNCHRONOUS-P is non-nil, throws `eshell-defer' if it needs to | |
999 | be finished later after the completion of an asynchronous subprocess." | |
1000 | (cond | |
1001 | ((not (listp form)) | |
1002 | (list 'quote (eval form))) | |
1003 | ((memq (car form) '(quote function)) | |
1004 | form) | |
1005 | (t | |
1006 | ;; skip past the call to `eshell-do-eval' | |
1007 | (when (eq (car form) 'eshell-do-eval) | |
1008 | (setq form (cadr (cadr form)))) | |
1009 | ;; expand any macros directly into the form. This is done so that | |
1010 | ;; we can modify any `let' forms to evaluate only once. | |
671d5c16 | 1011 | (if (macrop (car form)) |
25fffb31 GM |
1012 | (let ((exp (eshell-copy-tree (macroexpand form)))) |
1013 | (eshell-manipulate (format "expanding macro `%s'" | |
1014 | (symbol-name (car form))) | |
1015 | (setcar form (car exp)) | |
1016 | (setcdr form (cdr exp))))) | |
1017 | (let ((args (cdr form))) | |
1018 | (cond | |
1019 | ((eq (car form) 'while) | |
1020 | ;; `eshell-copy-tree' is needed here so that the test argument | |
1021 | ;; doesn't get modified and thus always yield the same result. | |
1022 | (when (car eshell-command-body) | |
a464a6c7 | 1023 | (cl-assert (not synchronous-p)) |
25fffb31 | 1024 | (eshell-do-eval (car eshell-command-body)) |
ca7aae91 JW |
1025 | (setcar eshell-command-body nil) |
1026 | (setcar eshell-test-body nil)) | |
25fffb31 GM |
1027 | (unless (car eshell-test-body) |
1028 | (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
ca7aae91 | 1029 | (while (cadr (eshell-do-eval (car eshell-test-body))) |
c1e2f5fa SM |
1030 | (setcar eshell-command-body |
1031 | (if (cddr args) | |
1032 | `(progn ,@(eshell-copy-tree (cdr args))) | |
1033 | (eshell-copy-tree (cadr args)))) | |
ca7aae91 JW |
1034 | (eshell-do-eval (car eshell-command-body) synchronous-p) |
1035 | (setcar eshell-command-body nil) | |
1036 | (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
25fffb31 GM |
1037 | (setcar eshell-command-body nil)) |
1038 | ((eq (car form) 'if) | |
1039 | ;; `eshell-copy-tree' is needed here so that the test argument | |
1040 | ;; doesn't get modified and thus always yield the same result. | |
ca7aae91 JW |
1041 | (if (car eshell-command-body) |
1042 | (progn | |
a464a6c7 | 1043 | (cl-assert (not synchronous-p)) |
ca7aae91 JW |
1044 | (eshell-do-eval (car eshell-command-body))) |
1045 | (unless (car eshell-test-body) | |
1046 | (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
1bc4c3ae SM |
1047 | (setcar eshell-command-body |
1048 | (eshell-copy-tree | |
1049 | (if (cadr (eshell-do-eval (car eshell-test-body))) | |
1050 | (cadr args) | |
1051 | (car (cddr args))))) | |
ca7aae91 JW |
1052 | (eshell-do-eval (car eshell-command-body) synchronous-p)) |
1053 | (setcar eshell-command-body nil) | |
1054 | (setcar eshell-test-body nil)) | |
25fffb31 GM |
1055 | ((eq (car form) 'setcar) |
1056 | (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)) | |
1057 | (eval form)) | |
1058 | ((eq (car form) 'setcdr) | |
1059 | (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)) | |
1060 | (eval form)) | |
1061 | ((memq (car form) '(let catch condition-case unwind-protect)) | |
1062 | ;; `let', `condition-case' and `unwind-protect' have to be | |
1063 | ;; handled specially, because we only want to call | |
1064 | ;; `eshell-do-eval' on their first form. | |
1065 | ;; | |
1066 | ;; NOTE: This requires obedience by all forms which this | |
1067 | ;; function might encounter, that they do not contain | |
1068 | ;; other special forms. | |
1069 | (if (and (eq (car form) 'let) | |
1070 | (not (eq (car (cadr args)) 'eshell-do-eval))) | |
1071 | (eshell-manipulate "evaluating let args" | |
a9eeff78 | 1072 | (dolist (letarg (car args)) |
25fffb31 GM |
1073 | (if (and (listp letarg) |
1074 | (not (eq (cadr letarg) 'quote))) | |
1075 | (setcdr letarg | |
1076 | (list (eshell-do-eval | |
1077 | (cadr letarg) synchronous-p))))))) | |
1078 | (unless (eq (car form) 'unwind-protect) | |
1079 | (setq args (cdr args))) | |
1080 | (unless (eq (caar args) 'eshell-do-eval) | |
1081 | (eshell-manipulate "handling special form" | |
1bc4c3ae | 1082 | (setcar args `(eshell-do-eval ',(car args) ,synchronous-p)))) |
25fffb31 | 1083 | (eval form)) |
6dbe3e96 SM |
1084 | ((eq (car form) 'setq) |
1085 | (if (cddr args) (error "Unsupported form (setq X1 E1 X2 E2..)")) | |
1086 | (eshell-manipulate "evaluating arguments to setq" | |
1087 | (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p))) | |
1088 | (list 'quote (eval form))) | |
25fffb31 GM |
1089 | (t |
1090 | (if (and args (not (memq (car form) '(run-hooks)))) | |
1091 | (eshell-manipulate | |
1092 | (format "evaluating arguments to `%s'" | |
1093 | (symbol-name (car form))) | |
1094 | (while args | |
1095 | (setcar args (eshell-do-eval (car args) synchronous-p)) | |
1096 | (setq args (cdr args))))) | |
1097 | (cond | |
1098 | ((eq (car form) 'progn) | |
1099 | (car (last form))) | |
1100 | ((eq (car form) 'prog1) | |
1101 | (cadr form)) | |
1102 | (t | |
dace60cf JW |
1103 | ;; If a command desire to replace its execution form with |
1104 | ;; another command form, all it needs to do is throw the new | |
1105 | ;; form using the exception tag `eshell-replace-command'. | |
1106 | ;; For example, let's say that the form currently being | |
1107 | ;; eval'd is: | |
1108 | ;; | |
1109 | ;; (eshell-named-command "hello") | |
1110 | ;; | |
1111 | ;; Now, let's assume the 'hello' command is an Eshell alias, | |
1112 | ;; the definition of which yields the command: | |
1113 | ;; | |
1114 | ;; (eshell-named-command "echo" (list "Hello" "world")) | |
1115 | ;; | |
1116 | ;; What the alias code would like to do is simply substitute | |
1117 | ;; the alias form for the original form. To accomplish | |
1118 | ;; this, all it needs to do is to throw the substitution | |
1119 | ;; form with the `eshell-replace-command' tag, and the form | |
1120 | ;; will be replaced within the current command, and | |
1121 | ;; execution will then resume (iteratively) as before. | |
1122 | ;; Thus, aliases can even contain references to asynchronous | |
1123 | ;; sub-commands, and things will still work out as they | |
1124 | ;; should. | |
6dbe3e96 SM |
1125 | (let* (result |
1126 | (new-form | |
1127 | (catch 'eshell-replace-command | |
1128 | (ignore | |
1129 | (setq result (eval form)))))) | |
1130 | (if new-form | |
25fffb31 GM |
1131 | (progn |
1132 | (eshell-manipulate "substituting replacement form" | |
1133 | (setcar form (car new-form)) | |
1134 | (setcdr form (cdr new-form))) | |
1135 | (eshell-do-eval form synchronous-p)) | |
1136 | (if (and (memq (car form) eshell-deferrable-commands) | |
1137 | (not eshell-current-subjob-p) | |
1138 | result | |
ca7aae91 | 1139 | (eshell-processp result)) |
25fffb31 GM |
1140 | (if synchronous-p |
1141 | (eshell/wait result) | |
1142 | (eshell-manipulate "inserting ignore form" | |
1143 | (setcar form 'ignore) | |
1144 | (setcdr form nil)) | |
1145 | (throw 'eshell-defer result)) | |
1146 | (list 'quote result)))))))))))) | |
1147 | ||
1148 | ;; command invocation | |
1149 | ||
1150 | (defun eshell/which (command &rest names) | |
1151 | "Identify the COMMAND, and where it is located." | |
a9eeff78 | 1152 | (dolist (name (cons command names)) |
25fffb31 | 1153 | (let (program alias direct) |
94d13633 | 1154 | (if (eq (aref name 0) eshell-explicit-command-char) |
25fffb31 GM |
1155 | (setq name (substring name 1) |
1156 | direct t)) | |
1157 | (if (and (not direct) | |
1158 | (eshell-using-module 'eshell-alias) | |
1159 | (setq alias | |
1160 | (funcall (symbol-function 'eshell-lookup-alias) | |
1161 | name))) | |
1162 | (setq program | |
1163 | (concat name " is an alias, defined as \"" | |
1164 | (cadr alias) "\""))) | |
1165 | (unless program | |
1166 | (setq program (eshell-search-path name)) | |
1167 | (let* ((esym (eshell-find-alias-function name)) | |
1168 | (sym (or esym (intern-soft name)))) | |
ac03c474 JW |
1169 | (if (and (or esym (and sym (fboundp sym))) |
1170 | (or eshell-prefer-lisp-functions (not direct))) | |
25fffb31 GM |
1171 | (let ((desc (let ((inhibit-redisplay t)) |
1172 | (save-window-excursion | |
1173 | (prog1 | |
1174 | (describe-function sym) | |
1175 | (message nil)))))) | |
c1043701 GM |
1176 | (setq desc (if desc (substring desc 0 |
1177 | (1- (or (string-match "\n" desc) | |
1178 | (length desc)))) | |
1179 | ;; This should not happen. | |
1180 | (format "%s is defined, \ | |
1181 | but no documentation was found" name))) | |
ca7aae91 JW |
1182 | (if (buffer-live-p (get-buffer "*Help*")) |
1183 | (kill-buffer "*Help*")) | |
25fffb31 GM |
1184 | (setq program (or desc name)))))) |
1185 | (if (not program) | |
1186 | (eshell-error (format "which: no %s in (%s)\n" | |
1187 | name (getenv "PATH"))) | |
1188 | (eshell-printn program))))) | |
1189 | ||
3cb27fd7 JW |
1190 | (put 'eshell/which 'eshell-no-numeric-conversions t) |
1191 | ||
25fffb31 GM |
1192 | (defun eshell-named-command (command &optional args) |
1193 | "Insert output from a plain COMMAND, using ARGS. | |
1194 | COMMAND may result in an alias being executed, or a plain command." | |
1195 | (setq eshell-last-arguments args | |
1196 | eshell-last-command-name (eshell-stringify command)) | |
1197 | (run-hook-with-args 'eshell-prepare-command-hook) | |
a464a6c7 | 1198 | (cl-assert (stringp eshell-last-command-name)) |
25fffb31 GM |
1199 | (if eshell-last-command-name |
1200 | (or (run-hook-with-args-until-success | |
1201 | 'eshell-named-command-hook eshell-last-command-name | |
1202 | eshell-last-arguments) | |
1203 | (eshell-plain-command eshell-last-command-name | |
1204 | eshell-last-arguments)))) | |
1205 | ||
1206 | (defalias 'eshell-named-command* 'eshell-named-command) | |
1207 | ||
1208 | (defun eshell-find-alias-function (name) | |
1209 | "Check whether a function called `eshell/NAME' exists." | |
1210 | (let* ((sym (intern-soft (concat "eshell/" name))) | |
5cb345c1 | 1211 | (file (symbol-file sym 'defun))) |
c7b1b508 | 1212 | ;; If the function exists, but is defined in an eshell module |
9a48e168 | 1213 | ;; that's not currently enabled, don't report it as found. |
25fffb31 | 1214 | (if (and file |
9a48e168 GM |
1215 | (setq file (file-name-base file)) |
1216 | (string-match "\\`\\(em\\|esh\\)-\\([[:alnum:]]+\\)\\'" file)) | |
c7b1b508 | 1217 | (let ((module-sym |
9a48e168 | 1218 | (intern (concat "eshell-" (match-string 2 file))))) |
2e88b53c JW |
1219 | (if (and (functionp sym) |
1220 | (or (null module-sym) | |
1221 | (eshell-using-module module-sym) | |
1222 | (memq module-sym (eshell-subgroups 'eshell)))) | |
c7b1b508 JW |
1223 | sym)) |
1224 | ;; Otherwise, if it's bound, return it. | |
1225 | (if (functionp sym) | |
1226 | sym)))) | |
25fffb31 GM |
1227 | |
1228 | (defun eshell-plain-command (command args) | |
1229 | "Insert output from a plain COMMAND, using ARGS. | |
1230 | COMMAND may result in either a Lisp function being executed by name, | |
1231 | or an external command." | |
1232 | (let* ((esym (eshell-find-alias-function command)) | |
1233 | (sym (or esym (intern-soft command)))) | |
1234 | (if (and sym (fboundp sym) | |
1235 | (or esym eshell-prefer-lisp-functions | |
1236 | (not (eshell-search-path command)))) | |
1237 | (eshell-lisp-command sym args) | |
1238 | (eshell-external-command command args)))) | |
1239 | ||
1240 | (defun eshell-exec-lisp (printer errprint func-or-form args form-p) | |
1241 | "Execute a lisp FUNC-OR-FORM, maybe passing ARGS. | |
1242 | PRINTER and ERRPRINT are functions to use for printing regular | |
1243 | messages, and errors. FORM-P should be non-nil if FUNC-OR-FORM | |
1244 | represent a lisp form; ARGS will be ignored in that case." | |
6dbe3e96 SM |
1245 | (eshell-condition-case err |
1246 | (let ((result | |
1247 | (save-current-buffer | |
1248 | (if form-p | |
1249 | (eval func-or-form) | |
1250 | (apply func-or-form args))))) | |
1251 | (and result (funcall printer result)) | |
1252 | result) | |
1253 | (error | |
1254 | (let ((msg (error-message-string err))) | |
1255 | (if (and (not form-p) | |
1256 | (string-match "^Wrong number of arguments" msg) | |
1257 | (fboundp 'eldoc-get-fnsym-args-string)) | |
1258 | (let ((func-doc (eldoc-get-fnsym-args-string func-or-form))) | |
1259 | (setq msg (format "usage: %s" func-doc)))) | |
1260 | (funcall errprint msg)) | |
1261 | nil))) | |
25fffb31 GM |
1262 | |
1263 | (defsubst eshell-apply* (printer errprint func args) | |
1264 | "Call FUNC, with ARGS, trapping errors and return them as output. | |
1265 | PRINTER and ERRPRINT are functions to use for printing regular | |
1266 | messages, and errors." | |
1267 | (eshell-exec-lisp printer errprint func args nil)) | |
1268 | ||
1269 | (defsubst eshell-funcall* (printer errprint func &rest args) | |
1270 | "Call FUNC, with ARGS, trapping errors and return them as output." | |
1271 | (eshell-apply* printer errprint func args)) | |
1272 | ||
1273 | (defsubst eshell-eval* (printer errprint form) | |
1274 | "Evaluate FORM, trapping errors and returning them." | |
1275 | (eshell-exec-lisp printer errprint form nil t)) | |
1276 | ||
1277 | (defsubst eshell-apply (func args) | |
1278 | "Call FUNC, with ARGS, trapping errors and return them as output. | |
1279 | PRINTER and ERRPRINT are functions to use for printing regular | |
1280 | messages, and errors." | |
1281 | (eshell-apply* 'eshell-print 'eshell-error func args)) | |
1282 | ||
1283 | (defsubst eshell-funcall (func &rest args) | |
1284 | "Call FUNC, with ARGS, trapping errors and return them as output." | |
1285 | (eshell-apply func args)) | |
1286 | ||
1287 | (defsubst eshell-eval (form) | |
1288 | "Evaluate FORM, trapping errors and returning them." | |
1289 | (eshell-eval* 'eshell-print 'eshell-error form)) | |
1290 | ||
1291 | (defsubst eshell-applyn (func args) | |
1292 | "Call FUNC, with ARGS, trapping errors and return them as output. | |
1293 | PRINTER and ERRPRINT are functions to use for printing regular | |
1294 | messages, and errors." | |
1295 | (eshell-apply* 'eshell-printn 'eshell-errorn func args)) | |
1296 | ||
1297 | (defsubst eshell-funcalln (func &rest args) | |
1298 | "Call FUNC, with ARGS, trapping errors and return them as output." | |
1299 | (eshell-applyn func args)) | |
1300 | ||
1301 | (defsubst eshell-evaln (form) | |
1302 | "Evaluate FORM, trapping errors and returning them." | |
1303 | (eshell-eval* 'eshell-printn 'eshell-errorn form)) | |
1304 | ||
1bc4c3ae SM |
1305 | (defvar eshell-last-output-end) ;Defined in esh-mode.el. |
1306 | ||
25fffb31 GM |
1307 | (defun eshell-lisp-command (object &optional args) |
1308 | "Insert Lisp OBJECT, using ARGS if a function." | |
25fffb31 GM |
1309 | (catch 'eshell-external ; deferred to an external command |
1310 | (let* ((eshell-ensure-newline-p (eshell-interactive-output-p)) | |
1311 | (result | |
1312 | (if (functionp object) | |
3cb27fd7 JW |
1313 | (progn |
1314 | (setq eshell-last-arguments args | |
1315 | eshell-last-command-name | |
1316 | (concat "#<function " (symbol-name object) ">")) | |
1317 | ;; if any of the arguments are flagged as numbers | |
1318 | ;; waiting for conversion, convert them now | |
1319 | (unless (get object 'eshell-no-numeric-conversions) | |
1320 | (while args | |
1321 | (let ((arg (car args))) | |
1322 | (if (and (stringp arg) | |
1323 | (> (length arg) 0) | |
175acc2d JW |
1324 | (not (text-property-not-all |
1325 | 0 (length arg) 'number t arg))) | |
ea7974a6 | 1326 | (setcar args (string-to-number arg)))) |
3cb27fd7 JW |
1327 | (setq args (cdr args)))) |
1328 | (eshell-apply object eshell-last-arguments)) | |
1329 | (setq eshell-last-arguments args | |
1330 | eshell-last-command-name "#<Lisp object>") | |
25fffb31 GM |
1331 | (eshell-eval object)))) |
1332 | (if (and eshell-ensure-newline-p | |
1333 | (save-excursion | |
1334 | (goto-char eshell-last-output-end) | |
1335 | (not (bolp)))) | |
1336 | (eshell-print "\n")) | |
1337 | (eshell-close-handles 0 (list 'quote result))))) | |
1338 | ||
1339 | (defalias 'eshell-lisp-command* 'eshell-lisp-command) | |
1340 | ||
56590d2f GM |
1341 | (provide 'esh-cmd) |
1342 | ||
25fffb31 | 1343 | ;;; esh-cmd.el ends here |