This is a simple way to use the text interface to Mathematica and get the advantages of the editing capabilities of the GNU readline. The Python script makes use of the builtin function raw_input(), which in Linux uses GNU readline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import os,sys f1=os.popen('math ', 'w') f1.flush() while 1: sys.stdout.write('') try: line=raw_input() f1.writelines(line+'\n') f1.flush() except KeyboardInterrupt: f1.close() break sys.stdout.write('\n') sys.exit()
This works on an xterm under the Linux operating system. To use it, give the code a name e.g. "wolfram.py", and start the Python intepreter in the xterm. Type
from wolfram import * That's it. The arrow keys should now functional with the up and down invoking the history function.
The initial position of the cursor may be incorrect. If anyone knows a more solid way achieve GNU readline editing, I would be pleased to hear about it. Also, it would nice if the whole thing could be started directly from the command line of the xterm, i.e.
rlwrap. I think you are looking for rlwrap:
(never used it, however)
Mathematica with readline input. Thanks for the clever idea! Wolfram Inc should have supplied line-editing themselves long ago.
Under Unix you can invoke your python-wrapped mathematica via a single command as follows.
Copy your Wolfram.py to a directory in your path such as /usr/local/bin, and put the following script (call it "mathcom", say) in the same directory:
Typing "mathcom" gets you a nice line-editable mathematica command line interface.
I run the shell in an emacs buffer. This gives me full emacs capability with mathematica---and with all shell commands. To the dismay of my system managers I submit emacs to the batch queue to get interactive use of our batch-only computers.
I have now tried Michele Simionato's suggestion above of using rlwrap. It works perfectly: just invoke mathematica with
rlwrap mathand you get command line editing and history. rlwrap is available as a package for Fedora and Debian Linux and perhaps other distributions as well.