The delay you're talking about is because of $wrap(), which is notoriously slow (although I remember it being optimized a few versions ago). Here's a faster alternative that uses /echo -h and a hidden window to determine the number of wraps:
alias selectmiddleline {
var %x = $1, %wanted = $calc($window(@ex1).dh / 2 / $window(@ex1).fontsize)
window -h @wrap
while $line(@ex1,%x) != $null {
echo -h @wrap $v1
if $line(@wrap,0) > %wanted { sline -a @ex1 %x | close -@ @wrap | return }
inc %x
}
close -@ @wrap
sline -a @ex1 $line(@ex1,0)
}
The main problem with your approach is that not all lines are wrapped. And while in DaveC's example the wrapped and unwrapped lines are roughly evenly distributed (because of $r(0,1)), this may not be the case in real-world cases. Even in this example, sometimes the line isn't centered exactly (it's as much as 5 lines off). Unfortunately I don't have any alternative or workaround to suggest.