This commit is contained in:
Henry Qin 2019-03-14 17:32:53 -07:00
commit abd12d4479
2 changed files with 71 additions and 14 deletions

View File

@ -63,7 +63,7 @@ endfunction
function! s:is_separator(line) function! s:is_separator(line)
return a:line =~# '^\s*'.s:rxSep().'\(--\+'.s:rxSep().'\)\+\s*$' return a:line =~# '^\s*'.s:rxSep().'\(:\=--\+:\='.s:rxSep().'\)\+\s*$'
endfunction endfunction
@ -72,6 +72,11 @@ function! s:is_separator_tail(line)
endfunction endfunction
function! s:is_last_column(lnum, cnum)
return a:line =~# '^\{-1}\%(\s*\|-*\)\%('.s:rxSep().'-\+\)\+'.s:rxSep().'\s*$'
endfunction
function! s:is_last_column(lnum, cnum) function! s:is_last_column(lnum, cnum)
let line = strpart(getline(a:lnum), a:cnum - 1) let line = strpart(getline(a:lnum), a:cnum - 1)
return line =~# s:rxSep().'\s*$' && line !~# s:rxSep().'.*'.s:rxSep().'\s*$' return line =~# s:rxSep().'\s*$' && line !~# s:rxSep().'.*'.s:rxSep().'\s*$'
@ -250,6 +255,35 @@ function! s:get_rows(lnum)
endfunction endfunction
function! s:get_cell_aligns(lnum)
let aligns = {}
for [lnum, row] in s:get_rows(a:lnum)
let found_separator = s:is_separator(row)
if found_separator
let cells = vimwiki#tbl#get_cells(row)
for idx in range(len(cells))
let cell = cells[idx]
if cell =~# '^--\+:'
let aligns[idx] = 'right'
elseif cell =~# '^:--\+:'
let aligns[idx] = 'center'
else
let aligns[idx] = 'left'
endif
endfor
return aligns
endif
endfor
if !found_separator
let cells = vimwiki#tbl#get_cells(row)
for idx in range(len(cells))
let aligns[idx] = 'left'
endfor
endif
return aligns
endfunction
function! s:get_cell_max_lens(lnum, ...) function! s:get_cell_max_lens(lnum, ...)
let max_lens = {} let max_lens = {}
for [lnum, row] in s:get_rows(a:lnum) for [lnum, row] in s:get_rows(a:lnum)
@ -278,12 +312,13 @@ function! s:get_aligned_rows(lnum, col1, col2)
call add(cells, vimwiki#tbl#get_cells(row)) call add(cells, vimwiki#tbl#get_cells(row))
endfor endfor
let max_lens = s:get_cell_max_lens(a:lnum, cells, startlnum) let max_lens = s:get_cell_max_lens(a:lnum, cells, startlnum)
let aligns = s:get_cell_aligns(a:lnum)
let result = [] let result = []
for [lnum, row] in rows for [lnum, row] in rows
if s:is_separator(row) if s:is_separator(row)
let new_row = s:fmt_sep(max_lens, a:col1, a:col2) let new_row = s:fmt_sep(max_lens, aligns, a:col1, a:col2)
else else
let new_row = s:fmt_row(cells[lnum - startlnum], max_lens, a:col1, a:col2) let new_row = s:fmt_row(cells[lnum - startlnum], max_lens, aligns, a:col1, a:col2)
endif endif
call add(result, [lnum, new_row]) call add(result, [lnum, new_row])
endfor endfor
@ -312,20 +347,25 @@ function! s:cur_column()
endfunction endfunction
function! s:fmt_cell(cell, max_len) function! s:fmt_cell(cell, max_len, align)
let cell = ' '.a:cell.' ' let cell = ' '.a:cell.' '
let diff = a:max_len - s:wide_len(a:cell) let diff = a:max_len - s:wide_len(a:cell)
if diff == 0 && empty(a:cell) if diff == 0 && empty(a:cell)
let diff = 1 let diff = 1
endif endif
if a:align == 'left'
let cell .= repeat(' ', diff) let cell .= repeat(' ', diff)
elseif a:align == 'right'
let cell = repeat(' ',diff).cell
else
let cell = repeat(' ',diff/2).cell.repeat(' ',diff-diff/2)
endif
return cell return cell
endfunction endfunction
function! s:fmt_row(cells, max_lens, col1, col2) function! s:fmt_row(cells, max_lens, aligns, col1, col2)
let new_line = s:rxSep() let new_line = s:rxSep()
for idx in range(len(a:cells)) for idx in range(len(a:cells))
if idx == a:col1 if idx == a:col1
@ -334,28 +374,36 @@ function! s:fmt_row(cells, max_lens, col1, col2)
let idx = a:col1 let idx = a:col1
endif endif
let value = a:cells[idx] let value = a:cells[idx]
let new_line .= s:fmt_cell(value, a:max_lens[idx]).s:rxSep() let new_line .= s:fmt_cell(value, a:max_lens[idx], a:aligns[idx]).s:rxSep()
endfor endfor
let idx = len(a:cells) let idx = len(a:cells)
while idx < len(a:max_lens) while idx < len(a:max_lens)
let new_line .= s:fmt_cell('', a:max_lens[idx]).s:rxSep() let new_line .= s:fmt_cell('', a:max_lens[idx], a:aligns[idx]).s:rxSep()
let idx += 1 let idx += 1
endwhile endwhile
return new_line return new_line
endfunction endfunction
function! s:fmt_cell_sep(max_len) function! s:fmt_cell_sep(max_len, align)
let cell = ''
if a:max_len == 0 if a:max_len == 0
return repeat('-', 3) let cell .= '-'
else else
return repeat('-', a:max_len+2) let cell .= repeat('-', a:max_len)
endif
if a:align == 'right'
return cell.'-:'
elseif a:align == 'left'
return cell.'--'
else
return ':'.cell.':'
endif endif
endfunction endfunction
function! s:fmt_sep(max_lens, col1, col2) function! s:fmt_sep(max_lens, aligns, col1, col2)
let new_line = s:rxSep() let new_line = s:rxSep()
for idx in range(len(a:max_lens)) for idx in range(len(a:max_lens))
if idx == a:col1 if idx == a:col1
@ -363,7 +411,7 @@ function! s:fmt_sep(max_lens, col1, col2)
elseif idx == a:col2 elseif idx == a:col2
let idx = a:col1 let idx = a:col1
endif endif
let new_line .= s:fmt_cell_sep(a:max_lens[idx]).s:rxSep() let new_line .= s:fmt_cell_sep(a:max_lens[idx], a:aligns[idx]).s:rxSep()
endfor endfor
return new_line return new_line
endfunction endfunction

View File

@ -1715,6 +1715,15 @@ values: >
To indent table indent the first row. Then format it with 'gqq'. To indent table indent the first row. Then format it with 'gqq'.
You can specify the type of horizontal alignment for columns in the separator
using the ':' character. The default is left-align. >
| Date | Item | Price |
|------------|:------:|--------:|
| yest | Coffee | $15.00 |
| 2017-02-13 | Tea | $2.10 |
| 2017-03-14 | Cake | $143.12 |
<
============================================================================== ==============================================================================
10. Diary *vimwiki-diary* 10. Diary *vimwiki-diary*