         @buffer.setText txt
 
   init: ($atomEmulator) ->
-    $textArea = $atomEmulator.find 'textarea'
-    atom.workspace.editor.buffer.$textArea = $textArea
-    $textArea.attr 'spellcheck', false
-    $textArea.keydown @keyHandler.bind this
+    $editableDiv = $atomEmulator.find '.edit-buffer'
+    atom.workspace.editor.buffer = new Buffer($editableDiv)
+    $editableDiv.attr 'spellcheck', false
+    $editableDiv.keydown @keyHandler.bind this
+    $editableDiv.focus()
+    # document.execCommand 'insertText', false, '[ 111\n, 222\n, 333\n]'
     setTimeout ( ->
       atom.notifications.addSuccess 'Atom emulator initialized'
     ), 300 # Small delay to see it appear after page has loaded

    console.error 'fromObj parameter is Point nor Array: '
    console.dir obj

class Buffer
  $textArea = null
  constructor: ($textArea) ->
    @$textArea = $textArea

  setText: (txt) ->
    @$textArea.val(txt)

  getText: ->
    @$textArea.val();

  getSelectionRange: ->
      new Range( @positionForCharacterIndex(@$textArea.get(0).selectionStart)
               , @positionForCharacterIndex(@$textArea.get(0).selectionEnd)
               )

  setSelectionRange: (range) ->
    range = Range.fromObj(range)
    @$textArea.get(0).selectionStart = @characterIndexForPosition range.start
    @$textArea.get(0).selectionEnd   = @characterIndexForPosition range.end

  delete: (range) ->
    range = Range.fromObj range
    rangeStartIx = @characterIndexForPosition range.start
    rangeEndIx   = @characterIndexForPosition range.end
    rangeLength = rangeEndIx - rangeStartIx
    selStartIx = @$textArea.get(0).selectionStart
    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

  setTextInRange: (range, text) ->
    range = Range.fromObj range
    rangeStartIx = @characterIndexForPosition range.start
    rangeEndIx   = @characterIndexForPosition range.end
    bufferText = @$textArea.val()
    @setText (bufferText.slice 0, rangeStartIx) + text + (bufferText.slice rangeEndIx, bufferText.length)

  positionForCharacterIndex: (ix) ->
    bufferText = @$textArea.val()
    row = 0
    col = 0
    i = 0

    if event.altKey and !event.shiftKey and event.metaKey and !event.ctrlKey or event.ctrlKey and !event.metaKey
      switch event.keyCode
        when 83
          # console.log '[' + window.getSelection().anchorOffset + ',' + window.getSelection().focusOffset + ']'
          # # document.execCommand 'insertText', false, 'XX'
          # $editableDiv = jQuery('.text-buffer')
          # console.log 'editableDiv: ' + $editableDiv.text();
          # el = $editableDiv[0]
          # range = document.createRange()
          # sel = window.getSelection()
          # range.setStart(el.childNodes[0], 5);
          # range.setEnd(el.childNodes[0], 107);
          # sel.removeAllRanges();
          # sel.addRange(range);

          # console.log '[' + window.getSelection().anchorOffset + ',' + window.getSelection().focusOffset + ']'
          #document.execCommand 'insertText', false, '\n'
          #console.log JSON.stringify atom.workspace.editor.buffer.getSelectionRange()
          atom.commands.dispatch(null, 'list-edit:select')
        when 88
          atom.commands.dispatch(null, 'list-edit:cut')
