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