Improvements to %run completions

This fixes #3459, and a couple of other problems I spotted with completions after %run while working on it. Tests are included.