 {qw} = require './util'
+
+# https://codemirror.net/doc/manual.html#modeapi
 
 letter = @letter = 'A-Z_a-zÀ-ÖØ-Ýß-öø-üþ∆⍙Ⓐ-Ⓩ'
 name0 = ///[#{letter}]///

        else 'apl-glb'
      else 'apl-err'

  electricInput: /(?::else|:end|:andif|:orif|:case|\})$/

  indent: (state, textAfter) ->
    state.indentStack[state.indentStack.length - 1] + config.indentUnit * !(
      if state.dfnDepth then /^\s*\}/.test textAfter
      else /^\s*:(?:end|else|andif|orif|case)/i.test textAfter
    )

idiomsRE = ///^(?:#{idioms.sort((x, y) -> y.length - x.length).map(escIdiom).join '|'})///i

CodeMirror.defineMIME 'text/apl', 'apl'
CodeMirror.defineMode 'apl', (config) ->
  startState: ->
    hdr: 1      # are we at a location where a tradfn header is possible?
    brs: ''     # a brs of brackets (as a string) consisting of '{', '[', and '('
    dfnDepth: 0 # how many surrounding {} have we got?
    kws: []     # a stack of keywords, e.g. ['if', 'for']
    inds: []    # a stack of the indents of lines containing { :if :for etc

  token: (stream, state) ->
    if state.hdr
      delete state.hdr; stream.match /[^⍝\n\r]*/; s = stream.current()
      if dfnHeader.test s
        stream.backUp s.length # re-tokenize without hdr
      else if /^\s*$/.test s
        delete state.vars
      else
