From ca39454ecf5e06a160b9bca28cd43cee39561190 Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Fri, 16 Aug 2013 18:04:03 +0000 Subject: [PATCH] add example.vim extension (#134) --- autoload/airline/extensions/example.vim | 47 +++++++++++++++++++++++++ doc/airline.txt | 2 ++ 2 files changed, 49 insertions(+) create mode 100644 autoload/airline/extensions/example.vim diff --git a/autoload/airline/extensions/example.vim b/autoload/airline/extensions/example.vim new file mode 100644 index 00000000..d9281204 --- /dev/null +++ b/autoload/airline/extensions/example.vim @@ -0,0 +1,47 @@ +" MIT License. Copyright (c) 2013 Bailey Ling. +" vim: ts=2 sts=2 sw=2 fdm=indent + +" Extension specific variables can be defined the usual fashion. +if !exists('g:airline#extensions#example#number_of_cats') + let g:airline#extensions#example#number_of_cats = 42 +endif + +" First you should follow the convention and define an 'init' function. +" It takes a single argument, which is the 'ext'ension manager of sorts, +" which you can invoke certain functions. The most important one is +" 'add_statusline_funcref', which as the name implies, adds a funcref to +" the collection such that it will be invoked prior to changes being made +" to the statusline. Finally, invoke this init function in the +" 'extensions.vim' file after a check to a non-autoloaded variable, +" command, or function. +function! airline#extensions#example#init(ext) + call a:ext.add_statusline_funcref(function('airline#extensions#example#apply')) + + " Alternatively, you can also modify the default global section by + " appending or prepending to it. But read on to see why using the funcref + " method is preferred. + let g:airline_section_y .= '%{airline#extensions#example#nyancat()}' +endfunction + +function! airline#extensions#example#apply() + " Here we are checking for the filetype, allowing for the extension to + " be loaded only in certain cases. + if &filetype == "nyancat" + + " Then we define a window-local variable, which overrides the default + " g: variable. + let w:airline_section_gutter = + \ g:airline_section_gutter + \ .' %{airline#extensions#example#get_cats()}' + endif +endfunction + +" Finally, this function will be invoked from the statusline. +function! airline#extensions#example#get_cats() + let cats = '' + for i in range(1, g:airline#extensions#example#number_of_cats) + let cats .= ' (,,,)=(^.^)=(,,,) ' + endfor + return cats +endfunction + diff --git a/doc/airline.txt b/doc/airline.txt index 7d5cc262..b82d7732 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -267,6 +267,8 @@ For contributions into the plugin, here are the following guidelines: branch extension does not follow this convention, but for legacy reasons those variables are kept as is. +See the source of |example.vim| for a working extension. + ============================================================================== WRITING THEMES *airline-themes*