diff options
Diffstat (limited to 'autoload/vital')
| -rw-r--r-- | autoload/vital/_crystal.vim | 313 | ||||
| -rw-r--r-- | autoload/vital/_crystal/ColorEcho.vim | 182 | ||||
| -rw-r--r-- | autoload/vital/_crystal/Data/List.vim | 446 | ||||
| -rw-r--r-- | autoload/vital/_crystal/Data/String.vim | 572 | ||||
| -rw-r--r-- | autoload/vital/_crystal/Prelude.vim | 389 | ||||
| -rw-r--r-- | autoload/vital/_crystal/Process.vim | 185 | ||||
| -rw-r--r-- | autoload/vital/_crystal/Web/JSON.vim | 112 | 
7 files changed, 2199 insertions, 0 deletions
| diff --git a/autoload/vital/_crystal.vim b/autoload/vital/_crystal.vim new file mode 100644 index 00000000..f5ff37f3 --- /dev/null +++ b/autoload/vital/_crystal.vim @@ -0,0 +1,313 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +let s:self_version = expand('<sfile>:t:r') +let s:self_file = expand('<sfile>') + +" Note: The extra argument to globpath() was added in Patch 7.2.051. +let s:globpath_third_arg = v:version > 702 || v:version == 702 && has('patch51') + +let s:loaded = {} +let s:cache_module_path = {} +let s:cache_sid = {} + +let s:_vital_files_cache_runtimepath = '' +let s:_vital_files_cache = [] +let s:_unify_path_cache = {} + +function! s:import(name, ...) abort +  let target = {} +  let functions = [] +  for a in a:000 +    if type(a) == type({}) +      let target = a +    elseif type(a) == type([]) +      let functions = a +    endif +    unlet a +  endfor +  let module = s:_import(a:name) +  if empty(functions) +    call extend(target, module, 'keep') +  else +    for f in functions +      if has_key(module, f) && !has_key(target, f) +        let target[f] = module[f] +      endif +    endfor +  endif +  return target +endfunction + +function! s:load(...) dict abort +  for arg in a:000 +    let [name; as] = type(arg) == type([]) ? arg[: 1] : [arg, arg] +    let target = split(join(as, ''), '\W\+') +    let dict = self +    let dict_type = type({}) +    while !empty(target) +      let ns = remove(target, 0) +      if !has_key(dict, ns) +        let dict[ns] = {} +      endif +      if type(dict[ns]) == dict_type +        let dict = dict[ns] +      else +        unlet dict +        break +      endif +    endwhile + +    if exists('dict') +      call extend(dict, s:_import(name)) +    endif +    unlet arg +  endfor +  return self +endfunction + +function! s:unload() abort +  let s:loaded = {} +  let s:cache_sid = {} +  let s:cache_module_path = {} +endfunction + +function! s:exists(name) abort +  return s:_get_module_path(a:name) !=# '' +endfunction + +function! s:search(pattern) abort +  let paths = s:_vital_files(a:pattern) +  let modules = sort(map(paths, 's:_file2module(v:val)')) +  return s:_uniq(modules) +endfunction + +function! s:expand_modules(entry, all) abort +  if type(a:entry) == type([]) +    let candidates = s:_concat(map(copy(a:entry), 's:search(v:val)')) +    if empty(candidates) +      throw printf('vital: Any of module %s is not found', string(a:entry)) +    endif +    if eval(join(map(copy(candidates), 'has_key(a:all, v:val)'), '+')) +      let modules = [] +    else +      let modules = [candidates[0]] +    endif +  else +    let modules = s:search(a:entry) +    if empty(modules) +      throw printf('vital: Module %s is not found', a:entry) +    endif +  endif +  call filter(modules, '!has_key(a:all, v:val)') +  for module in modules +    let a:all[module] = 1 +  endfor +  return modules +endfunction + +function! s:_import(name) abort +  if type(a:name) == type(0) +    return s:_build_module(a:name) +  endif +  let path = s:_get_module_path(a:name) +  if path ==# '' +    throw 'vital: module not found: ' . a:name +  endif +  let sid = s:_get_sid_by_script(path) +  if !sid +    try +      execute 'source' fnameescape(path) +    catch /^Vim\%((\a\+)\)\?:E484/ +      throw 'vital: module not found: ' . a:name +    catch /^Vim\%((\a\+)\)\?:E127/ +      " Ignore. +    endtry + +    let sid = s:_get_sid_by_script(path) +  endif +  return s:_build_module(sid) +endfunction + +function! s:_get_module_path(name) abort +  let key = a:name . '_' +  if has_key(s:cache_module_path, key) +    return s:cache_module_path[key] +  endif +  if s:_is_absolute_path(a:name) && filereadable(a:name) +    return a:name +  endif +  if a:name ==# '' +    let paths = [s:self_file] +  elseif a:name =~# '\v^\u\w*%(\.\u\w*)*$' +    let paths = s:_vital_files(a:name) +  else +    throw 'vital: Invalid module name: ' . a:name +  endif + +  call filter(paths, 'filereadable(expand(v:val, 1))') +  let path = get(paths, 0, '') +  let s:cache_module_path[key] = path +  return path +endfunction + +function! s:_get_sid_by_script(path) abort +  if has_key(s:cache_sid, a:path) +    return s:cache_sid[a:path] +  endif + +  let path = s:_unify_path(a:path) +  for line in filter(split(s:_redir('scriptnames'), "\n"), +  \                  'stridx(v:val, s:self_version) > 0') +    let list = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$') +    if !empty(list) && s:_unify_path(list[2]) ==# path +      let s:cache_sid[a:path] = list[1] - 0 +      return s:cache_sid[a:path] +    endif +  endfor +  return 0 +endfunction + +function! s:_file2module(file) abort +  let filename = fnamemodify(a:file, ':p:gs?[\\/]?/?') +  let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$') +  return join(split(tail, '[\\/]\+'), '.') +endfunction + +if filereadable(expand('<sfile>:r') . '.VIM') +  " resolve() is slow, so we cache results. +  " Note: On windows, vim can't expand path names from 8.3 formats. +  " So if getting full path via <sfile> and $HOME was set as 8.3 format, +  " vital load duplicated scripts. Below's :~ avoid this issue. +  function! s:_unify_path(path) abort +    if has_key(s:_unify_path_cache, a:path) +      return s:_unify_path_cache[a:path] +    endif +    let value = tolower(fnamemodify(resolve(fnamemodify( +    \                   a:path, ':p')), ':~:gs?[\\/]?/?')) +    let s:_unify_path_cache[a:path] = value +    return value +  endfunction +else +  function! s:_unify_path(path) abort +    return resolve(fnamemodify(a:path, ':p:gs?[\\/]?/?')) +  endfunction +endif + +if s:globpath_third_arg +  function! s:_runtime_files(path) abort +    return split(globpath(&runtimepath, a:path, 1), "\n") +  endfunction +else +  function! s:_runtime_files(path) abort +    return split(globpath(&runtimepath, a:path), "\n") +  endfunction +endif + +function! s:_vital_files(pattern) abort +  if s:_vital_files_cache_runtimepath !=# &runtimepath +    let path = printf('autoload/vital/%s/**/*.vim', s:self_version) +    let s:_vital_files_cache = s:_runtime_files(path) +    let mod = ':p:gs?[\\/]\+?/?' +    call map(s:_vital_files_cache, 'fnamemodify(v:val, mod)') +    let s:_vital_files_cache_runtimepath = &runtimepath +  endif +  let target = substitute(a:pattern, '\.', '/', 'g') +  let target = substitute(target, '\*', '[^/]*', 'g') +  let regexp = printf('autoload/vital/%s/%s.vim', s:self_version, target) +  return filter(copy(s:_vital_files_cache), 'v:val =~# regexp') +endfunction + +" Copy from System.Filepath +if has('win16') || has('win32') || has('win64') +  function! s:_is_absolute_path(path) abort +    return a:path =~? '^[a-z]:[/\\]' +  endfunction +else +  function! s:_is_absolute_path(path) abort +    return a:path[0] ==# '/' +  endfunction +endif + +function! s:_build_module(sid) abort +  if has_key(s:loaded, a:sid) +    return copy(s:loaded[a:sid]) +  endif +  let functions = s:_get_functions(a:sid) + +  let prefix = '<SNR>' . a:sid . '_' +  let module = {} +  for func in functions +    let module[func] = function(prefix . func) +  endfor +  if has_key(module, '_vital_created') +    call module._vital_created(module) +  endif +  let export_module = filter(copy(module), 'v:key =~# "^\\a"') +  let s:loaded[a:sid] = get(g:, 'vital_debug', 0) ? module : export_module +  if has_key(module, '_vital_loaded') +    let V = vital#{s:self_version}#new() +    call module._vital_loaded(V) +  endif +  return copy(s:loaded[a:sid]) +endfunction + +if exists('+regexpengine') +  function! s:_get_functions(sid) abort +    let funcs = s:_redir(printf("function /\\%%#=2^\<SNR>%d_", a:sid)) +    let map_pat = '<SNR>' . a:sid . '_\zs\w\+' +    return map(split(funcs, "\n"), 'matchstr(v:val, map_pat)') +  endfunction +else +  function! s:_get_functions(sid) abort +    let prefix = '<SNR>' . a:sid . '_' +    let funcs = s:_redir('function') +    let filter_pat = '^\s*function ' . prefix +    let map_pat = prefix . '\zs\w\+' +    return map(filter(split(funcs, "\n"), +    \          'stridx(v:val, prefix) > 0 && v:val =~# filter_pat'), +    \          'matchstr(v:val, map_pat)') +  endfunction +endif + +if exists('*uniq') +  function! s:_uniq(list) abort +    return uniq(a:list) +  endfunction +else +  function! s:_uniq(list) abort +    let i = len(a:list) - 1 +    while 0 < i +      if a:list[i] ==# a:list[i - 1] +        call remove(a:list, i) +        let i -= 2 +      else +        let i -= 1 +      endif +    endwhile +    return a:list +  endfunction +endif + +function! s:_concat(lists) abort +  let result_list = [] +  for list in a:lists +    let result_list += list +  endfor +  return result_list +endfunction + +function! s:_redir(cmd) abort +  let [save_verbose, save_verbosefile] = [&verbose, &verbosefile] +  set verbose=0 verbosefile= +  redir => res +    silent! execute a:cmd +  redir END +  let [&verbose, &verbosefile] = [save_verbose, save_verbosefile] +  return res +endfunction + +function! vital#{s:self_version}#new() abort +  return s:_import('') +endfunction + +endif diff --git a/autoload/vital/_crystal/ColorEcho.vim b/autoload/vital/_crystal/ColorEcho.vim new file mode 100644 index 00000000..f2eae658 --- /dev/null +++ b/autoload/vital/_crystal/ColorEcho.vim @@ -0,0 +1,182 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +scriptencoding utf-8 +let s:save_cpo = &cpo +set cpo&vim + +function! s:_is_available() abort +    if has('gui_running') +        return 1 +    endif + +    if has('win32') || has('win64') +        return 0 +    endif + +    return exists('&t_Co') && (&t_Co == 8 || &t_Co == 256) +endfunction + +function! s:is_available() abort +    if exists('s:is_available_cache') +        return s:is_available_cache +    endif +    let s:is_available_cache = s:_is_available() +    return s:is_available_cache +endfunction + +function! s:_define_ansi_highlights() abort +    hi ansiNone cterm=NONE gui=NONE + +    hi ansiBlackBg ctermbg=black guibg=black cterm=none gui=none +    hi ansiRedBg ctermbg=red guibg=red cterm=none gui=none +    hi ansiGreenBg ctermbg=green guibg=green cterm=none gui=none +    hi ansiYellowBg ctermbg=yellow guibg=yellow cterm=none gui=none +    hi ansiBlueBg ctermbg=blue guibg=blue cterm=none gui=none +    hi ansiMagentaBg ctermbg=magenta guibg=magenta cterm=none gui=none +    hi ansiCyanBg ctermbg=cyan guibg=cyan cterm=none gui=none +    hi ansiWhiteBg ctermbg=white guibg=white cterm=none gui=none +    hi ansiGrayBg ctermbg=gray guibg=gray cterm=none gui=none + +    hi ansiBlackFg ctermfg=black guifg=black cterm=none gui=none +    hi ansiRedFg ctermfg=red guifg=red cterm=none gui=none +    hi ansiGreenFg ctermfg=green guifg=green cterm=none gui=none +    hi ansiYellowFg ctermfg=yellow guifg=yellow cterm=none gui=none +    hi ansiBlueFg ctermfg=blue guifg=blue cterm=none gui=none +    hi ansiMagentaFg ctermfg=magenta guifg=magenta cterm=none gui=none +    hi ansiCyanFg ctermfg=cyan guifg=cyan cterm=none gui=none +    hi ansiWhiteFg ctermfg=white guifg=white cterm=none gui=none +    hi ansiGrayFg ctermfg=gray guifg=gray cterm=none gui=none + +    hi ansiBoldBlackFg ctermfg=black guifg=black cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldRedFg ctermfg=red guifg=red cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldGreenFg ctermfg=green guifg=green cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldYellowFg ctermfg=yellow guifg=yellow cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldBlueFg ctermfg=blue guifg=blue cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldMagentaFg ctermfg=magenta guifg=magenta cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldCyanFg ctermfg=cyan guifg=cyan cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldWhiteFg ctermfg=white guifg=white cterm=none gui=none cterm=bold gui=bold +    hi ansiBoldGrayFg ctermfg=gray guifg=gray cterm=none gui=none cterm=bold gui=bold + +    hi ansiUnderlineBlackFg ctermfg=black guifg=black cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineRedFg ctermfg=red guifg=red cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineGreenFg ctermfg=green guifg=green cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineYellowFg ctermfg=yellow guifg=yellow cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineBlueFg ctermfg=blue guifg=blue cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineMagentaFg ctermfg=magenta guifg=magenta cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineCyanFg ctermfg=cyan guifg=cyan cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineWhiteFg ctermfg=white guifg=white cterm=none gui=none cterm=underline gui=underline +    hi ansiUnderlineGrayFg ctermfg=gray guifg=gray cterm=none gui=none cterm=underline gui=underline + +endfunction + +let s:echorizer = { +        \   'value': '', +        \   'attr': '', +        \ } + +function s:echorizer.eat() abort +    let matched = match(self.value, '\e\[\d*;\=m') +    if matched == -1 +        return {} +    endif + +    let matched_end = matchend(self.value, '\e\[\d*;\=m') + +    let token = { +        \   'body': matched == 0 ? '' : self.value[ : matched-1], +        \   'code': matchstr(self.value[matched : matched_end-1], '\d\+') +        \ } + +    let self.value = self.value[matched_end : ] + +    return token +endfunction + +let s:COLORS = { +    \   0: "None", +    \   30: "BlackFg", +    \   31: "RedFg", +    \   32: "GreenFg", +    \   33: "YellowFg", +    \   34: "BlueFg", +    \   35: "MagentaFg", +    \   36: "CyanFg", +    \   37: "WhiteFg", +    \   40: "BlackBg", +    \   41: "RedBg", +    \   42: "GreenBg", +    \   43: "YellowBg", +    \   44: "BlueBg", +    \   45: "MagentaBg", +    \   46: "CyanBg", +    \   47: "WhiteBg", +    \   90: "GrayFg", +    \ } + +function s:echorizer.echo_ansi(code) abort +    if !has_key(s:COLORS, a:code) +        return +    endif + +    execute 'echohl' 'ansi' . self.attr . s:COLORS[a:code] + +    if a:code == 0 +        let self.attr = '' +    endif +endfunction + +function s:echorizer.echo() abort +    echo + +    while 1 +        let token = self.eat() +        if token == {} +            break +        endif + +        if token.body !=# '' +            echon token.body +        endif + +        " TODO: Now only one attribute can be specified +        if token.code == 1 +            let self.attr = 'Bold' +        elseif token.code == 4 +            let self.attr = 'Underline' +        elseif token.code ==# '' +            call self.echo_ansi(0) +        else +            call self.echo_ansi(token.code) +        endif +    endwhile + +    echon self.value +    echohl None +    let self.value = '' +endfunction + +function! s:get_echorizer(str) abort +    let e = deepcopy(s:echorizer) +    let e.value = a:str +    return e +endfunction + +function! s:echo(str) abort +    if !s:is_available() +        echo substitute(a:str, '\e[.*m', '', 'g') +        return +    endif + +    if !exists('g:__vital_color_echo_already_highlighted') +        call s:_define_ansi_highlights() +        let g:__vital_color_echo_already_highlighted = 1 +    endif + +    let echorizer = s:get_echorizer(a:str) +    call echorizer.echo() +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo + +endif diff --git a/autoload/vital/_crystal/Data/List.vim b/autoload/vital/_crystal/Data/List.vim new file mode 100644 index 00000000..55703b3d --- /dev/null +++ b/autoload/vital/_crystal/Data/List.vim @@ -0,0 +1,446 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +" Utilities for list. + +let s:save_cpo = &cpo +set cpo&vim + +function! s:pop(list) abort +  return remove(a:list, -1) +endfunction + +function! s:push(list, val) abort +  call add(a:list, a:val) +  return a:list +endfunction + +function! s:shift(list) abort +  return remove(a:list, 0) +endfunction + +function! s:unshift(list, val) abort +  return insert(a:list, a:val) +endfunction + +function! s:cons(x, xs) abort +  return [a:x] + a:xs +endfunction + +function! s:conj(xs, x) abort +  return a:xs + [a:x] +endfunction + +" Removes duplicates from a list. +function! s:uniq(list) abort +  return s:uniq_by(a:list, 'v:val') +endfunction + +" Removes duplicates from a list. +function! s:uniq_by(list, f) abort +  let list = map(copy(a:list), printf('[v:val, %s]', a:f)) +  let i = 0 +  let seen = {} +  while i < len(list) +    let key = string(list[i][1]) +    if has_key(seen, key) +      call remove(list, i) +    else +      let seen[key] = 1 +      let i += 1 +    endif +  endwhile +  return map(list, 'v:val[0]') +endfunction + +function! s:clear(list) abort +  if !empty(a:list) +    unlet! a:list[0 : len(a:list) - 1] +  endif +  return a:list +endfunction + +" Concatenates a list of lists. +" XXX: Should we verify the input? +function! s:concat(list) abort +  let memo = [] +  for Value in a:list +    let memo += Value +  endfor +  return memo +endfunction + +" Take each elements from lists to a new list. +function! s:flatten(list, ...) abort +  let limit = a:0 > 0 ? a:1 : -1 +  let memo = [] +  if limit == 0 +    return a:list +  endif +  let limit -= 1 +  for Value in a:list +    let memo += +          \ type(Value) == type([]) ? +          \   s:flatten(Value, limit) : +          \   [Value] +    unlet! Value +  endfor +  return memo +endfunction + +" Sorts a list with expression to compare each two values. +" a:a and a:b can be used in {expr}. +function! s:sort(list, expr) abort +  if type(a:expr) == type(function('function')) +    return sort(a:list, a:expr) +  endif +  let s:expr = a:expr +  return sort(a:list, 's:_compare') +endfunction + +function! s:_compare(a, b) abort +  return eval(s:expr) +endfunction + +" Sorts a list using a set of keys generated by mapping the values in the list +" through the given expr. +" v:val is used in {expr} +function! s:sort_by(list, expr) abort +  let pairs = map(a:list, printf('[v:val, %s]', a:expr)) +  return map(s:sort(pairs, +  \      'a:a[1] ==# a:b[1] ? 0 : a:a[1] ># a:b[1] ? 1 : -1'), 'v:val[0]') +endfunction + +" Returns a maximum value in {list} through given {expr}. +" Returns 0 if {list} is empty. +" v:val is used in {expr} +function! s:max_by(list, expr) abort +  if empty(a:list) +    return 0 +  endif +  let list = map(copy(a:list), a:expr) +  return a:list[index(list, max(list))] +endfunction + +" Returns a minimum value in {list} through given {expr}. +" Returns 0 if {list} is empty. +" v:val is used in {expr} +" FIXME: -0x80000000 == 0x80000000 +function! s:min_by(list, expr) abort +  return s:max_by(a:list, '-(' . a:expr . ')') +endfunction + +" Returns List of character sequence between [a:from, a:to] +" e.g.: s:char_range('a', 'c') returns ['a', 'b', 'c'] +function! s:char_range(from, to) abort +  return map( +  \   range(char2nr(a:from), char2nr(a:to)), +  \   'nr2char(v:val)' +  \) +endfunction + +" Returns true if a:list has a:value. +" Returns false otherwise. +function! s:has(list, value) abort +  return index(a:list, a:value) isnot -1 +endfunction + +" Returns true if a:list[a:index] exists. +" Returns false otherwise. +" NOTE: Returns false when a:index is negative number. +function! s:has_index(list, index) abort +  " Return true when negative index? +  " let index = a:index >= 0 ? a:index : len(a:list) + a:index +  return 0 <= a:index && a:index < len(a:list) +endfunction + +" similar to Haskell's Data.List.span +function! s:span(f, xs) abort +  let border = len(a:xs) +  for i in range(len(a:xs)) +    if !eval(substitute(a:f, 'v:val', string(a:xs[i]), 'g')) +      let border = i +      break +    endif +  endfor +  return border == 0 ? [[], copy(a:xs)] : [a:xs[: border - 1], a:xs[border :]] +endfunction + +" similar to Haskell's Data.List.break +function! s:break(f, xs) abort +  return s:span(printf('!(%s)', a:f), a:xs) +endfunction + +" similar to Haskell's Data.List.takeWhile +function! s:take_while(f, xs) abort +  return s:span(a:f, a:xs)[0] +endfunction + +" similar to Haskell's Data.List.partition +function! s:partition(f, xs) abort +  return [filter(copy(a:xs), a:f), filter(copy(a:xs), '!(' . a:f . ')')] +endfunction + +" similar to Haskell's Prelude.all +function! s:all(f, xs) abort +  return !s:any(printf('!(%s)', a:f), a:xs) +endfunction + +" similar to Haskell's Prelude.any +function! s:any(f, xs) abort +  return !empty(filter(map(copy(a:xs), a:f), 'v:val')) +endfunction + +" similar to Haskell's Prelude.and +function! s:and(xs) abort +  return s:all('v:val', a:xs) +endfunction + +" similar to Haskell's Prelude.or +function! s:or(xs) abort +  return s:any('v:val', a:xs) +endfunction + +function! s:map_accum(expr, xs, init) abort +  let memo = [] +  let init = a:init +  for x in a:xs +    let expr = substitute(a:expr, 'v:memo', init, 'g') +    let expr = substitute(expr, 'v:val', x, 'g') +    let [tmp, init] = eval(expr) +    call add(memo, tmp) +  endfor +  return memo +endfunction + +" similar to Haskell's Prelude.foldl +function! s:foldl(f, init, xs) abort +  let memo = a:init +  for x in a:xs +    let expr = substitute(a:f, 'v:val', string(x), 'g') +    let expr = substitute(expr, 'v:memo', string(memo), 'g') +    unlet memo +    let memo = eval(expr) +  endfor +  return memo +endfunction + +" similar to Haskell's Prelude.foldl1 +function! s:foldl1(f, xs) abort +  if len(a:xs) == 0 +    throw 'vital: Data.List: foldl1' +  endif +  return s:foldl(a:f, a:xs[0], a:xs[1:]) +endfunction + +" similar to Haskell's Prelude.foldr +function! s:foldr(f, init, xs) abort +  return s:foldl(a:f, a:init, reverse(copy(a:xs))) +endfunction + +" similar to Haskell's Prelude.fold11 +function! s:foldr1(f, xs) abort +  if len(a:xs) == 0 +    throw 'vital: Data.List: foldr1' +  endif +  return s:foldr(a:f, a:xs[-1], a:xs[0:-2]) +endfunction + +" similar to python's zip() +function! s:zip(...) abort +  return map(range(min(map(copy(a:000), 'len(v:val)'))), "map(copy(a:000), 'v:val['.v:val.']')") +endfunction + +" similar to zip(), but goes until the longer one. +function! s:zip_fill(xs, ys, filler) abort +  if empty(a:xs) && empty(a:ys) +    return [] +  elseif empty(a:ys) +    return s:cons([a:xs[0], a:filler], s:zip_fill(a:xs[1 :], [], a:filler)) +  elseif empty(a:xs) +    return s:cons([a:filler, a:ys[0]], s:zip_fill([], a:ys[1 :], a:filler)) +  else +    return s:cons([a:xs[0], a:ys[0]], s:zip_fill(a:xs[1 :], a:ys[1: ], a:filler)) +  endif +endfunction + +" Inspired by Ruby's with_index method. +function! s:with_index(list, ...) abort +  let base = a:0 > 0 ? a:1 : 0 +  return map(copy(a:list), '[v:val, v:key + base]') +endfunction + +" similar to Ruby's detect or Haskell's find. +function! s:find(list, default, f) abort +  for x in a:list +    if eval(substitute(a:f, 'v:val', string(x), 'g')) +      return x +    endif +  endfor +  return a:default +endfunction + +" Returns the index of the first element which satisfies the given expr. +function! s:find_index(xs, f, ...) abort +  let len = len(a:xs) +  let start = a:0 > 0 ? (a:1 < 0 ? len + a:1 : a:1) : 0 +  let default = a:0 > 1 ? a:2 : -1 +  if start >=# len || start < 0 +    return default +  endif +  for i in range(start, len - 1) +    if eval(substitute(a:f, 'v:val', string(a:xs[i]), 'g')) +      return i +    endif +  endfor +  return default +endfunction + +" Returns the index of the last element which satisfies the given expr. +function! s:find_last_index(xs, f, ...) abort +  let len = len(a:xs) +  let start = a:0 > 0 ? (a:1 < 0 ? len + a:1 : a:1) : len - 1 +  let default = a:0 > 1 ? a:2 : -1 +  if start >=# len || start < 0 +    return default +  endif +  for i in range(start, 0, -1) +    if eval(substitute(a:f, 'v:val', string(a:xs[i]), 'g')) +      return i +    endif +  endfor +  return default +endfunction + +" Similar to find_index but returns the list of indices satisfying the given expr. +function! s:find_indices(xs, f, ...) abort +  let len = len(a:xs) +  let start = a:0 > 0 ? (a:1 < 0 ? len + a:1 : a:1) : 0 +  let result = [] +  if start >=# len || start < 0 +    return result +  endif +  for i in range(start, len - 1) +    if eval(substitute(a:f, 'v:val', string(a:xs[i]), 'g')) +      call add(result, i) +    endif +  endfor +  return result +endfunction + +" Return non-zero if a:list1 and a:list2 have any common item(s). +" Return zero otherwise. +function! s:has_common_items(list1, list2) abort +  return !empty(filter(copy(a:list1), 'index(a:list2, v:val) isnot -1')) +endfunction + +function! s:intersect(list1, list2) abort +  let items = [] +  " for funcref +  for X in a:list1 +    if index(a:list2, X) != -1 && index(items, X) == -1 +      let items += [X] +    endif +  endfor +  return items +endfunction + +" similar to Ruby's group_by. +function! s:group_by(xs, f) abort +  let result = {} +  let list = map(copy(a:xs), printf('[v:val, %s]', a:f)) +  for x in list +    let Val = x[0] +    let key = type(x[1]) !=# type('') ? string(x[1]) : x[1] +    if has_key(result, key) +      call add(result[key], Val) +    else +      let result[key] = [Val] +    endif +    unlet Val +  endfor +  return result +endfunction + +function! s:_default_compare(a, b) abort +  return a:a <# a:b ? -1 : a:a ># a:b ? 1 : 0 +endfunction + +function! s:binary_search(list, value, ...) abort +  let Predicate = a:0 >= 1 ? a:1 : 's:_default_compare' +  let dic = a:0 >= 2 ? a:2 : {} +  let start = 0 +  let end = len(a:list) - 1 + +  while 1 +    if start > end +      return -1 +    endif + +    let middle = (start + end) / 2 + +    let compared = call(Predicate, [a:value, a:list[middle]], dic) + +    if compared < 0 +      let end = middle - 1 +    elseif compared > 0 +      let start = middle + 1 +    else +      return middle +    endif +  endwhile +endfunction + +function! s:product(lists) abort +  let result = [[]] +  for pool in a:lists +    let tmp = [] +    for x in result +      let tmp += map(copy(pool), 'x + [v:val]') +    endfor +    let result = tmp +  endfor +  return result +endfunction + +function! s:permutations(list, ...) abort +  if a:0 > 1 +    throw 'vital: Data.List: too many arguments' +  endif +  let r = a:0 == 1 ? a:1 : len(a:list) +  if r > len(a:list) +    return [] +  elseif r < 0 +    throw 'vital: Data.List: {r} must be non-negative integer' +  endif +  let n = len(a:list) +  let result = [] +  for indices in s:product(map(range(r), 'range(n)')) +    if len(s:uniq(indices)) == r +      call add(result, map(indices, 'a:list[v:val]')) +    endif +  endfor +  return result +endfunction + +function! s:combinations(list, r) abort +  if a:r > len(a:list) +    return [] +  elseif a:r < 0 +    throw 'vital: Data:List: {r} must be non-negative integer' +  endif +  let n = len(a:list) +  let result = [] +  for indices in s:permutations(range(n), a:r) +    if s:sort(copy(indices), 'a:a - a:b') == indices +      call add(result, map(indices, 'a:list[v:val]')) +    endif +  endfor +  return result +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim:set et ts=2 sts=2 sw=2 tw=0: + +endif diff --git a/autoload/vital/_crystal/Data/String.vim b/autoload/vital/_crystal/Data/String.vim new file mode 100644 index 00000000..c2e2382c --- /dev/null +++ b/autoload/vital/_crystal/Data/String.vim @@ -0,0 +1,572 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +" Utilities for string. + +let s:save_cpo = &cpo +set cpo&vim + +function! s:_vital_loaded(V) abort +  let s:V = a:V +  let s:P = s:V.import('Prelude') +  let s:L = s:V.import('Data.List') +endfunction + +function! s:_vital_depends() abort +  return ['Prelude', 'Data.List'] +endfunction + +" Substitute a:from => a:to by string. +" To substitute by pattern, use substitute() instead. +function! s:replace(str, from, to) abort +  return s:_replace(a:str, a:from, a:to, 'g') +endfunction + +" Substitute a:from => a:to only once. +" cf. s:replace() +function! s:replace_first(str, from, to) abort +  return s:_replace(a:str, a:from, a:to, '') +endfunction + +" implement of replace() and replace_first() +function! s:_replace(str, from, to, flags) abort +  return substitute(a:str, '\V'.escape(a:from, '\'), escape(a:to, '\'), a:flags) +endfunction + +function! s:scan(str, pattern) abort +  let list = [] +  call substitute(a:str, a:pattern, '\=add(list, submatch(0)) == [] ? "" : ""', 'g') +  return list +endfunction + +function! s:reverse(str) abort +  return join(reverse(split(a:str, '.\zs')), '') +endfunction + +function! s:starts_with(str, prefix) abort +  return stridx(a:str, a:prefix) == 0 +endfunction + +function! s:ends_with(str, suffix) abort +  let idx = strridx(a:str, a:suffix) +  return 0 <= idx && idx + len(a:suffix) == len(a:str) +endfunction + +function! s:common_head(strs) abort +  if empty(a:strs) +    return '' +  endif +  let len = len(a:strs) +  if len == 1 +    return a:strs[0] +  endif +  let strs = len == 2 ? a:strs : sort(copy(a:strs)) +  let pat = substitute(strs[0], '.', '\="[" . escape(submatch(0), "^\\") . "]"', 'g') +  return pat == '' ? '' : matchstr(strs[-1], '\C^\%[' . pat . ']') +endfunction + +" Split to two elements of List. ([left, right]) +" e.g.: s:split3('neocomplcache', 'compl') returns ['neo', 'compl', 'cache'] +function! s:split_leftright(expr, pattern) abort +  let [left, _, right] = s:split3(a:expr, a:pattern) +  return [left, right] +endfunction + +function! s:split3(expr, pattern) abort +  let ERROR = ['', '', ''] +  if a:expr ==# '' || a:pattern ==# '' +    return ERROR +  endif +  let begin = match(a:expr, a:pattern) +  if begin is -1 +    return ERROR +  endif +  let end   = matchend(a:expr, a:pattern) +  let left  = begin <=# 0 ? '' : a:expr[: begin - 1] +  let right = a:expr[end :] +  return [left, a:expr[begin : end-1], right] +endfunction + +" Slices into strings determines the number of substrings. +" e.g.: s:nsplit("neo compl cache", 2, '\s') returns ['neo', 'compl cache'] +function! s:nsplit(expr, n, ...) abort +  let pattern = get(a:000, 0, '\s') +  let keepempty = get(a:000, 1, 1) +  let ret = [] +  let expr = a:expr +  if a:n <= 1 +    return [expr] +  endif +  while 1 +    let pos = match(expr, pattern) +    if pos == -1 +      if expr !~ pattern || keepempty +        call add(ret, expr) +      endif +      break +    elseif pos >= 0 +      let left = pos > 0 ? expr[:pos-1] : '' +      if pos > 0 || keepempty +        call add(ret, left) +      endif +      let ml = len(matchstr(expr, pattern)) +      if pos == 0 && ml == 0 +        let pos = 1 +      endif +      let expr = expr[pos+ml :] +    endif +    if len(expr) == 0 +      break +    endif +    if len(ret) == a:n - 1 +      call add(ret, expr) +      break +    endif +  endwhile +  return ret +endfunction + +" Returns the number of character in a:str. +" NOTE: This returns proper value +" even if a:str contains multibyte character(s). +" s:strchars(str) {{{ +if exists('*strchars') +  function! s:strchars(str) abort +    return strchars(a:str) +  endfunction +else +  function! s:strchars(str) abort +    return strlen(substitute(copy(a:str), '.', 'x', 'g')) +  endfunction +endif "}}} + +" Returns the bool of contains any multibyte character in s:str +function! s:contains_multibyte(str) abort "{{{ +  return strlen(a:str) != s:strchars(a:str) +endfunction "}}} + +" Remove last character from a:str. +" NOTE: This returns proper value +" even if a:str contains multibyte character(s). +function! s:chop(str) abort "{{{ +  return substitute(a:str, '.$', '', '') +endfunction "}}} + +" Remove last \r,\n,\r\n from a:str. +function! s:chomp(str) abort "{{{ +  return substitute(a:str, '\%(\r\n\|[\r\n]\)$', '', '') +endfunction "}}} + +" wrap() and its internal functions +" * _split_by_wcswidth_once() +" * _split_by_wcswidth() +" * _concat() +" * wrap() +" +" NOTE _concat() is just a copy of Data.List.concat(). +" FIXME don't repeat yourself +function! s:_split_by_wcswidth_once(body, x) abort +  let fst = s:strwidthpart(a:body, a:x) +  let snd = s:strwidthpart_reverse(a:body, s:wcswidth(a:body) - s:wcswidth(fst)) +  return [fst, snd] +endfunction + +function! s:_split_by_wcswidth(body, x) abort +  let memo = [] +  let body = a:body +  while s:wcswidth(body) > a:x +    let [tmp, body] = s:_split_by_wcswidth_once(body, a:x) +    call add(memo, tmp) +  endwhile +  call add(memo, body) +  return memo +endfunction + +function! s:trim(str) abort +  return matchstr(a:str,'^\s*\zs.\{-}\ze\s*$') +endfunction + +function! s:trim_start(str) abort +  return matchstr(a:str,'^\s*\zs.\{-}$') +endfunction + +function! s:trim_end(str) abort +  return matchstr(a:str,'^.\{-}\ze\s*$') +endfunction + +function! s:wrap(str,...) abort +  let _columns = a:0 > 0 ? a:1 : &columns +  return s:L.concat( +        \ map(split(a:str, '\r\n\|[\r\n]'), 's:_split_by_wcswidth(v:val, _columns - 1)')) +endfunction + +function! s:nr2byte(nr) abort +  if a:nr < 0x80 +    return nr2char(a:nr) +  elseif a:nr < 0x800 +    return nr2char(a:nr/64+192).nr2char(a:nr%64+128) +  else +    return nr2char(a:nr/4096%16+224).nr2char(a:nr/64%64+128).nr2char(a:nr%64+128) +  endif +endfunction + +function! s:nr2enc_char(charcode) abort +  if &encoding == 'utf-8' +    return nr2char(a:charcode) +  endif +  let char = s:nr2byte(a:charcode) +  if strlen(char) > 1 +    let char = strtrans(iconv(char, 'utf-8', &encoding)) +  endif +  return char +endfunction + +function! s:nr2hex(nr) abort +  let n = a:nr +  let r = "" +  while n +    let r = '0123456789ABCDEF'[n % 16] . r +    let n = n / 16 +  endwhile +  return r +endfunction + +" If a ==# b, returns -1. +" If a !=# b, returns first index of different character. +function! s:diffidx(a, b) abort +  return a:a ==# a:b ? -1 : strlen(s:common_head([a:a, a:b])) +endfunction + +function! s:substitute_last(expr, pat, sub) abort +  return substitute(a:expr, printf('.*\zs%s', a:pat), a:sub, '') +endfunction + +function! s:dstring(expr) abort +  let x = substitute(string(a:expr), "^'\\|'$", '', 'g') +  let x = substitute(x, "''", "'", 'g') +  return printf('"%s"', escape(x, '"')) +endfunction + +function! s:lines(str) abort +  return split(a:str, '\r\?\n') +endfunction + +function! s:_pad_with_char(str, left, right, char) abort +  return repeat(a:char, a:left). a:str. repeat(a:char, a:right) +endfunction + +function! s:pad_left(str, width, ...) abort +  let char = get(a:, 1, ' ') +  if strdisplaywidth(char) != 1 +    throw "vital: Data.String: Can't use non-half-width characters for padding." +  endif +  let left = max([0, a:width - strdisplaywidth(a:str)]) +  return s:_pad_with_char(a:str, left, 0, char) +endfunction + +function! s:pad_right(str, width, ...) abort +  let char = get(a:, 1, ' ') +  if strdisplaywidth(char) != 1 +    throw "vital: Data.String: Can't use non-half-width characters for padding." +  endif +  let right = max([0, a:width - strdisplaywidth(a:str)]) +  return s:_pad_with_char(a:str, 0, right, char) +endfunction + +function! s:pad_both_sides(str, width, ...) abort +  let char = get(a:, 1, ' ') +  if strdisplaywidth(char) != 1 +    throw "vital: Data.String: Can't use non-half-width characters for padding." +  endif +  let space = max([0, a:width - strdisplaywidth(a:str)]) +  let left = space / 2 +  let right = space - left +  return s:_pad_with_char(a:str, left, right, char) +endfunction + +function! s:pad_between_letters(str, width, ...) abort +  let char = get(a:, 1, ' ') +  if strdisplaywidth(char) != 1 +    throw "vital: Data.String: Can't use non-half-width characters for padding." +  endif +  let letters = split(a:str, '\zs') +  let each_width = a:width / len(letters) +  let str = join(map(letters, 's:pad_both_sides(v:val, each_width, char)'), '') +  if a:width - strdisplaywidth(str) > 0 +    return char. s:pad_both_sides(str, a:width - 1, char) +  endif +  return str +endfunction + +function! s:justify_equal_spacing(str, width, ...) abort +  let char = get(a:, 1, ' ') +  if strdisplaywidth(char) != 1 +    throw "vital: Data.String: Can't use non-half-width characters for padding." +  endif +  let letters = split(a:str, '\zs') +  let first_letter = letters[0] +  " {width w/o the first letter} / {length w/o the first letter} +  let each_width = (a:width - strdisplaywidth(first_letter)) / (len(letters) - 1) +  let remainder = (a:width - strdisplaywidth(first_letter)) % (len(letters) - 1) +  return first_letter. join(s:L.concat([ +\     map(letters[1:remainder], 's:pad_left(v:val, each_width + 1, char)'), +\     map(letters[remainder + 1:], 's:pad_left(v:val, each_width, char)') +\   ]), '') +endfunction + +function! s:levenshtein_distance(str1, str2) abort +  let letters1 = split(a:str1, '\zs') +  let letters2 = split(a:str2, '\zs') +  let length1 = len(letters1) +  let length2 = len(letters2) +  let distances = map(range(1, length1 + 1), 'map(range(1, length2 + 1), "0")') + +  for i1 in range(0, length1) +    let distances[i1][0] = i1 +  endfor +  for i2 in range(0, length2) +    let distances[0][i2] = i2 +  endfor + +  for i1 in range(1, length1) +    for i2 in range(1, length2) +      let cost = (letters1[i1 - 1] ==# letters2[i2 - 1]) ? 0 : 1 + +      let distances[i1][i2] = min([ +      \ distances[i1 - 1][i2    ] + 1, +      \ distances[i1    ][i2 - 1] + 1, +      \ distances[i1 - 1][i2 - 1] + cost, +      \]) +    endfor +  endfor + +  return distances[length1][length2] +endfunction + +function! s:padding_by_displaywidth(expr, width, float) abort +  let padding_char = ' ' +  let n = a:width - strdisplaywidth(a:expr) +  if n <= 0 +    let n = 0 +  endif +  if a:float < 0 +    return a:expr . repeat(padding_char, n) +  elseif 0 < a:float +    return repeat(padding_char, n) . a:expr +  else +    if n % 2 is 0 +      return repeat(padding_char, n / 2) . a:expr . repeat(padding_char, n / 2) +    else +      return repeat(padding_char, (n - 1) / 2) . a:expr . repeat(padding_char, (n - 1) / 2) . padding_char +    endif +  endif +endfunction + +function! s:split_by_displaywidth(expr, width, float, is_wrap) abort +  if a:width is 0 +    return [''] +  endif + +  let lines = [] + +  let cs = split(a:expr, '\zs') +  let cs_index = 0 + +  let text = '' +  while cs_index < len(cs) +    if cs[cs_index] is "\n" +      let text = s:padding_by_displaywidth(text, a:width, a:float) +      let lines += [text] +      let text = '' +    else +      let w = strdisplaywidth(text . cs[cs_index]) + +      if w < a:width +        let text .= cs[cs_index] +      elseif a:width < w +        let text = s:padding_by_displaywidth(text, a:width, a:float) +      else +        let text .= cs[cs_index] +      endif + +      if a:width <= w +        let lines += [text] +        let text = '' +        if a:is_wrap +          if a:width < w +            if a:width < strdisplaywidth(cs[cs_index]) +              while get(cs, cs_index, "\n") isnot "\n" +                let cs_index += 1 +              endwhile +              continue +            else +              let text = cs[cs_index] +            endif +          endif +        else +          while get(cs, cs_index, "\n") isnot "\n" +            let cs_index += 1 +          endwhile +          continue +        endif +      endif + +    endif +    let cs_index += 1 +  endwhile + +  if !empty(text) +    let lines += [ s:padding_by_displaywidth(text, a:width, a:float) ] +  endif + +  return lines +endfunction + +function! s:hash(str) abort +  if exists('*sha256') +    return sha256(a:str) +  else +    " This gives up sha256ing but just adds up char with index. +    let sum = 0 +    for i in range(len(a:str)) +      let sum += char2nr(a:str[i]) * (i + 1) +    endfor + +    return printf('%x', sum) +  endif +endfunction + +function! s:truncate(str, width) abort +  " Original function is from mattn. +  " http://github.com/mattn/googlereader-vim/tree/master + +  if a:str =~# '^[\x00-\x7f]*$' +    return len(a:str) < a:width ? +          \ printf('%-'.a:width.'s', a:str) : strpart(a:str, 0, a:width) +  endif + +  let ret = a:str +  let width = s:wcswidth(a:str) +  if width > a:width +    let ret = s:strwidthpart(ret, a:width) +    let width = s:wcswidth(ret) +  endif + +  if width < a:width +    let ret .= repeat(' ', a:width - width) +  endif + +  return ret +endfunction + +function! s:truncate_skipping(str, max, footer_width, separator) abort +  let width = s:wcswidth(a:str) +  if width <= a:max +    let ret = a:str +  else +    let header_width = a:max - s:wcswidth(a:separator) - a:footer_width +    let ret = s:strwidthpart(a:str, header_width) . a:separator +          \ . s:strwidthpart_reverse(a:str, a:footer_width) +  endif +  return s:truncate(ret, a:max) +endfunction + +function! s:strwidthpart(str, width) abort +  if a:width <= 0 +    return '' +  endif +  let strarr = split(a:str, '\zs') +  let width = s:wcswidth(a:str) +  let index = len(strarr) +  let diff = (index + 1) / 2 +  let rightindex = index - 1 +  while width > a:width +    let index = max([rightindex - diff + 1, 0]) +    let partwidth = s:wcswidth(join(strarr[(index):(rightindex)], '')) +    if width - partwidth >= a:width || diff <= 1 +      let width -= partwidth +      let rightindex = index - 1 +    endif +    if diff > 1 +      let diff = diff / 2 +    endif +  endwhile +  return index ? join(strarr[:index - 1], '') : '' +endfunction + +function! s:strwidthpart_reverse(str, width) abort +  if a:width <= 0 +    return '' +  endif +  let strarr = split(a:str, '\zs') +  let width = s:wcswidth(a:str) +  let strlen = len(strarr) +  let diff = (strlen + 1) / 2 +  let leftindex = 0 +  let index = -1 +  while width > a:width +    let index = min([leftindex + diff, strlen]) - 1 +    let partwidth = s:wcswidth(join(strarr[(leftindex):(index)], '')) +    if width - partwidth >= a:width || diff <= 1 +      let width -= partwidth +      let leftindex = index + 1 +    endif +    if diff > 1 +      let diff = diff / 2 +    endif +  endwhile +  return index < strlen ? join(strarr[(index + 1):], '') : '' +endfunction + +if v:version >= 703 +  " Use builtin function. +  function! s:wcswidth(str) abort +    return strwidth(a:str) +  endfunction +else +  function! s:wcswidth(str) abort +    if a:str =~# '^[\x00-\x7f]*$' +      return strlen(a:str) +    endif +    let mx_first = '^\(.\)' +    let str = a:str +    let width = 0 +    while 1 +      let ucs = char2nr(substitute(str, mx_first, '\1', '')) +      if ucs == 0 +        break +      endif +      let width += s:_wcwidth(ucs) +      let str = substitute(str, mx_first, '', '') +    endwhile +    return width +  endfunction + +  " UTF-8 only. +  function! s:_wcwidth(ucs) abort +    let ucs = a:ucs +    if (ucs >= 0x1100 +          \  && (ucs <= 0x115f +          \  || ucs == 0x2329 +          \  || ucs == 0x232a +          \  || (ucs >= 0x2e80 && ucs <= 0xa4cf +          \      && ucs != 0x303f) +          \  || (ucs >= 0xac00 && ucs <= 0xd7a3) +          \  || (ucs >= 0xf900 && ucs <= 0xfaff) +          \  || (ucs >= 0xfe30 && ucs <= 0xfe6f) +          \  || (ucs >= 0xff00 && ucs <= 0xff60) +          \  || (ucs >= 0xffe0 && ucs <= 0xffe6) +          \  || (ucs >= 0x20000 && ucs <= 0x2fffd) +          \  || (ucs >= 0x30000 && ucs <= 0x3fffd) +          \  )) +      return 2 +    endif +    return 1 +  endfunction +endif + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim:set et ts=2 sts=2 sw=2 tw=0: + +endif diff --git a/autoload/vital/_crystal/Prelude.vim b/autoload/vital/_crystal/Prelude.vim new file mode 100644 index 00000000..be31f980 --- /dev/null +++ b/autoload/vital/_crystal/Prelude.vim @@ -0,0 +1,389 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +let s:save_cpo = &cpo +set cpo&vim + +if v:version ># 703 || +\  (v:version is 703 && has('patch465')) +  function! s:glob(expr) abort +    return glob(a:expr, 1, 1) +  endfunction +else +  function! s:glob(expr) abort +    let R = glob(a:expr, 1) +    return split(R, '\n') +  endfunction +endif + +function! s:globpath(path, expr) abort +  let R = globpath(a:path, a:expr, 1) +  return split(R, '\n') +endfunction + +" Wrapper functions for type(). +let [ +\   s:__TYPE_NUMBER, +\   s:__TYPE_STRING, +\   s:__TYPE_FUNCREF, +\   s:__TYPE_LIST, +\   s:__TYPE_DICT, +\   s:__TYPE_FLOAT] = [ +      \   type(3), +      \   type(""), +      \   type(function('tr')), +      \   type([]), +      \   type({}), +      \   has('float') ? type(str2float('0')) : -1] +" __TYPE_FLOAT = -1 when -float +" This doesn't match to anything. + +" Number or Float +function! s:is_numeric(Value) abort +  let _ = type(a:Value) +  return _ ==# s:__TYPE_NUMBER +  \   || _ ==# s:__TYPE_FLOAT +endfunction + +" Number +function! s:is_number(Value) abort +  return type(a:Value) ==# s:__TYPE_NUMBER +endfunction + +" Float +function! s:is_float(Value) abort +  return type(a:Value) ==# s:__TYPE_FLOAT +endfunction +" String +function! s:is_string(Value) abort +  return type(a:Value) ==# s:__TYPE_STRING +endfunction +" Funcref +function! s:is_funcref(Value) abort +  return type(a:Value) ==# s:__TYPE_FUNCREF +endfunction +" List +function! s:is_list(Value) abort +  return type(a:Value) ==# s:__TYPE_LIST +endfunction +" Dictionary +function! s:is_dict(Value) abort +  return type(a:Value) ==# s:__TYPE_DICT +endfunction + +function! s:truncate_skipping(str, max, footer_width, separator) abort +  call s:_warn_deprecated("truncate_skipping", "Data.String.truncate_skipping") + +  let width = s:wcswidth(a:str) +  if width <= a:max +    let ret = a:str +  else +    let header_width = a:max - s:wcswidth(a:separator) - a:footer_width +    let ret = s:strwidthpart(a:str, header_width) . a:separator +          \ . s:strwidthpart_reverse(a:str, a:footer_width) +  endif + +  return s:truncate(ret, a:max) +endfunction + +function! s:truncate(str, width) abort +  " Original function is from mattn. +  " http://github.com/mattn/googlereader-vim/tree/master + +  call s:_warn_deprecated("truncate", "Data.String.truncate") + +  if a:str =~# '^[\x00-\x7f]*$' +    return len(a:str) < a:width ? +          \ printf('%-'.a:width.'s', a:str) : strpart(a:str, 0, a:width) +  endif + +  let ret = a:str +  let width = s:wcswidth(a:str) +  if width > a:width +    let ret = s:strwidthpart(ret, a:width) +    let width = s:wcswidth(ret) +  endif + +  if width < a:width +    let ret .= repeat(' ', a:width - width) +  endif + +  return ret +endfunction + +function! s:strwidthpart(str, width) abort +  call s:_warn_deprecated("strwidthpart", "Data.String.strwidthpart") + +  if a:width <= 0 +    return '' +  endif +  let ret = a:str +  let width = s:wcswidth(a:str) +  while width > a:width +    let char = matchstr(ret, '.$') +    let ret = ret[: -1 - len(char)] +    let width -= s:wcswidth(char) +  endwhile + +  return ret +endfunction +function! s:strwidthpart_reverse(str, width) abort +  call s:_warn_deprecated("strwidthpart_reverse", "Data.String.strwidthpart_reverse") + +  if a:width <= 0 +    return '' +  endif +  let ret = a:str +  let width = s:wcswidth(a:str) +  while width > a:width +    let char = matchstr(ret, '^.') +    let ret = ret[len(char) :] +    let width -= s:wcswidth(char) +  endwhile + +  return ret +endfunction + +if v:version >= 703 +  " Use builtin function. +  function! s:wcswidth(str) abort +    call s:_warn_deprecated("wcswidth", "Data.String.wcswidth") +    return strwidth(a:str) +  endfunction +else +  function! s:wcswidth(str) abort +    call s:_warn_deprecated("wcswidth", "Data.String.wcswidth") + +    if a:str =~# '^[\x00-\x7f]*$' +      return strlen(a:str) +    end + +    let mx_first = '^\(.\)' +    let str = a:str +    let width = 0 +    while 1 +      let ucs = char2nr(substitute(str, mx_first, '\1', '')) +      if ucs == 0 +        break +      endif +      let width += s:_wcwidth(ucs) +      let str = substitute(str, mx_first, '', '') +    endwhile +    return width +  endfunction + +  " UTF-8 only. +  function! s:_wcwidth(ucs) abort +    let ucs = a:ucs +    if (ucs >= 0x1100 +          \  && (ucs <= 0x115f +          \  || ucs == 0x2329 +          \  || ucs == 0x232a +          \  || (ucs >= 0x2e80 && ucs <= 0xa4cf +          \      && ucs != 0x303f) +          \  || (ucs >= 0xac00 && ucs <= 0xd7a3) +          \  || (ucs >= 0xf900 && ucs <= 0xfaff) +          \  || (ucs >= 0xfe30 && ucs <= 0xfe6f) +          \  || (ucs >= 0xff00 && ucs <= 0xff60) +          \  || (ucs >= 0xffe0 && ucs <= 0xffe6) +          \  || (ucs >= 0x20000 && ucs <= 0x2fffd) +          \  || (ucs >= 0x30000 && ucs <= 0x3fffd) +          \  )) +      return 2 +    endif +    return 1 +  endfunction +endif + +let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95') +let s:is_cygwin = has('win32unix') +let s:is_mac = !s:is_windows && !s:is_cygwin +      \ && (has('mac') || has('macunix') || has('gui_macvim') || +      \   (!isdirectory('/proc') && executable('sw_vers'))) +let s:is_unix = has('unix') + +function! s:is_windows() abort +  return s:is_windows +endfunction + +function! s:is_cygwin() abort +  return s:is_cygwin +endfunction + +function! s:is_mac() abort +  return s:is_mac +endfunction + +function! s:is_unix() abort +  return s:is_unix +endfunction + +function! s:_warn_deprecated(name, alternative) abort +  try +    echohl Error +    echomsg "Prelude." . a:name . " is deprecated!  Please use " . a:alternative . " instead." +  finally +    echohl None +  endtry +endfunction + +function! s:smart_execute_command(action, word) abort +  execute a:action . ' ' . (a:word == '' ? '' : '`=a:word`') +endfunction + +function! s:escape_file_searching(buffer_name) abort +  return escape(a:buffer_name, '*[]?{}, ') +endfunction + +function! s:escape_pattern(str) abort +  return escape(a:str, '~"\.^$[]*') +endfunction + +function! s:getchar(...) abort +  let c = call('getchar', a:000) +  return type(c) == type(0) ? nr2char(c) : c +endfunction + +function! s:getchar_safe(...) abort +  let c = s:input_helper('getchar', a:000) +  return type(c) == type("") ? c : nr2char(c) +endfunction + +function! s:input_safe(...) abort +  return s:input_helper('input', a:000) +endfunction + +function! s:input_helper(funcname, args) abort +  let success = 0 +  if inputsave() !=# success +    throw 'vital: Prelude: inputsave() failed' +  endif +  try +    return call(a:funcname, a:args) +  finally +    if inputrestore() !=# success +      throw 'vital: Prelude: inputrestore() failed' +    endif +  endtry +endfunction + +function! s:set_default(var, val) abort +  if !exists(a:var) || type({a:var}) != type(a:val) +    let {a:var} = a:val +  endif +endfunction + +function! s:substitute_path_separator(path) abort +  return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path +endfunction + +function! s:path2directory(path) abort +  return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h')) +endfunction + +function! s:_path2project_directory_git(path) abort +  let parent = a:path + +  while 1 +    let path = parent . '/.git' +    if isdirectory(path) || filereadable(path) +      return parent +    endif +    let next = fnamemodify(parent, ':h') +    if next == parent +      return '' +    endif +    let parent = next +  endwhile +endfunction + +function! s:_path2project_directory_svn(path) abort +  let search_directory = a:path +  let directory = '' + +  let find_directory = s:escape_file_searching(search_directory) +  let d = finddir('.svn', find_directory . ';') +  if d == '' +    return '' +  endif + +  let directory = fnamemodify(d, ':p:h:h') + +  " Search parent directories. +  let parent_directory = s:path2directory( +        \ fnamemodify(directory, ':h')) + +  if parent_directory != '' +    let d = finddir('.svn', parent_directory . ';') +    if d != '' +      let directory = s:_path2project_directory_svn(parent_directory) +    endif +  endif +  return directory +endfunction + +function! s:_path2project_directory_others(vcs, path) abort +  let vcs = a:vcs +  let search_directory = a:path + +  let find_directory = s:escape_file_searching(search_directory) +  let d = finddir(vcs, find_directory . ';') +  if d == '' +    return '' +  endif +  return fnamemodify(d, ':p:h:h') +endfunction + +function! s:path2project_directory(path, ...) abort +  let is_allow_empty = get(a:000, 0, 0) +  let search_directory = s:path2directory(a:path) +  let directory = '' + +  " Search VCS directory. +  for vcs in ['.git', '.bzr', '.hg', '.svn'] +    if vcs ==# '.git' +      let directory = s:_path2project_directory_git(search_directory) +    elseif vcs ==# '.svn' +      let directory = s:_path2project_directory_svn(search_directory) +    else +      let directory = s:_path2project_directory_others(vcs, search_directory) +    endif +    if directory != '' +      break +    endif +  endfor + +  " Search project file. +  if directory == '' +    for d in ['build.xml', 'prj.el', '.project', 'pom.xml', 'package.json', +          \ 'Makefile', 'configure', 'Rakefile', 'NAnt.build', +          \ 'P4CONFIG', 'tags', 'gtags'] +      let d = findfile(d, s:escape_file_searching(search_directory) . ';') +      if d != '' +        let directory = fnamemodify(d, ':p:h') +        break +      endif +    endfor +  endif + +  if directory == '' +    " Search /src/ directory. +    let base = s:substitute_path_separator(search_directory) +    if base =~# '/src/' +      let directory = base[: strridx(base, '/src/') + 3] +    endif +  endif + +  if directory == '' && !is_allow_empty +    " Use original path. +    let directory = search_directory +  endif + +  return s:substitute_path_separator(directory) +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim:set et ts=2 sts=2 sw=2 tw=0: + +endif diff --git a/autoload/vital/_crystal/Process.vim b/autoload/vital/_crystal/Process.vim new file mode 100644 index 00000000..c2deb9e5 --- /dev/null +++ b/autoload/vital/_crystal/Process.vim @@ -0,0 +1,185 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +" TODO: move all comments to doc file. +" +" +" FIXME: This module name should be Vital.System ? +" But the name has been already taken. + +let s:save_cpo = &cpo +set cpo&vim + + +" FIXME: Unfortunately, can't use s:_vital_loaded() for this purpose. +" Because these variables are used when this script file is loaded. +let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95') +let s:is_unix = has('unix') +" As of 7.4.122, the system()'s 1st argument is converted internally by Vim. +" Note that Patch 7.4.122 does not convert system()'s 2nd argument and +" return-value. We must convert them manually. +let s:need_trans = v:version < 704 || (v:version == 704 && !has('patch122')) + +let s:TYPE_DICT = type({}) +let s:TYPE_LIST = type([]) +let s:TYPE_STRING = type("") + + +" Execute program in the background from Vim. +" Return an empty string always. +" +" If a:expr is a List, shellescape() each argument. +" If a:expr is a String, the arguments are passed as-is. +" +" Windows: +" Using :!start , execute program without via cmd.exe. +" Spawning 'expr' with 'noshellslash' +" keep special characters from unwanted expansion. +" (see :help shellescape()) +" +" Unix: +" using :! , execute program in the background by shell. +function! s:spawn(expr, ...) abort +  let shellslash = 0 +  if s:is_windows +    let shellslash = &l:shellslash +    setlocal noshellslash +  endif +  try +    if type(a:expr) is s:TYPE_LIST +      let special = 1 +      let cmdline = join(map(a:expr, 'shellescape(v:val, special)'), ' ') +    elseif type(a:expr) is s:TYPE_STRING +      let cmdline = a:expr +      if a:0 && a:1 +        " for :! command +        let cmdline = substitute(cmdline, '\([!%#]\|<[^<>]\+>\)', '\\\1', 'g') +      endif +    else +      throw 'Process.spawn(): invalid argument (value type:'.type(a:expr).')' +    endif +    if s:is_windows +      silent execute '!start' cmdline +    else +      silent execute '!' cmdline '&' +    endif +  finally +    if s:is_windows +      let &l:shellslash = shellslash +    endif +  endtry +  return '' +endfunction + +" iconv() wrapper for safety. +function! s:iconv(expr, from, to) abort +  if a:from == '' || a:to == '' || a:from ==? a:to +    return a:expr +  endif +  let result = iconv(a:expr, a:from, a:to) +  return result != '' ? result : a:expr +endfunction + +" Check vimproc. +function! s:has_vimproc() abort +  if !exists('s:exists_vimproc') +    try +      call vimproc#version() +      let s:exists_vimproc = 1 +    catch +      let s:exists_vimproc = 0 +    endtry +  endif +  return s:exists_vimproc +endfunction + +" * {command} [, {input} [, {timeout}]] +" * {command} [, {dict}] +"   {dict} = { +"     use_vimproc: bool, +"     input: string, +"     timeout: bool, +"     background: bool, +"   } +function! s:system(str, ...) abort +  " Process optional arguments at first +  " because use_vimproc is required later +  " for a:str argument. +  let input = '' +  let use_vimproc = s:has_vimproc() +  let background = 0 +  let args = [] +  if a:0 ==# 1 +    " {command} [, {dict}] +    " a:1 = {dict} +    if type(a:1) is s:TYPE_DICT +      if has_key(a:1, 'use_vimproc') +        let use_vimproc = a:1.use_vimproc +      endif +      if has_key(a:1, 'input') +        let args += [s:iconv(a:1.input, &encoding, 'char')] +      endif +      if use_vimproc && has_key(a:1, 'timeout') +        " ignores timeout unless you have vimproc. +        let args += [a:1.timeout] +      endif +      if has_key(a:1, 'background') +        let background = a:1.background +      endif +    elseif type(a:1) is s:TYPE_STRING +      let args += [s:iconv(a:1, &encoding, 'char')] +    else +      throw 'Process.system(): invalid argument (value type:'.type(a:1).')' +    endif +  elseif a:0 >= 2 +    " {command} [, {input} [, {timeout}]] +    " a:000 = [{input} [, {timeout}]] +    let [input; rest] = a:000 +    let input   = s:iconv(input, &encoding, 'char') +    let args += [input] + rest +  endif + +  " Process a:str argument. +  if type(a:str) is s:TYPE_LIST +    let expr = use_vimproc ? '"''" . v:val . "''"' : 's:shellescape(v:val)' +    let command = join(map(copy(a:str), expr), ' ') +  elseif type(a:str) is s:TYPE_STRING +    let command = a:str +  else +    throw 'Process.system(): invalid argument (value type:'.type(a:str).')' +  endif +  if s:need_trans +    let command = s:iconv(command, &encoding, 'char') +  endif +  let args = [command] + args +  if background && (use_vimproc || !s:is_windows) +    let args[0] = args[0] . ' &' +  endif + +  let funcname = use_vimproc ? 'vimproc#system' : 'system' +  let output = call(funcname, args) +  let output = s:iconv(output, 'char', &encoding) +  return output +endfunction + +function! s:get_last_status() abort +  return s:has_vimproc() ? +        \ vimproc#get_last_status() : v:shell_error +endfunction + +if s:is_windows +  function! s:shellescape(command) abort +    return substitute(a:command, '[&()[\]{}^=;!''+,`~]', '^\0', 'g') +  endfunction +else +  function! s:shellescape(...) abort +    return call('shellescape', a:000) +  endfunction +endif + + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim:set et ts=2 sts=2 sw=2 tw=0: + +endif diff --git a/autoload/vital/_crystal/Web/JSON.vim b/autoload/vital/_crystal/Web/JSON.vim new file mode 100644 index 00000000..0e42ee0d --- /dev/null +++ b/autoload/vital/_crystal/Web/JSON.vim @@ -0,0 +1,112 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +   +let s:save_cpo = &cpo +set cpo&vim + +function! s:_true() abort +  return 1 +endfunction + +function! s:_false() abort +  return 0 +endfunction + +function! s:_null() abort +  return 0 +endfunction + +let s:const = {} +let s:const.true = function('s:_true') +let s:const.false = function('s:_false') +let s:const.null = function('s:_null') + +function! s:_resolve(val, prefix) abort +  let t = type(a:val) +  if t == type('') +    let m = matchlist(a:val, '^' . a:prefix . '\(null\|true\|false\)$') +    if !empty(m) +      return s:const[m[1]] +    endif +  elseif t == type([]) || t == type({}) +    return map(a:val, 's:_resolve(v:val, a:prefix)') +  endif +  return a:val +endfunction + + +function! s:_vital_created(module) abort +  " define constant variables +  call extend(a:module, s:const) +endfunction + +function! s:_vital_loaded(V) abort +  let s:V = a:V +  let s:string = s:V.import('Data.String') +endfunction + +function! s:_vital_depends() abort +  return ['Data.String'] +endfunction + +" @vimlint(EVL102, 1, l:null) +" @vimlint(EVL102, 1, l:true) +" @vimlint(EVL102, 1, l:false) +function! s:decode(json, ...) abort +  let settings = extend({ +        \ 'use_token': 0, +        \}, get(a:000, 0, {})) +  let json = iconv(a:json, "utf-8", &encoding) +  let json = join(split(json, "\n"), '') +  let json = substitute(json, '\\u34;', '\\"', 'g') +  let json = substitute(json, '\\u\(\x\x\x\x\)', '\=s:string.nr2enc_char("0x".submatch(1))', 'g') +  if settings.use_token +    let prefix = '__Web.JSON__' +    while stridx(json, prefix) != -1 +      let prefix .= '_' +    endwhile +    let [null,true,false] = map(['null','true','false'], 'prefix . v:val') +    sandbox return s:_resolve(eval(json), prefix) +  else +    let [null,true,false] = [s:const.null(),s:const.true(),s:const.false()] +    sandbox return eval(json) +  endif +endfunction +" @vimlint(EVL102, 0, l:null) +" @vimlint(EVL102, 0, l:true) +" @vimlint(EVL102, 0, l:false) + +function! s:encode(val) abort +  if type(a:val) == 0 +    return a:val +  elseif type(a:val) == 1 +    let json = '"' . escape(a:val, '\"') . '"' +    let json = substitute(json, "\r", '\\r', 'g') +    let json = substitute(json, "\n", '\\n', 'g') +    let json = substitute(json, "\t", '\\t', 'g') +    return iconv(json, &encoding, "utf-8") +  elseif type(a:val) == 2 +    if s:const.true == a:val +      return 'true' +    elseif s:const.false == a:val +      return 'false' +    elseif s:const.null == a:val +      return 'null' +    else +      " backward compatibility +      return string(a:val) +    endif +  elseif type(a:val) == 3 +    return '[' . join(map(copy(a:val), 's:encode(v:val)'), ',') . ']' +  elseif type(a:val) == 4 +    return '{' . join(map(keys(a:val), 's:encode(v:val).":".s:encode(a:val[v:val])'), ',') . '}' +  else +    return string(a:val) +  endif +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim:set et ts=2 sts=2 sw=2 tw=0: + +endif | 
