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