robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1// highlight.c: low level code for UI and syntax highlighting 2 3#include <assert.h> 4#include <inttypes.h> 5#include <lauxlib.h> 6#include <string.h> 7 8#include "nvim/api/keysets_defs.h" 9#include "nvim/api/private/defs.h" 10#include "nvim/api/private/dispatch.h" 11#include "nvim/api/private/helpers.h" 12#include "nvim/api/private/validate.h" 13#include "nvim/api/ui.h" 14#include "nvim/decoration_defs.h" 15#include "nvim/decoration_provider.h" 16#include "nvim/drawscreen.h" 17#include "nvim/gettext_defs.h" 18#include "nvim/globals.h" 19#include "nvim/highlight.h" 20#include "nvim/highlight_defs.h" 21#include "nvim/highlight_group.h" 22#include "nvim/lua/executor.h" 23#include "nvim/macros_defs.h" 24#include "nvim/map_defs.h" 25#include "nvim/memory.h" 26#include "nvim/memory_defs.h" 27#include "nvim/message.h" 28#include "nvim/option.h" 29#include "nvim/popupmenu.h" 30#include "nvim/strings.h" 31#include "nvim/types_defs.h" 32#include "nvim/ui.h" 33#include "nvim/vim_defs.h" 34 35#include "highlight.c.generated.h" 36 37static bool hlstate_active = false; 38 39static Set (HlEntry )attr_entries = SET_INIT ; 40static Map (uint64_t ,int )combine_attr_entries = MAP_INIT ; 41static Map (uint64_t ,int )blend_attr_entries = MAP_INIT ; 42static Map (uint64_t ,int )blendthrough_attr_entries = MAP_INIT ; 43static Set (cstr_t )urls = SET_INIT ; 44/// Interned font names, referenced by index in HlAttrs.font. 45static Set (cstr_t )fonts = SET_INIT ; 46 47#define attr_entry (i ) attr_entries.keys[i] 48 49/// highlight entries private to a namespace 50static Map (ColorKey ,ColorItem )ns_hls ; 51typedef int NSHlAttr [HLF_COUNT ]; 52static PMap (int )ns_hl_attr ; 53 54void highlight_init (void ) 55{ 56// index 0 is no attribute, add dummy entry: 57set_put (HlEntry ,& attr_entries , ((HlEntry ){ .attr = HLATTRS_INIT , .kind = kHlInvalid , 58 .id1 = 0 , .id2 = 0 })); 59} 60 61/// @return true if hl table was reset 62bool highlight_use_hlstate (void ) 63{ 64if (hlstate_active ) { 65return false; 66 } 67hlstate_active = true; 68// hl tables must now be rebuilt. 69clear_hl_tables (true); 70return true; 71} 72 73/// Return the attr number for a set of colors and font, and optionally 74/// a semantic description (see ext_hlstate documentation). 75/// Add a new entry to the attr_entries array if the combination is new. 76/// @return 0 for error. 77static int get_attr_entry (HlEntry entry ) 78{ 79bool retried = false; 80if (!hlstate_active ) { 81// This information will not be used, erase it and reduce the table size. 82entry .kind = kHlUnknown ; 83entry .id1 = 0 ; 84entry .id2 = 0 ; 85 } 86 87retry : {} 88MHPutStatus status ; 89uint32_t k = set_put_idx (HlEntry ,& attr_entries ,entry ,& status ); 90if (status == kMHExisting ) { 91return (int )k ; 92 } 93 94static bool recursive = false; 95if (set_size (& attr_entries )> MAX_TYPENR ) { 96// Running out of attribute entries! remove all attributes, and 97// compute new ones for all groups. 98// When called recursively, we are really out of numbers. 99if (recursive || retried ) { 100emsg (_ ("E424: Too many different highlighting attributes in use" )); 101return 0 ; 102 } 103recursive = true; 104 105clear_hl_tables (true); 106 107recursive = false; 108if (entry .kind == kHlCombine ) { 109// This entry is now invalid, don't put it 110return 0 ; 111 } 112retried = true; 113 gotoretry ; 114 } 115 116// new attr id, send event to remote ui:s 117int id = (int )k ; 118 119Arena arena = ARENA_EMPTY ; 120Array inspect = hl_inspect (id ,& arena ); 121 122// Note: internally we don't distinguish between cterm and rgb attributes, 123// remote_ui_hl_attr_define will however. 124ui_call_hl_attr_define (id ,entry .attr ,entry .attr ,inspect ); 125arena_mem_free (arena_finish (& arena )); 126return id ; 127} 128 129/// When a UI connects, we need to send it the table of highlights used so far. 130void ui_send_all_hls (RemoteUI * ui ) 131{ 132for (size_t i = 1 ;i < set_size (& attr_entries );i ++ ) { 133Arena arena = ARENA_EMPTY ; 134Array inspect = hl_inspect ((int )i ,& arena ); 135HlAttrs attr = attr_entry (i ).attr ; 136remote_ui_hl_attr_define (ui , (Integer )i ,attr ,attr ,inspect ); 137arena_mem_free (arena_finish (& arena )); 138 } 139for (size_t hlf = 0 ;hlf < HLF_COUNT ;hlf ++ ) { 140remote_ui_hl_group_set (ui ,cstr_as_string (hlf_names [hlf ]), 141highlight_attr [hlf ]); 142 } 143} 144 145/// Get attribute code for a syntax group. 146int hl_get_syn_attr (int ns_id ,int idx ,HlAttrs at_en ) 147{ 148// TODO(bfredl): should we do this unconditionally 149if (at_en .cterm_fg_color != 0 || at_en .cterm_bg_color != 0 150|| at_en .rgb_fg_color != -1 || at_en .rgb_bg_color != -1 151|| at_en .rgb_sp_color != -1 || at_en .cterm_ae_attr != 0 152|| at_en .rgb_ae_attr != 0 || at_en .font >=0 || ns_id != 0 ) { 153return get_attr_entry ((HlEntry ){ .attr = at_en , .kind = kHlSyntax , 154 .id1 = idx , .id2 = ns_id }); 155 } 156// If all the fields are cleared, clear the attr field back to default value 157return 0 ; 158} 159 160void ns_hl_def (NS ns_id ,int hl_id ,HlAttrs attrs ,int link_id ,Dict (highlight )* dict ) 161{ 162if (ns_id == 0 ) { 163assert (dict ); 164// set in global (':highlight') namespace 165set_hl_group (hl_id ,attrs ,dict ,link_id ); 166return ; 167 } 168if ((attrs .rgb_ae_attr & HL_DEFAULT ) 169&& map_has (ColorKey ,& ns_hls , (ColorKey (ns_id ,hl_id )))) { 170return ; 171 } 172DecorProvider * p = get_decor_provider (ns_id , true); 173int attr_id = link_id > 0 ?-1 :hl_get_syn_attr (ns_id ,hl_id ,attrs ); 174ColorItem it = { .attr_id = attr_id , 175 .link_id = link_id , 176 .version = p -> hl_valid , 177 .is_default = (attrs .rgb_ae_attr & HL_DEFAULT ), 178 .link_global = (attrs .rgb_ae_attr & HL_GLOBAL ) }; 179map_put (ColorKey ,ColorItem )(& ns_hls ,ColorKey (ns_id ,hl_id ),it ); 180p -> hl_cached = false; 181} 182 183int ns_get_hl (NS * ns_hl ,int hl_id ,bool link ,bool nodefault ) 184{ 185static int recursive = 0 ; 186 187if (* ns_hl == 0 ) { 188// ns=0 (the default namespace) does not have a provider so stop here 189return -1 ; 190 } 191 192if (* ns_hl < 0 ) { 193if (ns_hl_active <=0 ) { 194return -1 ; 195 } 196* ns_hl = ns_hl_active ; 197 } 198 199int ns_id = * ns_hl ; 200 201DecorProvider * p = get_decor_provider (ns_id , true); 202ColorItem it = map_get (ColorKey ,ColorItem )(& ns_hls ,ColorKey (ns_id ,hl_id )); 203// TODO(bfredl): map_ref true even this? 204bool valid_item = it .version >=p -> hl_valid ; 205 206if (!valid_item && p -> hl_def != LUA_NOREF && !recursive ) { 207MAXSIZE_TEMP_ARRAY (args ,3 ); 208ADD_C (args ,INTEGER_OBJ ((Integer )ns_id )); 209ADD_C (args ,CSTR_AS_OBJ (syn_id2name (hl_id ))); 210ADD_C (args ,BOOLEAN_OBJ (link )); 211// TODO(bfredl): preload the "global" attr dict? 212 213Error err = ERROR_INIT ; 214recursive ++ ; 215Object ret = nlua_call_ref (p -> hl_def ,"hl_def" ,args ,kRetObject ,NULL ,& err ); 216recursive -- ; 217 218// TODO(bfredl): or "inherit", combine with global value? 219bool fallback = true; 220bool tmp = false; 221HlAttrs attrs = HLATTRS_INIT ; 222if (ret .type == kObjectTypeDict ) { 223fallback = false; 224Dict (highlight )dict = KEYDICT_INIT ; 225if (api_dict_to_keydict (& dict ,KeyDict_highlight_get_field ,ret .data .dict ,& err )) { 226attrs = dict2hlattrs (& dict , true,& it .link_id ,NULL ,& err ); 227fallback = GET_BOOL_OR_TRUE (& dict ,highlight ,fallback ); 228tmp = dict .fallback ;// or false 229if (it .link_id >=0 ) { 230fallback = true; 231 } 232 } 233 } 234 235it .attr_id = fallback ?-1 :hl_get_syn_attr (ns_id ,hl_id ,attrs ); 236it .version = p -> hl_valid - (int )tmp ; 237it .is_default = attrs .rgb_ae_attr & HL_DEFAULT ; 238it .link_global = attrs .rgb_ae_attr & HL_GLOBAL ; 239map_put (ColorKey ,ColorItem )(& ns_hls ,ColorKey (ns_id ,hl_id ),it ); 240valid_item = true; 241 } 242 243if ((it .is_default && nodefault )|| !valid_item ) { 244return -1 ; 245 } 246 247if (link ) { 248if (it .attr_id >=0 ) { 249return 0 ; 250 } 251if (it .link_global ) { 252* ns_hl = 0 ; 253 } 254return it .link_id ; 255 }else { 256return it .attr_id ; 257 } 258} 259 260bool hl_check_ns (void ) 261{ 262int ns = 0 ; 263if (ns_hl_fast > 0 ) { 264ns = ns_hl_fast ; 265 }else if (ns_hl_win >=0 ) { 266ns = ns_hl_win ; 267 }else { 268ns = ns_hl_global ; 269 } 270if (ns_hl_active == ns ) { 271return false; 272 } 273 274ns_hl_active = ns ; 275hl_attr_active = highlight_attr ; 276if (ns > 0 ) { 277update_ns_hl (ns ); 278NSHlAttr * hl_def = (NSHlAttr * )pmap_get (int )(& ns_hl_attr ,ns ); 279if (hl_def ) { 280hl_attr_active = * hl_def ; 281 } 282 } 283return true; 284} 285 286/// prepare for drawing window `wp` or global elements if NULL 287/// 288/// Note: pum should be drawn in the context of the current window! 289bool win_check_ns_hl (win_T * wp ) 290{ 291ns_hl_win = wp ?wp -> w_ns_hl :-1 ; 292return hl_check_ns (); 293} 294 295/// Get highlight attributes for a highlight group 296/// 297/// @param ns_id Namespace ID (0 for global namespace) 298/// @param hl_id Highlight group ID (1-based) 299/// @param[in] optional If non-NULL, passed to syn_ns_id2attr to track 300/// whether the group was explicitly defined in the namespace. 301/// @param[out] attrs Pointer to store the attributes 302/// @return true if highlight group exists and has valid attributes 303bool hl_ns_get_attrs (int ns_id ,int hl_id ,bool * optional ,HlAttrs * attrs ) 304{ 305bool opt = optional ?* optional : true; 306int syn_attr = syn_ns_id2attr (ns_id ,hl_id ,& opt ); 307if (optional ) { 308* optional = opt ; 309 } 310if (syn_attr <=0 ) { 311return false; 312 } 313* attrs = syn_attr2entry (syn_attr ); 314return true; 315} 316 317/// Get attribute code for a builtin highlight group. 318/// 319/// The final syntax group could be modified by hi-link or 'winhighlight'. 320int hl_get_ui_attr (int ns_id ,int idx ,int final_id ,bool optional ) 321{ 322HlAttrs attrs = HLATTRS_INIT ; 323bool available = false; 324 325if (final_id > 0 ) { 326available = hl_ns_get_attrs (ns_id ,final_id ,& optional ,& attrs ); 327 } 328 329if (HLF_PNI <=idx && idx <=HLF_PST ) { 330if (attrs .hl_blend == -1 && p_pb > 0 ) { 331attrs .hl_blend = (int )p_pb ; 332 } 333if (pum_drawn ()) { 334must_redraw_pum = true; 335 } 336 } 337 338if (optional && !available ) { 339return 0 ; 340 } 341return get_attr_entry ((HlEntry ){ .attr = attrs , .kind = kHlUI , 342 .id1 = idx , .id2 = final_id }); 343} 344 345/// Apply 'winblend' to highlight attributes. 346/// 347/// @param winbl The 'winblend' value. 348/// @param attr The original attribute code. 349/// 350/// @return The attribute code with 'winblend' applied. 351int hl_apply_winblend (int winbl ,int attr ) 352{ 353HlEntry entry = attr_entry (attr ); 354// if blend= attribute is not set, 'winblend' value overrides it. 355if (entry .attr .hl_blend == -1 && winbl > 0 ) { 356entry .attr .hl_blend = winbl ; 357attr = get_attr_entry (entry ); 358 } 359return attr ; 360} 361 362void update_window_hl (win_T * wp ,bool invalid ) 363{ 364int ns_id = wp -> w_ns_hl ; 365 366update_ns_hl (ns_id ); 367if (ns_id != wp -> w_ns_hl_active ) { 368wp -> w_ns_hl_active = ns_id ; 369 370NSHlAttr * hl_def_ptr = (NSHlAttr * )pmap_get (int )(& ns_hl_attr ,ns_id ); 371if (hl_def_ptr ) { 372wp -> w_ns_hl_attr = * hl_def_ptr ; 373 }else { 374// No specific highlights, use the defaults. 375wp -> w_ns_hl_attr = NULL ; 376 } 377 } 378 379int * hl_def = wp -> w_ns_hl_attr ?wp -> w_ns_hl_attr :hl_attr_active ; 380 381if (!wp -> w_hl_needs_update && !invalid ) { 382return ; 383 } 384wp -> w_hl_needs_update = false; 385 386// If a floating window is blending it always have a named 387// wp->w_hl_attr_normal group. HL_ATTR(HLF_NFLOAT) is always named. 388 389// determine window specific background set in 'winhighlight' 390bool float_win = wp -> w_floating && !wp -> w_config .external ; 391if (float_win && hl_def [HLF_NFLOAT ]!= 0 && ns_id > 0 ) { 392wp -> w_hl_attr_normal = hl_def [HLF_NFLOAT ]; 393 }else if (hl_def [HLF_NONE ]> 0 ) { 394wp -> w_hl_attr_normal = hl_def [HLF_NONE ]; 395 }else if (float_win ) { 396wp -> w_hl_attr_normal = HL_ATTR (HLF_NFLOAT )> 0 397 ?HL_ATTR (HLF_NFLOAT ) :highlight_attr [HLF_NFLOAT ]; 398 }else { 399wp -> w_hl_attr_normal = 0 ; 400 } 401 402if (wp -> w_floating ) { 403wp -> w_hl_attr_normal = hl_apply_winblend ((int )wp -> w_p_winbl ,wp -> w_hl_attr_normal ); 404 } 405 406wp -> w_config .shadow = false; 407if (wp -> w_floating && wp -> w_config .border ) { 408for (int i = 0 ;i < 8 ;i ++ ) { 409int attr = hl_def [HLF_BORDER ]; 410if (wp -> w_config .border_hl_ids [i ]) { 411attr = hl_get_ui_attr (ns_id ,HLF_BORDER , 412wp -> w_config .border_hl_ids [i ], false); 413 } 414attr = hl_apply_winblend ((int )wp -> w_p_winbl ,attr ); 415if (syn_attr2entry (attr ).hl_blend > 0 ) { 416wp -> w_config .shadow = true; 417 } 418wp -> w_config .border_attr [i ]= attr ; 419 } 420 } 421 422// shadow might cause blending 423check_blending (wp ); 424 425// TODO(bfredl): this a bit ad-hoc. move it from highlight ns logic to 'winhl' 426// implementation? 427if (hl_def [HLF_INACTIVE ]== 0 ) { 428wp -> w_hl_attr_normalnc = hl_combine_attr (HL_ATTR (HLF_INACTIVE ), 429wp -> w_hl_attr_normal ); 430 }else { 431wp -> w_hl_attr_normalnc = hl_def [HLF_INACTIVE ]; 432 } 433 434if (wp -> w_floating ) { 435wp -> w_hl_attr_normalnc = hl_apply_winblend ((int )wp -> w_p_winbl ,wp -> w_hl_attr_normalnc ); 436 } 437} 438 439void update_ns_hl (int ns_id ) 440{ 441if (ns_id <=0 ) { 442return ; 443 } 444DecorProvider * p = get_decor_provider (ns_id , true); 445if (p -> hl_cached ) { 446return ; 447 } 448 449NSHlAttr ** alloc = (NSHlAttr ** )pmap_put_ref (int )(& ns_hl_attr ,ns_id ,NULL ,NULL ); 450if (* alloc == NULL ) { 451* alloc = xmalloc (sizeof (** alloc )); 452 } 453int * hl_attrs = ** alloc ; 454 455for (int hlf = 1 ;hlf < HLF_COUNT ;hlf ++ ) { 456int id = syn_check_group (hlf_names [hlf ],strlen (hlf_names [hlf ])); 457bool optional = (hlf == HLF_INACTIVE || hlf == HLF_NFLOAT ); 458hl_attrs [hlf ]= hl_get_ui_attr (ns_id ,hlf ,id ,optional ); 459 } 460 461// NOOOO! You cannot just pretend that "Normal" is just like any other 462// syntax group! It needs at least 10 layers of special casing! Noooooo! 463// 464// haha, tema engine go brrr 465int normality = syn_check_group (S_LEN ("Normal" )); 466hl_attrs [HLF_NONE ]= hl_get_ui_attr (ns_id ,-1 ,normality , true); 467 468// hl_get_ui_attr might have invalidated the decor provider 469p = get_decor_provider (ns_id , true); 470p -> hl_cached = true; 471} 472 473int win_bg_attr (win_T * wp ) 474{ 475if (ns_hl_fast < 0 ) { 476int local = (wp == curwin ) ?wp -> w_hl_attr_normal :wp -> w_hl_attr_normalnc ; 477if (local ) { 478return local ; 479 } 480 } 481 482if (wp == curwin || hl_attr_active [HLF_INACTIVE ]== 0 ) { 483return hl_attr_active [HLF_NONE ]; 484 }else { 485return hl_attr_active [HLF_INACTIVE ]; 486 } 487} 488 489/// Gets HL_UNDERLINE highlight. 490int hl_get_underline (void ) 491{ 492HlAttrs attrs = HLATTRS_INIT ; 493attrs .cterm_ae_attr = (int16_t )HL_UNDERLINE ; 494attrs .rgb_ae_attr = (int16_t )HL_UNDERLINE ; 495return get_attr_entry ((HlEntry ){ 496 .attr = attrs , 497 .kind = kHlUI , 498 .id1 = 0 , 499 .id2 = 0 , 500 }); 501} 502 503/// Augment an existing attribute with a URL. 504/// 505/// @param attr Existing attribute to combine with 506/// @param url The URL to associate with the highlight attribute 507/// @return Combined attribute 508int hl_add_url (int attr ,const char * url ) 509{ 510HlAttrs attrs = HLATTRS_INIT ; 511 512MHPutStatus status ; 513uint32_t k = set_put_idx (cstr_t ,& urls ,url ,& status ); 514if (status != kMHExisting ) { 515urls .keys [k ]= xstrdup (url ); 516 } 517 518attrs .url = (int32_t )k ; 519 520int new = get_attr_entry ((HlEntry ){ 521 .attr = attrs , 522 .kind = kHlUI , 523 .id1 = 0 , 524 .id2 = 0 , 525 }); 526 527return hl_combine_attr (attr ,new ); 528} 529 530/// Get a URL by its index. 531/// 532/// @param index URL index 533/// @return URL 534const char * hl_get_url (uint32_t index ) 535{ 536assert (urls .keys ); 537return urls .keys [index ]; 538} 539 540// Intern a font name and return its stable index for use in HlAttrs.font. 541/// 542/// @param font_name The font name to add 543/// @return Font index, or -1 if font_name is NULL or empty 544int32_t hl_add_font_idx (const char * font_name ) 545{ 546if (font_name == NULL || * font_name == '\0' ) { 547return -1 ; 548 } 549 550MHPutStatus status ; 551uint32_t k = set_put_idx (cstr_t ,& fonts ,font_name ,& status ); 552if (status != kMHExisting ) { 553fonts .keys [k ]= xstrdup (font_name ); 554 } 555 556return (int32_t )k ; 557} 558 559/// Get a font name by its index. 560/// 561/// @param index Font index 562/// @return Font name, or NULL if index is invalid 563const char * hl_get_font (int32_t index ) 564{ 565if (index < 0 || !fonts .keys ) { 566return NULL ; 567 } 568return fonts .keys [index ]; 569} 570 571/// Get attribute code for forwarded :terminal highlights. 572int hl_get_term_attr (HlAttrs * aep ) 573{ 574return get_attr_entry ((HlEntry ){ .attr = * aep , .kind = kHlTerminal , 575 .id1 = 0 , .id2 = 0 }); 576} 577 578/// Clear all highlight tables. 579void clear_hl_tables (bool reinit ) 580{ 581const char * url = NULL ; 582set_foreach (& urls ,url , { 583xfree ((void * )url ); 584 }); 585 586if (reinit ) { 587set_clear (HlEntry ,& attr_entries ); 588highlight_init (); 589map_clear (uint64_t ,& combine_attr_entries ); 590map_clear (uint64_t ,& blend_attr_entries ); 591map_clear (uint64_t ,& blendthrough_attr_entries ); 592set_clear (cstr_t ,& urls ); 593memset (highlight_attr_last ,-1 ,sizeof (highlight_attr_last )); 594highlight_attr_set_all (); 595highlight_changed (); 596screen_invalidate_highlights (); 597 }else { 598const char * font = NULL ; 599set_foreach (& fonts ,font , { 600xfree ((void * )font ); 601 }); 602set_destroy (HlEntry ,& attr_entries ); 603map_destroy (uint64_t ,& combine_attr_entries ); 604map_destroy (uint64_t ,& blend_attr_entries ); 605map_destroy (uint64_t ,& blendthrough_attr_entries ); 606map_destroy (ColorKey ,& ns_hls ); 607set_destroy (cstr_t ,& urls ); 608set_destroy (cstr_t ,& fonts ); 609 } 610} 611 612void hl_invalidate_blends (void ) 613{ 614map_clear (uint64_t ,& blend_attr_entries ); 615map_clear (uint64_t ,& blendthrough_attr_entries ); 616highlight_changed (); 617update_window_hl (curwin , true); 618} 619 620/// Combine HlAttrFlags. 621/// The underline attribute in "prim_ae" overrules the one in "char_ae" if both are present. 622static int32_t hl_combine_ae (int32_t char_ae ,int32_t prim_ae ) 623{ 624int32_t char_ul = char_ae & HL_UNDERLINE_MASK ; 625int32_t prim_ul = prim_ae & HL_UNDERLINE_MASK ; 626int32_t new_ul = prim_ul ?prim_ul :char_ul ; 627return (char_ae & ~HL_UNDERLINE_MASK ) | (prim_ae & ~HL_UNDERLINE_MASK ) |new_ul ; 628} 629 630// Combine special attributes (e.g., for spelling) with other attributes 631// (e.g., for syntax highlighting). 632// "prim_attr" overrules "char_attr". 633// This creates a new group when required. 634// Since we expect there to be a lot of spelling mistakes we cache the result. 635// Return the resulting attributes. 636int hl_combine_attr (int char_attr ,int prim_attr ) 637{ 638if (char_attr == 0 ) { 639return prim_attr ; 640 }else if (prim_attr == 0 ) { 641return char_attr ; 642 } 643 644uint64_t combine_tag = HlAttrKey (char_attr ,prim_attr ); 645int id = map_get (uint64_t ,int )(& combine_attr_entries ,combine_tag ); 646if (id > 0 ) { 647return id ; 648 } 649 650HlAttrs char_aep = syn_attr2entry (char_attr ); 651HlAttrs prim_aep = syn_attr2entry (prim_attr ); 652 653// start with low-priority attribute, and override colors if present below. 654HlAttrs new_en = char_aep ; 655 656if (prim_aep .cterm_ae_attr & HL_NOCOMBINE ) { 657new_en .cterm_ae_attr = prim_aep .cterm_ae_attr ; 658 }else { 659new_en .cterm_ae_attr = hl_combine_ae (new_en .cterm_ae_attr ,prim_aep .cterm_ae_attr ); 660 } 661if (prim_aep .rgb_ae_attr & HL_NOCOMBINE ) { 662new_en .rgb_ae_attr = prim_aep .rgb_ae_attr ; 663 }else { 664new_en .rgb_ae_attr = hl_combine_ae (new_en .rgb_ae_attr ,prim_aep .rgb_ae_attr ); 665 } 666 667if (prim_aep .cterm_fg_color > 0 ) { 668new_en .cterm_fg_color = prim_aep .cterm_fg_color ; 669new_en .rgb_ae_attr &= ((~HL_FG_INDEXED ) 670 | (prim_aep .rgb_ae_attr & HL_FG_INDEXED )); 671 } 672 673if (prim_aep .cterm_bg_color > 0 ) { 674new_en .cterm_bg_color = prim_aep .cterm_bg_color ; 675new_en .rgb_ae_attr &= ((~HL_BG_INDEXED ) 676 | (prim_aep .rgb_ae_attr & HL_BG_INDEXED )); 677 } 678 679if (prim_aep .rgb_fg_color >=0 ) { 680new_en .rgb_fg_color = prim_aep .rgb_fg_color ; 681new_en .rgb_ae_attr &= ((~HL_FG_INDEXED ) 682 | (prim_aep .rgb_ae_attr & HL_FG_INDEXED )); 683 } 684 685if (prim_aep .rgb_bg_color >=0 ) { 686new_en .rgb_bg_color = prim_aep .rgb_bg_color ; 687new_en .rgb_ae_attr &= ((~HL_BG_INDEXED ) 688 | (prim_aep .rgb_ae_attr & HL_BG_INDEXED )); 689 } 690 691if (prim_aep .rgb_sp_color >=0 ) { 692new_en .rgb_sp_color = prim_aep .rgb_sp_color ; 693 } 694 695if (prim_aep .hl_blend >=0 ) { 696new_en .hl_blend = prim_aep .hl_blend ; 697 } 698 699if ((new_en .url == -1 )&& (prim_aep .url >=0 )) { 700new_en .url = prim_aep .url ; 701 } 702 703if (prim_aep .font >=0 ) { 704new_en .font = prim_aep .font ; 705 } 706 707id = get_attr_entry ((HlEntry ){ .attr = new_en , .kind = kHlCombine , 708 .id1 = char_attr , .id2 = prim_attr }); 709if (id > 0 ) { 710map_put (uint64_t ,int )(& combine_attr_entries ,combine_tag ,id ); 711 } 712 713return id ; 714} 715 716/// Get the used rgb colors for an attr group. 717/// 718/// If colors are unset, use builtin default colors. Never returns -1 719/// Cterm colors are unchanged. 720static HlAttrs get_colors_force (HlAttrs attrs ) 721{ 722if (attrs .rgb_bg_color == -1 ) { 723attrs .rgb_bg_color = normal_bg ; 724 } 725if (attrs .rgb_fg_color == -1 ) { 726attrs .rgb_fg_color = normal_fg ; 727 } 728if (attrs .rgb_sp_color == -1 ) { 729attrs .rgb_sp_color = normal_sp ; 730 } 731HL_SET_DEFAULT_COLORS (attrs .rgb_fg_color ,attrs .rgb_bg_color , 732attrs .rgb_sp_color ); 733 734if (attrs .rgb_ae_attr & HL_INVERSE ) { 735int temp = attrs .rgb_bg_color ; 736attrs .rgb_bg_color = attrs .rgb_fg_color ; 737attrs .rgb_fg_color = temp ; 738attrs .rgb_ae_attr &= ~HL_INVERSE ; 739 } 740 741return attrs ; 742} 743 744/// Blend overlay attributes (for popupmenu) with other attributes 745/// 746/// This creates a new group when required. 747/// This is called per-cell, so cache the result. 748/// 749/// @return the resulting attributes. 750int hl_blend_attrs (int back_attr ,int front_attr ,bool * through ) 751{ 752// Cannot blend uninitialized cells, use front_attr for uninitialized background cells. 753if (front_attr < 0 || back_attr < 0 ) { 754return front_attr ; 755 } 756 757HlAttrs fattrs_raw = syn_attr2entry (front_attr ); 758HlAttrs fattrs = get_colors_force (fattrs_raw ); 759int ratio = fattrs .hl_blend ; 760if (ratio <=0 ) { 761* through = false; 762return front_attr ; 763 } 764 765uint64_t combine_tag = HlAttrKey (back_attr ,front_attr ); 766Map (uint64_t ,int )* map = (* through 767 ?& blendthrough_attr_entries 768 :& blend_attr_entries ); 769int id = map_get (uint64_t ,int )(map ,combine_tag ); 770if (id > 0 ) { 771return id ; 772 } 773 774HlAttrs battrs_raw = syn_attr2entry (back_attr ); 775HlAttrs battrs = get_colors_force (battrs_raw ); 776HlAttrs cattrs ; 777 778if (* through ) { 779cattrs = battrs ; 780cattrs .rgb_fg_color = rgb_blend (ratio ,battrs .rgb_fg_color ,fattrs .rgb_bg_color ); 781// Blend the special color only when the cell below sets one explicitly, else clear it. 782if ((cattrs .rgb_ae_attr & HL_UNDERLINE_MASK )&& battrs_raw .rgb_sp_color != -1 ) { 783cattrs .rgb_sp_color = rgb_blend (ratio ,battrs .rgb_sp_color ,fattrs .rgb_bg_color ); 784 }else { 785cattrs .rgb_sp_color = -1 ; 786 } 787 788cattrs .cterm_bg_color = fattrs .cterm_bg_color ; 789cattrs .cterm_fg_color = (int16_t )cterm_blend (ratio ,battrs .cterm_fg_color , 790fattrs .cterm_bg_color ); 791cattrs .rgb_ae_attr &= ~(HL_FG_INDEXED |HL_BG_INDEXED ); 792 }else { 793cattrs = fattrs ; 794cattrs .rgb_fg_color = rgb_blend (ratio /2 ,battrs .rgb_fg_color ,fattrs .rgb_fg_color ); 795if (cattrs .rgb_ae_attr & HL_UNDERLINE_MASK ) { 796cattrs .rgb_sp_color = rgb_blend (ratio /2 ,battrs .rgb_bg_color ,fattrs .rgb_sp_color ); 797 }else { 798cattrs .rgb_sp_color = -1 ; 799 } 800 801cattrs .rgb_ae_attr &= ~(HL_FG_INDEXED |HL_BG_INDEXED ); 802 } 803 804// Check if we should preserve background transparency 805// Special case for blend=100: preserve back layer background exactly (including bg=NONE) 806if (ratio == 100 && battrs_raw .rgb_bg_color == -1 ) { 807// For 100% blend with transparent background, preserve the transparency 808cattrs .rgb_bg_color = -1 ; 809 }else { 810// Use the raw attributes (before forcing colors) to check original transparency 811cattrs .rgb_bg_color = (battrs_raw .rgb_bg_color == -1 )&& (fattrs_raw .rgb_bg_color == -1 ) 812 ?-1 813 :rgb_blend (ratio ,battrs .rgb_bg_color ,fattrs .rgb_bg_color ); 814 } 815cattrs .hl_blend = -1 ;// blend property was consumed 816HlKind kind = * through ?kHlBlendThrough :kHlBlend ; 817id = get_attr_entry ((HlEntry ){ .attr = cattrs , .kind = kind , 818 .id1 = back_attr , .id2 = front_attr }); 819if (id > 0 ) { 820map_put (uint64_t ,int )(map ,combine_tag ,id ); 821 } 822return id ; 823} 824 825static int rgb_blend (int ratio ,int rgb1 ,int rgb2 ) 826{ 827int a = ratio ; 828int b = 100 - ratio ; 829int r1 = (rgb1 & 0xFF0000 ) >>16 ; 830int g1 = (rgb1 & 0x00FF00 ) >>8 ; 831int b1 = (rgb1 & 0x0000FF ) >>0 ; 832int r2 = (rgb2 & 0xFF0000 ) >>16 ; 833int g2 = (rgb2 & 0x00FF00 ) >>8 ; 834int b2 = (rgb2 & 0x0000FF ) >>0 ; 835int mr = (a * r1 + b * r2 )/100 ; 836int mg = (a * g1 + b * g2 )/100 ; 837int mb = (a * b1 + b * b2 )/100 ; 838return (mr <<16 )+ (mg <<8 )+ mb ; 839} 840 841static int cterm_blend (int ratio ,int16_t c1 ,int16_t c2 ) 842{ 843// 1. Convert cterm color numbers to RGB. 844// 2. Blend the RGB colors. 845// 3. Convert the RGB result to a cterm color. 846int rgb1 = hl_cterm2rgb_color (c1 ); 847int rgb2 = hl_cterm2rgb_color (c2 ); 848int rgb_blended = rgb_blend (ratio ,rgb1 ,rgb2 ); 849return hl_rgb2cterm_color (rgb_blended ); 850} 851 852/// Converts RGB color to 8-bit color (0-255). 853static int hl_rgb2cterm_color (int rgb ) 854{ 855int r = (rgb & 0xFF0000 ) >>16 ; 856int g = (rgb & 0x00FF00 ) >>8 ; 857int b = (rgb & 0x0000FF ) >>0 ; 858 859return (r * 6 /256 )* 36 + (g * 6 /256 )* 6 + (b * 6 /256 ); 860} 861 862/// Converts 8-bit color (0-255) to RGB color. 863/// This is compatible with xterm. 864static int hl_cterm2rgb_color (int nr ) 865{ 866static int cube_value []= { 8670x00 ,0x5F ,0x87 ,0xAF ,0xD7 ,0xFF 868 }; 869static int grey_ramp []= { 8700x08 ,0x12 ,0x1C ,0x26 ,0x30 ,0x3A ,0x44 ,0x4E ,0x58 ,0x62 ,0x6C ,0x76 , 8710x80 ,0x8A ,0x94 ,0x9E ,0xA8 ,0xB2 ,0xBC ,0xC6 ,0xD0 ,0xDA ,0xE4 ,0xEE 872 }; 873static uint8_t ansi_table [16 ][4 ]= { 874// R G B idx 875 {0 ,0 ,0 ,1 },// black 876 {224 ,0 ,0 ,2 },// dark red 877 {0 ,224 ,0 ,3 },// dark green 878 {224 ,224 ,0 ,4 },// dark yellow / brown 879 {0 ,0 ,224 ,5 },// dark blue 880 {224 ,0 ,224 ,6 },// dark magenta 881 {0 ,224 ,224 ,7 },// dark cyan 882 {224 ,224 ,224 ,8 },// light grey 883 884 {128 ,128 ,128 ,9 },// dark grey 885 {255 ,64 ,64 ,10 },// light red 886 {64 ,255 ,64 ,11 },// light green 887 {255 ,255 ,64 ,12 },// yellow 888 {64 ,64 ,255 ,13 },// light blue 889 {255 ,64 ,255 ,14 },// light magenta 890 {64 ,255 ,255 ,15 },// light cyan 891 {255 ,255 ,255 ,16 },// white 892 }; 893 894int r = 0 ; 895int g = 0 ; 896int b = 0 ; 897int idx ; 898// *ansi_idx = 0; 899 900if (nr < 16 ) { 901r = ansi_table [nr ][0 ]; 902g = ansi_table [nr ][1 ]; 903b = ansi_table [nr ][2 ]; 904// *ansi_idx = ansi_table[nr][3]; 905 }else if (nr < 232 ) {// 216 color-cube 906idx = nr - 16 ; 907r = cube_value [idx /36 %6 ]; 908g = cube_value [idx /6 %6 ]; 909b = cube_value [idx %6 ]; 910// *ansi_idx = -1; 911 }else if (nr < 256 ) {// 24 greyscale ramp 912idx = nr - 232 ; 913r = grey_ramp [idx ]; 914g = grey_ramp [idx ]; 915b = grey_ramp [idx ]; 916// *ansi_idx = -1; 917 } 918return (r <<16 )+ (g <<8 )+ b ; 919} 920 921/// Get highlight attributes for a attribute code 922HlAttrs syn_attr2entry (int attr ) 923{ 924if (attr <=0 || attr >= (int )set_size (& attr_entries )) { 925// invalid attribute code, or the tables were cleared 926return HLATTRS_INIT ; 927 } 928return attr_entry (attr ).attr ; 929} 930 931/// Gets highlight description for id `attr_id` as a map. 932Dict hl_get_attr_by_id (Integer attr_id ,Boolean rgb ,Arena * arena ,Error * err ) 933{ 934Dict dic = ARRAY_DICT_INIT ; 935 936if (attr_id == 0 ) { 937return dic ; 938 } 939 940if (attr_id < 0 || attr_id >= (int )set_size (& attr_entries )) { 941api_set_error (err ,kErrorTypeException , 942"Invalid attribute id: %" PRId64 ,attr_id ); 943return dic ; 944 } 945Dict retval = arena_dict (arena ,HLATTRS_DICT_SIZE ); 946hlattrs2dict (& retval ,NULL ,syn_attr2entry ((int )attr_id ),rgb , false); 947return retval ; 948} 949 950/// Converts an HlAttrs into Dict 951/// 952/// @param[in/out] hl Dict with pre-allocated space for HLATTRS_DICT_SIZE elements 953/// @param[in] aep data to convert 954/// @param use_rgb use 'gui*' settings if true, else resorts to 'cterm*' 955/// @param short_keys change (foreground, background, special) to (fg, bg, sp) for 'gui*' settings 956/// (foreground, background) to (ctermfg, ctermbg) for 'cterm*' settings 957void hlattrs2dict (Dict * hl ,Dict * hl_attrs ,HlAttrs ae ,bool use_rgb ,bool short_keys ) 958{ 959hl_attrs = hl_attrs ?hl_attrs :hl ; 960assert (hl -> capacity >=HLATTRS_DICT_SIZE );// at most 24 items 961assert (hl_attrs -> capacity >=HLATTRS_DICT_SIZE );// at most 24 items 962int mask = use_rgb ?ae .rgb_ae_attr :ae .cterm_ae_attr ; 963 964if (mask & HL_INVERSE ) { 965PUT_C (* hl_attrs ,"reverse" ,BOOLEAN_OBJ (true)); 966 } 967 968if (mask & HL_BOLD ) { 969PUT_C (* hl_attrs ,"bold" ,BOOLEAN_OBJ (true)); 970 } 971 972if (mask & HL_ITALIC ) { 973PUT_C (* hl_attrs ,"italic" ,BOOLEAN_OBJ (true)); 974 } 975 976switch (mask & HL_UNDERLINE_MASK ) { 977case HL_UNDERLINE : 978PUT_C (* hl_attrs ,"underline" ,BOOLEAN_OBJ (true)); 979break ; 980 981case HL_UNDERCURL : 982PUT_C (* hl_attrs ,"undercurl" ,BOOLEAN_OBJ (true)); 983break ; 984 985case HL_UNDERDOUBLE : 986PUT_C (* hl_attrs ,"underdouble" ,BOOLEAN_OBJ (true)); 987break ; 988 989case HL_UNDERDOTTED : 990PUT_C (* hl_attrs ,"underdotted" ,BOOLEAN_OBJ (true)); 991break ; 992 993case HL_UNDERDASHED : 994PUT_C (* hl_attrs ,"underdashed" ,BOOLEAN_OBJ (true)); 995break ; 996 } 997 998if (mask & HL_STANDOUT ) { 999PUT_C (* hl_attrs ,"standout" ,BOOLEAN_OBJ (true)); 1000 } 1001 1002if (mask & HL_STRIKETHROUGH ) { 1003PUT_C (* hl_attrs ,"strikethrough" ,BOOLEAN_OBJ (true)); 1004 } 1005 1006if (mask & HL_ALTFONT ) { 1007PUT_C (* hl_attrs ,"altfont" ,BOOLEAN_OBJ (true)); 1008 } 1009 1010if (mask & HL_DIM ) { 1011PUT_C (* hl_attrs ,"dim" ,BOOLEAN_OBJ (true)); 1012 } 1013 1014if (mask & HL_BLINK ) { 1015PUT_C (* hl_attrs ,"blink" ,BOOLEAN_OBJ (true)); 1016 } 1017 1018if (mask & HL_CONCEALED ) { 1019PUT_C (* hl_attrs ,"conceal" ,BOOLEAN_OBJ (true)); 1020 } 1021 1022if (mask & HL_OVERLINE ) { 1023PUT_C (* hl_attrs ,"overline" ,BOOLEAN_OBJ (true)); 1024 } 1025 1026if (mask & HL_NOCOMBINE ) { 1027PUT_C (* hl_attrs ,"nocombine" ,BOOLEAN_OBJ (true)); 1028 } 1029 1030if (use_rgb ) { 1031if (ae .rgb_fg_color != -1 ) { 1032PUT_C (* hl ,short_keys ?"fg" :"foreground" ,INTEGER_OBJ (ae .rgb_fg_color )); 1033 } 1034 1035if (ae .rgb_bg_color != -1 ) { 1036PUT_C (* hl ,short_keys ?"bg" :"background" ,INTEGER_OBJ (ae .rgb_bg_color )); 1037 } 1038 1039if (ae .rgb_sp_color != -1 ) { 1040PUT_C (* hl ,short_keys ?"sp" :"special" ,INTEGER_OBJ (ae .rgb_sp_color )); 1041 } 1042 1043if (mask & HL_FG_INDEXED ) { 1044PUT_C (* hl ,"fg_indexed" ,BOOLEAN_OBJ (true)); 1045 } 1046 1047if (mask & HL_BG_INDEXED ) { 1048PUT_C (* hl ,"bg_indexed" ,BOOLEAN_OBJ (true)); 1049 } 1050 1051const char * font = hl_get_font (ae .font ); 1052if (font != NULL ) { 1053PUT_C (* hl ,"font" ,STRING_OBJ (cstr_as_string (font ))); 1054 } 1055 }else { 1056if (ae .cterm_fg_color != 0 ) { 1057PUT_C (* hl ,short_keys ?"ctermfg" :"foreground" ,INTEGER_OBJ (ae .cterm_fg_color - 1 )); 1058 } 1059 1060if (ae .cterm_bg_color != 0 ) { 1061PUT_C (* hl ,short_keys ?"ctermbg" :"background" ,INTEGER_OBJ (ae .cterm_bg_color - 1 )); 1062 } 1063 } 1064 1065if (ae .hl_blend > -1 && (use_rgb || !short_keys )) { 1066PUT_C (* hl ,"blend" ,INTEGER_OBJ (ae .hl_blend )); 1067 } 1068} 1069 1070HlAttrs dict2hlattrs (Dict (highlight )* dict ,bool use_rgb ,int * link_id ,HlAttrs * base ,Error * err ) 1071{ 1072#define HAS_KEY_X (d ,key ) HAS_KEY(d, highlight, key) 1073HlAttrs hlattrs = HLATTRS_INIT ; 1074int32_t fg = base ?base -> rgb_fg_color :-1 ; 1075int32_t bg = base ?base -> rgb_bg_color :-1 ; 1076int32_t ctermfg = base ? (base -> cterm_fg_color == 0 ?-1 :base -> cterm_fg_color - 1 ) :-1 ; 1077int32_t ctermbg = base ? (base -> cterm_bg_color == 0 ?-1 :base -> cterm_bg_color - 1 ) :-1 ; 1078int32_t sp = base ?base -> rgb_sp_color :-1 ; 1079int blend = base ?base -> hl_blend :-1 ; 1080int32_t mask = base ?base -> rgb_ae_attr :0 ; 1081int32_t cterm_mask = base ?base -> cterm_ae_attr :0 ; 1082int32_t font = base ?base -> font :-1 ; 1083bool cterm_mask_provided = false; 1084 1085#define CHECK_FLAG_WITH_KEY (d ,m ,name ,extra ,flag ) \ 1086 if (HAS_KEY_X(d, name)) { \ 1087 int32_t flag_ = (flag); \ 1088 int32_t cmask_ = (flag_ & HL_UNDERLINE_MASK) ? HL_UNDERLINE_MASK : flag_; \ 1089 if (d->name##extra) { \ 1090 m = (m & ~cmask_) | flag_; \ 1091 } else if ((m & cmask_) == flag_) { \ 1092 m &= ~cmask_; \ 1093 } \ 1094 } 1095 1096CHECK_FLAG_WITH_KEY (dict ,mask ,reverse , ,HL_INVERSE ); 1097CHECK_FLAG_WITH_KEY (dict ,mask ,bold , ,HL_BOLD ); 1098CHECK_FLAG_WITH_KEY (dict ,mask ,italic , ,HL_ITALIC ); 1099CHECK_FLAG_WITH_KEY (dict ,mask ,underline , ,HL_UNDERLINE ); 1100CHECK_FLAG_WITH_KEY (dict ,mask ,undercurl , ,HL_UNDERCURL ); 1101CHECK_FLAG_WITH_KEY (dict ,mask ,underdouble , ,HL_UNDERDOUBLE ); 1102CHECK_FLAG_WITH_KEY (dict ,mask ,underdotted , ,HL_UNDERDOTTED ); 1103CHECK_FLAG_WITH_KEY (dict ,mask ,underdashed , ,HL_UNDERDASHED ); 1104CHECK_FLAG_WITH_KEY (dict ,mask ,standout , ,HL_STANDOUT ); 1105CHECK_FLAG_WITH_KEY (dict ,mask ,strikethrough , ,HL_STRIKETHROUGH ); 1106CHECK_FLAG_WITH_KEY (dict ,mask ,altfont , ,HL_ALTFONT ); 1107CHECK_FLAG_WITH_KEY (dict ,mask ,dim , ,HL_DIM ); 1108CHECK_FLAG_WITH_KEY (dict ,mask ,blink , ,HL_BLINK ); 1109CHECK_FLAG_WITH_KEY (dict ,mask ,conceal , ,HL_CONCEALED ); 1110CHECK_FLAG_WITH_KEY (dict ,mask ,overline , ,HL_OVERLINE ); 1111if (use_rgb ) { 1112CHECK_FLAG_WITH_KEY (dict ,mask ,fg_indexed , ,HL_FG_INDEXED ); 1113CHECK_FLAG_WITH_KEY (dict ,mask ,bg_indexed , ,HL_BG_INDEXED ); 1114 } 1115CHECK_FLAG_WITH_KEY (dict ,mask ,nocombine , ,HL_NOCOMBINE ); 1116CHECK_FLAG_WITH_KEY (dict ,mask ,default ,_ ,HL_DEFAULT ); 1117 1118if (HAS_KEY_X (dict ,fg )) { 1119fg = object_to_color (dict -> fg ,"fg" ,use_rgb ,err ); 1120 }else if (HAS_KEY_X (dict ,foreground )) { 1121fg = object_to_color (dict -> foreground ,"foreground" ,use_rgb ,err ); 1122 } 1123if (ERROR_SET (err )) { 1124return hlattrs ; 1125 } 1126 1127if (HAS_KEY_X (dict ,bg )) { 1128bg = object_to_color (dict -> bg ,"bg" ,use_rgb ,err ); 1129 }else if (HAS_KEY_X (dict ,background )) { 1130bg = object_to_color (dict -> background ,"background" ,use_rgb ,err ); 1131 } 1132if (ERROR_SET (err )) { 1133return hlattrs ; 1134 } 1135 1136if (HAS_KEY_X (dict ,sp )) { 1137sp = object_to_color (dict -> sp ,"sp" , true,err ); 1138 }else if (HAS_KEY_X (dict ,special )) { 1139sp = object_to_color (dict -> special ,"special" , true,err ); 1140 } 1141if (ERROR_SET (err )) { 1142return hlattrs ; 1143 } 1144 1145if (HAS_KEY_X (dict ,blend )) { 1146Integer blend0 = dict -> blend ; 1147VALIDATE_RANGE ((blend0 >=0 && blend0 <=100 ),"blend" , { 1148return hlattrs ; 1149 }); 1150blend = (int )blend0 ; 1151 } 1152 1153if (HAS_KEY_X (dict ,link )|| HAS_KEY_X (dict ,link_global )) { 1154if (!link_id ) { 1155api_set_error (err ,kErrorTypeValidation ,"Invalid Key: '%s'" , 1156HAS_KEY_X (dict ,link_global ) ?"link_global" :"link" ); 1157return hlattrs ; 1158 } 1159if (HAS_KEY_X (dict ,link_global )) { 1160* link_id = (int )dict -> link_global ; 1161mask |=HL_GLOBAL ; 1162 }else { 1163* link_id = (int )dict -> link ; 1164 } 1165 1166if (ERROR_SET (err )) { 1167return hlattrs ; 1168 } 1169 } 1170 1171// Handle cterm attrs 1172if (HAS_KEY_X (dict ,cterm )) { 1173Dict (highlight_cterm )cterm [1 ]= KEYDICT_INIT ; 1174if (!api_dict_to_keydict (cterm ,KeyDict_highlight_cterm_get_field , 1175dict -> cterm ,err )) { 1176return hlattrs ; 1177 } 1178 1179cterm_mask_provided = true; 1180cterm_mask = 0 ; 1181 1182#define CHECK_FLAG (d ,m ,name ,extra ,flag ) \ 1183 if (d->name##extra) { \ 1184 if (flag & HL_UNDERLINE_MASK) { \ 1185 m &= ~HL_UNDERLINE_MASK; \ 1186 } \ 1187 m |= flag; \ 1188 } 1189 1190CHECK_FLAG (cterm ,cterm_mask ,reverse , ,HL_INVERSE ); 1191CHECK_FLAG (cterm ,cterm_mask ,bold , ,HL_BOLD ); 1192CHECK_FLAG (cterm ,cterm_mask ,italic , ,HL_ITALIC ); 1193CHECK_FLAG (cterm ,cterm_mask ,underline , ,HL_UNDERLINE ); 1194CHECK_FLAG (cterm ,cterm_mask ,undercurl , ,HL_UNDERCURL ); 1195CHECK_FLAG (cterm ,cterm_mask ,underdouble , ,HL_UNDERDOUBLE ); 1196CHECK_FLAG (cterm ,cterm_mask ,underdotted , ,HL_UNDERDOTTED ); 1197CHECK_FLAG (cterm ,cterm_mask ,underdashed , ,HL_UNDERDASHED ); 1198CHECK_FLAG (cterm ,cterm_mask ,standout , ,HL_STANDOUT ); 1199CHECK_FLAG (cterm ,cterm_mask ,strikethrough , ,HL_STRIKETHROUGH ); 1200CHECK_FLAG (cterm ,cterm_mask ,altfont , ,HL_ALTFONT ); 1201CHECK_FLAG (cterm ,cterm_mask ,dim , ,HL_DIM ); 1202CHECK_FLAG (cterm ,cterm_mask ,blink , ,HL_BLINK ); 1203CHECK_FLAG (cterm ,cterm_mask ,conceal , ,HL_CONCEALED ); 1204CHECK_FLAG (cterm ,cterm_mask ,overline , ,HL_OVERLINE ); 1205CHECK_FLAG (cterm ,cterm_mask ,nocombine , ,HL_NOCOMBINE ); 1206 } 1207#undef CHECK_FLAG 1208#undef CHECK_FLAG_WITH_KEY 1209 1210if (HAS_KEY_X (dict ,ctermfg )) { 1211ctermfg = object_to_color (dict -> ctermfg ,"ctermfg" , false,err ); 1212if (ERROR_SET (err )) { 1213return hlattrs ; 1214 } 1215 } 1216 1217if (HAS_KEY_X (dict ,ctermbg )) { 1218ctermbg = object_to_color (dict -> ctermbg ,"ctermbg" , false,err ); 1219if (ERROR_SET (err )) { 1220return hlattrs ; 1221 } 1222 } 1223 1224if (use_rgb ) { 1225// apply gui mask as default for cterm mask 1226if (!cterm_mask_provided ) { 1227cterm_mask = mask ; 1228 } 1229hlattrs .rgb_ae_attr = mask ; 1230hlattrs .rgb_bg_color = bg ; 1231hlattrs .rgb_fg_color = fg ; 1232hlattrs .rgb_sp_color = sp ; 1233hlattrs .hl_blend = blend ; 1234hlattrs .cterm_bg_color = ctermbg == -1 ?0 : (int16_t )(ctermbg + 1 ); 1235hlattrs .cterm_fg_color = ctermfg == -1 ?0 : (int16_t )(ctermfg + 1 ); 1236hlattrs .cterm_ae_attr = cterm_mask ; 1237 }else { 1238hlattrs .cterm_bg_color = bg == -1 ?0 : (int16_t )(bg + 1 ); 1239hlattrs .cterm_fg_color = fg == -1 ?0 : (int16_t )(fg + 1 ); 1240hlattrs .cterm_ae_attr = mask ; 1241 } 1242 1243if (HAS_KEY_X (dict ,font )) { 1244String str = dict -> font ; 1245font = (str .size > 0 && STRICMP (str .data ,"NONE" )!= 0 ) ?hl_add_font_idx (str .data ) :-1 ; 1246 } 1247hlattrs .font = font ; 1248 1249return hlattrs ; 1250#undef HAS_KEY_X 1251} 1252 1253int object_to_color (Object val ,char * key ,bool rgb ,Error * err ) 1254{ 1255if (val .type == kObjectTypeInteger ) { 1256return (int )val .data .integer ; 1257 }else if (val .type == kObjectTypeString ) { 1258String str = val .data .string ; 1259// TODO(bfredl): be more fancy with "bg", "fg" etc 1260if (!str .size || STRICMP (str .data ,"NONE" )== 0 ) { 1261return -1 ; 1262 } 1263int color ; 1264if (rgb ) { 1265int dummy ; 1266color = name_to_color (str .data ,& dummy ); 1267 }else { 1268color = name_to_ctermcolor (str .data ); 1269 } 1270VALIDATE_S ((color >=0 ),"highlight color" ,str .data , { 1271return color ; 1272 }); 1273return color ; 1274 }else { 1275VALIDATE_EXP (false,key ,"String or Integer" ,NULL , { 1276return 0 ; 1277 }); 1278 } 1279} 1280 1281Array hl_inspect (int attr ,Arena * arena ) 1282{ 1283if (!hlstate_active ) { 1284return (Array )ARRAY_DICT_INIT ; 1285 } 1286Array ret = arena_array (arena ,hl_inspect_size (attr )); 1287hl_inspect_impl (& ret ,attr ,arena ); 1288return ret ; 1289} 1290 1291static size_t hl_inspect_size (int attr ) 1292{ 1293if (attr <=0 || attr >= (int )set_size (& attr_entries )) { 1294return 0 ; 1295 } 1296 1297HlEntry e = attr_entry (attr ); 1298if (e .kind == kHlCombine || e .kind == kHlBlend || e .kind == kHlBlendThrough ) { 1299return hl_inspect_size (e .id1 )+ hl_inspect_size (e .id2 ); 1300 } 1301return 1 ; 1302} 1303 1304static void hl_inspect_impl (Array * arr ,int attr ,Arena * arena ) 1305{ 1306Dict item = ARRAY_DICT_INIT ; 1307if (attr <=0 || attr >= (int )set_size (& attr_entries )) { 1308return ; 1309 } 1310 1311HlEntry e = attr_entry (attr ); 1312switch (e .kind ) { 1313case kHlSyntax : 1314item = arena_dict (arena ,3 ); 1315PUT_C (item ,"kind" ,CSTR_AS_OBJ ("syntax" )); 1316PUT_C (item ,"hi_name" ,CSTR_AS_OBJ (syn_id2name (e .id1 ))); 1317break ; 1318 1319case kHlUI : 1320item = arena_dict (arena ,4 ); 1321PUT_C (item ,"kind" ,CSTR_AS_OBJ ("ui" )); 1322const char * ui_name = (e .id1 == -1 ) ?"Normal" :hlf_names [e .id1 ]; 1323PUT_C (item ,"ui_name" ,CSTR_AS_OBJ (ui_name )); 1324PUT_C (item ,"hi_name" ,CSTR_AS_OBJ (syn_id2name (e .id2 ))); 1325break ; 1326 1327case kHlTerminal : 1328item = arena_dict (arena ,2 ); 1329PUT_C (item ,"kind" ,CSTR_AS_OBJ ("term" )); 1330break ; 1331 1332case kHlCombine : 1333case kHlBlend : 1334case kHlBlendThrough : 1335// attribute combination is associative, so flatten to an array 1336hl_inspect_impl (arr ,e .id1 ,arena ); 1337hl_inspect_impl (arr ,e .id2 ,arena ); 1338return ; 1339 1340case kHlUnknown : 1341case kHlInvalid : 1342return ; 1343 } 1344PUT_C (item ,"id" ,INTEGER_OBJ (attr )); 1345ADD_C (* arr ,DICT_OBJ (item )); 1346}