Emacs: Make custom scrolling function follow mouse but not change keyboard focus

会有一股神秘感。 提交于 2019-12-08 02:37:45

问题


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.


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!