Subject: bug#67220: 30.0.50; ERC 5.6: Prefer parameter-driven MODE processing in ERC

From: "J.P." <[email-masked]>
Date: Sat, 13 Apr 2024 15:17:00 -0700

"J.P." <[email-masked]> writes:

>> I think it's worth correcting this in ERC 5.6. Proposed changes
>> attached. (The first patch is unrelated.)
>
> These changes now live on master as
>
>   3d87e343276 * Use modern fallback for channel name detection in ERC
>   25d15391f26 * Normalize ISUPPORT params with empty values in ERC
>
> If anyone experiences new difficulties related to detecting channel
> names, these are likely to blame.

A regression involving `erc-query-buffer-p' has surfaced that basically
makes the function unsuable in many situations. The root cause is some
combination of stupdiity and laziness on my part, as usual. The attached
patch should fix the issue. Thanks to Libera user mekeor for reporting
this.

>From 415bde2403aa9564d138d0f504df36e6f9e956a3 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Sat, 13 Apr 2024 14:58:13 -0700
Subject: [PATCH] Fix regression involving erc-query-buffer-p

* lisp/erc/erc.el (erc-query-buffer-p): Don't return non-nil in
non-ERC buffers and server buffers, and continue to honor string
arguments.  The regression was introduced by 3d87e343 "Use modern
fallback for channel name detection in ERC".  Thanks to Libera user
mekeor for reporting this bug.
* test/lisp/erc/erc-tests.el (erc-query-buffer-p): New test.
(Bug#67220)
---
 lisp/erc/erc.el            |  9 +++++++--
 test/lisp/erc/erc-tests.el | 29 +++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 4ed77655f19..ecb884fb1ab 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -1662,8 +1662,13 @@ erc-open-server-buffer-p
 
 (defun erc-query-buffer-p (&optional buffer)
   "Return non-nil if BUFFER is an ERC query buffer.
-If BUFFER is nil, the current buffer is used."
-  (not (erc-channel-p (or buffer (current-buffer)))))
+If BUFFER is nil, use the current buffer."
+  (and-let* ((target (if buffer
+                         (progn (when (stringp buffer)
+                                  (setq buffer (get-buffer buffer)))
+                                (buffer-local-value 'erc--target buffer))
+                       erc--target)))
+    (not (erc--target-channel-p target))))
 
 (defun erc-ison-p (nick)
   "Return non-nil if NICK is online."
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 22432a68034..52c6f8e75b6 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1227,6 +1227,35 @@ erc-channel-p
 
   (erc-tests-common-kill-buffers))
 
+(ert-deftest erc-query-buffer-p ()
+  ;; Nil in a non-ERC buffer.
+  (should-not (erc-query-buffer-p))
+  (should-not (erc-query-buffer-p (current-buffer)))
+  (should-not (erc-query-buffer-p (buffer-name)))
+
+  (erc-tests-common-make-server-buf)
+  ;; Nil in a server buffer.
+  (should-not (erc-query-buffer-p))
+  (should-not (erc-query-buffer-p (current-buffer)))
+  (should-not (erc-query-buffer-p (buffer-name)))
+
+  ;; Nil in a channel buffer.
+  (with-current-buffer (erc--open-target "#chan")
+    (should-not (erc-query-buffer-p))
+    (should-not (erc-query-buffer-p (current-buffer)))
+    (should-not (erc-query-buffer-p (buffer-name))))
+
+  ;; Non-nil in a query buffer.
+  (with-current-buffer (erc--open-target "alice")
+    (should (erc-query-buffer-p))
+    (should (erc-query-buffer-p (current-buffer)))
+    (should (erc-query-buffer-p (buffer-name))))
+
+  (should (erc-query-buffer-p (get-buffer "alice")))
+  (should (erc-query-buffer-p "alice"))
+
+  (erc-tests-common-kill-buffers))
+
 (ert-deftest erc--valid-local-channel-p ()
   (ert-info ("Local channels not supported")
     (let ((erc--isupport-params (make-hash-table)))
-- 
2.44.0