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?

Koz Ross
  • 425
  • 3
  • 13

1 Answers1

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))
itsjeyd
  • 14,586
  • 3
  • 58
  • 87
  • 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 Nov 18 '14 at 08:27
  • @KozRoss Try using the `defadvice` version of the code I just added to my answer. – itsjeyd Nov 18 '14 at 08:31
  • I've got a *very* odd error message: ``python-shell-send-buffer: Wrong number of arguments: #[nil "ÆÇ!ÈÉ\"ÆÊ!ÈÉ \"Ë !Ë!Ì2`` – Koz Ross Nov 18 '14 at 08: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 Nov 18 '14 at 08:40