Don't insert characters in vim command mode

GlobalKeymap.bindKey can take a selector and a custom function that
maps key events to commands. If it returns false, no command is
triggered but event propagation is halted.
This commit is contained in:
Nathan Sobo
2012-01-12 17:51:23 -08:00
parent ee068a4b84
commit a2cf77892d
6 changed files with 296 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
_ = require 'underscore'
$ = require 'jquery'
Specificity = require 'specificity'
@@ -11,13 +12,19 @@ class BindingSet
'=': 187, ';': 186, '\'': 222, '[': 219, ']': 221, '\\': 220
selector: null
bindings: null
bindingMap: null
bindingFunction: null
constructor: (@selector, @bindings) ->
constructor: (@selector, mapOrFunction) ->
if _.isFunction(mapOrFunction)
@bindingFunction = mapOrFunction
else
@bindingMap = mapOrFunction
@specificity = Specificity(@selector)
commandForEvent: (event) ->
for pattern, command of @bindings
return @bindingFunction(event) if @bindingFunction
for pattern, command of @bindingMap
return command if @eventMatchesPattern(event, pattern)
null

View File

@@ -18,9 +18,12 @@ class GlobalKeymap
candidateBindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
candidateBindingSets.sort (a, b) -> b.specificity - a.specificity
for bindingSet in candidateBindingSets
if command = bindingSet.commandForEvent(event)
command = bindingSet.commandForEvent(event)
if command
@triggerCommandEvent(event, command)
return false
else if command == false
return false
currentNode = currentNode.parent()
true

View File

@@ -9,6 +9,7 @@ class VimMode
constructor: (@editor) ->
@opStack = []
atom.bindKeys '.command-mode', > false
atom.bindKeys '.command-mode', @commandModeBindings()
atom.bindKeys '.insert-mode', '<esc>': 'command-mode:activate'