ex-mode/lib/ex-state.coffee
jazzpi 42a44ee9e1 Add specs; minor changes to some commands
`:tabedit` now works as an alias to `:edit` with a path and
  as an alias to `:tabnew` without.
`:tabnew` is a new command that opens a new tab with a new file if used
 without a path and works as an alias to `:tabedit` with one.
`:tabclose` now works as a proper alias to `:quit` (i.e. passes the arguments)
`:edit` now works more like before - it opens a given path in a new tab.
 It also doesn't do anything if the file was modified since the last commit,
 unless forced by using `:edit!`
`:write` works properly again and doesn't overwrite files, unless forced by
 using `:write!`
`:xit` is now called `:xit` and not just `:x`
`:substitute` now properly replaces multiple groups (`:s/(a)b(c)/X\1\2X\0`)
2015-07-29 19:13:13 +02:00

63 lines
1.6 KiB
CoffeeScript

{Emitter, Disposable, CompositeDisposable} = require 'event-kit'
Command = require './command'
CommandError = require './command-error'
class ExState
constructor: (@editorElement, @globalExState) ->
@emitter = new Emitter
@subscriptions = new CompositeDisposable
@editor = @editorElement.getModel()
@opStack = []
@history = []
@registerOperationCommands
open: (e) => new Command(@editor, @)
destroy: ->
@subscriptions.dispose()
getExHistoryItem: (index) ->
@globalExState.commandHistory[index]
pushExHistory: (command) ->
@globalExState.commandHistory.unshift command
registerOperationCommands: (commands) ->
for commandName, fn of commands
do (fn) =>
pushFn = (e) => @pushOperations(fn(e))
@subscriptions.add(
atom.commands.add(@editorElement, "ex-mode:#{commandName}", pushFn)
)
onDidFailToExecute: (fn) ->
@emitter.on('failed-to-execute', fn)
onDidProcessOpStack: (fn) ->
@emitter.on('processed-op-stack', fn)
pushOperations: (operations) ->
@opStack.push operations
@processOpStack() if @opStack.length == 2
clearOpStack: ->
@opStack = []
processOpStack: ->
[command, input] = @opStack
if input.characters.length > 0
@history.unshift command
try
command.execute(input)
catch e
if (e instanceof CommandError)
atom.notifications.addError("Command error: #{e.message}")
@emitter.emit('failed-to-execute')
else
throw e
@clearOpStack()
@emitter.emit('processed-op-stack')
module.exports = ExState