/
csv.vim
70 lines (63 loc) · 2.24 KB
/
csv.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
if exists('g:loaded_csv') && g:loaded_csv
finish
endif
let g:loaded_csv = 1
let s:cpo_save = &cpo
set cpo&vim
if exists("g:csv_autocmd_arrange") &&
\ !exists("#CSV_Edit#BufReadPost")
aug CSV_Editing
au!
au BufReadPost,BufWritePost *.csv,*.dat,*.tsv,*.tab :ru! ftplugin/csv.vim | exe ":sil! InitCSV" | exe ":sil! %ArrangeCol" | setl noro
au BufWritePre *.csv,*.dat,*.tsv,*.tab :%UnArrangeCol
aug end
elseif exists("#CSV_Edit#BufReadPost")
aug CSV_Edit
au!
aug end
aug! CSV_Edit
endif
com! -range -bang CSVTable call <sid>Table(<bang>0, <line1>, <line2>)
fu! <sid>Table(bang, line1, line2)
" save and restore some options
if has("conceal")
let _a = [ &l:lz, &l:syntax, &l:ft, &l:sol, &l:tw, &l:wrap, &l:cole, &l:cocu, &l:fen, &l:fdm, &l:fdl, &l:fdc, &l:fml, &l:fdt]
else
let _a = [ &l:lz, &l:syntax, &l:ft, &l:sol, &l:tw, &l:wrap, &l:fen, &l:fdm, &l:fdl, &l:fdc, &l:fml, &l:fdt]
endif
let _b = winsaveview()
" try to guess the delimiter from the specified region, therefore, we need
" to initialize the plugin to inspect only those lines
let [ b:csv_start, b:csv_end ] = [ a:line1, a:line2 ]
" Reset b:did_ftplugin just to be sure
unlet! b:did_ftplugin
setl ft=csv lz
" get indent
let indent = matchstr(getline(a:line1), '^\s\+')
exe printf(':sil %d,%ds/^\s\+//e', a:line1, a:line2)
let last = line('$')
try
let b:csv_list=getline(a:line1, a:line2)
call filter(b:csv_list, '!empty(v:val)')
call map(b:csv_list, 'split(v:val, b:col.''\zs'')')
if exists(":CSVTabularize")
exe printf("%d,%dCSVTabularize%s", a:line1, a:line2, empty(a:bang) ? '' : '!')
endif
unlet! b:col_width b:csv_list
catch
finally
if !empty(indent)
" Added one line above a:line1 and several lines below, so need to
" correct the range
exe printf(':sil %d,%ds/^/%s/e', (a:line1 - 1), (a:line2 + line('$') - last), indent)
endif
if has("conceal")
let [ &l:lz, &l:syntax, &l:ft, &l:sol, &l:tw, &l:wrap, &l:cole, &l:cocu, &l:fen, &l:fdm, &l:fdl, &l:fdc, &l:fml, &l:fdt] = _a
else
let [ &l:lz, &l:syntax, &l:ft, &l:sol, &l:tw, &l:wrap, &l:fen, &l:fdm, &l:fdl, &l:fdc, &l:fml, &l:fdt] = _a
endif
call winrestview(_b)
endtry
endfu
let &cpo = s:cpo_save
unlet s:cpo_save