0

Consider the following snippet:

vim9script

var colors = getcompletion('', 'color', true)

def PopupFilterColor(id: number, key: string): bool
  if index(['j', "\<down>", "\<c-n>"], key) != -1
    win_execute(id, 'norm j')
    var idx = line('.', id)
    var scheme = getbufline(winbufnr(id), idx)[0]
    exe $'colorscheme {scheme}'
    return true
  elseif index(['k', "\<Up>", "\<c-p>"], key) != -1
    win_execute(id, 'norm k')
    var idx = line('.', id)
    var scheme = getbufline(winbufnr(id), idx)[0]
    exe $'colorscheme {scheme}'
    return true
  elseif key == "\<esc>"
    popup_close(id)
    colorscheme blue
    return true
  endif
  return false
enddef


var opts = {
  filter: PopupFilterColor
}

popup_menu(colors, opts)

Now, source the script and scroll up and down. You see the colorscheme changes as you move in the popup_menu. Next, hit <esc> and source the script again.

Now, the hightlight is often lost when you scroll the items displayed in the popup menu.

What am I doing wrong? How to fix it?

1 Answer 1

1

If you're about current line highlight, this might be the issue with default bundled colorschemes.

In short, some of the default colorschemes, for example, koehler define PopupSelected highlight group, most of the others do not.

So when you switch colorshemes for the first time PopupSelected is not defined and popup_menu uses PmenuSel (see popup_menu help topic).

While trying all the colorschemes you came across koehler and now PopupSelected is defined and when you switch to another colorscheme where PopupSelected is not set -- it is cleared.

Now the second time you run it with existing PopupSelected which is cleared for most of the colorschemes.

We might add PopupSelected to bundled colorschemes -- if you could create an issue in https://github.com/vim/colorschemes repo, it might let me not forget about it next time I am on it.

As for workaround, add hi link PopupSelected PmenuSel after colorscheme:

vim9script

var colors = getcompletion('', 'color', true)

def PopupFilterColor(id: number, key: string): bool
  if index(['j', "\<down>", "\<c-n>"], key) != -1
    win_execute(id, 'norm j')
    var idx = line('.', id)
    var scheme = getbufline(winbufnr(id), idx)[0]
    exe $'colorscheme {scheme}'
    hi link PopupSelected PmenuSel
    return true
  elseif index(['k', "\<Up>", "\<c-p>"], key) != -1
    win_execute(id, 'norm k')
    var idx = line('.', id)
    var scheme = getbufline(winbufnr(id), idx)[0]
    exe $'colorscheme {scheme}'
    hi link PopupSelected PmenuSel
    return true
  elseif key == "\<esc>"
    popup_close(id)
    colorscheme blue
    hi link PopupSelected PmenuSel
    return true
  endif
  return false
enddef


var opts = {
  cursorline: 1,
  filter: PopupFilterColor
}

popup_menu(colors, opts)

or instead, add autocommand:

augroup colorschemes
    au!
    au Colorscheme * hi link PopupSelected PmenuSel
augroup END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.