<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Singletoned &#187; yasnippet</title>
	<atom:link href="http://blog.singletoned.net/category/yasnippet/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.singletoned.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 21 Jan 2010 20:50:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Emacs: Wrap lines in html tags</title>
		<link>http://blog.singletoned.net/2009/10/emacs-wrap-lines-in-html-tags/</link>
		<comments>http://blog.singletoned.net/2009/10/emacs-wrap-lines-in-html-tags/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 20:23:51 +0000</pubDate>
		<dc:creator>Ed Singleton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[yasnippet]]></category>

		<guid isPermaLink="false">http://blog.singletoned.net/?p=30</guid>
		<description><![CDATA[Before I switched to  Emacs, I used TextMate and was mightily
impresed by it.  (I still am).  There are lots of little touches in
there that haven&#8217;t been done better in any other editor, and the
snippet syntax is one of the best balances between ease of use and power I&#8217;ve
seen for a long time.

However [...]]]></description>
			<content:encoded><![CDATA[<p>Before I switched to  Emacs, I used <a href="http://macromates.com/">TextMate</a> and was mightily
impresed by it.  (I still am).  There are lots of little touches in
there that haven&#8217;t been done better in any other editor, and the
snippet syntax is one of the best balances between ease of use and power I&#8217;ve
seen for a long time.</p>

<p>However Emacs is better, cause Steve Yegge says so, and so I switched.
I used yasnippet for while, and loved it.  It&#8217;s a reworking of
TextMate&#8217;s snippet system with added Lisp; more power without a
sacrifice in ease of use.  For some reason I stopped using, but since a <a href="http://stackoverflow.com/questions/1558178/wrap-selection-in-open-close-tag-like-textmate/">StackOverflow
question</a> recently inspired me, I&#8217;m back with a vengence.</p>

<p>A yasnippet to wrap a region or the point in a html tag:</p>

<pre><code>(defun wrap-region-or-point-with-html-tag (start end)
  "Wraps the selected text or the point with a tag"
  (interactive "r")
  (let (string)
    (if mark-active 
        (list (setq string (buffer-substring start end))
          (delete-region start end)))
    (yas/expand-snippet (point)
                        (point)
                        (concat "&lt;${1:p}&gt;" string "$0&lt;/${1:$(replace-regexp-in-string \" .*\" \"\" text)}&gt;"))))

(global-set-key (kbd "C-W") 'wrap-region-or-point-with-html-tag)
</code></pre>

<p>I wrote this a while, and at the time it was one of the most complicated
pieces of Lisp I had written.</p>

<p>I initially misread the StackOverflow question, and submitted the
previous function.  However once I realised that they wanted something
more complex, I felt challenged and dived right in.  I eventually came
up with this, which hasn&#8217;t really been tested:</p>

<pre><code>(defun wrap-lines-in-region-with-html-tag (start end)
  "Wraps the selected text or the point with a tag"
  (interactive "r")
  (let (string)
    (if mark-active 
        (list (setq string (buffer-substring start end))
              (delete-region start end)))
    (yas/expand-snippet
     (replace-regexp-in-string "\\(&lt;$1&gt;\\).*\\'" "&lt;${1:p}&gt;"
      (mapconcat
       (lambda (line) (format "%s" line))
       (mapcar
        (lambda (match) (concat "&lt;$1&gt;" match "&lt;/${1:$(replace-regexp-in-string \" .*\" \"\" text)}&gt;"))
        (split-string string "[\r\n]")) "\n") t nil 1) (point) (point))))
</code></pre>

<p>From the end of the function working back (which seems to be the way
with Functional style), it:</p>

<ul>
<li>splits the region by linebreaks</li>
<li>wraps each line with the yasnippet syntax</li>
<li>formats each entry as a string</li>
<li>concatenates all those strings</li>
<li>replaces the first yasnippet marker with a yasnippet default marker.  </li>
</ul>

<p>I think I should be able to do something better instead of <code>(lambda (line) (format "%s" line))</code> but I don&#8217;t know what.</p>

<p>Definitely the most complex lisp I&#8217;ve written to date and I&#8217;ve learned
quite a lot from writing it.</p>

<p>As always, improvements and suggestions welcome; help yourself to any code.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.singletoned.net/2009/10/emacs-wrap-lines-in-html-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
