]> git.r.bdr.sh - rbdr/dotfiles/blame - atom/packages/ex-mode/lib/ex-state.coffee
Update vim plugins
[rbdr/dotfiles] / atom / packages / ex-mode / lib / ex-state.coffee
CommitLineData
24c7594d
BB
1{Emitter, Disposable, CompositeDisposable} = require 'event-kit'
2
3Command = require './command'
4CommandError = require './command-error'
5
6class ExState
7 constructor: (@editorElement, @globalExState) ->
8 @emitter = new Emitter
9 @subscriptions = new CompositeDisposable
10 @editor = @editorElement.getModel()
11 @opStack = []
12 @history = []
13
14 @registerOperationCommands
15 open: (e) => new Command(@editor, @)
16
17 destroy: ->
18 @subscriptions.dispose()
19
20 getExHistoryItem: (index) ->
21 @globalExState.commandHistory[index]
22
23 pushExHistory: (command) ->
24 @globalExState.commandHistory.unshift command
25
26 registerOperationCommands: (commands) ->
27 for commandName, fn of commands
28 do (fn) =>
29 pushFn = (e) => @pushOperations(fn(e))
30 @subscriptions.add(
31 atom.commands.add(@editorElement, "ex-mode:#{commandName}", pushFn)
32 )
33
34 onDidFailToExecute: (fn) ->
35 @emitter.on('failed-to-execute', fn)
36
455f099b
BB
37 onDidProcessOpStack: (fn) ->
38 @emitter.on('processed-op-stack', fn)
39
24c7594d
BB
40 pushOperations: (operations) ->
41 @opStack.push operations
42
43 @processOpStack() if @opStack.length == 2
44
45 clearOpStack: ->
46 @opStack = []
47
48 processOpStack: ->
49 [command, input] = @opStack
50 if input.characters.length > 0
51 @history.unshift command
52 try
53 command.execute(input)
54 catch e
55 if (e instanceof CommandError)
56 atom.notifications.addError("Command error: #{e.message}")
57 @emitter.emit('failed-to-execute')
58 else
59 throw e
60 @clearOpStack()
455f099b 61 @emitter.emit('processed-op-stack')
24c7594d
BB
62
63module.exports = ExState