Skip to content

Commit

Permalink
Allow keymaps to be referenced both by name and directly
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Feb 20, 2012
1 parent f5956d5 commit c5aa793
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/codemirror.js
Expand Up @@ -489,10 +489,10 @@ var CodeMirror = (function() {
}
function handleKeyBinding(e) {
// Handle auto keymap transitions
var startMap = keyMap[options.keyMap], next = startMap.auto;
var startMap = getKeyMap(options.keyMap), next = startMap.auto;
clearTimeout(maybeTransition);
if (next && !isModifierKey(e)) maybeTransition = setTimeout(function() {
if (keyMap[options.keyMap] == startMap) {
if (getKeyMap(options.keyMap) == startMap) {
options.keyMap = (next.call ? next.call(null, instance) : next);
}
}, 50);
Expand Down Expand Up @@ -1934,22 +1934,26 @@ var CodeMirror = (function() {
"Alt-D": "delWordRight", "Alt-Backspace": "delWordLeft", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars"
};

function getKeyMap(val) {
if (typeof val == "string") return keyMap[val];
else return val;
}
function lookupKey(name, extraMap, map, handle) {
function lookup(map, fallthrough) {
function lookup(map) {
map = getKeyMap(map);
var found = map[name];
if (found != null && handle(found)) return true;
if (fallthrough == null) fallthrough = map.fallthrough;
if (fallthrough == null) {
if (map.catchall) return handle(map.catchall);
return false;
}
if (typeof fallthrough == "string") return lookup(keyMap[fallthrough]);
if (map.catchall) return handle(map.catchall);
var fallthrough = map.fallthrough;
if (fallthrough == null) return false;
if (toString.call(fallthrough) != "[object Array]") return lookup(fallthrough);
for (var i = 0, e = fallthrough.length; i < e; ++i) {
if (lookup(keyMap[fallthrough[i]])) return true;
if (lookup(fallthrough[i])) return true;
}
return false;
}
return extraMap ? lookup(extraMap, map) : lookup(keyMap[map]);
if (extraMap && lookup(extraMap)) return true;
return lookup(map);
}
function isModifierKey(event) {
var name = keyNames[e_prop(event, "keyCode")];
Expand Down

0 comments on commit c5aa793

Please sign in to comment.