Subject: bug#67836: 29.1.90; map-y-or-n-p doesn't terminate when run in a kmacro in batch mode

From: Spencer Baugh <[email-masked]>
Date: Fri, 15 Dec 2023 10:44:47 -0500

Patch fixing this.

>From f894cbede9b65e6449c80393efb9d3417c9f661c Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Fri, 15 Dec 2023 09:57:19 -0500
Subject: [PATCH] Make ding terminate keyboard macros even in batch mode

ding's docstring states that it terminates keyboard macros.  But, due
to what seems to be an oversight, it does not do that while executing
in batch mode.

Generally, this means that some functions may infinite loop while run
in a keyboard macro in batch mode.

One concrete example: map-y-or-n-p will infinite-loop if run in a
keyboard macro while in batch mode.

Now ding properly terminates keyboard macros while running in batch
mode.  A test showing map-y-or-n-p behaves correctly in keyboard
macros is included.

* src/dispnew.c (bitch_at_user): Always signal while in a keyboard
macro, even if in batch mode. (bug#67836)
* test/lisp/emacs-lisp/map-ynp-tests.el (map-ynp-tests-simple-call)
(test-map-ynp-kmacro): Add.
---
 src/dispnew.c                         |  6 ++--
 test/lisp/emacs-lisp/map-ynp-tests.el | 46 +++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 3 deletions(-)
 create mode 100644 test/lisp/emacs-lisp/map-ynp-tests.el

diff --git a/src/dispnew.c b/src/dispnew.c
index e4037494775..645311be8c0 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -6185,14 +6185,14 @@ DEFUN ("ding", Fding, Sding, 0, 1, 0,
 void
 bitch_at_user (void)
 {
-  if (noninteractive)
-    putchar (07);
-  else if (!INTERACTIVE)  /* Stop executing a keyboard macro.  */
+  if (!NILP (Vexecuting_kbd_macro))  /* Stop executing a keyboard macro.  */
     {
       const char *msg
 	= "Keyboard macro terminated by a command ringing the bell";
       Fsignal (Quser_error, list1 (build_string (msg)));
     }
+  else if (noninteractive)
+    putchar (07);
   else
     ring_bell (XFRAME (selected_frame));
 }
diff --git a/test/lisp/emacs-lisp/map-ynp-tests.el b/test/lisp/emacs-lisp/map-ynp-tests.el
new file mode 100644
index 00000000000..4f5d10ee7f9
--- /dev/null
+++ b/test/lisp/emacs-lisp/map-ynp-tests.el
@@ -0,0 +1,46 @@
+;;; map-ynp-tests.el --- Tests for map-ynp.el        -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2023 Free Software Foundation, Inc.
+
+;; Author: Spencer Baugh <[email-masked]>
+;; Maintainer: [email-masked]
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Tests for map-ynp.el.
+
+;;; Code:
+
+(require 'ert)
+
+(defun map-ynp-tests-simple-call ()
+  (map-y-or-n-p ""  #'ignore '(1)))
+
+(ert-deftest test-map-ynp-kmacro ()
+  "Test that map-y-or-n-p in a kmacro terminates on end of input"
+  (execute-kbd-macro (read-kbd-macro "M-: (map-ynp-tests-simple-call) RET y"))
+  (should-error
+   (execute-kbd-macro (read-kbd-macro "M-: (map-ynp-tests-simple-call) RET")))
+  (unless noninteractive
+    (let ((noninteractive t))
+      (execute-kbd-macro (read-kbd-macro "M-: (map-ynp-tests-simple-call) RET y"))
+      (should-error
+       (execute-kbd-macro (read-kbd-macro "M-: (map-ynp-tests-simple-call) RET"))))))
+
+(provide 'map-ynp-tests)
+;;; map-ynp-tests.el ends here
-- 
2.39.3