Fix various \r issues with windows (patch slightly adapted to v8.4)

This closes bugs 3150, 3151, 3152, and 3153.