     return @selection.getRange()
 
   insertAt: (index, text) ->
-    this.doSilently( =>
-      @selection.preserve( =>
-        return this.trackDelta( =>
-          this.keepNormalized( =>
+    doSilently.call(this, =>
+      @selection.preserve( =>
+        return trackDelta.call(this, =>
+          keepNormalized.call(this, =>
             @doc.insertText(index, text)
           )
         )

    )

  deleteAt: (index, length) ->
    this.doSilently( =>
      @selection.preserve( =>
        return this.trackDelta( =>
          this.keepNormalized( =>
            @doc.deleteText(index, length)
          )
        )
      )
    )

  doSilently: (fn) ->
    oldIgnoreDomChange = @ignoreDomChanges
    @ignoreDomChanges = true
    fn()
    @ignoreDomChanges = oldIgnoreDomChange

  # formatAt: (Number index, Number length, String name, Mixed value) ->
  formatAt: (index, length, name, value) ->
    this.doSilently( =>
      @selection.preserve( =>
        return this.trackDelta( =>
          this.keepNormalized( =>
            @doc.formatText(index, length, name, value)
          )
        )

    @keyboard = new Scribe.Keyboard(this)
    @undoManager = new Scribe.UndoManager(this)
    @pasteManager = new Scribe.PasteManager(this)
    initListeners.call(this)
    @ignoreDomChanges = false
    ScribeEditor.editors.push(this)

  disable: ->
    doSilently.call(this, =>
      @root.setAttribute('contenteditable', false)
    )

  enable: ->
    if !@root.getAttribute('contenteditable')
      doSilently.call(this, =>
        @root.setAttribute('contenteditable', true)
      )

  applyDelta: (delta, external = true) ->
    # Make exception for systems that assume editors start with empty text
    if delta.startLength == 0 and @doc.length == 1 and @doc.trailingNewline
      return this.setDelta(delta)
    return if delta.isIdentity()
    doSilently.call(this, =>
      @selection.preserve( =>
        console.assert(delta.startLength == @doc.length, "Trying to apply delta to incorrect doc length", delta, @doc, @root)
        index = 0       # Stores where the last retain end was, so if we see another one, we know to delete
