commit abd12d44794d66007d6b7e5b568e714120d66d68
parent 54d1a997d91198ddf9622feedc9f0add4e76e169
Author: Henry Qin <root@hq6.me>
Date: Thu, 14 Mar 2019 17:32:53 -0700
Merge PR #377 from https://github.com/schmeisers/vimwiki
Diffstat:
2 files changed, 71 insertions(+), 14 deletions(-)
diff --git a/autoload/vimwiki/tbl.vim b/autoload/vimwiki/tbl.vim
@@ -63,7 +63,7 @@ endfunction
function! s:is_separator(line)
- return a:line =~# '^\s*'.s:rxSep().'\(--\+'.s:rxSep().'\)\+\s*$'
+ return a:line =~# '^\s*'.s:rxSep().'\(:\=--\+:\='.s:rxSep().'\)\+\s*$'
endfunction
@@ -73,6 +73,11 @@ 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)
let line = strpart(getline(a:lnum), a:cnum - 1)
return line =~# s:rxSep().'\s*$' && line !~# s:rxSep().'.*'.s:rxSep().'\s*$'
endfunction
@@ -250,6 +255,35 @@ function! s:get_rows(lnum)
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, ...)
let max_lens = {}
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))
endfor
let max_lens = s:get_cell_max_lens(a:lnum, cells, startlnum)
+ let aligns = s:get_cell_aligns(a:lnum)
let result = []
for [lnum, row] in rows
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
- 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
call add(result, [lnum, new_row])
endfor
@@ -312,20 +347,25 @@ function! s:cur_column()
endfunction
-function! s:fmt_cell(cell, max_len)
+function! s:fmt_cell(cell, max_len, align)
let cell = ' '.a:cell.' '
let diff = a:max_len - s:wide_len(a:cell)
if diff == 0 && empty(a:cell)
let diff = 1
endif
-
- let cell .= repeat(' ', diff)
+ if a:align == 'left'
+ 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
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()
for idx in range(len(a:cells))
if idx == a:col1
@@ -334,28 +374,36 @@ function! s:fmt_row(cells, max_lens, col1, col2)
let idx = a:col1
endif
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
let idx = len(a:cells)
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
endwhile
return new_line
endfunction
-function! s:fmt_cell_sep(max_len)
+function! s:fmt_cell_sep(max_len, align)
+ let cell = ''
if a:max_len == 0
- return repeat('-', 3)
+ let cell .= '-'
+ else
+ let cell .= repeat('-', a:max_len)
+ endif
+ if a:align == 'right'
+ return cell.'-:'
+ elseif a:align == 'left'
+ return cell.'--'
else
- return repeat('-', a:max_len+2)
+ return ':'.cell.':'
endif
endfunction
-function! s:fmt_sep(max_lens, col1, col2)
+function! s:fmt_sep(max_lens, aligns, col1, col2)
let new_line = s:rxSep()
for idx in range(len(a:max_lens))
if idx == a:col1
@@ -363,7 +411,7 @@ function! s:fmt_sep(max_lens, col1, col2)
elseif idx == a:col2
let idx = a:col1
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
return new_line
endfunction
diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt
@@ -1715,6 +1715,15 @@ values: >
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*