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