unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Manuel Uberti via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Dmitry Gutov <dgutov@yandex.ru>, 54100@debbugs.gnu.org
Subject: bug#54100: 29.0.50; Allow project-buffers to ignore some buffers
Date: Wed, 23 Feb 2022 10:48:37 +0100	[thread overview]
Message-ID: <2b6928a7-d4d3-e672-348f-56d7a136b879@inventati.org> (raw)
In-Reply-To: <5ec9d7c3-6107-c888-7497-fa584e1257db@inventati.org>

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

On 23/02/22 07:56, Manuel Uberti wrote:
> On 23/02/22 03:49, Dmitry Gutov wrote:
>> I suppose we can add a var similar to project-kill-buffer-conditions, call it 
>> project-switch-[to-]buffer-conditions, and have project-switch-to-buffer use it.
>>
>> Care to write a patch?
>>
>> You're already the best person to evaluate its performance, so... ;-)

I attached a patch. Let me know if I missed something or am approach the matter 
in a wrong way.

To try my code I used the following predicate (mu-ignored-buffers is in the 
first message of this ticket):

(defun mu-buffer-predicate (buffer)
   "Check if BUFFER is NOT a member of `mu-ignored-buffers'."
   (not (seq-contains-p mu-ignored-buffers
                        (buffer-name (cdr buffer))
                        #'string-match-p)))

And this setting:

(setq-default project-switch-to-buffer-conditions '(mu-buffer-predicate))

Note that I didn't bump the package-version on 
`project-switch-to-buffer-conditions' because I don't know if it is required.

-- 
Manuel Uberti
www.manueluberti.eu

[-- Attachment #2: 0001-Add-project-switch-to-buffer-conditions.patch --]
[-- Type: text/x-patch, Size: 5266 bytes --]

From 682ea21f669c46d90046a17d0ff6598e0624907d Mon Sep 17 00:00:00 2001
From: Manuel Uberti <manuel.uberti@inventati.org>
Date: Wed, 23 Feb 2022 09:25:32 +0100
Subject: [PATCH] Add project-switch-to-buffer-conditions

* lisp/progmodes/project.el (project-switch-to-buffer-conditions):
New defcustom.
* lisp/progmodes/project (project--switch-to-buffer-check):
New function.
(project-switch-to-buffer):
Use it (bug#54100).
---
 lisp/progmodes/project.el | 81 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 76 insertions(+), 5 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 880c5b5517..5357e1aa58 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1112,16 +1112,17 @@ project-compile
              compilation-buffer-name-function)))
     (call-interactively #'compile)))
 
-(defun project--read-project-buffer ()
+(defun project--read-project-buffer (&optional predicate)
   (let* ((pr (project-current t))
          (current-buffer (current-buffer))
          (other-buffer (other-buffer current-buffer))
          (other-name (buffer-name other-buffer))
          (buffers (project-buffers pr))
          (predicate
-          (lambda (buffer)
-            ;; BUFFER is an entry (BUF-NAME . BUF-OBJ) of Vbuffer_alist.
-            (memq (cdr buffer) buffers))))
+          (or predicate
+              (lambda (buffer)
+                ;; BUFFER is an entry (BUF-NAME . BUF-OBJ) of Vbuffer_alist.
+                (memq (cdr buffer) buffers)))))
     (read-buffer
      "Switch to buffer: "
      (when (funcall predicate (cons other-name other-buffer))
@@ -1129,6 +1130,72 @@ project--read-project-buffer
      nil
      predicate)))
 
+(defcustom project-switch-to-buffer-conditions nil
+  "List of conditions to filter the buffers to be switched to.
+This list is used by `project-switch-to-buffer'.
+Each condition is either:
+- a regular expression, to match a buffer name,
+- a predicate function that takes a buffer object as argument
+  and returns non-nil if the buffer should be switched to,
+- a cons-cell, where the car describes how to interpret the cdr.
+  The car can be one of the following:
+  * `major-mode': the buffer is switched to if the buffer's major
+    mode is eq to the cons-cell's cdr
+  * `derived-mode': the buffer is switched to if the buffer's major
+    mode is derived from the major mode denoted by the cons-cell's
+    cdr
+  * `not': the cdr is interpreted as a negation of a condition.
+  * `and': the cdr is a list of recursive conditions, that all have
+    to be met.
+  * `or': the cdr is a list of recursive conditions, of which at
+    least one has to be met.
+
+If any of these conditions are satisfied for a buffer in the
+current project, `project-switch-to-buffer' switches to it."
+  :type '(repeat (choice regexp function symbol
+                         (cons :tag "Major mode"
+                               (const major-mode) symbol)
+                         (cons :tag "Derived mode"
+                               (const derived-mode) symbol)
+                         (cons :tag "Negation"
+                               (const not) sexp)
+                         (cons :tag "Conjunction"
+                               (const and) sexp)
+                         (cons :tag "Disjunction"
+                               (const or) sexp)))
+  :version "29.1"
+  :group 'project
+  :package-version '(project . "0.8.2"))
+
+(defun project--switch-to-buffer-check (buf conditions)
+  "Check if buffer BUF matches any element of the list CONDITIONS.
+See `project-switch-to-buffer-conditions' for more details on the
+form of CONDITIONS."
+  (catch 'switch
+    (dolist (c conditions)
+      (when (cond
+             ((stringp c)
+              (string-match-p c (buffer-name buf)))
+             ((symbolp c)
+              (funcall c buf))
+             ((eq (car-safe c) 'major-mode)
+              (eq (buffer-local-value 'major-mode buf)
+                  (cdr c)))
+             ((eq (car-safe c) 'derived-mode)
+              (provided-mode-derived-p
+               (buffer-local-value 'major-mode buf)
+               (cdr c)))
+             ((eq (car-safe c) 'not)
+              (not (project--switch-to-buffer-check buf (cdr c))))
+             ((eq (car-safe c) 'or)
+              (project--switch-to-buffer-check buf (cdr c)))
+             ((eq (car-safe c) 'and)
+              (seq-every-p
+               (apply-partially #'project--switch-to-buffer-check
+                                buf)
+               (mapcar #'list (cdr c)))))
+        (throw 'switch t)))))
+
 ;;;###autoload
 (defun project-switch-to-buffer (buffer-or-name)
   "Display buffer BUFFER-OR-NAME in the selected window.
@@ -1136,7 +1203,11 @@ project-switch-to-buffer
 current project.  Two buffers belong to the same project if their
 project instances, as reported by `project-current' in each
 buffer, are identical."
-  (interactive (list (project--read-project-buffer)))
+  (interactive
+   (list (project--read-project-buffer
+          (lambda (buffer)
+            (project--switch-to-buffer-check
+             buffer project-switch-to-buffer-conditions)))))
   (switch-to-buffer buffer-or-name))
 
 ;;;###autoload
-- 
2.25.1


  reply	other threads:[~2022-02-23  9:48 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-22  8:27 bug#54100: 29.0.50; Allow project-buffers to ignore some buffers Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-22  9:41 ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-22 14:23   ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-23  2:49     ` Dmitry Gutov
2022-02-23  6:56       ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-23  9:48         ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-02-23  9:55           ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-23 15:00 ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-24  7:18 ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-25  2:33   ` Dmitry Gutov
2022-02-25  6:44     ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-26  1:50       ` Dmitry Gutov
2022-02-26  6:32         ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-27  3:27           ` Dmitry Gutov
2022-02-27  7:09             ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-27  7:41               ` Eli Zaretskii
2022-02-27  7:43                 ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors

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=2b6928a7-d4d3-e672-348f-56d7a136b879@inventati.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=54100@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    --cc=manuel.uberti@inventati.org \
    /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).