highlighter.vim 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. " MIT License. Copyright (c) 2013-2021 Bailey Ling Christian Brabandt et al.
  2. " vim: et ts=2 sts=2 sw=2 et
  3. scriptencoding utf-8
  4. let s:is_win32term = (has('win32') || has('win64')) &&
  5. \ !has('gui_running') &&
  6. \ (empty($CONEMUBUILD) || &term !=? 'xterm') &&
  7. \ empty($WT_SESSION) &&
  8. \ !(exists("+termguicolors") && &termguicolors)
  9. let s:separators = {}
  10. let s:accents = {}
  11. let s:hl_groups = {}
  12. if !exists(":def") || !airline#util#has_vim9_script()
  13. " Legacy Vimscript implementation
  14. function! s:gui2cui(rgb, fallback) abort
  15. if a:rgb == ''
  16. return a:fallback
  17. elseif match(a:rgb, '^\%(NONE\|[fb]g\)$') > -1
  18. return a:rgb
  19. elseif a:rgb[0] !~ '#'
  20. " a:rgb contains colorname
  21. return a:rgb
  22. endif
  23. let rgb = map(split(a:rgb[1:], '..\zs'), '0 + ("0x".v:val)')
  24. return airline#msdos#round_msdos_colors(rgb)
  25. endfunction
  26. function! s:group_not_done(list, name) abort
  27. if index(a:list, a:name) == -1
  28. call add(a:list, a:name)
  29. return 1
  30. else
  31. if &vbs
  32. echomsg printf("airline: group: %s already done, skipping", a:name)
  33. endif
  34. return 0
  35. endif
  36. endfu
  37. function! s:get_syn(group, what, mode) abort
  38. let color = ''
  39. if hlexists(a:group)
  40. let color = synIDattr(synIDtrans(hlID(a:group)), a:what, a:mode)
  41. endif
  42. if empty(color) || color == -1
  43. " should always exist
  44. let color = synIDattr(synIDtrans(hlID('Normal')), a:what, a:mode)
  45. " however, just in case
  46. if empty(color) || color == -1
  47. let color = 'NONE'
  48. endif
  49. endif
  50. return color
  51. endfunction
  52. function! s:get_array(guifg, guibg, ctermfg, ctermbg, opts) abort
  53. return [ a:guifg, a:guibg, a:ctermfg, a:ctermbg, empty(a:opts) ? '' : join(a:opts, ',') ]
  54. endfunction
  55. function! airline#highlighter#reset_hlcache() abort
  56. let s:hl_groups = {}
  57. endfunction
  58. function! airline#highlighter#get_highlight(group, ...) abort
  59. " only check for the cterm reverse attribute
  60. " TODO: do we need to check all modes (gui, term, as well)?
  61. let reverse = synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm')
  62. if get(g:, 'airline_highlighting_cache', 0) && has_key(s:hl_groups, a:group)
  63. let res = s:hl_groups[a:group]
  64. return reverse ? [ res[1], res[0], res[3], res[2], res[4] ] : res
  65. else
  66. let ctermfg = s:get_syn(a:group, 'fg', 'cterm')
  67. let ctermbg = s:get_syn(a:group, 'bg', 'cterm')
  68. let guifg = s:get_syn(a:group, 'fg', 'gui')
  69. let guibg = s:get_syn(a:group, 'bg', 'gui')
  70. let bold = synIDattr(synIDtrans(hlID(a:group)), 'bold')
  71. if reverse
  72. let res = s:get_array(guibg, guifg, ctermbg, ctermfg, bold ? ['bold'] : a:000)
  73. else
  74. let res = s:get_array(guifg, guibg, ctermfg, ctermbg, bold ? ['bold'] : a:000)
  75. endif
  76. endif
  77. let s:hl_groups[a:group] = res
  78. return res
  79. endfunction
  80. function! airline#highlighter#get_highlight2(fg, bg, ...) abort
  81. let guifg = s:get_syn(a:fg[0], a:fg[1], 'gui')
  82. let guibg = s:get_syn(a:bg[0], a:bg[1], 'gui')
  83. let ctermfg = s:get_syn(a:fg[0], a:fg[1], 'cterm')
  84. let ctermbg = s:get_syn(a:bg[0], a:bg[1], 'cterm')
  85. return s:get_array(guifg, guibg, ctermfg, ctermbg, a:000)
  86. endfunction
  87. function! s:hl_group_exists(group) abort
  88. if !hlexists(a:group)
  89. return 0
  90. elseif empty(synIDattr(synIDtrans(hlID(a:group)), 'fg'))
  91. return 0
  92. endif
  93. return 1
  94. endfunction
  95. function! s:CheckDefined(colors) abort
  96. " Checks, whether the definition of the colors is valid and is not empty or NONE
  97. " e.g. if the colors would expand to this:
  98. " hi airline_c ctermfg=NONE ctermbg=NONE
  99. " that means to clear that highlighting group, therefore, fallback to Normal
  100. " highlighting group for the cterm values
  101. " This only works, if the Normal highlighting group is actually defined, so
  102. " return early, if it has been cleared
  103. if !exists("g:airline#highlighter#normal_fg_hi")
  104. let g:airline#highlighter#normal_fg_hi = synIDattr(synIDtrans(hlID('Normal')), 'fg', 'cterm')
  105. endif
  106. if empty(g:airline#highlighter#normal_fg_hi) || g:airline#highlighter#normal_fg_hi < 0
  107. return a:colors
  108. endif
  109. for val in a:colors
  110. if !empty(val) && val !=# 'NONE'
  111. return a:colors
  112. endif
  113. endfor
  114. " this adds the bold attribute to the term argument of the :hi command,
  115. " but at least this makes sure, the group will be defined
  116. let fg = g:airline#highlighter#normal_fg_hi
  117. let bg = synIDattr(synIDtrans(hlID('Normal')), 'bg', 'cterm')
  118. if empty(bg) || bg < 0
  119. " in case there is no background color defined for Normal
  120. let bg = a:colors[3]
  121. endif
  122. return a:colors[0:1] + [fg, bg] + [a:colors[4]]
  123. endfunction
  124. function! s:GetHiCmd(list) abort
  125. " a:list needs to have 5 items!
  126. let res = ''
  127. let i = -1
  128. while i < 4
  129. let i += 1
  130. let item = get(a:list, i, '')
  131. if item is ''
  132. continue
  133. endif
  134. if i == 0
  135. let res .= ' guifg='.item
  136. elseif i == 1
  137. let res .= ' guibg='.item
  138. elseif i == 2
  139. let res .= ' ctermfg='.item
  140. elseif i == 3
  141. let res .= ' ctermbg='.item
  142. elseif i == 4
  143. let res .= printf(' gui=%s cterm=%s term=%s', item, item, item)
  144. endif
  145. endwhile
  146. return res
  147. endfunction
  148. function! airline#highlighter#load_theme() abort
  149. if pumvisible()
  150. return
  151. endif
  152. for winnr in filter(range(1, winnr('$')), 'v:val != winnr()')
  153. call airline#highlighter#highlight_modified_inactive(winbufnr(winnr))
  154. endfor
  155. call airline#highlighter#highlight(['inactive'])
  156. if getbufvar( bufnr('%'), '&modified' ) && &buftype != 'terminal'
  157. call airline#highlighter#highlight(['normal', 'modified'])
  158. else
  159. call airline#highlighter#highlight(['normal'])
  160. endif
  161. endfunction
  162. function! airline#highlighter#add_accent(accent) abort
  163. let s:accents[a:accent] = 1
  164. endfunction
  165. function! airline#highlighter#add_separator(from, to, inverse) abort
  166. let s:separators[a:from.a:to] = [a:from, a:to, a:inverse]
  167. call <sid>exec_separator({}, a:from, a:to, a:inverse, '')
  168. endfunction
  169. function! airline#highlighter#remove_separators_for_bufnr(bufnr) abort
  170. " remove all separators, that have the buffer number in their name,
  171. " but do not be too greedy!
  172. let pat = 'c' . a:bufnr . '\(\D\|$\)'
  173. call filter(s:separators, 'v:key !~# pat')
  174. endfunction
  175. function! s:exec_separator(dict, from, to, inverse, suffix) abort
  176. if pumvisible()
  177. return
  178. endif
  179. let group = a:from.'_to_'.a:to.a:suffix
  180. let l:from = airline#themes#get_highlight(a:from.a:suffix)
  181. let l:to = airline#themes#get_highlight(a:to.a:suffix)
  182. if a:inverse
  183. let colors = [ l:from[1], l:to[1], l:from[3], l:to[3] ]
  184. else
  185. let colors = [ l:to[1], l:from[1], l:to[3], l:from[3] ]
  186. endif
  187. let a:dict[group] = colors
  188. call airline#highlighter#exec(group, colors)
  189. endfunction
  190. function! airline#highlighter#highlight_modified_inactive(bufnr) abort
  191. if getbufvar(a:bufnr, '&modified')
  192. let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c')
  193. \ ? g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c : []
  194. else
  195. let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive.airline_c')
  196. \ ? g:airline#themes#{g:airline_theme}#palette.inactive.airline_c : []
  197. endif
  198. if !empty(colors)
  199. call airline#highlighter#exec('airline_c'.(a:bufnr).'_inactive', colors)
  200. endif
  201. endfunction
  202. function! airline#highlighter#exec(group, colors) abort
  203. if pumvisible()
  204. return
  205. endif
  206. let colors = a:colors
  207. if len(colors) == 4
  208. call add(colors, '')
  209. endif
  210. " colors should always be string values
  211. let colors = map(copy(colors), 'type(v:val) != type("") ? string(v:val) : v:val')
  212. if s:is_win32term
  213. let colors[2] = s:gui2cui(get(colors, 0, ''), get(colors, 2, ''))
  214. let colors[3] = s:gui2cui(get(colors, 1, ''), get(colors, 3, ''))
  215. endif
  216. let old_hi = airline#highlighter#get_highlight(a:group)
  217. let new_hi = [colors[0], colors[1], printf('%s', colors[2]), printf('%s', colors[3]), colors[4]]
  218. let colors = s:CheckDefined(colors)
  219. if old_hi != new_hi || !s:hl_group_exists(a:group)
  220. let cmd = printf('hi %s%s', a:group, s:GetHiCmd(colors))
  221. try
  222. exe cmd
  223. catch /^Vim\%((\a\+)\)\=:E421:/ " color definition not found
  224. let group=matchstr(v:exception, '\w\+\ze=')
  225. let color=matchstr(v:exception, '=\zs\w\+')
  226. let cmd=substitute(cmd, color, 'grey', 'g')
  227. exe cmd
  228. call airline#util#warning('color definition for group ' . a:group . ' not found, using grey as fallback')
  229. catch
  230. call airline#util#warning('Error when running command: '. cmd)
  231. endtry
  232. if has_key(s:hl_groups, a:group)
  233. let s:hl_groups[a:group] = colors
  234. endif
  235. endif
  236. endfunction
  237. function! airline#highlighter#highlight(modes, ...) abort
  238. let bufnr = a:0 ? a:1 : ''
  239. let p = g:airline#themes#{g:airline_theme}#palette
  240. " draw the base mode, followed by any overrides
  241. let mapped = map(a:modes, 'v:val == a:modes[0] ? v:val : a:modes[0]."_".v:val')
  242. let suffix = a:modes[0] == 'inactive' ? '_inactive' : ''
  243. let airline_grouplist = []
  244. let buffers_in_tabpage = sort(tabpagebuflist())
  245. if exists("*uniq")
  246. let buffers_in_tabpage = uniq(buffers_in_tabpage)
  247. endif
  248. " mapped might be something like ['normal', 'normal_modified']
  249. " if a group is in both modes available, only define the second
  250. " that is how this was done previously overwrite the previous definition
  251. for mode in reverse(mapped)
  252. if exists('g:airline#themes#{g:airline_theme}#palette[mode]')
  253. let dict = g:airline#themes#{g:airline_theme}#palette[mode]
  254. for kvp in items(dict)
  255. let mode_colors = kvp[1]
  256. let name = kvp[0]
  257. if name is# 'airline_c' && !empty(bufnr) && suffix is# '_inactive'
  258. let name = 'airline_c'.bufnr
  259. endif
  260. " do not re-create highlighting for buffers that are no longer visible
  261. " in the current tabpage
  262. if name =~# 'airline_c\d\+'
  263. let bnr = matchstr(name, 'airline_c\zs\d\+') + 0
  264. if bnr > 0 && index(buffers_in_tabpage, bnr) == -1
  265. continue
  266. endif
  267. elseif (name =~# '_to_') || (name[0:10] is# 'airline_tab' && !empty(suffix))
  268. " group will be redefined below at exec_separator
  269. " or is not needed for tabline with '_inactive' suffix
  270. " since active flag is 1 for builder)
  271. continue
  272. endif
  273. if s:group_not_done(airline_grouplist, name.suffix)
  274. call airline#highlighter#exec(name.suffix, mode_colors)
  275. endif
  276. if !has_key(p, 'accents')
  277. " work around a broken installation
  278. " shouldn't actually happen, p should always contain accents
  279. continue
  280. endif
  281. for accent in keys(s:accents)
  282. if !has_key(p.accents, accent)
  283. continue
  284. endif
  285. let colors = copy(mode_colors)
  286. if p.accents[accent][0] != ''
  287. let colors[0] = p.accents[accent][0]
  288. endif
  289. if p.accents[accent][2] != ''
  290. let colors[2] = p.accents[accent][2]
  291. endif
  292. if len(colors) >= 5
  293. let colors[4] = get(p.accents[accent], 4, '')
  294. else
  295. call add(colors, get(p.accents[accent], 4, ''))
  296. endif
  297. if s:group_not_done(airline_grouplist, name.suffix.'_'.accent)
  298. call airline#highlighter#exec(name.suffix.'_'.accent, colors)
  299. endif
  300. endfor
  301. endfor
  302. if empty(s:separators)
  303. " nothing to be done
  304. continue
  305. endif
  306. " TODO: optimize this
  307. for sep in items(s:separators)
  308. " we cannot check, that the group already exists, else the separators
  309. " might not be correctly defined. But perhaps we can skip above groups
  310. " that match the '_to_' name, because they would be redefined here...
  311. call <sid>exec_separator(dict, sep[1][0], sep[1][1], sep[1][2], suffix)
  312. endfor
  313. endif
  314. endfor
  315. endfunction
  316. " End legacy VimScript
  317. finish
  318. else
  319. " This is using Vim9 script
  320. def s:gui2cui(rgb: string, fallback: string): string
  321. if empty(rgb)
  322. return fallback
  323. elseif match(rgb, '^\%(NONE\|[fb]g\)$') > -1
  324. return rgb
  325. elseif rgb !~ '#'
  326. # rgb contains colorname
  327. return rgb
  328. endif
  329. var _rgb = []
  330. _rgb = mapnew(split(rgb[1 : ], '..\zs'), (_, v) => ('0x' .. v)->str2nr(16))
  331. return airline#msdos#round_msdos_colors(_rgb)
  332. enddef
  333. def s:group_not_done(list: list<string>, name: string): bool
  334. if index(list, name) == -1
  335. add(list, name)
  336. return true
  337. else
  338. if &vbs
  339. echomsg printf("airline: group: %s already done, skipping", name)
  340. endif
  341. return false
  342. endif
  343. enddef
  344. def s:get_syn(group: string, what: string, mode: string): string
  345. var color = ''
  346. if hlexists(group)
  347. color = hlID(group)->synIDtrans()->synIDattr(what, mode)
  348. endif
  349. if empty(color) || str2nr(color) == -1
  350. # Normal highlighting group should always exist
  351. color = hlID('Normal')->synIDtrans()->synIDattr(what, mode)
  352. # however, just in case
  353. if empty(color) || str2nr(color) == -1
  354. color = 'NONE'
  355. endif
  356. endif
  357. return color
  358. enddef
  359. def s:get_array(guifg: string, guibg: string, ctermfg: string, ctermbg: string, opts: list<string>): list<string>
  360. return [ guifg, guibg, ctermfg, ctermbg, empty(opts) ? '' : join(opts, ',') ]
  361. enddef
  362. def airline#highlighter#reset_hlcache(): void
  363. s:hl_groups = {}
  364. enddef
  365. def airline#highlighter#get_highlight(group: string, rest: list<string> = ['']): list<string>
  366. # only check for the cterm reverse attribute
  367. # TODO: do we need to check all modes (gui, term, as well)?
  368. var reverse = false
  369. var bold = false
  370. var property: string
  371. var res = []
  372. var ctermfg: string
  373. var ctermbg: string
  374. var guifg: string
  375. var guibg: string
  376. property = hlID(group)->synIDtrans()->synIDattr('reverse', 'cterm')
  377. if !empty(property) && property->str2nr()
  378. reverse = true
  379. endif
  380. if get(g:, 'airline_highlighting_cache', 0) && has_key(s:hl_groups, group)
  381. res = s:hl_groups[group]
  382. return reverse ? [ res[1], res[0], res[3], res[2], res[4] ] : res
  383. else
  384. ctermfg = s:get_syn(group, 'fg', 'cterm')
  385. ctermbg = s:get_syn(group, 'bg', 'cterm')
  386. guifg = s:get_syn(group, 'fg', 'gui')
  387. guibg = s:get_syn(group, 'bg', 'gui')
  388. property = hlID(group)->synIDtrans()->synIDattr('bold')
  389. if !empty(property) && property->str2nr()
  390. bold = true
  391. endif
  392. if reverse
  393. res = s:get_array(guibg, guifg, ctermbg, ctermfg, bold ? ['bold'] : rest)
  394. else
  395. res = s:get_array(guifg, guibg, ctermfg, ctermbg, bold ? ['bold'] : rest)
  396. endif
  397. endif
  398. s:hl_groups[group] = res
  399. return res
  400. enddef
  401. def airline#highlighter#get_highlight2(fg: list<string>, bg: list<string>, ...rest: list<string>): list<string>
  402. var guifg = s:get_syn(fg[0], fg[1], 'gui')
  403. var guibg = s:get_syn(bg[0], bg[1], 'gui')
  404. var ctermfg = s:get_syn(fg[0], fg[1], 'cterm')
  405. var ctermbg = s:get_syn(bg[0], bg[1], 'cterm')
  406. return s:get_array(guifg, guibg, ctermfg, ctermbg, filter(rest, (_, v) => !empty(v)))
  407. enddef
  408. def s:hl_group_exists(group: string): bool
  409. if !hlexists(group)
  410. return false
  411. elseif hlID(group)->synIDtrans()->synIDattr('fg')->empty()
  412. return false
  413. endif
  414. return true
  415. enddef
  416. def s:CheckDefined(colors: list<any>): list<any>
  417. # Checks, whether the definition of the colors is valid and is not empty or NONE
  418. # e.g. if the colors would expand to this:
  419. # hi airline_c ctermfg=NONE ctermbg=NONE
  420. # that means to clear that highlighting group, therefore, fallback to Normal
  421. # highlighting group for the cterm values
  422. # This only works, if the Normal highlighting group is actually defined,
  423. # so return early, if it has been cleared
  424. if !exists("g:airline#highlighter#normal_fg_hi")
  425. g:airline#highlighter#normal_fg_hi = hlID('Normal')->synIDtrans()->synIDattr('fg', 'cterm')
  426. endif
  427. if empty(g:airline#highlighter#normal_fg_hi) || str2nr(g:airline#highlighter#normal_fg_hi) < 0
  428. return colors
  429. endif
  430. for val in colors
  431. if !empty(val) && val !=# 'NONE'
  432. return colors
  433. endif
  434. endfor
  435. # this adds the bold attribute to the term argument of the :hi command,
  436. # but at least this makes sure, the group will be defined
  437. var fg = g:airline#highlighter#normal_fg_hi
  438. var bg = hlID('Normal')->synIDtrans()->synIDattr('bg', 'cterm')
  439. if empty(bg) || str2nr(bg) < 0
  440. # in case there is no background color defined for Normal
  441. bg = colors[3]
  442. endif
  443. return colors[ 0 : 1 ] + [fg, bg] + [colors[4]]
  444. enddef
  445. def s:GetHiCmd(list: list<string>): string
  446. # list needs to have 5 items!
  447. var res: string
  448. var i = -1
  449. var item: string
  450. while i < 4
  451. i += 1
  452. item = get(list, i, '')
  453. if item is ''
  454. continue
  455. endif
  456. if i == 0
  457. res ..= ' guifg=' .. item
  458. elseif i == 1
  459. res ..= ' guibg=' .. item
  460. elseif i == 2
  461. res ..= ' ctermfg=' .. item
  462. elseif i == 3
  463. res ..= ' ctermbg=' .. item
  464. elseif i == 4
  465. res ..= printf(' gui=%s cterm=%s term=%s', item, item, item)
  466. endif
  467. endwhile
  468. return res
  469. enddef
  470. def airline#highlighter#load_theme(): void
  471. if pumvisible()
  472. return
  473. endif
  474. for winnr in filter(range(1, winnr('$')), (_, v) => v != winnr())
  475. airline#highlighter#highlight_modified_inactive(winbufnr(winnr))
  476. endfor
  477. airline#highlighter#highlight(['inactive'])
  478. if getbufvar( bufnr('%'), '&modified' ) && &buftype != 'terminal'
  479. airline#highlighter#highlight(['normal', 'modified'])
  480. else
  481. airline#highlighter#highlight(['normal'])
  482. endif
  483. enddef
  484. def airline#highlighter#add_accent(accent: string): void
  485. s:accents[accent] = 1
  486. enddef
  487. def airline#highlighter#add_separator(from: string, to: string, inverse: bool): void
  488. s:separators[from .. to] = [from, to, inverse]
  489. s:exec_separator({}, from, to, inverse, '')
  490. enddef
  491. def airline#highlighter#remove_separators_for_bufnr(bufnr: string): void
  492. # remove all separators, that have the bufnr in its name, make sure we
  493. # have a full match here
  494. const pat = $'c{bufnr}\(\D\|$\)'
  495. filter(s:separators, (k, v) => k !~# pat)
  496. enddef
  497. def s:exec_separator(dict: dict<any>, from_arg: string, to_arg: string, inverse: bool, suffix: string): void
  498. if pumvisible()
  499. return
  500. endif
  501. var group = from_arg .. '_to_' .. to_arg .. suffix
  502. var from = mapnew(airline#themes#get_highlight(from_arg .. suffix), (_, v) => type(v) != type('') ? string(v) : v)
  503. var colors = []
  504. var to = mapnew(airline#themes#get_highlight(to_arg .. suffix), (_, v) => type(v) != type('') ? string(v) : v)
  505. if inverse
  506. colors = [ from[1], to[1], from[3], to[3] ]
  507. else
  508. colors = [ to[1], from[1], to[3], from[3] ]
  509. endif
  510. dict[group] = colors
  511. airline#highlighter#exec(group, colors)
  512. enddef
  513. def airline#highlighter#highlight_modified_inactive(bufnr: number): void
  514. var colors: list<any>
  515. var dict1 = eval('g:airline#themes#' .. g:airline_theme .. '#palette')->get('inactive_modified', {})
  516. var dict2 = eval('g:airline#themes#' .. g:airline_theme .. '#palette')->get('inactive', {})
  517. if empty(dict2)
  518. return
  519. endif
  520. if getbufvar(bufnr, '&modified')
  521. colors = get(dict1, 'airline_c', [])
  522. else
  523. colors = get(dict2, 'airline_c', [])
  524. endif
  525. if !empty(colors)
  526. airline#highlighter#exec('airline_c' .. bufnr .. '_inactive', colors)
  527. endif
  528. enddef
  529. def airline#highlighter#exec(group: string, clrs: list<any>): void
  530. if pumvisible()
  531. return
  532. endif
  533. var colors: list<string> = mapnew(copy(clrs), (_, v) => type(v) != type('') ? string(v) : v)
  534. if len(colors) == 4
  535. add(colors, '')
  536. endif
  537. if s:is_win32term
  538. colors[2] = s:gui2cui(get(colors, 0, ''), get(colors, 2, ''))
  539. colors[3] = s:gui2cui(get(colors, 1, ''), get(colors, 3, ''))
  540. endif
  541. var old_hi: list<string> = airline#highlighter#get_highlight(group)
  542. var new_hi: list<string> = colors
  543. if old_hi != new_hi || !s:hl_group_exists(group)
  544. var cmd = printf('hi %s%s', group, s:GetHiCmd(colors))
  545. try
  546. :exe cmd
  547. catch /^Vim\%((\a\+)\)\=:E421:/
  548. var grp = matchstr(v:exception, '\w\+\ze=')
  549. var clr = matchstr(v:exception, '=\zs\w\+')
  550. cmd = substitute(cmd, clr, 'grey', 'g')
  551. :exe cmd
  552. airline#util#warning('color ' .. clr .. ' definition for group ' .. grp .. ' not found, using grey as fallback')
  553. catch
  554. airline#util#warning('Error when running command: ' .. cmd)
  555. endtry
  556. if has_key(s:hl_groups, group)
  557. s:hl_groups[group] = colors
  558. endif
  559. endif
  560. enddef
  561. def airline#highlighter#highlight(modes: list<string>, bufnr: string = ''): void
  562. var p: dict<any> = eval('g:airline#themes#' .. g:airline_theme .. '#palette')
  563. # draw the base mode, followed by any overrides
  564. var mapped = map(modes, (_, v) => v == modes[0] ? v : modes[0] .. "_" .. v)
  565. var suffix = ''
  566. if modes[0] == 'inactive'
  567. suffix = '_inactive'
  568. endif
  569. var airline_grouplist = []
  570. var dict: dict<any>
  571. var bnr: number = 0
  572. var buffers_in_tabpage: list<number> = uniq(sort(tabpagebuflist()))
  573. # mapped might be something like ['normal', 'normal_modified']
  574. # if a group is in both modes available, only define the second
  575. # that is how this was done previously overwrite the previous definition
  576. for mode in reverse(mapped)
  577. if exists('g:airline#themes#' .. g:airline_theme .. '#palette.' .. mode)
  578. dict = eval('g:airline#themes#' .. g:airline_theme .. '#palette.' .. mode)
  579. for kvp in items(dict)
  580. var mode_colors = kvp[1]
  581. var name = kvp[0]
  582. if name == 'airline_c' && !empty(bufnr) && suffix == '_inactive'
  583. name = 'airline_c' .. bufnr
  584. endif
  585. # do not re-create highlighting for buffers that are no longer visible
  586. # in the current tabpage
  587. if name =~# 'airline_c\d\+'
  588. bnr = matchstr(name, 'airline_c\zs\d\+')->str2nr()
  589. if bnr > 0 && index(buffers_in_tabpage, bnr) == -1
  590. continue
  591. endif
  592. elseif (name =~ '_to_') || (name[ 0 : 10 ] == 'airline_tab' && !empty(suffix))
  593. # group will be redefined below at exec_separator
  594. # or is not needed for tabline with '_inactive' suffix
  595. # since active flag is 1 for builder)
  596. continue
  597. endif
  598. if s:group_not_done(airline_grouplist, name .. suffix)
  599. airline#highlighter#exec(name .. suffix, mode_colors)
  600. endif
  601. if !has_key(p, 'accents')
  602. # shouldn't actually happen, p should always contain accents
  603. continue
  604. endif
  605. for accent in keys(s:accents)
  606. if !has_key(p.accents, accent)
  607. continue
  608. endif
  609. var colors = copy(mode_colors)
  610. if p.accents[accent][0] != ''
  611. colors[0] = p.accents[accent][0]
  612. endif
  613. if type(get(p.accents[accent], 2, '')) == type('')
  614. colors[2] = get(p.accents[accent], 2, '')
  615. else
  616. colors[2] = string(p.accents[accent][2])
  617. endif
  618. if len(colors) >= 5
  619. colors[4] = get(p.accents[accent], 4, '')
  620. else
  621. add(colors, get(p.accents[accent], 4, ''))
  622. endif
  623. if s:group_not_done(airline_grouplist, name .. suffix .. '_' .. accent)
  624. airline#highlighter#exec(name .. suffix .. '_' .. accent, colors)
  625. endif
  626. endfor
  627. endfor
  628. if empty(s:separators)
  629. continue
  630. endif
  631. for sep in items(s:separators)
  632. # we cannot check, that the group already exists, else the separators
  633. # might not be correctly defined. But perhaps we can skip above groups
  634. # that match the '_to_' name, because they would be redefined here...
  635. s:exec_separator(dict, sep[1][0], sep[1][1], sep[1][2], suffix)
  636. endfor
  637. endif
  638. endfor
  639. enddef
  640. endif