:help matchit-install
inside Vim.
-For Vim version 6.3. Last change: 2007 Aug 29
+For Vim version 6.3. Last change: 2017 May 14
VIM REFERENCE MANUAL by Benji Fisher
==============================================================================
2. Activation *matchit-activate*
-You can use this script as a plugin, by copying it to your plugin directory.
-See |add-global-plugin| for instructions. You can also add a line to your
-|vimrc| file, such as >
- :source $VIMRUNTIME/macros/matchit.vim
-or >
- :runtime macros/matchit.vim
-Either way, the script should start working the next time you start up Vim.
+To use the matchit plugin add this line to your |vimrc|: >
+ packadd! matchit
+
+The script should start working the next time you start Vim.
(Earlier versions of the script did nothing unless a |buffer-variable| named
|b:match_words| was defined. Even earlier versions contained autocommands
In LaTeX, since "%" is used as the comment character, you can >
:let b:match_skip = 'r:%'
< Unfortunately, this will skip anything after "\%", an escaped "%". To
- allow for this, and also "\\%" (an excaped backslash followed by the
+ allow for this, and also "\\%" (an escaped backslash followed by the
comment character) you can >
:let b:match_skip = 'r:\(^\|[^\\]\)\(\\\\\)*%'
<
The various |:vmap|s defined in the script (%, |g%|, |[%|, |]%|, |a%|) may
have undesired effects in Select mode |Select-mode-mapping|. At least, if you
want to replace the selection with any character in "ag%[]" there will be a
-pause of |'updatetime'| first.
+pause of |'updatetime'| first. E.g., "yV%" would normally work linewise, but
+the plugin mapping makes it characterwise.
It would be nice if "\0" were recognized as the entire pattern. That is, it
would be nice if "foo:\end\0" had the same effect as "\(foo\):\end\1". I may
" matchit.vim: (global plugin) Extended "%" matching
-" Last Change: Fri Jan 25 10:00 AM 2008 EST
+" Last Change: 2017 Sep 15
" Maintainer: Benji Fisher PhD <benji@member.AMS.org>
-" Version: 1.13.2, for Vim 6.3+
+" Version: 1.13.3, for Vim 6.3+
+" Fix from Fernando Torres included.
+" Improvement from Ken Takata included.
" URL: http://www.vim.org/script.php?script_id=39
" Documentation:
let loaded_matchit = 1
let s:last_mps = ""
let s:last_words = ":"
+let s:patBR = ""
let s:save_cpo = &cpo
set cpo&vim
function! s:Match_wrapper(word, forward, mode) range
" In s:CleanUp(), :execute "set" restore_options .
- let restore_options = (&ic ? " " : " no") . "ignorecase"
- if exists("b:match_ignorecase")
+ let restore_options = ""
+ if exists("b:match_ignorecase") && b:match_ignorecase != &ic
+ let restore_options .= (&ic ? " " : " no") . "ignorecase"
let &ignorecase = b:match_ignorecase
endif
- let restore_options = " ve=" . &ve . restore_options
- set ve=
+ if &ve != ''
+ let restore_options = " ve=" . &ve . restore_options
+ set ve=
+ endif
" If this function was called from Visual mode, make sure that the cursor
" is at the correct end of the Visual range:
if a:mode == "v"
execute "let match_words =" b:match_words
endif
" Thanks to Preben "Peppe" Guldberg and Bram Moolenaar for this suggestion!
- if (match_words != s:last_words) || (&mps != s:last_mps) ||
- \ exists("b:match_debug")
- let s:last_words = match_words
+ if (match_words != s:last_words) || (&mps != s:last_mps)
+ \ || exists("b:match_debug")
let s:last_mps = &mps
" The next several lines were here before
" BF started messing with this script.
" let default = substitute(escape(&mps, '[$^.*~\\/?]'), '[,:]\+',
" \ '\\|', 'g').'\|\/\*\|\*\/\|#if\>\|#ifdef\>\|#else\>\|#elif\>\|#endif\>'
let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") .
- \ '\/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>'
+ \ '\/\*:\*\/,#\s*if\%(def\)\=:#\s*else\>:#\s*elif\>:#\s*endif\>'
" s:all = pattern with all the keywords
let match_words = match_words . (strlen(match_words) ? "," : "") . default
+ let s:last_words = match_words
if match_words !~ s:notslash . '\\\d'
let s:do_BR = 0
let s:pat = match_words
if exists("b:match_debug")
let b:match_pat = s:pat
endif
+ " Reconstruct the version with unresolved backrefs.
+ let s:patBR = substitute(match_words.',',
+ \ s:notslash.'\zs[,:]*,[,:]*', ',', 'g')
+ let s:patBR = substitute(s:patBR, s:notslash.'\zs:\{2,}', ':', 'g')
endif
" Second step: set the following local variables:
" group = colon-separated list of patterns, one of which matches
" = ini:mid:fin or ini:fin
"
- " Reconstruct the version with unresolved backrefs.
- let patBR = substitute(match_words.',',
- \ s:notslash.'\zs[,:]*,[,:]*', ',', 'g')
- let patBR = substitute(patBR, s:notslash.'\zs:\{2,}', ':', 'g')
" Now, set group and groupBR to the matching group: 'if:endif' or
" 'while:endwhile' or whatever. A bit of a kluge: s:Choose() returns
" group . "," . groupBR, and we pick it apart.
- let group = s:Choose(s:pat, matchline, ",", ":", prefix, suffix, patBR)
+ let group = s:Choose(s:pat, matchline, ",", ":", prefix, suffix, s:patBR)
let i = matchend(group, s:notslash . ",")
let groupBR = strpart(group, i)
let group = strpart(group, 0, i-1)
" Restore options and do some special handling for Operator-pending mode.
" The optional argument is the tail of the matching group.
fun! s:CleanUp(options, mode, startline, startcol, ...)
- execute "set" a:options
+ if strlen(a:options)
+ execute "set" a:options
+ endif
" Open folds, if appropriate.
if a:mode != "o"
if &foldopen =~ "percent"
let regexp = s:Wholematch(matchline, a:1, currcol-1)
let endcol = matchend(matchline, regexp)
if endcol > currcol " This is NOT off by one!
- execute "normal!" . (endcol - currcol) . "l"
+ call cursor(0, endcol)
endif
endif " a:0
endif " a:mode != "o" && etc.
execute s:Ref(ini, d, "start", "len")
let ini = strpart(ini, 0, start) . backref . strpart(ini, start+len)
let tailBR = substitute(tailBR, s:notslash . '\zs\\' . d,
- \ escape(backref, '\\'), 'g')
+ \ escape(backref, '\\&'), 'g')
endif
let d = d-1
endwhile
if !exists("b:match_words") || b:match_words == ""
return ""
end
- let restore_options = (&ic ? "" : "no") . "ignorecase"
- if exists("b:match_ignorecase")
+ let restore_options = ""
+ if exists("b:match_ignorecase") && b:match_ignorecase != &ic
+ let restore_options .= (&ic ? " " : " no") . "ignorecase"
let &ignorecase = b:match_ignorecase
endif
let startline = line(".")
" s:all regexp based on s:pat and the default groups
" This part is copied and slightly modified from s:Match_wrapper().
let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") .
- \ '\/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>'
+ \ '\/\*:\*\/,#\s*if\%(def\)\=:#\s*else\>:#\s*elif\>:#\s*endif\>'
" Allow b:match_words = "GetVimMatchWords()" .
if b:match_words =~ ":"
let match_words = b:match_words
\ exists("b:match_debug")
let s:last_words = match_words
let s:last_mps = &mps
+ let match_words = match_words . (strlen(match_words) ? "," : "") . default
if match_words !~ s:notslash . '\\\d'
let s:do_BR = 0
let s:pat = match_words
let s:do_BR = 1
let s:pat = s:ParseWords(match_words)
endif
- let s:all = '\%(' . substitute(s:pat . (strlen(s:pat)?",":"") . default,
- \ '[,:]\+','\\|','g') . '\)'
+ let s:all = '\%(' . substitute(s:pat . (strlen(s:pat) ? "," : "") . default,
+ \ '[,:]\+', '\\|', 'g') . '\)'
if exists("b:match_debug")
let b:match_pat = s:pat
endif
let skip = 's:comment\|string'
endif
let skip = s:ParseSkip(skip)
- " let restore_cursor = line(".") . "G" . virtcol(".") . "|"
- " normal! H
- " let restore_cursor = "normal!" . line(".") . "Gzt" . restore_cursor
+ " save v:count1 variable, might be reset from the restore_cursor command
+ let level = v:count1
let restore_cursor = virtcol(".") . "|"
normal! g0
let restore_cursor = line(".") . "G" . virtcol(".") . "|zs" . restore_cursor
execute "if " . skip . "| let skip = '0' | endif"
endif
mark '
- let level = v:count1
while level
if searchpair(openpat, '', closepat, a:spflag, skip) < 1
call s:CleanUp(restore_options, a:mode, startline, startcol)
endfun
let &cpo = s:save_cpo
+unlet s:save_cpo
" vim:sts=2:sw=2: