unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#57885: [PATCH] Add a command to restart the Python shell
@ 2022-09-17 17:09 Augusto Stoffel
  2022-09-17 17:15 ` Eli Zaretskii
  2022-09-17 17:32 ` Stefan Kangas
  0 siblings, 2 replies; 15+ messages in thread
From: Augusto Stoffel @ 2022-09-17 17:09 UTC (permalink / raw)
  To: 57885

[-- 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


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-10-07 22:47 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-17 17:09 bug#57885: [PATCH] Add a command to restart the Python shell Augusto Stoffel
2022-09-17 17:15 ` 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

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).