With tweaks from Andrea Canciani, Ben Young, Tennix, and Dirkjan Ochtman. This fulfills https://savannah.gnu.org/bugs/?48163.