All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 " Last Change: 2013 Dec 30
5 " Author: Andy Wokula <anwoku@yahoo.de>
6 " License: Vim License, see :h license
9 " Implements :ExplainPattern [pattern]
12 " 2013 Jun 21 AND/OR text is confusing, removed
17 " - add something like "(empty) ... match everywhere" ... example: '\v(&&|str)'
19 " Magic Pattern: \(\&\&\|str\)
20 " \( start of first capturing group
21 " | (empty) match everywhere
23 " | (empty) match everywhere
25 " | (empty) match everywhere
27 " | str literal string (3 atom(s))
29 " - more testing, completeness check
30 " ? detailed collections
31 " ? literal string: also print the unescaped magic items
32 " ? literal string: show leading/trailing spaces
39 let g:explainpat#loaded = 1
42 func! explainpat#ExplainPattern(cmd_arg, ...) "{{{
43 " {a:1} alternative help printer object (caution, no sanity check)
46 " let pattern_str = nwo#vis#Get()
47 echo "(usage) :ExplainPattern [{register} | {pattern}]"
49 elseif strlen(a:cmd_arg) == 1 && a:cmd_arg =~ '["@0-9a-z\-:.*+/]'
50 echo 'Register:' a:cmd_arg
51 let pattern_str = getreg(a:cmd_arg)
53 let pattern_str = a:cmd_arg
56 echo printf('Pattern: %s', pattern_str)
57 let magicpat = nwo#magic#MakeMagic(pattern_str)
58 if magicpat !=# pattern_str
59 echo printf('Magic Pattern: %s', magicpat)
63 " set flag when in `\%[ ... ]' (optionally matched atoms):
64 let s:in_opt_atoms = 0
66 let s:capture_group_nr = 0
67 " >=1 at pos 0 or after '\|', '\&', '\(', '\%(' or '\n'; else 0 or less:
68 let s:at_begin_of_pat = 1
70 let hulit = a:0>=1 && type(a:1)==s:DICT ? a:1 : s:NewHelpPrinter()
71 call hulit.AddIndent(' ')
72 let bull = s:NewTokenBiter(magicpat)
74 let item = bull.Bite(s:magic_item_pattern)
76 let Doc = get(s:doc, item, '')
78 call hulit.AddLiteral(item)
79 elseif type(Doc) == s:STRING
80 call hulit.Print(item, Doc)
81 elseif type(Doc) == s:FUNCREF
82 call call(Doc, [bull, hulit, item])
83 elseif type(Doc) == s:LIST
84 call call(Doc[0], [bull, hulit, item, Doc[1]])
86 let s:at_begin_of_pat -= 1
88 echoerr printf('ExplainPattern: cannot parse "%s"', bull.Rest())
93 call hulit.FlushLiterals()
97 let s:STRING = type("")
99 let s:FUNCREF = type(function("tr"))
100 let s:LIST = type([])
103 let s:magic_item_pattern = '\C^\%(\\\%(@<\|%#=\|%[dxouU[(^$V#<>]\=\|z[1-9se(]\|@[>=!]\|_[[^$.]\=\|.\)\|.\)'
106 " this is all the help data ...
107 " strings, funcrefs and intermixed s:DocFoo() functions
108 " strongly depends on s:magic_item_pattern
110 func! s:DocOrBranch(bull, hulit, item) "{{{
111 call a:hulit.RemIndent()
112 call a:hulit.Print(a:item, "OR")
113 call a:hulit.AddIndent(' ')
114 let s:at_begin_of_pat = 2
117 let s:doc['\|'] = function("s:DocOrBranch")
119 func! s:DocBeginOfPat(bull, hulit, item, msg) "{{{
120 call a:hulit.Print(a:item, a:msg)
121 let s:at_begin_of_pat = 2
124 let s:doc['\&'] = [function("s:DocBeginOfPat"), "AND"]
126 let s:ord = split('n first second third fourth fifth sixth seventh eighth ninth')
128 func! s:DocGroupStart(bull, hulit, item) "{{{
130 call a:hulit.Print(a:item, "start of non-capturing group")
131 elseif a:item == '\('
132 let s:capture_group_nr += 1
133 call a:hulit.Print(a:item, printf("start of %s capturing group", get(s:ord, s:capture_group_nr, '(invalid)')))
134 else " a:item == '\z('
135 call a:hulit.Print(a:item, 'start of "external" group (only usable in :syn-region)')
137 call a:hulit.AddIndent('| ', ' ')
138 let s:at_begin_of_pat = 2
140 func! s:DocGroupEnd(bull, hulit, item) "{{{
141 call a:hulit.RemIndent(2)
142 call a:hulit.Print(a:item, "end of group")
145 let s:doc['\('] = function("s:DocGroupStart")
146 let s:doc['\%('] = function("s:DocGroupStart")
147 let s:doc['\)'] = function("s:DocGroupEnd")
148 " let s:doc['\z('] = "only in syntax scripts"
149 let s:doc['\z('] = function("s:DocGroupStart")
151 func! s:DocStar(bull, hulit, item) "{{{
152 if s:at_begin_of_pat >= 1
153 " call a:hulit.Print(a:item, "(at begin of pattern) literal `*'")
154 call a:hulit.AddLiteral(a:item)
156 call a:hulit.Print(a:item, "(multi) zero or more of the preceding atom")
160 " let s:doc['*'] = "(multi) zero or more of the preceding atom"
161 let s:doc['*'] = function("s:DocStar")
163 let s:doc['\+'] = "(multi) one or more of the preceding atom"
164 let s:doc['\='] = "(multi) zero or one of the preceding atom"
165 let s:doc['\?'] = "(multi) zero or one of the preceding atom"
166 " let s:doc['\{'] = "(multi) N to M, greedy"
167 " let s:doc['\{-'] = "(multi) N to M, non-greedy"
169 func! s:DocBraceMulti(bull, hulit, item) "{{{
170 let rest = a:bull.Bite('^-\=\d*\%(,\d*\)\=}')
173 call a:hulit.Print(a:item. rest, "non-greedy version of `*'")
175 call a:hulit.Print(a:item. rest, "(multi) N to M, non-greedy")
177 call a:hulit.Print(a:item. rest, "(multi) N to M, greedy")
180 call a:hulit.Print(a:item, "(invalid) incomplete `\\{...}' item")
184 let s:doc['\{'] = function("s:DocBraceMulti")
186 let s:doc['\@>'] = "(multi) match preceding atom like a full pattern"
187 let s:doc['\@='] = "(assertion) require match for preceding atom"
188 let s:doc['\@!'] = "(assertion) forbid match for preceding atom"
190 func! s:DocBefore(bull, hulit, item) "{{{
191 let rest = a:bull.Bite('^[=!]')
193 call a:hulit.Print(a:item.rest, "(assertion) require match for preceding atom to the left")
195 call a:hulit.Print(a:item.rest, "(assertion) forbid match for preceding atom to the left")
197 call a:hulit.Print(a:item.rest, "(invalid) `\\@<' must be followed by `=' or `!'")
201 let s:doc['\@<'] = function("s:DocBefore")
203 func! s:DocCircumFlex(bull, hulit, item) "{{{
204 if s:at_begin_of_pat >= 1
205 call a:hulit.Print(a:item, "(assertion) require match at start of line")
206 " after `^' is not at begin of pattern ... handle special case `^*' here:
207 if a:bull.Bite('^\*') == "*"
208 call a:hulit.AddLiteral("*")
211 " call a:hulit.Print(a:item, "(not at begin of pattern) literal `^'")
212 call a:hulit.AddLiteral(a:item)
216 " let s:doc['^'] = "(assertion) require match at start of line"
217 let s:doc['^'] = function("s:DocCircumFlex")
219 let s:doc['\_^'] = "(assertion) like `^', allowed anywhere in the pattern"
221 func! s:DocDollar(bull, hulit, item) "{{{
222 if a:bull.Rest() =~ '^$\|^\\[&|)n]'
223 call a:hulit.Print(a:item, "(assertion) require match at end of line")
225 call a:hulit.AddLiteral(a:item)
229 " let s:doc['$'] = "(assertion) require match at end of line"
230 let s:doc['$'] = function("s:DocDollar")
232 let s:doc['\_$'] = "(assertion) like `$', allowed anywhere in the pattern"
233 let s:doc['.'] = "match any character"
234 let s:doc['\_.'] = "match any character or newline"
236 func! s:DocUnderscore(bull, hulit, item) "{{{
237 let cclass = a:bull.Bite('^\a')
239 let cclass_doc = get(s:doc, '\'. cclass, '(invalid character class)')
240 call a:hulit.Print(a:item. cclass, printf('%s or end-of-line', cclass_doc))
242 call a:hulit.Print(a:item, "(invalid) `\\_' should be followed by a letter or `[...]'")
243 " echoerr printf('ExplainPattern: cannot parse %s', a:item. matchstr(a:bull.Rest(), '.'))
247 let s:doc['\_'] = function("s:DocUnderscore")
248 let s:doc['\<'] = "(assertion) require match at begin of word, :h word"
249 let s:doc['\>'] = "(assertion) require match at end of word, :h word"
250 let s:doc['\zs'] = "set begin of match here"
251 let s:doc['\ze'] = "set end of match here"
252 let s:doc['\%^'] = "(assertion) match at begin of buffer"
253 let s:doc['\%$'] = "(assertion) match at end of buffer"
254 let s:doc['\%V'] = "(assertion) match within the Visual area"
255 let s:doc['\%#'] = "(assertion) match with cursor position"
257 func! s:DocRegexEngine(bull, hulit, item) "{{{
258 let engine = a:bull.Bite('^[012]')
260 call a:hulit.Print(a:item.engine, 'Force automatic selection of the regexp engine (since v7.3.970).')
262 call a:hulit.Print(a:item.engine, 'Force using the old engine (since v7.3.970).')
264 call a:hulit.Print(a:item.engine, 'Force using the NFA engine (since v7.3.970).')
266 call a:hulit.Print(a:item, '(invalid) \%#= can only be followed by 0, 1, or 2')
270 let s:doc['\%#='] = function("s:DocRegexEngine")
273 " \%23l \%<23l \%>23l
274 " \%23c \%<23c \%>23c
275 " \%23v \%<23v \%>23v
276 " backslash percent at/before/after
277 func! s:DocBspercAt(bull, hulit, item) "{{{
278 let rest = a:bull.Bite('^\%(''.\|\d\+[lvc]\)\C')
280 call a:hulit.Print(a:item.rest, "(assertion) match with position of mark ". rest[1])
282 let number = rest[:-2]
285 call a:hulit.Print(a:item.rest, "match in line ". number)
287 call a:hulit.Print(a:item.rest, "match in column ". number)
289 call a:hulit.Print(a:item.rest, "match in virtual column ". number)
291 call a:hulit.Print(a:item.rest, "(invalid) incomplete `\\%' item")
292 " echoerr printf('ExplainPattern: incomplete item %s', a:item. rest)
296 func! s:DocBspercBefore(bull, hulit, item) "{{{
297 let rest = a:bull.Bite('^\%(''.\|\d\+[lvc]\)\C')
299 call a:hulit.Print(a:item.rest, "(assertion) match before position of mark ". rest[1])
301 let number = rest[:-2]
304 call a:hulit.Print(a:item.rest, printf("match above line %d (towards start of buffer)", number))
306 call a:hulit.Print(a:item.rest, "match before column ". number)
308 call a:hulit.Print(a:item.rest, "match before virtual column ". number)
310 call a:hulit.Print(a:item.rest, "(invalid) incomplete `\\%<' item")
311 " echoerr printf('ExplainPattern: incomplete item %s', a:item. rest)
315 func! s:DocBspercAfter(bull, hulit, item) "{{{
316 let rest = a:bull.Bite('^\%(''.\|\d\+[lvc]\)\C')
318 call a:hulit.Print(a:item.rest, "(assertion) match after position of mark ". rest[1])
320 let number = rest[:-2]
323 call a:hulit.Print(a:item.rest, printf("match below line %d (towards end of buffer)", number))
325 call a:hulit.Print(a:item.rest, "match after column ". number)
327 call a:hulit.Print(a:item.rest, "match after virtual column ". number)
329 call a:hulit.Print(a:item.rest, "(invalid) incomplete `\\%>' item")
330 " echoerr printf('ExplainPattern: incomplete item %s', a:item. rest)
335 let s:doc['\%'] = function("s:DocBspercAt")
336 let s:doc['\%<'] = function("s:DocBspercBefore")
337 let s:doc['\%>'] = function("s:DocBspercAfter")
339 let s:doc['\i'] = "identifier character (see 'isident' option)"
340 let s:doc['\I'] = "like \"\\i\", but excluding digits"
341 let s:doc['\k'] = "keyword character (see 'iskeyword' option)"
342 let s:doc['\K'] = "like \"\\k\", but excluding digits"
343 let s:doc['\f'] = "file name character (see 'isfname' option)"
344 let s:doc['\F'] = "like \"\\f\", but excluding digits"
345 let s:doc['\p'] = "printable character (see 'isprint' option)"
346 let s:doc['\P'] = "like \"\\p\", but excluding digits"
347 let s:doc['\s'] = "whitespace character: <Space> and <Tab>"
348 let s:doc['\S'] = "non-whitespace character; opposite of \\s"
349 let s:doc['\d'] = "digit: [0-9]"
350 let s:doc['\D'] = "non-digit: [^0-9]"
351 let s:doc['\x'] = "hex digit: [0-9A-Fa-f]"
352 let s:doc['\X'] = "non-hex digit: [^0-9A-Fa-f]"
353 let s:doc['\o'] = "octal digit: [0-7]"
354 let s:doc['\O'] = "non-octal digit: [^0-7]"
355 let s:doc['\w'] = "word character: [0-9A-Za-z_]"
356 let s:doc['\W'] = "non-word character: [^0-9A-Za-z_]"
357 let s:doc['\h'] = "head of word character: [A-Za-z_]"
358 let s:doc['\H'] = "non-head of word character: [^A-Za-z_]"
359 let s:doc['\a'] = "alphabetic character: [A-Za-z]"
360 let s:doc['\A'] = "non-alphabetic character: [^A-Za-z]"
361 let s:doc['\l'] = "lowercase character: [a-z]"
362 let s:doc['\L'] = "non-lowercase character: [^a-z]"
363 let s:doc['\u'] = "uppercase character: [A-Z]"
364 let s:doc['\U'] = "non-uppercase character: [^A-Z]"
366 let s:doc['\e'] = "match <Esc>"
367 let s:doc['\t'] = "match <Tab>"
368 let s:doc['\r'] = "match <CR>"
369 let s:doc['\b'] = "match CTRL-H"
370 let s:doc['\n'] = [function("s:DocBeginOfPat"), "match a newline"]
371 let s:doc['~'] = "match the last given substitute string"
372 let s:doc['\1'] = "match first captured string"
373 let s:doc['\2'] = "match second captured string"
374 let s:doc['\3'] = "match third captured string"
375 let s:doc['\4'] = "match fourth captured string "
376 let s:doc['\5'] = "match fifth captured string"
377 let s:doc['\6'] = "match sixth captured string"
378 let s:doc['\7'] = "match seventh captured string"
379 let s:doc['\8'] = "match eighth captured string"
380 let s:doc['\9'] = "match ninth captured string"
382 let s:doc['\z1'] = 'match same string matched by first "external" group'
383 let s:doc['\z2'] = 'match same string matched by second "external" group'
384 let s:doc['\z3'] = 'match same string matched by third "external" group'
385 let s:doc['\z4'] = 'match same string matched by fourth "external" group '
386 let s:doc['\z5'] = 'match same string matched by fifth "external" group'
387 let s:doc['\z6'] = 'match same string matched by sixth "external" group'
388 let s:doc['\z7'] = 'match same string matched by seventh "external" group'
389 let s:doc['\z8'] = 'match same string matched by eighth "external" group'
390 let s:doc['\z9'] = 'match same string matched by ninth "external" group'
393 " skip the rest of a collection
394 let s:coll_skip_pat = '^\^\=]\=\%(\%(\\[\^\]\-\\bertn]\|\[:\w\+:]\|\[=.=]\|\[\..\.]\|[^\]]\)\@>\)*]'
396 func! s:DocCollection(bull, hulit, item) "{{{
397 let collstr = a:bull.Bite(s:coll_skip_pat)
398 if collstr == "" || collstr == "]"
399 call a:hulit.AddLiteral('['. collstr)
401 let inverse = collstr =~ '^\^'
402 let with_nl = a:item == '\_['
403 let descr = inverse ? printf('collection not matching [%s', collstr[1:]) : 'collection'
404 let descr_nl = printf("%s%s", (inverse && with_nl ? ', but' : ''), (with_nl ? ' with end-of-line added' : ''))
405 call a:hulit.Print(a:item. collstr, descr. descr_nl)
409 let s:doc['['] = function("s:DocCollection")
410 let s:doc['\_['] = function("s:DocCollection")
412 func! s:DocOptAtoms(bull, hulit, item) "{{{
414 call a:hulit.Print(a:item, "start a sequence of optionally matched atoms")
415 let s:in_opt_atoms = 1
416 call a:hulit.AddIndent('. ')
419 call a:hulit.RemIndent()
420 call a:hulit.Print(a:item, "end of optionally matched atoms")
421 let s:in_opt_atoms = 0
423 call a:hulit.AddLiteral(a:item)
428 " let s:doc['\%['] = "start a sequence of optionally matched atoms"
429 let s:doc['\%['] = function("s:DocOptAtoms")
430 let s:doc[']'] = function("s:DocOptAtoms")
432 func! s:DocAnywhere(bull, hulit, item, msg) "{{{
433 call a:hulit.Print(a:item, a:msg)
435 let s:at_begin_of_pat += 1
438 let s:doc['\c'] = [function("s:DocAnywhere"), "ignore case while matching the pattern"]
439 let s:doc['\C'] = [function("s:DocAnywhere"), "match case while matching the pattern"]
440 let s:doc['\Z'] = [function("s:DocAnywhere"), "ignore composing characters in the pattern"]
448 func! s:DocBspercDecimal(bull, hulit, item) "{{{
449 let number = a:bull.Bite('^\d\{,3}')
450 let char = strtrans(nr2char(str2nr(number)))
451 call a:hulit.Print(a:item. number, printf("match character specified by decimal number %s (%s)", number, char))
453 func! s:DocBspercHexTwo(bull, hulit, item) "{{{
454 let number = a:bull.Bite('^\x\{,2}')
455 let char = strtrans(nr2char(str2nr(number,16)))
456 call a:hulit.Print(a:item. number, printf("match character specified with hex number 0x%s (%s)", number, char))
458 func! s:DocBspercOctal(bull, hulit, item) "{{{
459 let number = a:bull.Bite('^\o\{,4}')
460 let char = strtrans(nr2char(str2nr(number,8)))
461 call a:hulit.Print(a:item. number, printf("match character specified with octal number 0%s (%s)", substitute(number, '^0*', '', ''), char))
463 func! s:DocBspercHexFour(bull, hulit, item) "{{{
464 let number = a:bull.Bite('^\x\{,4}')
465 let char = has("multi_byte_encoding") ? ' ('. strtrans(nr2char(str2nr(number,16))).')' : ''
466 call a:hulit.Print(a:item. number, printf("match character specified with hex number 0x%s%s", number, char))
468 func! s:DocBspercHexEight(bull, hulit, item) "{{{
469 let number = a:bull.Bite('^\x\{,8}')
470 let char = has("multi_byte_encoding") ? ' ('. strtrans(nr2char(str2nr(number,16))).')' : ''
471 call a:hulit.Print(a:item. number, printf("match character specified with hex number 0x%s%s", number, char))
474 let s:doc['\%d'] = function("s:DocBspercDecimal") " 123
475 let s:doc['\%x'] = function("s:DocBspercHexTwo") " 2a
476 let s:doc['\%o'] = function("s:DocBspercOctal") " 0377
477 let s:doc['\%u'] = function("s:DocBspercHexFour") " 20AC
478 let s:doc['\%U'] = function("s:DocBspercHexEight") " 1234abcd
486 func! s:NewHelpPrinter() "{{{
488 let obj.literals = ''
490 let obj.len = 0 " can be negative (!)
492 func! obj.Print(str, ...) "{{{
493 call self.FlushLiterals()
494 let indstr = join(self.indents, '')
501 " echo indstr. printf("`%s' %s", a:str, a:1)
503 echon printf("%-10s", a:str)
506 echon printf(" %s", a:1)
511 func! obj.AddLiteral(item) "{{{
512 let self.literals .= a:item
515 func! obj.FlushLiterals() "{{{
516 if self.literals == ''
519 let indstr = join(self.indents, '')
523 if self.literals =~ '^\s\|\s$'
524 echon printf("%-10s", '"'. self.literals. '"')
526 echon printf("%-10s", self.literals)
529 echon " literal string"
530 if exists("*strchars")
531 if self.literals =~ '\\'
532 let self.literals = substitute(self.literals, '\\\(.\)', '\1', 'g')
534 let spconly = self.literals =~ '[^ ]' ? '' : ', spaces only'
535 let nlit = strchars(self.literals)
536 echon " (". nlit. (nlit==1 ? " atom" : " atoms"). spconly.")"
539 let self.literals = ''
542 func! obj.AddIndent(...) "{{{
543 call self.FlushLiterals()
545 call extend(self.indents, copy(a:000))
546 elseif self.len + a:0 >= 1
547 call extend(self.indents, a:000[-(self.len+a:0):])
552 func! obj.RemIndent(...) "{{{
553 call self.FlushLiterals()
556 call remove(self.indents, -1)
561 call remove(self.indents, -a:1, -1)
563 call remove(self.indents, 0, -1)
572 func! s:NewTokenBiter(str) "{{{
573 " {str} string to eat pieces from
574 let obj = {'str': a:str}
576 " consume piece from start of input matching {pat}
577 func! obj.Bite(pat) "{{{
578 " {pat} should start with '^'
579 let bite = matchstr(self.str, a:pat)
580 let self.str = strpart(self.str, strlen(bite))
584 " get the unparsed rest of input (not consuming)
585 func! obj.Rest() "{{{
589 " check if end of input reached
590 func! obj.AtEnd() "{{{
591 return self.str == ""
598 let &cpo = s:cpo_save
600 " vim:ts=8:fdm=marker: