From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#65797: `buffer-match-p` should not use `func-arity` Date: Mon, 16 Oct 2023 12:33:10 -0400 Message-ID: References: <87v8cmct9b.fsf@breatheoutbreathe.in> <87sf7o38g1.fsf_-_@posteo.net> <871qf1rbdo.fsf@posteo.net> <87o7hz4zg2.fsf@posteo.net> <87sf7b8ker.fsf@posteo.net> <87y1h2blxq.fsf@posteo.net> <874jjqb715.fsf@posteo.net> <0371b6ff-58e4-961e-29c9-4efb65b82185@gutov.dev> <87v8bhtr3k.fsf@breatheoutbreathe.in> <3a282a0b-3efa-ec12-2143-0496ebf6c828@gutov.dev> <831qdxu3y8.fsf@gnu.org> <831qdwqupk.fsf@gnu.org> Reply-To: Stefan Monnier 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="35097"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 65797@debbugs.gnu.org, dmitry@gutov.dev, philipk@posteo.net, mattias.engdegard@gmail.com, joseph@breatheoutbreathe.in To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Oct 16 18:33:49 2023 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 1qsQXN-0008qE-3g for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 16 Oct 2023 18:33:49 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qsQXD-0007Fl-Qw; Mon, 16 Oct 2023 12:33:39 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qsQXC-0007FT-1j for bug-gnu-emacs@gnu.org; Mon, 16 Oct 2023 12:33:38 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qsQXB-0001y1-Kd for bug-gnu-emacs@gnu.org; Mon, 16 Oct 2023 12:33:37 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qsQXZ-000284-Kd for bug-gnu-emacs@gnu.org; Mon, 16 Oct 2023 12:34:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 16 Oct 2023 16:34:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65797 X-GNU-PR-Package: emacs Original-Received: via spool by 65797-submit@debbugs.gnu.org id=B65797.16974740298164 (code B ref 65797); Mon, 16 Oct 2023 16:34:01 +0000 Original-Received: (at 65797) by debbugs.gnu.org; 16 Oct 2023 16:33:49 +0000 Original-Received: from localhost ([127.0.0.1]:57695 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qsQXM-00027b-FF for submit@debbugs.gnu.org; Mon, 16 Oct 2023 12:33:49 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:15206) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qsQXH-00027L-H3 for 65797@debbugs.gnu.org; Mon, 16 Oct 2023 12:33:46 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id E1EC58072D; Mon, 16 Oct 2023 12:33:12 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1697473991; bh=GXlcLOU3+KGmLP2U2SKlW/Nrw6lJICclzR4RHIICOmY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=AbAB3nRzU1HarE+a7C5XynwVTDehmANOqtsJ7Tg1d6WP+bnX7lbWMqPjrd0EnPNx2 y0xG2c+y+bs4THMDR0aj9ZdceJ5IDe3YDLCcfaqe90ntigg+upjCRqSPU/0GbYoaY7 6WsyGCMmuTx2Xdre6z3Ow+ItERiSWTosQoKOPcQBv8K4yeHcVqTtIn+Mxf2sk4AZVX Kdw0E7vGAaqxasC4ptdUYWtUCRf3kuJfKQkCT9JLq0o6/2TMzNe9MCiwdbgXr73o5G X81iOvCtD56PvvOcXHNnKnGF318shNByBKdZf+Q4v1ZOrub+LNq3sqNebLjaX/BlgE uHm9kIxcYsDkg== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id F379D80189; Mon, 16 Oct 2023 12:33:10 -0400 (EDT) Original-Received: from alfajor (modemcable005.21-80-70.mc.videotron.ca [70.80.21.5]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id C3C301202B9; Mon, 16 Oct 2023 12:33:10 -0400 (EDT) In-Reply-To: <831qdwqupk.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 15 Oct 2023 09:13:27 +0300") 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:272577 Archived-At: --=-=-= Content-Type: text/plain > Sigh. I guess we can install this on emacs-29 and cross the > fingers... I'm not insisting on it. If you think it's risky, let's just have it on `master`. BTW, see below the actual patch, including changes to etc/NEWS, manual, as well as the addition of a runtime warning (without which I can't see how we'll ever be able to get rid of the ugly backward compatibility hack). Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=buffer-match.patch diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi index 86c47ae7310..a2d0f5687ba 100644 --- a/doc/lispref/buffers.texi +++ b/doc/lispref/buffers.texi @@ -957,10 +957,10 @@ Buffer List infinite recursion. @end defvar -@defun buffer-match-p condition buffer-or-name &optional arg +@defun buffer-match-p condition buffer-or-name &rest args This function checks if a buffer designated by @code{buffer-or-name} -satisfies the specified @code{condition}. Optional third argument -@var{arg} is passed to the predicate function in @var{condition}. A +satisfies the specified @code{condition}. Optional arguments +@var{args} are passed to the predicate function in @var{condition}. A valid @var{condition} can be one of the following: @itemize @bullet{} @item @@ -969,23 +969,21 @@ Buffer List name. @item A predicate function, which should return non-@code{nil} if the buffer -matches. If the function expects one argument, it is called with -@var{buffer-or-name} as the argument; if it expects 2 arguments, the -first argument is @var{buffer-or-name} and the second is @var{arg} -(or @code{nil} if @var{arg} is omitted). +matches. It is called with +@var{buffer-or-name} as the first argument followed by @var{args}. @item A cons-cell @code{(@var{oper} . @var{expr})} where @var{oper} is one of @table @code @item (not @var{cond}) Satisfied if @var{cond} doesn't satisfy @code{buffer-match-p} with -the same buffer and @code{arg}. +the same buffer and @code{args}. @item (or @var{conds}@dots{}) Satisfied if @emph{any} condition in @var{conds} satisfies -@code{buffer-match-p}, with the same buffer and @code{arg}. +@code{buffer-match-p}, with the same buffer and @code{args}. @item (and @var{conds}@dots{}) Satisfied if @emph{all} the conditions in @var{conds} satisfy -@code{buffer-match-p}, with the same buffer and @code{arg}. +@code{buffer-match-p}, with the same buffer and @code{args}. @item derived-mode Satisfied if the buffer's major mode derives from @var{expr}. @item major-mode @@ -998,14 +996,14 @@ Buffer List @end itemize @end defun -@defun match-buffers condition &optional buffer-list arg +@defun match-buffers condition &optional buffer-list &rest args This function returns a list of all buffers that satisfy the @code{condition}. If no buffers match, the function returns @code{nil}. The argument @var{condition} is as defined in @code{buffer-match-p} above. By default, all the buffers are considered, but this can be restricted via the optional argument @code{buffer-list}, which should be a list of buffers to consider. -Optional third argument @var{arg} will be passed to @var{condition} in +Remaining arguments @var{args} will be passed to @var{condition} in the same way as @code{buffer-match-p} does. @end defun diff --git a/etc/NEWS b/etc/NEWS index 3bd47a0112b..c74b4978afe 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -945,6 +945,13 @@ the file listing's performance is still optimized. * Incompatible Lisp Changes in Emacs 30.1 +** `buffer-match-p and `match-buffers` take `&rest args` +They used to take a single `&optional arg` and were documented to use +an unreliable hack to try and accommodate condition predicates that +don't accept this optional arg. +The new semantics makes no such affordances, tho the code still +supports it (with a warning) for backward compatibility. + ** 'post-gc-hook' runs after updating 'gcs-done' and 'gcs-elapsed'. --- diff --git a/lisp/subr.el b/lisp/subr.el index 58274987d71..0732319ccd0 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -7277,13 +7277,15 @@ string-lines (setq start (length string))))) (nreverse lines)))) -(defun buffer-match-p (condition buffer-or-name &optional arg) +(defvar buffer-match-p--past-warnings nil) + +(defun buffer-match-p (condition buffer-or-name &rest args) "Return non-nil if BUFFER-OR-NAME matches CONDITION. CONDITION is either: - the symbol t, to always match, - the symbol nil, which never matches, - a regular expression, to match a buffer name, -- a predicate function that takes BUFFER-OR-NAME and ARG as +- a predicate function that takes BUFFER-OR-NAME plus ARGS 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: @@ -7308,9 +7310,18 @@ buffer-match-p ((pred stringp) (string-match-p condition (buffer-name buffer))) ((pred functionp) - (if (eq 1 (cdr (func-arity condition))) - (funcall condition buffer-or-name) - (funcall condition buffer-or-name arg))) + (if (cdr args) + ;; New in Emacs>29.1. no need for compatibility hack. + (apply condition buffer-or-name args) + (condition-case-unless-debug err + (apply condition buffer-or-name args) + (wrong-number-of-arguments + (unless (member condition + buffer-match-p--past-warnings) + (message "%s" (error-message-string err)) + (push condition buffer-match-p--past-warnings)) + (apply condition buffer-or-name + (if args nil '(nil))))))) (`(major-mode . ,mode) (eq (buffer-local-value 'major-mode buffer) @@ -7332,17 +7343,17 @@ buffer-match-p (throw 'match t))))))) (funcall match (list condition)))) -(defun match-buffers (condition &optional buffers arg) +(defun match-buffers (condition &optional buffers &rest args) "Return a list of buffers that match CONDITION, or nil if none match. See `buffer-match-p' for various supported CONDITIONs. By default all buffers are checked, but the optional argument BUFFERS can restrict that: its value should be an explicit list of buffers to check. -Optional argument ARG is passed to `buffer-match-p', for +Optional arguments ARGS are passed to `buffer-match-p', for predicate conditions in CONDITION." (let (bufs) (dolist (buf (or buffers (buffer-list))) - (when (buffer-match-p condition (get-buffer buf) arg) + (when (apply #'buffer-match-p condition (get-buffer buf) args) (push buf bufs))) bufs)) diff --git a/lisp/window.el b/lisp/window.el index 2f9b46ebb0a..12d3fb1dfe7 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -7535,10 +7535,8 @@ display-buffer-alist arguments: a buffer to display and an alist of the same form as ALIST. See `display-buffer' for details. -`display-buffer' scans this alist until it either finds a -matching regular expression or the function specified by a -condition returns non-nil. In any of these cases, it adds the -associated action to the list of actions it will try." +`display-buffer' scans this alist until the CONDITION is satisfied +and adds the associated ACTION to the list of actions it will try." :type `(alist :key-type (choice :tag "Condition" regexp --=-=-=--