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: Wed, 24 Aug 2022 18:55:33 +0200 Message-ID: <874jy1eg3e.fsf@gmail.com> References: <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="29886"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: Philip Kaludercic , 56997@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Aug 24 18:56:22 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 1oQtfy-0007bL-0Y for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 24 Aug 2022 18:56:22 +0200 Original-Received: from localhost ([::1]:45950 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQtfw-00063W-DF for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 24 Aug 2022 12:56:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51388) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQtff-00063H-Ty for bug-gnu-emacs@gnu.org; Wed, 24 Aug 2022 12:56:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:58241) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oQtfe-0006IB-NG for bug-gnu-emacs@gnu.org; Wed, 24 Aug 2022 12:56:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oQtfe-0002mD-6X for bug-gnu-emacs@gnu.org; Wed, 24 Aug 2022 12:56: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: Wed, 24 Aug 2022 16:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56997 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 56997-submit@debbugs.gnu.org id=B56997.166136014410651 (code B ref 56997); Wed, 24 Aug 2022 16:56:02 +0000 Original-Received: (at 56997) by debbugs.gnu.org; 24 Aug 2022 16:55:44 +0000 Original-Received: from localhost ([127.0.0.1]:47990 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oQtfM-0002lh-2P for submit@debbugs.gnu.org; Wed, 24 Aug 2022 12:55:44 -0400 Original-Received: from mail-ej1-f48.google.com ([209.85.218.48]:35641) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oQtfK-0002lS-4L for 56997@debbugs.gnu.org; Wed, 24 Aug 2022 12:55:43 -0400 Original-Received: by mail-ej1-f48.google.com with SMTP id n7so16520997ejh.2 for <56997@debbugs.gnu.org>; Wed, 24 Aug 2022 09:55:42 -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:references:in-reply-to :subject:cc:to:from:from:to:cc; bh=FlPAO6qjyDuML5RChuMAxjAoItHpNw/Kn60+VyOFI/w=; b=goISY7ZbtL1CJcCrR30fidZ6X3lBuJJJp1zkuQzjWY1s6h9NzUu6AW5lhdcrw6D83R YSucTi4om3gEjOdeHSyqQdc7xfUB2yoigGBKj7h1TIEV7Pa1+vu7A0f8qYTmi0K5ZiFx 6mmLAmkTs3e2ck2BwxvTueo712T996R30WMXQot8tF4h/OYIztwNSmRd6A0oLR3L5yDz FaKtVn91wmT9zhSQbbdvSrGs0rN6gZDcciXoPeL57XoAZo9sQlUnO+mGy21lXcyr70qU 0C/HXFGPsF6gxolIxDkKMh1N0KWK6MYD/2t7QwnndXgS9KPZhQOzrIMVvSk7238VES9l x85A== 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:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc; bh=FlPAO6qjyDuML5RChuMAxjAoItHpNw/Kn60+VyOFI/w=; b=MvQV+6zQ0N80MjSVDlNgFrjDX7SX52gFQpqzm2jkMsXZEC5VfUMw3OTO30e0/dDwur v67uvKnlqK4QTbcQcUHI8DV58ktfoLu1l45X/w+0kOavrY9qzZXBNf/EN0oUgCebb2xz uZxdt0RrX/tcjPrdvb0WzLOlDl40Fo0iFu3z4YhpblUOGgOtew2A5U/1UP49lCMVyExA Ig0QwwA46k3aKTJ9zYC2DBo02l+KREfHZF2OM47hjgt/jeV8+BEDWM3lltHKgYpVHFjt zbCLUFgU6RaO+DbuqOXC2BzMduzzwjfOhPwVBmw/z2hYkshu023I+x04WOUBz4OECci7 m45w== X-Gm-Message-State: ACgBeo22xlSJ8/P3ehuNC4C4oyEHaUsPhtD7O4AHeNc7mKBONKqWw6pG ySkMxEKiCYSv3JJeSntSthU= X-Google-Smtp-Source: AA6agR4RSs3OAC5C4Wk0L52xPyZZgLbUUIpVShbr37EosrStLTPJ63tBxj2Wn9yfRGBE0ppfi7p2Wg== X-Received: by 2002:a17:907:2bce:b0:73d:6ab5:17f4 with SMTP id gv14-20020a1709072bce00b0073d6ab517f4mr3583215ejc.632.1661360136204; Wed, 24 Aug 2022 09:55:36 -0700 (PDT) Original-Received: from ars3 (pd95c95b0.dip0.t-ipconnect.de. [217.92.149.176]) by smtp.gmail.com with ESMTPSA id y3-20020aa7d503000000b00445f3ba4674sm64188edq.63.2022.08.24.09.55.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 09:55:35 -0700 (PDT) In-Reply-To: (Stefan Monnier's message of "Tue, 23 Aug 2022 12:40:30 -0400") 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:240646 Archived-At: --=-=-= Content-Type: text/plain On Tue, 23 Aug 2022 at 12:40, Stefan Monnier wrote: > >> 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.) > > I must admit that I don't really know how important is the ability to > install python.el in older Emacsen. I know the maintainers of python.el > want it to be possible, but that doesn't preclude depending on `seq.el` > and `project.el` since those are also available on GNU ELPA. But it > does make it less convenient for the end user, so whether that matters > depends on its importance. I see the point of remaining compatible going back one or two versions, not so much with even older versions; it's not like a Python major mode is unavailable in Emacs 24 and 25. > I suggested a "soft" dependency as a possible choice, without implying > it's necessarily better or worse, sorry :-( In any case I don't see any other practical way to proceed... > But, FWIW the patch looks good to me. > >> -;; Package-Requires: ((emacs "24.4") (cl-lib "1.0")) > > BTW, Emacs-24.3 already provides cl-lib-1.0 so we can drop `cl-lib` from > this line. > > > Stefan I've attached a new patch with a requirement on compat, so we can use read-multiple-choice unconditionally (I also removed the unnecessary cl-lib from the Package-Requires header). --=-=-= 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 8b39ed7a724ae040798834c01ad11d4537fda0ee 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) 'compat' and '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 | 78 +++++++++++++++++++++++++++------------- 2 files changed, 65 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..31909e6dc2 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") (compat "28.1.2.1") (seq "2.23")) ;; Maintainer: emacs-devel@gnu.org ;; Created: Jul 2010 ;; Keywords: languages @@ -245,6 +245,9 @@ (require 'ansi-color) (require 'cl-lib) (require 'comint) +(require 'compat nil 'noerror) +(require 'project nil 'noerror) +(require 'seq) (eval-when-compile (require 'subr-x)) ;For `string-empty-p'. =20 ;; Avoid compiler warnings @@ -2218,6 +2221,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 +2593,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 +3063,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 +3078,25 @@ run-python (if current-prefix-arg (list (read-shell-command "Run Python: " (python-shell-calculate-command= )) - (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 +3126,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.2 --=-=-=--