     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)
          )
        )

  setSelection: (range) ->
    @selection.setRange(range)



window.Scribe ||= {}
