Well after working again on that buffer issue, I was trying those examples again and figured the first one was actually working correctly, I thought a line wrapped into X lines would still count as one, somehow. The others example are however not really working as one would expect.
Anyway, there's a difference with what $line reports and what the window displays:
Note: my windows are maximized and $str(1,500) is wrapped into three lines when being displayed.
//window -ehj5000 @a | var %a 5000 | while (%a) { echo @a %a | dec %a }
In the Window's menu in the menubar, select @a, and echo $str(1,500)
At this point, check with /echo -s $line(@a,0), the value is 4998, but the window correctly has 5000 lines.
Echo that long string again and $line(@a,0) is 4996, there is still 5000 lines in the window.
It's also possible to delete lines from the buffer:
//window -ej2 @a | echo @a test | .timer 1 1 echo -a $str(1,500)
Should result in the window being empty.
And with a buffer of 5000 lines:
//window -ehj5000 @a | var %a 5000 | while (%a) { echo @a $str(%a,10) | dec %a } | var %b 1667 | while (%b) { echo @a $str(1,500) | dec %b }
This first displays 5000 small lines, then displays 1667 times a line that is wrapped into three lines, 1667*3 being 5001. Visually, you can see that the window contains only blocks of three lines, which means there is probably 4498 lines.
Now I'm not really sure how one would handle those situations but $line is clearly buggy, and those behaviors are not really expected, I suspect this is what is happening to people in the others thread. Note that the big script in this thread is pointless since $line is affected regardless of the buffer issue.