Subject: Re: bug#17378: 24.3.1: xterm-mouse-event: Wrong type argument: wholenump, -6

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 20 Sep 2020 23:34:15 +0200

Stefan Monnier <[email-masked]> writes:

>> 5
>> ESC [ M 2 ESC [
>> ESC [ M 2 ESC [
>> ESC [ M 2 ESC [
>> ESC [ M 2 ESC [
>> ESC [ M 2 ESC [
>> ESC [ M 2 ESC [
>
> And here, again the "ESC [ M ?? ESC [" form for the mouse-5.
> I don't think there's much hope to support this mouse-5 click format.
> Looks like a bug somewhere in the terminal (or something stacked in
> between, like screen/pmux).

This was the final message in the thread six years ago, so I wonder
whether it's likely whether we'll make further progress here.  Instead
we can make xterm-mouse-event more defensive here and check whether the
click event is valid (like in the patch below).

Looking at the backtrace, I think this should fix the reported bug, so
I've installed it in Emacs 28.

diff --git a/lisp/xt-mouse.el b/lisp/xt-mouse.el
index 2b9fab556e..362d29b943 100644
--- a/lisp/xt-mouse.el
+++ b/lisp/xt-mouse.el
@@ -237,7 +237,10 @@ xterm-mouse-event
 		      (xterm-mouse--read-event-sequence extension))
 		     (t
 		      (error "Unsupported XTerm mouse protocol")))))
-    (when click
+    (when (and click
+               ;; In very obscure circumstances, the click may become
+               ;; invalid (see bug#17378).
+               (>= (nth 1 click) 0))
       (let* ((type (nth 0 click))
              (x    (nth 1 click))
              (y    (nth 2 click))

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no