       switch which
         when 'open' then ['backwardsScanInBufferRange', rangeToBeginningOfFileFromPoint(from)]
         when 'close' then ['scanInBufferRange', rangeToEndOfFileFromPoint(from)]
+    # console.log  "---#{which}-------"
+    # console.log @editor.getTextInBufferRange(scanRange)
+    # console.log  '----------'
     pairRegexp = pair.split('').map(_.escapeRegExp).join('|')
     pattern = ///#{pairRegexp}///g
 
     found = null # We will search to fill this var.
     @editor[scanFunc] pattern, scanRange, (arg) =>
-      {matchText, range: {start, end}, stop} = arg
+      {matchText, range, stop} = arg
+      {start, end} = range
+      # console.log "#{which}:", matchText
       return if @isEscapedCharAtPoint(start)
       return stop() if (not allowNextLine) and (from.row isnt start.row)
 
+      if which is 'close'
+        end = end.translate([0, -1])
       if (charOpen is charClose) and (from.row isnt start.row)
         switch which
           when 'open' then return stop()
           when 'close' then nest = 0
-      if @getPairState(pair, arg) is which
+      if @getPairState(pair, matchText, start) is which
         nest = Math.max(nest-1, 0)
       else
         nest++
       if nest is 0
         found = end
-        found = found.translate([0, -1]) if which is 'close'
         stop()
     found
 

        open  = @findPair pair, {from,       @allowNextLine, nest: 1, which: 'open'}
        close = @findPair pair, {from: open, @allowNextLine, nest: 1, which: 'close'} if open?
      when 'next'
        close = @findPair pair, {from,        @allowNextLine, nest: 0, which: 'close'}
        open  = @findPair pair, {from: close, @allowNextLine, nest: 1, which: 'open'} if close?
      when 'previous' # FIXME but currently unused
        open  = @findPair pair, {from,       @allowNextLine, nest: 0, which: 'open'}
        close = @findPair pair, {from: open, @allowNextLine, nest: 1, which: 'close'} if open?

          from = range.end.translate([0, +1])
      # Since range is same area, retry to expand outer pair.
      range = @getPairRange(from, @pair, what)
    # selection.selectLeft() if (not range) and wasEmpty
    range

  select: ->
