vim. introduce basic transaction 'class'/concept
This commit is contained in:
parent
2dff4dea3f
commit
cf40d1c9d7
1 changed files with 54 additions and 27 deletions
|
|
@ -268,42 +268,69 @@ function! LedgerGetTags() "{{{1
|
||||||
return alltags
|
return alltags
|
||||||
endf "}}}
|
endf "}}}
|
||||||
|
|
||||||
" Helper functions {{{1
|
function! LedgerSetTransactionState(lnum, char) "{{{1
|
||||||
|
|
||||||
function! LedgerSetTransactionState(char)
|
|
||||||
" modifies or sets the state of the transaction at the cursor,
|
" modifies or sets the state of the transaction at the cursor,
|
||||||
" removing the state alltogether if a:char is empty
|
" removing the state alltogether if a:char is empty
|
||||||
let head = search('^\d\S\+', 'bcnW')
|
endf "}}}
|
||||||
|
|
||||||
|
let s:transaction = {} "{{{1
|
||||||
|
function! s:transaction.new() dict
|
||||||
|
return copy(s:transaction)
|
||||||
|
endf
|
||||||
|
|
||||||
|
function! s:transaction.from_lnum(lnum) dict "{{{2
|
||||||
|
let head = s:get_transaction_extents(a:lnum)[0]
|
||||||
if ! head
|
if ! head
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
let trans = copy(s:transaction)
|
||||||
let parts = split(getline(head), '\s\+')
|
let parts = split(getline(head), '\s\+')
|
||||||
|
let description = []
|
||||||
let result = []
|
for part in parts
|
||||||
while 1
|
if ! has_key(trans, 'date') && part =~ '^\d'
|
||||||
let part = remove(parts, 0)
|
let trans['date'] = part
|
||||||
" add state after date or (code)
|
elseif ! has_key(trans, 'code') && part =~ '^([^)]*)$'
|
||||||
if part =~ '^\d' || part =~ '^([^)]*)$'
|
let trans['code'] = part[1:-2]
|
||||||
call add(result, part)
|
elseif ! has_key(trans, 'state') && part =~ '^[!?*]$'
|
||||||
" replace existing state with new state
|
let trans['state'] = part
|
||||||
elseif part =~ '^[!?*]$'
|
|
||||||
if ! empty(a:char)
|
|
||||||
call add(result, a:char)
|
|
||||||
endif
|
|
||||||
break
|
|
||||||
" add state in front of anything else if it does not exist yet
|
|
||||||
else
|
else
|
||||||
if ! empty(a:char)
|
call add(description, part)
|
||||||
call add(result, a:char)
|
endif
|
||||||
endif
|
endfor
|
||||||
call add(result, part)
|
let trans['description'] = join(description)
|
||||||
break
|
return trans
|
||||||
end
|
endf "}}}
|
||||||
endwhile
|
|
||||||
|
|
||||||
call setline(head, join(extend(result, parts)))
|
function! s:transaction.format_head() dict "{{{2
|
||||||
endf
|
let parts = []
|
||||||
|
if has_key(self, 'date') | call add(parts, self['date']) | endif
|
||||||
|
if has_key(self, 'code') | call add(parts, '('.self['code'].')') | endif
|
||||||
|
if has_key(self, 'state') | call add(parts, self['state']) | endif
|
||||||
|
if has_key(self, 'description') | call add(parts, self['description']) | endif
|
||||||
|
return join(parts)
|
||||||
|
endf "}}}
|
||||||
|
"}}}
|
||||||
|
|
||||||
|
" Helper functions {{{1
|
||||||
|
|
||||||
|
function! s:get_transaction_extents(lnum) "{{{2
|
||||||
|
" safe view / position
|
||||||
|
let view = winsaveview()
|
||||||
|
let fe = &foldenable
|
||||||
|
set nofoldenable
|
||||||
|
|
||||||
|
call cursor(a:lnum, 0)
|
||||||
|
let head = search('^\d\S\+', 'bcnW')
|
||||||
|
let tail = search('^[^;[:blank:]]\S\+', 'nW')
|
||||||
|
let tail = tail > head ? tail - 1 : line('$')
|
||||||
|
|
||||||
|
" restore view / position
|
||||||
|
let &foldenable = fe
|
||||||
|
call winrestview(view)
|
||||||
|
|
||||||
|
return head ? [head, tail] : [0, 0]
|
||||||
|
endf "}}}
|
||||||
|
|
||||||
" return length of string with fix for multibyte characters
|
" return length of string with fix for multibyte characters
|
||||||
function! s:multibyte_strlen(text) "{{{2
|
function! s:multibyte_strlen(text) "{{{2
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue