fugitiveline.vim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. " MIT License. Copyright (c) 2017-2021 Cimbali et al
  2. " Plugin: https://github.com/tpope/vim-fugitive
  3. " vim: et ts=2 sts=2 sw=2
  4. scriptencoding utf-8
  5. if !airline#util#has_fugitive()
  6. finish
  7. endif
  8. let s:has_percent_eval = v:version > 802 || (v:version == 802 && has("patch2854"))
  9. function! airline#extensions#fugitiveline#bufname() abort
  10. if !exists('b:fugitive_name')
  11. let b:fugitive_name = ''
  12. try
  13. if bufname('%') =~? '^fugitive:' && exists('*FugitiveReal')
  14. let b:fugitive_name = FugitiveReal(bufname('%'))
  15. endif
  16. catch
  17. endtry
  18. endif
  19. let fmod = (exists("+autochdir") && &autochdir) ? ':p' : ':.'
  20. let result=''
  21. if empty(b:fugitive_name)
  22. if empty(bufname('%'))
  23. return &buftype ==# 'nofile' ? '[Scratch]' : '[No Name]'
  24. endif
  25. return s:has_percent_eval ? '%f' : fnamemodify(bufname('%'), fmod)
  26. else
  27. return s:has_percent_eval ? '%f [git]' : (fnamemodify(b:fugitive_name, fmod). " [git]")
  28. endif
  29. endfunction
  30. function! s:sh_autocmd_handler()
  31. if exists('#airline')
  32. unlet! b:fugitive_name
  33. endif
  34. endfunction
  35. function! airline#extensions#fugitiveline#init(ext) abort
  36. let prct = s:has_percent_eval ? '%' : ''
  37. if exists("+autochdir") && &autochdir
  38. " if 'acd' is set, vim-airline uses the path section, so we need to redefine this here as well
  39. if get(g:, 'airline_stl_path_style', 'default') ==# 'short'
  40. call airline#parts#define_raw('path', '%<%{'. prct. 'pathshorten(airline#extensions#fugitiveline#bufname())' . prct . '}%m')
  41. else
  42. call airline#parts#define_raw('path', '%<%{' . prct . 'airline#extensions#fugitiveline#bufname()' . prct . '}%m')
  43. endif
  44. else
  45. if get(g:, 'airline_stl_path_style', 'default') ==# 'short'
  46. call airline#parts#define_raw('file', '%<%{' . prct . 'pathshorten(airline#extensions#fugitiveline#bufname())' . prct . '}%m')
  47. else
  48. call airline#parts#define_raw('file', '%<%{' . prct . 'airline#extensions#fugitiveline#bufname()' . prct . '}%m')
  49. endif
  50. endif
  51. autocmd ShellCmdPost,CmdwinLeave * call s:sh_autocmd_handler()
  52. autocmd User AirlineBeforeRefresh call s:sh_autocmd_handler()
  53. endfunction