Subject: bug#74833: 31.0.50; Copy to OS clipboard doesn't work in macOS Terminal.app with xterm-mouse-mode enabled

From: Jared Finder <[email-masked]>
Date: Sat, 18 Jan 2025 21:21:51 -0800

On 2025-01-06 10:25, Stefan Monnier wrote:
<feedback trimmed>

Thanks for the feedback. Patch attached with most feedback attached.  
Not addressed feedback follows:

> - The above enables `xterm-mouse-mode` which will
>   then proceed to *not* obey `xterm--auto-xt-mouse-allowed-*` on any
>   further ttys used in the same session.
> 
>   IOW, I think the above test belongs in
>   `turn-on-xterm-mouse-tracking-on-terminal` rather than here.
> 
> - Maybe instead of `xterm-mouse-mode-called` we should test
> 
>     (eq xterm-mouse-mode 'when-safe)
> 
>   or maybe rename the var to something more explicit like
>   `xterm-mouse-mode-only-when-safe`.

Yeah, I wanted to implement auto enabling like this, but could not 
figure out a way that seemed ergonomic to a user.  The problem I could 
not solve is that I wanted to ensure that a user running M-x 
xterm-mouse-mode still toggled from whatever auto-enabling logic ended 
up choosing.  That is, if xterm-mouse-mode was auto-enabled, running M-x 
xterm-mouse-mode should disable it and if xterm-mouse-mode was not 
auto-enabled, running M-x xterm-mouse-mode should enable it.

> - Of course, the previous code was also very weird:
> 
>     (when xterm-mouse-mode (xterm-mouse-mode 1))
> 
>   says to enable the mode, but only when it's already enabled?!

Yeah, it's weird. I followed the pattern from term/linux.el.  That file 
has the exact same pattern for gpm-mouse-mode, for the exact same 
purpose.

   -- MJF

From 7a30a729a4d5fc25119298b1e135dd01915b7642 Mon Sep 17 00:00:00 2001
From: Jared Finder <[email-masked]>
Date: Sat, 18 Jan 2025 21:06:00 -0800
Subject: [PATCH] ; Clean up xterm-mouse-mode auto-enabling vars

* lisp/term/xterm.el (xterm--auto-xt-mouse-allowed-names)
(xterm--auto-xt-mouse-allowed-types): Use `rx' macro and
update docstring.
---
 lisp/term/xterm.el | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el
index 23e29400c2f..15101ebd59d 100644
--- a/lisp/term/xterm.el
+++ b/lisp/term/xterm.el
@@ -84,13 +84,13 @@ xterm-paste-ending-sequence
   "Characters sent by the terminal to end a bracketed paste.")
 
 (defconst xterm--auto-xt-mouse-allowed-names
-  (mapconcat (lambda (s) (concat "^" s "\\>"))
-             '("Konsole"
-               "WezTerm"
-               ;; "XTerm"   ;Disabled because OSC52 support is opt-in only.
-               "iTerm2"     ;OSC52 support has opt-in/out UI on first usage
-               "kitty")
-             "\\|")
+  (rx string-start
+      (or "Konsole"
+          "WezTerm"
+          ;; "XTerm"   ;Disabled because OSC52 support is opt-in only.
+          "iTerm2"     ;OSC52 support has opt-in/out UI on first usage
+          "kitty")
+      word-end)
   "Regexp for terminals that automatically enable `xterm-mouse-mode' at startup.
 This will get matched against the terminal's XTVERSION string.
 
@@ -105,14 +105,16 @@ xterm--auto-xt-mouse-allowed-names
 \"Mouse motion mode\" (DECSET1003): Allows Emacs to get event on mouse
     motion.
 
-Also see `xterm--auto-xt-mouse-allowed-types' which mtches against the
-value of TERM instead.")
+Also see `xterm--auto-xt-mouse-allowed-types' which matches against the
+value of TERM instead.  If either `xterm--auto-xt-mouse-allowed-names'
+or `xterm--auto-xt-mouse-allowed-types' matches, then `xterm-mouse-mode'
+will get enabled automatically.")
 
 (defconst xterm--auto-xt-mouse-allowed-types
-  (mapconcat (lambda (s) (concat "^" s "$"))
-             '("alacritty"
-               "contour")
-             "\\|")
+  (rx string-start
+      (or "alacritty"
+          "contour")
+      string-end)
   "Like `xterm--auto-xt-mouse-allowed-names', but for the terminal's type.
 This will get matched against the environment variable \"TERM\".")
 
-- 
2.39.5