From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#54296: Add buffer-matching functionality Date: Thu, 14 Apr 2022 08:25:58 +0000 Message-ID: <87a6co5aw9.fsf@posteo.net> References: <87ee3d4cli.fsf@posteo.net> <87k0d35c82.fsf@gnus.org> <877d92unqn.fsf@posteo.net> <83bkyednvx.fsf@gnu.org> <87cziudmyg.fsf@posteo.net> <838rtheogd.fsf@gnu.org> <87ilskbgul.fsf@posteo.net> <83ilskcp9i.fsf@gnu.org> <87v8whio1a.fsf@posteo.net> <83czipkylw.fsf@gnu.org> <87pmmp9c63.fsf@posteo.net> <83v8wgk7tn.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11514"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 54296@debbugs.gnu.org, larsi@gnus.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Apr 14 10:27:24 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1neup1-0002qW-4c for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 14 Apr 2022 10:27:23 +0200 Original-Received: from localhost ([::1]:34416 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1neuoz-0001Xg-Sh for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 14 Apr 2022 04:27:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51636) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1neuof-0001VL-Vh for bug-gnu-emacs@gnu.org; Thu, 14 Apr 2022 04:27:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:32937) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1neuof-00077n-Lx for bug-gnu-emacs@gnu.org; Thu, 14 Apr 2022 04:27:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1neuof-0007Ae-Ij for bug-gnu-emacs@gnu.org; Thu, 14 Apr 2022 04:27:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 14 Apr 2022 08:27:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54296 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 54296-submit@debbugs.gnu.org id=B54296.164992477627496 (code B ref 54296); Thu, 14 Apr 2022 08:27:01 +0000 Original-Received: (at 54296) by debbugs.gnu.org; 14 Apr 2022 08:26:16 +0000 Original-Received: from localhost ([127.0.0.1]:55065 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1neunv-00079P-C1 for submit@debbugs.gnu.org; Thu, 14 Apr 2022 04:26:16 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:53913) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1neunm-00078i-Oj for 54296@debbugs.gnu.org; Thu, 14 Apr 2022 04:26:13 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 853D624002A for <54296@debbugs.gnu.org>; Thu, 14 Apr 2022 10:26:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1649924760; bh=rMI/wCVMZcMd2SEvIO1oRkBX15tTmgj2SLG6zmscOYs=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=kHE8peNkKI9dkv12Snlwv3IQ+1UYrJ2jzS5GXcUMPTvhSk08ElNjBxJ2k4w/ZiGk5 0v5MmtmBLFQvn3w1oV46m3orr3zxcEiMTxGPqF2JDRbm+2b2SS1dq/bsSXEWqDn9C/ UI66MZ7VGda45WM5SL4crV5p/YGcqvDiF8qfv/DhB5nf18E+x2OYlY0gRNkrwYqbfy 7J+Rgr5enuhmD91P6G5d2tPFHsN6kq2tCmtoDpq2yRxVHiiePnzJuO61kFiwfMsfMm f+KDJA+ed3ZY1mMicvlbCP5BatGOnIkiBk81kl19Qjjeed11YH8O/xclTr6NfLFjhp aq/J7a59RIghg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4KfCGH38hrz6tmT; Thu, 14 Apr 2022 10:25:59 +0200 (CEST) Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB In-Reply-To: <83v8wgk7tn.fsf@gnu.org> (Eli Zaretskii's message of "Mon, 14 Mar 2022 15:00:20 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:229856 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Cc: larsi@gnus.org, 54296@debbugs.gnu.org >> Date: Mon, 14 Mar 2022 08:21:56 +0000 >> >> Eli Zaretskii writes: >> >> >> From: Philip Kaludercic >> >> Cc: larsi@gnus.org, 54296@debbugs.gnu.org >> >> Date: Sun, 13 Mar 2022 20:40:49 +0000 >> >> >> >> Eli Zaretskii writes: >> >> >> >> >> From: Philip Kaludercic >> >> >> Cc: larsi@gnus.org, 54296@debbugs.gnu.org >> >> >> Date: Fri, 11 Mar 2022 16:21:06 +0000 >> >> >> >> >> >> > I think we want in general avoid comparison with major-mode, and >> >> >> > prefer derived-mode instead, and if so, IMO we had better >> >> >> > did as we >> >> >> > say and not exposed comparison to major mode unless we >> >> >> > absolutely >> >> >> > must. >> >> >> >> >> >> Would it be enough to clarify this point in the documentation >> >> >> string? >> >> > >> >> > What would you like to clarify? >> >> >> >> To clarify that the usage of (major-mode . foo-mode) might not be >> >> what >> >> the user intends, and that in most cases derived-mode is preferable. >> > >> > I suggested to "clarify" that by not providing the 'major-mode' >> > predicate at all. I still don't think I understand why it is so >> > important that we should provide a special case for it. >> >> It is not inherently important, it is just that if the predicate would >> also be used in project.el, then compatibility would have to be broken, >> as the distinction between `major-mode' and `derived-mode' exists there. > > Then project.el could use the predicate route, right? It's quite a > special case, AFAIU, so having a special solution is OK. I have updated the commits as you recommended, and add a commit deprecating the use of `derived-mode' in project.el. -- Philip Kaludercic --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Generalise-buffer-matching-from-project.el.patch >From f218d19837ce53e5b94d16356594a5f48bd8f3d1 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Mon, 7 Mar 2022 20:49:42 +0100 Subject: [PATCH 1/3] Generalise buffer matching from project.el * subr.el (buffer-match): Add function to check if a buffer satisfies a condition. (match-buffers): Returns all buffers that satisfy a condition. --- lisp/subr.el | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lisp/subr.el b/lisp/subr.el index e7d5d36461..d0b73db019 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6651,4 +6651,63 @@ string-lines If OMIT-NULLS, empty lines will be removed from the results." (split-string string "\n" omit-nulls)) +(defun buffer-match-p (condition buffer-or-name &optional arg) + "Return non-nil if BUFFER-OR-NAME matches CONDITION. +CONDITION is either: +- a regular expression, to match a buffer name, +- a predicate function that takes a buffer object and ARG as + arguments, and returns non-nil if the buffer matches, +- a cons-cell, where the car describes how to interpret the cdr. + The car can be one of the following: + * `major-mode': the buffer matches 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 condition, of which at + least one has to be met." + (letrec + ((buffer (get-buffer buffer-or-name)) + (match + (lambda (conditions) + (catch 'match + (dolist (condition conditions) + (when (cond + ((stringp condition) + (string-match-p condition (buffer-name buffer))) + ((functionp condition) + (if (eq 1 (cdr (func-arity condition))) + (funcall condition buffer) + (funcall condition buffer arg))) + ((eq (car-safe condition) 'major-mode) + (provided-mode-derived-p + (buffer-local-value 'major-mode buffer) + (cdr condition))) + ((eq (car-safe condition) 'not) + (not (funcall match (cdr condition)))) + ((eq (car-safe condition) 'or) + (funcall match (cdr condition))) + ((eq (car-safe condition) 'and) + (catch 'fail + (dolist (c conditions) + (unless (funcall match c) + (throw 'fail nil))) + t))) + (throw 'match t))))))) + (funcall match (list condition)))) + +(defun match-buffers (condition &optional buffers arg) + "Return a list of buffers that match CONDITION. +See `buffer-match' for details on CONDITION. By default all +buffers are checked, this can be restricted by passing an +optional argument BUFFERS, set to a list of buffers to check. +ARG is passed to `buffer-match', for predicate conditions in +CONDITION." + (let (bufs) + (dolist (buf (or buffers (buffer-list))) + (when (buffer-match-p condition (get-buffer buf) arg) + (push buf bufs))) + bufs)) + ;;; subr.el ends here -- 2.34.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-window.el-display-buffer-assq-regexp-Use-buffer-matc.patch >From b6e03a6d4dda8db47405475675f2defb5b8520d9 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Thu, 10 Mar 2022 10:59:52 +0100 Subject: [PATCH 2/3] * window.el (display-buffer-assq-regexp): Use buffer-match --- lisp/window.el | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lisp/window.el b/lisp/window.el index dd297a3169..2da2f8bb2c 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -7498,19 +7498,12 @@ display-buffer-fallback-action (defun display-buffer-assq-regexp (buffer-name alist action) "Retrieve ALIST entry corresponding to BUFFER-NAME. This returns the cdr of the alist entry ALIST if either its key -is a string that matches BUFFER-NAME, as reported by -`string-match-p'; or if the key is a function that returns -non-nil when called with three arguments: the ALIST key, -BUFFER-NAME and ACTION. ACTION should have the form of the -action argument passed to `display-buffer'." +satisfied a BUFFER-NAME per `buffer-match'. ACTION should have +the form of the action argument passed to `display-buffer'." (catch 'match (dolist (entry alist) - (let ((key (car entry))) - (when (or (and (stringp key) - (string-match-p key buffer-name)) - (and (functionp key) - (funcall key buffer-name action))) - (throw 'match (cdr entry))))))) + (when (buffer-match-p (car entry) buffer-name action) + (throw 'match (cdr entry)))))) (defvar display-buffer--same-window-action '(display-buffer-same-window -- 2.34.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0003-Update-project-kill-buffer-conditions-to-match-buffe.patch >From 1f2e73890f378ee4a25a6bcd2683f922115a4a58 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Thu, 14 Apr 2022 10:24:27 +0200 Subject: [PATCH 3/3] Update project-kill-buffer-conditions to match buffer-match-p * project.el (project-kill-buffer-conditions): Document the deprecation of the use of derived-mode (project--buffer-check): Have `major-mode' behave like `derived-mode' did previously, and issue a warning of `derived-mode' is used. --- lisp/progmodes/project.el | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index daaf86f327..1d04c19906 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -1203,16 +1203,20 @@ project-display-buffer-other-frame (defcustom project-kill-buffer-conditions '(buffer-file-name ; All file-visiting buffers are included. ;; Most of the temp buffers in the background: - (major-mode . fundamental-mode) + (lambda (buf) + (not (eq (buffer-local-value 'major-mode buf) + 'fundamental-mode))) ;; non-text buffer such as xref, occur, vc, log, ... - (and (derived-mode . special-mode) - (not (major-mode . help-mode))) - (derived-mode . compilation-mode) - (derived-mode . dired-mode) - (derived-mode . diff-mode) - (derived-mode . comint-mode) - (derived-mode . eshell-mode) - (derived-mode . change-log-mode)) + (and (major-mode . special-mode) + (lambda (buf) + (not (eq (buffer-local-value 'major-mode buf) + 'help-mode)))) + (major-mode . compilation-mode) + (major-mode . dired-mode) + (major-mode . diff-mode) + (major-mode . comint-mode) + (major-mode . eshell-mode) + (major-mode . change-log-mode)) "List of conditions to kill buffers related to a project. This list is used by `project-kill-buffers'. Each condition is either: @@ -1222,10 +1226,11 @@ project-kill-buffer-conditions - 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 killed if the buffer's major - mode is eq to the cons-cell's cdr - * `derived-mode': the buffer is killed if the buffer's major mode is derived from the major mode denoted by the cons-cell's - cdr + cdr. + * `derived-mode': the buffer is killed if the buffer's major + mode is eq to the cons-cell's cdr (this is deprecated and will + result in a warning if used). * `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. @@ -1285,10 +1290,13 @@ project--buffer-check (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) + (warn "The use of `derived-mode' in \ +`project--buffer-check' is deprecated.") + (provided-mode-derived-p + (buffer-local-value 'major-mode buf) + (cdr c))) + ((eq (car-safe c) 'major-mode) (provided-mode-derived-p (buffer-local-value 'major-mode buf) (cdr c))) -- 2.34.0 --=-=-=--