rickhowe / diffchar.vim

Highlight the exact differences, based on characters and words
http://www.vim.org/scripts/script.php?script_id=4932
MIT License
217 stars 10 forks source link

Error when starting in diffmode #34

Closed sblask closed 1 year ago

sblask commented 1 year ago

I get the following error when starting in diffmode:

Error detected while processing VimEnter Autocommands for "*"..function diffchar#Toggl
eDiffModeSync[23]..<SNR>110_ShowDiffChar[65]..<SNR>110_ToggleDiffHL:
line    2:
E421: Color name or number not recognized: ctermbg=#393939 ctermfg=#99cc99 cterm=NONE
P

I believe it's when diffchar remaps DiffAdd

It started yesterday when I updated neovim and my plugins, so not sure where the change came from that triggers the problem. My dotfiles including my vim config are here: https://github.com/sblask/dotfiles

I usually run git difftool which is mapped to

[difftool "vimdiff2"]
    cmd = nvim -f -d \"$LOCAL\" \"$REMOTE\" -c \"windo set nofoldenable\"

And I get the error right away. I also use base16 and when I don't enable the colorscheme the error does no occur. But I don't think it does anything special, in particular it does:

https://github.com/chriskempson/base16-vim/blob/master/colors/base16-eighties.vim#LL275C34-L275C34

and when just starting nvim without parameters and run :hi DiffAdd I get:

:hi DiffAdd
DiffAdd        xxx ctermfg=2 ctermbg=18
                   guifg=#99cc99 guibg=#393939
Press ENTER or type command to continue

So it seems to me that somewhere in here: https://github.com/rickhowe/diffchar.vim/blob/master/autoload/diffchar.vim#L315 the gui and the term values get swapped.

rickhowe commented 1 year ago

Thank you for your question. I tried to install and use base16 color schemes on my nvim 8.0 in WIndows 10, but can not reproduce the problem. And you are right, 'cterm' is wrongly used in my plugin. Just let me know the followings in your env. In my env, 0, 1, 1 for them.

has('gui_running')
has('termguicolors')
&termguicolors
sblask commented 1 year ago

For me it's 0, 1, 0. I forgot to mention: I tried setting termguicolors and it makes this problem go away, but it also totally messed up my colours and I couldn't find a way to make them work properly. I am on Neovim 0.9 and MacOS.

rickhowe commented 1 year ago

I can reproduce it if &termguicolors is off in my nvim. This looks 'cterm' but synIDattr() returns gui values., which is different from vim. I will check and fix in the next update. As a workaround, you can change the code something like:

let hm = (has('nvim') || has('gui_running') ||
                  \has('termguicolors') && &termguicolors) ? 'gui' : 'cterm'
rickhowe commented 1 year ago

I found nvim_list_uis() to check if nvim uses 'cterm' or 'gui' color.

Just after:

  let hm = (has('gui_running') || has('termguicolors') && &termguicolors) ?
                                                              \'gui' : 'cterm'

Add this:

  if exists('*nvim_list_uis')
    let nu = nvim_list_uis()
    if !empty(nu) | let hm = (nu[0].rgb == v:true) ? 'gui' : 'cterm' | endif
  endif

Hope you can check it. If it works, I will update the plugin.

sblask commented 1 year ago

That does get rid of the error, but it seems like the diff is not working anymore like that? If I understand this correctly the change makes it so only guifg and guibg are set (with the correct values from DiffAdd) but as I am in the terminal ctermfg and ctermbg should be set to their respective values as well.

rickhowe commented 1 year ago

Let me know what nvim_list_uil() returns in your env. In my nvim 0.9.0 on Windows 10,

when termguicolors=1:
[{'ext_multigrid': v:false, 'term_name': 'builtin_vtpcon', 'height': 25, 'stdout_tty': v:true, 'chan': 1, 'ext_wildmenu': v:false, 'ext_popupmenu': v:false, 'width': 80, 'rgb': v:true, 'override': v:false, 'ext_hlstate': v:false, 'ext_messages': v:false, 'ext_termcolors': v:true, 'ext_tabline': v:false, 'ext_linegrid': v:true, 'term_background': '', 'ext_cmdline': v:false, 'stdin_tty': v:true, 'term_colors': 256}]

when termguicolors=0:
[{'ext_multigrid': v:false, 'term_name': 'builtin_vtpcon', 'height': 25, 'stdout_tty': v:true, 'chan': 1, 'ext_wildmenu': v:false, 'ext_popupmenu': v:false, 'width': 80, 'rgb': v:false, 'override': v:false, 'ext_hlstate': v:false, 'ext_messages': v:false, 'ext_termcolors': v:true, 'ext_tabline': v:false, 'ext_linegrid': v:true, 'term_background': '', 'ext_cmdline': v:false, 'stdin_tty': v:true, 'term_colors': 256}]

termguicolors is consistent with 'rgb'.

If it returns an empty list, I can not use the function. If so, let me know what they return for both termguicolors 0 and 1:

synIDattr(hlID('DiffAdd'), 'bg#')
synIDattr(hlID('DiffAdd'), 'bg#', 'cterm')
synIDattr(hlID('DiffAdd'), 'bg#', 'gui')
sblask commented 1 year ago

Not sure how to best call these functions, but when I add them to diffchar.vim:

function! diffchar#ToggleDiffModeSync(...) abort
  " not sure whether there is a better way to call these?
  echoerr nvim_list_uis()
  echoerr synIDattr(hlID('DiffAdd'), 'bg#')
  echoerr synIDattr(hlID('DiffAdd'), 'bg#', 'cterm')
  echoerr synIDattr(hlID('DiffAdd'), 'bg#', 'gui')

I get:

Error detected while processing VimEnter Autocommands for "*"..function diffchar#ToggleDiffModeSync:
line    1:
[{'ext_multigrid': v:false, 'term_name': 'screen-256color', 'height': 29, 'stdout_tty': v:true, 'chan': 1, 'ext_wildmenu': v:false, 'ext_popupmenu': v:false, 'width': 212, 'rgb': v:true, 'override': v:false, 'ext
_hlstate': v:false, 'ext_messages': v:false, 'ext_termcolors': v:true, 'ext_tabline': v:false, 'ext_linegrid': v:true, 'term_background': '', 'ext_cmdline': v:false, 'stdin_tty': v:true, 'term_colors': 256}]
Press ENTER or type command to continue
Error detected while processing VimEnter Autocommands for "*"..function diffchar#ToggleDiffModeSync:
line    2:
#393939
Press ENTER or type command to continue
Error detected while processing VimEnter Autocommands for "*"..function diffchar#ToggleDiffModeSync:
line    3:
18
Press ENTER or type command to continue
Error detected while processing VimEnter Autocommands for "*"..function diffchar#ToggleDiffModeSync:
line    4:
#393939

With termguicolors I get:

[{'ext_multigrid': v:false, 'term_name': 'screen-256color', 'height': 29, 'stdout_tty': v:true, 'chan': 1, 'ext_wildmenu': v:false, 'ext_popupmenu': v:false, 'width': 212, 'rgb': v:true, 'override': v:false, 'ext
_hlstate': v:false, 'ext_messages': v:false, 'ext_termcolors': v:true, 'ext_tabline': v:false, 'ext_linegrid': v:true, 'term_background': '', 'ext_cmdline': v:false, 'stdin_tty': v:true, 'term_colors': 256}]
P
rickhowe commented 1 year ago

Thanks. Your nvim uses 'gui' color, not 'cterm'. It looks there is another reason why your diff does not work. Execute this on the command line: :echo t:DChar Are there more then 2 diff windows in a tab page?

sblask commented 1 year ago

That gives me:

{'wid': {'1': 1000, '2': 1001}, 'dtf': function('<SNR>110_LuaVimDiff'), 'dfp': 0, 'hgp': ['dcDiffText'], '
dfl': {'1': [42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55], '2': [41, 42, 43, 44, 45, 46, 47, 48
, 49, 50, 51, 52, 53, 54]}, 'mid': {'1': {'48': [1040, 1041], '45': [1034, 1035], '51': [1046, 1047], '47'
: [1038, 1039], '42': [1028, 1029], '43': [1030, 1031], '44': [1032, 1033], '50': [1044, 1045], '46': [103
6, 1037], '52': [1048, 1049], '53': [1050, 1051], '49': [1042, 1043], '55': [1054, 1055], '54': [1052, 105
3]}, '2': {'48': [1020, 1021], '45': [1014, 1015], '54': [1026, 1027], '41': [1006, 1007], '42': [1008, 10
09], '43': [1010, 1011], '44': [1012, 1013], '50': [1000, 1001], '46': [1016, 1017], '52': [1002, 1003], '
53': [1004, 1005], '49': [1022, 1023], '51': [1024, 1025], '47': [1018, 1019]}}, 'hlc': {'1': {'48': [['a'
, [5, 7]]], '45': [['a', [5, 7]]], '51': [['a', [5, 7]]], '47': [['a', [5, 7]]], '42': [['a', [5, 7]]], '4
3': [['a', [5, 7]]], '44': [['a', [5, 7]]], '50': [['a', [5, 7]]], '46': [['a', [5, 7]]], '52': [['a', [5,
 7]]], '53': [['a', [5, 7]]], '49': [['a', [5, 7]]], '55': [['a', [5, 7]]], '54': [['a', [5, 7]]]}, '2': {
'48': [['d', [4, 5]]], '45': [['d', [4, 5]]], '54': [['d', [4, 5]]], '41': [['d', [4, 5]]], '42': [['d', [
4, 5]]], '43': [['d', [4, 5]]], '44': [['d', [4, 5]]], '50': [['d', [4, 5]]], '46': [['d', [4, 5]]], '52':
 [['d', [4, 5]]], '53': [['d', [4, 5]]], '49': [['d', [4, 5]]], '51': [['d', [4, 5]]], '47': [['d', [4, 5]
]]}}, 'cks': {'1': {'50': 40119, '51': 57895, '52': 16913, '53': 25381, '54': 57755, '55': 31882, '42': 16
871, '43': 58956, '44': 1932, '45': 34760, '46': 47534, '47': 4218, '48': 1775, '49': 23634}, '2': {'50':
30272, '52': 58442, '53': 24678, '41': 23561, '42': 973, '43': 48041, '44': 46974, '45': 4467, '46': 13097
, '47': 2416, '48': 55139, '49': 31574, '51': 53731, '54': 27968}}, 'bnr': {'1': 2, '2': 1}, 'opt': {'ic':
 0, 'iw': 0, 'ih': 1, 'pv': 1, 'cl': 0, 'ut': 'Word1', 'cn': 'base16-eighties'}, 'dpv': {'pv': 1, 'ch': {}
}, 'lcc': {'1': {'tl': 29, 'cc': 1, 'bl': 54, 'll': 71, 'cl': 48, 'cn': 1}, '2': {'tl': 29, 'cc': 1, 'bl':
 53, 'll': 70, 'cl': 40, 'cn': 0}}, 'upa': '\%(\w\+\|\W\)\zs'}

No, it's only two splits.

I am pretty sure nvim uses cterm colours because I can diff and add a line so DiffAdd is used and then with :hi DiffAdd ctermbg=5 the background colour changes and with :hi DiffAdd guibg=red it doesn't. It should be the other way around if nvim were using gui colors?

rickhowe commented 1 year ago

Your t:DChar looks fine. Probably :echo getmatches() shows where is highlighted in which color.

While in diff mode, if you add/change some cterm colors to the plugin specific highlights: hi dcDiffAdd ctermfg=xx ctermbg=yy Does your diff look changed?

I am still not sure why your nvim uses cterm colors because nvim_list_uis() and sysnIDattr() return gui colors in your nvim.

sblask commented 1 year ago

:echo getmatches()

gives me:

[{'group': 'dcDiffChange', 'id': 1000, 'priority': -5, 'pos1': [50]}, {'group': 'dcDiffChange', 'id': 1002
, 'priority': -5, 'pos1': [52]}, {'group': 'dcDiffChange', 'id': 1004, 'priority': -5, 'pos1': [53]}, {'gr
oup': 'dcDiffChange', 'id': 1006, 'priority': -5, 'pos1': [55]}, {'group': 'dcDiffChange', 'id': 1008, 'pr
iority': -5, 'pos1': [42]}, {'group': 'dcDiffChange', 'id': 1010, 'priority': -5, 'pos1': [43]}, {'group':
 'dcDiffChange', 'id': 1012, 'priority': -5, 'pos1': [44]}, {'group': 'dcDiffChange', 'id': 1014, 'priorit
y': -5, 'pos1': [45]}, {'group': 'dcDiffChange', 'id': 1016, 'priority': -5, 'pos1': [46]}, {'group': 'dcD
iffChange', 'id': 1018, 'priority': -5, 'pos1': [47]}, {'group': 'dcDiffChange', 'id': 1020, 'priority': -
5, 'pos1': [48]}, {'group': 'dcDiffChange', 'id': 1022, 'priority': -5, 'pos1': [49]}, {'group': 'dcDiffCh
ange', 'id': 1024, 'priority': -5, 'pos1': [51]}, {'group': 'dcDiffChange', 'id': 1026, 'priority': -5, 'p
os1': [54]}, {'group': 'dcDiffAdd', 'id': 1001, 'priority': -3, 'pos1': [50, 5, 3]}, {'group': 'dcDiffAdd'
, 'id': 1003, 'priority': -3, 'pos1': [52, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1005, 'priority': -3, 'pos
1': [53, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1007, 'priority': -3, 'pos1': [55, 5, 3]}, {'group': 'dcDiff
Add', 'id': 1009, 'priority': -3, 'pos1': [42, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1011, 'priority': -3,
'pos1': [43, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1013, 'priority': -3, 'pos1': [44, 5, 3]}, {'group': 'dc
DiffAdd', 'id': 1015, 'priority': -3, 'pos1': [45, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1017, 'priority':
-3, 'pos1': [46, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1019, 'priority': -3, 'pos1': [47, 5, 3]}, {'group':
 'dcDiffAdd', 'id': 1021, 'priority': -3, 'pos1': [48, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1023, 'priorit
y': -3, 'pos1': [49, 5, 3]}, {'group': 'dcDiffAdd', 'id': 1025, 'priority': -3, 'pos1': [51, 5, 3]}, {'gro
up': 'dcDiffAdd', 'id': 1027, 'priority': -3, 'pos1': [54, 5, 3]}]
Press ENTER or type command to continue

:hi dcDiffAdd ctermbg=2 does change the background of the change

rickhowe commented 1 year ago

Thank you. Those behavior is reasonable.

I found nvim 0.8.x is OK but 0.9.0 unstable to handle cterm and gui colors. Probably it would be fixed later but I did update my plugin to take care of both colors together. Can I ask you to replace your autoload/diffchar.vim with this and try it on your nvim?

sblask commented 1 year ago

That seems to work, no more error and the diff looks right. Thank you!

rickhowe commented 1 year ago

I updated the plugin to 9.5. Close this issue if it works.

sblask commented 1 year ago

9.5 works! Thanks again for the fix and for the amazing plugin.