esh-cmd.el 48 KB

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