Stole this from auctex / proof-general code, wondering if I should just regex and capture $1 instead.