Title: Emacs for TextMate junkies
Date: June 22, 2007
Timestamp: 1182565020
Author: sjs
Tags: emacs, textmate
----

<p><em>Update #1: What I first posted will take out your &lt; key by mistake (it’s available via <code>C-q &lt;</code>), it has since been revised to Do The Right Thing.</em></p>


	<p><em>Update #2: Thanks to an anonymouse[sic] commenter this code is a little cleaner.</em></p>


	<p><em>Update #3: I should read the Emacs manual sometime, especially since I have it in dead-tree form. Check out <a href="http://web.archive.org/web/20070626150908/http://www.gnu.org/software/emacs/manual/html_node/autotype/Inserting-Pairs.html">skeleton pairs</a> in the Emacs manual.</em></p>


	<p>Despite my current infatuation with Emacs there are many reasons I started using TextMate, especially little time-savers that are very addictive. I’ll talk about one of those features tonight. When you have text selected in TextMate and you hit say the <code>'</code> (single quote) then TextMate will surround the selected text with single quotes. The same goes for double quotes, parentheses, brackets, and braces. This little trick is one of my favourites so I had to come up with something similar in Emacs. It was easy since a <a href="http://web.archive.org/web/20070626150908/http://osdir.com/ml/emacs.nxml.general/2005-08/msg00002.html">mailing list post</a> has a solution for surrounding the current region with tags, which served as a great starting point.</p>


<table class="CodeRay"><tr>
  <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt></pre></td>
  <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">(defun surround-region-with-tag (tag-name beg end)<tt>
</tt>      (interactive "sTag name: \nr")<tt>
</tt>      (save-excursion<tt>
</tt>        (goto-char beg)<tt>
</tt>        (insert "&lt;" tag-name "&gt;")<tt>
</tt>        (goto-char (+ end 2 (length tag-name)))<tt>
</tt>        (insert "&lt;/" tag-name "&gt;")))</pre></td>
</tr></table>


	<p>With a little modification I now have the following in my ~/.emacs file:</p>


<table class="CodeRay"><tr>
  <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt>26<tt>
</tt>27<tt>
</tt>28<tt>
</tt>29<tt>
</tt><strong>30</strong><tt>
</tt>31<tt>
</tt>32<tt>
</tt>33<tt>
</tt>34<tt>
</tt>35<tt>
</tt>36<tt>
</tt>37<tt>
</tt>38<tt>
</tt>39<tt>
</tt><strong>40</strong><tt>
</tt>41<tt>
</tt>42<tt>
</tt>43<tt>
</tt>44<tt>
</tt>45<tt>
</tt>46<tt>
</tt>47<tt>
</tt></pre></td>
  <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">;; help out a TextMate junkie<tt>
</tt><tt>
</tt>(defun wrap-region (left right beg end)<tt>
</tt>  "Wrap the region in arbitrary text, LEFT goes to the left and RIGHT goes to the right."<tt>
</tt>  (interactive)<tt>
</tt>  (save-excursion<tt>
</tt>    (goto-char beg)<tt>
</tt>    (insert left)<tt>
</tt>    (goto-char (+ end (length left)))<tt>
</tt>    (insert right)))<tt>
</tt><tt>
</tt>(defmacro wrap-region-with-function (left right)<tt>
</tt>  "Returns a function which, when called, will interactively `wrap-region-or-insert' using LEFT and RIGHT."<tt>
</tt>  `(lambda () (interactive)<tt>
</tt>     (wrap-region-or-insert ,left ,right)))<tt>
</tt><tt>
</tt>(defun wrap-region-with-tag-or-insert ()<tt>
</tt>  (interactive)<tt>
</tt>  (if (and mark-active transient-mark-mode)<tt>
</tt>      (call-interactively 'wrap-region-with-tag)<tt>
</tt>    (insert "&lt;")))<tt>
</tt><tt>
</tt>(defun wrap-region-with-tag (tag beg end)<tt>
</tt>  "Wrap the region in the given HTML/XML tag using `wrap-region'. If any<tt>
</tt>attributes are specified then they are only included in the opening tag."<tt>
</tt>  (interactive "*sTag (including attributes): \nr")<tt>
</tt>  (let* ((elems    (split-string tag " "))<tt>
</tt>         (tag-name (car elems))<tt>
</tt>         (right    (concat "&lt;/" tag-name "&gt;")))<tt>
</tt>    (if (= 1 (length elems))<tt>
</tt>        (wrap-region (concat "&lt;" tag-name "&gt;") right beg end)<tt>
</tt>      (wrap-region (concat "&lt;" tag "&gt;") right beg end))))<tt>
</tt><tt>
</tt>(defun wrap-region-or-insert (left right)<tt>
</tt>  "Wrap the region with `wrap-region' if an active region is marked, otherwise insert LEFT at point."<tt>
</tt>  (interactive)<tt>
</tt>  (if (and mark-active transient-mark-mode)<tt>
</tt>      (wrap-region left right (region-beginning) (region-end))<tt>
</tt>    (insert left)))<tt>
</tt><tt>
</tt>(global-set-key "'"  (wrap-region-with-function "'" "'"))<tt>
</tt>(global-set-key "\"" (wrap-region-with-function "\"" "\""))<tt>
</tt>(global-set-key "`"  (wrap-region-with-function "`" "`"))<tt>
</tt>(global-set-key "("  (wrap-region-with-function "(" ")"))<tt>
</tt>(global-set-key "["  (wrap-region-with-function "[" "]"))<tt>
</tt>(global-set-key "{"  (wrap-region-with-function "{" "}"))<tt>
</tt>(global-set-key "&lt;"  'wrap-region-with-tag-or-insert) ;; I opted not to have a wrap-with-angle-brackets</pre></td>
</tr></table>


	<p><a href="http://web.archive.org/web/20070626150908/http://sami.samhuri.net/assets/2007/6/23/wrap-region.el" alt="wrap-region.el">Download wrap-region.el</a></p>


	<p>That more or less sums up why I like Emacs so much. I wanted that functionality so I implemented it (barely! It was basically done for me), debugged it by immediately evaluating sexps and then trying it out, and then once it worked I reloaded my config and used the wanted feature. That’s just awesome, and shows one strength of open source.</p>
