-" Find backwards the closest open parenthesis/bracket/brace.
-function! s:SearchParensPair()
- let line = line('.')
- let col = col('.')
-
- " Skip strings and comments and don't look too far
- let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" .
- \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' .
- \ '"string\\|comment"'
-
- " Search for parentheses
- call cursor(line, col)
- let parlnum = searchpair('(', '', ')', 'bW', skip)
- let parcol = col('.')
-
- " Search for brackets
- call cursor(line, col)
- let par2lnum = searchpair('\[', '', '\]', 'bW', skip)
- let par2col = col('.')
-
- " Search for braces
- call cursor(line, col)
- let par3lnum = searchpair('{', '', '}', 'bW', skip)
- let par3col = col('.')
-
- " Get the closest match
- if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol)
- let parlnum = par2lnum
- let parcol = par2col
- endif
- if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol)
- let parlnum = par3lnum
- let parcol = par3col
+" Skip strings and comments. Return 1 for chars to skip.
+" jedi* refers to syntax definitions from jedi-vim for call signatures, which
+" are inserted temporarily into the buffer.
+let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
+ \ '=~? "\\vstring|comment|jedi\\S"'
+
+let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
+ \ '=~? "\\vcomment|jedi\\S"'
+
+" Also ignore anything concealed.
+" Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
+function! s:is_concealed(line, col)
+ let concealed = synconcealed(a:line, a:col)
+ return len(concealed) && concealed[0]
+endfunction
+if has('conceal')
+ let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))'
+endif
+
+
+let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
+ \ '=~? "comment"'
+
+" Use 'shiftwidth()' instead of '&sw'.
+" (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop').
+if exists('*shiftwidth')
+ function! s:sw()
+ return shiftwidth()
+ endfunction
+else
+ function! s:sw()
+ return &shiftwidth
+ endfunction
+endif
+
+function! s:pair_sort(x, y)
+ if a:x[0] == a:y[0]
+ return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
+ else
+ return a:x[0] > a:y[0] ? 1 : -1