Calaf
Calaf

Reputation: 10847

Keyboard scrolling with acceleration

One can easily map some key to scroll up.

(defun up1()
  (interactive)
  (scroll-up 1))
(defun up2()
  (interactive)
  (scroll-up 2))
(global-set-key "\M-]" 'up2)

I am looking instead for the following behavior. The first handful of scrolls would call up1() and the subsequent ones would call up2().

Upvotes: 1

Views: 241

Answers (1)

Tyler
Tyler

Reputation: 10032

How about this:

(setq my-scroll-counter 0)
(setq my-scroll-limit 5)

(defun up1()
  (interactive)
  (if (eq last-command this-command)
      (incf my-scroll-counter)
    (setq my-scroll-counter 0))
  (if (> my-scroll-counter my-scroll-limit) 
      (scroll-up 2)
    (scroll-up 1)))

(global-set-key "\M-]" 'up1)

If you want something a little fancier, you calculate your scroll step dynamically based on how many times you repeat the command:

(setq my-scroll-counter 0)
(setq my-maximum-scroll 20)
(setq my-scroll-acceleration 4)

(defun up1()
  (interactive)
  (if (eq last-command this-command)
      (incf my-scroll-counter)
    (setq my-scroll-counter 0))
  (scroll-up (min 
               (+ 1 (/ my-scroll-counter my-scroll-acceleration)) 
               my-maximum-scroll)))

(global-set-key "\M-]" 'up1)

Upvotes: 1

Related Questions