HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Iterating over global keymap, removing all bindings to super keys

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
globaliteratingremovingallbindingssuperkeyskeymapover

Problem

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)


Lines added to init.el.

;; 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

  • Is this reasonably idiomatic lisp?



  • Are there elisp constructs that would simplify the implementation?



  • Are there elisp constructs that would make the implementation clearer?



  • And finally, is (while (setq (car ))) 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.

Solution

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

Code Snippets

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

Context

StackExchange Code Review Q#115977, answer score: 2

Revisions (0)

No revisions yet.