Emacs:自定义滚动功能跟随鼠标但不改变键盘焦点

时间:2012-07-17 23:13:25

标签: emacs elisp

我在Emacs中有一些自定义滚动功能,可帮助我解决单个滚动事件发送两个<mouse-4><mouse-5>动作的错误。我有:

(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)

这完美无缺,但(interactive "@")并不完全符合我的要求。这会导致鼠标下的任何缓冲区滚动并获得键盘焦点。我想要一种方法让它滚动,但不要窃取键盘焦点(就像(setq mouse-wheel-follow-mouse 't)对普通滚动库一样)。我怎么能实现这个目标?

我正在使用Emacs的开发版本,所以不要害怕给我任何新功能。

1 个答案:

答案 0 :(得分:1)

您不应重新定义<mouse-4><mouse-5>,而应重新定义:

(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)