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