Subject: bug#41879: closed (Re: bug#41879: 28.0.50; [Patch]: Add project-switch-to-buffer in project.el)

From: [email-masked] (GNU bug Tracking System)
Date: Wed, 17 Jun 2020 23:12:02 +0000

Your bug report

#41879: 28.0.50; [Patch]: Add project-switch-to-buffer in project.el

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
41879: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=41879
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#41879: 28.0.50; [Patch]: Add project-switch-to-buffer in project.el
From: Dmitry Gutov <[email-masked]>
To: Theodor Thornhill <[email-masked]>, "Basil L. Contovounesios" <[email-masked]>
Cc: [email-masked]
Date: Thu, 18 Jun 2020 02:10:52 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

On 16.06.2020 21:58, Theodor Thornhill wrote:
> Got it. I stole your comment - I hope that is OK?

Also installed. Thanks!

Subject: 28.0.50; [Patch]: Add project-switch-to-buffer in project.el
From: Theodor Thornhill <[email-masked]>
To: [email-masked]
Date: Mon, 15 Jun 2020 17:45:53 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello!

This patch is directly dependent on https://debbugs.gnu.org/cgi/bugreport.cgi?bug=41868
since I reused one of the functions added by Philip K.

For now I rely on that report to be resolved before this.

However, attached is a patch for project-switch-to-buffer, where only file-bound buffers are included. I believe the function should be kind of self explanatory.

I use this daily, but stole the buffer list function since it was better than my initial one, and seems to be accepted soon :)

I find this useful, and hope others will as well. I bind it myself to 'C-x p b'.

The 'project--list-buffers' will not be part of the final patch, but I wanted to provide it so that you have a working example.

Theodor

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 218058b195..65f3285705 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -753,6 +753,35 @@ project-compile
          (default-directory (project-root pr)))
     (compile command comint)))
 
+(defun project--list-buffers (pr)
+  "Return a list of all buffers in project PR."
+  (let ((root (project-root pr))
+        bufs)
+    (dolist (buf (buffer-list))
+      (when-let* ((path (or (buffer-file-name buf)
+                            (buffer-local-value 'default-directory buf)))
+                  (true (file-truename path)))
+        (when (file-in-directory-p true root)
+          (push buf bufs))))
+    bufs))
+
+(defun project--buffer-file-names (pr)
+  "Return the buffers in project PR associated with a file."
+  (mapcar #'buffer-name
+          (cl-remove-if-not
+           (lambda (buffer) (buffer-file-name buffer))
+           (project--list-buffers pr))))
+
+;;;###autoload
+(defun project-switch-to-buffer ()
+  "Switch to a buffer in the current project."
+  (interactive)
+  (let ((pr (project-current t)))
+    (switch-to-buffer
+     (project--completing-read-strict
+      "Switch to buffer"
+      (project--buffer-file-names pr)))))
+
 
 ;;; Project list