esh-cmd.el 48.3 KB
Newer Older
1
;;; esh-cmd.el --- command invocation  -*- lexical-binding:t -*-
Gerd Moellmann's avatar
Gerd Moellmann committed
2

3
;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
4

Gerd Moellmann's avatar
Gerd Moellmann committed
5 6
;; Author: John Wiegley <johnw@gnu.org>

Gerd Moellmann's avatar
Gerd Moellmann committed
7 8
;; This file is part of GNU Emacs.

9
;; GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
10
;; it under the terms of the GNU General Public License as published by
11 12
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
13 14 15 16 17 18 19

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
20
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Gerd Moellmann's avatar
Gerd Moellmann committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

;;; Commentary:

;;;_* Invoking external commands
;;
;; External commands cause processes to be created, by loading
;; external executables into memory.  This is what most normal shells
;; do, most of the time.  For more information, see [External commands].
;;
;;;_* Invoking Lisp functions
;;
;; A Lisp function can be invoked using Lisp syntax, or command shell
;; syntax.  For example, to run `dired' to edit the current directory:
;;
;;   /tmp $ (dired ".")
;;
;; Or:
;;
;;   /tmp $ dired .
;;
;; The latter form is preferable, but the former is more precise,
;; since it involves no translations.  See [Argument parsing], to
;; learn more about how arguments are transformed before passing them
;; to commands.
;;
;; Ordinarily, if 'dired' were also available as an external command,
;; the external version would be called in preference to any Lisp
;; function of the same name.  To change this behavior so that Lisp
;; functions always take precedence, set
;; `eshell-prefer-lisp-functions' to t.

;;;_* Alias functions
;;
;; Whenever a command is specified using a simple name, such as 'ls',
;; Eshell will first look for a Lisp function of the name `eshell/ls'.
;; If it exists, it will be called in preference to any other command
;; which might have matched the name 'ls' (such as command aliases,
;; external commands, Lisp functions of that name, etc).
;;
;; This is the most flexible mechanism for creating new commands,
;; since it does not pollute the global namespace, yet allows you to
;; use all of Lisp's facilities to define that piece of functionality.
;; Most of Eshell's "builtin" commands are defined as alias functions.
;;
;;;_* Lisp arguments
;;
;; It is possible to invoke a Lisp form as an argument.  This can be
;; done either by specifying the form as you might in Lisp, or by
;; using the '$' character to introduce a value-interpolation:
;;
;;   echo (+ 1 2)
;;
;; Or
;;
;;   echo $(+ 1 2)
;;
;; The two forms are equivalent.  The second is required only if the
;; form being interpolated is within a string, or is a subexpression
;; of a larger argument:
;;
;;   echo x$(+ 1 2) "String $(+ 1 2)"
;;
;; To pass a Lisp symbol as a argument, use the alternate quoting
;; syntax, since the single quote character is far too overused in
;; shell syntax:
;;
;;   echo #'lisp-symbol
;;
;; Backquote can also be used:
;;
;;   echo `(list ,lisp-symbol)
;;
;; Lisp arguments are identified using the following regexp:

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
;;;_* Command hooks
;;
;; There are several hooks involved with command execution, which can
;; be used either to change or augment Eshell's behavior.


;;; Code:

(require 'esh-util)
(unless (featurep 'xemacs)
  (require 'eldoc))
(require 'esh-arg)
(require 'esh-proc)
(require 'esh-ext)

(eval-when-compile
111
  (require 'cl-lib)
112 113 114 115 116 117 118 119 120 121 122 123
  (require 'pcomplete))


(defgroup eshell-cmd nil
  "Executing an Eshell command is as simple as typing it in and
pressing <RET>.  There are several different kinds of commands,
however."
  :tag "Command invocation"
  ;; :link '(info-link "(eshell)Command invocation")
  :group 'eshell)

(defcustom eshell-prefer-lisp-functions nil
Glenn Morris's avatar
Glenn Morris committed
124
  "If non-nil, prefer Lisp functions to external commands."
125 126 127
  :type 'boolean
  :group 'eshell-cmd)

Gerd Moellmann's avatar
Gerd Moellmann committed
128
(defcustom eshell-lisp-regexp "\\([(`]\\|#'\\)"
Glenn Morris's avatar
Glenn Morris committed
129
  "A regexp which, if matched at beginning of an argument, means Lisp.
Gerd Moellmann's avatar
Gerd Moellmann committed
130 131 132 133 134
Such arguments will be passed to `read', and then evaluated."
  :type 'regexp
  :group 'eshell-cmd)

(defcustom eshell-pre-command-hook nil
Glenn Morris's avatar
Glenn Morris committed
135
  "A hook run before each interactive command is invoked."
Gerd Moellmann's avatar
Gerd Moellmann committed
136 137 138 139
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-post-command-hook nil
Glenn Morris's avatar
Glenn Morris committed
140
  "A hook run after each interactive command is invoked."
Gerd Moellmann's avatar
Gerd Moellmann committed
141 142 143 144
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-prepare-command-hook nil
Glenn Morris's avatar
Glenn Morris committed
145
  "A set of functions called to prepare a named command.
Gerd Moellmann's avatar
Gerd Moellmann committed
146 147 148 149 150 151 152 153 154 155
The command name and its argument are in `eshell-last-command-name'
and `eshell-last-arguments'.  The functions on this hook can change
the value of these symbols if necessary.

To prevent a command from executing at all, set
`eshell-last-command-name' to nil."
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-named-command-hook nil
Glenn Morris's avatar
Glenn Morris committed
156
  "A set of functions called before a named command is invoked.
Gerd Moellmann's avatar
Gerd Moellmann committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
Each function will be passed the command name and arguments that were
passed to `eshell-named-command'.

If any of the functions returns a non-nil value, the named command
will not be invoked, and that value will be returned from
`eshell-named-command'.

In order to substitute an alternate command form for execution, the
hook function should throw it using the tag `eshell-replace-command'.
For example:

  (add-hook 'eshell-named-command-hook 'subst-with-cd)
  (defun subst-with-cd (command args)
    (throw 'eshell-replace-command
	   (eshell-parse-command \"cd\" args)))

Although useless, the above code will cause any non-glob, non-Lisp
command (i.e., 'ls' as opposed to '*ls' or '(ls)') to be replaced by a
call to `cd' using the arguments that were passed to the function."
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-pre-rewrite-command-hook
  '(eshell-no-command-conversion
    eshell-subcommand-arg-values)
Glenn Morris's avatar
Glenn Morris committed
182
  "A hook run before command rewriting begins.
Gerd Moellmann's avatar
Gerd Moellmann committed
183 184 185 186 187 188 189 190 191 192 193 194
The terms of the command to be rewritten is passed as arguments, and
may be modified in place.  Any return value is ignored."
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-rewrite-command-hook
  '(eshell-rewrite-for-command
    eshell-rewrite-while-command
    eshell-rewrite-if-command
    eshell-rewrite-sexp-command
    eshell-rewrite-initial-subcommand
    eshell-rewrite-named-command)
Glenn Morris's avatar
Glenn Morris committed
195
  "A set of functions used to rewrite the command argument.
Gerd Moellmann's avatar
Gerd Moellmann committed
196 197 198 199 200 201 202 203 204 205 206 207 208
Once parsing of a command line is completed, the next step is to
rewrite the initial argument into something runnable.

A module may wish to associate special behavior with certain argument
syntaxes at the beginning of a command line.  They are welcome to do
so by adding a function to this hook.  The first function to return a
substitute command form is the one used.  Each function is passed the
command's full argument list, which is a list of sexps (typically
forms or strings)."
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-post-rewrite-command-hook nil
Glenn Morris's avatar
Glenn Morris committed
209
  "A hook run after command rewriting is finished.
Gerd Moellmann's avatar
Gerd Moellmann committed
210 211 212 213 214
Each function is passed the symbol containing the rewritten command,
which may be modified directly.  Any return value is ignored."
  :type 'hook
  :group 'eshell-cmd)

215
(defcustom eshell-complex-commands '("ls")
Glenn Morris's avatar
Glenn Morris committed
216
  "A list of commands names or functions, that determine complexity.
John Wiegley's avatar
John Wiegley committed
217 218 219 220 221 222 223 224 225 226 227 228 229
That is, if a command is defined by a function named eshell/NAME,
and NAME is part of this list, it is invoked as a complex command.
Complex commands are always correct, but run much slower.  If a
command works fine without being part of this list, then it doesn't
need to be.

If an entry is a function, it will be called with the name, and should
return non-nil if the command is complex."
  :type '(repeat :tag "Commands"
		 (choice (string :tag "Name")
			 (function :tag "Predicate")))
  :group 'eshell-cmd)

Gerd Moellmann's avatar
Gerd Moellmann committed
230 231
;;; User Variables:

232
(defcustom eshell-cmd-load-hook nil
Glenn Morris's avatar
Glenn Morris committed
233
  "A hook that gets run when `eshell-cmd' is loaded."
234
  :version "24.1"		       ; removed eshell-cmd-initialize
Gerd Moellmann's avatar
Gerd Moellmann committed
235 236 237 238
  :type 'hook
  :group 'eshell-cmd)

(defcustom eshell-debug-command nil
239 240 241
  "If non-nil, enable Eshell debugging code.
This is slow, and only useful for debugging problems with Eshell.
If you change this without using customize after Eshell has loaded,
Glenn Morris's avatar
Glenn Morris committed
242
you must re-load 'esh-cmd.el'."
243 244
  :initialize 'custom-initialize-default
  :set (lambda (symbol value)
245
	 (set symbol value)
246
	 (load-library "esh-cmd"))
Gerd Moellmann's avatar
Gerd Moellmann committed
247 248 249 250 251 252 253
  :type 'boolean
  :group 'eshell-cmd)

(defcustom eshell-deferrable-commands
  '(eshell-named-command
    eshell-lisp-command
    eshell-process-identity)
Paul Eggert's avatar
Paul Eggert committed
254
  "A list of functions which might return an asynchronous process.
Gerd Moellmann's avatar
Gerd Moellmann committed
255 256 257 258 259 260 261 262 263 264
If they return a process object, execution of the calling Eshell
command will wait for completion (in the background) before finishing
the command."
  :type '(repeat function)
  :group 'eshell-cmd)

(defcustom eshell-subcommand-bindings
  '((eshell-in-subcommand-p t)
    (default-directory default-directory)
    (process-environment (eshell-copy-environment)))
Glenn Morris's avatar
Glenn Morris committed
265
  "A list of `let' bindings for subcommand environments."
Gerd Moellmann's avatar
Gerd Moellmann committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  :type 'sexp
  :group 'eshell-cmd)

(put 'risky-local-variable 'eshell-subcommand-bindings t)

(defvar eshell-ensure-newline-p nil
  "If non-nil, ensure that a newline is emitted after a Lisp form.
This can be changed by Lisp forms that are evaluated from the Eshell
command line.")

;;; Internal Variables:

(defvar eshell-current-command nil)
(defvar eshell-command-name nil)
(defvar eshell-command-arguments nil)
281 282 283 284
(defvar eshell-in-pipeline-p nil
  "Internal Eshell variable, non-nil inside a pipeline.
Has the value 'first, 'last for the first/last commands in the pipeline,
otherwise t.")
Gerd Moellmann's avatar
Gerd Moellmann committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
(defvar eshell-in-subcommand-p nil)
(defvar eshell-last-arguments nil)
(defvar eshell-last-command-name nil)
(defvar eshell-last-async-proc nil
  "When this foreground process completes, resume command evaluation.")

;;; Functions:

(defsubst eshell-interactive-process ()
  "Return currently running command process, if non-Lisp."
  eshell-last-async-proc)

(defun eshell-cmd-initialize ()
  "Initialize the Eshell command processing module."
  (set (make-local-variable 'eshell-current-command) nil)
  (set (make-local-variable 'eshell-command-name) nil)
  (set (make-local-variable 'eshell-command-arguments) nil)
  (set (make-local-variable 'eshell-last-arguments) nil)
  (set (make-local-variable 'eshell-last-command-name) nil)
  (set (make-local-variable 'eshell-last-async-proc) nil)

  (add-hook 'eshell-kill-hook 'eshell-resume-command nil t)

  ;; make sure that if a command is over, and no process is being
  ;; waited for, that `eshell-current-command' is set to nil.  This
  ;; situation can occur, for example, if a Lisp function results in
  ;; `debug' being called, and the user then types \\[top-level]
  (add-hook 'eshell-post-command-hook
	    (function
	     (lambda ()
	       (setq eshell-current-command nil
		     eshell-last-async-proc nil))) nil t)

  (add-hook 'eshell-parse-argument-hook
	    'eshell-parse-subcommand-argument nil t)
  (add-hook 'eshell-parse-argument-hook
	    'eshell-parse-lisp-argument nil t)

  (when (eshell-using-module 'eshell-cmpl)
    (add-hook 'pcomplete-try-first-hook
	      'eshell-complete-lisp-symbols nil t)))

(defun eshell-complete-lisp-symbols ()
  "If there is a user reference, complete it."
  (let ((arg (pcomplete-actual-arg)))
    (when (string-match (concat "\\`" eshell-lisp-regexp) arg)
      (setq pcomplete-stub (substring arg (match-end 0))
	    pcomplete-last-completion-raw t)
      (throw 'pcomplete-completions
	     (all-completions pcomplete-stub obarray 'boundp)))))

;; Command parsing

(defun eshell-parse-command (command &optional args top-level)
  "Parse the COMMAND, adding ARGS if given.
COMMAND can either be a string, or a cons cell demarcating a buffer
region.  TOP-LEVEL, if non-nil, means that the outermost command (the
user's input command) is being parsed, and that pre and post command
hooks should be run before and after the command."
  (let* (sep-terms
	 (terms
	  (append
	   (if (consp command)
	       (eshell-parse-arguments (car command) (cdr command))
	     (let ((here (point))
350 351 352 353 354 355 356 357
		   (inhibit-point-motion-hooks t))
               (with-silent-modifications
                 ;; FIXME: Why not use a temporary buffer and avoid this
                 ;; "insert&delete" business?  --Stef
                 (insert command)
                 (prog1
                     (eshell-parse-arguments here (point))
                   (delete-region here (point))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
358 359 360 361 362
	   args))
	 (commands
	  (mapcar
	   (function
	    (lambda (cmd)
363 364 365 366 367 368
              (setq cmd
                    (if (or (not (car sep-terms))
                            (string= (car sep-terms) ";"))
			(eshell-parse-pipeline cmd (not (car sep-terms)))
		      `(eshell-do-subjob
                        (list ,(eshell-parse-pipeline cmd)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
369 370 371
	      (setq sep-terms (cdr sep-terms))
	      (if eshell-in-pipeline-p
		  cmd
372
		`(eshell-trap-errors ,cmd))))
Gerd Moellmann's avatar
Gerd Moellmann committed
373 374 375 376
	   (eshell-separate-commands terms "[&;]" nil 'sep-terms))))
    (let ((cmd commands))
      (while cmd
	(if (cdr cmd)
377
	    (setcar cmd `(eshell-commands ,(car cmd))))
Gerd Moellmann's avatar
Gerd Moellmann committed
378 379
	(setq cmd (cdr cmd))))
    (setq commands
380 381 382 383 384 385 386
	  `(progn
             ,@(if top-level
                   '((run-hooks 'eshell-pre-command-hook)))
             ,@(if (not top-level)
                   commands
                 `((catch 'top-level (progn ,@commands))
                   (run-hooks 'eshell-post-command-hook)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
387
    (if top-level
388
	`(eshell-commands ,commands)
Gerd Moellmann's avatar
Gerd Moellmann committed
389 390
      commands)))

391 392 393 394
(defun eshell-debug-command (tag subform)
  "Output a debugging message to '*eshell last cmd*'."
  (let ((buf (get-buffer-create "*eshell last cmd*"))
	(text (eshell-stringify eshell-current-command)))
395
    (with-current-buffer buf
396 397 398 399 400 401
      (if (not tag)
	  (erase-buffer)
	(insert "\n\C-l\n" tag "\n\n" text
		(if subform
		    (concat "\n\n" (eshell-stringify subform)) ""))))))

Gerd Moellmann's avatar
Gerd Moellmann committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
(defun eshell-debug-show-parsed-args (terms)
  "Display parsed arguments in the debug buffer."
  (ignore
   (if eshell-debug-command
       (eshell-debug-command "parsed arguments" terms))))

(defun eshell-no-command-conversion (terms)
  "Don't convert the command argument."
  (ignore
   (if (and (listp (car terms))
	    (eq (caar terms) 'eshell-convert))
       (setcar terms (cadr (car terms))))))

(defun eshell-subcommand-arg-values (terms)
  "Convert subcommand arguments {x} to ${x}, in order to take their values."
  (setq terms (cdr terms))		; skip command argument
  (while terms
    (if (and (listp (car terms))
	     (eq (caar terms) 'eshell-as-subcommand))
421 422
	(setcar terms `(eshell-convert
                        (eshell-command-to-value ,(car terms)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    (setq terms (cdr terms))))

(defun eshell-rewrite-sexp-command (terms)
  "Rewrite a sexp in initial position, such as '(+ 1 2)'."
  ;; this occurs when a Lisp expression is in first position
  (if (and (listp (car terms))
	   (eq (caar terms) 'eshell-command-to-value))
      (car (cdar terms))))

(defun eshell-rewrite-initial-subcommand (terms)
  "Rewrite a subcommand in initial position, such as '{+ 1 2}'."
  (if (and (listp (car terms))
	   (eq (caar terms) 'eshell-as-subcommand))
      (car terms)))

(defun eshell-rewrite-named-command (terms)
  "If no other rewriting rule transforms TERMS, assume a named command."
440 441 442 443 444 445
  (let ((sym (if eshell-in-pipeline-p
		 'eshell-named-command*
	       'eshell-named-command))
	(cmd (car terms))
	(args (cdr terms)))
    (if args
446
	(list sym cmd `(list ,@(cdr terms)))
447
      (list sym cmd))))
Gerd Moellmann's avatar
Gerd Moellmann committed
448

449 450
(defvar eshell-command-body)
(defvar eshell-test-body)
Gerd Moellmann's avatar
Gerd Moellmann committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

(defsubst eshell-invokify-arg (arg &optional share-output silent)
  "Change ARG so it can be invoked from a structured command.

SHARE-OUTPUT, if non-nil, means this invocation should share the
current output stream, which is separately redirectable.  SILENT
means the user and/or any redirections shouldn't see any output
from this command.  If both SHARE-OUTPUT and SILENT are non-nil,
the second is ignored."
  ;; something that begins with `eshell-convert' means that it
  ;; intends to return a Lisp value.  We want to get past this,
  ;; but if it's not _actually_ a value interpolation -- in which
  ;; we leave it alone.  In fact, the only time we muck with it
  ;; is in the case of a {subcommand} that has been turned into
  ;; the interpolation, ${subcommand}, by the parser because it
  ;; didn't know better.
  (if (and (listp arg)
	   (eq (car arg) 'eshell-convert)
	   (eq (car (cadr arg)) 'eshell-command-to-value))
      (if share-output
	  (cadr (cadr arg))
472
	`(eshell-commands ,(cadr (cadr arg)) ,silent))
Gerd Moellmann's avatar
Gerd Moellmann committed
473 474
    arg))

475 476
(defvar eshell-last-command-status)     ;Define in esh-io.el.

Gerd Moellmann's avatar
Gerd Moellmann committed
477 478 479
(defun eshell-rewrite-for-command (terms)
  "Rewrite a `for' command into its equivalent Eshell command form.
Because the implementation of `for' relies upon conditional evaluation
480
of its argument (i.e., use of a Lisp special form), it must be
Gerd Moellmann's avatar
Gerd Moellmann committed
481
implemented via rewriting, rather than as a function."
482 483
  (if (and (equal (car terms) "for")
	   (equal (nth 2 terms) "in"))
Gerd Moellmann's avatar
Gerd Moellmann committed
484 485
      (let ((body (car (last terms))))
	(setcdr (last terms 2) nil)
486
	`(let ((for-items
Chong Yidong's avatar
Chong Yidong committed
487 488 489 490 491 492 493 494 495
		(copy-tree
		 (append
		  ,@(mapcar
		     (lambda (elem)
		       (if (listp elem)
			   elem
			 `(list ,elem)))
		     (cdr (cddr terms))))))
	       (eshell-command-body '(nil))
496
               (eshell-test-body '(nil)))
Chong Yidong's avatar
Chong Yidong committed
497 498 499 500 501 502
	   (while (car for-items)
	     (let ((,(intern (cadr terms)) (car for-items)))
	       (eshell-protect
	   	,(eshell-invokify-arg body t)))
	     (setcar for-items (cadr for-items))
	     (setcdr for-items (cddr for-items)))
503 504 505
           (eshell-close-handles
            eshell-last-command-status
            (list 'quote eshell-last-command-result))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

(defun eshell-structure-basic-command (func names keyword test body
					    &optional else vocal-test)
  "With TERMS, KEYWORD, and two NAMES, structure a basic command.
The first of NAMES should be the positive form, and the second the
negative.  It's not likely that users should ever need to call this
function.

If VOCAL-TEST is non-nil, it means output from the test should be
shown, as well as output from the body."
  ;; If the test form begins with `eshell-convert', it means
  ;; something data-wise will be returned, and we should let
  ;; that determine the truth of the statement.
  (unless (eq (car test) 'eshell-convert)
    (setq test
521 522
	  `(progn ,test
                  (eshell-exit-success-p))))
Gerd Moellmann's avatar
Gerd Moellmann committed
523 524 525 526 527 528 529 530 531

  ;; should we reverse the sense of the test?  This depends
  ;; on the `names' parameter.  If it's the symbol nil, yes.
  ;; Otherwise, it can be a pair of strings; if the keyword
  ;; we're using matches the second member of that pair (a
  ;; list), we should reverse it.
  (if (or (eq names nil)
	  (and (listp names)
	       (string= keyword (cadr names))))
532
      (setq test `(not ,test)))
Gerd Moellmann's avatar
Gerd Moellmann committed
533 534 535

  ;; finally, create the form that represents this structured
  ;; command
536 537 538 539 540 541
  `(let ((eshell-command-body '(nil))
         (eshell-test-body '(nil)))
     (,func ,test ,body ,else)
     (eshell-close-handles
        eshell-last-command-status
        (list 'quote eshell-last-command-result))))
Gerd Moellmann's avatar
Gerd Moellmann committed
542 543 544 545 546 547 548 549 550 551 552

(defun eshell-rewrite-while-command (terms)
  "Rewrite a `while' command into its equivalent Eshell command form.
Because the implementation of `while' relies upon conditional
evaluation of its argument (i.e., use of a Lisp special form), it
must be implemented via rewriting, rather than as a function."
  (if (and (stringp (car terms))
	   (member (car terms) '("while" "until")))
      (eshell-structure-basic-command
       'while '("while" "until") (car terms)
       (eshell-invokify-arg (cadr terms) nil t)
553 554
       `(eshell-protect
         ,(eshell-invokify-arg (car (last terms)) t)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
555 556 557 558 559 560 561 562 563 564 565

(defun eshell-rewrite-if-command (terms)
  "Rewrite an `if' command into its equivalent Eshell command form.
Because the implementation of `if' relies upon conditional
evaluation of its argument (i.e., use of a Lisp special form), it
must be implemented via rewriting, rather than as a function."
  (if (and (stringp (car terms))
	   (member (car terms) '("if" "unless")))
      (eshell-structure-basic-command
       'if '("if" "unless") (car terms)
       (eshell-invokify-arg (cadr terms) nil t)
566 567 568
       `(eshell-protect
         ,(eshell-invokify-arg (car (last terms (if (= (length terms) 4) 2)))
                               t))
569
       (if (= (length terms) 4)
570 571 572 573
	   `(eshell-protect
             ,(eshell-invokify-arg (car (last terms)))) t))))

(defvar eshell-last-command-result)     ;Defined in esh-io.el.
Gerd Moellmann's avatar
Gerd Moellmann committed
574 575 576 577 578

(defun eshell-exit-success-p ()
  "Return non-nil if the last command was \"successful\".
For a bit of Lisp code, this means a return value of non-nil.
For an external command, it means an exit code of 0."
579 580 581
  (if (save-match-data
	(string-match "#<\\(Lisp object\\|function .*\\)>"
		      eshell-last-command-name))
Gerd Moellmann's avatar
Gerd Moellmann committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
      eshell-last-command-result
    (= eshell-last-command-status 0)))

(defun eshell-parse-pipeline (terms &optional final-p)
  "Parse a pipeline from TERMS, return the appropriate Lisp forms."
  (let* (sep-terms
	 (bigpieces (eshell-separate-commands terms "\\(&&\\|||\\)"
					      nil 'sep-terms))
	 (bp bigpieces)
	 (results (list t))
	 final)
    (while bp
      (let ((subterms (car bp)))
	(let* ((pieces (eshell-separate-commands subterms "|"))
	       (p pieces))
	  (while p
	    (let ((cmd (car p)))
	      (run-hook-with-args 'eshell-pre-rewrite-command-hook cmd)
	      (setq cmd (run-hook-with-args-until-success
			 'eshell-rewrite-command-hook cmd))
	      (run-hook-with-args 'eshell-post-rewrite-command-hook 'cmd)
	      (setcar p cmd))
	    (setq p (cdr p)))
	  (nconc results
		 (list
		  (if (<= (length pieces) 1)
		      (car pieces)
609
		    (cl-assert (not eshell-in-pipeline-p))
610
		    `(eshell-execute-pipeline (quote ,pieces))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
611 612 613 614 615 616 617 618 619
	(setq bp (cdr bp))))
    ;; `results' might be empty; this happens in the case of
    ;; multi-line input
    (setq results (cdr results)
	  results (nreverse results)
	  final (car results)
	  results (cdr results)
	  sep-terms (nreverse sep-terms))
    (while results
620
      (cl-assert (car sep-terms))
Gerd Moellmann's avatar
Gerd Moellmann committed
621 622
      (setq final (eshell-structure-basic-command
		   'if (string= (car sep-terms) "&&") "if"
623 624
		   `(eshell-protect ,(car results))
		   `(eshell-protect ,final)
Gerd Moellmann's avatar
Gerd Moellmann committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
		   nil t)
	    results (cdr results)
	    sep-terms (cdr sep-terms)))
    final))

(defun eshell-parse-subcommand-argument ()
  "Parse a subcommand argument of the form '{command}'."
  (if (and (not eshell-current-argument)
	   (not eshell-current-quoted)
	   (eq (char-after) ?\{)
	   (or (= (point-max) (1+ (point)))
	       (not (eq (char-after (1+ (point))) ?\}))))
      (let ((end (eshell-find-delimiter ?\{ ?\})))
	(if (not end)
	    (throw 'eshell-incomplete ?\{)
	  (when (eshell-arg-delimiter (1+ end))
	    (prog1
642 643
		`(eshell-as-subcommand
                  ,(eshell-parse-command (cons (1+ (point)) end)))
Gerd Moellmann's avatar
Gerd Moellmann committed
644 645 646 647 648 649 650 651 652
	      (goto-char (1+ end))))))))

(defun eshell-parse-lisp-argument ()
  "Parse a Lisp expression which is specified as an argument."
  (if (and (not eshell-current-argument)
	   (not eshell-current-quoted)
	   (looking-at eshell-lisp-regexp))
      (let* ((here (point))
	     (obj
653
	      (condition-case nil
Gerd Moellmann's avatar
Gerd Moellmann committed
654 655 656 657
		  (read (current-buffer))
		(end-of-file
		 (throw 'eshell-incomplete ?\()))))
	(if (eshell-arg-delimiter)
658 659
	    `(eshell-command-to-value
              (eshell-lisp-command (quote ,obj)))
Gerd Moellmann's avatar
Gerd Moellmann committed
660 661
	  (ignore (goto-char here))))))

John Wiegley's avatar
John Wiegley committed
662 663
(defun eshell-separate-commands (terms separator &optional
				       reversed last-terms-sym)
Gerd Moellmann's avatar
Gerd Moellmann committed
664 665
  "Separate TERMS using SEPARATOR.
If REVERSED is non-nil, the list of separated term groups will be
666
returned in reverse order.  If LAST-TERMS-SYM is a symbol, its value
Gerd Moellmann's avatar
Gerd Moellmann committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
will be set to a list of all the separator operators found (or '(list
nil)' if none)."
  (let ((sub-terms (list t))
	(eshell-sep-terms (list t))
	subchains)
    (while terms
      (if (and (consp (car terms))
	       (eq (caar terms) 'eshell-operator)
	       (string-match (concat "^" separator "$")
			     (nth 1 (car terms))))
	  (progn
	    (nconc eshell-sep-terms (list (nth 1 (car terms))))
	    (setq subchains (cons (cdr sub-terms) subchains)
		  sub-terms (list t)))
	(nconc sub-terms (list (car terms))))
      (setq terms (cdr terms)))
    (if (> (length sub-terms) 1)
	(setq subchains (cons (cdr sub-terms) subchains)))
    (if reversed
	(progn
	  (if last-terms-sym
	      (set last-terms-sym (reverse (cdr eshell-sep-terms))))
	  subchains)                    ; already reversed
      (if last-terms-sym
	  (set last-terms-sym (cdr eshell-sep-terms)))
      (nreverse subchains))))

;;_* Command evaluation macros
;;
;; The structure of the following macros is very important to
;; `eshell-do-eval' [Iterative evaluation]:
;;
;; @ Don't use forms that conditionally evaluate their arguments, such
;;   as `setq', `if', `while', `let*', etc.  The only special forms
;;   that can be used are `let', `condition-case' and
;;   `unwind-protect'.
;;
;; @ The main body of a `let' can contain only one form.  Use `progn'
;;   if necessary.
;;
;; @ The two `special' variables are `eshell-current-handles' and
;;   `eshell-current-subjob-p'.  Bind them locally with a `let' if you
;;   need to change them.  Change them directly only if your intention
;;   is to change the calling environment.

(defmacro eshell-do-subjob (object)
  "Evaluate a command OBJECT as a subjob.
714
We indicate that the process was run in the background by returning it
Gerd Moellmann's avatar
Gerd Moellmann committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
ensconced in a list."
  `(let ((eshell-current-subjob-p t))
     ,object))

(defmacro eshell-commands (object &optional silent)
  "Place a valid set of handles, and context, around command OBJECT."
  `(let ((eshell-current-handles
	  (eshell-create-handles ,(not silent) 'append))
	 eshell-current-subjob-p)
     ,object))

(defmacro eshell-trap-errors (object)
  "Trap any errors that occur, so they are not entirely fatal.
Also, the variable `eshell-this-command-hook' is available for the
duration of OBJECT's evaluation.  Note that functions should be added
to this hook using `nconc', and *not* `add-hook'.

Someday, when Scheme will become the dominant Emacs language, all of
this grossness will be made to disappear by using `call/cc'..."
734
  `(let ((eshell-this-command-hook '(ignore)))
Gerd Moellmann's avatar
Gerd Moellmann committed
735 736 737 738 739 740 741 742 743
     (eshell-condition-case err
	 (prog1
	     ,object
	   (run-hooks 'eshell-this-command-hook))
       (error
	(run-hooks 'eshell-this-command-hook)
	(eshell-errorn (error-message-string err))
	(eshell-close-handles 1)))))

744 745 746
(defvar eshell-output-handle)           ;Defined in esh-io.el.
(defvar eshell-error-handle)            ;Defined in esh-io.el.

John Wiegley's avatar
John Wiegley committed
747 748 749 750 751 752 753 754 755 756
(defmacro eshell-copy-handles (object)
  "Duplicate current I/O handles, so OBJECT works with its own copy."
  `(let ((eshell-current-handles
	  (eshell-create-handles
	   (car (aref eshell-current-handles
		      eshell-output-handle)) nil
	   (car (aref eshell-current-handles
		      eshell-error-handle)) nil)))
     ,object))

Gerd Moellmann's avatar
Gerd Moellmann committed
757 758 759 760 761 762
(defmacro eshell-protect (object)
  "Protect I/O handles, so they aren't get closed after eval'ing OBJECT."
  `(progn
     (eshell-protect-handles eshell-current-handles)
     ,object))

763 764 765
(defmacro eshell-do-pipelines (pipeline &optional notfirst)
  "Execute the commands in PIPELINE, connecting each to one another.
This macro calls itself recursively, with NOTFIRST non-nil."
Gerd Moellmann's avatar
Gerd Moellmann committed
766
  (when (setq pipeline (cadr pipeline))
John Wiegley's avatar
John Wiegley committed
767 768 769
    `(eshell-copy-handles
      (progn
	,(when (cdr pipeline)
770 771
	   `(let ((nextproc
		   (eshell-do-pipelines (quote ,(cdr pipeline)) t)))
772 773 774 775 776
              (eshell-set-output-handle ,eshell-output-handle
                                        'append nextproc)
              (eshell-set-output-handle ,eshell-error-handle
                                        'append nextproc)
              (setq tailproc (or tailproc nextproc))))
John Wiegley's avatar
John Wiegley committed
777 778 779 780 781 782 783 784
	,(let ((head (car pipeline)))
	   (if (memq (car head) '(let progn))
	       (setq head (car (last head))))
	   (when (memq (car head) eshell-deferrable-commands)
	     (ignore
	      (setcar head
		      (intern-soft
		       (concat (symbol-name (car head)) "*"))))))
785 786 787 788 789 790 791
	;; First and last elements in a pipeline may need special treatment.
	;; (Currently only eshell-ls-files uses 'last.)
	;; Affects process-connection-type in eshell-gather-process-output.
	(let ((eshell-in-pipeline-p
	       ,(cond ((not notfirst) (quote 'first))
		      ((cdr pipeline) t)
		      (t (quote 'last)))))
792
	  ,(car pipeline))))))
John Wiegley's avatar
John Wiegley committed
793 794 795 796 797 798

(defmacro eshell-do-pipelines-synchronously (pipeline)
  "Execute the commands in PIPELINE in sequence synchronously.
Output of each command is passed as input to the next one in the pipeline.
This is used on systems where `start-process' is not supported."
  (when (setq pipeline (cadr pipeline))
799
    `(progn
800
       ,(when (cdr pipeline)
801
          `(let ((output-marker ,(point-marker)))
802 803 804 805 806 807 808 809 810 811 812
             (eshell-set-output-handle ,eshell-output-handle
                                       'append output-marker)
             (eshell-set-output-handle ,eshell-error-handle
                                       'append output-marker)))
       ,(let ((head (car pipeline)))
          (if (memq (car head) '(let progn))
              (setq head (car (last head))))
          ;; FIXME: is deferrable significant here?
          (when (memq (car head) eshell-deferrable-commands)
            (ignore
             (setcar head
813 814 815
                     (intern-soft
                      (concat (symbol-name (car head)) "*"))))))
       ;; The last process in the pipe should get its handles
816 817 818 819 820
       ;; redirected as we found them before running the pipe.
       ,(if (null (cdr pipeline))
            `(progn
               (setq eshell-current-handles tail-handles)
               (setq eshell-in-pipeline-p nil)))
821 822 823 824 825 826 827
       (let ((result ,(car pipeline)))
         ;; tailproc gets the result of the last successful process in
         ;; the pipeline.
         (setq tailproc (or result tailproc))
         ,(if (cdr pipeline)
              `(eshell-do-pipelines-synchronously (quote ,(cdr pipeline))))
         result))))
Gerd Moellmann's avatar
Gerd Moellmann committed
828 829 830 831 832 833 834

(defalias 'eshell-process-identity 'identity)

(defmacro eshell-execute-pipeline (pipeline)
  "Execute the commands in PIPELINE, connecting each to one another."
  `(let ((eshell-in-pipeline-p t) tailproc)
     (progn
John Wiegley's avatar
John Wiegley committed
835 836 837 838 839 840 841 842
       ,(if (fboundp 'start-process)
	    `(eshell-do-pipelines ,pipeline)
	  `(let ((tail-handles (eshell-create-handles
				(car (aref eshell-current-handles
					   ,eshell-output-handle)) nil
				(car (aref eshell-current-handles
					   ,eshell-error-handle)) nil)))
	     (eshell-do-pipelines-synchronously ,pipeline)))
Gerd Moellmann's avatar
Gerd Moellmann committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
       (eshell-process-identity tailproc))))

(defmacro eshell-as-subcommand (command)
  "Execute COMMAND using a temp buffer.
This is used so that certain Lisp commands, such as `cd', when
executed in a subshell, do not disturb the environment of the main
Eshell buffer."
  `(let ,eshell-subcommand-bindings
     ,command))

(defmacro eshell-do-command-to-value (object)
  "Run a subcommand prepared by `eshell-command-to-value'.
This avoids the need to use `let*'."
  `(let ((eshell-current-handles
	  (eshell-create-handles value 'overwrite)))
     (progn
       ,object
       (symbol-value value))))

(defmacro eshell-command-to-value (object)
  "Run OBJECT synchronously, returning its result as a string.
Returns a string comprising the output from the command."
  `(let ((value (make-symbol "eshell-temp")))
     (eshell-do-command-to-value ,object)))

;;;_* Iterative evaluation
;;
;; Eshell runs all of its external commands asynchronously, so that
;; Emacs is not blocked while the operation is being performed.
;; However, this introduces certain synchronization difficulties,
;; since the Lisp code, once it returns, will not "go back" to finish
;; executing the commands which haven't yet been started.
;;
;; What Eshell does to work around this problem (basically, the lack
;; of threads in Lisp), is that it evaluates the command sequence
;; iteratively.  Whenever an asynchronous process is begun, evaluation
;; terminates and control is given back to Emacs.  When that process
;; finishes, it will resume the evaluation using the remainder of the
;; command tree.

(defun eshell/eshell-debug (&rest args)
  "A command for toggling certain debug variables."
  (ignore
   (cond
    ((not args)
     (if eshell-handle-errors
	 (eshell-print "errors\n"))
     (if eshell-debug-command
	 (eshell-print "commands\n")))
892
    ((member (car args) '("-h" "--help"))
Gerd Moellmann's avatar
Gerd Moellmann committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
     (eshell-print "usage: eshell-debug [kinds]

This command is used to aid in debugging problems related to Eshell
itself.  It is not useful for anything else.  The recognized `kinds'
at the moment are:

  errors       stops Eshell from trapping errors
  commands     shows command execution progress in `*eshell last cmd*'
"))
    (t
     (while args
       (cond
	((string= (car args) "errors")
	 (setq eshell-handle-errors (not eshell-handle-errors)))
	((string= (car args) "commands")
	 (setq eshell-debug-command (not eshell-debug-command))))
       (setq args (cdr args)))))))

(defun pcomplete/eshell-mode/eshell-debug ()
  "Completion for the `debug' command."
  (while (pcomplete-here '("errors" "commands"))))

John Wiegley's avatar
John Wiegley committed
915 916 917 918 919 920 921 922 923
(defun eshell-invoke-directly (command input)
  (let ((base (cadr (nth 2 (nth 2 (cadr command))))) name)
    (if (and (eq (car base) 'eshell-trap-errors)
	     (eq (car (cadr base)) 'eshell-named-command))
	(setq name (cadr (cadr base))))
    (and name (stringp name)
	 (not (member name eshell-complex-commands))
	 (catch 'simple
	   (progn
924
	    (dolist (pred eshell-complex-commands)
John Wiegley's avatar
John Wiegley committed
925 926 927 928 929 930
	      (if (and (functionp pred)
		       (funcall pred name))
		  (throw 'simple nil)))
	    t))
	 (fboundp (intern-soft (concat "eshell/" name))))))

Gerd Moellmann's avatar
Gerd Moellmann committed
931 932 933 934 935 936
(defun eshell-eval-command (command &optional input)
  "Evaluate the given COMMAND iteratively."
  (if eshell-current-command
      ;; we can just stick the new command at the end of the current
      ;; one, and everything will happen as it should
      (setcdr (last (cdr eshell-current-command))
937 938 939 940 941 942
	      (list `(let ((here (and (eobp) (point))))
                       ,(and input
                             `(insert-and-inherit ,(concat input "\n")))
                       (if here
                           (eshell-update-markers here))
                       (eshell-do-eval ',command))))
Gerd Moellmann's avatar
Gerd Moellmann committed
943
    (and eshell-debug-command
944 945 946
         (with-current-buffer (get-buffer-create "*eshell last cmd*")
           (erase-buffer)
           (insert "command: \"" input "\"\n")))
Gerd Moellmann's avatar
Gerd Moellmann committed
947
    (setq eshell-current-command command)
John Wiegley's avatar
John Wiegley committed
948 949
    (let ((delim (catch 'eshell-incomplete
		   (eshell-resume-eval))))
950 951
      ;; On systems that don't support async subprocesses, eshell-resume
      ;; can return t.  Don't treat that as an error.
952 953
      (if (listp delim)
	  (setq delim (car delim)))
954
      (if (and delim (not (eq delim t)))
955
	  (error "Unmatched delimiter: %c" delim)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
956 957 958 959

(defun eshell-resume-command (proc status)
  "Resume the current command when a process ends."
  (when proc
John Wiegley's avatar
John Wiegley committed
960 961
    (unless (or (not (stringp status))
		(string= "stopped" status)
Gerd Moellmann's avatar
Gerd Moellmann committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
		(string-match eshell-reset-signals status))
      (if (eq proc (eshell-interactive-process))
	  (eshell-resume-eval)))))

(defun eshell-resume-eval ()
  "Destructively evaluate a form which may need to be deferred."
  (eshell-condition-case err
      (progn
	(setq eshell-last-async-proc nil)
	(when eshell-current-command
	  (let* (retval
		 (proc (catch 'eshell-defer
			 (ignore
			  (setq retval
				(eshell-do-eval
				 eshell-current-command))))))
John Wiegley's avatar
John Wiegley committed
978
	    (if (eshell-processp proc)
Gerd Moellmann's avatar
Gerd Moellmann committed
979 980 981 982 983 984 985
		(ignore (setq eshell-last-async-proc proc))
	      (cadr retval)))))
    (error
     (error (error-message-string err)))))

(defmacro eshell-manipulate (tag &rest commands)
  "Manipulate a COMMAND form, with TAG as a debug identifier."
986
  (declare (indent 1))
987 988 989
  ;; Check `bound'ness since at compile time the code until here has not
  ;; executed yet.
  (if (not (and (boundp 'eshell-debug-command) eshell-debug-command))
Gerd Moellmann's avatar
Gerd Moellmann committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
      `(progn ,@commands)
    `(progn
       (eshell-debug-command ,(eval tag) form)
       ,@commands
       (eshell-debug-command ,(concat "done " (eval tag)) form))))

(defun eshell-do-eval (form &optional synchronous-p)
  "Evaluate form, simplifying it as we go.
Unless SYNCHRONOUS-P is non-nil, throws `eshell-defer' if it needs to
be finished later after the completion of an asynchronous subprocess."
  (cond
   ((not (listp form))
    (list 'quote (eval form)))
   ((memq (car form) '(quote function))
    form)
   (t
    ;; skip past the call to `eshell-do-eval'
    (when (eq (car form) 'eshell-do-eval)
      (setq form (cadr (cadr form))))
    ;; expand any macros directly into the form.  This is done so that
    ;; we can modify any `let' forms to evaluate only once.
1011
    (if (macrop (car form))
Gerd Moellmann's avatar
Gerd Moellmann committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	(let ((exp (eshell-copy-tree (macroexpand form))))
	  (eshell-manipulate (format "expanding macro `%s'"
				     (symbol-name (car form)))
	    (setcar form (car exp))
	    (setcdr form (cdr exp)))))
    (let ((args (cdr form)))
      (cond
       ((eq (car form) 'while)
	;; `eshell-copy-tree' is needed here so that the test argument
	;; doesn't get modified and thus always yield the same result.
	(when (car eshell-command-body)
1023
	  (cl-assert (not synchronous-p))
Gerd Moellmann's avatar
Gerd Moellmann committed
1024
	  (eshell-do-eval (car eshell-command-body))
John Wiegley's avatar
John Wiegley committed
1025 1026
	  (setcar eshell-command-body nil)
	  (setcar eshell-test-body nil))
Gerd Moellmann's avatar
Gerd Moellmann committed
1027 1028
	(unless (car eshell-test-body)
	  (setcar eshell-test-body (eshell-copy-tree (car args))))
John Wiegley's avatar
John Wiegley committed
1029
	(while (cadr (eshell-do-eval (car eshell-test-body)))
1030 1031 1032 1033
	  (setcar eshell-command-body
                  (if (cddr args)
                      `(progn ,@(eshell-copy-tree (cdr args)))
                    (eshell-copy-tree (cadr args))))
John Wiegley's avatar
John Wiegley committed
1034 1035 1036
	  (eshell-do-eval (car eshell-command-body) synchronous-p)
	  (setcar eshell-command-body nil)
	  (setcar eshell-test-body (eshell-copy-tree (car args))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1037 1038 1039 1040
	(setcar eshell-command-body nil))
       ((eq (car form) 'if)
	;; `eshell-copy-tree' is needed here so that the test argument
	;; doesn't get modified and thus always yield the same result.
John Wiegley's avatar
John Wiegley committed
1041 1042
	(if (car eshell-command-body)
	    (progn
1043
	      (cl-assert (not synchronous-p))
John Wiegley's avatar
John Wiegley committed
1044 1045 1046
	      (eshell-do-eval (car eshell-command-body)))
	  (unless (car eshell-test-body)
	    (setcar eshell-test-body (eshell-copy-tree (car args))))
1047 1048 1049 1050 1051
	  (setcar eshell-command-body
                  (eshell-copy-tree
                   (if (cadr (eshell-do-eval (car eshell-test-body)))
                       (cadr args)
                     (car (cddr args)))))
John Wiegley's avatar
John Wiegley committed
1052 1053 1054
	  (eshell-do-eval (car eshell-command-body) synchronous-p))
	(setcar eshell-command-body nil)
	(setcar eshell-test-body nil))
Gerd Moellmann's avatar
Gerd Moellmann committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
       ((eq (car form) 'setcar)
	(setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p))
	(eval form))
       ((eq (car form) 'setcdr)
	(setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p))
	(eval form))
       ((memq (car form) '(let catch condition-case unwind-protect))
	;; `let', `condition-case' and `unwind-protect' have to be
	;; handled specially, because we only want to call
	;; `eshell-do-eval' on their first form.
	;;
	;; NOTE: This requires obedience by all forms which this
	;; function might encounter, that they do not contain
	;; other special forms.
	(if (and (eq (car form) 'let)
		 (not (eq (car (cadr args)) 'eshell-do-eval)))
	    (eshell-manipulate "evaluating let args"
1072
	      (dolist (letarg (car args))
Gerd Moellmann's avatar
Gerd Moellmann committed
1073 1074 1075 1076 1077 1078 1079 1080 1081
		(if (and (listp letarg)
			 (not (eq (cadr letarg) 'quote)))
		    (setcdr letarg
			    (list (eshell-do-eval
				   (cadr letarg) synchronous-p)))))))
	(unless (eq (car form) 'unwind-protect)
	  (setq args (cdr args)))
	(unless (eq (caar args) 'eshell-do-eval)
	  (eshell-manipulate "handling special form"
1082
	    (setcar args `(eshell-do-eval ',(car args) ,synchronous-p))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1083
	(eval form))
1084 1085 1086 1087 1088
       ((eq (car form) 'setq)
	(if (cddr args) (error "Unsupported form (setq X1 E1 X2 E2..)"))
        (eshell-manipulate "evaluating arguments to setq"
          (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)))
	(list 'quote (eval form)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
       (t
	(if (and args (not (memq (car form) '(run-hooks))))
	    (eshell-manipulate
		(format "evaluating arguments to `%s'"
			(symbol-name (car form)))
	      (while args
		(setcar args (eshell-do-eval (car args) synchronous-p))
		(setq args (cdr args)))))
	(cond
	 ((eq (car form) 'progn)
	  (car (last form)))
	 ((eq (car form) 'prog1)
	  (cadr form))
	 (t
John Wiegley's avatar
John Wiegley committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	  ;; If a command desire to replace its execution form with
	  ;; another command form, all it needs to do is throw the new
	  ;; form using the exception tag `eshell-replace-command'.
	  ;; For example, let's say that the form currently being
	  ;; eval'd is:
	  ;;
	  ;;   (eshell-named-command "hello")
	  ;;
	  ;; Now, let's assume the 'hello' command is an Eshell alias,
	  ;; the definition of which yields the command:
	  ;;
	  ;;   (eshell-named-command "echo" (list "Hello" "world"))
	  ;;
	  ;; What the alias code would like to do is simply substitute
	  ;; the alias form for the original form.  To accomplish
	  ;; this, all it needs to do is to throw the substitution
	  ;; form with the `eshell-replace-command' tag, and the form
	  ;; will be replaced within the current command, and
	  ;; execution will then resume (iteratively) as before.
	  ;; Thus, aliases can even contain references to asynchronous
	  ;; sub-commands, and things will still work out as they
	  ;; should.
1125 1126 1127 1128 1129 1130
	  (let* (result
                 (new-form
                  (catch 'eshell-replace-command
                    (ignore
                     (setq result (eval form))))))
	    (if new-form
Gerd Moellmann's avatar
Gerd Moellmann committed
1131 1132 1133 1134 1135 1136 1137 1138
		(progn
		  (eshell-manipulate "substituting replacement form"
		    (setcar form (car new-form))
		    (setcdr form (cdr new-form)))
		  (eshell-do-eval form synchronous-p))
	      (if (and (memq (car form) eshell-deferrable-commands)
		       (not eshell-current-subjob-p)
		       result
John Wiegley's avatar
John Wiegley committed
1139
		       (eshell-processp result))
Gerd Moellmann's avatar
Gerd Moellmann committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
		  (if synchronous-p
		      (eshell/wait result)
		    (eshell-manipulate "inserting ignore form"
		      (setcar form 'ignore)
		      (setcdr form nil))
		    (throw 'eshell-defer result))
		(list 'quote result))))))))))))

;; command invocation

(defun eshell/which (command &rest names)
  "Identify the COMMAND, and where it is located."
1152
  (dolist (name (cons command names))
Gerd Moellmann's avatar
Gerd Moellmann committed
1153
    (let (program alias direct)
1154
      (if (eq (aref name 0) eshell-explicit-command-char)
Gerd Moellmann's avatar
Gerd Moellmann committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	  (setq name (substring name 1)
		direct t))
      (if (and (not direct)
	       (eshell-using-module 'eshell-alias)
	       (setq alias
		     (funcall (symbol-function 'eshell-lookup-alias)
			      name)))
	  (setq program
		(concat name " is an alias, defined as \""
			(cadr alias) "\"")))
      (unless program
	(setq program (eshell-search-path name))
	(let* ((esym (eshell-find-alias-function name))
	       (sym (or esym (intern-soft name))))
1169 1170
	  (if (and (or esym (and sym (fboundp sym)))
		   (or eshell-prefer-lisp-functions (not direct)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1171 1172 1173 1174 1175
	      (let ((desc (let ((inhibit-redisplay t))
			    (save-window-excursion
			      (prog1
				  (describe-function sym)
				(message nil))))))
1176 1177 1178 1179 1180 1181
		(setq desc (if desc (substring desc 0
					       (1- (or (string-match "\n" desc)
						       (length desc))))
			     ;; This should not happen.
			     (format "%s is defined, \
but no documentation was found" name)))
John Wiegley's avatar
John Wiegley committed
1182 1183
		(if (buffer-live-p (get-buffer "*Help*"))
		    (kill-buffer "*Help*"))
Gerd Moellmann's avatar
Gerd Moellmann committed
1184 1185 1186 1187 1188 1189
		(setq program (or desc name))))))
      (if (not program)
	  (eshell-error (format "which: no %s in (%s)\n"
				name (getenv "PATH")))
	(eshell-printn program)))))

1190 1191
(put 'eshell/which 'eshell-no-numeric-conversions t)

Gerd Moellmann's avatar
Gerd Moellmann committed
1192 1193 1194 1195 1196 1197
(defun eshell-named-command (command &optional args)
  "Insert output from a plain COMMAND, using ARGS.
COMMAND may result in an alias being executed, or a plain command."
  (setq eshell-last-arguments args
	eshell-last-command-name (eshell-stringify command))
  (run-hook-with-args 'eshell-prepare-command-hook)
1198
  (cl-assert (stringp eshell-last-command-name))
Gerd Moellmann's avatar
Gerd Moellmann committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
  (if eshell-last-command-name
      (or (run-hook-with-args-until-success
	   'eshell-named-command-hook eshell-last-command-name
	   eshell-last-arguments)
	  (eshell-plain-command eshell-last-command-name
				eshell-last-arguments))))

(defalias 'eshell-named-command* 'eshell-named-command)

(defun eshell-find-alias-function (name)
  "Check whether a function called `eshell/NAME' exists."
  (let* ((sym (intern-soft (concat "eshell/" name)))
1211
	 (file (symbol-file sym 'defun)))
1212
    ;; If the function exists, but is defined in an eshell module
Glenn Morris's avatar
Glenn Morris committed
1213
    ;; that's not currently enabled, don't report it as found.
Gerd Moellmann's avatar
Gerd Moellmann committed
1214
    (if (and file
Glenn Morris's avatar
Glenn Morris committed
1215 1216
	     (setq file (file-name-base file))
	     (string-match "\\`\\(em\\|esh\\)-\\([[:alnum:]]+\\)\\'" file))
1217
	(let ((module-sym
Glenn Morris's avatar
Glenn Morris committed
1218
	       (intern (concat "eshell-" (match-string 2 file)))))
1219 1220 1221 1222
	  (if (and (functionp sym)
		   (or (null module-sym)
		       (eshell-using-module module-sym)
		       (memq module-sym (eshell-subgroups 'eshell))))
1223 1224 1225 1226
	      sym))
      ;; Otherwise, if it's bound, return it.
      (if (functionp sym)
	  sym))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244

(defun eshell-plain-command (command args)
  "Insert output from a plain COMMAND, using ARGS.
COMMAND may result in either a Lisp function being executed by name,
or an external command."
  (let* ((esym (eshell-find-alias-function command))
	 (sym (or esym (intern-soft command))))
    (if (and sym (fboundp sym)
	     (or esym eshell-prefer-lisp-functions
		 (not (eshell-search-path command))))
	(eshell-lisp-command sym args)
      (eshell-external-command command args))))

(defun eshell-exec-lisp (printer errprint func-or-form args form-p)
  "Execute a lisp FUNC-OR-FORM, maybe passing ARGS.
PRINTER and ERRPRINT are functions to use for printing regular
messages, and errors.  FORM-P should be non-nil if FUNC-OR-FORM
represent a lisp form; ARGS will be ignored in that case."
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  (eshell-condition-case err
      (let ((result
             (save-current-buffer
               (if form-p
                   (eval func-or-form)
                 (apply func-or-form args)))))
        (and result (funcall printer result))
        result)
    (error
     (let ((msg (error-message-string err)))
       (if (and (not form-p)
                (string-match "^Wrong number of arguments" msg)
                (fboundp 'eldoc-get-fnsym-args-string))
           (let ((func-doc (eldoc-get-fnsym-args-string func-or-form)))
             (setq msg (format "usage: %s" func-doc))))
       (funcall errprint msg))
     nil)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

(defsubst eshell-apply* (printer errprint func args)
  "Call FUNC, with ARGS, trapping errors and return them as output.
PRINTER and ERRPRINT are functions to use for printing regular
messages, and errors."
  (eshell-exec-lisp printer errprint func args nil))

(defsubst eshell-funcall* (printer errprint func &rest args)
  "Call FUNC, with ARGS, trapping errors and return them as output."
  (eshell-apply* printer errprint func args))

(defsubst eshell-eval* (printer errprint form)
  "Evaluate FORM, trapping errors and returning them."
  (eshell-exec-lisp printer errprint form nil t))

(defsubst eshell-apply (func args)
  "Call FUNC, with ARGS, trapping errors and return them as output.
PRINTER and ERRPRINT are functions to use for printing regular
messages, and errors."
  (eshell-apply* 'eshell-print 'eshell-error func args))

(defsubst eshell-funcall (func &rest args)
  "Call FUNC, with ARGS, trapping errors and return them as output."
  (eshell-apply func args))

(defsubst eshell-eval (form)
  "Evaluate FORM, trapping errors and returning them."
  (eshell-eval* 'eshell-print 'eshell-error form))

(defsubst eshell-applyn (func args)
  "Call FUNC, with ARGS, trapping errors and return them as output.
PRINTER and ERRPRINT are functions to use for printing regular
messages, and errors."
  (eshell-apply* 'eshell-printn 'eshell-errorn func args))

(defsubst eshell-funcalln (func &rest args)
  "Call FUNC, with ARGS, trapping errors and return them as output."
  (eshell-applyn func args))

(defsubst eshell-evaln (form)
  "Evaluate FORM, trapping errors and returning them."
  (eshell-eval* 'eshell-printn 'eshell-errorn form))

1305 1306
(defvar eshell-last-output-end)         ;Defined in esh-mode.el.

Gerd Moellmann's avatar
Gerd Moellmann committed
1307 1308 1309 1310 1311 1312
(defun eshell-lisp-command (object &optional args)
  "Insert Lisp OBJECT, using ARGS if a function."
  (catch 'eshell-external               ; deferred to an external command
    (let* ((eshell-ensure-newline-p (eshell-interactive-output-p))
	   (result
	    (if (functionp object)
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
		(progn
		  (setq eshell-last-arguments args
			eshell-last-command-name
			(concat "#<function " (symbol-name object) ">"))
		  ;; if any of the arguments are flagged as numbers
		  ;; waiting for conversion, convert them now
		  (unless (get object 'eshell-no-numeric-conversions)
		    (while args
		      (let ((arg (car args)))
			(if (and (stringp arg)
				 (> (length arg) 0)
1324 1325
				 (not (text-property-not-all
				       0 (length arg) 'number t arg)))
1326
			    (setcar args (string-to-number arg))))
1327 1328 1329 1330
		      (setq args (cdr args))))
		  (eshell-apply object eshell-last-arguments))
	      (setq eshell-last-arguments args
		    eshell-last-command-name "#<Lisp object>")
Gerd Moellmann's avatar
Gerd Moellmann committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	      (eshell-eval object))))
      (if (and eshell-ensure-newline-p
	       (save-excursion
		 (goto-char eshell-last-output-end)
		 (not (bolp))))
	  (eshell-print "\n"))
      (eshell-close-handles 0 (list 'quote result)))))

(defalias 'eshell-lisp-command* 'eshell-lisp-command)

1341 1342
(provide 'esh-cmd)

Gerd Moellmann's avatar
Gerd Moellmann committed
1343
;;; esh-cmd.el ends here