2

Whenever I'm in Python mode and do C-c C-c, I have to answer two questions - firstly, what command to run (which defaults to /usr/bin/python2 -i for me) and secondly, whether I want a dedicated process. However, I want these things to be the same all the time (namely, use /usr/bin/python2 -i and not create a dedicated process), and don't want to have to tell Emacs this explicitly every time. How do I set it up so that these are defaults for C-c C-c, and won't require me to tell Emacs anything extra to just pressing C-c C-c?

Also, I would like to have Python start in a pop-up frame after the first call to C-c C-c. I tried code like this:

(setq display-buffer-alist
  (quote (("\\*Python\\*" display-buffer-pop-up-frame
     (nil)))))

However, this doesn't seem to affect the *Python* I get from C-c C-c. What am I missing?

1 Answer 1

5

C-c C-c is bound to python-shell-send-buffer by default. Normally, you'd run this command after creating a Python process via C-c C-p (run-python), which will not prompt for anything by default.

If you always want to be able to hit C-c C-c, irrespective of whether there is a Python process or not, you can advise python-shell-send-buffer as follows1:

(defun python-shell-send-buffer-no-prompt (&optional arg)
  (python-shell-get-or-create-process "/usr/bin/python -i" nil t))

(advice-add 'python-shell-send-buffer :before #'python-shell-send-buffer-no-prompt)

In conjunction with the code you posted, this also takes care of popping to a new frame and displaying the *Python* buffer there when it is first created: As the signature of python-shell-get-or-create-process indicates, the last argument specifies whether the *Python* buffer should be shown or not:

(python-shell-get-or-create-process &optional CMD DEDICATED SHOW)

1 This code was written to make use of the new advice system introduced in Emacs 24.4. If you are using an earlier version of Emacs, the following code will work:

(defadvice python-shell-send-buffer
    (before python-shell-send-buffer-no-prompt (&optional arg) activate compile)
  (python-shell-get-or-create-process "/usr/bin/python -i" nil t))
4
  • It seems my .emacs doesn't like it very much: it claims that advice-add function definition is void. Should I call this after or before a particular thing?
    – Koz Ross
    Commented Nov 18, 2014 at 8:27
  • @KozRoss Try using the defadvice version of the code I just added to my answer.
    – itsjeyd
    Commented Nov 18, 2014 at 8:31
  • I've got a very odd error message: python-shell-send-buffer: Wrong number of arguments: #[nil "ÆÇ!ÈÉ\"ÆÊ!ÈÉ \"Ë !Ë!Ì2
    – Koz Ross
    Commented Nov 18, 2014 at 8:37
  • @KozRoss That must be coming from customizations in your init-file. I tested both versions of the code in emacs -Q (Emacs version 24.4), and it is working for me.
    – itsjeyd
    Commented Nov 18, 2014 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.