unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dgutov@yandex.ru>
To: Matthias Meulien <orontee@gmail.com>, emacs-devel <emacs-devel@gnu.org>
Subject: Re: Name of buffers created by project-shell
Date: Thu, 4 Mar 2021 05:21:59 +0200	[thread overview]
Message-ID: <d54bd3e3-5c2b-dd74-f2b6-d078eca97e8a@yandex.ru> (raw)
In-Reply-To: <87czwh9rv9.fsf@gmail.com>

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

Hi Matthias,

On 02.03.2021 23:18, Matthias Meulien wrote:
> When I C-x p v (project-vc-dir) from a buffer with project root called, 
> say, "myproject" , I switch to a buffer called *vc-dir*<myproject>.
> When I C-x p s (project-shell) from a buffer with same project root, I 
> switch to a buffer called *myproject-shell*
> 
> Later, I found it not very convenient when switching between buffers by 
> name using C-x b (switch-to-buffer) to have one buffer with a prefix 
> build from the project root and one with a suffix. Don't you think we 
> should homogenize those namings?   Since when I M-x shell then C-u M-x 
> shell, I switch to a *shell*<2> buffer, I'd suggest to rename the buffer 
> created by project-shell to *shell*<myproject>...

What you're asking for makes sense, but there is a snag because of how 
these commands are implemented.

vc-dir doesn't choose the buffer name format itself. It ultimately calls 
create-file-buffer (which has an advice made by uniquify) which renames 
the buffer based on uniquify-buffer-name-style (if uniquify is loaded in 
the current session), of course. The problem with that function is that 
it always creates a new buffer. So, before calling it, 
vc-dir-prepare-status-buffer does a search for existing buffer and can 
find existing one if it matches by major mode and default directory.

Both shell and eshell buffers can change their default-directory, but we 
can track which project they belong to with a new variable. See the 
attached patch.

There's one problem, though: when called with C-u, the piece of behavior 
which reads as "create a new inferior shell buffer even if one already 
exists" now creates buffers uniquely named according to uniquify's 
rules, which seems to mean

   *shell*
   emacs-master/*shell*
   vc/emacs-master/*shell*

instead of what one might expect, like

   emacs-master/*shell*
   emacs-master/*shell*<2>
   emacs-master/*shell*<3>

Perhaps the solution is not to go through uniquify for this, but then we 
project-shell can't really be consistent with project-vc-dir.

[-- Attachment #2: uniquify-project-shell.diff --]
[-- Type: text/x-patch, Size: 2794 bytes --]

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 944b886ce5..f61a3ebea6 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -917,6 +917,22 @@ project-vc-dir
   (interactive)
   (vc-dir (project-root (project-current t))))
 
+(defvar-local project--origin-project-root nil)
+
+(defun project--find-existing-buffer (mode root)
+  (seq-find
+   (lambda (buf)
+     (and
+      (buffer-live-p buf)
+      (provided-mode-derived-p
+       (buffer-local-value 'major-mode buf)
+       mode)
+      (equal (buffer-local-value
+              'project--origin-project-root
+              buf)
+             root)))
+   (buffer-list)))
+
 ;;;###autoload
 (defun project-shell ()
   "Start an inferior shell in the current project's root directory.
@@ -926,15 +942,12 @@ project-shell
 if one already exists."
   (interactive)
   (let* ((default-directory (project-root (project-current t)))
-         (default-project-shell-name
-           (concat "*" (file-name-nondirectory
-                        (directory-file-name
-                         (file-name-directory default-directory)))
-                   "-shell*"))
-         (shell-buffer (get-buffer default-project-shell-name)))
-    (if (and shell-buffer (not current-prefix-arg))
-        (pop-to-buffer shell-buffer)
-      (shell (generate-new-buffer-name default-project-shell-name)))))
+         (existing-buffer (project--find-existing-buffer
+                           'shell-mode default-directory)))
+    (if (and existing-buffer (not current-prefix-arg))
+        (pop-to-buffer existing-buffer)
+      (shell (create-file-buffer (expand-file-name "*shell*")))
+      (setq project--origin-project-root default-directory))))
 
 ;;;###autoload
 (defun project-eshell ()
@@ -946,15 +959,14 @@ project-eshell
   (interactive)
   (defvar eshell-buffer-name)
   (let* ((default-directory (project-root (project-current t)))
-         (eshell-buffer-name
-          (concat "*" (file-name-nondirectory
-                       (directory-file-name
-                        (file-name-directory default-directory)))
-                  "-eshell*"))
-         (eshell-buffer (get-buffer eshell-buffer-name)))
-    (if (and eshell-buffer (not current-prefix-arg))
-        (pop-to-buffer eshell-buffer)
-      (eshell t))))
+         (existing-buffer (project--find-existing-buffer
+                           'eshell-mode default-directory)))
+    (if (and existing-buffer (not current-prefix-arg))
+        (pop-to-buffer existing-buffer)
+      (let ((eshell-buffer-name
+             (buffer-name (create-file-buffer (expand-file-name "*eshell*")))))
+        (eshell)
+        (setq project--origin-project-root default-directory)))))
 
 ;;;###autoload
 (defun project-async-shell-command ()

  reply	other threads:[~2021-03-04  3:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-02 21:18 Name of buffers created by project-shell Matthias Meulien
2021-03-04  3:21 ` Dmitry Gutov [this message]
2021-03-04  4:03   ` Stefan Monnier
2021-03-04 17:50   ` Juri Linkov
2021-03-05 14:22     ` Dmitry Gutov
2021-03-14 13:46   ` Matthias Meulien
2021-03-15  0:41     ` Dmitry Gutov
2021-03-15 13:05       ` Matthias Meulien
2021-03-21 19:44       ` Dmitry Gutov
2021-03-23 20:08         ` Matthias Meulien
2021-03-24 20:12           ` Juri Linkov
2021-03-24 20:47             ` Dmitry Gutov
2021-05-08 17:09         ` Matthias Meulien
2021-05-10  0:08           ` Dmitry Gutov
2021-03-14 13:56   ` Matthias Meulien

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=d54bd3e3-5c2b-dd74-f2b6-d078eca97e8a@yandex.ru \
    --to=dgutov@yandex.ru \
    --cc=emacs-devel@gnu.org \
    --cc=orontee@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).