parts.vim 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. let s:parts = {}
  5. " PUBLIC API {{{
  6. function! airline#parts#define(key, config)
  7. let s:parts[a:key] = get(s:parts, a:key, {})
  8. if exists('g:airline#init#bootstrapping')
  9. call extend(s:parts[a:key], a:config, 'keep')
  10. else
  11. call extend(s:parts[a:key], a:config, 'force')
  12. endif
  13. endfunction
  14. function! airline#parts#define_function(key, name)
  15. call airline#parts#define(a:key, { 'function': a:name })
  16. endfunction
  17. function! airline#parts#define_text(key, text)
  18. call airline#parts#define(a:key, { 'text': a:text })
  19. endfunction
  20. function! airline#parts#define_raw(key, raw)
  21. call airline#parts#define(a:key, { 'raw': a:raw })
  22. endfunction
  23. function! airline#parts#define_minwidth(key, width)
  24. call airline#parts#define(a:key, { 'minwidth': a:width })
  25. endfunction
  26. function! airline#parts#define_condition(key, predicate)
  27. call airline#parts#define(a:key, { 'condition': a:predicate })
  28. endfunction
  29. function! airline#parts#define_accent(key, accent)
  30. call airline#parts#define(a:key, { 'accent': a:accent })
  31. endfunction
  32. function! airline#parts#define_empty(keys)
  33. for key in a:keys
  34. call airline#parts#define_raw(key, '')
  35. endfor
  36. endfunction
  37. function! airline#parts#get(key)
  38. return get(s:parts, a:key, {})
  39. endfunction
  40. " }}}
  41. function! airline#parts#mode()
  42. let part = airline#parts#get('mode')
  43. let minwidth = get(part, 'minwidth', 79)
  44. return airline#util#shorten(get(w:, 'airline_current_mode', ''), minwidth, 1)
  45. endfunction
  46. function! airline#parts#crypt()
  47. return g:airline_detect_crypt && exists("+key") && !empty(&key) ? g:airline_symbols.crypt : ''
  48. endfunction
  49. function! airline#parts#paste()
  50. return g:airline_detect_paste && &paste ? g:airline_symbols.paste : ''
  51. endfunction
  52. " Sources:
  53. " https://ftp.nluug.nl/pub/vim/runtime/spell/
  54. " https://en.wikipedia.org/wiki/Regional_indicator_symbol
  55. let s:flags = {
  56. \ 'af_za': '🇿🇦[af]',
  57. \ 'am_et': '🇭🇺[am]',
  58. \ 'bg_bg': '🇧🇬',
  59. \ 'br_fr': '🇫🇷[br]',
  60. \ 'ca_es': '🇪🇸[ca]',
  61. \ 'cs_cz': '🇨🇿',
  62. \ 'cy_gb': '🇬🇧[cy]',
  63. \ 'da_dk': '🇩🇰',
  64. \ 'de' : '🇩🇪',
  65. \ 'de_19': '🇩🇪[19]',
  66. \ 'de_20': '🇩🇪[20]',
  67. \ 'de_at': '🇩🇪[at]',
  68. \ 'de_ch': '🇩🇪[ch]',
  69. \ 'de_de': '🇩🇪',
  70. \ 'el_gr': '🇬🇷',
  71. \ 'en': '🇬🇧',
  72. \ 'en_au': '🇦🇺',
  73. \ 'en_ca': '🇨🇦',
  74. \ 'en_gb': '🇬🇧',
  75. \ 'en_nz': '🇳🇿',
  76. \ 'en_us': '🇺🇸',
  77. \ 'es': '🇪🇸',
  78. \ 'es_es': '🇪🇸',
  79. \ 'es_mx': '🇲🇽',
  80. \ 'fo_fo': '🇫🇴',
  81. \ 'fr_fr': '🇫🇷',
  82. \ 'ga_ie': '🇮🇪',
  83. \ 'gd_gb': '🇬🇧[gd]',
  84. \ 'gl_es': '🇪🇸[gl]',
  85. \ 'he_il': '🇮🇱',
  86. \ 'hr_hr': '🇭🇷',
  87. \ 'hu_hu': '🇭🇺',
  88. \ 'id_id': '🇮🇩',
  89. \ 'it_it': '🇮🇹',
  90. \ 'ku_tr': '🇹🇷[ku]',
  91. \ 'la' : '🇮🇹[la]',
  92. \ 'lt_lt': '🇱🇹',
  93. \ 'lv_lv': '🇱🇻',
  94. \ 'mg_mg': '🇲🇬',
  95. \ 'mi_nz': '🇳🇿[mi]',
  96. \ 'ms_my': '🇲🇾',
  97. \ 'nb_no': '🇳🇴',
  98. \ 'nl_nl': '🇳🇱',
  99. \ 'nn_no': '🇳🇴[ny]',
  100. \ 'ny_mw': '🇲🇼',
  101. \ 'pl_pl': '🇵🇱',
  102. \ 'pt': '🇵🇹',
  103. \ 'pt_br': '🇧🇷',
  104. \ 'pt_pt': '🇵🇹',
  105. \ 'ro_ro': '🇷🇴',
  106. \ 'ru' : '🇷🇺',
  107. \ 'ru_ru': '🇷🇺',
  108. \ 'ru_yo': '🇷🇺[yo]',
  109. \ 'rw_rw': '🇷🇼',
  110. \ 'sk_sk': '🇸🇰',
  111. \ 'sl_si': '🇸🇮',
  112. \ 'sr_rs': '🇷🇸',
  113. \ 'sv_se': '🇸🇪',
  114. \ 'sw_ke': '🇰🇪',
  115. \ 'tet_id': '🇮🇩[tet]',
  116. \ 'th' : '🇹🇭',
  117. \ 'tl_ph': '🇵🇭',
  118. \ 'tn_za': '🇿🇦[tn]',
  119. \ 'uk_ua': '🇺🇦',
  120. \ 'yi' : '🇻🇮',
  121. \ 'yi_tr': '🇹🇷',
  122. \ 'zu_za': '🇿🇦[zu]',
  123. \ }
  124. " Also support spelllang without region codes
  125. let s:flags_noregion = {}
  126. for s:key in keys(s:flags)
  127. let s:flags_noregion[split(s:key, '_')[0]] = s:flags[s:key]
  128. endfor
  129. function! airline#parts#spell()
  130. let spelllang = g:airline_detect_spelllang ? printf(" [%s]", toupper(substitute(&spelllang, ',', '/', 'g'))) : ''
  131. if g:airline_detect_spell && (&spell || (exists('g:airline_spell_check_command') && eval(g:airline_spell_check_command)))
  132. if g:airline_detect_spelllang !=? '0' && g:airline_detect_spelllang ==? 'flag'
  133. let spelllang = tolower(&spelllang)
  134. if has_key(s:flags, spelllang)
  135. return s:flags[spelllang]
  136. elseif has_key(s:flags_noregion, spelllang)
  137. return s:flags_noregion[spelllang]
  138. endif
  139. endif
  140. let winwidth = airline#util#winwidth()
  141. if winwidth >= 90
  142. return g:airline_symbols.spell . spelllang
  143. elseif winwidth >= 70
  144. return g:airline_symbols.spell
  145. elseif !empty(g:airline_symbols.spell)
  146. return split(g:airline_symbols.spell, '\zs')[0]
  147. endif
  148. endif
  149. return ''
  150. endfunction
  151. function! airline#parts#iminsert()
  152. if g:airline_detect_iminsert && &iminsert && exists('b:keymap_name')
  153. return toupper(b:keymap_name)
  154. endif
  155. return ''
  156. endfunction
  157. function! airline#parts#readonly()
  158. " only consider regular buffers (e.g. ones that represent actual files,
  159. " but not special ones like e.g. NERDTree)
  160. if !empty(&buftype) || airline#util#ignore_buf(bufname('%'))
  161. return ''
  162. endif
  163. if &readonly && !filereadable(bufname('%'))
  164. return '[noperm]'
  165. else
  166. return &readonly ? g:airline_symbols.readonly : ''
  167. endif
  168. endfunction
  169. function! airline#parts#filetype()
  170. return (airline#util#winwidth() < 90 && strlen(&filetype) > 3)
  171. \ ? matchstr(&filetype, '...'). (&encoding is? 'utf-8' ? "\u2026" : '>')
  172. \ : &filetype
  173. endfunction
  174. function! airline#parts#ffenc()
  175. let expected = get(g:, 'airline#parts#ffenc#skip_expected_string', '')
  176. let bomb = &bomb ? '[BOM]' : ''
  177. let noeolf = &eol ? '' : '[!EOL]'
  178. let ff = strlen(&ff) ? '['.&ff.']' : ''
  179. if expected is# &fenc.bomb.noeolf.ff
  180. return ''
  181. else
  182. return &fenc.bomb.noeolf.ff
  183. endif
  184. endfunction