   # Listen for edits in CodeMirror.
   editorListener = (ed, change) ->
     return if suppress
-    applyCMToShareJS editor, change, sharedoc
-
+    applyCMToShareJS editorDoc, change, sharedoc
     check()
 
-  editor.on 'change', editorListener
+  editorDoc.on 'change', editorListener
 
   @on 'insert', (pos, text) ->
     suppress = true


  @detach_cm = ->
    # TODO: can we remove the insert and delete event callbacks?
    editor.off 'onChange', editorListener
    delete @detach_cm

  return

    throw new Error 'Only text documents can be attached to CodeMirror2'

  sharedoc = @
  editorDoc = editor.getDoc()

  check = ->
    window.setTimeout ->
        editorText = editor.getValue()
