# Iterating over global keymap, removing all bindings to super keys

Background

Emacs on OS X by default binds several keys with the super modifier. I do not want these bindings as I switch between OS X and Windows frequently, and want a clean slate on both platforms to bind super keys for my own purposes.

Code

global-unset-all-super-key.el

(defun global-unset-all-super-key ()
"Will unset any single key in global keymap that has the super
modifier."
(let ((km (current-global-map)))
(while km
(let ((maybe-event (and (listp (car km))
(caar km))))
(if (and (eventp maybe-event) ; Also filters out maybe-event
; when nil because (car km) was not a list.
(memq 'super (event-modifiers maybe-event)))
(global-unset-key (vector maybe-event))))
(setq km (cdr km)))))

(provide 'global-unset-all-super-key)


;; Remove default super bindings on Mac systems.
;; Do this early, before any mappings are added.
(when (string-equal system-type "darwin")
(require 'global-unset-all-super-key)
(global-unset-all-super-key))


Questions

1. Is this reasonably idiomatic lisp?
2. Are there elisp constructs that would simplify the implementation?
3. Are there elisp constructs that would make the implementation clearer?
4. And finally, is (while <list> <body> (setq <list> (car <list>))) the best that can be done to loop over a list taking each element in turn and doing something for side effects? map doesn't seem like a reasonable alternative because I'm not trying to build up a list to return. Something like Clojure's doseq.
• 4. you want dolist – BRPocock Jan 13 '16 at 19:22

Apart from using dolist as mentioned by @brfennpocock it looks mostly fine.

system-type is a symbol though, so you can compare it with eq instead of string-equal (unless I missed a reason to use that here).

Also the single if can also be when for consistency.

(defun global-unset-all-super-key ()
"Will unset any single key in global keymap that has the super modifier."
(dolist (km (current-global-map))
(let ((maybe-event (and (listp (car km)) (caar km))))
(when (and (eventp maybe-event)
(memq 'super (event-modifiers maybe-event)))
(global-unset-key (vector maybe-event))))))


For init.el if you have more functions also consider the autoload facilities, but that's not necessary for just this bit of code (actually I'd probably put it all in init.el to be honest even though a separate file like this is somewhat cleaner).