ledger.vim: use full width, not only 80 chars
Also add option g:ledger_maxwidth to provide old behaviour by putting a line like this in .vimrc: let g:ledger_maxwidth = 80 I think that it would make sense to put all the functions and everything that has to do with foldtext in ftplugin/ledger.vim
This commit is contained in:
parent
0e9f782a05
commit
7c67866b22
1 changed files with 85 additions and 35 deletions
|
|
@ -1,17 +1,20 @@
|
||||||
" Vim syntax file
|
" Vim syntax file
|
||||||
" filetype: ledger
|
" filetype: ledger
|
||||||
" Version: 0.0.5
|
" Version: 0.1.0
|
||||||
" by Johann Klähn; Use according to the terms of the GPL>=2.
|
" by Johann Klähn; Use according to the terms of the GPL>=2.
|
||||||
" by Stefan Karrmann; Use according to the terms of the GPL>=2.
|
" by Stefan Karrmann; Use according to the terms of the GPL>=2.
|
||||||
" by Wolfgang Oertl; Use according to the terms of the GPL>=2.
|
" by Wolfgang Oertl; Use according to the terms of the GPL>=2.
|
||||||
" Revision history
|
" Revision history
|
||||||
" 2009-03-25 J. Klähn: Allow Metadata in postings (Ledger 3.0)
|
" 2009-06-12 J. Klähn: Use all available columns for foldtext
|
||||||
|
" 2009-03-25 J. Klähn: Allow Metadata
|
||||||
|
" in transactions and postings (Ledger 3.0)
|
||||||
" Also fixed alignment for multi-byte-characters
|
" Also fixed alignment for multi-byte-characters
|
||||||
" 2009-01-28 S.Karrmann: minor fixes
|
" 2009-01-28 S.Karrmann: minor fixes
|
||||||
" 2009-01-27 third version by S.Karrmann.
|
" 2009-01-27 third version by S.Karrmann.
|
||||||
" better extraction of the amount of the posting
|
" better extraction of the amount of the posting
|
||||||
" decimal separator can be one of '.' and ','.
|
" decimal separator can be one of '.' and ','.
|
||||||
" 2005-02-05 first version (partly copied from ledger.vim 0.0.1)
|
" 2005-02-05 first version (partly copied from ledger.vim 0.0.1)
|
||||||
|
" vim:ts=2:sw=2:sts=2:foldmethod=marker
|
||||||
|
|
||||||
if version < 600
|
if version < 600
|
||||||
syntax clear
|
syntax clear
|
||||||
|
|
@ -19,56 +22,103 @@ elseif exists("b:current_sytax")
|
||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
" You can set a maximal number of columns the fold text (excluding amount)
|
||||||
|
" will use by overriding g:ledger_maxwidth in your .vimrc.
|
||||||
|
" When maxwidth is zero, the amount will be displayed at the far right side
|
||||||
|
" of the screen.
|
||||||
|
if !exists('g:ledger_maxwidth')
|
||||||
|
let g:ledger_maxwidth = 0
|
||||||
|
endif
|
||||||
|
|
||||||
" for debugging
|
" for debugging
|
||||||
syntax clear
|
syntax clear
|
||||||
|
|
||||||
" region: a normal posting
|
" region: a transaction containing postings
|
||||||
syn region postNorm start=/^\d/ skip=/^\s/ end=/^/ fold keepend transparent contains=postDate,Metadata
|
syn region transNorm start=/^\d/ skip=/^\s/ end=/^/
|
||||||
syn match postDate /^\d\S\+/ contained
|
\ fold keepend transparent contains=transDate, Metadata
|
||||||
|
syn match transDate /^\d\S\+/ contained
|
||||||
syn match Metadata /^\s\+;.*/ contained
|
syn match Metadata /^\s\+;.*/ contained
|
||||||
syn match Comment /^;.*$/
|
syn match Comment /^;.*$/
|
||||||
|
|
||||||
highlight default link Comment SpecialKey
|
highlight default link Comment SpecialKey
|
||||||
highlight default link Metadata SpecialKey
|
highlight default link Metadata SpecialKey
|
||||||
highlight default link postDate Question
|
highlight default link transDate Question
|
||||||
|
|
||||||
" folding: how to represent a posting in one line.
|
function! s:multibyte_strlen(text) "{{{
|
||||||
|
return strlen(substitute(a:text, ".", "x", "g"))
|
||||||
|
endf "}}}
|
||||||
|
|
||||||
|
function s:get_columns(win) "{{{
|
||||||
|
" As long as vim doesn't provide a command natively,
|
||||||
|
" we have to compute the available columns.
|
||||||
|
" see :help todo.txt -> /Add argument to winwidth()/
|
||||||
|
" FIXME: Although this will propably never be used with debug mode enabled
|
||||||
|
" this should take the signs column into account (:help sign.txt)
|
||||||
|
let columns = (winwidth(a:win) == 0 ? 80 : winwidth(a:win)) - &foldcolumn
|
||||||
|
if &number
|
||||||
|
" line('w$') is the line number of the last line
|
||||||
|
let columns -= max([len(line('w$'))+1, &numberwidth])
|
||||||
|
endif
|
||||||
|
return columns
|
||||||
|
endf "}}}
|
||||||
|
|
||||||
|
let s:rx_amount = '\('.
|
||||||
|
\ '\%([0-9]\+\)'.
|
||||||
|
\ '\%([,.][0-9]\+\)*'.
|
||||||
|
\ '\|'.
|
||||||
|
\ '[,.][0-9]\+'.
|
||||||
|
\ '\)'.
|
||||||
|
\ '\s*\%([[:alpha:]¢$€£]\+\s*\)\?'.
|
||||||
|
\ '\%(\s*;.*\)\?$'
|
||||||
|
|
||||||
|
" folding: how to represent a transaction in one line.
|
||||||
function! LedgerFoldText()
|
function! LedgerFoldText()
|
||||||
let line = strpart(getline(v:foldstart), 0, 99)
|
" find amount
|
||||||
let amount = ""
|
let amount = ""
|
||||||
|
let lnum = v:foldstart
|
||||||
|
while lnum <= v:foldend
|
||||||
|
let line = getline(lnum)
|
||||||
|
|
||||||
let delta = 1
|
" Skip metadata/leading comment
|
||||||
while (delta >= 1 && delta < (v:foldend-v:foldstart+1))
|
if line !~ '^\s\+;'
|
||||||
let other_line = getline(v:foldstart+delta)
|
" No comment, look for amount...
|
||||||
|
let groups = matchlist(line, s:rx_amount)
|
||||||
if len(matchstr(other_line, '^\s\+;')) != 0
|
echomsg string(groups)
|
||||||
" Skip metadata/leading comment
|
if ! empty(groups)
|
||||||
let delta = delta + 1
|
echomsg amount
|
||||||
else
|
let amount = groups[1]
|
||||||
" No comment, look for amount...
|
break
|
||||||
let delta = -1
|
|
||||||
let lst = matchlist(other_line,'\(\%([0-9]\+[,.]\)\=[0-9]\+\%([,.][0-9]\+\)\=\|[,.][0-9]\+\)\s*\%([A-Za-z$€¢]\+\s*\)\=\%(\s*;.*\)\=$')
|
|
||||||
if (len(lst) != 0)
|
|
||||||
let amount = lst[1]
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
endwhile
|
endif
|
||||||
|
let lnum += 1
|
||||||
|
endwhile
|
||||||
|
|
||||||
" Replace each char with x prior to counting
|
let fmt = '%s %s '
|
||||||
" this fixes problems with multi byte characters
|
" strip whitespace at beginning and end of line
|
||||||
let pad_len = 80 - strlen(substitute(line, ".", "x", "g")) - strlen(substitute(amount, ".", "x", "g"))
|
let foldtext = substitute(getline(v:foldstart),
|
||||||
|
\ '\(^\s\+\|\s\+$\)', '', 'g')
|
||||||
|
|
||||||
if (pad_len < 0)
|
" number of columns foldtext can use
|
||||||
pad_len = 0
|
let columns = s:get_columns(0)
|
||||||
endif
|
if g:ledger_maxwidth
|
||||||
let pad = strpart(" ", 0, pad_len)
|
let columns = min([columns, g:ledger_maxwidth])
|
||||||
return line . pad . amount
|
endif
|
||||||
|
let columns -= s:multibyte_strlen(printf(fmt, '', amount))
|
||||||
|
|
||||||
|
" add spaces so the text is always long enough when we strip it
|
||||||
|
" to a certain width (fake table)
|
||||||
|
let foldtext .= repeat(' ', s:get_columns(0))
|
||||||
|
" we don't use slices[:5], because that messes up multibyte characters
|
||||||
|
let foldtext = substitute(foldtext, '.\{'.columns.'}\zs.*$', '', '')
|
||||||
|
|
||||||
|
return printf(fmt, foldtext, amount)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
set foldtext=LedgerFoldText()
|
set foldtext=LedgerFoldText()
|
||||||
set foldmethod=syntax
|
set foldmethod=syntax
|
||||||
|
|
||||||
" syncinc is easy: search for the first posting.
|
" syncinc is easy: search for the first transaction.
|
||||||
syn sync clear
|
syn sync clear
|
||||||
syn sync match ledgerSync grouphere postNorm "^\d"
|
syn sync match ledgerSync grouphere transNorm "^\d"
|
||||||
|
|
||||||
let b:current_syntax = "ledger"
|
let b:current_syntax = "ledger"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue