   getLineTextToBufferPosition
 } = require './utils'
 
+getPatternForPair = (pair) ->
+  [open, close] = pair
+  if open is close
+    new RegExp("(#{_.escapeRegExp(open)})", 'g')
+  else
+    new RegExp("(#{_.escapeRegExp(open)})|(#{_.escapeRegExp(close)})", 'g')
+
+findPair = (editor, from, pair, direction, fn) ->
+  pattern = getPatternForPair(pair)
+  switch direction
+    when 'forward'
+      findPairForward(editor, from, pattern, fn)
+    when 'backward'
+      findPairBackward(editor, from, pattern, fn)
+
+findPairForward = (editor, from, pattern, fn) ->
+  scanRange = new Range(from, editor.buffer.getEndPosition())
+  editor.scanInBufferRange(pattern, scanRange, fn)
+
+findPairBackward = (editor, from, pattern, fn) ->
+  scanRange = new Range([0, 0], from)
+  editor.backwardsScanInBufferRange(pattern, scanRange, fn)
+
+# Take start point of matched range.
+backSlashPattern = _.escapeRegExp('\\')
+isEscapedCharAtPoint = (editor, point) ->
+  escaped = false
+  pattern = new RegExp("[^#{backSlashPattern}]#{backSlashPattern}")
+  scanRange = [[point.row, 0], point]
+  editor.backwardsScanInBufferRange pattern, scanRange, ({matchText, range, stop}) ->
+    if range.end.isEqual(point)
+      stop()
+      escaped = true
+  escaped
+
 # -------------------------
 class Pair extends TextObject
   @extend(false)
+
+  _newStyle: true # REMOVE after rewrite DONE
+
   allowNextLine: false
   adjustInnerRange: true
   pair: null
   wise: 'characterwise'
   supportCount: true
-
-  getPattern: ->
-    [open, close] = @pair
-    if open is close
-      new RegExp("(#{_.escapeRegExp(open)})", 'g')
-    else
-      new RegExp("(#{_.escapeRegExp(open)})|(#{_.escapeRegExp(close)})", 'g')
 
   # Return 'open' or 'close'
   getPairState: ({matchText, range, match}) ->


  getPairInfo: (from) ->
    pairInfo = null
    pattern = @getPattern()
    closeRange = @findClose(from, pattern)
    openRange = @findOpen(closeRange.end, pattern) if closeRange?

    unless (openRange? and closeRange?)
      return null

          when match[1] then 'open'
          when match[2] then 'close'

  pairStateInBufferRange: (range, char) ->
    text = getLineTextToBufferPosition(@editor, range.end)
    escapedChar = _.escapeRegExp(char)
