   setSelectionRange: (range) ->
     range = Range.fromObj(range)
     @$textArea.get(0).selectionStart = @characterIndexForPosition range.start
-    @$textArea.get(0).selectionEnd = @characterIndexForPosition range.end
+    @$textArea.get(0).selectionEnd   = @characterIndexForPosition range.end
 
   delete: (range) ->
     range = Range.fromObj range
     rangeStartIx = @characterIndexForPosition range.start
-    rangeEndIx = @characterIndexForPosition range.end
+    rangeEndIx   = @characterIndexForPosition range.end
     rangeLength = rangeEndIx - rangeStartIx
     selStartIx = @$textArea.get(0).selectionStart
-    selEndIx = @$textArea.get(0).selectionEnd
-    # TODO: fix layout
-    selStartIx = if selStartIx <= rangeStartIx then selStartIx else
-                                                    if selStartIx < rangeEndIx then rangeStartIx else
-                                                                                    selStartIx - rangeLength
-    selEndIx = if selEndIx <= rangeStartIx then selEndIx else if selEndIx < rangeEndIx then rangeStartIx else selEndIx - rangeLength
+    selEndIx   = @$textArea.get(0).selectionEnd
+
+    selStartIx = switch
+                   when selStartIx <= rangeStartIx then selStartIx
+                   when selStartIx < rangeEndIx    then rangeStartIx
+                   else selStartIx - rangeLength
+    selEndIx   = switch
+                   when selEndIx <= rangeStartIx then selEndIx
+                   when selEndIx < rangeEndIx    then rangeStartIx
+                   else selEndIx - rangeLength
     @setTextInRange range, ''
     @$textArea.get(0).selectionStart = selStartIx
-    @$textArea.get(0).selectionEnd = selEndIx
+    @$textArea.get(0).selectionEnd   = selEndIx
 
   setTextInRange: (range, text) ->
     range = Range.fromObj range
     rangeStartIx = @characterIndexForPosition range.start
-    rangeEndIx = @characterIndexForPosition range.end
+    rangeEndIx   = @characterIndexForPosition range.end
     bufferText = @$textArea.val()
     @setText (bufferText.slice 0, rangeStartIx) + text + (bufferText.slice rangeEndIx, bufferText.length)
 

      $notification.fadeIn 50
      if @hideTimer
        clearTimeout @hideTimer
      @hideTimer = setTimeout((->
        $notification.fadeOut 100
        return
      ), duration)
      return
    addInfo: (msg) ->
      @addNotification msg, '9ed2ff', 3000
      return
    addSuccess: (msg) ->
      @addNotification msg, '99dba6', 3000
      return
    addWarning: (msg) ->
      @addNotification msg, 'e8d0a1', 3000
      return
    addError: (msg) ->
      @addNotification msg, 'ecb1b1', 4000
      return
  clipboard:
    clipboard:
      text: null
      meta: null
    readWithMetadata: ->
      @clipboard
    write: (text, metadata) ->
      @clipboard =
        text: text
        metadata: if metadata then metadata else null
      return
  workspace:
    getActiveTextEditor: ->
      @editor
    editor:
      getGrammar: ->
        @grammar
      grammar:
        tokenizeLine: ->
          {
            tags: []
            ruleStack: []
          }
        registry: idsByScope: {}
      getBuffer: ->
        @buffer
      setCursorBufferPosition: (pos) ->
        @setSelectedBufferRange [
          pos
          pos
        ]
        return
      getSelectedBufferRange: ->
        @buffer.getSelectionRange()
      setSelectedBufferRange: (range) ->
        @buffer.setSelectionRange range
        return
      getText: ->
        @buffer.getText()
      setText: (txt) ->
        @buffer.setText txt
        return
      buffer: new Buffer
  init: ($atomEmulator) ->
    $textArea = $atomEmulator.find('textarea')
    atom.workspace.editor.buffer.$textArea = $textArea
    $textArea.attr 'spellcheck', false
    $textArea.keydown @keyHandler.bind(this)
    setTimeout (->
      atom.notifications.addSuccess 'Atom emulator initialized'
    ), 300
    # Small delay to see it appear after page has loaded
    return
  keyHandler: (event) ->
    # console.log(event.charCode);
    # console.log(event.keyCode);

          @listPaste()
        else
          return true
      return false # disable event propagation after execution of list-edit command

  listSelect: ->
    console.log 'list-select'
    ListEdit.selectCmd()

  listCut: ->
    console.log 'list-cut'
    ListEdit.cutCmd()

  listCopy: ->
    console.log 'list-copy'
    ListEdit.copyCmd()

  listPaste: ->
    console.log 'list-paste'
    ListEdit.pasteCmd()
