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