Gvim: Smart buffer list tooltip on :b/:sb

When I use :b or :sb or :vert sb, I want a quick way to see the available buffers and to quickly switch to the one I choose, and the quickest way is usually typing its number right away.

This displays a tooltip with the list of buffers right when one such command is typed. The tooltip is dismissed once the command line loses focus (or the command is corrected to something else).

"This piece of software is released under the GNU General Public License v3.
function BuffersList()
  if getcmdtype() != ":" || match(getcmdline(), '^ *\(b\|sb\|vert sb\)') < 0
    return
  endif
  let res = ""
  let b = 1
  while b <= bufnr('$')
    if buflisted(b)
      let s = b == bufnr("%") ? ">" : bufwinnr(b) > 0 ? "*" : " "
      let res = res . s . printf("%3d", b) . " " . fnamemodify(bufname(b), ":p") . "\n"
    endif
    let b += 1
  endwhile
  call balloon_show(strpart(res, 0, strcharlen(res)-1))
endfunction
au CmdlineChanged * call BuffersList()
au CmdlineLeave * call balloon_show("")

It works perfectly on Motif and Athena versions of Gvim. It works for GTK too, but since the default tooltip font is not monospaced, alignment suffers (there is probably a way to fix that with CSS though).

Unfortunately it doesn't work on terminal Vim.

Technically, it will still show the tooltip for commands that don't accept buffer arguments but begin with :b or :sb, but those are mostly obscure so I didn't bother.

It would be admitedly better to use :create_popup as then it would work on all Vims, but I still could not get it to work correctly. It has problems with CmdlineChanged.