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