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