I have some custom scrolling functions in Emacs, which help me get around a bug where a single scroll event sends two <mouse-4>
or <mouse-5>
actions. I have:
(setq scroll-down-this-time t)
(defun my-scroll-down-line ()
(interactive "@")
(if scroll-down-this-time
(progn
(scroll-down-line)
(setq scroll-down-this-time nil))
(setq scroll-down-this-time t)))
(setq scroll-up-this-time t)
(defun my-scroll-up-line ()
(interactive "@")
(if scroll-up-this-time
(progn
(scroll-up-line)
(setq scroll-up-this-time nil))
(setq scroll-up-this-time t)))
(global-set-key (kbd "<mouse-4>") 'my-scroll-down-line)
(global-set-key (kbd "<mouse-5>") 'my-scroll-up-line)
This works perfectly, except that the (interactive "@")
isn't exactly what I want. This causes whatever buffer that is under the mouse to scroll and to gain the keyboard focus. I want a way to make it scroll, but not steal the keyboard focus (like (setq mouse-wheel-follow-mouse 't)
does for the normal scrolling library). How might I achieve this?
I am using the development version of Emacs, so don't be afraid to give me any new features.
You should not redefine <mouse-4>
and <mouse-5>
, but instead:
(mouse-wheel-mode 1)
(defvar alternating-scroll-down-next t)
(defvar alternating-scroll-up-next t)
(defun alternating-scroll-down-line (&optional arg)
(when alternating-scroll-down-next
(scroll-down-line (or arg 1)))
(setq alternating-scroll-down-next (not alternating-scroll-down-next)))
(defun alternating-scroll-up-line (&optional arg)
(when alternating-scroll-up-next
(scroll-up-line (or arg 1)))
(setq alternating-scroll-up-next (not alternating-scroll-up-next)))
(setq mwheel-scroll-up-function 'alternating-scroll-up-line)
(setq mwheel-scroll-down-function 'alternating-scroll-down-line)
来源:https://stackoverflow.com/questions/11532149/emacs-make-custom-scrolling-function-follow-mouse-but-not-change-keyboard-focus