table align - catch up to dev

This commit is contained in:
Steven Schmeiser
2018-07-09 13:51:18 -04:00
22 changed files with 3673 additions and 3099 deletions

View File

@ -1,35 +1,36 @@
" vim:tabstop=2:shiftwidth=2:expandtab:foldmethod=marker:textwidth=79
" vim:tabstop=2:shiftwidth=2:expandtab:textwidth=99
" Vimwiki autoload plugin file
" Desc: Tables
" Description: Tables
" | Easily | manageable | text | tables | ! |
" |--------|------------|-------|--------|---------|
" | Have | fun! | Drink | tea | Period. |
"
" Home: https://github.com/vimwiki/vimwiki/
" Load only once {{{
if exists("g:loaded_vimwiki_tbl_auto") || &cp
finish
endif
let g:loaded_vimwiki_tbl_auto = 1
"}}}
let s:textwidth = &tw
" Misc functions {{{
function! s:rxSep() "{{{
return g:vimwiki_rxTableSep
endfunction "}}}
function! s:rxSep()
return vimwiki#vars#get_syntaxlocal('rxTableSep')
endfunction
function! s:wide_len(str) "{{{
function! s:wide_len(str)
" vim73 has new function that gives correct string width.
if exists("*strdisplaywidth")
return strdisplaywidth(a:str)
endif
" get str display width in vim ver < 7.2
if !g:vimwiki_CJK_length
if !vimwiki#vars#get_global('CJK_length')
let ret = strlen(substitute(a:str, '.', 'x', 'g'))
else
let savemodified = &modified
@ -42,42 +43,54 @@ function! s:wide_len(str) "{{{
let &modified = savemodified
endif
return ret
endfunction "}}}
endfunction
function! s:cell_splitter() "{{{
function! s:cell_splitter()
return '\s*'.s:rxSep().'\s*'
endfunction "}}}
endfunction
function! s:sep_splitter() "{{{
function! s:sep_splitter()
return '-'.s:rxSep().'-'
endfunction "}}}
endfunction
function! s:is_table(line) "{{{
return s:is_separator(a:line) || (a:line !~# s:rxSep().s:rxSep() && a:line =~# '^\s*'.s:rxSep().'.\+'.s:rxSep().'\s*$')
endfunction "}}}
function! s:is_separator(line) "{{{
function! s:is_table(line)
return s:is_separator(a:line) ||
\ (a:line !~# s:rxSep().s:rxSep() && a:line =~# '^\s*'.s:rxSep().'.\+'.s:rxSep().'\s*$')
endfunction
function! s:is_separator(line)
return a:line =~# '^\s*'.s:rxSep().'\(:\=--\+:\='.s:rxSep().'\)\+\s*$'
endfunction "}}}
endfunction
function! s:is_separator_tail(line) "{{{
function! s:is_separator_tail(line)
return a:line =~# '^\{-1}\%(\s*\|-*\)\%('.s:rxSep().'-\+\)\+'.s:rxSep().'\s*$'
endfunction "}}}
endfunction
function! s:is_last_column(lnum, cnum) "{{{
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)
"echomsg "DEBUG is_last_column> ".(line =~# s:rxSep().'\s*$' && line !~# s:rxSep().'.*'.s:rxSep().'\s*$')
return line =~# s:rxSep().'\s*$' && line !~# s:rxSep().'.*'.s:rxSep().'\s*$'
endfunction "}}}
endfunction
function! s:is_first_column(lnum, cnum) "{{{
function! s:is_first_column(lnum, cnum)
let line = strpart(getline(a:lnum), 0, a:cnum - 1)
"echomsg "DEBUG is_first_column> ".(line =~# '^\s*'.s:rxSep() && line !~# '^\s*'.s:rxSep().'.*'.s:rxSep())
return line =~# '^\s*$' || (line =~# '^\s*'.s:rxSep() && line !~# '^\s*'.s:rxSep().'.*'.s:rxSep())
endfunction "}}}
return line =~# '^\s*$' ||
\ (line =~# '^\s*'.s:rxSep() && line !~# '^\s*'.s:rxSep().'.*'.s:rxSep())
endfunction
function! s:count_separators_up(lnum) "{{{
function! s:count_separators_up(lnum)
let lnum = a:lnum - 1
while lnum > 1
if !s:is_separator(getline(lnum))
@ -87,9 +100,10 @@ function! s:count_separators_up(lnum) "{{{
endwhile
return (a:lnum-lnum)
endfunction "}}}
endfunction
function! s:count_separators_down(lnum) "{{{
function! s:count_separators_down(lnum)
let lnum = a:lnum + 1
while lnum < line('$')
if !s:is_separator(getline(lnum))
@ -99,9 +113,10 @@ function! s:count_separators_down(lnum) "{{{
endwhile
return (lnum-a:lnum)
endfunction "}}}
endfunction
function! s:create_empty_row(cols) "{{{
function! s:create_empty_row(cols)
let row = s:rxSep()
let cell = " ".s:rxSep()
@ -110,9 +125,10 @@ function! s:create_empty_row(cols) "{{{
endfor
return row
endfunction "}}}
endfunction
function! s:create_row_sep(cols) "{{{
function! s:create_row_sep(cols)
let row = s:rxSep()
let cell = "---".s:rxSep()
@ -121,9 +137,10 @@ function! s:create_row_sep(cols) "{{{
endfor
return row
endfunction "}}}
endfunction
function! vimwiki#tbl#get_cells(line) "{{{
function! vimwiki#tbl#get_cells(line)
let result = []
let cell = ''
let quote = ''
@ -174,13 +191,15 @@ function! vimwiki#tbl#get_cells(line) "{{{
call add(result, vimwiki#u#trim(cell.quote, '|'))
endif
return result
endfunction "}}}
endfunction
function! s:col_count(lnum) "{{{
function! s:col_count(lnum)
return len(vimwiki#tbl#get_cells(getline(a:lnum)))
endfunction "}}}
endfunction
function! s:get_indent(lnum) "{{{
function! s:get_indent(lnum)
if !s:is_table(getline(a:lnum))
return
endif
@ -198,9 +217,10 @@ function! s:get_indent(lnum) "{{{
endwhile
return indent
endfunction " }}}
endfunction
function! s:get_rows(lnum) "{{{
function! s:get_rows(lnum)
if !s:is_table(getline(a:lnum))
return
endif
@ -232,14 +252,14 @@ function! s:get_rows(lnum) "{{{
endwhile
return upper_rows + lower_rows
endfunction "}}}
endfunction
function! s:get_cell_aligns(lnum) "{{{
function! s:get_cell_aligns(lnum)
let aligns = {}
for [lnum, row] in s:get_rows(a:lnum)
let found_separator = 0
if s:is_separator(row)
let found_separator = 1
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]
@ -261,9 +281,10 @@ function! s:get_cell_aligns(lnum) "{{{
endfor
endif
return aligns
endfunction "}}}
endfunction
function! s:get_cell_max_lens(lnum, ...) "{{{
function! s:get_cell_max_lens(lnum, ...)
let max_lens = {}
for [lnum, row] in s:get_rows(a:lnum)
if s:is_separator(row)
@ -280,9 +301,10 @@ function! s:get_cell_max_lens(lnum, ...) "{{{
endfor
endfor
return max_lens
endfunction "}}}
endfunction
function! s:get_aligned_rows(lnum, col1, col2) "{{{
function! s:get_aligned_rows(lnum, col1, col2)
let rows = s:get_rows(a:lnum)
let startlnum = rows[0][0]
let cells = []
@ -301,10 +323,11 @@ function! s:get_aligned_rows(lnum, col1, col2) "{{{
call add(result, [lnum, new_row])
endfor
return result
endfunction "}}}
endfunction
" Number of the current column. Starts from 0.
function! s:cur_column() "{{{
function! s:cur_column()
let line = getline('.')
if !s:is_table(line)
return -1
@ -321,12 +344,10 @@ function! s:cur_column() "{{{
endif
endwhile
return col
endfunction "}}}
endfunction
" }}}
" Format functions {{{
function! s:fmt_cell(cell, max_len, align) "{{{
function! s:fmt_cell(cell, max_len, align)
let cell = ' '.a:cell.' '
let diff = a:max_len - s:wide_len(a:cell)
@ -337,13 +358,14 @@ function! s:fmt_cell(cell, max_len, align) "{{{
let cell .= repeat(' ', diff)
elseif a:align == 'right'
let cell = repeat(' ',diff).cell
else
else
let cell = repeat(' ',diff/2).cell.repeat(' ',diff-diff/2)
endif
return cell
endfunction "}}}
endfunction
function! s:fmt_row(cells, max_lens, aligns, 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
@ -361,9 +383,10 @@ function! s:fmt_row(cells, max_lens, aligns, col1, col2) "{{{
let idx += 1
endwhile
return new_line
endfunction "}}}
endfunction
function! s:fmt_cell_sep(max_len, align) "{{{
function! s:fmt_cell_sep(max_len, align)
let cell = ''
if a:max_len == 0
let cell .= '-'
@ -377,9 +400,10 @@ function! s:fmt_cell_sep(max_len, align) "{{{
else
return ':'.cell.':'
endif
endfunction "}}}
endfunction
function! s:fmt_sep(max_lens, aligns, 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
@ -390,11 +414,10 @@ function! s:fmt_sep(max_lens, aligns, col1, col2) "{{{
let new_line .= s:fmt_cell_sep(a:max_lens[idx], a:aligns[idx]).s:rxSep()
endfor
return new_line
endfunction "}}}
"}}}
endfunction
" Keyboard functions "{{{
function! s:kbd_create_new_row(cols, goto_first) "{{{
function! s:kbd_create_new_row(cols, goto_first)
let cmd = "\<ESC>o".s:create_empty_row(a:cols)
let cmd .= "\<ESC>:call vimwiki#tbl#format(line('.'))\<CR>"
let cmd .= "\<ESC>0"
@ -407,26 +430,29 @@ function! s:kbd_create_new_row(cols, goto_first) "{{{
let cmd .= "a"
return cmd
endfunction "}}}
endfunction
function! s:kbd_goto_next_row() "{{{
function! s:kbd_goto_next_row()
let cmd = "\<ESC>j"
let cmd .= ":call search('.\\(".s:rxSep()."\\)', 'c', line('.'))\<CR>"
let cmd .= ":call search('\\(".s:rxSep()."\\)\\zs', 'bc', line('.'))\<CR>"
let cmd .= "a"
return cmd
endfunction "}}}
endfunction
function! s:kbd_goto_prev_row() "{{{
function! s:kbd_goto_prev_row()
let cmd = "\<ESC>k"
let cmd .= ":call search('.\\(".s:rxSep()."\\)', 'c', line('.'))\<CR>"
let cmd .= ":call search('\\(".s:rxSep()."\\)\\zs', 'bc', line('.'))\<CR>"
let cmd .= "a"
return cmd
endfunction "}}}
endfunction
" Used in s:kbd_goto_next_col
function! vimwiki#tbl#goto_next_col() "{{{
function! vimwiki#tbl#goto_next_col()
let curcol = virtcol('.')
let lnum = line('.')
let newcol = s:get_indent(lnum)
@ -439,9 +465,10 @@ function! vimwiki#tbl#goto_next_col() "{{{
endfor
let newcol += 2 " +2 == 1 separator + 1 space |<space
call vimwiki#u#cursor(lnum, newcol)
endfunction "}}}
endfunction
function! s:kbd_goto_next_col(jumpdown) "{{{
function! s:kbd_goto_next_col(jumpdown)
let cmd = "\<ESC>"
if a:jumpdown
let seps = s:count_separators_down(line('.'))
@ -449,10 +476,11 @@ function! s:kbd_goto_next_col(jumpdown) "{{{
endif
let cmd .= ":call vimwiki#tbl#goto_next_col()\<CR>a"
return cmd
endfunction "}}}
endfunction
" Used in s:kbd_goto_prev_col
function! vimwiki#tbl#goto_prev_col() "{{{
function! vimwiki#tbl#goto_prev_col()
let curcol = virtcol('.')
let lnum = line('.')
let newcol = s:get_indent(lnum)
@ -471,9 +499,10 @@ function! vimwiki#tbl#goto_prev_col() "{{{
endfor
let newcol += 2 " +2 == 1 separator + 1 space |<space
call vimwiki#u#cursor(lnum, newcol)
endfunction "}}}
endfunction
function! s:kbd_goto_prev_col(jumpup) "{{{
function! s:kbd_goto_prev_col(jumpup)
let cmd = "\<ESC>"
if a:jumpup
let seps = s:count_separators_up(line('.'))
@ -485,12 +514,10 @@ function! s:kbd_goto_prev_col(jumpup) "{{{
" let cmd .= "a"
"echomsg "DEBUG kbd_goto_prev_col> ".cmd
return cmd
endfunction "}}}
endfunction
"}}}
" Global functions {{{
function! vimwiki#tbl#kbd_cr() "{{{
function! vimwiki#tbl#kbd_cr()
let lnum = line('.')
if !s:is_table(getline(lnum))
return ""
@ -502,9 +529,10 @@ function! vimwiki#tbl#kbd_cr() "{{{
else
return s:kbd_goto_next_row()
endif
endfunction "}}}
endfunction
function! vimwiki#tbl#kbd_tab() "{{{
function! vimwiki#tbl#kbd_tab()
let lnum = line('.')
if !s:is_table(getline(lnum))
return "\<Tab>"
@ -518,9 +546,10 @@ function! vimwiki#tbl#kbd_tab() "{{{
return s:kbd_create_new_row(cols, 1)
endif
return s:kbd_goto_next_col(is_sep || last)
endfunction "}}}
endfunction
function! vimwiki#tbl#kbd_shift_tab() "{{{
function! vimwiki#tbl#kbd_shift_tab()
let lnum = line('.')
if !s:is_table(getline(lnum))
return "\<S-Tab>"
@ -533,9 +562,10 @@ function! vimwiki#tbl#kbd_shift_tab() "{{{
return ""
endif
return s:kbd_goto_prev_col(is_sep || first)
endfunction "}}}
endfunction
function! vimwiki#tbl#format(lnum, ...) "{{{
function! vimwiki#tbl#format(lnum, ...)
if !(&filetype ==? 'vimwiki')
return
endif
@ -563,11 +593,12 @@ function! vimwiki#tbl#format(lnum, ...) "{{{
let row = indentstring.row
call setline(lnum, row)
endfor
let &tw = s:textwidth
endfunction "}}}
function! vimwiki#tbl#create(...) "{{{
let &tw = s:textwidth
endfunction
function! vimwiki#tbl#create(...)
if a:0 > 1
let cols = a:1
let rows = a:2
@ -598,19 +629,21 @@ function! vimwiki#tbl#create(...) "{{{
for r in range(rows - 1)
call add(lines, row)
endfor
call append(line('.'), lines)
endfunction "}}}
function! vimwiki#tbl#align_or_cmd(cmd) "{{{
call append(line('.'), lines)
endfunction
function! vimwiki#tbl#align_or_cmd(cmd)
if s:is_table(getline('.'))
call vimwiki#tbl#format(line('.'))
else
exe 'normal! '.a:cmd
endif
endfunction "}}}
endfunction
function! vimwiki#tbl#reset_tw(lnum) "{{{
function! vimwiki#tbl#reset_tw(lnum)
if !(&filetype ==? 'vimwiki')
return
endif
@ -618,14 +651,14 @@ function! vimwiki#tbl#reset_tw(lnum) "{{{
if !s:is_table(line)
return
endif
let s:textwidth = &tw
let &tw = 0
endfunction "}}}
endfunction
" TODO: move_column_left and move_column_right are good candidates to be
" refactored.
function! vimwiki#tbl#move_column_left() "{{{
" TODO: move_column_left and move_column_right are good candidates to be refactored.
function! vimwiki#tbl#move_column_left()
"echomsg "DEBUG move_column_left: "
@ -641,7 +674,7 @@ function! vimwiki#tbl#move_column_left() "{{{
endif
if cur_col > 0
call vimwiki#tbl#format(line('.'), cur_col-1, cur_col)
call vimwiki#tbl#format(line('.'), cur_col-1, cur_col)
call cursor(line('.'), 1)
let sep = '\('.s:rxSep().'\).\zs'
@ -651,16 +684,16 @@ function! vimwiki#tbl#move_column_left() "{{{
let mpos = match(line, sep, mpos+1)
if mpos != -1
let col += 1
else
else
break
endif
endwhile
endif
endfunction
endfunction "}}}
function! vimwiki#tbl#move_column_right() "{{{
function! vimwiki#tbl#move_column_right()
let line = getline('.')
@ -674,7 +707,7 @@ function! vimwiki#tbl#move_column_right() "{{{
endif
if cur_col < s:col_count(line('.'))-1
call vimwiki#tbl#format(line('.'), cur_col, cur_col+1)
call vimwiki#tbl#format(line('.'), cur_col, cur_col+1)
call cursor(line('.'), 1)
let sep = '\('.s:rxSep().'\).\zs'
@ -684,33 +717,35 @@ function! vimwiki#tbl#move_column_right() "{{{
let mpos = match(line, sep, mpos+1)
if mpos != -1
let col += 1
else
else
break
endif
endwhile
endif
endfunction
endfunction "}}}
function! vimwiki#tbl#get_rows(lnum) "{{{
function! vimwiki#tbl#get_rows(lnum)
return s:get_rows(a:lnum)
endfunction "}}}
endfunction
function! vimwiki#tbl#is_table(line) "{{{
function! vimwiki#tbl#is_table(line)
return s:is_table(a:line)
endfunction "}}}
endfunction
function! vimwiki#tbl#is_separator(line) "{{{
function! vimwiki#tbl#is_separator(line)
return s:is_separator(a:line)
endfunction "}}}
endfunction
function! vimwiki#tbl#cell_splitter() "{{{
function! vimwiki#tbl#cell_splitter()
return s:cell_splitter()
endfunction "}}}
endfunction
function! vimwiki#tbl#sep_splitter() "{{{
function! vimwiki#tbl#sep_splitter()
return s:sep_splitter()
endfunction "}}}
endfunction
"}}}