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?

Drew
  • 77,472
  • 10
  • 114
  • 243
Dodgie
  • 472
  • 2
  • 14

1 Answers1

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.

Stefan
  • 26,404
  • 3
  • 48
  • 85