use derived mode 

Is there any reason why not using derived modes and let emacs do most of the bookeeping?

The PR is not ready yet but I wanted to know if it makes sense to keep improving it and eventually merge it to master