"  matchit.vim: (global plugin) Extended "%" matching
"  autload script of matchit plugin, see ../plugin/matchit.vim
"  Last Change: Jun 10, 2021

" Neovim does not support scriptversion
if has("vimscript-4")
  scriptversion 4
endif

let s:last_mps = ""
let s:last_words = ":"
let s:patBR = ""

let s:save_cpo = &cpo
set cpo&vim

" Auto-complete mappings:  (not yet "ready for prime time")
" TODO Read :help write-plugin for the "right" way to let the user
" specify a key binding.
"   let g:match_auto = '<C-]>'
"   let g:match_autoCR = '<C-CR>'
" if exists("g:match_auto")
"   execute "inoremap " . g:match_auto . ' x<Esc>"=<SID>Autocomplete()<CR>Pls'
" endif
" if exists("g:match_autoCR")
"   execute "inoremap " . g:match_autoCR . ' <CR><C-R>=<SID>Autocomplete()<CR>'
" endif
" if exists("g:match_gthhoh")
"   execute "inoremap " . g:match_gthhoh . ' <C-O>:call <SID>Gthhoh()<CR>'
" endif " gthhoh = "Get the heck out of here!"

let s:notslash = '\\\@1<!\%(\\\\\)*'

function s:RestoreOptions()
  " In s:CleanUp(), :execute "set" restore_options .
  let restore_options = ""
  if get(b:, 'match_ignorecase', &ic) != &ic
    let restore_options ..= (&ic ? " " : " no") .. "ignorecase"
    let &ignorecase = b:match_ignorecase
  endif
  if &ve != ''
    let restore_options = " ve=" .. &ve .. restore_options
    set ve=
  endif
  return restore_options
endfunction

function matchit#Match_wrapper(word, forward, mode) range
  let restore_options = s:RestoreOptions()
  " In s:CleanUp(), we may need to check whether the cursor moved forward.
  let startpos = [line("."), col(".")]
  " if a count has been applied, use the default [count]% mode (see :h N%)
  if v:count
    exe "normal! " .. v:count .. "%"
    return s:CleanUp(restore_options, a:mode, startpos)
  end
  if a:mode =~# "v" && mode(1) =~# 'ni'
    exe "norm! gv"
  elseif a:mode == "o" && mode(1) !~# '[