I was hacking on undo support for notmuch-emacs and sort of accidentally wrote this instead. This series fixes a set of well-known races where tagging from search-mode unexpectedly affects messages that arrived after the search was performed (and hence the user doesn't know they're tagging them). We've attacked this a few times before, but have always run up against something that was missing. It turns out the pieces are finally all in place. The first five patches just clean various things up in preparation. Patches 6 and 7 add support for tagging large queries, which would otherwise become a problem when later patches start using explicit message ID-based queries for tagging. The remaining four patches actually fix the search tagging races using explicit message ID-based queries. It's a fairly long series, but none of the patches are very big.