(or emacs irrelevant)

Wrap a region with a LaTeX environment

Prompted by this StackOverflow question, I wrote down a new package called latex-wrap. It's only a few hours old, but I like it a lot. I've grepped the sources of AUCTeX, and it doesn't seem to have this functionality.

Here's how it works

You start with an active region:

Homer
Marge
Bart
Lisa
Maggie 

After calling M-x latex-wrap-region and selecting enumerate from the list of environments (others being itemize, center etc.), you get this:

\begin{enumerate}
\item Homer
\item Marge
\item Bart
\item Lisa
\item Maggie 
\end{enumerate}

Let's mark everything, M-x latex-wrap-region, and select center:

\begin{center}
  \begin{enumerate}
  \item Homer
  \item Marge
  \item Bart
  \item Lisa
  \item Maggie
  \end{enumerate} 
\end{center}

As you can see, I want to make sure that it's possible to conveniently continue by always placing the point on the end of the last line of the inserted environment.

How it works with no active region

If (looking-back "^ *") is true, the current line is used as if it was the region. Otherwise, an empty environment is inserted after the current line.

Using the code

If you like the idea, you can check out the code at github and test it out. If you know of another package that already does this, do let me know, otherwise I'll post the package on MELPA soon. And if you have some ideas, or want to add a few environments that I forgot to mention to the list, just post an issue, I don't bite.