From e0a3cf753e3df2188acab5c64f8555fbc08a2c4f Mon Sep 17 00:00:00 2001 From: LCD 47 Date: Tue, 22 Sep 2015 12:27:55 +0300 Subject: [PATCH] Add width controls to the new stl_format flags. --- autoload/syntastic/util.vim | 78 +++++++++++++++++++++++++++++++----- doc/syntastic.txt | 7 ++++ plugin/syntastic.vim | 2 +- plugin/syntastic/loclist.vim | 3 +- 4 files changed, 78 insertions(+), 12 deletions(-) diff --git a/autoload/syntastic/util.vim b/autoload/syntastic/util.vim index 92e0bdd4..9dbf4116 100644 --- a/autoload/syntastic/util.vim +++ b/autoload/syntastic/util.vim @@ -107,16 +107,16 @@ function! syntastic#util#rmrf(what) abort " {{{2 endif endfunction " }}}2 -"search the first 5 lines of the file for a magic number and return a map -"containing the args and the executable +" Search the first 5 lines of the file for a magic number and return a map +" containing the args and the executable " -"e.g. +" e.g. " -"#!/usr/bin/perl -f -bar +" #!/usr/bin/perl -f -bar " -"returns +" returns " -"{'exe': '/usr/bin/perl', 'args': ['-f', '-bar']} +" {'exe': '/usr/bin/perl', 'args': ['-f', '-bar']} function! syntastic#util#parseShebang() abort " {{{2 for lnum in range(1, 5) let line = getline(lnum) @@ -183,7 +183,7 @@ function! syntastic#util#screenWidth(str, tabstop) abort " {{{2 return width endfunction " }}}2 -"print as much of a:msg as possible without "Press Enter" prompt appearing +" Print as much of a:msg as possible without "Press Enter" prompt appearing function! syntastic#util#wideMsg(msg) abort " {{{2 let old_ruler = &ruler let old_showcmd = &showcmd @@ -226,7 +226,7 @@ function! syntastic#util#bufIsActive(buffer) abort " {{{2 return 0 endfunction " }}}2 -" start in directory a:where and walk up the parent folders until it finds a +" Start in directory a:where and walk up the parent folders until it finds a " file named a:what; return path to that file function! syntastic#util#findFileInParent(what, where) abort " {{{2 let old_suffixesadd = &suffixesadd @@ -236,7 +236,7 @@ function! syntastic#util#findFileInParent(what, where) abort " {{{2 return file endfunction " }}}2 -" start in directory a:where and walk up the parent folders until it finds a +" Start in directory a:where and walk up the parent folders until it finds a " file matching a:what; return path to that file function! syntastic#util#findGlobInParent(what, where) abort " {{{2 let here = fnamemodify(a:where, ':p') @@ -303,7 +303,7 @@ function! syntastic#util#argsescape(opt) abort " {{{2 return [] endfunction " }}}2 -" decode XML entities +" Decode XML entities function! syntastic#util#decodeXMLEntities(string) abort " {{{2 let str = a:string let str = substitute(str, '\m<', '<', 'g') @@ -350,6 +350,64 @@ function! syntastic#util#float2str(val) abort " {{{2 return s:_float2str(a:val) endfunction " }}}2 +" Crude printf()-like width formatter. Handles wide characters. +function! syntastic#util#wformat(format, str) abort " {{{2 + if a:format ==# '' + return a:str + endif + + echomsg string(a:format) . ', ' . string(a:str) + let specs = matchlist(a:format, '\v^(-?)(0?)(%([1-9]\d*))?%(\.(\d+))?$') + if len(specs) < 5 + return a:str + endif + + let flushleft = specs[1] ==# '-' + let lpad = specs[2] ==# '0' ? '0' : ' ' + let minlen = str2nr(specs[3]) + let maxlen = str2nr(specs[4]) + let out = substitute(a:str, "\t", ' ', 'g') + + if maxlen && s:_width(out) > maxlen + let chars = filter(split(a:str, '\zs\ze', 1), 'v:val !=# ""') + let totlen = 0 + let out = '' + + if flushleft + for c in chars + if totlen + s:_width(c) < maxlen + let out .= c + let totlen += s:_width(c) + else + let out .= &encoding ==# 'utf-8' && &termencoding ==# 'utf-8' ? "\u2026" : '>' + break + endif + endfor + else + call reverse(chars) + for c in chars + if totlen + s:_width(c) < maxlen + let out = c . out + let totlen += s:_width(c) + else + let out = (&encoding ==# 'utf-8' && &termencoding ==# 'utf-8' ? "\u2026" : '<') . out + break + endif + endfor + endif + endif + + if minlen && s:_width(out) < minlen + if flushleft + let out .= repeat(' ', minlen - s:_width(out)) + else + let out = repeat(lpad, minlen - s:_width(out)) . out + endif + endif + + return out +endfunction " }}}2 + " }}}1 " Private functions {{{1 diff --git a/doc/syntastic.txt b/doc/syntastic.txt index e5d0a546..f859acbe 100644 --- a/doc/syntastic.txt +++ b/doc/syntastic.txt @@ -585,6 +585,13 @@ magic flags are available to insert information: %fw - line number of first warning %F - line number of first warning or error +These flags accept width and alignment controls similar to the ones used by +|'statusline'| flags: + %-0{minwid}.{maxwid}{flag} + +All fields except {flag} are optional. A single percent sign can be given as +"%%". + Several additional flags are available to hide text under certain conditions: %E{...} - hide the text in the brackets unless there are errors %W{...} - hide the text in the brackets unless there are warnings diff --git a/plugin/syntastic.vim b/plugin/syntastic.vim index f541d108..af3d8804 100644 --- a/plugin/syntastic.vim +++ b/plugin/syntastic.vim @@ -19,7 +19,7 @@ if has('reltime') lockvar! g:_SYNTASTIC_START endif -let g:_SYNTASTIC_VERSION = '3.6.0-170' +let g:_SYNTASTIC_VERSION = '3.6.0-171' lockvar g:_SYNTASTIC_VERSION " Sanity checks {{{1 diff --git a/plugin/syntastic/loclist.vim b/plugin/syntastic/loclist.vim index a89ec783..790c37f9 100644 --- a/plugin/syntastic/loclist.vim +++ b/plugin/syntastic/loclist.vim @@ -111,6 +111,7 @@ function! g:SyntasticLoclist.getStatuslineFlag() abort " {{{2 let output = substitute(output, '\m\C%B{\([^}]*\)}', (num_warnings && num_errors) ? '\1' : '' , 'g') let flags = { + \ '%': '%', \ 't': num_issues, \ 'e': num_errors, \ 'w': num_warnings, @@ -123,7 +124,7 @@ function! g:SyntasticLoclist.getStatuslineFlag() abort " {{{2 \ 'nw': (num_warnings ? fnamemodify( bufname(warnings[0]['bufnr']), ':t') : ''), \ 'pw': (num_warnings ? fnamemodify( bufname(warnings[0]['bufnr']), ':p:~:.') : ''), \ 'fw': (num_warnings ? warnings[0]['lnum'] : '') } - let output = substitute(output, '\v\C\%([npf][ew]|[NPFtew])', '\=flags[submatch(1)]', 'g') + let output = substitute(output, '\v\C\%(-?\d*%(\.\d+)?)([npf][ew]|[NPFtew%])', '\=syntastic#util#wformat(submatch(1), flags[submatch(2)])', 'g') let self._stl_flag = output else