Running MathKernel from Emacs brings cursor to end of line

When I start MathKernel from the Emacs shell, there are always extra spaces inserted so the cursor goes to the far right side of the screen.

For example

% emacs -q M-x shell /Applications/Mathematica.app/Contents/MacOS/MathKernel


produces

Mathematica 9.0 for Mac OS X x86 (64-bit)
In[1]:=                                                                       |<-


where I am using |<- to show where the cursor is after the 70 whitespace characters are inserted after In[1]:=.

Does anyone knows why such a thing happens and how to prevent it?

-

This can be prevented by turning off the terminal input library. In Mathematica 9.0.1 or later, the -rawterm command line switch can be used when starting the kernel. For Mathematica 9.0.0 this can be accomplished by adding the following line

InternalNewInput=False;


to the kernel init.m` file.

-

I see the same problem running the Mma kernel 10 on Linux Debian Wheezy. This happens with emacs 23 and emacs 24; Running in either an X window or terminal. I can ssh into a remote machine from an emacs shell and run the Mma kernel on the remote machine and I see the problem. I do not see the problem if I run Mma 3.0 kernel. I had access to Mma 6 and 8 in the past and never saw the problem. But, I no longer have access and can't check them now. I do not have access to to Mma kernel 9 mentioned by the original poster. I tried both M-x shell and M-x eshell. Both show the problem. I spent quite a bit of time searching for similar problems with emacs shell mode, but found nothing.

EDIT: There are rumors saying that Mma/Wolfram is using GNU readline in kernels 9 and 10. ("rumor" not because they are hiding anything, but they don't document the kernel) Indeed, I notice that the arrow keys work with Kernel 10, and some other strange behavior as well. Ctrl-a Ctrl-k will kill the line, but you don't see the cursor move. This is all in a plain terminal. But it makes sense that this is what is causing the new emacs problem and why it does not occur for Mma kernel 8. Someone said that at a conference, a Wolfram employee said the readline keybindings are hardcoded. There are some posts that talk about removing some dlls in order to kill readline support. This may make emacs problem go away. It is extraordinarily difficult to get any information on using the Kernel from Wolfram web pages, which is strange since the document many things so well. But, if you google WolframKernel you find a page that lists some command line switches. I assume this is an exhaustive list, and I don't see one to disable readline. By the way, for me the new partial readline support breaks "rlwrap math" as well. We may have to wait to see if Wolfram improves this in their next version.

-