From fa517238c771c9749b1e87870ced8b61fb8bb169 Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Fri, 30 Aug 2013 21:51:10 +0000 Subject: [PATCH] introduce parts metadata. --- autoload/airline/extensions/branch.vim | 2 +- autoload/airline/extensions/bufferline.vim | 3 +- autoload/airline/extensions/hunks.vim | 2 +- autoload/airline/parts.vim | 25 +++++++++++---- autoload/airline/util.vim | 35 +++----------------- plugin/airline.vim | 37 +++++++++++++--------- 6 files changed, 49 insertions(+), 55 deletions(-) diff --git a/autoload/airline/extensions/branch.vim b/autoload/airline/extensions/branch.vim index 14dc8af..446c89d 100644 --- a/autoload/airline/extensions/branch.vim +++ b/autoload/airline/extensions/branch.vim @@ -31,6 +31,6 @@ function! airline#extensions#branch#get_head() endfunction function! airline#extensions#branch#init(ext) - let g:airline_parts.branch = 'airline#extensions#branch#get_head' + call airline#parts#define_function('branch', 'airline#extensions#branch#get_head') endfunction diff --git a/autoload/airline/extensions/bufferline.vim b/autoload/airline/extensions/bufferline.vim index 4a6e014..cc13ec0 100644 --- a/autoload/airline/extensions/bufferline.vim +++ b/autoload/airline/extensions/bufferline.vim @@ -14,5 +14,6 @@ function! airline#extensions#bufferline#init(ext) let g:bufferline_separator = ' ' endif - let g:airline_parts.file = '%{bufferline#refresh_status()}'.bufferline#get_status_string() + call airline#parts#define_raw('file', '%{bufferline#refresh_status()}'.bufferline#get_status_string()) endfunction + diff --git a/autoload/airline/extensions/hunks.vim b/autoload/airline/extensions/hunks.vim index 0c5ee50..803dfaf 100644 --- a/autoload/airline/extensions/hunks.vim +++ b/autoload/airline/extensions/hunks.vim @@ -54,6 +54,6 @@ function! airline#extensions#hunks#get_hunks() endfunction function! airline#extensions#hunks#init(ext) - let g:airline_parts.hunks = 'airline#extensions#hunks#get_hunks' + call airline#parts#define_function('hunks', 'airline#extensions#hunks#get_hunks') endfunction diff --git a/autoload/airline/parts.vim b/autoload/airline/parts.vim index 63fe741..42ab21e 100644 --- a/autoload/airline/parts.vim +++ b/autoload/airline/parts.vim @@ -1,14 +1,27 @@ " MIT License. Copyright (c) 2013 Bailey Ling. " vim: et ts=2 sts=2 sw=2 -function! airline#parts#append(name) - let val = function(a:name)() - return empty(val) ? '' : ' '.g:airline_left_alt_sep.' '.val +let s:parts = {} + +function! airline#parts#define(key, config) + let s:parts[a:key] = get(s:parts, a:key, {}) + call extend(s:parts[a:key], a:config) endfunction -function! airline#parts#prepend(name) - let val = function(a:name)() - return empty(val) ? '' : val.' '.g:airline_right_alt_sep.' ' +function! airline#parts#define_function(key, name) + call airline#parts#define(a:key, { 'function': a:name }) +endfunction + +function! airline#parts#define_text(key, text) + call airline#parts#define(a:key, { 'text': a:text }) +endfunction + +function! airline#parts#define_raw(key, raw) + call airline#parts#define(a:key, { 'raw': a:raw }) +endfunction + +function! airline#parts#get(key) + return get(s:parts, a:key, {}) endfunction function! airline#parts#empty() diff --git a/autoload/airline/util.vim b/autoload/airline/util.vim index 3de33c3..e7b27f8 100644 --- a/autoload/airline/util.vim +++ b/autoload/airline/util.vim @@ -1,39 +1,12 @@ " MIT License. Copyright (c) 2013 Bailey Ling. " vim: et ts=2 sts=2 sw=2 -function! s:get_val(part, append) - if has_key(g:airline_parts, a:part) - let val = g:airline_parts[a:part] - if match(val, '%\| ') > -1 - return val - elseif a:append > 0 - return '%{airline#parts#append("'.val.'")}' - elseif a:append < 0 - return '%{airline#parts#prepend("'.val.'")}' - else - return '%{'.val.'()}' - endif - endif - return a:part +function! airline#util#append(text) + return empty(a:text) ? '' : ' '.g:airline_left_alt_sep.' '.a:text endfunction -function! airline#util#define_left_section(key, parts) - if !exists('g:airline_section_{a:key}') && len(a:parts) > 0 - let g:airline_section_{a:key} = s:get_val(a:parts[0], 0) - for i in range(1, len(a:parts) - 1) - let g:airline_section_{a:key} .= s:get_val(a:parts[i], 1) - endfor - endif -endfunction - -function! airline#util#define_right_section(key, parts) - if !exists('g:airline_section_{a:key}') && len(a:parts) > 0 - let g:airline_section_{a:key} = '' - for i in range(0, len(a:parts) - 2) - let g:airline_section_{a:key} .= s:get_val(a:parts[i], -1) - endfor - let g:airline_section_{a:key} .= s:get_val(a:parts[-1], 0) - endif +function! airline#util#prepend(text) + return empty(a:text) ? '' : a:text.' '.g:airline_right_alt_sep.' ' endfunction if v:version >= 704 diff --git a/plugin/airline.vim b/plugin/airline.vim index 7e64569..b54ae3c 100644 --- a/plugin/airline.vim +++ b/plugin/airline.vim @@ -41,16 +41,18 @@ function! s:init() \ 'branch': get(g:, 'airline_branch_prefix', get(g:, 'airline_powerline_fonts', 0) ? '' : ''), \ }, 'keep') + 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 s:check_defined('g:airline_parts', {}) call extend(g:airline_parts, { - \ 'mode': 'airline#parts#mode', - \ 'iminsert': 'airline#parts#iminsert', - \ 'paste': 'airline#parts#paste', - \ 'readonly': '%#airline_file#%{airline#parts#readonly()}', \ 'ffenc': '%{printf("%s%s",&fenc,strlen(&ff)>0?"[".&ff."]":"")}', - \ 'file': '%f%m', - \ 'hunks': 'airline#parts#empty', - \ 'branch': 'airline#parts#empty', \ 'tagbar': 'airline#parts#empty', \ 'syntastic': 'airline#parts#empty', \ 'whitespace': 'airline#parts#empty', @@ -80,14 +82,19 @@ function! s:init() \ }, 'keep') call airline#extensions#load() - call airline#util#define_left_section('a', ['mode', 'paste', 'iminsert']) - call airline#util#define_left_section('b', ['hunks', 'branch']) - call airline#util#define_left_section('c', ['%<', 'file']) - call airline#util#define_left_section('gutter', ['readonly', '%=']) - call airline#util#define_right_section('x', ['tagbar', '%{&filetype}']) - call airline#util#define_right_section('y', ['ffenc']) - call airline#util#define_right_section('z', ['%3p%% %{g:airline_symbols.linenr} %3l:%3c ']) - call airline#util#define_right_section('warning', ['syntastic', 'whitespace']) + + if !exists('g:airline_section_a') + let g:airline_section_a = airline#section#create_left(['mode', 'paste', 'iminsert']) + endif + if !exists('g:airline_section_b') + let g:airline_section_b = airline#section#create(['hunks', 'branch']) + endif + let g:airline_section_c = airline#section#create(['%<', 'file']) + let g:airline_section_gutter = airline#section#create([' ', 'readonly', '%=']) + let g:airline_section_x = airline#section#create(['tagbar', '%{&filetype}']) + let g:airline_section_y = airline#section#create(['ffenc']) + let g:airline_section_z = airline#section#create(['%3p%% %{g:airline_symbols.linenr} %3l:%3c ']) + let g:airline_section_warning = airline#section#create(['syntastic', 'whitespace']) let s:airline_theme_defined = exists('g:airline_theme') if s:airline_theme_defined || !airline#switch_matching_theme()