Indentation of the body can be a little unclear. For example, I usually indent equation environments, but would never dream of indenting the document environment.
To add environment insertion into the LaTeX mode for SubEthaEdit, we begin by getting the environment name using
tryblock is to handle when the user cancels instead of entering and environment name. Somewhat arbitrarily, I set the default answer to be "equation", since my guess is that equations are probably the most common environment.
After that, there is just some fiddly work getting the formatting of the environment correct. It needs to intelligently insert newlines and tabs to keep the document readable. As well, something needs to be done with the selection text. There are two possibilities as I see it: (1) treat the selection as the name of the environment and (2) treat the selection as the body of the environment. I went with the latter. Finally, it would be nice to place the insertion point somewhere reasonable; I think it works nicely at the end of the body, especially since the insertion point is positioned to start typing immediately if the body is empty. Putting it all together, we have:
Note that I've made repeated use of a convenience function:
selectByComparinghandler is not part of SubEthaEditTools - maybe it should be?
That's all there is to it, apart from the boilerplate:
Update: There is an interesting possibility for adjusting the default environment. I had used "equation" as the default, but it was pretty arbitrary. Another approach would be to just repeat whichever environment was last given. We just add a property to the script that holds the default environment, use its value in making the dialog, and update the property based on the dialog result:
Is this actually a good idea? or will it just be annoying? Hard to say without using it, so I guess I'll try it out for a while.