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

From: Theodor Thornhill <[email-masked]>
Date: Mon, 15 Jun 2020 22:05:59 +0000

Hey,
"Dmitry Gutov" <[email-masked]> writes:

[...]
> I see that the code uses project--completing-read-strict. I'm not
> entirely happy with that function, yet.
Ok, I just thought I'd use something already written :)

>
> Have you tried calling read-buffer with a PREDICATE argument instead?
> That would require extracting some code from project--list-buffers
> (defined in Philip's patch), but it should be straightforward.

The attached patch works fine, and is also not reliant on Philip's patch. I omitted the DEF argument, since then I had to do the filtering beforehand, and also in the PREDICATE function. Just seems a little redundant to me, but maybe it should be there

The complete patch is a lot smaller now as well!
What do you think?

Theo

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 218058b195..fa50bbe57b 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -753,6 +753,20 @@ project-compile
          (default-directory (project-root pr)))
     (compile command comint)))
 
+;;;###autoload
+(defun project-switch-to-buffer ()
+  "Switch to a buffer in the current project."
+  (interactive)
+  (let* ((pr (project-current t)))
+    (switch-to-buffer
+     (read-buffer
+      "Switch-to-buffer: " nil nil
+      (lambda (buffer)
+        (when-let* ((path (buffer-file-name (cdr buffer)))
+                    (true (file-truename path)))
+          (when (file-in-directory-p true (project-root pr))
+            'buffer)))))))
+
 
 ;;; Project list