1

I want to make a complement to run-python in python-mode. Here is my snippet:

(defun dpy-run-python ()
  (interactive)
  (when (python-shell-get-process)
      (delete-process (python-shell-get-process)))
  (run-python (python-shell-calculate-command) t)
  (python-shell-send-buffer t)
  )

Explain:

Each time when I run dpy-run-python,it kill running python shell if any exists, then create a new process at the same window, which looks like:

enter image description here

However, my code come out error when executing:

enter image description here

I have read the source code of python.el,it seems like some comint protocols between emacs and python shell process, but I have no idea what should I do.

6
  • Maybe running as a dedicated process does it for you? C-u M-x run-python RET - and get prompted for decicated. Jun 13, 2022 at 9:12
  • Actually, run-python in my snippet with the 2nd para t means a dedicated process. And the python shell in image is a dedicated process in fact.
    – Mr.Dust
    Jun 13, 2022 at 9:22
  • This is just a guess, but it looks like run-python will pass some special arguments to Python when it runs in a new buffer, but not in an existing buffer. Two suggestions: in part 1 you could not only kill the process but the buffer too; or, if you care about the contents of the buffer, there may be a specialized function you have to call instead of delete-process to tell python mode that you're starting afresh.
    – q.undertow
    Jun 14, 2022 at 5:57
  • In order to make sure a running process is killed first, maybe you should not start it as dedicated. Its easier to spot than. Otherwise you need something like M-x list-processes RET. Jun 14, 2022 at 5:57
  • @q.undertow Maybe you're right, there may be some potential arguments. And I do want to keep the contents in buffer. I'll try to look for the functions if any exists.
    – Mr.Dust
    Jun 15, 2022 at 5:44

0

Your Answer

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

Browse other questions tagged or ask your own question.