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