Orthogonally to my other answer, here's a version of SetVirtualEdit()
that you can plug into your existing solution which does not require Python:
function! SetVirtualEdit() abort
let saved_view = winsaveview()
keepjumps normal! g0
let is_on_leftmost_screen = col('.') == 1
call winrestview(saved_view)
if is_on_leftmost_screen
set virtualedit=
endif
endfunction
It works by moving the cursor to the left of the screen with the g0
command, and then checking if it's at the start of the buffer's line, using :keepjumps
and winsaveview
/winrestview
to prevent the movement from affecting Vim's state.