extract common code into polymode-common.el