Permalink
Browse files

Version 1.13

The script uses its own logic, instead of the default % behavior, if the match comes from the 'matchpairs' option rather than from the b:match_words variable.  This implies that you can get the matchit behavior for strings and comments just by loading the script; you need not define b:match_words at all.
  • Loading branch information...
Benji Fisher authored and vim-scripts committed Aug 29, 2007
1 parent 7013b7d commit 442745fd6e46ec8d0ff4a8efb42a79091764868e
Showing with 38 additions and 39 deletions.
  1. +17 −15 doc/matchit.txt
  2. +21 −24 plugin/matchit.vim
View
@@ -4,7 +4,7 @@ For instructions on installing this file, type
:help matchit-install
inside Vim.
-For Vim version 6.3. Last change: 2006 Feb 23
+For Vim version 6.3. Last change: 2007 Aug 29
VIM REFERENCE MANUAL by Benji Fisher
@@ -34,7 +34,7 @@ in your |vimrc| file: >
*g%* *v_g%* *o_g%*
g% Cycle backwards through matching groups, as specified by
- |b:match_words|. For example, go from "endif" to "else" to "if".
+ |b:match_words|. For example, go from "if" to "endif" to "else".
*[%* *v_[%* *o_[%*
[% Go to [count] previous unmatched group, as specified by
@@ -64,9 +64,9 @@ option. The matchit plugin extends this in several ways:
By default, words inside comments and strings are ignored, unless
the cursor is inside a comment or string when you type "%". If the
only thing you want to do is modify the behavior of "%" so that it
- behaves this way, you can >
- :let b:match_words = &matchpairs
-<
+ behaves this way, you do not have to define |b:match_words|, since the
+ script uses the 'matchpairs' option as well as this variable.
+
See |matchit-details| for details on what the script does, and |b:match_words|
for how to specify matching patterns.
@@ -84,7 +84,7 @@ LANGUAGES: *matchit-languages*
Currently, the following languages are supported: Ada, ASP with VBS, Csh,
DTD, Entity, Essbase, Fortran, HTML, JSP (same as HTML), LaTeX, Lua, Pascal,
SGML, Shell, Tcsh, Vim, XML. Other languages may already have support via
-|filetype-plugin|s.
+the default |filetype-plugin|s in the standard vim distribution.
To support a new language, see |matchit-newlang| below.
@@ -109,7 +109,6 @@ The script follows these rules:
Prefer a match that includes the cursor position (that is, one that
starts on or before the cursor).
Prefer a match that starts as close to the cursor as possible.
- Prefer a match in |b:match_words| to a match in 'matchpairs'.
If more than one pattern in |b:match_words| matches, choose the one
that is listed first.
@@ -131,9 +130,10 @@ Examples:
cursor starts on the "end " then "end if" is chosen. (You can avoid
this problem by using a more complicated pattern.)
-If there is no match, the script falls back on the usual behavior of |%|. If
-debugging is turned on, the matched bit of text is saved as |b:match_match|
-and the cursor column of the start of the match is saved as |b:match_col|.
+If there is no match, the cursor does not move. (Before version 1.13 of the
+script, it would fall back on the usual behavior of |%|). If debugging is
+turned on, the matched bit of text is saved as |b:match_match| and the cursor
+column of the start of the match is saved as |b:match_col|.
Next, the script looks through |b:match_words| (original and parsed versions)
for the group and pattern that match. If debugging is turned on, the group is
@@ -160,11 +160,13 @@ or >
:runtime macros/matchit.vim
Either way, the script should start working the next time you start up Vim.
-The script does nothing unless it finds a |buffer-variable| named
-|b:match_words|. The script contains autocommands that set this variable for
-various file types: see |matchit-languages| above. For a new language, you
-can add autocommands to the script or to your vimrc file, but the recommended
-method is to add a line such as >
+(Earlier versions of the script did nothing unless a |buffer-variable| named
+|b:match_words| was defined. Even earlier versions contained autocommands
+that set this variable for various file types. Now, |b:match_words| is
+defined in many of the default |filetype-plugin|s instead.)
+
+For a new language, you can add autocommands to the script or to your vimrc
+file, but the recommended method is to add a line such as >
let b:match_words = '\<foo\>:\<bar\>'
to the |filetype-plugin| for your language. See |b:match_words| below for how
this variable is interpreted.
View
@@ -1,7 +1,7 @@
" matchit.vim: (global plugin) Extended "%" matching
-" Last Change: Tue Oct 24 11:00 AM 2006 EDT
+" Last Change: Wed Aug 29 05:00 PM 2007 EDT
" Maintainer: Benji Fisher PhD <benji@member.AMS.org>
-" Version: 1.12, for Vim 6.3+
+" Version: 1.13, for Vim 6.3+
" URL: http://www.vim.org/script.php?script_id=39
" Documentation:
@@ -42,7 +42,7 @@ if exists("loaded_matchit") || &cp
endif
let loaded_matchit = 1
let s:last_mps = ""
-let s:last_words = ""
+let s:last_words = ":"
let s:save_cpo = &cpo
set cpo&vim
@@ -100,22 +100,21 @@ function! s:Match_wrapper(word, forward, mode) range
" In s:CleanUp(), we may need to check whether the cursor moved forward.
let startline = line(".")
let startcol = col(".")
- " Use default behavior if called with a count or if no patterns are defined.
+ " Use default behavior if called with a count.
if v:count
exe "normal! " . v:count . "%"
return s:CleanUp(restore_options, a:mode, startline, startcol)
- elseif !exists("b:match_words") || b:match_words == ""
- silent! normal! %
- return s:CleanUp(restore_options, a:mode, startline, startcol)
end
" First step: if not already done, set the script variables
" s:do_BR flag for whether there are backrefs
" s:pat parsed version of b:match_words
" s:all regexp based on s:pat and the default groups
"
- " Allow b:match_words = "GetVimMatchWords()" .
- if b:match_words =~ ":"
+ if !exists("b:match_words") || b:match_words == ""
+ let match_words = ""
+ " Allow b:match_words = "GetVimMatchWords()" .
+ elseif b:match_words =~ ":"
let match_words = b:match_words
else
execute "let match_words =" b:match_words
@@ -125,13 +124,6 @@ function! s:Match_wrapper(word, forward, mode) range
\ exists("b:match_debug")
let s:last_words = match_words
let s:last_mps = &mps
- if match_words !~ s:notslash . '\\\d'
- let s:do_BR = 0
- let s:pat = match_words
- else
- let s:do_BR = 1
- let s:pat = s:ParseWords(match_words)
- endif
" The next several lines were here before
" BF started messing with this script.
" quote the special chars in 'matchpairs', replace [,:] with \| and then
@@ -141,10 +133,17 @@ function! s:Match_wrapper(word, forward, mode) range
let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") .
\ '\/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>'
" s:all = pattern with all the keywords
- let s:all = s:pat . (strlen(s:pat) ? "," : "") . default
- let s:all = substitute(s:all, s:notslash . '\zs[,:]\+', '\\|', 'g')
+ let match_words = match_words . (strlen(match_words) ? "," : "") . default
+ let s:all = substitute(match_words, s:notslash . '\zs[,:]\+', '\\|', 'g')
let s:all = '\%(' . s:all . '\)'
" let s:all = '\%(' . substitute(s:all, '\\\ze[,:]', '', 'g') . '\)'
+ if match_words !~ s:notslash . '\\\d'
+ let s:do_BR = 0
+ let s:pat = match_words
+ else
+ let s:do_BR = 1
+ let s:pat = s:ParseWords(match_words)
+ endif
if exists("b:match_debug")
let b:match_pat = s:pat
endif
@@ -172,16 +171,14 @@ function! s:Match_wrapper(word, forward, mode) range
else " Find the match that ends on or after the cursor and set curcol.
let regexp = s:Wholematch(matchline, s:all, startcol-1)
let curcol = match(matchline, regexp)
+ " If there is no match, give up.
+ if curcol == -1
+ return s:CleanUp(restore_options, a:mode, startline, startcol)
+ endif
let endcol = matchend(matchline, regexp)
let suf = strlen(matchline) - endcol
let prefix = (curcol ? '^.*\%' . (curcol + 1) . 'c\%(' : '^\%(')
let suffix = (suf ? '\)\%' . (endcol + 1) . 'c.*$' : '\)$')
- " If the match comes from the defaults, bail out.
- if matchline !~ prefix .
- \ substitute(s:pat, s:notslash.'\zs[,:]\+', '\\|', 'g') . suffix
- silent! norm! %
- return s:CleanUp(restore_options, a:mode, startline, startcol)
- endif
endif
if exists("b:match_debug")
let b:match_match = matchstr(matchline, regexp)

0 comments on commit 442745f

Please sign in to comment.