unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: 57885@debbugs.gnu.org
Subject: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 19:09:40 +0200	[thread overview]
Message-ID: <875yhmapwr.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 481 bytes --]

Tags: patch

I find this pretty much essential when running "notebooks" and the like.

I think "C-c C-n" would be a reasonable keybinding, but since I can't
find any precedent, I didn't include a keybinding.

Also, in case the `string-trim' call looks hacky, I should add that the
relationship between the shell buffer and process name is pretty much
hardcoded in python.el, so there doesn't seem to be anything more
reasonable to do that wouldn't require some major refactoring.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-progmodes-python.el-python-shell-restart-New-co.patch --]
[-- Type: text/patch, Size: 1556 bytes --]

From 3ccd5651604bd0e90b6504148e8c770355b2e3e4 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 17 Sep 2022 19:01:57 +0200
Subject: [PATCH] * lisp/progmodes/python.el (python-shell-restart): New
 command.

---
 lisp/progmodes/python.el | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 9f9439aac6..4965d436a0 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3217,6 +3217,25 @@ run-python
                   show)))
     (get-buffer-process buffer)))
 
+(defun python-shell-restart (&optional show)
+  "Restart the Python shell.
+With a prefix argument, also SHOW the buffer."
+  (interactive "P")
+  (with-current-buffer
+      (or (when (derived-mode-p 'inferior-python-mode)
+            (current-buffer))
+          (seq-some (lambda (dedicated)
+                      (get-buffer (format "*%s*" (python-shell-get-process-name
+                                                  dedicated))))
+                    '(buffer project nil))
+          (user-error "No Python shell"))
+    (when-let ((proc (get-buffer-process (current-buffer))))
+      (kill-process proc)
+      (while (accept-process-output proc)))
+    (python-shell-make-comint (python-shell-calculate-command)
+                              (string-trim (buffer-name) "\\*" "\\*")
+                              show)))
+
 (defun run-python-internal ()
   "Run an inferior Internal Python process.
 Input and output via buffer named after
-- 
2.37.3


             reply	other threads:[~2022-09-17 17:09 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-17 17:09 Augusto Stoffel [this message]
2022-09-17 17:15 ` bug#57885: [PATCH] Add a command to restart the Python shell Eli Zaretskii
2022-09-17 17:36   ` Augusto Stoffel
2022-09-17 17:32 ` Stefan Kangas
2022-09-17 17:39   ` Augusto Stoffel
2022-09-17 18:04     ` Stefan Kangas
2022-09-18  6:53       ` Augusto Stoffel
2022-09-18 10:53         ` Lars Ingebrigtsen
2022-09-19  3:32     ` Richard Stallman
2022-09-19  7:20       ` Augusto Stoffel
2022-09-28  2:52         ` Richard Stallman
2022-09-28  6:36           ` Augusto Stoffel
2022-09-29  3:00             ` Richard Stallman
2022-09-29  7:09               ` Augusto Stoffel
2022-10-07 22:47                 ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875yhmapwr.fsf@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=57885@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).