Updated README, fixed expr evaluator

This commit is contained in:
Dhruva Sagar
2013-05-17 14:12:44 +05:30
parent d0d9d1d4f6
commit 34e271b24a
2 changed files with 42 additions and 41 deletions

View File

@@ -144,8 +144,9 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
- The `formula` can be a simple mathematical expression involving cells - 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 Apart from basic mathematical expressions, table mode also provides
special functions `Sum` and `Average`. Both these functions take a range special functions `Sum` and `Average`. Although you can use all native vim
as input. A range can be of two forms : 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` - `n,m`: This represents cells in the current column from row `n`
through `m`. If `m` is negative it represents `m` row above the 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 : - Examples :
- `$2 = $1 * $1` - `$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` - `$2 = $1 / $1,3`
- `$1,2 = $1,1 * $1,1` - `$1,2 = $1,1 * $1,1`
- `$5,1 = Sum(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,3 = Sum(1,2:5,2)/$5,1` - `$5,3 = Sum(1,2:5,2)/$5,1`
- `$5,3 = Average(1,2:5,2)/$5,1`
### Demo ### Demo

View File

@@ -27,54 +27,52 @@ endif
let g:autoloaded_table_mode = 1 let g:autoloaded_table_mode = 1
" }}}2 " }}}2
" Utility Functions {{{2 function! s:throw(string) abort "{{{2
function! s:throw(string) abort "{{{3
let v:errmsg = 'table-mode: ' . a:string let v:errmsg = 'table-mode: ' . a:string
throw v:errmsg throw v:errmsg
endfunction 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,'') return substitute(a:str,'\v\C'.a:pat,a:rep,'')
endfunction 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') return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
endfunction endfunction
" }}}3 " }}}2
function! s:SetBufferOptDefault(opt, val) "{{{3 function! s:SetBufferOptDefault(opt, val) "{{{2
if !exists('b:' . a:opt) if !exists('b:' . a:opt)
let b:{a:opt} = a:val let b:{a:opt} = a:val
endif endif
endfunction endfunction
" }}}3 " }}}2
function! s:Line(line) "{{{3 function! s:Line(line) "{{{2
if type(a:line) == type('') if type(a:line) == type('')
return line(a:line) return line(a:line)
else else
return a:line return a:line
endif endif
endfunction 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 " See :h strlen() for more details
function! s:Strlen(text) function! s:Strlen(text)
return strlen(substitute(a:text, '.', 'x', 'g')) return strlen(substitute(a:text, '.', 'x', 'g'))
endfunction endfunction
" }}}3 " }}}2
function! s:Strip(string) "{{{3 function! s:Strip(string) "{{{2
return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$') return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$')
endfunction endfunction
" }}}3 " }}}2
function! s:Sum(list) "{{{3 function! s:Sum(list) "{{{2
let result = 0 let result = 0.0
for item in a:list for item in a:list
if type(item) == type(1) || type(item) == type(1.0) if type(item) == type(1) || type(item) == type(1.0)
let result = result + item let result = result + item
@@ -86,14 +84,14 @@ function! s:Sum(list) "{{{3
endfor endfor
return result return result
endfunction endfunction
" }}}3 " }}}2
function! s:Average(list) "{{{3 function! s:Average(list) "{{{2
return s:Sum(a:list)/len(a:list) return s:Sum(a:list)/len(a:list)
endfunction endfunction
" }}}3 " }}}2
function! s:GetCommentStart() "{{{3 function! s:GetCommentStart() "{{{2
let cstring = &commentstring let cstring = &commentstring
if s:Strlen(cstring) > 0 if s:Strlen(cstring) > 0
return substitute(split(cstring, '%s')[0], '.', '\\\0', 'g') return substitute(split(cstring, '%s')[0], '.', '\\\0', 'g')
@@ -101,9 +99,9 @@ function! s:GetCommentStart() "{{{3
return '' return ''
endif endif
endfunction endfunction
" }}}3 " }}}2
function! s:StartExpr() "{{{3 function! s:StartExpr() "{{{2
let cstart = s:GetCommentStart() let cstart = s:GetCommentStart()
if s:Strlen(cstart) > 0 if s:Strlen(cstart) > 0
return '^\s*\(' . cstart . '\)\?\s*' return '^\s*\(' . cstart . '\)\?\s*'
@@ -111,9 +109,9 @@ function! s:StartExpr() "{{{3
return '^\s*' return '^\s*'
endif endif
endfunction endfunction
" }}}3 " }}}2
function! s:StartCommentExpr() "{{{3 function! s:StartCommentExpr() "{{{2
let cstartexpr = s:GetCommentStart() let cstartexpr = s:GetCommentStart()
if s:Strlen(cstartexpr) > 0 if s:Strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*' return '^\s*' . cstartexpr . '\s*'
@@ -121,21 +119,19 @@ function! s:StartCommentExpr() "{{{3
return '' return ''
endif endif
endfunction endfunction
" }}}3 " }}}2
function! s:IsTableModeActive() "{{{3 function! s:IsTableModeActive() "{{{2
if g:table_mode_always_active | return 1 | endif if g:table_mode_always_active | return 1 | endif
call s:SetBufferOptDefault('table_mode_active', 0) call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active return b:table_mode_active
endfunction endfunction
" }}}3 " }}}2
function! s:RowGap() "{{{3 function! s:RowGap() "{{{2
return g:table_mode_border ? 2 : 1 return g:table_mode_border ? 2 : 1
endfunction endfunction
" }}}3
" }}}2 " }}}2
function! s:ToggleMapping() "{{{2 function! s:ToggleMapping() "{{{2
@@ -558,7 +554,7 @@ function! s:Align(lines) "{{{3
if len(line) <= 1 | continue | endif if len(line) <= 1 | continue | endif
for i in range(len(line)) 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') let field = s:Padding(line[i], maxes[i], 'r')
else else
let field = s:Padding(line[i], maxes[i], 'l') let field = s:Padding(line[i], maxes[i], 'l')
@@ -874,7 +870,7 @@ function! tablemode#AddFormula() "{{{2
if len(cstring) > 0 if len(cstring) > 0
let cstring = split(cstring, '%s')[0] let cstring = split(cstring, '%s')[0]
endif endif
let fr = cstring . 'tmf: ' . fr let fr = cstring . ' tmf: ' . fr
call append(fline-1, fr) call append(fline-1, fr)
call cursor(cursor_pos) call cursor(cursor_pos)
endif endif
@@ -894,15 +890,15 @@ function! tablemode#EvaluateExpr(expr, line) abort "{{{2
endif endif
if expr =~# 'Sum(.*)' 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 endif
if expr =~# 'Average(.*)' 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 endif
if expr =~# '[\$,]' if expr =~# '\$\d\+,\d\+'
let expr = substitute(expr, '\$\(\d\+\),\(\d*\)', let expr = substitute(expr, '\$\(\d\+\),\(\d\+\)',
\ '\=str2float(s:GetCells(line, submatch(1), submatch(2)))', 'g') \ '\=str2float(s:GetCells(line, submatch(1), submatch(2)))', 'g')
endif endif