mirror of
https://github.com/dhruvasagar/vim-table-mode.git
synced 2025-11-10 12:03:48 -05:00
Updated README, fixed expr evaluator
This commit is contained in:
11
README.md
11
README.md
@@ -142,10 +142,11 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
|
||||
cell.
|
||||
|
||||
- The `formula` can be a simple mathematical expression involving cells
|
||||
which are also defined by the same format as that of the target cell.
|
||||
which are also defined by the same format as that of the target cell.
|
||||
Apart from basic mathematical expressions, table mode also provides
|
||||
special functions `Sum` and `Average`. Both these functions take a range
|
||||
as input. A range can be of two forms :
|
||||
special functions `Sum` and `Average`. Although you can use all native vim
|
||||
functions as well. Both these functions take a range as input. A range can
|
||||
be of two forms :
|
||||
|
||||
- `n,m`: This represents cells in the current column from row `n`
|
||||
through `m`. If `m` is negative it represents `m` row above the
|
||||
@@ -156,11 +157,15 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
|
||||
|
||||
- Examples :
|
||||
- `$2 = $1 * $1`
|
||||
- `$2 = pow($1, 5)` NOTE: Remember to put space between the $1, and 5
|
||||
here otherwise it will be treated like a table cell.
|
||||
- `$2 = $1 / $1,3`
|
||||
- `$1,2 = $1,1 * $1,1`
|
||||
- `$5,1 = Sum(1:-1)`
|
||||
- `$5,1 = float2nr(Sum(1:-1))`
|
||||
- `$5,3 = Sum(1,2:5,2)`
|
||||
- `$5,3 = Sum(1,2:5,2)/$5,1`
|
||||
- `$5,3 = Average(1,2:5,2)/$5,1`
|
||||
|
||||
### Demo
|
||||
|
||||
|
||||
@@ -27,54 +27,52 @@ endif
|
||||
let g:autoloaded_table_mode = 1
|
||||
" }}}2
|
||||
|
||||
" Utility Functions {{{2
|
||||
|
||||
function! s:throw(string) abort "{{{3
|
||||
function! s:throw(string) abort "{{{2
|
||||
let v:errmsg = 'table-mode: ' . a:string
|
||||
throw v:errmsg
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:sub(str,pat,rep) abort "{{{3
|
||||
function! s:sub(str,pat,rep) abort "{{{2
|
||||
return substitute(a:str,'\v\C'.a:pat,a:rep,'')
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:gsub(str,pat,rep) abort "{{{3
|
||||
function! s:gsub(str,pat,rep) abort "{{{2
|
||||
return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:SetBufferOptDefault(opt, val) "{{{3
|
||||
function! s:SetBufferOptDefault(opt, val) "{{{2
|
||||
if !exists('b:' . a:opt)
|
||||
let b:{a:opt} = a:val
|
||||
endif
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:Line(line) "{{{3
|
||||
function! s:Line(line) "{{{2
|
||||
if type(a:line) == type('')
|
||||
return line(a:line)
|
||||
else
|
||||
return a:line
|
||||
endif
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
" function! s:Strlen(text) - Count multibyte characters accurately {{{3
|
||||
" 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
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:Strip(string) "{{{3
|
||||
function! s:Strip(string) "{{{2
|
||||
return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$')
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:Sum(list) "{{{3
|
||||
let result = 0
|
||||
function! s:Sum(list) "{{{2
|
||||
let result = 0.0
|
||||
for item in a:list
|
||||
if type(item) == type(1) || type(item) == type(1.0)
|
||||
let result = result + item
|
||||
@@ -86,14 +84,14 @@ function! s:Sum(list) "{{{3
|
||||
endfor
|
||||
return result
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:Average(list) "{{{3
|
||||
function! s:Average(list) "{{{2
|
||||
return s:Sum(a:list)/len(a:list)
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:GetCommentStart() "{{{3
|
||||
function! s:GetCommentStart() "{{{2
|
||||
let cstring = &commentstring
|
||||
if s:Strlen(cstring) > 0
|
||||
return substitute(split(cstring, '%s')[0], '.', '\\\0', 'g')
|
||||
@@ -101,9 +99,9 @@ function! s:GetCommentStart() "{{{3
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:StartExpr() "{{{3
|
||||
function! s:StartExpr() "{{{2
|
||||
let cstart = s:GetCommentStart()
|
||||
if s:Strlen(cstart) > 0
|
||||
return '^\s*\(' . cstart . '\)\?\s*'
|
||||
@@ -111,9 +109,9 @@ function! s:StartExpr() "{{{3
|
||||
return '^\s*'
|
||||
endif
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:StartCommentExpr() "{{{3
|
||||
function! s:StartCommentExpr() "{{{2
|
||||
let cstartexpr = s:GetCommentStart()
|
||||
if s:Strlen(cstartexpr) > 0
|
||||
return '^\s*' . cstartexpr . '\s*'
|
||||
@@ -121,21 +119,19 @@ function! s:StartCommentExpr() "{{{3
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:IsTableModeActive() "{{{3
|
||||
function! s:IsTableModeActive() "{{{2
|
||||
if g:table_mode_always_active | return 1 | endif
|
||||
|
||||
call s:SetBufferOptDefault('table_mode_active', 0)
|
||||
return b:table_mode_active
|
||||
endfunction
|
||||
" }}}3
|
||||
" }}}2
|
||||
|
||||
function! s:RowGap() "{{{3
|
||||
function! s:RowGap() "{{{2
|
||||
return g:table_mode_border ? 2 : 1
|
||||
endfunction
|
||||
" }}}3
|
||||
|
||||
" }}}2
|
||||
|
||||
function! s:ToggleMapping() "{{{2
|
||||
@@ -558,7 +554,7 @@ function! s:Align(lines) "{{{3
|
||||
|
||||
if len(line) <= 1 | continue | endif
|
||||
for i in range(len(line))
|
||||
if line[i] !~# '[^0-9]'
|
||||
if line[i] !~# '[^0-9\.]'
|
||||
let field = s:Padding(line[i], maxes[i], 'r')
|
||||
else
|
||||
let field = s:Padding(line[i], maxes[i], 'l')
|
||||
@@ -874,7 +870,7 @@ function! tablemode#AddFormula() "{{{2
|
||||
if len(cstring) > 0
|
||||
let cstring = split(cstring, '%s')[0]
|
||||
endif
|
||||
let fr = cstring . 'tmf: ' . fr
|
||||
let fr = cstring . ' tmf: ' . fr
|
||||
call append(fline-1, fr)
|
||||
call cursor(cursor_pos)
|
||||
endif
|
||||
@@ -894,15 +890,15 @@ function! tablemode#EvaluateExpr(expr, line) abort "{{{2
|
||||
endif
|
||||
|
||||
if expr =~# 'Sum(.*)'
|
||||
let expr = substitute(expr, 'Sum(\(.*\))', 'tablemode#Sum("\1",'.line.','.colm.')', 'g')
|
||||
let expr = substitute(expr, 'Sum(\([^)]*\))', 'tablemode#Sum("\1",'.line.','.colm.')', 'g')
|
||||
endif
|
||||
|
||||
if expr =~# 'Average(.*)'
|
||||
let expr = substitute(expr, 'Average(\(.*\))', 'tablemode#Average("\1",'.line.','.colm.')', 'g')
|
||||
let expr = substitute(expr, 'Average(\([^)]*\))', 'tablemode#Average("\1",'.line.','.colm.')', 'g')
|
||||
endif
|
||||
|
||||
if expr =~# '[\$,]'
|
||||
let expr = substitute(expr, '\$\(\d\+\),\(\d*\)',
|
||||
if expr =~# '\$\d\+,\d\+'
|
||||
let expr = substitute(expr, '\$\(\d\+\),\(\d\+\)',
|
||||
\ '\=str2float(s:GetCells(line, submatch(1), submatch(2)))', 'g')
|
||||
endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user