masm11 / emacs

Mirror of GNU Emacs
http://www.gnu.org/software/emacs/
GNU General Public License v3.0
198 stars 14 forks source link

Cannot scroll with xterm-mouse-mode #108

Closed hmenke closed 2 years ago

hmenke commented 3 years ago

The easiest reproducer is to launch Emacs like this and then scroll using the mouse wheel

$ emacs -Q
$ emacs -nw -Q --eval '(xterm-mouse-mode t)'

It works fine in GUI Emacs but terminal Emacs will report

<mouse-5> is undefined
<mouse-4> is undefined

This is due to https://github.com/masm11/emacs/blob/3df4ca451d41a5f1036713277ef55ca9734c6fa7/lisp/mwheel.el#L54-L70 which unconditionally maps the scroll events to 'wheel-up and 'wheel-down when pgtk is active.

A quick fix would be something like

$ emacs -nw -Q --load test.el
;; test.el
(unless window-system
  (xterm-mouse-mode t)
  (when (featurep 'pgtk)
    (setq mouse-wheel-down-event 'mouse-4)
    (setq mouse-wheel-up-event 'mouse-5)
    (global-set-key (kbd "<mouse-4>") 'mwheel-scroll)
    (global-set-key (kbd "<mouse-5>") 'mwheel-scroll)))

Unfortuantely, it is not as simple because the user could have Emacs running as a daemon, connecting with both GUI and terminal clients in which case one of them won't work. In fact, using the above command to start a daemon and then connecting with a GUI client will cause Bad binding in mwheel-scroll upon scrolling.

I tried fixing this by changing mouse-wheel-(down|up)-event in focus-in-hook depending on the (window-system (selected-frame)) but this crashes Emacs with segmentation fault upon switching between frames.

;; test.el
(when (featurep 'pgtk)
  (add-hook 'focus-in-hook
            (lambda ()
              (if (window-system (selected-frame))
                  (setq mouse-wheel-down-event 'mouse-4
            mouse-wheel-up-event 'mouse-5)
                (setq mouse-wheel-down-event 'wheel-up
                      mouse-wheel-up-event 'wheel-down)))))
(unless window-system
  (xterm-mouse-mode t)
  (when (featurep 'pgtk)
    (setq mouse-wheel-down-event 'mouse-4)
    (setq mouse-wheel-up-event 'mouse-5)
    (global-set-key (kbd "<mouse-4>") 'mwheel-scroll)
    (global-set-key (kbd "<mouse-5>") 'mwheel-scroll)
    (global-set-key (kbd "<C-mouse-4>") 'mouse-wheel-text-scale)
    (global-set-key (kbd "<C-mouse-5>") 'mouse-wheel-text-scale)
    (global-set-key (kbd "<S-mouse-4>") 'mwheel-scroll)
    (global-set-key (kbd "<S-mouse-5>") 'mwheel-scroll)))

Related thread on Emacs Stack Exchange: https://emacs.stackexchange.com/questions/64935/mwheel-scroll-bindings-between-gui-and-terminal


I'm running this version of Emacs: https://git.savannah.gnu.org/cgit/emacs.git/commit/?h=feature/pgtk&id=13a9a5e836cbe6e64aadaba40fe1f7eb83320d08

hmenke commented 2 years ago

Also reported properly on the Emacs bugtracker at https://debbugs.gnu.org/cgi/bugreport.cgi?bug=50321