From fd689973981001c75838f325eb549b7442979572 Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Sat, 31 Aug 2013 12:57:02 -0400 Subject: [PATCH] lazy load variables until needed (#204). this allows users to define section variables in their vimrc. --- autoload/airline/init.vim | 21 ++++++++------------- autoload/airline/parts.vim | 16 ++++++++++++++++ autoload/airline/util.vim | 2 ++ t/airline.vim | 6 ++++++ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/autoload/airline/init.vim b/autoload/airline/init.vim index ac9bf5b..a03f4d2 100644 --- a/autoload/airline/init.vim +++ b/autoload/airline/init.vim @@ -7,7 +7,11 @@ function! s:check_defined(variable, default) endif endfunction -function! airline#init#bootstrap() +let s:init_vars = 0 +function airline#init#vars() + if s:init_vars | return | endif + let s:init_vars = 1 + call s:check_defined('g:airline_left_sep', get(g:, 'airline_powerline_fonts', 0)?"":">") call s:check_defined('g:airline_left_alt_sep', get(g:, 'airline_powerline_fonts', 0)?"":">") call s:check_defined('g:airline_right_sep', get(g:, 'airline_powerline_fonts', 0)?"":"<") @@ -51,19 +55,10 @@ function! airline#init#bootstrap() \ 'linenr': get(g:, 'airline_linecolumn_prefix', get(g:, 'airline_powerline_fonts', 0) ? '' : ':' ), \ 'branch': get(g:, 'airline_branch_prefix', get(g:, 'airline_powerline_fonts', 0) ? '' : ''), \ }, 'keep') +endfunction - call airline#parts#define_raw('mode', '%{get(w:, "airline_current_mode", "")}') - call airline#parts#define_function('iminsert', 'airline#parts#iminsert') - call airline#parts#define_function('paste', 'airline#parts#paste') - call airline#parts#define('readonly', { - \ 'function': 'airline#parts#readonly', - \ 'highlight': 'airline_file', - \ }) - call airline#parts#define_raw('file', '%f%m') - call airline#parts#define_raw('ffenc', '%{printf("%s%s",&fenc,strlen(&ff)>0?"[".&ff."]":"")}') - - call airline#parts#define_empty(['hunks', 'branch', 'tagbar', 'syntastic']) - +function! airline#init#bootstrap() + call airline#init#vars() call airline#extensions#load() if !exists('g:airline_section_a') diff --git a/autoload/airline/parts.vim b/autoload/airline/parts.vim index fc39efa..02a6fa5 100644 --- a/autoload/airline/parts.vim +++ b/autoload/airline/parts.vim @@ -38,6 +38,10 @@ endfunction " }}} +function! airline#parts#mode() + return get(w:, 'airline_current_mode', '') +endfunction + function! airline#parts#paste() return g:airline_detect_paste && &paste ? g:airline_symbols.paste : '' endfunction @@ -53,3 +57,15 @@ function! airline#parts#readonly() return &readonly ? g:airline_symbols.readonly : '' endfunction +call airline#parts#define_function('mode', 'airline#parts#mode') +call airline#parts#define_function('iminsert', 'airline#parts#iminsert') +call airline#parts#define_function('paste', 'airline#parts#paste') +call airline#parts#define('readonly', { + \ 'function': 'airline#parts#readonly', + \ 'highlight': 'airline_file', + \ }) +call airline#parts#define_raw('file', '%f%m') +call airline#parts#define_raw('ffenc', '%{printf("%s%s",&fenc,strlen(&ff)>0?"[".&ff."]":"")}') + +call airline#parts#define_empty(['hunks', 'branch', 'tagbar', 'syntastic']) + diff --git a/autoload/airline/util.vim b/autoload/airline/util.vim index 1dd918f..c2306f8 100644 --- a/autoload/airline/util.vim +++ b/autoload/airline/util.vim @@ -1,6 +1,8 @@ " MIT License. Copyright (c) 2013 Bailey Ling. " vim: et ts=2 sts=2 sw=2 +call airline#init#vars() + function! airline#util#wrap(text, minwidth) if a:minwidth > 0 && winwidth(0) < a:minwidth return '' diff --git a/t/airline.vim b/t/airline.vim index b31251e..cb48cf7 100644 --- a/t/airline.vim +++ b/t/airline.vim @@ -42,5 +42,11 @@ describe 'airline' call airline#update_statusline() Expect &statusline =~ 'helloworld' end + + it 'should allow users to redefine sections' + let g:airline_section_a = airline#section#create(['mode', 'mode']) + call airline#update_statusline() + Expect &statusline =~ '%{airline#util#wrap(airline#parts#mode(),0)}%{airline#util#wrap(airline#parts#mode(),0)}' + end end