Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Fix #81 *WIP* #82

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions autoload/gista/util/prompt.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let s:V = gista#vital()
let s:Prompt = s:V.import('Vim.Prompt')
let s:Console = s:V.import('Vim.Console')

function! s:is_debug() abort
return g:gista#debug
Expand All @@ -9,25 +9,25 @@ function! s:is_batch() abort
endfunction

function! gista#util#prompt#debug(...) abort
call call(s:Prompt.debug, a:000, s:Prompt)
call call(s:Console.debug, a:000, s:Console)
endfunction
function! gista#util#prompt#info(...) abort
call call(s:Prompt.info, a:000, s:Prompt)
call call(s:Console.info, a:000, s:Console)
endfunction
function! gista#util#prompt#warn(...) abort
call call(s:Prompt.warn, a:000, s:Prompt)
call call(s:Console.warn, a:000, s:Console)
endfunction
function! gista#util#prompt#error(...) abort
call call(s:Prompt.error, a:000, s:Prompt)
call call(s:Console.error, a:000, s:Console)
endfunction
function! gista#util#prompt#ask(...) abort
return call(s:Prompt.ask, a:000, s:Prompt)
return call(s:Console.ask, a:000, s:Console)
endfunction
function! gista#util#prompt#select(...) abort
return call(s:Prompt.select, a:000, s:Prompt)
return call(s:Console.select, a:000, s:Console)
endfunction
function! gista#util#prompt#confirm(...) abort
return call(s:Prompt.confirm, a:000, s:Prompt)
return call(s:Console.confirm, a:000, s:Console)
endfunction

function! gista#util#prompt#indicate(options, message) abort
Expand All @@ -36,7 +36,7 @@ function! gista#util#prompt#indicate(options, message) abort
endif
endfunction

call s:Prompt.set_config({
call s:Console.set_config({
\ 'debug': function('s:is_debug'),
\ 'batch': function('s:is_batch'),
\})
11 changes: 8 additions & 3 deletions autoload/vital/_vim_gista/Data/List.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
" Do not mofidify the code nor insert new lines before '" ___vital___'
if v:version > 703 || v:version == 703 && has('patch1170')
function! vital#_vim_gista#Data#List#import() abort
return map({'combinations': '', 'and': '', 'sort_by': '', 'foldr1': '', 'sort': '', 'flatten': '', 'has_index': '', 'find_indices': '', 'any': '', 'unshift': '', 'span': '', 'pop': '', 'binary_search': '', 'uniq_by': '', 'or': '', 'all': '', 'zip': '', 'find_last_index': '', 'find': '', 'partition': '', 'map_accum': '', 'permutations': '', 'break': '', 'max_by': '', 'foldl': '', 'foldr': '', 'find_index': '', 'group_by': '', 'take_while': '', 'conj': '', 'push': '', 'char_range': '', 'cons': '', 'foldl1': '', 'intersect': '', 'concat': '', 'shift': '', 'clear': '', 'has_common_items': '', 'product': '', 'zip_fill': '', 'uniq': '', 'has': '', 'min_by': '', 'with_index': ''}, 'function("s:" . v:key)')
return map({'combinations': '', 'and': '', 'sort_by': '', 'foldr1': '', 'sort': '', 'flatten': '', 'has_index': '', 'find_indices': '', 'any': '', 'unshift': '', 'span': '', 'pop': '', 'binary_search': '', 'uniq_by': '', 'or': '', 'all': '', 'zip': '', 'find_last_index': '', 'find': '', 'partition': '', 'shift': '', 'permutations': '', 'break': '', 'max_by': '', 'foldl': '', 'foldr': '', 'find_index': '', 'drop_while': '', 'group_by': '', 'take_while': '', 'conj': '', 'push': '', 'char_range': '', 'cons': '', 'foldl1': '', 'intersect': '', 'concat': '', 'map_accum': '', 'clear': '', 'has_common_items': '', 'product': '', 'zip_fill': '', 'uniq': '', 'has': '', 'min_by': '', 'with_index': ''}, 'function("s:" . v:key)')
endfunction
else
function! s:_SID() abort
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
execute join(['function! vital#_vim_gista#Data#List#import() abort', printf("return map({'combinations': '', 'and': '', 'sort_by': '', 'foldr1': '', 'sort': '', 'flatten': '', 'has_index': '', 'find_indices': '', 'any': '', 'unshift': '', 'span': '', 'pop': '', 'binary_search': '', 'uniq_by': '', 'or': '', 'all': '', 'zip': '', 'find_last_index': '', 'find': '', 'partition': '', 'map_accum': '', 'permutations': '', 'break': '', 'max_by': '', 'foldl': '', 'foldr': '', 'find_index': '', 'group_by': '', 'take_while': '', 'conj': '', 'push': '', 'char_range': '', 'cons': '', 'foldl1': '', 'intersect': '', 'concat': '', 'shift': '', 'clear': '', 'has_common_items': '', 'product': '', 'zip_fill': '', 'uniq': '', 'has': '', 'min_by': '', 'with_index': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
execute join(['function! vital#_vim_gista#Data#List#import() abort', printf("return map({'combinations': '', 'and': '', 'sort_by': '', 'foldr1': '', 'sort': '', 'flatten': '', 'has_index': '', 'find_indices': '', 'any': '', 'unshift': '', 'span': '', 'pop': '', 'binary_search': '', 'uniq_by': '', 'or': '', 'all': '', 'zip': '', 'find_last_index': '', 'find': '', 'partition': '', 'shift': '', 'permutations': '', 'break': '', 'max_by': '', 'foldl': '', 'foldr': '', 'find_index': '', 'drop_while': '', 'group_by': '', 'take_while': '', 'conj': '', 'push': '', 'char_range': '', 'cons': '', 'foldl1': '', 'intersect': '', 'concat': '', 'map_accum': '', 'clear': '', 'has_common_items': '', 'product': '', 'zip_fill': '', 'uniq': '', 'has': '', 'min_by': '', 'with_index': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
delfunction s:_SID
endif
" ___vital___
Expand Down Expand Up @@ -188,6 +188,11 @@ function! s:take_while(f, xs) abort
return s:span(a:f, a:xs)[0]
endfunction

" similar to Haskell's Data.List.dropWhile
function! s:drop_while(f, xs) abort
return s:span(a:f, a:xs)[1]
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 . ')')]
Expand Down Expand Up @@ -439,7 +444,7 @@ 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'
throw 'vital: Data.List: {r} must be non-negative integer'
endif
let n = len(a:list)
let result = []
Expand Down
76 changes: 24 additions & 52 deletions autoload/vital/_vim_gista/Data/String.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
" Do not mofidify the code nor insert new lines before '" ___vital___'
if v:version > 703 || v:version == 703 && has('patch1170')
function! vital#_vim_gista#Data#String#import() abort
return map({'starts_with': '', 'split3': '', 'chop': '', 'unescape': '', 'split_posix_text': '', 'replace': '', 'scan': '', 'strchars': '', 'strwidthpart': '', 'common_head': '', 'reverse': '', 'escape_pattern': '', 'trim_end': '', '_vital_depends': '', 'wrap': '', 'join_posix_lines': '', 'contains_multibyte': '', 'truncate_skipping': '', 'split_leftright': '', 'ends_with': '', 'nsplit': '', 'substitute_last': '', 'strwidthpart_reverse': '', 'unescape_pattern': '', 'levenshtein_distance': '', 'trim_start': '', 'nr2hex': '', 'remove_ansi_sequences': '', 'iconv': '', 'pad_left': '', 'nr2enc_char': '', 'lines': '', 'repair_posix_text': '', 'nr2byte': '', 'trim': '', 'diffidx': '', 'truncate': '', 'split_by_displaywidth': '', 'padding_by_displaywidth': '', 'hash': '', 'chomp': '', 'pad_between_letters': '', 'wcswidth': '', 'dstring': '', 'pad_both_sides': '', 'justify_equal_spacing': '', 'pad_right': '', 'replace_first': '', '_vital_loaded': ''}, 'function("s:" . v:key)')
return map({'starts_with': '', 'split3': '', 'replace_first': '', 'chop': '', 'unescape': '', 'split_posix_text': '', 'replace': '', 'scan': '', 'strwidthpart': '', 'common_head': '', 'reverse': '', 'escape_pattern': '', 'trim_end': '', '_vital_depends': '', 'wrap': '', 'join_posix_lines': '', 'contains_multibyte': '', 'truncate_skipping': '', 'split_leftright': '', 'ends_with': '', 'nsplit': '', 'strwidthpart_reverse': '', 'unescape_pattern': '', 'levenshtein_distance': '', 'trim_start': '', 'justify_equal_spacing': '', 'nr2hex': '', 'iconv': '', 'pad_left': '', 'nr2enc_char': '', 'lines': '', 'repair_posix_text': '', 'nr2byte': '', 'trim': '', 'diffidx': '', 'truncate': '', 'split_by_displaywidth': '', '_vital_created': '', 'padding_by_displaywidth': '', 'hash': '', 'chomp': '', 'pad_between_letters': '', 'dstring': '', 'pad_both_sides': '', 'substitute_last': '', 'pad_right': '', 'remove_ansi_sequences': '', '_vital_loaded': ''}, 'function("s:" . v:key)')
endfunction
else
function! s:_SID() abort
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
execute join(['function! vital#_vim_gista#Data#String#import() abort', printf("return map({'starts_with': '', 'split3': '', 'chop': '', 'unescape': '', 'split_posix_text': '', 'replace': '', 'scan': '', 'strchars': '', 'strwidthpart': '', 'common_head': '', 'reverse': '', 'escape_pattern': '', 'trim_end': '', '_vital_depends': '', 'wrap': '', 'join_posix_lines': '', 'contains_multibyte': '', 'truncate_skipping': '', 'split_leftright': '', 'ends_with': '', 'nsplit': '', 'substitute_last': '', 'strwidthpart_reverse': '', 'unescape_pattern': '', 'levenshtein_distance': '', 'trim_start': '', 'nr2hex': '', 'remove_ansi_sequences': '', 'iconv': '', 'pad_left': '', 'nr2enc_char': '', 'lines': '', 'repair_posix_text': '', 'nr2byte': '', 'trim': '', 'diffidx': '', 'truncate': '', 'split_by_displaywidth': '', 'padding_by_displaywidth': '', 'hash': '', 'chomp': '', 'pad_between_letters': '', 'wcswidth': '', 'dstring': '', 'pad_both_sides': '', 'justify_equal_spacing': '', 'pad_right': '', 'replace_first': '', '_vital_loaded': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
execute join(['function! vital#_vim_gista#Data#String#import() abort', printf("return map({'starts_with': '', 'split3': '', 'replace_first': '', 'chop': '', 'unescape': '', 'split_posix_text': '', 'replace': '', 'scan': '', 'strwidthpart': '', 'common_head': '', 'reverse': '', 'escape_pattern': '', 'trim_end': '', '_vital_depends': '', 'wrap': '', 'join_posix_lines': '', 'contains_multibyte': '', 'truncate_skipping': '', 'split_leftright': '', 'ends_with': '', 'nsplit': '', 'strwidthpart_reverse': '', 'unescape_pattern': '', 'levenshtein_distance': '', 'trim_start': '', 'justify_equal_spacing': '', 'nr2hex': '', 'iconv': '', 'pad_left': '', 'nr2enc_char': '', 'lines': '', 'repair_posix_text': '', 'nr2byte': '', 'trim': '', 'diffidx': '', 'truncate': '', 'split_by_displaywidth': '', '_vital_created': '', 'padding_by_displaywidth': '', 'hash': '', 'chomp': '', 'pad_between_letters': '', 'dstring': '', 'pad_both_sides': '', 'substitute_last': '', 'pad_right': '', 'remove_ansi_sequences': '', '_vital_loaded': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
delfunction s:_SID
endif
" ___vital___
Expand All @@ -27,6 +27,16 @@ function! s:_vital_depends() abort
return ['Data.List']
endfunction

function! s:_vital_created(module) abort
" Expose script-local funcref
if exists('s:strchars')
let a:module.strchars = s:strchars
endif
if exists('s:wcswidth')
let a:module.wcswidth = s:wcswidth
endif
endfunction

" Substitute a:from => a:to by string.
" To substitute by pattern, use substitute() instead.
function! s:replace(str, from, to) abort
Expand Down Expand Up @@ -142,9 +152,7 @@ endfunction
" even if a:str contains multibyte character(s).
" s:strchars(str) {{{
if exists('*strchars')
function! s:strchars(str) abort
return strchars(a:str)
endfunction
let s:strchars = function('strchars')
else
function! s:strchars(str) abort
return strlen(substitute(copy(a:str), '.', 'x', 'g'))
Expand Down Expand Up @@ -452,8 +460,9 @@ function! s:truncate(str, width) abort
" 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)
return len(a:str) < a:width
\ ? printf('%-' . a:width . 's', a:str)
\ : strpart(a:str, 0, a:width)
endif

let ret = a:str
Expand Down Expand Up @@ -483,57 +492,20 @@ function! s:truncate_skipping(str, max, footer_width, separator) abort
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], '') : ''
let str = tr(a:str, "\t", ' ')
let vcol = a:width + 2
return matchstr(str, '.*\%<' . (vcol < 0 ? 0 : vcol) . 'v')
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):], '') : ''
let str = tr(a:str, "\t", ' ')
let vcol = s:wcswidth(str) - a:width
return matchstr(str, '\%>' . (vcol < 0 ? 0 : vcol) . 'v.*')
endfunction

if v:version >= 703
" Use builtin function.
function! s:wcswidth(str) abort
return strwidth(a:str)
endfunction
let s:wcswidth = function('strwidth')
else
function! s:wcswidth(str) abort
if a:str =~# '^[\x00-\x7f]*$'
Expand Down Expand Up @@ -577,7 +549,7 @@ else
endif

function! s:remove_ansi_sequences(text) abort
return substitute(a:text, '\e\[\%(\%(\d;\)\?\d\{1,2}\)\?[mK]', '', 'g')
return substitute(a:text, '\e\[\%(\%(\d\+;\)*\d\+\)\?[mK]', '', 'g')
endfunction

function! s:escape_pattern(str) abort
Expand Down
59 changes: 40 additions & 19 deletions autoload/vital/_vim_gista/Prelude.vim
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,37 @@ else
endif

" 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.
" NOTE: __TYPE_FLOAT = -1 when -float.
" this doesn't match to anything.
if has('patch-7.4.2071')
let [
\ s:__TYPE_NUMBER,
\ s:__TYPE_STRING,
\ s:__TYPE_FUNCREF,
\ s:__TYPE_LIST,
\ s:__TYPE_DICT,
\ s:__TYPE_FLOAT] = [
\ v:t_number,
\ v:t_string,
\ v:t_func,
\ v:t_list,
\ v:t_dict,
\ v:t_float]
else
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]
endif

" Number or Float
function! s:is_numeric(Value) abort
Expand All @@ -67,27 +83,32 @@ 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

" Float
function! s:is_float(Value) abort
return type(a:Value) ==# s:__TYPE_FLOAT
endfunction


function! s:truncate_skipping(str, max, footer_width, separator) abort
call s:_warn_deprecated('truncate_skipping', 'Data.String.truncate_skipping')

Expand Down
9 changes: 6 additions & 3 deletions autoload/vital/_vim_gista/Process.vim
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function! s:spawn(expr, ...) abort
let cmdline = substitute(cmdline, '\([!%#]\|<[^<>]\+>\)', '\\\1', 'g')
endif
else
throw 'Process.spawn(): invalid argument (value type:'.type(a:expr).')'
throw 'vital: Process: invalid argument (value type:' . type(a:expr) . ')'
endif
if s:is_windows
silent execute '!start' cmdline
Expand Down Expand Up @@ -126,7 +126,7 @@ function! s:system(str, ...) abort
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).')'
throw 'vital: Process: invalid argument (value type:' . type(a:1) . ')'
endif
elseif a:0 >= 2
" {command} [, {input} [, {timeout}]]
Expand All @@ -143,13 +143,16 @@ function! s:system(str, ...) abort
elseif type(a:str) is s:TYPE_STRING
let command = a:str
else
throw 'Process.system(): invalid argument (value type:'.type(a:str).')'
throw 'vital: Process: 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)
if has('nvim')
throw "vital: Process: neovim's system() doesn't support background(&) process (cmdline:" . a:str . ')'
endif
let args[0] = args[0] . ' &'
endif

Expand Down
2 changes: 1 addition & 1 deletion autoload/vital/_vim_gista/System/Filepath.vim
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ endfunction
" Check if the path is absolute path.
if s:is_windows
function! s:is_absolute(path) abort
return a:path =~? '^[a-z]:[/\\]'
return a:path =~# '^[a-zA-Z]:[/\\]'
endfunction
else
function! s:is_absolute(path) abort
Expand Down