mirror of
https://github.com/vim-airline/vim-airline.git
synced 2025-12-06 20:24:27 +08:00
improve documentation for writing extensions, also add helper methods.
This commit is contained in:
@@ -6,33 +6,35 @@ 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_func', which as the name implies, adds a function 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.
|
||||
" There are predominantly two methods for integrating a plugin into
|
||||
" vim-airline. The first method here simply modifies the global section and
|
||||
" appends information to it. This is useful for cases where the information
|
||||
" should be displayed all the time for all filetypes.
|
||||
function! airline#extensions#example#init(ext)
|
||||
let g:airline_section_y .= '%{airline#extensions#example#get_cats()}'
|
||||
endfunction
|
||||
|
||||
" The second method involves using the 'ext'ension manager that was passed in
|
||||
" and appends a name of a function. This function will be invoked just prior
|
||||
" to updating the statusline. This method is useful for plugin-specific
|
||||
" statuslines (like NERDTree or Tagbar) or language specific plugins (like
|
||||
" virtualenv) which do not need to be loaded all the time.
|
||||
function! airline#extensions#example#init(ext)
|
||||
call a:ext.add_statusline_func('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()}'
|
||||
" There is also the following function for making changes just prior to an
|
||||
" inactive statusline.
|
||||
" call a:ext.add_inactive_statusline_func('airline#extensions#example#unapply')
|
||||
endfunction
|
||||
|
||||
" This function will be invoked just prior to the statusline getting modified.
|
||||
function! airline#extensions#example#apply(...)
|
||||
" Here we are checking for the filetype, allowing for the extension to
|
||||
" be loaded only in certain cases.
|
||||
" First we check for the filetype.
|
||||
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()}'
|
||||
" Let's use a helper function. It will take care of ensuring that the
|
||||
" window-local override exists (and create one based on the global
|
||||
" airline_section if not), and prepend to it.
|
||||
call airline#extensions#prepend_to_section('x', '%{airline#extensions#example#get_cats()} ')
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
Reference in New Issue
Block a user