Bring autofold feature in from the template branch.

Independent of templates, autofolding is a useful and interesting feature, and its implementation is simple.

This pull request brings that feature in.