- (let* ((dn mouse-wheel-down-event)
- (up mouse-wheel-up-event)
- (keys
- (nconc (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,up)])
- mouse-wheel-scroll-amount)
- (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,dn)])
- mouse-wheel-scroll-amount))))
- ;; This condition-case is here because Emacs 19 will throw an error
- ;; if you try to define a key that it does not know about. I for one
- ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
- ;; that if the wheeled-mouse is there, it just works, and this way it
- ;; doesn't yell at me if I'm on my laptop or another machine, etc.
- (condition-case ()
- (dolist (key keys)
- (cond (mouse-wheel-mode
- (global-set-key key 'mwheel-scroll))
- ((eq (lookup-key (current-global-map) key) 'mwheel-scroll)
- (global-unset-key key))))
- (error nil))))
+ ;; Remove previous bindings, if any.
+ (while mwheel-installed-bindings
+ (let ((key (pop mwheel-installed-bindings)))
+ (when (eq (lookup-key (current-global-map) key) 'mwheel-scroll)
+ (global-unset-key key))))
+ ;; Setup bindings as needed.
+ (when mouse-wheel-mode
+ (dolist (event (list mouse-wheel-down-event mouse-wheel-up-event))
+ (dolist (key (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,event)])
+ mouse-wheel-scroll-amount))
+ (global-set-key key 'mwheel-scroll)
+ (push key mwheel-installed-bindings)))))