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