       @checkAutocomplete()
 
     # @input.on "blur", (event) =>
-    #   console.log $(event.target)
-    #   @closeAutocomplete()
+    #   setTimeout( =>
+    #     @closeAutocomplete()
+    #   , 100)
 
   checkAutocomplete: ->
     # Open autocomplete menu if it's a word.


  registerEvents: ->
    @input.on "keyup paste input", (event) =>
      @checkAutocomplete()

      # Spans lose contenteditable attr when pasted. (!?)

    id = result.data('id')
    return false unless id

    # Create new token.
    name = result.html()
    token = '<span class="token" contenteditable="false" data-id="' + id + '">' + name + '</span>'
    
    # Re-place the caret (necessary if the user clicked on the autocomplete menu).
    sel = window.getSelection()
    sel.removeAllRanges()
    sel.addRange(@range)

    if (sel.getRangeAt && sel.rangeCount)
      # Set range to metric name fragment.
      @range.setStart(@range.startContainer, @range.endOffset - @word[0].length)
      @range.deleteContents()

      # Create and insert new token.
      node = document.createElement('span')
