Eval region ipython interpreter in aquamacs

449 views Asked by At

I'm trying to make Aquamacs 3.0a GNU Emacs 24.3.50.2 work with iPython 1.2.0 as the interpreter. I was able to get iPython basically working using the instructions in python.el. In particular, I added the settings given in the top answer here to my .emacs file, but I'm still seeing some weird behavior.

Note: The settings in my .emacs file are apparently for iPython 0.11, not 1.2.0, and that may be the source of the problems. I'm not a lisp expert, so any help with this would be greatly appreciated.

Here's one symptom that I'm able to reproduce: the ^C^R (eval region) command may generate a lot of blank "In [X]:" from to the interpreter. To reproduce the problem in aquamacs, I make an file called test.py and place the following code in it:

spam = "spam "
spam = spam + spam
spam = spam + spam
print spam

Then I type ^C^P (start interpreter), return back to the test.py buffer, and type ^C^C (eval buffer). The output is:

(... iPython startup message ...)

In [1]: 
In [2]: 
In [3]: 
In [4]: spam spam spam spam 

In [5]: 

This is all fine. But then I highlight the final line print spam and then type ^C^R. The output:

In [5]: 
In [5]: 
In [5]: 

Odd, no? Things get much worse for longer files. Even when the active region is one line, I often see 15 blank "In [X]:" lines or more.

1

There are 1 answers

3
AudioBubble On BEST ANSWER

the ^C^R (eval region) command may generate a lot of blank "In [X]:" from to the interpreter.

Here is what I have been able to understand about the issue. What you are seeing is a side effect of how code is evaluated in comint-mode (this is a general mode on top of which python-mode's repl is written).

Basically it sends the string you selected to the inferior process as it is (including the newlines etc), then it receives the output from the inferior process and prints it in the emacs comint buffer (the repl you opened in emacs). The python code

spam = "spam "
spam = spam + spam
spam = spam + spam

do not produce any output that is why you see the blank "In [X]:" lines.

This is all fine. But then I highlight the final line print spam and then type ^C^R. The output:

After doing C-cC-r, if you switch to the python repl and press enter you will actually get the output printed. The issue I guess is that the python-mode is NOT adding the final newline (which triggers the evaluation) to the string sent to command interpreter. I was able to trace this issue to the function python-shell-send-string in python.el (do C-h fpython-shell-send-stringRET, you will see a link to python.el in the help buffer, click on it and emacs will take you to definition of function). Towards the end of the function, we have these lines

(when (or (not (string-match "\n$" string))
                (string-match "\n[ \t].*\n?$" string))
        (comint-send-string process "\n"))

It seems this checks whether the string just sent had a final newline, if not it explicitly sends a newline, this check fails for single line strings and python mode never inserts the last newline. This seems to be a bug, maybe this is fixed in emacs trunk will need to check.

UPDATE

I can confirm that this has been fixed in the emacs trunk. In fact both the issues you have reported have been fixed.

UPDATE

The python.el's github repository, which is now deprecated, has the instructions for getting the latest version of the library.

1) Download the latest version from this url

2) Add the directory, where you have downloaded python.el, to your load path by adding something like this to your init file

(add-to-list 'load-path "<name_of_the_directory>")

3) Finally require python-mode

(require 'python)