   lastQuery = lastIC = overlay = annotation = null
   clearSearch = -> cm.removeOverlay overlay; overlay = null; annotation?.clear(); annotation = null; return
   highlightSearch = ->
-    window.cm = cm
     ic = !$('.tb-case:visible', $tb).hasClass 'pressed' # ic: ignore case (like in vim)
     q = $('.tb-search:visible', $tb).val(); if ic then q = q.toLowerCase() # q: the query string
     if lastQuery != q || lastIC != ic

    .on 'click', '.tb-hid, .tb-case', -> $(@).toggleClass 'pressed'; highlightSearch(); false
    .on 'keydown', '.tb-search', 'return',       -> cm.execCommand 'NX'; false
    .on 'keydown', '.tb-search', 'shift+return', -> cm.execCommand 'PV'; false
    .on 'keydown', '.tb-search', 'esc', -> clearSearch(); cm.focus(); false
    .on 'click', '.tb-refac-m', ->
      if !/^\s*$/.test s = cm.getLine l = cm.getCursor().line

    [q, ic]
  search = (backwards) ->
    [q, ic] = highlightSearch()
    if q
      s = cm.getValue(); if ic then s = s.toLowerCase()
      if backwards
        i = cm.indexFromPos cm.getCursor 'anchor'
