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