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

From: Andrii Kolomoiets <[email-masked]>
Date: Thu, 18 Jun 2020 21:03:13 +0300

Hi Theodor,

Please take a look at the attached patch.  It's based on your patch and
resolves this:

> - Check if other-buffer is passes the predicate function.  The buffer
>   from another project must not be the default.
> - Exclude the current buffer from the buffers list to switch to.

Thanks!

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 7a41df614b..b8d5731a71 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -777,14 +777,23 @@ project-compile
 (defun project-switch-to-buffer ()
   "Switch to a buffer in the current project."
   (interactive)
-  (let ((root (project-root (project-current t))))
+  (let* ((root (project-root (project-current t)))
+         (current-buffer (current-buffer))
+         (other-buffer (other-buffer current-buffer))
+         (other-name (buffer-name other-buffer))
+         (predicate
+          (lambda (buffer)
+            ;; BUFFER is an entry (BUF-NAME . BUF-OBJ) of Vbuffer_alist.
+            (and (not (eq (cdr buffer) current-buffer))
+                 (when-let ((file (buffer-local-value 'default-directory (cdr buffer))))
+                   (file-in-directory-p file root))))))
     (switch-to-buffer
      (read-buffer
-      "Switch to buffer: " nil t
-      (lambda (buffer)
-        ;; BUFFER is an entry (BUF-NAME . BUF-OBJ) of Vbuffer_alist.
-        (when-let ((file (buffer-file-name (cdr buffer))))
-          (file-in-directory-p file root)))))))
+      "Switch to buffer: "
+      (when (funcall predicate (cons other-name other-buffer))
+        other-name)
+      t
+      predicate))))
 
 (defcustom project-kill-buffers-skip-conditions
   '("\\*Help\\*")