This is an improved variant of the patch here https://sourceware.org/ml/gdb-patches/2014-09/msg00109.html that also works properly for secondary prompts (and overlong lines).