From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#56997: [PATCH] Analogue of project-shell for Python Date: Fri, 05 Aug 2022 09:31:02 +0200 Message-ID: <874jyr87ux.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33789"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: Philip Kaludercic , Stefan Monnier To: 56997@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 05 09:32:14 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oJroc-0008cL-6S for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 05 Aug 2022 09:32:14 +0200 Original-Received: from localhost ([::1]:35774 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oJroZ-0001YG-HA for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 05 Aug 2022 03:32:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57282) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJroQ-0001Xq-In for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 03:32:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:38017) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oJroQ-0006FJ-9J for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 03:32:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oJroQ-0004Tn-0f for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 03:32:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 05 Aug 2022 07:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 56997 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.165968467417159 (code B ref -1); Fri, 05 Aug 2022 07:32:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 5 Aug 2022 07:31:14 +0000 Original-Received: from localhost ([127.0.0.1]:55999 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oJrnd-0004Sg-Qd for submit@debbugs.gnu.org; Fri, 05 Aug 2022 03:31:14 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:51294) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oJrnZ-0004SW-U4 for submit@debbugs.gnu.org; Fri, 05 Aug 2022 03:31:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57204) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJrnZ-0001Pc-1Z for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 03:31:09 -0400 Original-Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]:37861) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oJrnW-00068y-OJ for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 03:31:08 -0400 Original-Received: by mail-ej1-x62c.google.com with SMTP id kb8so3549760ejc.4 for ; Fri, 05 Aug 2022 00:31:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:message-id:date:subject:cc:to:from:from:to :cc; bh=mze4LXLfUjCp5B6lnhDW7T6B2lRMATKKh5tPbQawbhE=; b=jaV8vMtdLIa5nLW1ziLLu7jTjsEzvJpOn7GtYvNQQyPfF1gQYZa+6cd00h50EsMxmV FuSr2WbYjcv6uvzrUTuQH+hrPmnrUrTSzxPPVMfAvKWmPGN0TevgJDYTB3esOA9XLN6W p0wdlP2LeqIi10r8Vwe1F7tGKtG4VwcIiDVWLwyLxuHVQ7n+NUwEoLFFfh5Eh1XMOFtn abSUbsVTTI4oCR3lGWA6zDGCW0o4iH0RtZNAPlxG4pt3DdQ9nXtgbUPE1AVAw9Y4TeFy R1fDn90Wl95RzToqofbGw4m0cHokwCQLLG4Tlvf/DApWmL0kB74lKq9CRSJfKirsZay6 +VNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc; bh=mze4LXLfUjCp5B6lnhDW7T6B2lRMATKKh5tPbQawbhE=; b=m1OZc5vN7xP20jfMxDt+oqjoU/G4bmeL0KsJvGKjf6yBq9H5epaRN7KYZZkz8D3UcE CbuG0WvpKrUBk0mgJ8O1TSuLFAR73I3pND9XnS6OV7uTneGPHzr5rC0Pi/cz0Ms2YaUY LXuS0HusvV2yba37omZ1fcD51Ni6dU0w3NGGz0Z09eXrHGWZPr9uvf9g8MzBRVd7rxai qLCgw4PC8+xyaKu8ApciKllJh44LhL4tGeheu7TvvzkSG6xccoQcJAoKKXZfyGB01/Pd UKmKgZjiWef6BgW3Wgh1CDwy0ilzvkT14DclPlFad/k5gc/Z/pdDL0YJ0/gAMhK9pD6t cazw== X-Gm-Message-State: ACgBeo3rgocyO1JXHglENxDCAdnFo1T4xjvVXTk5KU2YfU68xO+lSek1 dixiTO9WDCmcmVZAoP9x1EY= X-Google-Smtp-Source: AA6agR5qpLAPoKJhztfiX4WNCKyluM+LO/I6uHqtE7WWDO9B/vmpNh4WiJ14V+OrUwCGm8DYok7Yxw== X-Received: by 2002:a17:907:1c1c:b0:730:9473:7a0e with SMTP id nc28-20020a1709071c1c00b0073094737a0emr4397493ejc.492.1659684664560; Fri, 05 Aug 2022 00:31:04 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::a827]) by smtp.gmail.com with ESMTPSA id l1-20020a1709060cc100b0072ee9790894sm1243357ejh.197.2022.08.05.00.31.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 00:31:03 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::62c; envelope-from=arstoffel@gmail.com; helo=mail-ej1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:238832 Archived-At: --=-=-= Content-Type: text/plain The Python shell already allows a shell to be "dedicated" to a buffer. This patch adds the option to create a shell dedicated to a project, as well as the option to make all Python shells dedicated by default. Stefan: you mentioned python.el could use project.el without a hard dependency, so it remains compatible with old Emacsen. Is this a good approach? (Also: the added seq dependency is kinda superfluous now, but it's nice to have it available for future developments as well.) Philip: I'm working around the possible absence of 'read-multiple-choice' here. Not sure it's a popular/useful enough function to include in compat, but I thought I would bring this up. --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-Python-shells-dedicated-to-a-project.patch Content-Transfer-Encoding: quoted-printable >From 7174b34701cb201c088f0fce6a57aee9a75eef20 Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Thu, 4 Aug 2022 12:08:58 +0200 Subject: [PATCH] Python shells dedicated to a project These shells run in the project root directory and are shared among all project buffers. * lisp/progmodes/python.el: Require 'seq' and (optionally) 'project' libraries. (python-shell-dedicated): New user option (python-shell-get-process-name): Adapt to project-dedicated shells. (run-python): Offer possibility to create a project-dedicated shell, or use 'python-shell-dedicated' as the default behavior. (python-shell-get-buffer): Adapt to project-dedicated shells. --- etc/NEWS | 11 ++++++ lisp/progmodes/python.el | 79 ++++++++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 963aa22c68..fcbf7deba7 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2055,6 +2055,17 @@ the Galeon web browser was released in September, 20= 08. Note that this historical web browser is different from Mozilla Firefox; it is its predecessor. =20 +** Python Mode ++++ +*** Project shells and a new user option 'python-shell-dedicated' +When called with a prefix argument, 'run-python' now offers the choice +of creating a shell dedicated to the current project. This shell runs +in the project root directory and is shared among all project buffers. + +Without a prefix argument, the kind of shell (buffer-dedicated, +project-dedicated or global) is specified by the new +'python-shell-dedicated' variable. + ** Ruby Mode =20 --- diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index b8fc7d4c54..bbdd2a9502 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -5,7 +5,7 @@ ;; Author: Fabi=C3=A1n E. Gallina ;; URL: https://github.com/fgallina/python.el ;; Version: 0.28 -;; Package-Requires: ((emacs "24.4") (cl-lib "1.0")) +;; Package-Requires: ((emacs "24.4") (cl-lib "1.0") (seq "2.23")) ;; Maintainer: emacs-devel@gnu.org ;; Created: Jul 2010 ;; Keywords: languages @@ -245,6 +245,8 @@ (require 'ansi-color) (require 'cl-lib) (require 'comint) +(require 'project nil 'noerror) +(require 'seq) (eval-when-compile (require 'subr-x)) ;For `string-empty-p'. =20 ;; Avoid compiler warnings @@ -2218,6 +2220,16 @@ python-shell-compilation-regexp-alist "`compilation-error-regexp-alist' for inferior Python." :type '(alist regexp)) =20 +(defcustom python-shell-dedicated nil + "Whether to make Python shells dedicated by default. +This option influences `run-python' when called without a prefix +argument. If `buffer' or `project', create a Python shell +dedicated to the current buffer or its project (if one is found)." + :version "29.1" + :type '(choice (const :tag "To buffer" buffer) + (const :tag "To project" project) + (const :tag "Not dedicated" nil))) + (defvar python-shell-output-filter-in-progress nil) (defvar python-shell-output-filter-buffer nil) =20 @@ -2580,12 +2592,19 @@ python-shell-prompt-set-calculated-regexps =20 (defun python-shell-get-process-name (dedicated) "Calculate the appropriate process name for inferior Python process. -If DEDICATED is t returns a string with the form -`python-shell-buffer-name'[`buffer-name'] else returns the value -of `python-shell-buffer-name'." - (if dedicated - (format "%s[%s]" python-shell-buffer-name (buffer-name)) - python-shell-buffer-name)) +If DEDICATED is nil, this is simply `python-shell-buffer-name'. +If DEDICATED is `buffer' or `project', append the current buffer +name respectively the current project name." + (pcase dedicated + ('nil python-shell-buffer-name) + ('project + (if-let ((proj (and (featurep 'project) + (project-current)))) + (format "%s[%s]" python-shell-buffer-name (file-name-nondirectory + (directory-file-name + (project-root proj)))) + python-shell-buffer-name)) + (_ (format "%s[%s]" python-shell-buffer-name (buffer-name))))) =20 (defun python-shell-internal-get-process-name () "Calculate the appropriate process name for Internal Python process. @@ -3043,8 +3062,8 @@ run-python Argument CMD defaults to `python-shell-calculate-command' return value. When called interactively with `prefix-arg', it allows the user to edit such value and choose whether the interpreter -should be DEDICATED for the current buffer. When numeric prefix -arg is other than 0 or 4 do not SHOW. +should be DEDICATED to the current buffer or project. When +numeric prefix arg is other than 0 or 4 do not SHOW. =20 For a given buffer and same values of DEDICATED, if a process is already running for it, it will do nothing. This means that if @@ -3058,13 +3077,27 @@ run-python (if current-prefix-arg (list (read-shell-command "Run Python: " (python-shell-calculate-command= )) - (y-or-n-p "Make dedicated process? ") + (if (not (fboundp 'read-multiple-choice)) + (y-or-n-p "Make dedicated process? ") + (alist-get (car (read-multiple-choice "Make dedicated process?" + '((?b "to buffer") + (?p "to project") + (?n "no")))) + '((?b . buffer) (?p . project)))) (=3D (prefix-numeric-value current-prefix-arg) 4)) - (list (python-shell-calculate-command) nil t))) - (let ((buffer - (python-shell-make-comint - (or cmd (python-shell-calculate-command)) - (python-shell-get-process-name dedicated) show))) + (list (python-shell-calculate-command) + python-shell-dedicated + t))) + (let* ((project (and (eq 'project dedicated) + (featurep 'project) + (project-current t))) + (default-directory (if project + (project-root project) + default-directory)) + (buffer (python-shell-make-comint + (or cmd (python-shell-calculate-command)) + (python-shell-get-process-name dedicated) + show))) (get-buffer-process buffer))) =20 (defun run-python-internal () @@ -3094,15 +3127,13 @@ python-shell-get-buffer If current buffer is in `inferior-python-mode', return it." (if (derived-mode-p 'inferior-python-mode) (current-buffer) - (let* ((dedicated-proc-name (python-shell-get-process-name t)) - (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) - (global-proc-name (python-shell-get-process-name nil)) - (global-proc-buffer-name (format "*%s*" global-proc-name)) - (dedicated-running (comint-check-proc dedicated-proc-buffer-nam= e)) - (global-running (comint-check-proc global-proc-buffer-name))) - ;; Always prefer dedicated - (or (and dedicated-running dedicated-proc-buffer-name) - (and global-running global-proc-buffer-name))))) + (seq-some + (lambda (dedicated) + (let* ((proc-name (python-shell-get-process-name dedicated)) + (buffer-name (format "*%s*" proc-name))) + (when (comint-check-proc buffer-name) + buffer-name))) + '(buffer project nil)))) =20 (defun python-shell-get-process () "Return inferior Python process for current buffer." --=20 2.37.1 --=-=-=--