unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: Stefan Kangas <stefankangas@gmail.com>
Cc: 57885@debbugs.gnu.org
Subject: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 19:39:10 +0200	[thread overview]
Message-ID: <87sfkpaojl.fsf@gmail.com> (raw)
In-Reply-To: <CADwFkmmo6-=aV5QmcpZWX0tDY-g0yPUcYLM6Tu4YL+i+tw4_dA@mail.gmail.com> (Stefan Kangas's message of "Sat, 17 Sep 2022 13:32:53 -0400")

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

On Sat, 17 Sep 2022 at 13:32, Stefan Kangas wrote:

> Augusto Stoffel <arstoffel@gmail.com> writes:
>
>> 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.
>
> `C-c C-n' is usually used to go to the next thing, so maybe we could try
> to find some better key.

Okay, then let's decide later (or not decide :-)).

> Looks good to me (but I didn't test it), with two minor comments:
>
>> +(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))
>
> As a minor style point, I'd prefer:
>
>     (and (derived-mode-p 'inferior-python-mode)
>          (current-buffer))
>
> to make it clear that we want the value.


Okay

>> +          (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)))
>
> Should there be an error here if there is no running process?
>

No, and if fact, if the process is dead, you want to call this command
to start the shell again (it happens to me and it works).


[-- 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/x-patch, Size: 1631 bytes --]

From e0b1d4cb08a57e2170efb92b74c2ce60e4ea7113 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 | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 9f9439aac6..6ef468d2cb 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3217,6 +3217,26 @@ run-python
                   show)))
     (get-buffer-process buffer)))
 
+(defun python-shell-restart (&optional show)
+  "Restart the Python shell.
+Optional argument SHOW (interactively, the prefix argument), if
+non-nil, means also display the Python shell buffer."
+  (interactive "P")
+  (with-current-buffer
+      (or (and (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:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=87sfkpaojl.fsf@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=57885@debbugs.gnu.org \
    --cc=stefankangas@gmail.com \
    /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).