Patch 8.2.1703
Problem:    ":highlight clear" does not restore default link.
Solution:   Remember the default link and restore it. (Antony Scriven,
            closes #6970, closes #4405)
Files:      runtime/doc/syntax.txt, src/highlight.c,
            src/testdir/test_highlight.vim


*** ../vim-8.2.1702/runtime/doc/syntax.txt      2020-06-28 13:10:17.546125326 
+0200
--- runtime/doc/syntax.txt      2020-09-17 19:50:32.187003450 +0200
***************
*** 4800,4805 ****
--- 4809,4815 ----
                        highlighting for groups added by the user!
                        Uses the current value of 'background' to decide which
                        default colors to use.
+                       If there was a default link, restore it. |:hi-link|
  
  :hi[ghlight] clear {group-name}
  :hi[ghlight] {group-name} NONE
*** ../vim-8.2.1702/src/highlight.c     2020-09-16 15:43:18.521746573 +0200
--- src/highlight.c     2020-09-17 19:56:38.461303144 +0200
***************
*** 73,78 ****
--- 73,79 ----
      char_u    *sg_gui_sp_name;// GUI special color name
  #endif
      int               sg_link;        // link to this highlight group ID
+     int               sg_deflink;     // default link; restored in 
highlight_clear()
      int               sg_set;         // combination of SG_* flags
  #ifdef FEAT_EVAL
      sctx_T    sg_script_ctx;  // script in which the group was last set
***************
*** 715,720 ****
--- 716,722 ----
        char_u      *to_end;
        int         from_id;
        int         to_id;
+       hl_group_T  *hlgroup = NULL;
  
        from_end = skiptowhite(from_start);
        to_start = skipwhite(from_end);
***************
*** 740,746 ****
        else
            to_id = syn_check_group(to_start, (int)(to_end - to_start));
  
!       if (from_id > 0 && (!init || HL_TABLE()[from_id - 1].sg_set == 0))
        {
            /*
             * Don't allow a link when there already is some highlighting
--- 742,755 ----
        else
            to_id = syn_check_group(to_start, (int)(to_end - to_start));
  
!       if (from_id > 0)
!       {
!           hlgroup = &HL_TABLE()[from_id - 1];
!           if (dodefault && (forceit || hlgroup->sg_deflink == 0))
!               hlgroup->sg_deflink = to_id;
!       }
! 
!       if (from_id > 0 && (!init || hlgroup->sg_set == 0))
        {
            /*
             * Don't allow a link when there already is some highlighting
***************
*** 752,772 ****
                if (SOURCING_NAME == NULL && !dodefault)
                    emsg(_("E414: group has settings, highlight link ignored"));
            }
!           else if (HL_TABLE()[from_id - 1].sg_link != to_id
  #ifdef FEAT_EVAL
!                   || HL_TABLE()[from_id - 1].sg_script_ctx.sc_sid
!                                                        != current_sctx.sc_sid
  #endif
!                   || HL_TABLE()[from_id - 1].sg_cleared)
            {
                if (!init)
!                   HL_TABLE()[from_id - 1].sg_set |= SG_LINK;
!               HL_TABLE()[from_id - 1].sg_link = to_id;
  #ifdef FEAT_EVAL
!               HL_TABLE()[from_id - 1].sg_script_ctx = current_sctx;
!               HL_TABLE()[from_id - 1].sg_script_ctx.sc_lnum += SOURCING_LNUM;
  #endif
!               HL_TABLE()[from_id - 1].sg_cleared = FALSE;
                redraw_all_later(SOME_VALID);
  
                // Only call highlight_changed() once after multiple changes.
--- 761,780 ----
                if (SOURCING_NAME == NULL && !dodefault)
                    emsg(_("E414: group has settings, highlight link ignored"));
            }
!           else if (hlgroup->sg_link != to_id
  #ifdef FEAT_EVAL
!                   || hlgroup->sg_script_ctx.sc_sid != current_sctx.sc_sid
  #endif
!                   || hlgroup->sg_cleared)
            {
                if (!init)
!                   hlgroup->sg_set |= SG_LINK;
!               hlgroup->sg_link = to_id;
  #ifdef FEAT_EVAL
!               hlgroup->sg_script_ctx = current_sctx;
!               hlgroup->sg_script_ctx.sc_lnum += SOURCING_LNUM;
  #endif
!               hlgroup->sg_cleared = FALSE;
                redraw_all_later(SOME_VALID);
  
                // Only call highlight_changed() once after multiple changes.
***************
*** 1684,1689 ****
--- 1692,1699 ----
      HL_TABLE()[idx].sg_gui_attr = 0;
  #endif
  #ifdef FEAT_EVAL
+     // Restore any default link.
+     HL_TABLE()[idx].sg_link = HL_TABLE()[idx].sg_deflink;
      // Clear the script ID only when there is no link, since that is not
      // cleared.
      if (HL_TABLE()[idx].sg_link == 0)
*** ../vim-8.2.1702/src/testdir/test_highlight.vim      2020-09-16 
15:43:18.521746573 +0200
--- src/testdir/test_highlight.vim      2020-09-17 19:50:32.187003450 +0200
***************
*** 832,841 ****
    hi clear
  endfunc
  
! " Test default highlighting is restored
! func Test_highlight_restore_defaults()
!   hi! link TestLink Identifier
!   hi! TestHi ctermbg=red
  
    let hlTestLinkPre = HighlightArgs('TestLink')
    let hlTestHiPre = HighlightArgs('TestHi')
--- 832,878 ----
    hi clear
  endfunc
  
! func Test_highlight_clear_restores_links()
!   let aaa_id = hlID('aaa')
!   call assert_equal(aaa_id, 0)
! 
!   " create default link aaa --> bbb
!   hi def link aaa bbb
!   let id_aaa = hlID('aaa')
!   let hl_aaa_bbb = HighlightArgs('aaa')
! 
!   " try to redefine default link aaa --> ccc; check aaa --> bbb
!   hi def link aaa ccc
!   call assert_equal(HighlightArgs('aaa'), hl_aaa_bbb)
! 
!   " clear aaa; check aaa --> bbb
!   hi clear aaa
!   call assert_equal(HighlightArgs('aaa'), hl_aaa_bbb)
! 
!   " link aaa --> ccc; clear aaa; check aaa --> bbb
!   hi link aaa ccc
!   let id_ccc = hlID('ccc')
!   call assert_equal(synIDtrans(id_aaa), id_ccc)
!   hi clear aaa
!   call assert_equal(HighlightArgs('aaa'), hl_aaa_bbb)
! 
!   " forcibly set default link aaa --> ddd
!   hi! def link aaa ddd
!   let id_ddd = hlID('ddd')
!   let hl_aaa_ddd = HighlightArgs('aaa')
!   call assert_equal(synIDtrans(id_aaa), id_ddd)
! 
!   " link aaa --> eee; clear aaa; check aaa --> ddd
!   hi link aaa eee
!   let eee_id = hlID('eee')
!   call assert_equal(synIDtrans(id_aaa), eee_id)
!   hi clear aaa
!   call assert_equal(HighlightArgs('aaa'), hl_aaa_ddd)
! endfunc
! 
! func Test_highlight_default_colorscheme_restores_links()
!   hi link TestLink Identifier
!   hi TestHi ctermbg=red
  
    let hlTestLinkPre = HighlightArgs('TestLink')
    let hlTestHiPre = HighlightArgs('TestHi')
***************
*** 846,864 ****
      syntax reset
    endif
    let g:colors_name = 'test'
!   hi! link TestLink ErrorMsg
!   hi! TestHi ctermbg=green
  
    " Restore default highlighting
    colorscheme default
-   syntax on
    " 'default' should work no matter if highlight group was cleared
    hi def link TestLink Identifier
    hi def TestHi ctermbg=red
- 
    let hlTestLinkPost = HighlightArgs('TestLink')
    let hlTestHiPost = HighlightArgs('TestHi')
- 
    call assert_equal(hlTestLinkPre, hlTestLinkPost)
    call assert_equal(hlTestHiPre, hlTestHiPost)
    hi clear
--- 883,898 ----
      syntax reset
    endif
    let g:colors_name = 'test'
!   hi link TestLink ErrorMsg
!   hi TestHi ctermbg=green
  
    " Restore default highlighting
    colorscheme default
    " 'default' should work no matter if highlight group was cleared
    hi def link TestLink Identifier
    hi def TestHi ctermbg=red
    let hlTestLinkPost = HighlightArgs('TestLink')
    let hlTestHiPost = HighlightArgs('TestHi')
    call assert_equal(hlTestLinkPre, hlTestLinkPost)
    call assert_equal(hlTestHiPre, hlTestHiPost)
    hi clear
*** ../vim-8.2.1702/src/version.c       2020-09-17 19:35:53.650187477 +0200
--- src/version.c       2020-09-17 19:58:27.100844029 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     1703,
  /**/

-- 
A M00se once bit my sister ...
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202009171759.08HHxtKt2462275%40masaka.moolenaar.net.

Raspunde prin e-mail lui