summaryrefslogtreecommitdiffstats
path: root/autoload/csv.vim
diff options
context:
space:
mode:
authorAdam Stankiewicz <sheerun@sher.pl>2021-07-08 11:54:15 +0200
committerAdam Stankiewicz <sheerun@sher.pl>2021-07-08 11:54:15 +0200
commitc794f186c0a618d2d4cdd5445d9ff20e6f640762 (patch)
tree11e6b01bac01b0ec694c8fa5c574870f5e268182 /autoload/csv.vim
parent4f5388350be1052f610b830c8fce8fbc17370ec6 (diff)
downloadvim-polyglot-c794f186c0a618d2d4cdd5445d9ff20e6f640762.tar.gz
vim-polyglot-c794f186c0a618d2d4cdd5445d9ff20e6f640762.zip
Update
Diffstat (limited to 'autoload/csv.vim')
-rw-r--r--autoload/csv.vim124
1 files changed, 75 insertions, 49 deletions
diff --git a/autoload/csv.vim b/autoload/csv.vim
index 98feee75..36a65dd3 100644
--- a/autoload/csv.vim
+++ b/autoload/csv.vim
@@ -14,7 +14,7 @@ endif
" Some ideas are taken from the wiki http://vim.wikia.com/wiki/VimTip667
" though, implementation differs.
-let s:csv_numeric_sort = v:version > 704 || v:version == 704 && has("patch341")
+let s:csv_numeric_sort = v:version > 704 || v:version == 704 && has("patch951")
if !s:csv_numeric_sort "{{{2
fu! csv#CSVSortValues(i1, i2) "{{{3
return (a:i1+0) == (a:i2+0) ? 0 : (a:i1+0) > (a:i2+0) ? 1 : -1
@@ -222,6 +222,15 @@ fu! csv#LocalSettings(type) "{{{3
endif
endfu
+fu! csv#RemoveAutoHighlight() "{{{3
+ exe "aug CSV_HI".bufnr('')
+ exe "au! CursorMoved <buffer=".bufnr('').">"
+ aug end
+ exe "aug! CSV_HI".bufnr('')
+ " Remove any existing highlighting
+ HiColumn!
+endfu
+
fu! csv#DoAutoCommands() "{{{3
" Highlight column, on which the cursor is
if exists("g:csv_highlight_column") && g:csv_highlight_column =~? 'y'
@@ -233,12 +242,7 @@ fu! csv#DoAutoCommands() "{{{3
" Set highlighting for column, on which the cursor is currently
HiColumn
else
- exe "aug CSV_HI".bufnr('')
- exe "au! CursorMoved <buffer=".bufnr('').">"
- aug end
- exe "aug! CSV_HI".bufnr('')
- " Remove any existing highlighting
- HiColumn!
+ call csv#RemoveAutoHighlight()
endif
" undo autocommand:
let b:undo_ftplugin .= '| exe "sil! au! CSV_HI'.bufnr('').' CursorMoved <buffer> "'
@@ -539,7 +543,7 @@ fu! csv#WColumn(...) "{{{3
let temp=getpos('.')[2]
let j=1
let ret = 1
- for i in sort(b:csv_fixed_width_cols, s:csv_numeric_sort ? 'n' : 'csv#CSVSortValues')
+ for i in sort(b:csv_fixed_width_cols, s:csv_numeric_sort ? 'N' : 'csv#CSVSortValues')
if temp >= i
let ret = j
endif
@@ -549,6 +553,9 @@ fu! csv#WColumn(...) "{{{3
call setpos('.',_cur)
return ret
endfu
+fu! csv#ValidComment() "{{{3
+ return b:csv_cmt != ['', ''] && !empty(b:csv_cmt[0])
+endfu
fu! csv#MaxColumns(...) "{{{3
let this_col = exists("a:1")
"return maximum number of columns in first 10 lines
@@ -561,8 +568,10 @@ fu! csv#MaxColumns(...) "{{{3
endif
" Filter comments out
- let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
- call filter(l, 'v:val !~ pat')
+ if csv#ValidComment()
+ let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ call filter(l, 'v:val !~ pat')
+ endif
if !empty(l) || this_col
break
else
@@ -606,8 +615,10 @@ fu! csv#ColWidth(colnr, row, silent) "{{{3
endif
endif
let b:csv_list=getline(skipfirst+1,last)
- let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
- call filter(b:csv_list, 'v:val !~ pat')
+ if csv#ValidComment()
+ let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ call filter(b:csv_list, 'v:val !~ pat')
+ endif
call filter(b:csv_list, '!empty(v:val)')
call map(b:csv_list, 'split(v:val, b:col.''\zs'')')
endif
@@ -898,7 +909,7 @@ fu! csv#Columnize(field) "{{{3
return result
else
" right align
- return printf("%*S", width+1 , a:field)
+ return printf("%*S", width , a:field)
endif
endfun
fu! csv#GetColPat(colnr, zs_flag) "{{{3
@@ -912,15 +923,15 @@ fu! csv#GetColPat(colnr, zs_flag) "{{{3
let pat='\%' . b:csv_fixed_width_cols[-1] . 'v.*'
else
let pat='\%' . b:csv_fixed_width_cols[(a:colnr - 1)] .
- \ 'c.\{-}\%' . b:csv_fixed_width_cols[a:colnr] . 'v'
+ \ 'c.*\%<' . (b:csv_fixed_width_cols[a:colnr] + 1) . 'v'
endif
endif
elseif !exists("b:csv_fixed_width_cols")
let pat=b:col
else
- let pat='\%' . b:csv_fixed_width_cols[0] . 'v.\{-}' .
+ let pat='\%' . b:csv_fixed_width_cols[0] . 'v.*' .
\ (len(b:csv_fixed_width_cols) > 1 ?
- \ '\%' . b:csv_fixed_width_cols[1] . 'v' :
+ \ '\%<' . (b:csv_fixed_width_cols[1] + 1) . 'v' :
\ '')
endif
return pat . (a:zs_flag ? '\zs' : '')
@@ -1115,7 +1126,12 @@ fu! csv#MoveCol(forward, line, ...) "{{{3
let pat=csv#GetColPat(1, 0)
else
" Move backwards
- let pat=csv#GetColPat(maxcol, 0)
+ if cpos == 1 && (exists("a:1") && a:1)
+ " H move to previous line
+ let pat=csv#GetColPat(maxcol, 0)
+ else
+ let pat='\%1v'
+ endif
endif
endif
else
@@ -1149,9 +1165,13 @@ fu! csv#MoveCol(forward, line, ...) "{{{3
" of a field.
let epos = getpos('.')
if getline('.')[col('.')-1] == ' '
- call search('\S', 'W', line('.'))
- if getpos('.')[2] > spos
- call setpos('.', epos)
+ if !exists("b:csv_fixed_width_cols")
+ call search('\S', 'W', line('.'))
+ if getpos('.')[2] > spos
+ call setpos('.', epos)
+ endif
+ elseif cpos > b:csv_fixed_width_cols[colnr]
+ call search('\%'. b:csv_fixed_width_cols[colnr]. 'v', 'W', line('.'))
endif
endif
endif
@@ -1246,8 +1266,10 @@ fu! csv#CopyCol(reg, col, cnt) "{{{3
endfor
endif
" Filter comments out
- let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
- call filter(a, 'v:val !~ pat')
+ if csv#ValidComment()
+ let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ call filter(a, 'v:val !~ pat')
+ endif
if !exists("b:csv_fixed_width_cols")
call map(a, 'split(v:val, ''^'' . b:col . ''\zs'')[col-1:cnt_cols]')
@@ -1288,10 +1310,9 @@ fu! csv#MoveColumn(start, stop, ...) range "{{{3
endif
" Swap line by line, instead of reading the whole range into memory
-
for i in range(a:start, a:stop)
let content = getline(i)
- if content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if b:csv_cmt != ['',''] && content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
" skip comments
continue
endif
@@ -1359,13 +1380,9 @@ fu! csv#DupColumn(start, stop, ...) range "{{{3
" skipping comment lines (we could do it with a single :s statement,
" but that would fail for the first and last column.
- let commentpat = '\%(\%>'.(a:start-1).'l\V'.
- \ escape(b:csv_cmt[0], '\\').'\m\)'. '\&\%(\%<'.
- \ (a:stop+1). 'l\V'. escape(b:csv_cmt[0], '\\'). '\m\)'
-
for i in range(a:start, a:stop)
let content = getline(i)
- if content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if csv#ValidComment() && content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
" skip comments
continue
endif
@@ -1429,10 +1446,12 @@ fu! csv#AddColumn(start, stop, ...) range "{{{3
" skipping comment lines (we could do it with a single :s statement,
" but that would fail for the first and last column.
- let commentpat = '\%(\%>'.(a:start-1).'l\V'.
- \ escape(b:csv_cmt[0], '\\').'\m\)'. '\&\%(\%<'.
- \ (a:stop+1). 'l\V'. escape(b:csv_cmt[0], '\\'). '\m\)'
- if search(commentpat)
+ if b:csv_cmt != ['','']
+ let commentpat = '\%(\%>'.(a:start-1).'l\V'.
+ \ escape(b:csv_cmt[0], '\\').'\m\)'. '\&\%(\%<'.
+ \ (a:stop+1). 'l\V'. escape(b:csv_cmt[0], '\\'). '\m\)'
+ endif
+ if !empty(commentpat) && search(commentpat)
for i in range(a:start, a:stop)
let content = getline(i)
if content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
@@ -1611,7 +1630,7 @@ fu! csv#MaxColumn(list) "{{{3
endtry
call add(result, str2float(nr))
endfor
- let result = sort(result, s:csv_numeric_sort ? 'n' : 'csv#CSVSortValues')
+ let result = sort(result, s:csv_numeric_sort ? 'N' : 'csv#CSVSortValues')
let ind = len(result) > 9 ? 9 : len(result)
if has_key(get(s:, 'additional', {}), 'distinct') && s:additional['distinct']
if exists("*uniq")
@@ -1672,7 +1691,7 @@ fu! csv#DoForEachColumn(start, stop, bang) range "{{{3
endif
let t = g:csv_convert
let line = getline(item)
- if line =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if b:csv_cmt!=['',''] && line =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
" Filter comments out
call add(result, line)
continue
@@ -1736,7 +1755,7 @@ fu! csv#FoldValue(lnum, filter) "{{{3
for item in values(a:filter)
" always fold comments away
let content = getline(a:lnum)
- if content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if b:csv_cmt != ['',''] && content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
return 1
elseif eval('content' . (item.match ? '!~' : '=~') . 'item.pat')
let result += 1
@@ -1898,7 +1917,7 @@ fu! csv#GetColumn(line, col, strip) "{{{3
" Return Column content at a:line, a:col
let a=getline(a:line)
" Filter comments out
- if a =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if csv#ValidComment() && a =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
return ''
endif
@@ -1979,7 +1998,7 @@ fu! csv#AnalyzeColumn(...) "{{{3
let res[item]+=1
endfor
- let max_items = reverse(sort(values(res), s:csv_numeric_sort ? 'n' : 'csv#CSVSortValues'))
+ let max_items = reverse(sort(values(res), s:csv_numeric_sort ? 'N' : 'csv#CSVSortValues'))
" What about the minimum 5 items?
let count_items = keys(res)
if len(max_items) > topn
@@ -2057,6 +2076,7 @@ fu! csv#InitCSVFixedWidth() "{{{3
endif
" Turn off syntax highlighting
syn clear
+ call csv#RemoveAutoHighlight()
let max_line = line('$') > 10 ? 10 : line('$')
let t = getline(1, max_line)
let max_len = max(map(t, 'len(split(v:val, ''\zs''))'))
@@ -2118,8 +2138,8 @@ fu! csv#InitCSVFixedWidth() "{{{3
endw
let b:csv_fixed_width_cols=[]
let tcc=0
- let b:csv_fixed_width_cols = sort(keys(Dict), s:csv_numeric_sort ? 'n' : 'csv#CSVSortValues')
- let b:csv_fixed_width = join(sort(keys(Dict), s:csv_numeric_sort ? 'n' : 'csv#CSVSortValues'), ',')
+ let b:csv_fixed_width_cols = sort(keys(Dict), s:csv_numeric_sort ? 'N' : 'csv#CSVSortValues')
+ let b:csv_fixed_width = join(sort(keys(Dict), s:csv_numeric_sort ? 'N' : 'csv#CSVSortValues'), ',')
call csv#Init(1, line('$'))
let &l:cc=_cc
@@ -2200,7 +2220,7 @@ fu! csv#CSVMappings() "{{{3
call csv#Map('nnoremap', '<C-Right>', ':<C-U>call csv#MoveCol(1, line("."))<CR>')
call csv#Map('nnoremap', 'L', ':<C-U>call csv#MoveCol(1, line("."))<CR>')
try
- if g:csv_bind_B == 1
+ if get(g:, 'csv_bind_B', 0) == 1
call csv#Map('nnoremap', 'B', ':<C-U>call csv#MoveCol(-1, line("."))<CR>')
else
call csv#Map('nnoremap', 'E', ':<C-U>call csv#MoveCol(-1, line("."))<CR>')
@@ -2431,7 +2451,7 @@ fu! csv#NewDelimiter(newdelimiter, firstl, lastl) "{{{3
let line=a:firstl
while line <= a:lastl
" Don't change delimiter for comments
- if getline(line) =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if csv#ValidComment() && getline(line) =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
let line+=1
continue
endif
@@ -2476,7 +2496,7 @@ fu! csv#DuplicateRows(columnlist) "{{{3
let i = 1
let content = getline(line)
" Skip comments
- if content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if csv#ValidComment() && content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
continue
endif
let cols = split(content, b:col. '\zs')
@@ -2532,7 +2552,11 @@ fu! csv#Transpose(line1, line2) "{{{3
let TrailingDelim = getline(1) =~ b:delimiter.'$'
endif
- let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if b:csv_cmt != ['','']
+ let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ else
+ let pat = ''
+ endif
try
let columns = csv#MaxColumns(a:line1)
@@ -2544,7 +2568,7 @@ fu! csv#Transpose(line1, line2) "{{{3
let matrix = []
for line in range(a:line1, a:line2)
" Filter comments out
- if getline(line) =~ pat
+ if !empty(pat) && getline(line) =~ pat
continue
endif
let r = []
@@ -3033,10 +3057,12 @@ fu! csv#SumCSVRow(line, nr) "{{{3
endif
let line=getline(ln)
" Filter comments out
- let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
- if line =~ pat
- call csv#Warn("Invalid count specified")
- return
+ if csv#ValidComment()
+ let pat = '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ if line =~ pat
+ call csv#Warn("Invalid count specified")
+ return
+ endif
endif
let func='csv#SumColumn'
let cells=split(line, b:col.'\zs')