         @element     = @editor.element
 
         # window.w = @
+
         # replace undo/redo by sync-aware versions
         for cm in [@codemirror, @codemirror1]
             cm.undo = @undo

                    redux.getProjectActions(@editor.project_id).close_tab(@filename)

                save_state = () => @_sync()
                # We debounce instead of throttle, because we want a single "diff/commit" to correspond
                # a burst of activity, not a bunch of little pieces of that burst.  This is more
                # consistent with how undo stacks work.
                @save_state_debounce = underscore.debounce(save_state, @opts.sync_interval)

                @codemirror.on 'change', (instance, changeObj) =>
                    if @_closed
                        return
                    #console.log("change event when live='#{@live().string()}'")
                    if changeObj.origin?
                        if changeObj.origin == 'undo'
                            @on_undo?(instance, changeObj)

                            @on_redo?(instance, changeObj)
                        if changeObj.origin != 'setValue'
                            @_last_change_time = new Date()
                            @save_state_throttle?()
                            @_syncstring.exit_undo_mode()
                    update_unsaved_uncommitted_changes()

                @emit('connect')   # successful connection
                cb?()  # done initializing document (this is used, e.g., in the SynchronizedWorksheet derived class).

    # Set value of the syncstring to equal current value of the codemirror editor
    _set_syncstring_to_codemirror: =>
        if not @_user_action
            # console.log 'not setting due to no user action'
            # user has not explicitly done anything, so there should be no changes.
            return
        # console.log 'user action so setting'
        @_user_action = false
        @_last_val = val = @codemirror.getValue()
        @_syncstring.from_str(val)

    # Set value of the codemirror editor to equal current value of the syncstring
    _set_codemirror_to_syncstring: =>
        @_setting_from_syncstring = true
        @codemirror.setValueNoJump(@_syncstring.to_str())
        @_setting_from_syncstring = false

    has_unsaved_changes: =>
        if not @codemirror?
