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

From: Theodor Thornhill <[email-masked]>
Date: Mon, 15 Jun 2020 17:45:53 +0000

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