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