From 6c0ccd5ffd0a9ac1f97adeca1dc22a7c9db844c5 Mon Sep 17 00:00:00 2001 From: Johann Klähn Date: Wed, 8 Sep 2010 23:24:39 +0200 Subject: vim. add LedgerToggleTransactionState() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By using call LedgerToggleTransactionState(line('.'), ' *?') the state of the transaction will toggle through: no state -> * -> ? -> no state -> … beginning at what ever state it currently has. Adjust to your liking. Thanks to Chad Voegele for the suggestion. --- contrib/vim/ftplugin/ledger.vim | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'contrib/vim/ftplugin/ledger.vim') diff --git a/contrib/vim/ftplugin/ledger.vim b/contrib/vim/ftplugin/ledger.vim index 1cced35c..623b0795 100644 --- a/contrib/vim/ftplugin/ledger.vim +++ b/contrib/vim/ftplugin/ledger.vim @@ -268,6 +268,26 @@ function! LedgerGetTags() "{{{1 return alltags endf "}}} +function! LedgerToggleTransactionState(lnum, ...) + if a:0 == 1 + let chars = a:1 + else + let chars = ' *' + endif + let trans = s:transaction.from_lnum(a:lnum) + if empty(trans) + return + endif + + let old = has_key(trans, 'state') ? trans['state'] : ' ' + let i = stridx(chars, old) + 1 + let new = chars[i > len(chars) ? 0 : i] + + call trans.set_state(new) + + call setline(trans['head'], trans.format_head()) +endf + function! LedgerSetTransactionState(lnum, char) "{{{1 " modifies or sets the state of the transaction at the cursor, " removing the state alltogether if a:char is empty @@ -276,11 +296,7 @@ function! LedgerSetTransactionState(lnum, char) "{{{1 return endif - if empty(a:char) && has_key(trans, 'state') - call remove(trans, 'state') - else - let trans['state'] = a:char - endif + call trans.set_state(a:char) call setline(trans['head'], trans.format_head()) endf "}}} @@ -351,7 +367,8 @@ function! s:transaction.from_lnum(lnum) dict "{{{2 let trans['date'] = part elseif ! has_key(trans, 'code') && part =~ '^([^)]*)$' let trans['code'] = part[1:-2] - elseif ! has_key(trans, 'state') && part =~ '^[!?*]$' + elseif ! has_key(trans, 'state') && part =~ '^.$' + " the first character by itself is assumed to be the state of the transaction. let trans['state'] = part else call add(description, part) @@ -361,6 +378,14 @@ function! s:transaction.from_lnum(lnum) dict "{{{2 return trans endf "}}} +function! s:transaction.set_state(char) dict "{{{2 + if has_key(self, 'state') && a:char =~ '^\s*$' + call remove(self, 'state') + else + let self['state'] = a:char + endif +endf "}}} + function! s:transaction.parse_body(...) dict "{{{2 if a:0 == 2 let head = a:1 -- cgit v1.2.3