I'm trying to customize my eshell to intercept python
to do two things:
- Open a new window and run python if no arguments are given (e.g.,
$ python
) - Run the command "per usual" if arguments are given (e.g.,
$ python foobar.py
)
So far I have something like this
(defun eshell/python (&rest cmd-args)
(if (not cmd-args)
(progn (run-python "python")
(select-window (split-window-below))
(switch-to-buffer "*Python*")
(balance-windows)
nil)
(message "Use '*python' to call python directly")))
I've tried replacing (message ...)
with a few different things:
Based on the ouput of eshell-parse-command "python test.py"
I tried
(progn (eshell-trap-errors
(eshell-named-command "python" cmd-args)))
but it hits a recursion limit.
Since *python test.py
does what I want, I then tried
(progn (eshell-trap-errors
(eshell-named-command (concat "*" "python") cmd-args)))
but that puts the python process in the background and interrupts stdout with the output of my eshell-prompt-function.
Finally, I've fiddled with shell-command
but I can't get it to write to the eshell buffer. In particular,
(progn (eshell-trap-errors
(shell-command (mapconcat (lambda(x) x) cmd-args " ")
(get-buffer "*eshell*") (get-buffer "*eshell*"))))
gives me a Text is read only
message and moves point to start of the eshell buffer.
Is what I'm looking for possible?
Edit 1
Running without eshell/python
defined, I've instead tried to avoid alias problems:
(defun eshell/gvr (&rest cmd-args)
(if (not cmd-args)
(progn (run-python "python")
(select-window (split-window-below))
(switch-to-buffer "*Python*")
(balance-windows)
nil)
(progn (eshell-trap-errors
(eshell-named-command "python" cmd-args)))))
If test.py
is
print "Hello World"
x = raw_input("What should I repeat? ")
print x
running gvr test.py
in eshell fails when I reply to the prompt because eshell tries to execute the input instead of handing it to python, but running python test.py
goes off without a hitch.
How can I get run my own subprocesses in eshell the same way that they happen by default?