A tranny from a 4 banger or a 3.0 won't work unless you can change the bellhousing. The bolt patterns are different. Any tranny from a 2.8 or a 2.9 will bolt up to the 4.0. The tranny to engine bolt pattern are the same for the 2.8,2.9 and 4.0.
The M5OD is the best of the Ranger manual trannys. The belhouseings can be changed on all except the M5OD.
All of them are different lengths, so the best bet would be to get one like the original or you would have to shorten/lengthen the driveshaft and move the tranny crossmember.