1 ;;; esh-cmd --- command invocation
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
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)
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.
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.
24 (eval-when-compile (require 'esh-maint))
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,
30 :tag "Command invocation"
31 :link '(info-link "(eshell)Command invocation")
36 ;;;_* Invoking external commands
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].
42 ;;;_* Invoking Lisp functions
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:
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
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.
64 (defcustom eshell-prefer-lisp-functions nil
65 "*If non-nil, prefer Lisp functions to external commands."
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).
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.
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:
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:
98 ;; echo x$(+ 1 2) "String $(+ 1 2)"
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
104 ;; echo #'lisp-symbol
106 ;; Backquote can also be used:
108 ;; echo `(list ,lisp-symbol)
110 ;; Lisp arguments are identified using the following regexp:
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."
120 ;; There are several hooks involved with command execution, which can
121 ;; be used either to change or augment Eshell's behavior.
123 (defcustom eshell-pre-command-hook nil
124 "*A hook run before each interactive command is invoked."
128 (defcustom eshell-post-command-hook nil
129 "*A hook run after each interactive command is invoked."
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.
139 To prevent a command from executing at all, set
140 `eshell-last-command-name' to nil."
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'.
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'.
153 In order to substitute an alternate command form for execution, the
154 hook function should throw it using the tag `eshell-replace-command'.
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)))
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."
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."
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.
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
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."
207 (unless (eshell-under-xemacs-p)
215 (defcustom eshell-cmd-load-hook '(eshell-cmd-initialize)
216 "*A hook that gets run when `eshell-cmd' is loaded."
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
228 (defcustom eshell-deferrable-commands
229 '(eshell-named-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
236 :type '(repeat function)
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."
247 (put 'risky-local-variable 'eshell-subcommand-bindings t)
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
254 ;;; Internal Variables:
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.")
268 (defsubst eshell-interactive-process ()
269 "Return currently running command process, if non-Lisp."
270 eshell-last-async-proc)
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)
281 (make-local-hook 'eshell-kill-hook)
282 (add-hook 'eshell-kill-hook 'eshell-resume-command nil t)
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
292 (setq eshell-current-command nil
293 eshell-last-async-proc nil))) nil t)
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)
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)))
306 (eshell-deftest var last-result-var
307 "\"last result\" variable"
308 (eshell-command-result-p "+ 1 2; + $$ 2" "3\n5\n"))
310 (eshell-deftest var last-result-var2
311 "\"last result\" variable"
312 (eshell-command-result-p "+ 1 2; + $$ $$" "3\n6\n"))
314 (eshell-deftest var last-arg-var
315 "\"last arg\" variable"
316 (eshell-command-result-p "+ 1 2; + $_ 4" "3\n6\n"))
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)))))
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."
339 (eshell-parse-arguments (car command) (cdr command))
341 (inhibit-point-motion-hooks t)
342 after-change-functions)
345 (eshell-parse-arguments here (point))
346 (delete-region here (point)))))
352 (if (or (not (car sep-terms))
353 (string= (car sep-terms) ";"))
355 (eshell-parse-pipeline cmd (not (car sep-terms))))
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
362 (list 'eshell-trap-errors cmd))))
363 (eshell-separate-commands terms "[&;]" nil 'sep-terms))))
364 (let ((cmd commands))
367 (setcar cmd (list 'eshell-commands (car cmd))))
368 (setq cmd (cdr cmd))))
370 (append (list 'progn)
372 (list '(run-hooks 'eshell-pre-command-hook)))
376 (list 'catch (quote 'top-level)
377 (append (list 'progn) commands))
378 '(run-hooks 'eshell-post-command-hook)))))
380 (list 'eshell-commands commands)
383 (defun eshell-debug-show-parsed-args (terms)
384 "Display parsed arguments in the debug buffer."
386 (if eshell-debug-command
387 (eshell-debug-command "parsed arguments" terms))))
389 (defun eshell-no-command-conversion (terms)
390 "Don't convert the command argument."
392 (if (and (listp (car terms))
393 (eq (caar terms) 'eshell-convert))
394 (setcar terms (cadr (car terms))))))
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
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
405 (setq terms (cdr terms))))
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))
414 (eshell-deftest cmd lisp-command
415 "Evaluate Lisp command"
416 (eshell-command-result-p "(+ 1 2)" "3"))
418 (eshell-deftest cmd lisp-command-args
419 "Evaluate Lisp command (ignore args)"
420 (eshell-command-result-p "(+ 1 2) 3" "3"))
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))
428 (eshell-deftest cmd subcommand
430 (eshell-command-result-p "{+ 1 2}" "3\n"))
432 (eshell-deftest cmd subcommand-args
433 "Run subcommand (ignore args)"
434 (eshell-command-result-p "{+ 1 2} 3" "3\n"))
436 (eshell-deftest cmd subcommand-lisp
437 "Run subcommand + Lisp form"
438 (eshell-command-result-p "{(+ 1 2)}" "3\n"))
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)
447 (append (list 'list) (cdr terms)))))
449 (eshell-deftest cmd named-command
450 "Execute named command"
451 (eshell-command-result-p "+ 1 2" "3\n"))
454 (defvar eshell-command-body)
455 (defvar eshell-test-body))
457 (defsubst eshell-invokify-arg (arg &optional share-output silent)
458 "Change ARG so it can be invoked from a structured command.
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.
473 (eq (car arg) 'eshell-convert)
474 (eq (car (cadr arg)) 'eshell-command-to-value))
477 (list 'eshell-commands (cadr (cadr arg))
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)
493 'let (list (list 'for-items
502 (cdr (cddr terms)))))
503 (list 'eshell-command-body
504 (list 'quote (list nil)))
505 (list 'eshell-test-body
506 (list 'quote (list nil))))
510 'while (list 'car (list 'symbol-value
511 (list 'quote 'for-items)))
515 (list (list (intern (cadr terms))
518 (list 'quote 'for-items)))))
519 (list 'eshell-copy-handles
520 (eshell-invokify-arg body t)))
521 (list 'setcar 'for-items
524 (list 'quote 'for-items))))
525 (list 'setcdr 'for-items
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)))))))
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
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)
549 (list 'eshell-exit-success-p))))
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)
558 (string= keyword (cadr names))))
559 (setq test (list 'not test)))
561 ;; finally, create the form that represents this structured
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))))
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)))))
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)
598 (if (= (length terms) 5)
600 (car (last terms))) t)
602 (if (= (length terms) 5)
603 (car (last terms))) t))))
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)))
613 (defun eshell-parse-pipeline (terms &optional final-p)
614 "Parse a pipeline from TERMS, return the appropriate Lisp forms."
616 (bigpieces (eshell-separate-commands terms "\\(&&\\|||\\)"
622 (let ((subterms (car bp)))
623 (let* ((pieces (eshell-separate-commands subterms "|"))
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)
635 (if (<= (length pieces) 1)
637 (assert (not eshell-in-pipeline-p))
638 (list 'eshell-execute-pipeline
639 (list 'quote pieces))))))
641 ;; `results' might be empty; this happens in the case of
643 (setq results (cdr results)
644 results (nreverse results)
646 results (cdr results)
647 sep-terms (nreverse sep-terms))
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))
655 results (cdr results)
656 sep-terms (cdr sep-terms)))
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 ?\{ ?\})))
668 (throw 'eshell-incomplete ?\{)
669 (when (eshell-arg-delimiter (1+ end))
671 (list 'eshell-as-subcommand
672 (eshell-parse-command (cons (1+ (point)) end)))
673 (goto-char (1+ end))))))))
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))
683 (read (current-buffer))
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))))))
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
698 (let ((sub-terms (list t))
699 (eshell-sep-terms (list t))
702 (if (and (consp (car terms))
703 (eq (caar terms) 'eshell-operator)
704 (string-match (concat "^" separator "$")
705 (nth 1 (car terms))))
707 (nconc eshell-sep-terms (list (nth 1 (car terms))))
708 (setq subchains (cons (cdr sub-terms) subchains)
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)))
717 (set last-terms-sym (reverse (cdr eshell-sep-terms))))
718 subchains) ; already reversed
720 (set last-terms-sym (cdr eshell-sep-terms)))
721 (nreverse subchains))))
723 ;;_* Command evaluation macros
725 ;; The structure of the following macros is very important to
726 ;; `eshell-do-eval' [Iterative evaluation]:
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
733 ;; @ The main body of a `let' can contain only one form. Use `progn'
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.
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))
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)
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'.
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
767 (run-hooks 'eshell-this-command-hook))
769 (run-hooks 'eshell-this-command-hook)
770 (eshell-errorn (error-message-string err))
771 (eshell-close-handles 1)))))
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)))
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)))
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)))
798 (defmacro eshell-protect (object)
799 "Protect I/O handles, so they aren't get closed after eval'ing OBJECT."
801 (eshell-protect-handles eshell-current-handles)
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
809 ,(when (cdr pipeline)
813 (eshell-do-pipelines (quote ,(cdr pipeline))))
814 (eshell-set-output-handle ,eshell-output-handle
816 (eshell-set-output-handle ,eshell-error-handle
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)
826 (concat (symbol-name (car head)) "*"))))))
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))
836 ,(when (cdr pipeline)
837 `(let (output-marker)
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)
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))
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
862 (set 'tailproc (or result tailproc))
864 `(eshell-do-pipelines-synchronously (quote ,(cdr pipeline))))
867 (defalias 'eshell-process-identity 'identity)
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)
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))))
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
888 `(let ,eshell-subcommand-bindings
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)))
898 (symbol-value value))))
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)))
906 ;;;_* Iterative evaluation
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.
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
921 (defun eshell/eshell-debug (&rest args)
922 "A command for toggling certain debug variables."
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]
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'
938 errors stops Eshell from trapping errors
939 commands shows command execution progress in `*eshell last cmd*'
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)))))))
950 (defun pcomplete/eshell-mode/eshell-debug ()
951 "Completion for the `debug' command."
952 (while (pcomplete-here '("errors" "commands"))))
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)))
962 (insert "\n\C-l\n" tag "\n\n" text
964 (concat "\n\n" (eshell-stringify subform)) ""))))))
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))))
974 (list 'insert-and-inherit
975 (concat input "\n")))
977 (eshell-update-markers here))
978 (list 'eshell-do-eval
979 (list 'quote command)))))
980 (and eshell-debug-command
982 (let ((buf (get-buffer-create "*eshell last cmd*")))
985 (insert "command: \"" input "\"\n"))))
986 (setq eshell-current-command command)
987 (let ((delim (catch 'eshell-incomplete
988 (eshell-resume-eval))))
990 (error "Unmatched delimiter: %c"
995 (defun eshell-resume-command (proc status)
996 "Resume the current command when a process ends."
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)))))
1004 (defun eshell-resume-eval ()
1005 "Destructively evaluate a form which may need to be deferred."
1006 (eshell-condition-case err
1008 (setq eshell-last-async-proc nil)
1009 (when eshell-current-command
1011 (proc (catch 'eshell-defer
1015 eshell-current-command))))))
1016 (if (eshell-processp proc)
1017 (ignore (setq eshell-last-async-proc proc))
1020 (error (error-message-string err)))))
1022 (defmacro eshell-manipulate (tag &rest commands)
1023 "Manipulate a COMMAND form, with TAG as a debug identifier."
1024 (if (not eshell-debug-command)
1027 (eshell-debug-command ,(eval tag) form)
1029 (eshell-debug-command ,(concat "done " (eval tag)) form))))
1031 (put 'eshell-manipulate 'lisp-indent-function 1)
1033 ;; eshell-lookup-function, eshell-functionp, and eshell-macrop taken
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)))
1042 (defconst function-p-func
1043 (if (eshell-under-xemacs-p)
1044 'compiled-function-p
1045 'byte-code-function-p))
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)
1053 (eq (car object) 'lambda)
1054 (listp (car (cdr object)))))
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)))
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."
1071 (list 'quote (eval form)))
1072 ((memq (car form) '(quote function))
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)))
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)
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))
1122 ((eq (car form) 'setcdr)
1123 (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p))
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.
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)))
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))
1151 (if (and args (not (memq (car form) '(run-hooks))))
1153 (format "evaluating arguments to `%s'"
1154 (symbol-name (car form)))
1156 (setcar args (eshell-do-eval (car args) synchronous-p))
1157 (setq args (cdr args)))))
1159 ((eq (car form) 'progn)
1161 ((eq (car form) 'prog1)
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:
1171 ;; (eshell-named-command \"hello\")
1173 ;; Now, let's assume the 'hello' command is an Eshell
1174 ;; alias, the definition of which yields the command:
1176 ;; (eshell-named-command \"echo\" (list \"Hello\" \"world\"))
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.
1188 (catch 'eshell-replace-command
1190 (setq result (eval form)))))
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)
1199 (eshell-processp result))
1201 (eshell/wait result)
1202 (eshell-manipulate "inserting ignore form"
1203 (setcar form 'ignore)
1205 (throw 'eshell-defer result))
1206 (list 'quote result))))))))))))
1208 ;; command invocation
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)
1217 (if (and (not direct)
1218 (eshell-using-module 'eshell-alias)
1220 (funcall (symbol-function 'eshell-lookup-alias)
1223 (concat name " is an alias, defined as \""
1224 (cadr alias) "\"")))
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
1232 (let ((desc (let ((inhibit-redisplay t))
1233 (save-window-excursion
1235 (describe-function sym)
1237 (setq desc (substring desc 0
1238 (1- (or (string-match "\n" desc)
1240 (if (buffer-live-p (get-buffer "*Help*"))
1241 (kill-buffer "*Help*"))
1242 (setq program (or desc name))))))
1244 (eshell-error (format "which: no %s in (%s)\n"
1245 name (getenv "PATH")))
1246 (eshell-printn program)))))
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))))
1262 (defalias 'eshell-named-command* 'eshell-named-command)
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))
1270 (string-match "\\(em\\|esh\\)-\\(.*\\)\\(\\.el\\)?\\'" file))
1271 (setq file (concat "eshell-" (match-string 2 file))))
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)))
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))))
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."
1300 (eshell-condition-case err
1303 (save-current-buffer
1306 (apply func-or-form args))))
1307 (and result (funcall printer result))
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))
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))
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))
1329 (defsubst eshell-eval* (printer errprint form)
1330 "Evaluate FORM, trapping errors and returning them."
1331 (eshell-exec-lisp printer errprint form nil t))
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))
1339 (defsubst eshell-funcall (func &rest args)
1340 "Call FUNC, with ARGS, trapping errors and return them as output."
1341 (eshell-apply func args))
1343 (defsubst eshell-eval (form)
1344 "Evaluate FORM, trapping errors and returning them."
1345 (eshell-eval* 'eshell-print 'eshell-error form))
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))
1353 (defsubst eshell-funcalln (func &rest args)
1354 "Call FUNC, with ARGS, trapping errors and return them as output."
1355 (eshell-applyn func args))
1357 (defsubst eshell-evaln (form)
1358 "Evaluate FORM, trapping errors and returning them."
1359 (eshell-eval* 'eshell-printn 'eshell-errorn form))
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))
1368 (if (functionp object)
1369 (eshell-apply object args)
1370 (eshell-eval object))))
1371 (if (and eshell-ensure-newline-p
1373 (goto-char eshell-last-output-end)
1375 (eshell-print "\n"))
1376 (eshell-close-handles 0 (list 'quote result)))))
1378 (defalias 'eshell-lisp-command* 'eshell-lisp-command)
1380 ;;; esh-cmd.el ends here