1 {Emitter, Disposable, CompositeDisposable} = require 'event-kit'
3 Command = require './command'
4 CommandError = require './command-error'
7 constructor: (@editorElement, @globalExState) ->
9 @subscriptions = new CompositeDisposable
10 @editor = @editorElement.getModel()
14 @registerOperationCommands
15 open: (e) => new Command(@editor, @)
18 @subscriptions.dispose()
20 getExHistoryItem: (index) ->
21 @globalExState.commandHistory[index]
23 pushExHistory: (command) ->
24 @globalExState.commandHistory.unshift command
26 registerOperationCommands: (commands) ->
27 for commandName, fn of commands
29 pushFn = (e) => @pushOperations(fn(e))
31 atom.commands.add(@editorElement, "ex-mode:#{commandName}", pushFn)
34 onDidFailToExecute: (fn) ->
35 @emitter.on('failed-to-execute', fn)
37 onDidProcessOpStack: (fn) ->
38 @emitter.on('processed-op-stack', fn)
40 pushOperations: (operations) ->
41 @opStack.push operations
43 @processOpStack() if @opStack.length == 2
49 [command, input] = @opStack
50 if input.characters.length > 0
51 @history.unshift command
53 command.execute(input)
55 if (e instanceof CommandError)
56 atom.notifications.addError("Command error: #{e.message}")
57 @emitter.emit('failed-to-execute')
61 @emitter.emit('processed-op-stack')
63 module.exports = ExState