Subject: Re: bug#12689: 24.2; Eshell ${cmd} substitution

From: Samer Masterson <[email-masked]>
Date: Sun, 05 Apr 2015 20:41:04 -0700

Hi,

I haven't finished, but I'm putting this work on hold for now as I've 
spent ages on it, and I'll return to this when I've learned more about 
eshell's architecture. The state of my work is below.

The patch below addresses the points you brought up, but it is 
incomplete for two reasons:
 - my code uses eshell-wait-for-process, which doesn't seem to be the 
correct wait to actually wait for a process to end because it locks up 
Emacs (because the core of eshell-wait-for-process is a while loop).
 - "prompt$ ${ext_command_one} | ext_command_two" will not reset the 
eshell prompt to normal if ext_command_one takes longer than 
ext_command_two. It works normally when ext_command_two takes longer 
than ext_command_one.

So I need to figure out a couple things in order to unblock myself:
 - how eshell knows to wait for other external processes (excluding 
eshell-wait-for-process), and whether that can be used at command-level 
granularity. E.g. for "cmd ${ext_cmd0} | ext_cmd1", the entire process 
needs to wait for both sides of the pipe to finish, and "cmd" needs to 
wait for "ext_cmd0" to finish before executing). "cmd ${ext_cmd0}" also 
shouldn't lock Emacs.
 - where the entry point is for the command prompt, and how the command 
prompt is reset after a set of commands finishes.

I've cc'd johnw because he might know the answer to the above 
questions, and what the correct approach to fixing this should be. I'll 
return to this when I've learned more about how eshell works.

-samer

work-in-progress patch:

--- a/lisp/eshell/esh-cmd.el
+++ b/lisp/eshell/esh-cmd.el
@@ -851,8 +851,9 @@ This is used on systems where `start-process' is 
not supported."
 (defmacro eshell-as-subcommand (command)
   "Execute COMMAND using a temp buffer.
 This is used so that certain Lisp commands, such as `cd', when
-executed in a subshell, do not disturb the environment of the main
-Eshell buffer."
+executed in a subshell, do not disturb the environment of the
+main Eshell buffer.  If COMMAND is an external command, it is
+called synchronously."
   `(let ,eshell-subcommand-bindings
      ,command))

@@ -1234,14 +1235,21 @@ COMMAND may result in an alias being executed, 
or a plain command."
 (defun eshell-plain-command (command args)
   "Insert output from a plain COMMAND, using ARGS.
 COMMAND may result in either a Lisp function being executed by name,
-or an external command."
+or an external command.
+
+If COMMAND is in an external command and `eshell-in-subcommand-p'
+is true, `eshell-plain-command' is synchronous and waits for
+COMMAND to finish."
   (let* ((esym (eshell-find-alias-function command))
 	 (sym (or esym (intern-soft command))))
     (if (and sym (fboundp sym)
 	     (or esym eshell-prefer-lisp-functions
 		 (not (eshell-search-path command))))
 	(eshell-lisp-command sym args)
-      (eshell-external-command command args))))
+      (let ((ext (eshell-external-command command args)))
+        (if eshell-in-subcommand-p
+            (eshell-wait-for-process ext)
+          ext)))))