Mercurial > emacs
annotate lisp/eshell/esh-cmd.el @ 95948:d55ec23f052d
*** empty log message ***
| author | Glenn Morris <rgm@gnu.org> |
|---|---|
| date | Sun, 15 Jun 2008 02:53:17 +0000 |
| parents | 45dbb3c749a6 |
| children | b0ac9927a5c0 |
| 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))) |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
400 (save-excursion |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
401 (set-buffer buf) |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
402 (if (not tag) |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
403 (erase-buffer) |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
404 (insert "\n\C-l\n" tag "\n\n" text |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
405 (if subform |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
406 (concat "\n\n" (eshell-stringify subform)) "")))))) |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
407 |
| 29873 | 408 (defun eshell-debug-show-parsed-args (terms) |
| 409 "Display parsed arguments in the debug buffer." | |
| 410 (ignore | |
| 411 (if eshell-debug-command | |
| 412 (eshell-debug-command "parsed arguments" terms)))) | |
| 413 | |
| 414 (defun eshell-no-command-conversion (terms) | |
| 415 "Don't convert the command argument." | |
| 416 (ignore | |
| 417 (if (and (listp (car terms)) | |
| 418 (eq (caar terms) 'eshell-convert)) | |
| 419 (setcar terms (cadr (car terms)))))) | |
| 420 | |
| 421 (defun eshell-subcommand-arg-values (terms) | |
| 422 "Convert subcommand arguments {x} to ${x}, in order to take their values." | |
| 423 (setq terms (cdr terms)) ; skip command argument | |
| 424 (while terms | |
| 425 (if (and (listp (car terms)) | |
| 426 (eq (caar terms) 'eshell-as-subcommand)) | |
| 427 (setcar terms (list 'eshell-convert | |
| 428 (list 'eshell-command-to-value | |
| 429 (car terms))))) | |
| 430 (setq terms (cdr terms)))) | |
| 431 | |
| 432 (defun eshell-rewrite-sexp-command (terms) | |
| 433 "Rewrite a sexp in initial position, such as '(+ 1 2)'." | |
| 434 ;; this occurs when a Lisp expression is in first position | |
| 435 (if (and (listp (car terms)) | |
| 436 (eq (caar terms) 'eshell-command-to-value)) | |
| 437 (car (cdar terms)))) | |
| 438 | |
| 439 (eshell-deftest cmd lisp-command | |
| 440 "Evaluate Lisp command" | |
| 441 (eshell-command-result-p "(+ 1 2)" "3")) | |
| 442 | |
| 443 (eshell-deftest cmd lisp-command-args | |
| 444 "Evaluate Lisp command (ignore args)" | |
| 445 (eshell-command-result-p "(+ 1 2) 3" "3")) | |
| 446 | |
| 447 (defun eshell-rewrite-initial-subcommand (terms) | |
| 448 "Rewrite a subcommand in initial position, such as '{+ 1 2}'." | |
| 449 (if (and (listp (car terms)) | |
| 450 (eq (caar terms) 'eshell-as-subcommand)) | |
| 451 (car terms))) | |
| 452 | |
| 453 (eshell-deftest cmd subcommand | |
| 454 "Run subcommand" | |
| 455 (eshell-command-result-p "{+ 1 2}" "3\n")) | |
| 456 | |
| 457 (eshell-deftest cmd subcommand-args | |
| 458 "Run subcommand (ignore args)" | |
| 459 (eshell-command-result-p "{+ 1 2} 3" "3\n")) | |
| 460 | |
| 461 (eshell-deftest cmd subcommand-lisp | |
| 462 "Run subcommand + Lisp form" | |
| 463 (eshell-command-result-p "{(+ 1 2)}" "3\n")) | |
| 464 | |
| 465 (defun eshell-rewrite-named-command (terms) | |
| 466 "If no other rewriting rule transforms TERMS, assume a named command." | |
|
65167
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
467 (let ((sym (if eshell-in-pipeline-p |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
468 'eshell-named-command* |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
469 'eshell-named-command)) |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
470 (cmd (car terms)) |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
471 (args (cdr terms))) |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
472 (if args |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
473 (list sym cmd (append (list 'list) (cdr terms))) |
|
2980740e3f1c
(eshell-rewrite-named-command): Changed the code around a bit so that
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
474 (list sym cmd)))) |
| 29873 | 475 |
| 476 (eshell-deftest cmd named-command | |
| 477 "Execute named command" | |
| 478 (eshell-command-result-p "+ 1 2" "3\n")) | |
| 479 | |
|
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)) "*")))))) | |
| 842 ,(car pipeline))))) | |
| 843 | |
| 844 (defmacro eshell-do-pipelines-synchronously (pipeline) | |
| 845 "Execute the commands in PIPELINE in sequence synchronously. | |
| 846 Output of each command is passed as input to the next one in the pipeline. | |
| 847 This is used on systems where `start-process' is not supported." | |
| 848 (when (setq pipeline (cadr pipeline)) | |
| 849 `(let (result) | |
| 29873 | 850 (progn |
| 851 ,(when (cdr pipeline) | |
| 31241 | 852 `(let (output-marker) |
| 29873 | 853 (progn |
| 31241 | 854 (set 'output-marker ,(point-marker)) |
| 29873 | 855 (eshell-set-output-handle ,eshell-output-handle |
| 31241 | 856 'append output-marker) |
| 29873 | 857 (eshell-set-output-handle ,eshell-error-handle |
| 31241 | 858 'append output-marker)))) |
| 29873 | 859 ,(let ((head (car pipeline))) |
| 860 (if (memq (car head) '(let progn)) | |
| 861 (setq head (car (last head)))) | |
| 31241 | 862 ;;; FIXME: is deferrable significant here? |
| 29873 | 863 (when (memq (car head) eshell-deferrable-commands) |
| 864 (ignore | |
| 865 (setcar head | |
| 866 (intern-soft | |
| 867 (concat (symbol-name (car head)) "*")))))) | |
| 31241 | 868 ;; The last process in the pipe should get its handles |
| 869 ;; redirected as we found them before running the pipe. | |
| 870 ,(if (null (cdr pipeline)) | |
| 871 `(progn | |
| 872 (set 'eshell-current-handles tail-handles) | |
| 873 (set 'eshell-in-pipeline-p nil))) | |
| 874 (set 'result ,(car pipeline)) | |
| 875 ;; tailproc gets the result of the last successful process in | |
| 876 ;; the pipeline. | |
| 877 (set 'tailproc (or result tailproc)) | |
| 878 ,(if (cdr pipeline) | |
| 879 `(eshell-do-pipelines-synchronously (quote ,(cdr pipeline)))) | |
| 880 result)))) | |
| 29873 | 881 |
| 882 (defalias 'eshell-process-identity 'identity) | |
| 883 | |
| 884 (defmacro eshell-execute-pipeline (pipeline) | |
| 885 "Execute the commands in PIPELINE, connecting each to one another." | |
| 886 `(let ((eshell-in-pipeline-p t) tailproc) | |
| 887 (progn | |
| 31241 | 888 ,(if (fboundp 'start-process) |
| 889 `(eshell-do-pipelines ,pipeline) | |
| 890 `(let ((tail-handles (eshell-create-handles | |
| 891 (car (aref eshell-current-handles | |
| 892 ,eshell-output-handle)) nil | |
| 893 (car (aref eshell-current-handles | |
| 894 ,eshell-error-handle)) nil))) | |
| 895 (eshell-do-pipelines-synchronously ,pipeline))) | |
| 29873 | 896 (eshell-process-identity tailproc)))) |
| 897 | |
| 898 (defmacro eshell-as-subcommand (command) | |
| 899 "Execute COMMAND using a temp buffer. | |
| 900 This is used so that certain Lisp commands, such as `cd', when | |
| 901 executed in a subshell, do not disturb the environment of the main | |
| 902 Eshell buffer." | |
| 903 `(let ,eshell-subcommand-bindings | |
| 904 ,command)) | |
| 905 | |
| 906 (defmacro eshell-do-command-to-value (object) | |
| 907 "Run a subcommand prepared by `eshell-command-to-value'. | |
| 908 This avoids the need to use `let*'." | |
| 909 `(let ((eshell-current-handles | |
| 910 (eshell-create-handles value 'overwrite))) | |
| 911 (progn | |
| 912 ,object | |
| 913 (symbol-value value)))) | |
| 914 | |
| 915 (defmacro eshell-command-to-value (object) | |
| 916 "Run OBJECT synchronously, returning its result as a string. | |
| 917 Returns a string comprising the output from the command." | |
| 918 `(let ((value (make-symbol "eshell-temp"))) | |
| 919 (eshell-do-command-to-value ,object))) | |
| 920 | |
| 921 ;;;_* Iterative evaluation | |
| 922 ;; | |
| 923 ;; Eshell runs all of its external commands asynchronously, so that | |
| 924 ;; Emacs is not blocked while the operation is being performed. | |
| 925 ;; However, this introduces certain synchronization difficulties, | |
| 926 ;; since the Lisp code, once it returns, will not "go back" to finish | |
| 927 ;; executing the commands which haven't yet been started. | |
| 928 ;; | |
| 929 ;; What Eshell does to work around this problem (basically, the lack | |
| 930 ;; of threads in Lisp), is that it evaluates the command sequence | |
| 931 ;; iteratively. Whenever an asynchronous process is begun, evaluation | |
| 932 ;; terminates and control is given back to Emacs. When that process | |
| 933 ;; finishes, it will resume the evaluation using the remainder of the | |
| 934 ;; command tree. | |
| 935 | |
| 936 (defun eshell/eshell-debug (&rest args) | |
| 937 "A command for toggling certain debug variables." | |
| 938 (ignore | |
| 939 (cond | |
| 940 ((not args) | |
| 941 (if eshell-handle-errors | |
| 942 (eshell-print "errors\n")) | |
| 943 (if eshell-debug-command | |
| 944 (eshell-print "commands\n"))) | |
| 945 ((or (string= (car args) "-h") | |
| 946 (string= (car args) "--help")) | |
| 947 (eshell-print "usage: eshell-debug [kinds] | |
| 948 | |
| 949 This command is used to aid in debugging problems related to Eshell | |
| 950 itself. It is not useful for anything else. The recognized `kinds' | |
| 951 at the moment are: | |
| 952 | |
| 953 errors stops Eshell from trapping errors | |
| 954 commands shows command execution progress in `*eshell last cmd*' | |
| 955 ")) | |
| 956 (t | |
| 957 (while args | |
| 958 (cond | |
| 959 ((string= (car args) "errors") | |
| 960 (setq eshell-handle-errors (not eshell-handle-errors))) | |
| 961 ((string= (car args) "commands") | |
| 962 (setq eshell-debug-command (not eshell-debug-command)))) | |
| 963 (setq args (cdr args))))))) | |
| 964 | |
| 965 (defun pcomplete/eshell-mode/eshell-debug () | |
| 966 "Completion for the `debug' command." | |
| 967 (while (pcomplete-here '("errors" "commands")))) | |
| 968 | |
| 33020 | 969 (defun eshell-invoke-directly (command input) |
| 970 (let ((base (cadr (nth 2 (nth 2 (cadr command))))) name) | |
| 971 (if (and (eq (car base) 'eshell-trap-errors) | |
| 972 (eq (car (cadr base)) 'eshell-named-command)) | |
| 973 (setq name (cadr (cadr base)))) | |
| 974 (and name (stringp name) | |
| 975 (not (member name eshell-complex-commands)) | |
| 976 (catch 'simple | |
| 977 (progn | |
| 978 (eshell-for pred eshell-complex-commands | |
| 979 (if (and (functionp pred) | |
| 980 (funcall pred name)) | |
| 981 (throw 'simple nil))) | |
| 982 t)) | |
| 983 (fboundp (intern-soft (concat "eshell/" name)))))) | |
| 984 | |
| 29873 | 985 (defun eshell-eval-command (command &optional input) |
| 986 "Evaluate the given COMMAND iteratively." | |
| 987 (if eshell-current-command | |
| 988 ;; we can just stick the new command at the end of the current | |
| 989 ;; one, and everything will happen as it should | |
| 990 (setcdr (last (cdr eshell-current-command)) | |
| 991 (list (list 'let '((here (and (eobp) (point)))) | |
| 992 (and input | |
| 993 (list 'insert-and-inherit | |
| 994 (concat input "\n"))) | |
| 995 '(if here | |
| 996 (eshell-update-markers here)) | |
| 997 (list 'eshell-do-eval | |
| 998 (list 'quote command))))) | |
| 999 (and eshell-debug-command | |
| 1000 (save-excursion | |
| 1001 (let ((buf (get-buffer-create "*eshell last cmd*"))) | |
| 1002 (set-buffer buf) | |
| 1003 (erase-buffer) | |
| 1004 (insert "command: \"" input "\"\n")))) | |
| 1005 (setq eshell-current-command command) | |
| 31241 | 1006 (let ((delim (catch 'eshell-incomplete |
| 1007 (eshell-resume-eval)))) | |
|
42971
a4e7fd8ad209
(eshell-eval-command): If eshell-resume-eval
Eli Zaretskii <eliz@gnu.org>
parents:
38414
diff
changeset
|
1008 ;; 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
|
1009 ;; 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
|
1010 (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
|
1011 (setq delim (car delim))) |
|
42971
a4e7fd8ad209
(eshell-eval-command): If eshell-resume-eval
Eli Zaretskii <eliz@gnu.org>
parents:
38414
diff
changeset
|
1012 (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
|
1013 (error "Unmatched delimiter: %c" delim))))) |
| 29873 | 1014 |
| 1015 (defun eshell-resume-command (proc status) | |
| 1016 "Resume the current command when a process ends." | |
| 1017 (when proc | |
| 31241 | 1018 (unless (or (not (stringp status)) |
| 1019 (string= "stopped" status) | |
| 29873 | 1020 (string-match eshell-reset-signals status)) |
| 1021 (if (eq proc (eshell-interactive-process)) | |
| 1022 (eshell-resume-eval))))) | |
| 1023 | |
| 1024 (defun eshell-resume-eval () | |
| 1025 "Destructively evaluate a form which may need to be deferred." | |
| 1026 (eshell-condition-case err | |
| 1027 (progn | |
| 1028 (setq eshell-last-async-proc nil) | |
| 1029 (when eshell-current-command | |
| 1030 (let* (retval | |
| 1031 (proc (catch 'eshell-defer | |
| 1032 (ignore | |
| 1033 (setq retval | |
| 1034 (eshell-do-eval | |
| 1035 eshell-current-command)))))) | |
| 31241 | 1036 (if (eshell-processp proc) |
| 29873 | 1037 (ignore (setq eshell-last-async-proc proc)) |
| 1038 (cadr retval))))) | |
| 1039 (error | |
| 1040 (error (error-message-string err))))) | |
| 1041 | |
| 1042 (defmacro eshell-manipulate (tag &rest commands) | |
| 1043 "Manipulate a COMMAND form, with TAG as a debug identifier." | |
| 1044 (if (not eshell-debug-command) | |
| 1045 `(progn ,@commands) | |
| 1046 `(progn | |
| 1047 (eshell-debug-command ,(eval tag) form) | |
| 1048 ,@commands | |
| 1049 (eshell-debug-command ,(concat "done " (eval tag)) form)))) | |
| 1050 | |
| 1051 (put 'eshell-manipulate 'lisp-indent-function 1) | |
| 1052 | |
| 1053 ;; eshell-lookup-function, eshell-functionp, and eshell-macrop taken | |
| 1054 ;; from edebug | |
| 1055 | |
| 1056 (defsubst eshell-lookup-function (object) | |
| 1057 "Return the ultimate function definition of OBJECT." | |
| 1058 (while (and (symbolp object) (fboundp object)) | |
| 1059 (setq object (symbol-function object))) | |
| 1060 object) | |
| 1061 | |
| 1062 (defconst function-p-func | |
|
48211
e23f9344f37d
(function-p-func): Avoid `xemacs-p'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43335
diff
changeset
|
1063 (if (fboundp 'compiled-function-p) |
| 29873 | 1064 'compiled-function-p |
| 1065 'byte-code-function-p)) | |
| 1066 | |
| 1067 (defsubst eshell-functionp (object) | |
| 1068 "Returns the function named by OBJECT, or nil if it is not a function." | |
| 1069 (setq object (eshell-lookup-function object)) | |
| 1070 (if (or (subrp object) | |
| 1071 (funcall function-p-func object) | |
| 1072 (and (listp object) | |
| 1073 (eq (car object) 'lambda) | |
| 1074 (listp (car (cdr object))))) | |
| 1075 object)) | |
| 1076 | |
| 1077 (defsubst eshell-macrop (object) | |
| 1078 "Return t if OBJECT is a macro or nil otherwise." | |
| 1079 (setq object (eshell-lookup-function object)) | |
| 1080 (if (and (listp object) | |
| 1081 (eq 'macro (car object)) | |
| 1082 (eshell-functionp (cdr object))) | |
| 1083 t)) | |
| 1084 | |
| 1085 (defun eshell-do-eval (form &optional synchronous-p) | |
| 1086 "Evaluate form, simplifying it as we go. | |
| 1087 Unless SYNCHRONOUS-P is non-nil, throws `eshell-defer' if it needs to | |
| 1088 be finished later after the completion of an asynchronous subprocess." | |
| 1089 (cond | |
| 1090 ((not (listp form)) | |
| 1091 (list 'quote (eval form))) | |
| 1092 ((memq (car form) '(quote function)) | |
| 1093 form) | |
| 1094 (t | |
| 1095 ;; skip past the call to `eshell-do-eval' | |
| 1096 (when (eq (car form) 'eshell-do-eval) | |
| 1097 (setq form (cadr (cadr form)))) | |
| 1098 ;; expand any macros directly into the form. This is done so that | |
| 1099 ;; we can modify any `let' forms to evaluate only once. | |
| 1100 (if (eshell-macrop (car form)) | |
| 1101 (let ((exp (eshell-copy-tree (macroexpand form)))) | |
| 1102 (eshell-manipulate (format "expanding macro `%s'" | |
| 1103 (symbol-name (car form))) | |
| 1104 (setcar form (car exp)) | |
| 1105 (setcdr form (cdr exp))))) | |
| 1106 (let ((args (cdr form))) | |
| 1107 (cond | |
| 1108 ((eq (car form) 'while) | |
| 1109 ;; `eshell-copy-tree' is needed here so that the test argument | |
| 1110 ;; doesn't get modified and thus always yield the same result. | |
| 1111 (when (car eshell-command-body) | |
| 1112 (assert (not synchronous-p)) | |
| 1113 (eshell-do-eval (car eshell-command-body)) | |
| 31241 | 1114 (setcar eshell-command-body nil) |
| 1115 (setcar eshell-test-body nil)) | |
| 29873 | 1116 (unless (car eshell-test-body) |
| 1117 (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
| 31241 | 1118 (while (cadr (eshell-do-eval (car eshell-test-body))) |
| 1119 (setcar eshell-command-body (eshell-copy-tree (cadr args))) | |
| 1120 (eshell-do-eval (car eshell-command-body) synchronous-p) | |
| 1121 (setcar eshell-command-body nil) | |
| 1122 (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
| 29873 | 1123 (setcar eshell-command-body nil)) |
| 1124 ((eq (car form) 'if) | |
| 1125 ;; `eshell-copy-tree' is needed here so that the test argument | |
| 1126 ;; doesn't get modified and thus always yield the same result. | |
| 31241 | 1127 (if (car eshell-command-body) |
| 1128 (progn | |
| 1129 (assert (not synchronous-p)) | |
| 1130 (eshell-do-eval (car eshell-command-body))) | |
| 1131 (unless (car eshell-test-body) | |
| 1132 (setcar eshell-test-body (eshell-copy-tree (car args)))) | |
| 1133 (if (cadr (eshell-do-eval (car eshell-test-body))) | |
| 1134 (setcar eshell-command-body (eshell-copy-tree (cadr args))) | |
| 1135 (setcar eshell-command-body (eshell-copy-tree (car (cddr args))))) | |
| 1136 (eshell-do-eval (car eshell-command-body) synchronous-p)) | |
| 1137 (setcar eshell-command-body nil) | |
| 1138 (setcar eshell-test-body nil)) | |
| 29873 | 1139 ((eq (car form) 'setcar) |
| 1140 (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)) | |
| 1141 (eval form)) | |
| 1142 ((eq (car form) 'setcdr) | |
| 1143 (setcar (cdr args) (eshell-do-eval (cadr args) synchronous-p)) | |
| 1144 (eval form)) | |
| 1145 ((memq (car form) '(let catch condition-case unwind-protect)) | |
| 1146 ;; `let', `condition-case' and `unwind-protect' have to be | |
| 1147 ;; handled specially, because we only want to call | |
| 1148 ;; `eshell-do-eval' on their first form. | |
| 1149 ;; | |
| 1150 ;; NOTE: This requires obedience by all forms which this | |
| 1151 ;; function might encounter, that they do not contain | |
| 1152 ;; other special forms. | |
| 1153 (if (and (eq (car form) 'let) | |
| 1154 (not (eq (car (cadr args)) 'eshell-do-eval))) | |
| 1155 (eshell-manipulate "evaluating let args" | |
| 1156 (eshell-for letarg (car args) | |
| 1157 (if (and (listp letarg) | |
| 1158 (not (eq (cadr letarg) 'quote))) | |
| 1159 (setcdr letarg | |
| 1160 (list (eshell-do-eval | |
| 1161 (cadr letarg) synchronous-p))))))) | |
| 1162 (unless (eq (car form) 'unwind-protect) | |
| 1163 (setq args (cdr args))) | |
| 1164 (unless (eq (caar args) 'eshell-do-eval) | |
| 1165 (eshell-manipulate "handling special form" | |
| 1166 (setcar args (list 'eshell-do-eval | |
| 1167 (list 'quote (car args)) | |
| 1168 synchronous-p)))) | |
| 1169 (eval form)) | |
| 1170 (t | |
| 1171 (if (and args (not (memq (car form) '(run-hooks)))) | |
| 1172 (eshell-manipulate | |
| 1173 (format "evaluating arguments to `%s'" | |
| 1174 (symbol-name (car form))) | |
| 1175 (while args | |
| 1176 (setcar args (eshell-do-eval (car args) synchronous-p)) | |
| 1177 (setq args (cdr args))))) | |
| 1178 (cond | |
| 1179 ((eq (car form) 'progn) | |
| 1180 (car (last form))) | |
| 1181 ((eq (car form) 'prog1) | |
| 1182 (cadr form)) | |
| 1183 (t | |
| 33020 | 1184 ;; If a command desire to replace its execution form with |
| 1185 ;; another command form, all it needs to do is throw the new | |
| 1186 ;; form using the exception tag `eshell-replace-command'. | |
| 1187 ;; For example, let's say that the form currently being | |
| 1188 ;; eval'd is: | |
| 1189 ;; | |
| 1190 ;; (eshell-named-command "hello") | |
| 1191 ;; | |
| 1192 ;; Now, let's assume the 'hello' command is an Eshell alias, | |
| 1193 ;; the definition of which yields the command: | |
| 1194 ;; | |
| 1195 ;; (eshell-named-command "echo" (list "Hello" "world")) | |
| 1196 ;; | |
| 1197 ;; What the alias code would like to do is simply substitute | |
| 1198 ;; the alias form for the original form. To accomplish | |
| 1199 ;; this, all it needs to do is to throw the substitution | |
| 1200 ;; form with the `eshell-replace-command' tag, and the form | |
| 1201 ;; will be replaced within the current command, and | |
| 1202 ;; execution will then resume (iteratively) as before. | |
| 1203 ;; Thus, aliases can even contain references to asynchronous | |
| 1204 ;; sub-commands, and things will still work out as they | |
| 1205 ;; should. | |
| 29873 | 1206 (let (result new-form) |
| 1207 (if (setq new-form | |
| 1208 (catch 'eshell-replace-command | |
| 1209 (ignore | |
| 1210 (setq result (eval form))))) | |
| 1211 (progn | |
| 1212 (eshell-manipulate "substituting replacement form" | |
| 1213 (setcar form (car new-form)) | |
| 1214 (setcdr form (cdr new-form))) | |
| 1215 (eshell-do-eval form synchronous-p)) | |
| 1216 (if (and (memq (car form) eshell-deferrable-commands) | |
| 1217 (not eshell-current-subjob-p) | |
| 1218 result | |
| 31241 | 1219 (eshell-processp result)) |
| 29873 | 1220 (if synchronous-p |
| 1221 (eshell/wait result) | |
| 1222 (eshell-manipulate "inserting ignore form" | |
| 1223 (setcar form 'ignore) | |
| 1224 (setcdr form nil)) | |
| 1225 (throw 'eshell-defer result)) | |
| 1226 (list 'quote result)))))))))))) | |
| 1227 | |
| 1228 ;; command invocation | |
| 1229 | |
| 1230 (defun eshell/which (command &rest names) | |
| 1231 "Identify the COMMAND, and where it is located." | |
| 1232 (eshell-for name (cons command names) | |
| 1233 (let (program alias direct) | |
|
37817
431f430082e9
(eshell/which): Use `eshell-explicit-command-char' instead of ?*.
John Wiegley <johnw@newartisans.com>
parents:
37665
diff
changeset
|
1234 (if (eq (aref name 0) eshell-explicit-command-char) |
| 29873 | 1235 (setq name (substring name 1) |
| 1236 direct t)) | |
| 1237 (if (and (not direct) | |
| 1238 (eshell-using-module 'eshell-alias) | |
| 1239 (setq alias | |
| 1240 (funcall (symbol-function 'eshell-lookup-alias) | |
| 1241 name))) | |
| 1242 (setq program | |
| 1243 (concat name " is an alias, defined as \"" | |
| 1244 (cadr alias) "\""))) | |
| 1245 (unless program | |
| 1246 (setq program (eshell-search-path name)) | |
| 1247 (let* ((esym (eshell-find-alias-function name)) | |
| 1248 (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
|
1249 (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
|
1250 (or eshell-prefer-lisp-functions (not direct))) |
| 29873 | 1251 (let ((desc (let ((inhibit-redisplay t)) |
| 1252 (save-window-excursion | |
| 1253 (prog1 | |
| 1254 (describe-function sym) | |
| 1255 (message nil)))))) | |
| 1256 (setq desc (substring desc 0 | |
| 1257 (1- (or (string-match "\n" desc) | |
| 1258 (length desc))))) | |
| 31241 | 1259 (if (buffer-live-p (get-buffer "*Help*")) |
| 1260 (kill-buffer "*Help*")) | |
| 29873 | 1261 (setq program (or desc name)))))) |
| 1262 (if (not program) | |
| 1263 (eshell-error (format "which: no %s in (%s)\n" | |
| 1264 name (getenv "PATH"))) | |
| 1265 (eshell-printn program))))) | |
| 1266 | |
|
37662
cb20d33bef50
(eshell-lisp-command): Don't perform numeric conversions if a Lisp
John Wiegley <johnw@newartisans.com>
parents:
37657
diff
changeset
|
1267 (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
|
1268 |
| 29873 | 1269 (defun eshell-named-command (command &optional args) |
| 1270 "Insert output from a plain COMMAND, using ARGS. | |
| 1271 COMMAND may result in an alias being executed, or a plain command." | |
| 1272 (setq eshell-last-arguments args | |
| 1273 eshell-last-command-name (eshell-stringify command)) | |
| 1274 (run-hook-with-args 'eshell-prepare-command-hook) | |
| 1275 (assert (stringp eshell-last-command-name)) | |
| 1276 (if eshell-last-command-name | |
| 1277 (or (run-hook-with-args-until-success | |
| 1278 'eshell-named-command-hook eshell-last-command-name | |
| 1279 eshell-last-arguments) | |
| 1280 (eshell-plain-command eshell-last-command-name | |
| 1281 eshell-last-arguments)))) | |
| 1282 | |
| 1283 (defalias 'eshell-named-command* 'eshell-named-command) | |
| 1284 | |
| 1285 (defun eshell-find-alias-function (name) | |
| 1286 "Check whether a function called `eshell/NAME' exists." | |
| 1287 (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
|
1288 (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
|
1289 ;; 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
|
1290 ;; that's not currently enabled, don't report it as found |
| 29873 | 1291 (if (and file |
| 1292 (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
|
1293 (let ((module-sym |
| 29873 | 1294 (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
|
1295 (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
|
1296 (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
|
1297 (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
|
1298 (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
|
1299 (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
|
1300 (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
|
1301 sym)) |
|
f4b209194d8c
(eshell-find-alias-function): Return t in the case where the function
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
1302 ;; 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
|
1303 (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
|
1304 sym)))) |
| 29873 | 1305 |
| 1306 (defun eshell-plain-command (command args) | |
| 1307 "Insert output from a plain COMMAND, using ARGS. | |
| 1308 COMMAND may result in either a Lisp function being executed by name, | |
| 1309 or an external command." | |
| 1310 (let* ((esym (eshell-find-alias-function command)) | |
| 1311 (sym (or esym (intern-soft command)))) | |
| 1312 (if (and sym (fboundp sym) | |
| 1313 (or esym eshell-prefer-lisp-functions | |
| 1314 (not (eshell-search-path command)))) | |
| 1315 (eshell-lisp-command sym args) | |
| 1316 (eshell-external-command command args)))) | |
| 1317 | |
| 1318 (defun eshell-exec-lisp (printer errprint func-or-form args form-p) | |
| 1319 "Execute a lisp FUNC-OR-FORM, maybe passing ARGS. | |
| 1320 PRINTER and ERRPRINT are functions to use for printing regular | |
| 1321 messages, and errors. FORM-P should be non-nil if FUNC-OR-FORM | |
| 1322 represent a lisp form; ARGS will be ignored in that case." | |
| 1323 (let (result) | |
| 1324 (eshell-condition-case err | |
| 1325 (progn | |
| 1326 (setq result | |
| 1327 (save-current-buffer | |
| 1328 (if form-p | |
| 1329 (eval func-or-form) | |
| 1330 (apply func-or-form args)))) | |
| 1331 (and result (funcall printer result)) | |
| 1332 result) | |
| 1333 (error | |
| 1334 (let ((msg (error-message-string err))) | |
| 1335 (if (and (not form-p) | |
| 1336 (string-match "^Wrong number of arguments" msg) | |
| 1337 (fboundp 'eldoc-get-fnsym-args-string)) | |
| 1338 (let ((func-doc (eldoc-get-fnsym-args-string func-or-form))) | |
| 1339 (setq msg (format "usage: %s" func-doc)))) | |
| 1340 (funcall errprint msg)) | |
| 1341 nil)))) | |
| 1342 | |
| 1343 (defsubst eshell-apply* (printer errprint func args) | |
| 1344 "Call FUNC, with ARGS, trapping errors and return them as output. | |
| 1345 PRINTER and ERRPRINT are functions to use for printing regular | |
| 1346 messages, and errors." | |
| 1347 (eshell-exec-lisp printer errprint func args nil)) | |
| 1348 | |
| 1349 (defsubst eshell-funcall* (printer errprint func &rest args) | |
| 1350 "Call FUNC, with ARGS, trapping errors and return them as output." | |
| 1351 (eshell-apply* printer errprint func args)) | |
| 1352 | |
| 1353 (defsubst eshell-eval* (printer errprint form) | |
| 1354 "Evaluate FORM, trapping errors and returning them." | |
| 1355 (eshell-exec-lisp printer errprint form nil t)) | |
| 1356 | |
| 1357 (defsubst eshell-apply (func args) | |
| 1358 "Call FUNC, with ARGS, trapping errors and return them as output. | |
| 1359 PRINTER and ERRPRINT are functions to use for printing regular | |
| 1360 messages, and errors." | |
| 1361 (eshell-apply* 'eshell-print 'eshell-error func args)) | |
| 1362 | |
| 1363 (defsubst eshell-funcall (func &rest args) | |
| 1364 "Call FUNC, with ARGS, trapping errors and return them as output." | |
| 1365 (eshell-apply func args)) | |
| 1366 | |
| 1367 (defsubst eshell-eval (form) | |
| 1368 "Evaluate FORM, trapping errors and returning them." | |
| 1369 (eshell-eval* 'eshell-print 'eshell-error form)) | |
| 1370 | |
| 1371 (defsubst eshell-applyn (func args) | |
| 1372 "Call FUNC, with ARGS, trapping errors and return them as output. | |
| 1373 PRINTER and ERRPRINT are functions to use for printing regular | |
| 1374 messages, and errors." | |
| 1375 (eshell-apply* 'eshell-printn 'eshell-errorn func args)) | |
| 1376 | |
| 1377 (defsubst eshell-funcalln (func &rest args) | |
| 1378 "Call FUNC, with ARGS, trapping errors and return them as output." | |
| 1379 (eshell-applyn func args)) | |
| 1380 | |
| 1381 (defsubst eshell-evaln (form) | |
| 1382 "Evaluate FORM, trapping errors and returning them." | |
| 1383 (eshell-eval* 'eshell-printn 'eshell-errorn form)) | |
| 1384 | |
| 1385 (defun eshell-lisp-command (object &optional args) | |
| 1386 "Insert Lisp OBJECT, using ARGS if a function." | |
| 1387 (catch 'eshell-external ; deferred to an external command | |
| 1388 (let* ((eshell-ensure-newline-p (eshell-interactive-output-p)) | |
| 1389 (result | |
| 1390 (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
|
1391 (progn |
|
cb20d33bef50
(eshell-lisp-command): Don't perform numeric conversions if a Lisp
John Wiegley <johnw@newartisans.com>
parents:
37657
diff
changeset
|
1392 (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
|
1393 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
|
1394 (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
|
1395 ;; 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
|
1396 ;; 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
|
1397 (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
|
1398 (while args |
|
cb20d33bef50
(eshell-lisp-command): Don't perform numeric conversions if a Lisp
John Wiegley <johnw@newartisans.com>
parents:
37657
diff
changeset
|
1399 (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
|
1400 (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
|
1401 (> (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
|
1402 (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
|
1403 0 (length arg) 'number t arg))) |
|
37665
ebd292552bfe
Fixed reference to free variable.
John Wiegley <johnw@newartisans.com>
parents:
37662
diff
changeset
|
1404 (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
|
1405 (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
|
1406 (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
|
1407 (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
|
1408 eshell-last-command-name "#<Lisp object>") |
| 29873 | 1409 (eshell-eval object)))) |
| 1410 (if (and eshell-ensure-newline-p | |
| 1411 (save-excursion | |
| 1412 (goto-char eshell-last-output-end) | |
| 1413 (not (bolp)))) | |
| 1414 (eshell-print "\n")) | |
| 1415 (eshell-close-handles 0 (list 'quote result))))) | |
| 1416 | |
| 1417 (defalias 'eshell-lisp-command* 'eshell-lisp-command) | |
| 1418 | |
|
87079
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
1419 (provide 'esh-cmd) |
|
c1197dc2780b
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
1420 |
|
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
1421 ;; arch-tag: 8e4f3867-a0c5-441f-96ba-ddd142d94366 |
| 29873 | 1422 ;;; esh-cmd.el ends here |
