term.vim 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. " MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
  2. " vim: et ts=2 sts=2 sw=2
  3. scriptencoding utf-8
  4. call airline#parts#define_function('tmode', 'airline#extensions#term#termmode')
  5. call airline#parts#define('terminal', {'text': get(g:airline_mode_map, 't', 't'), 'accent': 'bold'})
  6. function! s:GetAirlineSection()
  7. if exists("g:airline_section_z_term")
  8. let section_z = g:airline_section_z_term
  9. else
  10. let section_z = airline#section#create(['linenr', 'maxlinenr'])
  11. endif
  12. if exists("g:airline_section_a_term")
  13. let section_a = g:airline_section_a_term
  14. else
  15. let section_a = airline#section#create_left(['terminal', 'tmode'])
  16. endif
  17. return [section_a, section_z]
  18. endfunction
  19. function! airline#extensions#term#apply(...) abort
  20. if &buftype ==? 'terminal' || bufname(a:2.bufnr)[0] ==? '!'
  21. let sections = s:GetAirlineSection()
  22. let spc = g:airline_symbols.space
  23. call a:1.add_section_spaced('airline_a', sections[0])
  24. call a:1.add_section_spaced('airline_b', s:neoterm_id(a:2.bufnr))
  25. call a:1.add_section('airline_term', spc.s:termname(a:2.bufnr))
  26. call a:1.split()
  27. call a:1.add_section('airline_y', '')
  28. call a:1.add_section_spaced('airline_z', sections[1])
  29. return 1
  30. endif
  31. endfunction
  32. function! airline#extensions#term#inactive_apply(...) abort
  33. if getbufvar(a:2.bufnr, '&buftype') ==? 'terminal'
  34. let sections = s:GetAirlineSection()
  35. let spc = g:airline_symbols.space
  36. call a:1.add_section_spaced('airline_a', sections[0])
  37. call a:1.add_section_spaced('airline_b', s:neoterm_id(a:2.bufnr))
  38. call a:1.add_section('airline_term', spc.s:termname(a:2.bufnr))
  39. call a:1.split()
  40. call a:1.add_section('airline_y', '')
  41. call a:1.add_section_spaced('airline_z', sections[1])
  42. return 1
  43. endif
  44. endfunction
  45. function! airline#extensions#term#termmode() abort
  46. let mode = airline#parts#mode()[0]
  47. if mode ==? 'T' || mode ==? '-'
  48. " We don't need to output T, the statusline already says "TERMINAL".
  49. " Also we don't want to output "-" on an inactive statusline.
  50. let mode = ''
  51. endif
  52. return mode
  53. endfunction
  54. function! s:termname(bufnr) abort
  55. let bufname = bufname(a:bufnr)
  56. if has('nvim')
  57. " Get rid of the leading "term", working dir and process ID.
  58. " Afterwards, remove the possibly added neoterm ID.
  59. return substitute(matchstr(bufname, 'term.*:\zs.*'),
  60. \ ';#neoterm-\d\+', '', '')
  61. else
  62. if bufname =~? 'neoterm-\d\+'
  63. " Do not return a redundant buffer name, when this is a neoterm terminal.
  64. return ''
  65. endif
  66. " Get rid of the leading "!".
  67. if bufname[0] ==? '!'
  68. return bufname[1:]
  69. else
  70. return bufname
  71. endif
  72. endif
  73. endfunction
  74. function! s:neoterm_id(bufnr) abort
  75. let id = getbufvar(a:bufnr, 'neoterm_id')
  76. if id !=? ''
  77. let id = 'neoterm-'.id
  78. endif
  79. return id
  80. endfunction
  81. function! airline#extensions#term#init(ext) abort
  82. call a:ext.add_statusline_func('airline#extensions#term#apply')
  83. call a:ext.add_inactive_statusline_func('airline#extensions#term#inactive_apply')
  84. endfunction