aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/vim-table-mode/autoload/tablemode/spreadsheet/formula.vim
blob: 8271b5d46cf62d23b02e5a5f1aacc220eb31df7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
" Private Functions {{{1

" Public Functions {{{1
function! tablemode#spreadsheet#formula#Add(...) "{{{2
  let fr = a:0 ? a:1 : input('f=')
  let row = tablemode#spreadsheet#RowNr('.')
  let colm = tablemode#spreadsheet#ColumnNr('.')
  let indent = indent('.')
  let indent_str = repeat(' ', indent)

  if fr !=# ''
    let fr = '$' . row . ',' . colm . '=' . fr
    let fline = tablemode#spreadsheet#GetLastRow('.') + 1
    if tablemode#table#IsBorder(fline) | let fline += 1 | endif
    let cursor_pos = [line('.'), col('.')]
    if getline(fline) =~# 'tmf: '
      " Comment line correctly
      let line_val = getline(fline)
      let start_pos = match(line_val, tablemode#table#StartCommentExpr())
      let end_pos = match(line_val, tablemode#table#EndCommentExpr())
      if empty(end_pos) | let end_pos = len(line_val) | endif
      let line_expr = strpart(line_val, start_pos, end_pos)
      let sce = matchstr(line_val, tablemode#table#StartCommentExpr() . '\zs')
      let ece = matchstr(line_val, tablemode#table#EndCommentExpr())
      call setline(fline, sce . line_expr . '; ' . fr . ece)
    else
      let cstring = &commentstring
      let [cmss, cmse] = ['', '']
      if len(cstring) > 0
        let cms = split(cstring, '%s')
        if len(cms) == 2
          let [cmss, cmse] = cms
        else
          let [cmss, cmse] = [cms[0], '']
        endif
      endif
      let fr = indent_str . cmss . ' tmf: ' . fr . ' ' . cmse
      call append(fline-1, fr)
      call cursor(cursor_pos)
    endif
    call tablemode#spreadsheet#formula#EvaluateFormulaLine()
  endif
endfunction

function! tablemode#spreadsheet#formula#EvaluateExpr(expr, line) abort "{{{2
  let line = tablemode#utils#line(a:line)
  let [target, expr] = map(split(a:expr, '='), 'tablemode#utils#strip(v:val)')
  let cell = substitute(target, '\$', '', '')
  if cell =~# ','
    let [row, colm] = map(split(cell, ','), 'str2nr(v:val)')
  else
    let [row, colm] = [0, str2nr(cell)]
  endif

  if expr =~# 'Sum(.*)'
    let expr = substitute(expr, 'Sum(\([^)]*\))', 'tablemode#spreadsheet#Sum("\1",'.line.','.colm.')', 'g')
  endif

  if expr =~# 'Average(.*)'
    let expr = substitute(expr, 'Average(\([^)]*\))', 'tablemode#spreadsheet#Average("\1",'.line.','.colm.')', 'g')
  endif

  if expr =~# '\$\d\+,\d\+'
    let expr = substitute(expr, '\$\(\d\+\),\(\d\+\)',
          \ '\=str2float(tablemode#spreadsheet#cell#GetCells(line, submatch(1), submatch(2)))', 'g')
  endif

  if cell =~# ','
    if expr =~# '\$'
      let expr = substitute(expr, '\$\(\d\+\)',
          \ '\=str2float(tablemode#spreadsheet#cell#GetCells(line, row, submatch(1)))', 'g')
    endif
    call tablemode#spreadsheet#cell#SetCell(eval(expr), line, row, colm)
  else
    let [row, line] = [1, tablemode#spreadsheet#GetFirstRow(line)]
    while tablemode#table#IsRow(line)
      let texpr = expr
      if expr =~# '\$'
        let texpr = substitute(texpr, '\$\(\d\+\)',
              \ '\=str2float(tablemode#spreadsheet#cell#GetCells(line, row, submatch(1)))', 'g')
      endif

      call tablemode#spreadsheet#cell#SetCell(eval(texpr), line, row, colm)
      let row += 1
      let line += 1
    endwhile
  endif
endfunction

function! tablemode#spreadsheet#formula#EvaluateFormulaLine() abort "{{{2
  let exprs = []
  let cstring = &commentstring
  let matchexpr = ''
  if len(cstring) > 0
    let cms = split(cstring, '%s')
    if len(cms) == 2
      let matchexpr = '^\s*' . escape(cms[0], '/*') . '\s*tmf: \zs.*\ze' . escape(cms[1], '/*') . '\s*$'
    else
      let matchexpr = '^\s*' . escape(cms[0], '/*') . '\s*tmf: \zs.*$'
    endif
  else
    let matchexpr = '^\s* tmf: \zs.*$'
  endif
  if tablemode#table#IsRow('.') " We're inside the table
    let line = tablemode#spreadsheet#GetLastRow('.')
    let fline = line + 1
    if tablemode#table#IsBorder(fline) | let fline += 1 | endif
    if getline(fline) =~# 'tmf: '
      let exprs = split(matchstr(getline(fline), matchexpr), ';')
    endif
  elseif getline('.') =~# 'tmf: ' " We're on the formula line
    let line = line('.') - 1
    if tablemode#table#IsBorder(line) | let line -= 1 | endif
    if tablemode#table#IsRow(line)
      let exprs = split(matchstr(getline('.'), matchexpr), ';')
    endif
  endif

  for expr in exprs
    call tablemode#spreadsheet#formula#EvaluateExpr(expr, line)
  endfor
endfunction