Subject: 30.0.50; mouse-autoselect-window has no effect in terminal
From: Olaf Rogalsky <[email-masked]>
To: [email-masked]
Date: Wed, 20 Mar 2024 15:29:33 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

when using emacs in the terminal, the mouse-autoselect-window variable
has no effect, i.e. moving the mouse from one window to the next does
not change the selected window.

Recipe:
--- snip ---
emacs -nw
M-: (xterm-mouse-mode 1)
M-: (setq mouse-autoselect-window t)
C-x 2
<mouse-motion <<<other window>>> >
--- snap ---

The reason is, that xt-mouse.el does not generate <select-window>
events. The following patch fixes that.

To this end, it also fixes a bug in "window.el". Here, the
<select-window> event which is added to `last-input-event', does not
contain a propper posn field. In X11, this works fine, but in the
terminal it results in a "nil <select-window> is undefined" error.

Regards, Olaf

diff --git a/lisp/window.el b/lisp/window.el
index 246708dbd56..8af1cf485bd 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -10645,7 +10645,7 @@ mouse-autoselect-window-select
       ;; autoselection again, set `mouse-autoselect-window-state'."
       (setq mouse-autoselect-window-state 'select)
       (setq unread-command-events
-            (cons (list 'select-window (list window))
+            (cons (list 'select-window (posn-at-point nil window))
                   unread-command-events)))
      ((or (not (numberp mouse-autoselect-window))
           (equal mouse-position mouse-autoselect-window-position))
diff --git a/lisp/xt-mouse.el b/lisp/xt-mouse.el
index 081b8f32456..aaaffb3a534 100644
--- a/lisp/xt-mouse.el
+++ b/lisp/xt-mouse.el
@@ -42,6 +42,8 @@

 (require 'mwheel)

+(defvar last-mouse-window nil)
+
 (defvar xterm-mouse-debug-buffer nil)

 (defun xterm-mouse-translate (_event)
@@ -338,6 +340,15 @@ xterm-mouse-event
           (set-terminal-parameter nil 'xterm-mouse-last-click
                                   (list type this-time click-count x y)))

+        (when (and mouse-autoselect-window
+                   (mouse-movement-p event)
+                   (windowp w)
+                   (not (posn-area (event-start event))))
+          (when (and (not (eq w last-mouse-window))
+                     (not (eq w (selected-window))))
+            (setf (car event) 'select-window))
+          (setq last-mouse-window w))
+
         (set-terminal-parameter nil 'xterm-mouse-x x)
         (set-terminal-parameter nil 'xterm-mouse-y y)
         (setq last-input-event event)))))

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.18.0) of 2024-01-22 built on blaubaer
Repository revision: 797c688f4ab33a196477fd85f83f7438d113dc7d
Repository branch: master
System Description: Manjaro Linux

Configured using:
 'configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
 --localstatedir=/var --mandir=/usr/share/man --with-gameuser=:games
 --with-modules --without-m17n-flt --without-gconf
 --with-native-compilation=yes --with-xinput2 --with-x-toolkit=gtk3
 --without-xaw3d --with-sound=no --with-tree-sitter --without-gpm
 --without-compress-install
 '--program-transform-name=s/\([ec]tags\)/\1.emacs/'
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong
 -fno-plt' CPPFLAGS=-D_FORTIFY_SOURCE=2
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
LCMS2 LIBOTF LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY
PDUMPER PNG RSVG SECCOMP SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS
TREE_SITTER WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

Important settings:
  value of $LC_COLLATE: C.UTF-8
  value of $LC_MONETARY: de_DE.UTF-8
  value of $LC_NUMERIC: de_DE.UTF-8
  value of $LC_TIME: de_DE.UTF-8
  value of $LANG: de_DE.UTF-8
  locale-coding-system: utf-8-unix

Major mode: mu4e-headers

Minor modes in effect:
  mu4e-search-minor-mode: t
  hl-line-mode: t
  mu4e-update-minor-mode: t
  mu4e-context-minor-mode: t
  global-page-break-lines-mode: t
  openwith-mode: t
  winner-mode: t
  recentf-mode: t
  windmove-mode: t
  auto-compile-on-load-mode: t
  auto-compile-on-save-mode: t
  corfu-indexed-mode: t
  corfu-history-mode: t
  corfu-echo-mode: t
  corfu-doc-terminal-mode: t
  corfu-terminal-mode: t
  global-corfu-mode: t
  corfu-mode: t
  marginalia-mode: t
  vertico-mouse-mode: t
  vertico-indexed-mode: t
  vertico-mode: t
  xclip-mode: t
  save-place-mode: t
  global-goto-address-mode: t
  goto-address-mode: t
  savehist-mode: t
  minibuffer-depth-indicate-mode: t
  xterm-mouse-mode: t
  override-global-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  buffer-read-only: t
  size-indication-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
/home/y1rog/.emacs.d/elpa/popon-0.13/popon hides /home/y1rog/.emacs.d/elpa/popon/popon
/home/y1rog/.emacs.d/elpa/popup-20210317.138/popup hides /usr/share/emacs/site-lisp/popup
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company hides /usr/share/emacs/site-lisp/company-mode/company
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-files hides /usr/share/emacs/site-lisp/company-mode/company-files
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-etags hides /usr/share/emacs/site-lisp/company-mode/company-etags
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-capf hides /usr/share/emacs/site-lisp/company-mode/company-capf
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-keywords hides /usr/share/emacs/site-lisp/company-mode/company-keywords
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-dabbrev-code hides /usr/share/emacs/site-lisp/company-mode/company-dabbrev-code
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-bbdb hides /usr/share/emacs/site-lisp/company-mode/company-bbdb
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-dabbrev hides /usr/share/emacs/site-lisp/company-mode/company-dabbrev
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-template hides /usr/share/emacs/site-lisp/company-mode/company-template
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-gtags hides /usr/share/emacs/site-lisp/company-mode/company-gtags
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-semantic hides /usr/share/emacs/site-lisp/company-mode/company-semantic
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-yasnippet hides /usr/share/emacs/site-lisp/company-mode/company-yasnippet
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-elisp hides /usr/share/emacs/site-lisp/company-mode/company-elisp
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-css hides /usr/share/emacs/site-lisp/company-mode/company-css
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-cmake hides /usr/share/emacs/site-lisp/company-mode/company-cmake
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-clang hides /usr/share/emacs/site-lisp/company-mode/company-clang
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-tng hides /usr/share/emacs/site-lisp/company-mode/company-tng
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-tempo hides /usr/share/emacs/site-lisp/company-mode/company-tempo
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-oddmuse hides /usr/share/emacs/site-lisp/company-mode/company-oddmuse
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-nxml hides /usr/share/emacs/site-lisp/company-mode/company-nxml
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-abbrev hides /usr/share/emacs/site-lisp/company-mode/company-abbrev
/home/y1rog/.emacs.d/elpa/company-20221206.2122/company-ispell hides /usr/share/emacs/site-lisp/company-mode/company-ispell
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-bind-key hides /usr/share/emacs/site-lisp/use-package/use-package-bind-key
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-lint hides /usr/share/emacs/site-lisp/use-package/use-package-lint
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-diminish hides /usr/share/emacs/site-lisp/use-package/use-package-diminish
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-jump hides /usr/share/emacs/site-lisp/use-package/use-package-jump
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package hides /usr/share/emacs/site-lisp/use-package/use-package
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-delight hides /usr/share/emacs/site-lisp/use-package/use-package-delight
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-core hides /usr/share/emacs/site-lisp/use-package/use-package-core
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-ensure hides /usr/share/emacs/site-lisp/use-package/use-package-ensure
/usr/share/emacs/site-lisp/use-package/bind-key hides /usr/share/emacs/30.0.50/lisp/bind-key
/home/y1rog/.emacs.d/elpa/transient-20221202.1727/transient hides /usr/share/emacs/30.0.50/lisp/transient
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-bind-key hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-bind-key
/usr/share/emacs/site-lisp/use-package/use-package-ensure-system-package hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-ensure-system-package
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-lint hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-lint
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-diminish hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-diminish
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-jump hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-jump
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package hides /usr/share/emacs/30.0.50/lisp/use-package/use-package
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-delight hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-delight
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-core hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-core
/home/y1rog/.emacs.d/elpa/use-package-20210207.1926/use-package-ensure hides /usr/share/emacs/30.0.50/lisp/use-package/use-package-ensure

Features:
(shadow emacsbug smerge-mode diff diff-mode shortdoc cus-edit cus-start
cus-load vertico-directory help-fns radix-tree org-element org-persist
org-id org-refile oc-basic ol-eww eww url-queue mm-url ol-rmail ol-mhe
ol-irc ol-info ol-gnus nnselect ol-docview doc-view filenotify jka-compr
image-mode exif ol-bibtex bibtex ol-bbdb ol-w3m ol-doi org-link-doi
shr-color qp mm-archive sort gnus-cite mail-extr textsec uni-scripts
idna-mapping ucs-normalize uni-confusable textsec-check my-email mu4e
mu4e-org org ob ob-tangle ob-ref ob-lob ob-table ob-exp org-macro
org-src ob-comint org-pcomplete org-list org-footnote org-faces
org-entities ob-emacs-lisp ob-core ob-eval org-cycle org-table ol
org-fold org-fold-core org-keys oc org-loaddefs find-func org-version
org-compat org-macs mu4e-main mu4e-view gnus-art mm-uu mml2015 mm-view
mml-smime smime gnutls dig gnus-sum gnus-group gnus-undo gnus-start
gnus-dbus dbus gnus-cloud nnimap nnmail mail-source utf7 nnoo gnus-spec
gnus-int gnus-range gnus-win gnus nnheader range cal-menu calendar
cal-loaddefs mu4e-headers mu4e-compose mu4e-draft mu4e-actions smtpmail
mu4e-search mu4e-lists mu4e-bookmarks mu4e-mark mu4e-message shr
pixel-fill kinsoku url-file svg xml dom browse-url url url-proxy
url-privacy url-expand url-methods url-history url-cookie
generate-lisp-file url-domsuf url-util url-parse url-vars flow-fill
mule-util hl-line mu4e-contacts mu4e-update mu4e-folders mu4e-server
mu4e-context mu4e-obsolete mu4e-vars mu4e-helpers mu4e-config
mu4e-window bookmark pp ido message sendmail mailcap yank-media puny
dired dired-loaddefs rfc822 mml mml-sec epa derived epg rfc6068
epg-config gnus-util text-property-search mm-decode mm-bodies mm-encode
mail-parse rfc2231 rfc2047 rfc2045 mm-util ietf-drums mail-prsvr
mailabbrev mail-utils gmm-utils mailheader page-break-lines rainbow-mode
which-key openwith winner recentf tree-widget wid-edit browse-kill-ring
delsel windmove auto-compile comp comp-cstr cl-extra warnings packed
corfu-indexed corfu-info corfu-history cape-keyword cape corfu-echo
corfu-doc-terminal avl-tree generator corfu-doc corfu-terminal popon
corfu marginalia vertico-mouse vertico-buffer vertico-indexed vertico
xclip comp-run comp-common term/xterm xterm indent-bars treesit compat
noutline outline icons face-remap highlight-parentheses
sanityinc-tomorrow-eighties-theme color-theme-sanityinc-tomorrow color
my-key-bindings my-packages gnuplot-autoloads ediff ediff-merg
ediff-mult ediff-wind ediff-diff ediff-help ediff-init ediff-util
saveplace tramp-sh tramp rx trampver tramp-integration files-x
tramp-message help-mode tramp-compat xdg shell pcomplete comint ansi-osc
ring parse-time iso8601 time-date format-spec auth-source eieio
eieio-core password-cache json subr-x map ansi-color tramp-loaddefs
edmacro kmacro byte-opt diminish my-vertico-corfu my-tex
auctex-autoloads tex-site my-defaults goto-addr thingatpt savehist
mb-depth xt-mouse disp-table my-functions my-xterm cl-seq cl-macs gv
use-package use-package-ensure use-package-delight use-package-diminish
use-package-bind-key bind-key easy-mmode use-package-core cl-loaddefs
cl-lib bytecomp byte-compile info rmc iso-transl tooltip cconv eldoc
paren electric uniquify ediff-hook vc-hooks lisp-float-type elisp-mode
mwheel term/x-win x-win term/common-win x-dnd touch-screen tool-bar dnd
fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow
isearch easymenu timer select scroll-bar mouse jit-lock font-lock syntax
font-core term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads dbusbind inotify lcms2
dynamic-setting system-font-setting font-render-setting cairo gtk
x-toolkit xinput2 x multi-tty move-toolbar make-network-process
native-compile emacs)

Memory information:
((conses 16 686735 101378) (symbols 48 31301 5)
 (strings 32 132848 5474) (string-bytes 1 3923210) (vectors 16 74276)
 (vector-slots 8 862021 26500) (floats 8 946 9980)
 (intervals 56 5825 1610) (buffers 984 19))

-- 
Olaf Rogalsky
Schwoerhausgasse 5
89073 Ulm
Germany