Emacs: refactor some code. Improve re-usability and semantics.