Repository: neovim/neovim
File: src/nvim/highlight.c (C)

Code before:

  HlAttrs char_aep = syn_attr2entry(char_attr);
  HlAttrs prim_aep = syn_attr2entry(prim_attr);

  // start with low-priority attribute, and override colors if present below.
  HlAttrs new_en = char_aep;

  if (prim_aep.cterm_ae_attr & HL_NOCOMBINE) {
    new_en.cterm_ae_attr = prim_aep.cterm_ae_attr;
  } else {
    new_en.cterm_ae_attr = hl_combine_ae(new_en.cterm_ae_attr, prim_aep.cterm_ae_attr);
  }
  if (prim_aep.rgb_ae_attr & HL_NOCOMBINE) {
    new_en.rgb_ae_attr = prim_aep.rgb_ae_attr;
  } else {
    new_en.rgb_ae_attr = hl_combine_ae(new_en.rgb_ae_attr, prim_aep.rgb_ae_attr);
  }

  if (prim_aep.cterm_fg_color > 0) {
    new_en.cterm_fg_color = prim_aep.cterm_fg_color;
    new_en.rgb_ae_attr &= ((~HL_FG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_FG_INDEXED));
  }

  if (prim_aep.cterm_bg_color > 0
      && !(prim_aep.cterm_bg_color == cterm_normal_bg_color && char_aep.cterm_bg_color > 0
           && char_aep.cterm_bg_color != cterm_normal_bg_color)) {
    new_en.cterm_bg_color = prim_aep.cterm_bg_color;
    new_en.rgb_ae_attr &= ((~HL_BG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_BG_INDEXED));
  }

  if (prim_aep.rgb_fg_color >= 0) {
    new_en.rgb_fg_color = prim_aep.rgb_fg_color;
    new_en.rgb_ae_attr &= ((~HL_FG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_FG_INDEXED));
  }

  // Don't let normal background (from groups linked to Normal) override
  // explicit non-normal backgrounds (like CursorLine). This ensures consistent
  // behavior regardless of whether text uses Normal directly or through a linked group.
  if (prim_aep.rgb_bg_color >= 0
      && !(prim_aep.rgb_bg_color == normal_bg && char_aep.rgb_bg_color >= 0
           && char_aep.rgb_bg_color != normal_bg)) {
    new_en.rgb_bg_color = prim_aep.rgb_bg_color;
    new_en.rgb_ae_attr &= ((~HL_BG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_BG_INDEXED));
  }

  if (prim_aep.rgb_sp_color >= 0) {
    new_en.rgb_sp_color = prim_aep.rgb_sp_color;

Review comment (bug):
this will break any case where `prim_aep` was explicitly set to the same RGB color as Normal. if this is about special rules for low-priority CursorLine, it is better to add a specific rule for that in the cursorline code in drawline.c .

 Although "normal linked" groups should ideally just be replaced with cleared groups in the cases the present effect is undesirable (including replacing a `hi def link foo Normal` with just letting the default of `foo` being completely unset). Linking normal feels like an opt-in for allowing the normal values to work at the priority of the `foo` group. 

Code after:
  HlAttrs prim_aep = syn_attr2entry(prim_attr);

  // start with low-priority attribute, and override colors if present below.
  HlAttrs new_en = char_aep;

  if (prim_aep.cterm_ae_attr & HL_NOCOMBINE) {
    new_en.cterm_ae_attr = prim_aep.cterm_ae_attr;
  } else {
    new_en.cterm_ae_attr = hl_combine_ae(new_en.cterm_ae_attr, prim_aep.cterm_ae_attr);
  }
  if (prim_aep.rgb_ae_attr & HL_NOCOMBINE) {
    new_en.rgb_ae_attr = prim_aep.rgb_ae_attr;
  } else {
    new_en.rgb_ae_attr = hl_combine_ae(new_en.rgb_ae_attr, prim_aep.rgb_ae_attr);
  }

  if (prim_aep.cterm_fg_color > 0) {
    new_en.cterm_fg_color = prim_aep.cterm_fg_color;
    new_en.rgb_ae_attr &= ((~HL_FG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_FG_INDEXED));
  }

  if (prim_aep.cterm_bg_color > 0) {
    new_en.cterm_bg_color = prim_aep.cterm_bg_color;
    new_en.rgb_ae_attr &= ((~HL_BG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_BG_INDEXED));
  }

  if (prim_aep.rgb_fg_color >= 0) {
    new_en.rgb_fg_color = prim_aep.rgb_fg_color;
    new_en.rgb_ae_attr &= ((~HL_FG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_FG_INDEXED));
  }

  if (prim_aep.rgb_bg_color >= 0) {
    new_en.rgb_bg_color = prim_aep.rgb_bg_color;
    new_en.rgb_ae_attr &= ((~HL_BG_INDEXED)
                           | (prim_aep.rgb_ae_attr & HL_BG_INDEXED));
  }

  if (prim_aep.rgb_sp_color >= 0) {
    new_en.rgb_sp_color = prim_aep.rgb_sp_color;
  }

  if (prim_aep.hl_blend >= 0) {
    new_en.hl_blend = prim_aep.hl_blend;
  }

  if ((new_en.url == -1) && (prim_aep.url >= 0)) {
    new_en.url = prim_aep.url;
  }