maybe the 5mm requirement makes this possible by means of a trigger
try creating a M581 E0 T2 trigger for E0 endstop, and in the /sys/trigger2.g file do
G0 E5
M582 T2
that should make it move 5mm, and if the trigger condition is still asserted then it should run the trigger again, and again. You may have to experiment with the S parameter for rising or falling edge of the trigger to get the desired effect.
https://duet3d.dozuki.com/Wiki/Gcode#Section_M581_Configure_external_trigger