pi_matchit.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. *pi_matchit.txt* Extended "%" matching
  2. For Vim version 6.3. Last change: 2017 May 14
  3. *matchit* *matchit.vim*
  4. 1. Extended matching with "%" |matchit-intro|
  5. 2. Activation |matchit-activate|
  6. 3. Configuration |matchit-configure|
  7. 4. Supporting a New Language |matchit-newlang|
  8. 5. Known Bugs and Limitations |matchit-bugs|
  9. The functionality mentioned here is a plugin, see |add-plugin|.
  10. You can avoid loading this plugin by setting the "loaded_matchit" variable
  11. in your |vimrc| file: >
  12. :let loaded_matchit = 1
  13. ==============================================================================
  14. 1. Extended matching with "%" *matchit-intro*
  15. *matchit-%*
  16. % Cycle forward through matching groups, such as "if", "else", "endif",
  17. as specified by |b:match_words|.
  18. *g%* *v_g%* *o_g%*
  19. g% Cycle backwards through matching groups, as specified by
  20. |b:match_words|. For example, go from "if" to "endif" to "else".
  21. *[%* *v_[%* *o_[%*
  22. [% Go to [count] previous unmatched group, as specified by
  23. |b:match_words|. Similar to |[{|.
  24. *]%* *v_]%* *o_]%*
  25. ]% Go to [count] next unmatched group, as specified by
  26. |b:match_words|. Similar to |]}|.
  27. *a%* *v_a%*
  28. a% In Visual mode, select the matching group, as specified by
  29. |b:match_words|, containing the cursor. Similar to |v_a[|.
  30. A [count] is ignored, and only the first character of the closing
  31. pattern is selected.
  32. In Vim, as in plain vi, the percent key, |%|, jumps the cursor from a brace,
  33. bracket, or paren to its match. This can be configured with the 'matchpairs'
  34. option. The matchit plugin extends this in several ways:
  35. You can match whole words, such as "if" and "endif", not just
  36. single characters. You can also specify a |regular-expression|.
  37. You can define groups with more than two words, such as "if",
  38. "else", "endif". Banging on the "%" key will cycle from the "if" to
  39. the first "else", the next "else", ..., the closing "endif", and back
  40. to the opening "if". Nested structures are skipped. Using |g%| goes
  41. in the reverse direction.
  42. By default, words inside comments and strings are ignored, unless
  43. the cursor is inside a comment or string when you type "%". If the
  44. only thing you want to do is modify the behavior of "%" so that it
  45. behaves this way, you do not have to define |b:match_words|, since the
  46. script uses the 'matchpairs' option as well as this variable.
  47. See |matchit-details| for details on what the script does, and |b:match_words|
  48. for how to specify matching patterns.
  49. MODES: *matchit-modes* *matchit-v_%* *matchit-o_%*
  50. Mostly, % and related motions (|g%| and |[%| and |]%|) work just like built-in
  51. motion commands in |Operator-pending| and |Visual| modes. However, you
  52. cannot make these motions |linewise| or |characterwise|, since the |:omap|s
  53. that define them start with "v" in order to make the default behavior
  54. inclusive. (See |o_v|.) In other words, "dV%" will not work. The
  55. work-around is to go through Visual mode: "V%d" will work.
  56. LANGUAGES: *matchit-languages*
  57. Currently, the following languages are supported: Ada, ASP with VBS, Csh,
  58. DTD, Entity, Essbase, Fortran, HTML, JSP (same as HTML), LaTeX, Lua, Pascal,
  59. SGML, Shell, Tcsh, Vim, XML. Other languages may already have support via
  60. the default |filetype-plugin|s in the standard vim distribution.
  61. To support a new language, see |matchit-newlang| below.
  62. DETAILS: *matchit-details* *matchit-parse*
  63. Here is an outline of what matchit.vim does each time you hit the "%" key. If
  64. there are backrefs in |b:match_words| then the first step is to produce a
  65. version in which these back references have been eliminated; if there are no
  66. backrefs then this step is skipped. This step is called parsing. For
  67. example, "\(foo\|bar\):end\1" is parsed to yield
  68. "\(foo\|bar\):end\(foo\|bar\)". This can get tricky, especially if there are
  69. nested groups. If debugging is turned on, the parsed version is saved as
  70. |b:match_pat|.
  71. *matchit-choose*
  72. Next, the script looks for a word on the current line that matches the pattern
  73. just constructed. It includes the patterns from the 'matchpairs' option.
  74. The goal is to do what you expect, which turns out to be a little complicated.
  75. The script follows these rules:
  76. Insist on a match that ends on or after the cursor.
  77. Prefer a match that includes the cursor position (that is, one that
  78. starts on or before the cursor).
  79. Prefer a match that starts as close to the cursor as possible.
  80. If more than one pattern in |b:match_words| matches, choose the one
  81. that is listed first.
  82. Examples:
  83. Suppose you >
  84. :let b:match_words = '<:>,<tag>:</tag>'
  85. < and hit "%" with the cursor on or before the "<" in "a <tag> is born".
  86. The pattern '<' comes first, so it is preferred over '<tag>', which
  87. also matches. If the cursor is on the "t", however, then '<tag>' is
  88. preferred, because this matches a bit of text containing the cursor.
  89. If the two groups of patterns were reversed then '<' would never be
  90. preferred.
  91. Suppose you >
  92. :let b:match_words = 'if:end if'
  93. < (Note the space!) and hit "%" with the cursor at the end of "end if".
  94. Then "if" matches, which is probably not what you want, but if the
  95. cursor starts on the "end " then "end if" is chosen. (You can avoid
  96. this problem by using a more complicated pattern.)
  97. If there is no match, the cursor does not move. (Before version 1.13 of the
  98. script, it would fall back on the usual behavior of |%|). If debugging is
  99. turned on, the matched bit of text is saved as |b:match_match| and the cursor
  100. column of the start of the match is saved as |b:match_col|.
  101. Next, the script looks through |b:match_words| (original and parsed versions)
  102. for the group and pattern that match. If debugging is turned on, the group is
  103. saved as |b:match_ini| (the first pattern) and |b:match_tail| (the rest). If
  104. there are backrefs then, in addition, the matching pattern is saved as
  105. |b:match_word| and a table of translations is saved as |b:match_table|. If
  106. there are backrefs, these are determined from the matching pattern and
  107. |b:match_match| and substituted into each pattern in the matching group.
  108. The script decides whether to search forwards or backwards and chooses
  109. arguments for the |searchpair()| function. Then, the cursor is moved to the
  110. start of the match, and |searchpair()| is called. By default, matching
  111. structures inside strings and comments are ignored. This can be changed by
  112. setting |b:match_skip|.
  113. ==============================================================================
  114. 2. Activation *matchit-activate*
  115. For a new language, you can add a line such as >
  116. let b:match_words = '\<foo\>:\<bar\>'
  117. to the corresponding |filetype-plugin|. See |b:match_words| below for how
  118. this variable is interpreted.
  119. ==============================================================================
  120. 3. Configuration *matchit-configure*
  121. There are several variables that govern the behavior of matchit.vim. Note
  122. that these are variables local to the buffer, not options, so use |:let| to
  123. define them, not |:set|. Some of these variables have values that matter; for
  124. others, it only matters whether the variable has been defined. All of these
  125. can be defined in the |filetype-plugin| or autocommand that defines
  126. |b:match_words| or "on the fly."
  127. The main variable is |b:match_words|. It is described in the section below on
  128. supporting a new language.
  129. *MatchError* *matchit-hl* *matchit-highlight*
  130. MatchError is the highlight group for error messages from the script. By
  131. default, it is linked to WarningMsg. If you do not want to be bothered by
  132. error messages, you can define this to be something invisible. For example,
  133. if you use the GUI version of Vim and your command line is normally white, you
  134. can do >
  135. :hi MatchError guifg=white guibg=white
  136. <
  137. *b:match_ignorecase*
  138. If you >
  139. :let b:match_ignorecase = 1
  140. then matchit.vim acts as if 'ignorecase' is set: for example, "end" and "END"
  141. are equivalent. If you >
  142. :let b:match_ignorecase = 0
  143. then matchit.vim treats "end" and "END" differently. (There will be no
  144. b:match_infercase option unless someone requests it.)
  145. *b:match_debug*
  146. Define b:match_debug if you want debugging information to be saved. See
  147. |matchit-debug|, below.
  148. *b:match_skip*
  149. If b:match_skip is defined, it is passed as the skip argument to
  150. |searchpair()|. This controls when matching structures are skipped, or
  151. ignored. By default, they are ignored inside comments and strings, as
  152. determined by the |syntax| mechanism. (If syntax highlighting is turned off,
  153. nothing is skipped.) You can set b:match_skip to a string, which evaluates to
  154. a non-zero, numerical value if the match is to be skipped or zero if the match
  155. should not be skipped. In addition, the following special values are
  156. supported by matchit.vim:
  157. s:foo becomes (current syntax item) =~ foo
  158. S:foo becomes (current syntax item) !~ foo
  159. r:foo becomes (line before cursor) =~ foo
  160. R:foo becomes (line before cursor) !~ foo
  161. (The "s" is meant to suggest "syntax", and the "r" is meant to suggest
  162. "regular expression".)
  163. Examples:
  164. You can get the default behavior with >
  165. :let b:match_skip = 's:comment\|string'
  166. <
  167. If you want to skip matching structures unless they are at the start
  168. of the line (ignoring whitespace) then you can >
  169. :let b:match_skip = 'R:^\s*'
  170. < Do not do this if strings or comments can span several lines, since
  171. the normal syntax checking will not be done if you set b:match_skip.
  172. In LaTeX, since "%" is used as the comment character, you can >
  173. :let b:match_skip = 'r:%'
  174. < Unfortunately, this will skip anything after "\%", an escaped "%". To
  175. allow for this, and also "\\%" (an escaped backslash followed by the
  176. comment character) you can >
  177. :let b:match_skip = 'r:\(^\|[^\\]\)\(\\\\\)*%'
  178. <
  179. See the $VIMRUNTIME/ftplugin/vim.vim for an example that uses both
  180. syntax and a regular expression.
  181. ==============================================================================
  182. 4. Supporting a New Language *matchit-newlang*
  183. *b:match_words*
  184. In order for matchit.vim to support a new language, you must define a suitable
  185. pattern for |b:match_words|. You may also want to set some of the
  186. |matchit-configure| variables, as described above. If your language has a
  187. complicated syntax, or many keywords, you will need to know something about
  188. Vim's |regular-expression|s.
  189. The format for |b:match_words| is similar to that of the 'matchpairs' option:
  190. it is a comma (,)-separated list of groups; each group is a colon(:)-separated
  191. list of patterns (regular expressions). Commas and backslashes that are part
  192. of a pattern should be escaped with backslashes ('\:' and '\,'). It is OK to
  193. have only one group; the effect is undefined if a group has only one pattern.
  194. A simple example is >
  195. :let b:match_words = '\<if\>:\<endif\>,'
  196. \ . '\<while\>:\<continue\>:\<break\>:\<endwhile\>'
  197. (In Vim regular expressions, |/\<| and |/\>| denote word boundaries. Thus "if"
  198. matches the end of "endif" but "\<if\>" does not.) Then banging on the "%"
  199. key will bounce the cursor between "if" and the matching "endif"; and from
  200. "while" to any matching "continue" or "break", then to the matching "endwhile"
  201. and back to the "while". It is almost always easier to use |literal-string|s
  202. (single quotes) as above: '\<if\>' rather than "\\<if\\>" and so on.
  203. Exception: If the ":" character does not appear in b:match_words, then it is
  204. treated as an expression to be evaluated. For example, >
  205. :let b:match_words = 'GetMatchWords()'
  206. allows you to define a function. This can return a different string depending
  207. on the current syntax, for example.
  208. Once you have defined the appropriate value of |b:match_words|, you will
  209. probably want to have this set automatically each time you edit the
  210. appropriate file type. The recommended way to do this is by adding the
  211. definition to a |filetype-plugin| file.
  212. Tips: Be careful that your initial pattern does not match your final pattern.
  213. See the example above for the use of word-boundary expressions. It is usually
  214. better to use ".\{-}" (as many as necessary) instead of ".*" (as many as
  215. possible). See |/\{-|. For example, in the string "<tag>label</tag>", "<.*>"
  216. matches the whole string whereas "<.\{-}>" and "<[^>]*>" match "<tag>" and
  217. "</tag>".
  218. *matchit-spaces* *matchit-s:notend*
  219. If "if" is to be paired with "end if" (Note the space!) then word boundaries
  220. are not enough. Instead, define a regular expression s:notend that will match
  221. anything but "end" and use it as follows: >
  222. :let s:notend = '\%(\<end\s\+\)\@<!'
  223. :let b:match_words = s:notend . '\<if\>:\<end\s\+if\>'
  224. < *matchit-s:sol*
  225. This is a simplified version of what is done for Ada. The s:notend is a
  226. |script-variable|. Similarly, you may want to define a start-of-line regular
  227. expression >
  228. :let s:sol = '\%(^\|;\)\s*'
  229. if keywords are only recognized after the start of a line or after a
  230. semicolon (;), with optional white space.
  231. *matchit-backref* *matchit-\1*
  232. In any group, the expressions `\1`, `\2`, ..., `\9` (see |/\1|) refer to parts of the
  233. INITIAL pattern enclosed in escaped parentheses. These are referred to as
  234. back references, or backrefs. For example, >
  235. :let b:match_words = '\<b\(o\+\)\>:\(h\)\1\>'
  236. means that "bo" pairs with "ho" and "boo" pairs with "hoo" and so on. Note
  237. that "\1" does not refer to the "\(h\)" in this example. If you have
  238. "\(nested \(parentheses\)\) then "\d" refers to the d-th "\(" and everything
  239. up to and including the matching "\)": in "\(nested\(parentheses\)\)", "\1"
  240. refers to everything and "\2" refers to "\(parentheses\)". If you use a
  241. variable such as `s:notend` or `s:sol` in the previous paragraph then remember
  242. to count any "\(" patterns in this variable. You do not have to count groups
  243. defined by |/\%(\)|.
  244. It should be possible to resolve back references from any pattern in the
  245. group. For example, >
  246. :let b:match_words = '\(foo\)\(bar\):more\1:and\2:end\1\2'
  247. would not work because "\2" cannot be determined from "morefoo" and "\1"
  248. cannot be determined from "andbar". On the other hand, >
  249. :let b:match_words = '\(\(foo\)\(bar\)\):\3\2:end\1'
  250. should work (and have the same effect as "foobar:barfoo:endfoobar"), although
  251. this has not been thoroughly tested.
  252. You can use |/zero-width| patterns such as |/\@<=| and |/\zs|. (The latter has
  253. not been thouroughly tested in matchit.vim.) For example, if the keyword "if"
  254. must occur at the start of the line, with optional white space, you might use
  255. the pattern "\(^\s*\)\@<=if" so that the cursor will end on the "i" instead of
  256. at the start of the line. For another example, if HTML had only one tag then
  257. one could >
  258. :let b:match_words = '<:>,<\@<=tag>:<\@<=/tag>'
  259. so that "%" can bounce between matching "<" and ">" pairs or (starting on
  260. "tag" or "/tag") between matching tags. Without the |/\@<=|, the script would
  261. bounce from "tag" to the "<" in "</tag>", and another "%" would not take you
  262. back to where you started.
  263. DEBUGGING *matchit-debug* *:MatchDebug*
  264. If you are having trouble figuring out the appropriate definition of
  265. |b:match_words| then you can take advantage of the same information I use when
  266. debugging the script. This is especially true if you are not sure whether
  267. your patterns or my script are at fault! To make this more convenient, I have
  268. made the command :MatchDebug, which defines the variable |b:match_debug| and
  269. creates a Matchit menu. This menu makes it convenient to check the values of
  270. the variables described below. You will probably also want to read
  271. |matchit-details| above.
  272. Defining the variable |b:match_debug| causes the script to set the following
  273. variables, each time you hit the "%" key. Several of these are only defined
  274. if |b:match_words| includes backrefs.
  275. *b:match_pat*
  276. The b:match_pat variable is set to |b:match_words| with backrefs parsed.
  277. *b:match_match*
  278. The b:match_match variable is set to the bit of text that is recognized as a
  279. match.
  280. *b:match_col*
  281. The b:match_col variable is set to the cursor column of the start of the
  282. matching text.
  283. *b:match_wholeBR*
  284. The b:match_wholeBR variable is set to the comma-separated group of patterns
  285. that matches, with backrefs unparsed.
  286. *b:match_iniBR*
  287. The b:match_iniBR variable is set to the first pattern in |b:match_wholeBR|.
  288. *b:match_ini*
  289. The b:match_ini variable is set to the first pattern in |b:match_wholeBR|,
  290. with backrefs resolved from |b:match_match|.
  291. *b:match_tail*
  292. The b:match_tail variable is set to the remaining patterns in
  293. |b:match_wholeBR|, with backrefs resolved from |b:match_match|.
  294. *b:match_word*
  295. The b:match_word variable is set to the pattern from |b:match_wholeBR| that
  296. matches |b:match_match|.
  297. *b:match_table*
  298. The back reference '\'.d refers to the same thing as '\'.b:match_table[d] in
  299. |b:match_word|.
  300. ==============================================================================
  301. 5. Known Bugs and Limitations *matchit-bugs*
  302. The various |:vmap|s defined in the script (%, |g%|, |[%|, |]%|, |a%|) may
  303. have undesired effects in Select mode |Select-mode-mapping|. At least, if you
  304. want to replace the selection with any character in "ag%[]" there will be a
  305. pause of |'updatetime'| first. E.g., "yV%" would normally work linewise, but
  306. the plugin mapping makes it characterwise.
  307. It would be nice if "\0" were recognized as the entire pattern. That is, it
  308. would be nice if "foo:\end\0" had the same effect as "\(foo\):\end\1".
  309. ==============================================================================
  310. vim:tw=78:ts=8:noet:ft=help:norl: