1

I'm trying to sync the cursors movement of a popup window, and the current window.

call range(50)
    \ ->map({_, i -> string(i)})
    \ ->popup_create(#{
    \     line: 1,
    \     col: 1,
    \     minwidth: 1,
    \     minheight: 1,
    \     maxheight: &lines - 1,
    \     cursorline: 1,
    \     wrap: 0,
    \     mapping: 0,
    \     filter: 'popup_filter_menu',
    \     firstline: 1,
    \ })

I tried using windo set scrollbind, but this doesn't work on a popup window (as far as i know).

Any ways to do this?

2

I tried using windo set scrollbind, but this doesn't work on a popup window (as far as i know).

It cannot work because :windo only works on windows which have a number; popup windows don't.

Any ways to do this?

OTOH, popup windows do have an ID. So, you could try:

call win_execute(winid, 'setlocal scrollbind')

Or:

call setwinvar(winid, '&scrollbind', 1)

But it won't give you the desired result.


In case you're interested in an alternative solution, try this:

vim9script

com -bang -bar CustomColumnNumber CustomColumnNumber(<bang>0)

def CustomColumnNumber(off: bool)
    if off
        if exists('b:_ccn_winid')
            popup_close(b:_ccn_winid)
            unlet! b:_ccn_winid b:_ccn_lastlnum
            au! scroll_custom_numbercolumn
        endif
        return
    elseif exists('b:_ccn_winid')
        return
    endif
    let what = range(1, line('$'))->map({_, i -> string(i)})
    b:_ccn_winid = popup_create(what, #{
        line: 1,
        col: 1,
        minwidth: 1,
        minheight: 1,
        maxheight: &lines - 1,
        wrap: 0,
        mapping: 0,
        firstline: 1,
        })
    augroup scroll_custom_numbercolumn
        au! * <buffer>
        au CursorMoved <buffer> Scroll_custom_column_number()
    augroup END
enddef

def Scroll_custom_column_number()
    let curlnum = line('.')
    if curlnum == get(b:, '_ccn_lastlnum') || !exists('b:_ccn_winid')
        return
    endif
    b:_ccn_lastlnum = curlnum
    popup_setoptions(b:_ccn_winid, #{firstline: line('w0')})
enddef

Write the code in ~/.vim/plugin/mycustomcolumnnumber.vim. It should install the custom command :CustomColumnNumber which should display a popup window with numbers matching the line addresses; and it should scroll when you move in the buffer. Add a bang to toggle the popup off: :CustomColumnNumber!.

I only tested the code briefly, so there might be bugs to fix, and you might want to tweak the code to change the appearance of the popup.

| improve this answer | |
  • Seems to work perfectly :) Though, one little addition I tried was adding set number in one of the function, so that it set the correct width, and don't hover on the text of the window...doesn't seem to work when i do that. – Nordine Lotfi Sep 16 at 19:08
  • Nvm, works now. Disregard what i said above :D – Nordine Lotfi Sep 16 at 19:14
2

I've had some success by creating the popup window without focus:

let m = range(50)
    \ ->map({_, i -> string(i)})
    \ ->popup_create(#{
    \     line: 1,
    \     col: 1,
    \     minwidth: 1,
    \     minheight: 1,
    \     maxheight: &lines - 1,
    \     cursorline: 1,
    \     wrap: 0,
    \     firstline: 1,
    \     scrollbar: 0,
    \ })

And then using win_execute() to run a normal! mode command setting both the first line and the cursor line. For example, to keep line 20 (the one that contains the text "19") at the top, and the cursor on line 30 (the one with the text "29"), you can use:

call win_execute(m, 'normal! 20Gzt30G')

Unfortunately I couldn't find a combination using popup_setoptions() with firstline and cursorline that worked, that would have been better I guess...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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