|
|
|
|
@@ -1,10 +1,10 @@
|
|
|
|
|
" ============================== Header ==================================={{{
|
|
|
|
|
" ============================== Header ======================================
|
|
|
|
|
" File: autoload/tablemode.vim
|
|
|
|
|
" Description: Table mode for vim for creating neat tables.
|
|
|
|
|
" Author: Dhruva Sagar <http://dhruvasagar.com/>
|
|
|
|
|
" License: MIT (http://www.opensource.org/licenses/MIT)
|
|
|
|
|
" Website: http://github.com/dhruvasagar/vim-table-mode
|
|
|
|
|
" Version: 3.0
|
|
|
|
|
" Version: 3.1
|
|
|
|
|
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
|
|
|
|
|
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and
|
|
|
|
|
" uses a small amount of code from it.
|
|
|
|
|
@@ -17,7 +17,7 @@
|
|
|
|
|
" of any kind, either expressed or implied. In no event will
|
|
|
|
|
" the copyright holder be liable for any damamges resulting
|
|
|
|
|
" from the use of this software.
|
|
|
|
|
" ==========================================================================}}}
|
|
|
|
|
" =============================================================================
|
|
|
|
|
|
|
|
|
|
" Private Functions {{{1
|
|
|
|
|
|
|
|
|
|
@@ -25,30 +25,25 @@ if exists('g:autoloaded_table_mode') "{{{2
|
|
|
|
|
finish
|
|
|
|
|
endif
|
|
|
|
|
let g:autoloaded_table_mode = 1
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:throw(string) abort "{{{2
|
|
|
|
|
let v:errmsg = 'table-mode: ' . a:string
|
|
|
|
|
throw v:errmsg
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:sub(str,pat,rep) abort "{{{2
|
|
|
|
|
return substitute(a:str,'\v\C'.a:pat,a:rep,'')
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:gsub(str,pat,rep) abort "{{{2
|
|
|
|
|
return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:SetBufferOptDefault(opt, val) "{{{2
|
|
|
|
|
if !exists('b:' . a:opt)
|
|
|
|
|
let b:{a:opt} = a:val
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:Line(line) "{{{2
|
|
|
|
|
if type(a:line) == type('')
|
|
|
|
|
@@ -57,19 +52,16 @@ function! s:Line(line) "{{{2
|
|
|
|
|
return a:line
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
" function! s:Strlen(text) - Count multibyte characters accurately {{{2
|
|
|
|
|
" See :h strlen() for more details
|
|
|
|
|
function! s:Strlen(text)
|
|
|
|
|
return strlen(substitute(a:text, '.', 'x', 'g'))
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:Strip(string) "{{{2
|
|
|
|
|
return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$')
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:Sum(list) "{{{2
|
|
|
|
|
let result = 0.0
|
|
|
|
|
@@ -84,12 +76,10 @@ function! s:Sum(list) "{{{2
|
|
|
|
|
endfor
|
|
|
|
|
return result
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:Average(list) "{{{2
|
|
|
|
|
return s:Sum(a:list)/len(a:list)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetCommentStart() "{{{2
|
|
|
|
|
let cstring = &commentstring
|
|
|
|
|
@@ -99,7 +89,6 @@ function! s:GetCommentStart() "{{{2
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetCommentEnd() "{{{2
|
|
|
|
|
let cstring = &commentstring
|
|
|
|
|
@@ -114,7 +103,6 @@ function! s:GetCommentEnd() "{{{2
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:StartExpr() "{{{2
|
|
|
|
|
let cstart = s:GetCommentStart()
|
|
|
|
|
@@ -124,7 +112,10 @@ function! s:StartExpr() "{{{2
|
|
|
|
|
return '^\s*'
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:HeaderBorderExpr() "{{{2
|
|
|
|
|
return '^\s*' . g:table_mode_corner . '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' . g:table_mode_corner . '$'
|
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
function! s:EndExpr() "{{{2
|
|
|
|
|
let cend = s:GetCommentEnd()
|
|
|
|
|
@@ -134,7 +125,6 @@ function! s:EndExpr() "{{{2
|
|
|
|
|
return '\s*$'
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:StartCommentExpr() "{{{2
|
|
|
|
|
let cstartexpr = s:GetCommentStart()
|
|
|
|
|
@@ -144,7 +134,6 @@ function! s:StartCommentExpr() "{{{2
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:EndCommentExpr() "{{{2
|
|
|
|
|
let cendexpr = s:GetCommentEnd()
|
|
|
|
|
@@ -154,7 +143,6 @@ function! s:EndCommentExpr() "{{{2
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:IsTableModeActive() "{{{2
|
|
|
|
|
if g:table_mode_always_active | return 1 | endif
|
|
|
|
|
@@ -162,12 +150,11 @@ function! s:IsTableModeActive() "{{{2
|
|
|
|
|
call s:SetBufferOptDefault('table_mode_active', 0)
|
|
|
|
|
return b:table_mode_active
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:RowGap() "{{{2
|
|
|
|
|
return g:table_mode_border ? 2 : 1
|
|
|
|
|
" Getting rid of borders, so the row gap is now just 1
|
|
|
|
|
return 1
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:ToggleMapping() "{{{2
|
|
|
|
|
if exists('b:table_mode_active') && b:table_mode_active
|
|
|
|
|
@@ -177,64 +164,41 @@ function! s:ToggleMapping() "{{{2
|
|
|
|
|
|
|
|
|
|
execute "inoremap <silent> <buffer> " . b:table_mode_separator_map . ' ' .
|
|
|
|
|
\ b:table_mode_separator_map . "<Esc>:call tablemode#TableizeInsertMode()<CR>a"
|
|
|
|
|
|
|
|
|
|
execute "inoreabbrev <silent> <buffer> " . g:table_mode_corner .
|
|
|
|
|
\ g:table_mode_fillchar . "<Esc>:call tablemode#AddHeaderBorder('.')<CR>A"
|
|
|
|
|
else
|
|
|
|
|
execute "iunmap <silent> <buffer> " . b:table_mode_separator_map
|
|
|
|
|
|
|
|
|
|
execute "iunabbrev <silent> <buffer> " . g:table_mode_corner . g:table_mode_fillchar
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:SetActive(bool) "{{{2
|
|
|
|
|
let b:table_mode_active = a:bool
|
|
|
|
|
call s:ToggleMapping()
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GenerateBorder(line) "{{{2
|
|
|
|
|
let line_val = getline(a:line)
|
|
|
|
|
let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g')
|
|
|
|
|
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
|
|
|
|
|
|
|
|
|
|
let cstartexpr = s:StartCommentExpr()
|
|
|
|
|
if s:Strlen(cstartexpr) > 0 && getline(a:line) =~# cstartexpr
|
|
|
|
|
let sce = matchstr(line_val, s:StartCommentExpr())
|
|
|
|
|
let ece = matchstr(line_val, s:EndCommentExpr())
|
|
|
|
|
return sce . border . ece
|
|
|
|
|
elseif getline(a:line) =~# s:StartExpr()
|
|
|
|
|
let indent = matchstr(line_val, s:StartExpr())
|
|
|
|
|
return indent . border
|
|
|
|
|
else
|
|
|
|
|
return border
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:UpdateLineBorder(line) "{{{2
|
|
|
|
|
function! s:GenerateHeaderBorder(line) "{{{2
|
|
|
|
|
let line = s:Line(a:line)
|
|
|
|
|
if tablemode#IsATableRow(line - s:RowGap())
|
|
|
|
|
let line_val = getline(line - s:RowGap())
|
|
|
|
|
let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g')
|
|
|
|
|
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
|
|
|
|
|
|
|
|
|
|
let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner .
|
|
|
|
|
\ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?' . s:EndExpr()
|
|
|
|
|
|
|
|
|
|
let rowgap = s:RowGap()
|
|
|
|
|
let border = s:GenerateBorder(line)
|
|
|
|
|
|
|
|
|
|
let [prev_line, next_line] = [getline(line-1), getline(line+1)]
|
|
|
|
|
if next_line =~# hf
|
|
|
|
|
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line + rowgap)) || !tablemode#IsATableRow(line + rowgap)
|
|
|
|
|
call setline(line+1, border)
|
|
|
|
|
let cstartexpr = s:StartCommentExpr()
|
|
|
|
|
if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
|
|
|
|
|
let sce = matchstr(line_val, s:StartCommentExpr())
|
|
|
|
|
let ece = matchstr(line_val, s:EndCommentExpr())
|
|
|
|
|
return sce . border . ece
|
|
|
|
|
elseif getline(line) =~# s:StartExpr()
|
|
|
|
|
let indent = matchstr(line_val, s:StartExpr())
|
|
|
|
|
return indent . border
|
|
|
|
|
else
|
|
|
|
|
return border
|
|
|
|
|
endif
|
|
|
|
|
else
|
|
|
|
|
call append(line, border)
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
if prev_line =~# hf
|
|
|
|
|
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line - rowgap)) || !tablemode#IsATableRow(line - rowgap)
|
|
|
|
|
call setline(line-1, border)
|
|
|
|
|
endif
|
|
|
|
|
else
|
|
|
|
|
call append(line-1, border)
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
|
|
|
|
|
let delim = g:table_mode_delimiter
|
|
|
|
|
@@ -242,31 +206,34 @@ function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
|
|
|
|
|
if delim ==# ','
|
|
|
|
|
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/g"
|
|
|
|
|
endif
|
|
|
|
|
silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze.\|' . delim . '\|.\zs\ze' . s:EndExpr() . '/' .
|
|
|
|
|
|
|
|
|
|
let [cstart, cend] = [s:GetCommentStart(), s:GetCommentEnd()]
|
|
|
|
|
let [match_char_start, match_char_end] = ['.', '.']
|
|
|
|
|
if s:Strlen(cend) > 0 | let match_char_end = '[^' . cend . ']' | endif
|
|
|
|
|
if s:Strlen(cstart) > 0 | let match_char_start = '[^' . cstart . ']' | endif
|
|
|
|
|
|
|
|
|
|
silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze' . match_char_start .
|
|
|
|
|
\ '\|' . delim . '\|' . match_char_end . '\zs\ze' . s:EndExpr() . '/' .
|
|
|
|
|
\ g:table_mode_separator . '/g'
|
|
|
|
|
|
|
|
|
|
if delim ==# ','
|
|
|
|
|
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g"
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:Tableizeline(line, ...) "{{{2
|
|
|
|
|
let delim = g:table_mode_delimiter
|
|
|
|
|
if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif
|
|
|
|
|
call s:ConvertDelimiterToSeparator(a:line, delim)
|
|
|
|
|
if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:IsFirstCell() "{{{2
|
|
|
|
|
return tablemode#ColumnNr('.') ==# 1
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:IsLastCell() "{{{2
|
|
|
|
|
return tablemode#ColumnNr('.') ==# tablemode#ColumnCount('.')
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetFirstRow(line) "{{{2
|
|
|
|
|
if tablemode#IsATableRow(a:line)
|
|
|
|
|
@@ -279,14 +246,12 @@ function! s:GetFirstRow(line) "{{{2
|
|
|
|
|
return line
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:MoveToFirstRow() "{{{2
|
|
|
|
|
if tablemode#IsATableRow('.')
|
|
|
|
|
call cursor(s:GetFirstRow('.'), col('.'))
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetLastRow(line) "{{{2
|
|
|
|
|
if tablemode#IsATableRow(a:line)
|
|
|
|
|
@@ -299,14 +264,12 @@ function! s:GetLastRow(line) "{{{2
|
|
|
|
|
return line
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:MoveToLastRow() "{{{2
|
|
|
|
|
if tablemode#IsATableRow('.')
|
|
|
|
|
call cursor(s:GetLastRow('.'), col('.'))
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:MoveToStartOfCell() "{{{2
|
|
|
|
|
if getline('.')[col('.')-1] ==# g:table_mode_separator && !s:IsLastCell()
|
|
|
|
|
@@ -315,7 +278,6 @@ function! s:MoveToStartOfCell() "{{{2
|
|
|
|
|
execute 'normal! F' . g:table_mode_separator . '2l'
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
" function! s:GetCells() - Function to get values of cells in a table {{{2
|
|
|
|
|
" s:GetCells(row) - Get values of all cells in a row as a List.
|
|
|
|
|
@@ -338,7 +300,7 @@ function! s:GetCells(line, ...) abort
|
|
|
|
|
let line = s:GetFirstRow(line)
|
|
|
|
|
while tablemode#IsATableRow(line)
|
|
|
|
|
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
|
|
|
|
|
call add(values, s:Strip(split(row_line, g:table_mode_separator)[colm>0?colm-1:colm]))
|
|
|
|
|
call add(values, s:Strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, '')))
|
|
|
|
|
let line = line + s:RowGap()
|
|
|
|
|
endwhile
|
|
|
|
|
return values
|
|
|
|
|
@@ -353,12 +315,12 @@ function! s:GetCells(line, ...) abort
|
|
|
|
|
if colm == 0
|
|
|
|
|
return map(split(row_line, g:table_mode_separator), 's:Strip(v:val)')
|
|
|
|
|
else
|
|
|
|
|
return s:Strip(split(row_line, g:table_mode_separator)[colm>0?colm-1:colm])
|
|
|
|
|
let split_line = split(row_line, g:table_mode_separator)
|
|
|
|
|
return s:Strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, ''))
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetCell(...) "{{{2
|
|
|
|
|
if a:0 == 0
|
|
|
|
|
@@ -369,7 +331,6 @@ function! s:GetCell(...) "{{{2
|
|
|
|
|
|
|
|
|
|
return s:GetCells('.', row, col)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:SetCell(val, ...) abort "{{{2
|
|
|
|
|
if a:0 == 0
|
|
|
|
|
@@ -385,6 +346,7 @@ function! s:SetCell(val, ...) abort "{{{2
|
|
|
|
|
let line_val = getline(line)
|
|
|
|
|
let cstartexpr = s:StartCommentExpr()
|
|
|
|
|
let values = split(getline(line)[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator)
|
|
|
|
|
if len(values) < colm | return | endif
|
|
|
|
|
let values[colm-1] = a:val
|
|
|
|
|
let line_value = g:table_mode_separator . join(values, g:table_mode_separator) . g:table_mode_separator
|
|
|
|
|
if s:Strlen(cstartexpr) > 0 && line_val =~# cstartexpr
|
|
|
|
|
@@ -396,33 +358,28 @@ function! s:SetCell(val, ...) abort "{{{2
|
|
|
|
|
call tablemode#TableRealign(line)
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetRow(row, ...) abort "{{{2
|
|
|
|
|
let line = a:0 < 1 ? '.' : a:1
|
|
|
|
|
return s:GetCells(line, a:row)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetRowColumn(col, ...) abort "{{{2
|
|
|
|
|
let line = a:0 < 1 ? '.' : a:1
|
|
|
|
|
let row = tablemode#RowNr('.')
|
|
|
|
|
return s:GetCells(line, row, a:col)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetColumn(col, ...) abort "{{{2
|
|
|
|
|
let line = a:0 < 1 ? '.' : a:1
|
|
|
|
|
return s:GetCells(line, 0, a:col)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:GetColumnRow(row, ...) abort "{{{2
|
|
|
|
|
let line = a:0 < 1 ? '.' : a:1
|
|
|
|
|
let col = tablemode#ColumnNr('.')
|
|
|
|
|
return s:GetCells(line, a:row, col)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! s:ParseRange(range, ...) "{{{2
|
|
|
|
|
if a:0 < 1
|
|
|
|
|
@@ -454,7 +411,6 @@ function! s:ParseRange(range, ...) "{{{2
|
|
|
|
|
|
|
|
|
|
return [row1, col1, row2, col2]
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
" function! s:GetCellRange(range, ...) {{{2
|
|
|
|
|
" range: A string representing range of cells.
|
|
|
|
|
@@ -498,7 +454,6 @@ function! s:GetCellRange(range, ...) abort
|
|
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
" Borrowed from Tabular : {{{2
|
|
|
|
|
|
|
|
|
|
@@ -507,7 +462,6 @@ endfunction
|
|
|
|
|
function! s:StripTrailingSpaces(string)
|
|
|
|
|
return matchstr(a:string, '^.\{-}\ze\s*$')
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}3
|
|
|
|
|
|
|
|
|
|
function! s:Padding(string, length, where) "{{{3
|
|
|
|
|
let gap_length = a:length - s:Strlen(a:string)
|
|
|
|
|
@@ -521,7 +475,6 @@ function! s:Padding(string, length, where) "{{{3
|
|
|
|
|
return repeat(" ", left) . a:string . repeat(" ", right)
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}3
|
|
|
|
|
|
|
|
|
|
" function! s:Split() - Split a string into fields and delimiters {{{3
|
|
|
|
|
" Like split(), but include the delimiters as elements
|
|
|
|
|
@@ -565,7 +518,6 @@ function! s:Split(string, delim)
|
|
|
|
|
|
|
|
|
|
return rv
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}3
|
|
|
|
|
|
|
|
|
|
function! s:Align(lines) "{{{3
|
|
|
|
|
let lines = map(a:lines, 's:Split(v:val, g:table_mode_separator)')
|
|
|
|
|
@@ -614,23 +566,18 @@ function! s:Align(lines) "{{{3
|
|
|
|
|
|
|
|
|
|
return lines
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}3
|
|
|
|
|
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
|
|
|
|
|
" Public API {{{1
|
|
|
|
|
|
|
|
|
|
function! tablemode#GetLastRow(line) "{{{2
|
|
|
|
|
return s:GetLastRow(a:line)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#GetFirstRow(line) "{{{2
|
|
|
|
|
return s:GetFirstRow(a:line)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableizeInsertMode() "{{{2
|
|
|
|
|
if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator)
|
|
|
|
|
@@ -641,17 +588,14 @@ function! tablemode#TableizeInsertMode() "{{{2
|
|
|
|
|
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableModeEnable() "{{{2
|
|
|
|
|
call s:SetActive(1)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableModeDisable() "{{{2
|
|
|
|
|
call s:SetActive(0)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableModeToggle() "{{{2
|
|
|
|
|
if g:table_mode_always_active
|
|
|
|
|
@@ -661,25 +605,17 @@ function! tablemode#TableModeToggle() "{{{2
|
|
|
|
|
call s:SetBufferOptDefault('table_mode_active', 0)
|
|
|
|
|
call s:SetActive(!b:table_mode_active)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableizeRange(...) range "{{{2
|
|
|
|
|
let shift = 1
|
|
|
|
|
if g:table_mode_border | let shift += 1 | endif
|
|
|
|
|
call s:Tableizeline(a:firstline, a:1)
|
|
|
|
|
undojoin
|
|
|
|
|
" The first one causes 2 extra lines for top & bottom border while the
|
|
|
|
|
" following lines cause only 1 for the bottom border.
|
|
|
|
|
let lnum = a:firstline + shift + (g:table_mode_border > 0)
|
|
|
|
|
while lnum < (a:firstline + (a:lastline - a:firstline + 1)*shift)
|
|
|
|
|
let lnum = a:firstline
|
|
|
|
|
while lnum < (a:firstline + (a:lastline - a:firstline + 1)*s:RowGap())
|
|
|
|
|
call s:Tableizeline(lnum, a:1)
|
|
|
|
|
undojoin
|
|
|
|
|
let lnum = lnum + shift
|
|
|
|
|
let lnum = lnum + s:RowGap()
|
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
|
|
if g:table_mode_border | call tablemode#TableRealign(lnum - shift) | endif
|
|
|
|
|
call tablemode#TableRealign(lnum - s:RowGap())
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableizeByDelimiter() "{{{2
|
|
|
|
|
let delim = input('/')
|
|
|
|
|
@@ -689,20 +625,39 @@ function! tablemode#TableizeByDelimiter() "{{{2
|
|
|
|
|
exec line("'<") . ',' . line("'>") . "call tablemode#TableizeRange('/' . delim)"
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#AddHeaderBorder(line) "{{{2
|
|
|
|
|
call setline(a:line, s:GenerateHeaderBorder(a:line))
|
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableRealign(line) "{{{2
|
|
|
|
|
let line = s:Line(a:line)
|
|
|
|
|
|
|
|
|
|
let [lnums, lines] = [[], []]
|
|
|
|
|
let tline = line
|
|
|
|
|
let [bline, tline] = [0, line]
|
|
|
|
|
while tablemode#IsATableRow(tline)
|
|
|
|
|
call insert(lnums, tline)
|
|
|
|
|
call insert(lines, getline(tline))
|
|
|
|
|
let tline = tline - s:RowGap()
|
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
|
|
" If we reached header walking upwards
|
|
|
|
|
if getline(tline) =~# s:HeaderBorderExpr() && tablemode#IsATableRow(tline - s:RowGap())
|
|
|
|
|
let bline = tline
|
|
|
|
|
let tline = tline - s:RowGap()
|
|
|
|
|
" Insert the header line
|
|
|
|
|
call insert(lnums, tline)
|
|
|
|
|
call insert(lines, getline(tline))
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
let tline = line + s:RowGap()
|
|
|
|
|
|
|
|
|
|
" If we start at header
|
|
|
|
|
if !bline && getline(tline) =~# s:HeaderBorderExpr()
|
|
|
|
|
let bline = tline
|
|
|
|
|
let tline = tline + s:RowGap()
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
while tablemode#IsATableRow(tline)
|
|
|
|
|
call add(lnums, tline)
|
|
|
|
|
call add(lines, getline(tline))
|
|
|
|
|
@@ -714,16 +669,16 @@ function! tablemode#TableRealign(line) "{{{2
|
|
|
|
|
for lnum in lnums
|
|
|
|
|
let index = index(lnums, lnum)
|
|
|
|
|
call setline(lnum, lines[index])
|
|
|
|
|
undojoin
|
|
|
|
|
call s:UpdateLineBorder(lnum)
|
|
|
|
|
endfor
|
|
|
|
|
|
|
|
|
|
if bline
|
|
|
|
|
call tablemode#AddHeaderBorder(bline)
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#IsATableRow(line) "{{{2
|
|
|
|
|
return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#RowCount(line) "{{{2
|
|
|
|
|
let line = s:Line(a:line)
|
|
|
|
|
@@ -742,7 +697,6 @@ function! tablemode#RowCount(line) "{{{2
|
|
|
|
|
|
|
|
|
|
return totalRowCount
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#RowNr(line) "{{{2
|
|
|
|
|
let line = s:Line(a:line)
|
|
|
|
|
@@ -755,14 +709,12 @@ function! tablemode#RowNr(line) "{{{2
|
|
|
|
|
|
|
|
|
|
return rowNr
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#ColumnCount(line) "{{{2
|
|
|
|
|
let line = s:Line(a:line)
|
|
|
|
|
|
|
|
|
|
return s:Strlen(substitute(getline(line), '[^' . g:table_mode_separator . ']', '', 'g'))-1
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#ColumnNr(pos) "{{{2
|
|
|
|
|
let pos = []
|
|
|
|
|
@@ -776,7 +728,6 @@ function! tablemode#ColumnNr(pos) "{{{2
|
|
|
|
|
let row_start = stridx(getline(pos[0]), g:table_mode_separator)
|
|
|
|
|
return s:Strlen(substitute(getline(pos[0])[(row_start):pos[1]-2], '[^' . g:table_mode_separator . ']', '', 'g'))
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#TableMotion(direction) "{{{2
|
|
|
|
|
if tablemode#IsATableRow('.')
|
|
|
|
|
@@ -813,7 +764,6 @@ function! tablemode#TableMotion(direction) "{{{2
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#CellTextObject() "{{{2
|
|
|
|
|
if tablemode#IsATableRow('.')
|
|
|
|
|
@@ -827,7 +777,6 @@ function! tablemode#CellTextObject() "{{{2
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#DeleteColumn() "{{{2
|
|
|
|
|
if tablemode#IsATableRow('.')
|
|
|
|
|
@@ -842,7 +791,6 @@ function! tablemode#DeleteColumn() "{{{2
|
|
|
|
|
call tablemode#TableRealign('.')
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#DeleteRow() "{{{2
|
|
|
|
|
if tablemode#IsATableRow('.')
|
|
|
|
|
@@ -863,47 +811,43 @@ function! tablemode#DeleteRow() "{{{2
|
|
|
|
|
call tablemode#TableRealign('.')
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#GetCells(...) abort "{{{2
|
|
|
|
|
let args = copy(a:000)
|
|
|
|
|
call insert(args, '.')
|
|
|
|
|
return call('s:GetCells', args)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#SetCell(val, ...) "{{{2
|
|
|
|
|
let args = copy(a:000)
|
|
|
|
|
call insert(args, a:val)
|
|
|
|
|
call call('s:SetCell', args)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#GetCellRange(range, ...) abort "{{{2
|
|
|
|
|
let args = copy(a:000)
|
|
|
|
|
call insert(args, a:range)
|
|
|
|
|
return call('s:GetCellRange', args)
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#Sum(range, ...) abort "{{{2
|
|
|
|
|
let args = copy(a:000)
|
|
|
|
|
call insert(args, a:range)
|
|
|
|
|
return s:Sum(call('s:GetCellRange', args))
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#Average(range, ...) abort "{{{2
|
|
|
|
|
let args = copy(a:000)
|
|
|
|
|
call insert(args, a:range)
|
|
|
|
|
return s:Average(call('s:GetCellRange', args))
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#AddFormula() "{{{2
|
|
|
|
|
let fr = input('f=')
|
|
|
|
|
let row = tablemode#RowNr('.')
|
|
|
|
|
let colm = tablemode#ColumnNr('.')
|
|
|
|
|
let indent = indent('.')
|
|
|
|
|
let indent_str = repeat(' ', indent)
|
|
|
|
|
|
|
|
|
|
if fr !=# ''
|
|
|
|
|
let fr = '$' . row . ',' . colm . '=' . fr
|
|
|
|
|
@@ -927,14 +871,13 @@ function! tablemode#AddFormula() "{{{2
|
|
|
|
|
let [cmss, cmse] = [cms[0], '']
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
let fr = cmss . ' tmf: ' . fr . ' ' . cmse
|
|
|
|
|
let fr = indent_str . cmss . ' tmf: ' . fr . ' ' . cmse
|
|
|
|
|
call append(fline-1, fr)
|
|
|
|
|
call cursor(cursor_pos)
|
|
|
|
|
endif
|
|
|
|
|
call tablemode#EvaluateFormulaLine()
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#EvaluateExpr(expr, line) abort "{{{2
|
|
|
|
|
let line = s:Line(a:line)
|
|
|
|
|
@@ -980,9 +923,8 @@ function! tablemode#EvaluateExpr(expr, line) abort "{{{2
|
|
|
|
|
endwhile
|
|
|
|
|
endif
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
function! tablemode#EvaluateFormulaLine() "{{{2
|
|
|
|
|
function! tablemode#EvaluateFormulaLine() abort "{{{2
|
|
|
|
|
let exprs = []
|
|
|
|
|
let cstring = &commentstring
|
|
|
|
|
let matchexpr = ''
|
|
|
|
|
@@ -1012,8 +954,5 @@ function! tablemode#EvaluateFormulaLine() "{{{2
|
|
|
|
|
call tablemode#EvaluateExpr(expr, line)
|
|
|
|
|
endfor
|
|
|
|
|
endfunction
|
|
|
|
|
" }}}2
|
|
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
|
|
|
|
|
" vim: sw=2 sts=2 fdl=0 fdm=marker
|
|
|
|
|
|