0

I often open up interactive python sessions particular to the project that I'm working on, and will use M-x rename-buffer to title them *Python[Project1]*, Python[Project2]`, etc.

I would like to configure my C-c C-c to send to the correct project's buffer.

I know I can use python-shell-buffer-name . "Python[Project1]") to set a buffer's name, but what variable do I set to ensure that C-c C-c and similar will respect this, versus sending to *Python* or to the last used buffer, which may be *Python[project_wrong]* ?

I've looked at the following:

Mittenchops
  • 289
  • 1
  • 8

1 Answers1

1

One way to set temporarily python-shell-buffer-name during a function call is to call this function inside a let statement that sets python-shell-buffer-name.

(defun adl/elpy-shell-send-region-or-buffer ()
  "Send python- region or buffer to *Python[ PY-BUFFER ]*."
  (interactive)
  (let ((python-shell-buffer-name (concat "Python[" (file-name-base (buffer-name)) "]")))
    (elpy-shell-send-region-or-buffer)))

Using the function above instead of (elpy-shell-send-region-or-buffer), you will send the code of file.py to a buffer named *Python[file]*.

Code from different files with the same name will run in the same buffer.

If you change (file-name-base (buffer-name)) to default-directory, all files from the same directory will run in the same buffer named *Python[path/to/directory/]*.

If you change (file-name-base (buffer-name)) to (buffer-file-name), each file will run in a different buffer named *Python[path/to/directory/file.py]*. Note that unsaved buffers, e.g., *scratch*, will run in *Python[]*.

If none of the above satisfies you, choose a better string :).

You can replace "C-c C-c" key binding to the new function (or use other binding):

(add-hook 'elpy-mode-hook
          (lambda ()
            (define-key elpy-mode-map (kbd "C-c C-c") #'adl/elpy-shell-send-region-or-buffer)))
adl
  • 636
  • 3
  • 6