(equivalently, if you use screen; the key point is the \eP and the \e\\ between which the real xterm escape codes go, or else screen will eat your escape codes which will never see the light of day)

augroup term_normal
au!
autocmd WinNew * let w:winnew = 1
autocmd CmdlineEnter * let w:outcmd = 0
autocmd CmdlineLeave * let w:outcmd = 1
autocmd WinEnter * if exists ('w:winnew') | unlet w:winnew | if (mode() == 't') | call Termcursor () | endif | elseif get(w:, 'outcmd', 1) && (mode() == 't') | call feedkeys("\<C-@>N", "") | endif
autocmd WinLeave * if (mode() == 't') && (! exists('w:tu')) | call feedkeys("\<C-@>N", 'x') | call Normalcursor () | elseif (mode(1) == 'nt') && (exists('w:tu')) | call feedkeys("i", 'ix') | endif

tmap <expr> <C-@><C-@> ((mode() == 't') && get(w:, 'outcmd', 1)) ? '<C-@>N' : '<C-@><C-@>'
augroup end

let &t_SI = "\eP\e[6 q\e]12;#FF00DF\<C-g>\e\\"
let &t_EI = "\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
let &t_ti = "\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"
let &t_te = "\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
let &t_Us = "\eP\e[4:2m\e\\"
let &t_ds = "\eP\e[4:4m\e\\"
let &t_Ds = "\eP\e[58:5:3m\e[4:5m\e\\"

function! Normalcursor ()
let &t_ve ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
let &t_vi ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
endfunction
function! Echonormalcursor ()
call echoraw("\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\")
endfunction
function! Termcursor ()
let &t_ve ="\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"
let &t_vi ="\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"
endfunction
function! Echotermcursor ()
call echoraw("\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\")
endfunction

augroup termcursor
au!
autocmd ModeChanged *:n* let &t_ve ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
autocmd ModeChanged *:n* let &t_vi ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
autocmd ModeChanged i:* if &t_ve == "" | let &t_ve ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\" | endif
autocmd ModeChanged i:* if &t_vi == "" | let &t_vi ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\" | endif
autocmd ModeChanged *:i* let &t_ve =""
autocmd ModeChanged *:i* let &t_vi =""
autocmd ModeChanged *:t call echoraw("\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\")
autocmd ModeChanged *:t let &t_ve ="\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"
autocmd ModeChanged *:t let &t_vi ="\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"
autocmd ModeChanged t:* call echoraw("\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\")
autocmd ModeChanged t:* if (mode(1) != 'ct') | let &t_ve ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\" | endif
autocmd ModeChanged t:* if (mode(1) != 'ct') | let &t_vi ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\" | endif

" TODO -- doesnt restore from redraw?
let g:cmdlinedepth = 0
autocmd CmdlineEnter * let g:cmdlinedepth = g:cmdlinedepth + 1 | let &t_ve = "\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\" | let &t_vi = "\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"
autocmd CmdlineLeave * let g:cmdlinedepth = g:cmdlinedepth - 1 | if g:cmdlinedepth == 0 | if (mode(1) == 'ct') | call Termcursor () | else | call Normalcursor () | endif | endif
autocmd CmdwinEnter * let &t_ve ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\" | let &t_vi ="\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\"
autocmd CmdwinLeave * let &t_ve = "\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\" | let &t_vi = "\eP\e[4 q\e]12;#FF00DF\<C-g>\e\\"

" autocmd WinEnter * call echoraw("\eP\e[2 q\e]12;#FF00DF\<C-g>\e\\")
augroup end