1

The following code

(define-key (copy-tree global-map) (kbd "C-r") 'forward-char)

Immediately remaps C-r to forward-char. My understanding is that copy-tree will return a deepcopy of global-map. So why does editing the resulting keymap with define-key change my current global map?

3
  • copy-tree isn't a deepcopy. Try copy-keymap.
    – npostavs
    Jul 13, 2016 at 21:52
  • And why are you even copying the keymap? Just what is it that you are really trying to do?
    – Drew
    Jul 13, 2016 at 22:47
  • an alternate global map, so I can build/design a different keyboard scheme without committing to it. Bind it to a minor mode, I suppose
    – Dodgie
    Jul 13, 2016 at 22:52

1 Answer 1

3

copy-tree only makes copies of the cons cells, not the arrays. global-map is a "dense" map, which implies it has an array (well, a char-table) inside where it stores the mapping for "simple char" bindings. And (kbd "C-r") corresponds to a simple char binding, so it gets stored in the array (which was not copied by copy-tree).

This said, you should basically never copy a keymap.

Instead just make your new keymap inherit from global-map, e.g. with (defvar my-new-map (make-composed-keymap nil global-map)). This map will start out identical to global-map but you can then modify it all you want without affecting global-map itself.

1
  • Oh, that's so much better. Thank you.
    – Dodgie
    Jul 14, 2016 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.