Monday, February 23, 2009

Smooth scrolling - Emacs hackery

Emacs mouse wheel scrolling can be abrupt or "jumpy" and cause one to lose their tracking. This is a hack that rebinds the mouse wheels to some functions that scroll one line at a time, pausing for a slight delay between each scroll. The times are tweakable to achieve different effects (like a scroll that slows down as it nears the end of its duration):

(defun smooth-scroll (increment)
(scroll-up increment) (sit-for 0.05)
(scroll-up increment) (sit-for 0.02)
(scroll-up increment) (sit-for 0.02)
(scroll-up increment) (sit-for 0.05)
(scroll-up increment) (sit-for 0.06)
(scroll-up increment))

(global-set-key [(mouse-5)] '(lambda () (interactive) (smooth-scroll 1)))
(global-set-key [(mouse-4)] '(lambda () (interactive) (smooth-scroll -1)))



A more generic function is as follows, though it cannot pause for variable lengths of time. You could use this if you want to more easily change the number of lines scrolled:

(defun smooth-scroll (number-lines increment)
(if (= 0 number-lines)
t
(progn
(sit-for 0.02)
(scroll-up increment)
(smooth-scroll (- number-lines 1) increment))))

(global-set-key [(mouse-5)] '(lambda () (interactive) (smooth-scroll 6 1)))
(global-set-key [(mouse-4)] '(lambda () (interactive) (smooth-scroll 6 -1)))



I have only tested these on GNU Emacs (version 23.0.60). If they do not work on XEmacs, I would appreciate any tips you could send me. Write me at "dzwell at [this domain]".

No comments: