     reader.readAsText file
 
 window.init_editor = ->
+  init_codemirror()
   $document = $ '#document'
   $file_picker = $ '#file'
   $file_picker.on 'change', (e) ->

    tabSize: 2
    viewportMargin: Infinity
    gutters: ['error']
    extraKeys:
      'Shift-Enter': 'run'
      'F1': 'contextHelp'
      'Ctrl-Space': 'suggest'

  context =
    used: false

    else
      CodeMirror.Pass

  next_cell: (cm) ->
    cell = input_cell_at_offset cm.lead_cell, 1
    if cell?
      cell.editor.focus()
    else
      CodeMirror.Pass

  previous_cell: (cm) ->
    cell = input_cell_at_offset cm.lead_cell, -1
    if cell?
      cell.editor.focus()
    else
      CodeMirror.Pass

  maybe_next_cell: (cm) ->
    cur = cm.getCursor()
    if cur.line is cm.lineCount() - 1
      lead_editor_commands.next_cell cm
    else
      CodeMirror.Pass

  maybe_previous_cell: (cm) ->
    cur = cm.getCursor()
    if cur.line is 0
      lead_editor_commands.previous_cell cm
    else
      CodeMirror.Pass

lead_key_map =
  Tab: (cm) ->
    if cm.somethingSelected()
      cm.indentSelection 'add'
    else
      spaces = Array(cm.getOption("indentUnit") + 1).join(" ")
      cm.replaceSelection(spaces, "end", "+input")
  Up: 'maybe_previous_cell'
  Down: 'maybe_next_cell'
  'Shift-Up': 'fill_with_last_value'
  'Shift-Enter': 'run'
  'F1': 'contextHelp'
  'Ctrl-Space': 'suggest'

  fallthrough: ['default']

init_codemirror = ->
  CodeMirror.keyMap.lead = lead_key_map
  $.extend CodeMirror.commands, lead_editor_commands

contexts = []

