From 8209ca7da10fe08c1e55865a01e189254bb785dc Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Fri, 9 Mar 2018 11:10:45 -0800 Subject: [PATCH] Make branch detection customizable Instead of requiring each version control plugin to modify airline to show the current branch, provide a customization function we can check instead. Following the example of airline_theme_patch_func, you define the variable like so: let g:airline#extensions#branch#custom_head = 'david#svn#get_branch' Custom functions should cache their value. They may need an autocmd to invalidate their cache: " Use a buffer-unique group name to prevent clearing autocmds for other " buffers. exec 'augroup svndavid-'. bufnr("%") au! autocmd BufWinLeave unlet! b:svndavid_branch augroup END This change lets me integrate with vc.vim (I couldn't get VCSCommand working for svn) or write my own thing for perforce. Additionally, always load whole file and check for existence. Instead of determining up front whether various scm plugins are installed, check for them on use so they can be added after this script is sourced. This also mitigates the problem of checking for existence of autoload functions (which are not loaded by exist()). Since we're checking root-level functions, they're likely to be loaded once we're using any part of the plugin. --- autoload/airline/extensions/branch.vim | 34 ++++++++++++++++++-------- doc/airline.txt | 17 +++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/autoload/airline/extensions/branch.vim b/autoload/airline/extensions/branch.vim index 0f5f2f8..3fb6614 100644 --- a/autoload/airline/extensions/branch.vim +++ b/autoload/airline/extensions/branch.vim @@ -3,13 +3,18 @@ scriptencoding utf-8 -let s:has_fugitive = exists('*fugitive#head') -let s:has_lawrencium = exists('*lawrencium#statusline') -let s:has_vcscommand = get(g:, 'airline#extensions#branch#use_vcscommand', 0) && exists('*VCSCommandGetStatusLine') - -if !s:has_fugitive && !s:has_lawrencium && !s:has_vcscommand - finish -endif +function! s:has_fugitive() + return exists('*fugitive#head') +endfunction +function! s:has_lawrencium() + return exists('*lawrencium#statusline') +endfunction +function! s:has_vcscommand() + return get(g:, 'airline#extensions#branch#use_vcscommand', 0) && exists('*VCSCommandGetStatusLine') +endfunction +function! s:has_custom_scm() + return !empty(get(g:, 'airline#extensions#branch#custom_head', '')) +endfunction " s:vcs_config contains static configuration of VCSes and their status relative " to the active file. @@ -89,7 +94,7 @@ let s:names = {'0': 'index', '1': 'orig', '2':'fetch', '3':'merge'} let s:sha1size = get(g:, 'airline#extensions#branch#sha1_len', 7) function! s:update_git_branch() - if !s:has_fugitive + if !s:has_fugitive() let s:vcs_config['git'].branch = '' return endif @@ -123,7 +128,7 @@ function! s:display_git_branch() endfunction function! s:update_hg_branch() - if s:has_lawrencium + if s:has_lawrencium() let cmd='LC_ALL=C hg qtop' let stl=lawrencium#statusline() let file=expand('%:p') @@ -248,7 +253,7 @@ function! airline#extensions#branch#head() endfor if empty(heads) - if s:has_vcscommand + if s:has_vcscommand() call VCSCommandEnableBufferSetup() if exists('b:VCSCommandBufferInfo') let b:airline_head = s:format_name(get(b:VCSCommandBufferInfo, 0, '')) @@ -256,6 +261,15 @@ function! airline#extensions#branch#head() endif endif + if empty(heads) + if s:has_custom_scm() + try + let Fn = function(g:airline#extensions#branch#custom_head) + let b:airline_head = Fn() + endtry + endif + endif + if exists("g:airline#extensions#branch#displayed_head_limit") let w:displayed_head_limit = g:airline#extensions#branch#displayed_head_limit if len(b:airline_head) > w:displayed_head_limit - 1 diff --git a/doc/airline.txt b/doc/airline.txt index c0ac19b..7d86382 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -459,6 +459,23 @@ notexists symbol will be displayed after the branch name. < * truncate sha1 commits at this number of characters > let g:airline#extensions#branch#sha1_len = 10 + +* customize branch name retrieval for any version control system > + let g:airline#extensions#branch#custom_head = 'GetScmBranch' + function! GetScmBranch() + if !exists('b:perforce_client') + let b:perforce_client = system('p4 client -o | grep Client') + " Invalidate cache to prevent stale data when switching clients. Use a + " buffer-unique group name to prevent clearing autocmds for other + " buffers. + exec 'augroup perforce_client-'. bufnr("%") + au! + autocmd BufWinLeave silent! unlet! b:perforce_client + augroup END + endif + return b:perforce_client + endfunction + < ------------------------------------- *airline-syntastic* syntastic