<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ruslan's Blog &#187; emacs</title>
	<atom:link href="http://ruslanspivak.com/category/emacs/feed/" rel="self" type="application/rss+xml" />
	<link>http://ruslanspivak.com</link>
	<description>Musings on dynamic languages and more than that</description>
	<lastBuildDate>Wed, 02 Jun 2010 19:16:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ruslanspivak.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/04a636b5f08c182c190ff6d8ae67239e?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ruslan's Blog &#187; emacs</title>
		<link>http://ruslanspivak.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ruslanspivak.com/osd.xml" title="Ruslan&#039;s Blog" />
	<atom:link rel='hub' href='http://ruslanspivak.com/?pushpress=hub'/>
		<item>
		<title>Downcase with replace-regexp</title>
		<link>http://ruslanspivak.com/2009/04/06/downcase-with-replace-regexp/</link>
		<comments>http://ruslanspivak.com/2009/04/06/downcase-with-replace-regexp/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 01:45:02 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=117</guid>
		<description><![CDATA[Recently I&#8217;ve been working with CSS files and wanted to replace color definitions with downcased equivalents, i.e. #FFFFFF; would become #ffffff; To do that I used pretty well known feature of Emacs, namely use of Lisp expressions in the replacement string of replace-regexp function. Commands entered in minibuffer: M-x replace-regexp RET \(#.*\)  RET \,(downcase \1) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=117&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been working with CSS files and wanted to replace color definitions with downcased equivalents, i.e. <strong>#FFFFFF;</strong> would become <strong>#ffffff;</strong></p>
<p>To do that I used pretty well known feature of Emacs, namely use of <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Replace.html">Lisp expressions</a> in the replacement string of <strong>replace-regexp</strong> function.</p>
<p>Commands entered in minibuffer:</p>
<p><strong>M-x replace-regexp RET \(#.*\)  RET \,(downcase \1) RET</strong></p>
<p>The only problem with that code is that if you have variable <strong>case-fold-search</strong> in Emacs set to <strong>true</strong> (which is the case in my Emacs by default) <strong>replace-regexp</strong> will report about successful replace but string won&#8217;t be downcased anyway. To quickly switch off <strong>case-fold-search</strong> I used <strong>M-: (setq case-fold-search nil)</strong> and after that downcasing worked as expected.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=117&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2009/04/06/downcase-with-replace-regexp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Refresh Firefox page from Emacs</title>
		<link>http://ruslanspivak.com/2009/03/16/refresh-firefox-page-from-emacs/</link>
		<comments>http://ruslanspivak.com/2009/03/16/refresh-firefox-page-from-emacs/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 18:26:04 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[grok]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=57</guid>
		<description><![CDATA[I often need to refresh page in Firefox when I edit page template or javascript file. This is especially true when working on grok-awesome. Though I use tiling window manager StumpWM and it&#8217;s very easy to switch to running Firefox with keyboard shortcut and make refresh, it&#8217;s even easier not to leave focus from Emacs [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=57&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I often need to refresh page in Firefox when I edit page template or javascript file. This is especially true when working on <a href="http://ruslanspivak.com/2009/03/12/grok-awesome/">grok-awesome</a>.</p>
<p>Though I use tiling window manager <a href="http://www.nongnu.org/stumpwm/">StumpWM</a> and it&#8217;s very easy to switch to running Firefox with keyboard shortcut and make refresh, it&#8217;s even easier not to leave focus from Emacs window and just send command to Firefox to refresh itself.<br />
For that I use:</p>
<ul>
<li> <a href="http://wiki.github.com/bard/mozrepl">MozRepl</a> firefox plugin to allow connect to Firefox&#8217;s REPL. After installation in Tools -&gt; MozRepl menu I chose &#8216;Activate on startup&#8217; for convenience</li>
<li> MozRepl Emacs <a href="http://wiki.github.com/bard/mozrepl/emacs-integration">integration</a> (If you use <a href="http://ourcomments.org/Emacs/nXhtml/doc/nxhtml.html">nXhtml</a> package in Emacs you already have that integration and can skip this part)</li>
</ul>
<p>Command to refresh Firefox is <strong>BrowserReload();</strong></p>
<p>Final touch is small anonymous function to send that command to mozrepl, keybinding to <strong>C-x p</strong> and we&#8217;re all set:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">(global-set-key (kbd <span style="color:#61CE3C;">"C-x p"</span>)
                (<span style="color:#FBDE2D;">lambda</span> ()
                  (interactive)
                  (comint-send-string (inferior-moz-process)
                                      <span style="color:#61CE3C;">"BrowserReload();"</span>)))</pre>
<p>Now anytime just press <strong>C-x p</strong> and refresh command will be sent to Firefox starting along the way mozrepl session in your Emacs if it&#8217;s not been already started.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=57&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2009/03/16/refresh-firefox-page-from-emacs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Emacs dark theme + IPython colors</title>
		<link>http://ruslanspivak.com/2009/03/09/emacs-dark-theme-ipython-colors/</link>
		<comments>http://ruslanspivak.com/2009/03/09/emacs-dark-theme-ipython-colors/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 07:15:09 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=39</guid>
		<description><![CDATA[Ever wondered how to make IPython colors be nice on dark background of your dark (no pun intended) Emacs theme like blackboard ? Just add to your .emacs (setq py-python-command-args '("-pylab" "-colors" "Linux")) before you load ipython package (setq py-python-command-args '("-pylab" "-colors" "Linux")) (require 'ipython) Now IPython uses &#8216;Linux&#8217; color scheme which is suitable for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=39&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how to make IPython colors be nice on dark background of your dark (no pun intended) Emacs theme like <a href="http://blog.jdhuntington.com/2008/11/emacs-color-theme-blackboard.html">blackboard </a>?</p>
<p>Just add to your .emacs</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;"><span style="background-color:#4f94cd;">(</span>setq py-python-command-args '(<span style="color:#61CE3C;">"-pylab"</span> <span style="color:#61CE3C;">"-colors"</span> <span style="color:#61CE3C;">"Linux"</span>)<span style="background-color:#4f94cd;">)</span></pre>
<p>before you load ipython package</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">(setq py-python-command-args '(<span style="color:#61CE3C;">"-pylab"</span> <span style="color:#61CE3C;">"-colors"</span> <span style="color:#61CE3C;">"Linux"</span>))
(<span style="color:#FBDE2D;">require</span> '<span style="color:#D8FA3C;">ipython</span>)</pre>
<p>Now IPython uses &#8216;Linux&#8217; color scheme which is suitable for dark background with light fonts instead of default &#8216;LightBG&#8217;.</p>
<p>Here it is:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">Python 2.5.2 (r252:60911, Sep 30 2008, 15:41:38)
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
?         -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help      -&gt; Python's own help system.
object?   -&gt; Details about 'object'. ?object also works, ?? prints more.

  Welcome to pylab, a matplotlib-based Python environment.
  For more information, type 'help(pylab)'.

<span style="color:#00ff00;background-color:#0C1021;">In [</span><span style="color:#00ff00;background-color:#0C1021;font-weight:bold;">1</span><span style="color:#00ff00;background-color:#0C1021;font-weight:bold;">]: </span><span style="font-weight:bold;">d = dict()</span>

<span style="color:#00ff00;background-color:#0C1021;">In [</span><span style="color:#00ff00;background-color:#0C1021;font-weight:bold;">2</span><span style="color:#00ff00;background-color:#0C1021;font-weight:bold;">]: </span><span style="font-weight:bold;">?d</span>
<span style="color:#ff0000;font-weight:bold;">Type:           </span>dict
<span style="color:#ff0000;font-weight:bold;">Base Class:     </span>&lt;type 'dict'&gt;
<span style="color:#ff0000;font-weight:bold;">String Form:</span>    {}
<span style="color:#ff0000;font-weight:bold;">Namespace:      </span>Interactive
<span style="color:#ff0000;font-weight:bold;">Length:         </span>0
<span style="color:#ff0000;font-weight:bold;">Docstring:
</span>    dict() -&gt; new empty dictionary.
    dict(mapping) -&gt; new dictionary initialized from a mapping object's
        (key, value) pairs.
    dict(seq) -&gt; new dictionary initialized as if via:
        d = {}
        for k, v in seq:
            d[k] = v
    dict(**kwargs) -&gt; new dictionary initialized with the name=value pairs
        in the keyword argument list.  For example:  dict(one=1, two=2)

<span style="color:#00ff00;background-color:#0C1021;">In [</span><span style="color:#00ff00;background-color:#0C1021;font-weight:bold;">3</span><span style="color:#00ff00;background-color:#0C1021;font-weight:bold;">]: </span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=39&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2009/03/09/emacs-dark-theme-ipython-colors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Delete blank lines with flush-lines</title>
		<link>http://ruslanspivak.com/2007/10/22/delete-blank-lines-with-flush-lines/</link>
		<comments>http://ruslanspivak.com/2007/10/22/delete-blank-lines-with-flush-lines/#comments</comments>
		<pubDate>Sun, 21 Oct 2007 23:12:07 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/10/22/delete-blank-lines-with-flush-lines/</guid>
		<description><![CDATA[Sometimes after I copy-paste code snippets from web page into emacs buffer I get text with superfluous blank lines delimiting code. Marking region and running M-x flush-lines RET ^$ RET quickly makes it better by removing those pesky blank lines. This is, of course, makes sense only if pasted code doesn&#8217;t contain blank lines you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=19&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes after I copy-paste code snippets from web page into  emacs buffer I get text with superfluous blank lines delimiting code.</p>
<p>Marking region and running <strong>M-x flush-lines RET ^$ RET</strong> quickly makes it better by removing those pesky blank lines. This is, of course, makes sense only if pasted code doesn&#8217;t contain blank lines you want to preserve.</p>
<p><em>Update</em>:<br />
You may need to use something more complex than simple <strong>^$</strong> (something like <strong>^\W*$</strong> as regexp) if &#8220;blank&#8221; line contains some whitespace characters.</p>
<p>Example before:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-export</span>([test1/0,

         test2/0,

         test3/0,

         double/1]).</pre>
<p>After marking region and applying <strong>M-x flush-lines RET ^\W*$ RET</strong>  (in your case just <strong>^$</strong> may be sufficient):</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-export</span>([test1/0,
         test2/0,
         test3/0,
         double/1]).</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=19&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/22/delete-blank-lines-with-flush-lines/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Incremental search forward</title>
		<link>http://ruslanspivak.com/2007/10/05/incremental-search-forward/</link>
		<comments>http://ruslanspivak.com/2007/10/05/incremental-search-forward/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 21:41:36 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/10/05/incremental-search-forward/</guid>
		<description><![CDATA[Well-known incremental search forward(isearch-forward Lisp function) which is invoked with C-s has also additional useful key-bindings which I use often to instruct what to search. After you pressed C-s in buffer they are: C-w to yank next word or character in buffer onto the end of the search string, and search for it. You can [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=18&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Well-known incremental search forward(<strong>isearch-forward</strong> Lisp function) which is invoked with <strong>C-s</strong> has also additional useful key-bindings which I use often to instruct what to search. After you pressed <strong>C-s</strong> in buffer they are:</p>
<ol>
<li><strong>C-w</strong> to yank next word or character in buffer onto the end of the search string, and search for it. You can repeatedly press <strong>C-w</strong> consuming next word or character.</li>
<li><strong>C-y</strong> to yank rest of line onto end of search string and search for it.</li>
<li><strong>M-n</strong>/<strong>M-p</strong> to search for the next/previous item in the search ring.</li>
</ol>
<p>As usual for more information read help (<strong>C-h k C-s</strong> or <strong>C-h f isearch-forward RET</strong> ).</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=18&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/05/incremental-search-forward/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>align-regexp</title>
		<link>http://ruslanspivak.com/2007/10/03/align-regexp/</link>
		<comments>http://ruslanspivak.com/2007/10/03/align-regexp/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 00:16:01 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/10/03/align-regexp/</guid>
		<description><![CDATA[This interactive Emacs Lisp function was very handy for me during some log files analysis. You mark region, invoke function, provide regexp and voila &#8211; your region is aligned. Example data from align-regexp help: Fred (123) 456-7890 Alice (123) 456-7890 Mary-Anne (123) 456-7890 Joe (123) 456-7890 Let&#8217;s say we want to align region on &#8220;(&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=17&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This interactive Emacs Lisp function was very handy for me during some log files analysis.</p>
<p>You mark region, invoke function, provide <em>regexp </em>and voila &#8211; your region is aligned.</p>
<p>Example data from <strong>align-regexp</strong> help:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
Fred (123) 456-7890
Alice (123) 456-7890
Mary-Anne (123) 456-7890
Joe (123) 456-7890</pre>
<p>Let&#8217;s say we want to align region on &#8220;<strong>(</strong>&#8221; character. For that we mark region we want to align and invoke function with: <strong>M-x align-regexp RET (  RET</strong><br />
Result of such invocation will be:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
Fred      (123) 456-7890
Alice     (123) 456-7890
Mary-Anne (123) 456-7890
Joe       (123) 456-7890</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=17&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/03/align-regexp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Erlang for Python programmers: Part IV</title>
		<link>http://ruslanspivak.com/2007/10/02/erlang-for-python-programmers-part-iv/</link>
		<comments>http://ruslanspivak.com/2007/10/02/erlang-for-python-programmers-part-iv/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 16:17:49 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/10/02/erlang-for-python-programmers-part-iv/</guid>
		<description><![CDATA[Previous parts: Intro, Part I, Part II and Part III Funs &#8211; sequel Continuing previous post about functions let&#8217;s remember about Fun. I already mentioned it in Part I and promised to show its advanced forms. So, here we go. Fun with one clause: 1&#62; Double = fun(X) -&#62; X * 2 end. #Fun&#60;erl_eval.6.56006484&#62; 2&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=16&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Previous parts: <a href="http://ruslanspivak.com/2007/08/27/erlang-for-python-programmers-intro/">Intro</a>, <a href="http://ruslanspivak.com/2007/09/09/erlang-for-python-programmers-part-i/">Part I</a>, <a href="http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/">Part II</a> and <a href="http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/">Part III</a></p>
<h3>Funs &#8211; sequel</h3>
<p>Continuing previous post about functions let&#8217;s remember about <strong>Fun</strong>. I already mentioned it in <a href="http://ruslanspivak.com/2007/09/09/erlang-for-python-programmers-part-i/">Part I</a> and promised to show its advanced forms. So, here we go.</p>
<p><strong>Fun</strong> with one <strong>clause</strong>:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">Double = fun(X) -&gt; X * 2 end.</span>
#Fun&lt;erl_eval.6.56006484&gt;
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">Double(2).</span>
4</pre>
<p>But <strong>Fun</strong> actually has the same <a href="http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/">function declaration syntax as regular function</a>, except that it has no name in declaration, so we can have this:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">Big = fun (X) when X &gt; 100 -&gt; true;</span>
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">          (X) -&gt; false</span>
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">      end.</span>
#Fun&lt;erl_eval.6.56006484&gt;
<span style="color:#00ffff;">4&gt; </span><span style="font-weight:bold;">lists:filter(Big, [20, 30, 120, 130]).</span>
[120,130]</pre>
<p>As you see our <strong>Fun</strong> has <strong>two clauses</strong> separated by <strong>semicolon(;)</strong> and first clause has also <strong>guard</strong>, ie syntax is the same as in regular function declaration.</p>
<p>There are also following fun expressions that can be used:</p>
<ol>
<li><strong>fun FunctionName/Arity</strong> (<em>FunctionName</em> should point to local function defined in the same module with our <strong>fun</strong>)</li>
<li><strong>fun Module:FunctionName/Arity</strong> (<em>FunctionName</em> should be exported from module <em>Module</em>)</li>
</ol>
<p><strong>fun FunctionName/Arity</strong> is just <em>syntactic sugar </em>for <strong>fun (X1, ..Xn) -&gt; FunctionName(X1, .., Xn) end.</strong></p>
<p>Let&#8217;s take a look on foregoing in action and create <em>mymath.erl</em></p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([test1/0,
         test2/0,
         test3/0,
         double/1]).

<span style="color:#87cefa;">test1</span>() -&gt;<span style="color:#87cefa;"> </span>lists:map(<span style="color:#00ffff;">fun</span>(<span style="color:#eedd82;">X</span>) -&gt;<span style="color:#87cefa;"> </span><span style="color:#eedd82;">X</span> * 2 <span style="color:#00ffff;">end</span>, [1, 2, 3]).

<span style="color:#87cefa;">test2</span>() -&gt;<span style="color:#87cefa;"> </span>lists:map(<span style="color:#00ffff;">fun</span> double_local/1, [1, 2, 3]).

<span style="color:#87cefa;">test3</span>() -&gt;<span style="color:#87cefa;"> </span>lists:map(<span style="color:#00ffff;">fun</span> mymath:double/1, [1, 2, 3]).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">helper function, not exported
</span><span style="color:#87cefa;">double_local</span>(<span style="color:#eedd82;">X</span>) -&gt;
    <span style="color:#eedd82;">X</span> * 2.

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">helper function, exported
</span><span style="color:#87cefa;">double</span>(<span style="color:#eedd82;">X</span>) -&gt;
    <span style="color:#eedd82;">X</span> * 2.</pre>
<p>Compile and test it (I&#8217;m compiling as usual in Emacs with <strong>C-c C-k</strong>):</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">c("/home/alienoid/dev/erlang/mymath", [{outdir, "/home/alienoid/dev/erlang/"}]).</span>
{ok,mymath}
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">mymath:test1().</span>
[2,4,6]
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">mymath:test2().</span>
[2,4,6]
<span style="color:#00ffff;">4&gt; </span><span style="font-weight:bold;">mymath:test3().</span>
[2,4,6]</pre>
<p>Also <strong>tuple of type {Module, FunctionName}</strong> is interpreted as a <strong>fun Module:FunctionName/Arity</strong> which you already saw. This usage is deprecated, but to be complete here is an example:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">5&gt; </span><span style="font-weight:bold;">Append = {lists, append}.</span>
{lists,append}
<span style="color:#00ffff;">6&gt; </span><span style="font-weight:bold;">Append([1, 2], [3, 4, 5]).</span>
[1,2,3,4,5]</pre>
<p>As you already noted <strong>Fun</strong> in Erlang is <em>&#8220;richer&#8221;</em> than <strong>lambda</strong> in Python.</p>
<h3> Built-in functions, BIFs</h3>
<p>Both Erlang and Python provide built-in functions that are always available and do not require explicit usage of module name to use them.</p>
<p><u>Python:</u></p>
<p>Read more about builtins in <a href="http://docs.python.org/lib/builtin.html">library reference</a>.</p>
<p>In Python shell you can inspect in addition contents of __builtins__ module if you need:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">import pprint</span>
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">pprint.pprint(dir(__builtins__))</span>
['ArithmeticError',
 'AssertionError',
 ...
 'type',
 'unichr',
 'unicode',
 'vars',
 'xrange',
 'zip']</pre>
<p><u>Erlang:</u><br />
Most of the built-in functions belong to module <em>erlang</em> and they are <strong>auto-imported</strong>.<br />
To get list of all BIFs with description read <em>man erlang</em>.<br />
In Eshell you can get list of functions that belong to <em>erlang</em> module with <strong>m(Module).</strong> command:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">m(erlang).</span>
Module erlang compiled: No compile time info available
Compiler options:  []
Object file: preloaded
<span style="color:#87cefa;">Exports</span>:
'!'/2                         list_to_existing_atom/1
'$erase'/1                    list_to_float/1
'$erase'/0                    list_to_integer/2
'$get'/1                      list_to_integer/1
'$get'/0                      list_to_pid/1
'$get_keys'/1                 list_to_tuple/1
'$put'/2                      load_module/2
'*'/2                         loaded/0
'+'/1                         localtime/0
'+'/2                         localtime_to_universaltime/1
'++'/2                        localtime_to_universaltime/2
...
ok</pre>
<p>Take into account though that not all functions in above list that you&#8217;ll see are auto-imported and may require usage of <em>module:</em> prefix. Again, for more information read <em>man erlang</em> (either in command line with <strong>erl -man erlang</strong> or if you use Emacs take a look at <a href="http://ruslanspivak.com/2007/08/19/erlang-man-pages-in-emacs/">Erlang man pages in Emacs</a>)</p>
<h3>Macros</h3>
<p>Python does not have them, Erlang does. In Erlang they are not as powerful as in <a href="http://www.gigamonkeys.com/book/macros-defining-your-own.html">Common Lisp</a>, but more like in C.</p>
<p>They are defined in following form:</p>
<pre><strong>-define(Const, Replacement).
-define(Func(Var1,...,VarN), Replacement).</strong></pre>
<p>Whenever erlang preprocessor encounters expression of form <strong>?MacroName</strong> it expands corresponding macro.<br />
Define <em>utils.erl</em>:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(utils).
<span style="color:#87cefa;">-export</span>([foo/0]).
<span style="color:#87cefa;">-define</span>(<span style="color:#7fffd4;">TIMEOUT</span>, 100).
<span style="color:#87cefa;">-define</span>(<span style="color:#7fffd4;">FUNCMACRO</span>(<span style="color:#eedd82;">X</span>, <span style="color:#eedd82;">Y</span>), {<span style="color:#eedd82;">X</span>, x, <span style="color:#eedd82;">Y</span>, y}).

<span style="color:#87cefa;">foo</span>() -&gt;
    io:format(<span style="color:#ffa07a;">"TIMEOUT = ~p~n"</span>, [?<span style="color:#7fffd4;">TIMEOUT</span>]),
    ?<span style="color:#7fffd4;">FUNCMACRO</span>(3, 7).</pre>
<p>Compile and run:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">utils:foo().</span>
TIMEOUT = 100
{3,x,7,y}</pre>
<p>There are also some predefined macros:</p>
<dl>
<dt> <strong><code>?MODULE</code></strong> </dt>
<dd> The name of the current module. </dd>
<dt> <strong><code>?MODULE_STRING</code></strong>. </dt>
<dd> The name of the current module as a string. </dd>
<dt> <strong><code>?FILE</code></strong>. </dt>
<dd> The file name of the current module. </dd>
<dt> <strong><code>?LINE</code></strong>. </dt>
<dd> The current line number. </dd>
<dt> <strong><code>?MACHINE</code></strong>. </dt>
<dd> The machine name, <code>'BEAM'</code>.</dd>
</dl>
<p>Having <strong>?MODULE</strong> we can rewrite earlier example with <strong>Fun</strong> as:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([test1/0,
         test2/0,
         test3/0,
         double/1]).

<span style="color:#87cefa;">test1</span>() -&gt;<span style="color:#87cefa;"> </span>lists:map(<span style="color:#00ffff;">fun</span>(<span style="color:#eedd82;">X</span>) -&gt;<span style="color:#87cefa;"> </span><span style="color:#eedd82;">X</span> * 2 <span style="color:#00ffff;">end</span>, [1, 2, 3]).

<span style="color:#87cefa;">test2</span>() -&gt;<span style="color:#87cefa;"> </span>lists:map(<span style="color:#00ffff;">fun</span> double_local/1, [1, 2, 3]).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">test3() -&gt; lists:map(fun mymath:double/1, [1, 2, 3]).
</span><span style="color:#87cefa;">test3</span>() -&gt;<span style="color:#87cefa;"> </span>lists:map(<span style="color:#00ffff;">fun</span> ?<span style="color:#7fffd4;">MODULE</span>:double/1, [1, 2, 3]).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">helper function, not exported
</span><span style="color:#87cefa;">double_local</span>(<span style="color:#eedd82;">X</span>) -&gt;
    <span style="color:#eedd82;">X</span> * 2.

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">helper function, exported
</span><span style="color:#87cefa;">double</span>(<span style="color:#eedd82;">X</span>) -&gt;
    <span style="color:#eedd82;">X</span> * 2.</pre>
<p>And as usual compile and run to see that result is the same:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">c("/home/alienoid/dev/erlang/mymath", [{outdir, "/home/alienoid/dev/erlang/"}]).</span>
{ok,mymath}
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;"> mymath:test1().</span>
[2,4,6]
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;"> mymath:test2().</span>
[2,4,6]
<span style="color:#00ffff;">4&gt; </span><span style="font-weight:bold;"> mymath:test3().</span>
[2,4,6]</pre>
<p>In addition there is also <strong>flow control</strong> in macros. Some of corresponding macro directives are:</p>
<ul>
<li><strong>ifdef(Macro).</strong> &#8211; Evaluate the following lines only if <em>Macro</em> is defined.</li>
<li><strong>else.</strong> &#8211; It&#8217;s used after <em>ifdef</em> or <em>ifndef</em></li>
<li><strong>endif. </strong>- Marks the end of <em>ifdef</em> or <em>ifndef</em> directive</li>
</ul>
<p>Example in <em>utils.erl</em>:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(utils).
<span style="color:#87cefa;">-export</span>([foo/0]).

<span style="color:#87cefa;">-ifdef</span>(<span style="color:#7fffd4;">debug</span>).
<span style="color:#87cefa;">-define</span>(<span style="color:#7fffd4;">LOG</span>(<span style="color:#eedd82;">Msg</span>), io:format(<span style="color:#ffa07a;">"{~p:~p}: ~p~n"</span>, [?<span style="color:#7fffd4;">MODULE</span>, ?<span style="color:#7fffd4;">LINE</span>, <span style="color:#eedd82;">Msg</span>])).
<span style="color:#87cefa;">-else</span>.
<span style="color:#87cefa;">-define</span>(<span style="color:#7fffd4;">LOG</span>(<span style="color:#eedd82;">Msg</span>), true).
<span style="color:#87cefa;">-endif</span>.

<span style="color:#87cefa;">foo</span>() -&gt;
    ?<span style="color:#7fffd4;">LOG</span>(<span style="color:#ffa07a;">"Debug is enabled"</span>).</pre>
<p>To turn LOG macro on <em>debug</em> should be defined. This can be achieved from command line with <strong>erlc -Ddebug utils.erl</strong> or from Eshell using <strong>c/2</strong> function:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">c(utils, {d, debug}).</span>
{ok,utils}
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">utils:foo().</span>
{utils:11}: "Debug is enabled"
ok</pre>
<p>To turn LOG macro off <em>debug</em> should be omitted:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">c(utils).</span>
{ok,utils}
<span style="color:#00ffff;">4&gt; </span><span style="font-weight:bold;">utils:foo().</span>
true</pre>
<p>To be continued.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=16&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/02/erlang-for-python-programmers-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating tables in Emacs</title>
		<link>http://ruslanspivak.com/2007/10/01/creating-tables-in-emacs/</link>
		<comments>http://ruslanspivak.com/2007/10/01/creating-tables-in-emacs/#comments</comments>
		<pubDate>Mon, 01 Oct 2007 13:13:18 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/10/01/creating-tables-in-emacs/</guid>
		<description><![CDATA[Sometimes I need to create tables in Emacs as it was in post describing comparison and arithmetic operations in Python and Erlang. Over time I became aware of several means to create tables and export them in different formats in Emacs(at the moment I&#8217;m personally interested in plain ASCII and HTML): Excellent org-mode has built-in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=15&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes I need to create tables in Emacs as it was in <a href="http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/">post</a> describing comparison and arithmetic operations in Python and Erlang.</p>
<p>Over time I became aware of several means to create tables and export them in different formats in Emacs(at the moment I&#8217;m personally interested in plain ASCII and HTML):</p>
<ol>
<li>Excellent <a href="http://orgmode.org/">org-mode</a> has built-in <a href="http://orgmode.org/org.html#Tables">table editor</a>  which allows easily format tables in plain ASCII</li>
<li><a href="http://table.sourceforge.net/">table.el</a> with its advanced ability to construct table layout</li>
</ol>
<p>Both modes (technically though Table mode in <em>table.el</em> is not mode) are part of modern Emacs and both allow export table in HTML format.</p>
<p>To quickly create tables personally I prefer <em>table.el</em> which provides also following features:</p>
<ol>
<li>A cell can be split horizontally and vertically.</li>
<li>A cell can span into an adjacent cell.</li>
</ol>
<p>Use <strong>C-h b </strong>in table to get self-descriptive information about table.el key bindings. In contrast to <em>org-mode</em>(or its <em>orgtbl-mode</em> minor mode) <em>table.el</em> has no special bindings to copy row/cell and yank it. But this is easily achieved by using usual region commands for rows and rectangular commands for cells.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=15&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/01/creating-tables-in-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>Erlang for Python programmers: Part III</title>
		<link>http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/</link>
		<comments>http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 23:53:50 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/</guid>
		<description><![CDATA[Previous parts: Intro, Part I and Part II Today we are going to take a look at functions. In Erlang there is no direct correspondence to what we have in Python: 1. default values, keyword arguments and formal parameter of form **name def foo(key, name='ruslan', type='unknown', **kw): print 'key = %s, name = %s, type [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=14&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Previous parts: <a href="http://ruslanspivak.com/2007/08/27/erlang-for-python-programmers-intro/">Intro</a>, <a href="http://ruslanspivak.com/2007/09/09/erlang-for-python-programmers-part-i/">Part I</a> and <a href="http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/">Part II</a></p>
<p>Today we are going to take a look at functions.<br />
In Erlang there is no direct correspondence to what we have in Python:</p>
<p>1. default values, keyword arguments and formal parameter of form <strong>**name</strong></p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">def</span> <span style="color:#87cefa;">foo</span>(key, name=<span style="color:#ffa07a;">'ruslan'</span>, type=<span style="color:#ffa07a;">'unknown'</span>, **kw):
    <span style="color:#00ffff;">print</span> <span style="color:#ffa07a;">'key = %s, name = %s, type = %s'</span> % (key, name, type)
    <span style="color:#00ffff;">for</span> key, val <span style="color:#00ffff;">in</span> kw.items():
        <span style="color:#00ffff;">print</span> <span style="color:#ffa07a;">'%s = %s'</span> % (key, val)</pre>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">foo(1)</span>
key = 1, name = ruslan, type = unknown
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">foo(1, type='known', x=1, y=10)</span>
key = 1, name = ruslan, type = known
y = 10
x = 1</pre>
<p>2. formal parameter of form <strong>*name</strong></p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">def</span> <span style="color:#87cefa;">double_sum</span>(*args):
    <span style="color:#00ffff;">return</span> sum(x * 2 <span style="color:#00ffff;">for</span> x <span style="color:#00ffff;">in</span> args)</pre>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">double_sum(2, 4, 5)</span>
22</pre>
<p>Let&#8217;s take a look at Erlang&#8217;s <u><em>function declaration syntax</em> (</u>a bit modified from Erlang Reference Manual):</p>
<ol>
<li><strong>Function declaration</strong> consists of function <strong>clauses</strong> separated by <strong>semicolons (;) </strong>and terminated by <strong>period (.)</strong></li>
<li><strong>Function clause</strong> consists of <strong>head</strong> and <strong>body</strong> separated by <strong>-&gt;</strong></li>
<li>A <strong>clause head</strong> consists of <em>function name</em> (which is atom), <em>argument list</em>(each argument is <strong>pattern</strong>) and optional <strong>guard sequence</strong> beginning with keyword <strong>when</strong>.</li>
<li>A <strong>clause body</strong> consists of sequence of expressions separated by <strong>comma (,)</strong></li>
</ol>
<h4>Name(Pattern11,&#8230;,Pattern1N) [when GuardSeq1] -&gt;<br />
Body1;<br />
&#8230;;<br />
Name(PatternK1,&#8230;,PatternKN) [when GuardSeqK] -&gt;<br />
BodyK.</h4>
<h4></h4>
<h4>where Body is in form:<br />
Expr1,<br />
Expr2,<br />
&#8230;<br />
ExprN</h4>
<p>Number of arguments in function is called <strong>arity</strong>. Function is uniquely identified by combination of <u><em>module name</em></u>, <u><em>function name</em></u> and <u><em>function&#8217;s arity</em></u> and this form is often written as <strong>m:f/N</strong>. Two functions in the module with the same name, but different arity<br />
are completely different functions, remember it.</p>
<p>Enough dry theory, let&#8217;s define our Erlang <em>factorial</em> function in module <em>mymath.erl</em> :</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([factorial/1]).

<span style="color:#87cefa;">factorial</span>(0) -&gt;
    1;
<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) -&gt;
    <span style="color:#eedd82;">N</span> * factorial(<span style="color:#eedd82;">N</span>-1).</pre>
<p>What we have here:</p>
<p>1) <strong>function declaration</strong> of one function called  <em>factorial</em><br />
2) there are two <strong>clauses</strong><br />
- first clause separated by <strong>semicolon (;)</strong></p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">factorial</span>(0) -&gt;
    1;</pre>
<p>- second and final clause terminated with <strong>period (.)</strong></p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) -&gt;
    <span style="color:#eedd82;">N</span> * factorial(<span style="color:#eedd82;">N</span>-1).</pre>
<p>3) Every clause (first and second) has correspondingly head and body. Head consists of function&#8217;s name, in our case it&#8217;s <u>factorial</u> and after follows arguments list which contains patterns. In first clause pattern is <strong>0</strong>, in second &#8211; <strong>N</strong>.</p>
<p>How this works:</p>
<p>When function is entered, the function clauses are pattern matched against passed arguments. Pattern matching happens in order functions are defined in module. So when you enter</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">mymath:factorial(5).</span>
120</pre>
<p>first <strong>5</strong> is matched against clause <strong>factorial(0)</strong> and fails as <strong>5 != 0</strong>, then <strong>5</strong> is matched against <strong>N</strong> in second clause and pattern matching succeeds. As a result unbound variable <strong>N</strong> in pattern becomes bound and gets value <strong>5</strong>. This leads to execution of body of second clause, namely<strong> N * factorial(N-1)</strong>. This happens recursively until <strong>N</strong> becomes <strong>0</strong> and first clause<strong> factorial(0)</strong> is executed terminating recursion.<br />
The same <em>factorial</em> function will look in Python without pattern matching like:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">def</span> <span style="color:#87cefa;">factorial</span>(n):
    <span style="color:#00ffff;">if</span> n == 0:
        <span style="color:#00ffff;">return</span> 1
    <span style="color:#00ffff;">return</span> n * factorial(n-1)</pre>
<p>In Python&#8217;s version we &#8220;hardcoded&#8221; branching in function&#8217;s body.</p>
<p>The benefits of pattern matching are that when conditions inside function become complex you may do your job better and write less code using pattern matching and also if you have big function this may mean that you do not have to modify body of your function but to add another clause with corresponding pattern.</p>
<p>Earlier I mentioned that function clauses are pattern matched in order they are defined in module. For our function this is important. If we change order of clauses this will lead to problems, as the condition to terminate recursion never appear:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([factorial/1]).

<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) -&gt;
    <span style="color:#eedd82;">N</span> * factorial(<span style="color:#eedd82;">N</span>-1);
<span style="color:#87cefa;">factorial</span>(0) -&gt;
    1.</pre>
<p>When compiling we will get warning (use <strong>C-c C-k</strong> in Emacs or <strong>c(mymath).</strong> in Eshell to compile)</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">c("/home/alienoid/dev/erlang/mymath", [{outdir, "/home/alienoid/dev/erlang/"}]).</span>
<span style="color:#ffc0cb;font-weight:bold;text-decoration:underline;">/home/alienoid/dev/erlang/mymath.erl</span><span style="text-decoration:underline;">:</span><span style="color:#eedd82;text-decoration:underline;">6</span><span style="text-decoration:underline;">:</span><span style="text-decoration:underline;"> Warning</span>: this clause cannot match because a previous clause at line 4 always matches
{ok,mymath}</pre>
<p>indicating that second clause will never match, but still you can call the function (it will most likely to crash after eating all your available memory).</p>
<p>You can easily avoid that problem introducing <strong>guard</strong> that starts with keyword <strong>when</strong> in function clause&#8217;s head:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([factorial/1]).

<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) <span style="color:#00ffff;">when</span> <span style="color:#eedd82;">N</span> &gt; 0 -&gt;
    <span style="color:#eedd82;">N</span> * factorial(<span style="color:#eedd82;">N</span>-1);
<span style="color:#87cefa;">factorial</span>(0) -&gt;
    1.</pre>
<p>Now you can put those two clauses in any order you like. I should also add that if during pattern matching no matched function clause is found a <em>function_clause</em> run time error will occur:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">mymath:factorial(-4).</span>

=ERROR REPORT==== 24-Sep-2007::01:53:25 ===
Error in process &lt;0.29.0&gt; with exit value: {function_clause,[{mymath,factorial,[-4]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {function_clause,[{mymath,factorial,[-4]},
                             {erl_eval,do_apply,5},
                             {shell,exprs,6},
                             {shell,eval_loop,3}]} **</pre>
<p>What is <strong>guard</strong> anyway ? Guards make pattern matching more powerful, they can be used to perform different tests and comparison operations on variables in a pattern as you could see.  You can have several <em>guard expressions</em> in clause separated by <strong>commans (,). </strong>The guard<strong> </strong>containing several guard expressions evaluates to <strong>true</strong> only if every guard expression evaluates to true. Example of guard containing several guard expressions:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([factorial/1]).

<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) <span style="color:#00ffff;">when</span> <span style="color:#7fffd4;">is_integer</span>(<span style="color:#eedd82;">N</span>), <span style="color:#eedd82;">N</span> &gt; 0 -&gt;
    <span style="color:#eedd82;">N</span> * factorial(<span style="color:#eedd82;">N</span>-1);
<span style="color:#87cefa;">factorial</span>(0) -&gt;
    1.</pre>
<p>For above definition calling <u>mymath:factorial(5)</u> reads as: <strong>choose clause factorial(N) when (N is integer AND N &gt; 0)</strong></p>
<p>There are also <strong>guard sequences</strong> which have form <strong>guard1; guard2; &#8230;; guardN</strong> and guard sequence evaluates to true if at least one guard is evaluated to true:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([factorial/1]).

<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) <span style="color:#00ffff;">when</span> <span style="color:#7fffd4;">is_integer</span>(<span style="color:#eedd82;">N</span>), <span style="color:#eedd82;">N</span> &gt; 0; <span style="color:#7fffd4;">is_list</span>(<span style="color:#eedd82;">N</span>) -&gt;
    <span style="color:#eedd82;">N</span> * factorial(<span style="color:#eedd82;">N</span>-1);
<span style="color:#87cefa;">factorial</span>(0) -&gt;
    1.</pre>
<p>For above definition calling <u>mymath:factorial(5)</u> reads as: <strong>choose clause factorial(N) when (N is integer AND N &gt; 0) OR N is list</strong>.</p>
<p>You have probably noted that the way we defined our factorial function means that it will be calling itself recursively and this will use stack and stack is not infinite resource. We need to do something about our function and this means to make it <strong>tail recursive</strong>. When last expression in function body is function call this means that call is <strong>tail recursive</strong> and this is how infinite loop may be done without consuming stack resources. So here is our new <strong>tail-recursive</strong> function <u><em>factorial</em></u> that uses accumulator to pass value of previous calculation:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([factorial/1]).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">this one is exported, arity is 1
</span><span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>) -&gt;
    factorial(<span style="color:#eedd82;">N</span>, 1).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">helper function, not exported, arity is 2
</span><span style="color:#ff4500;">%% </span><span style="color:#ff4500;">tail-recursive, uses accumulator to store
</span><span style="color:#ff4500;">%% </span><span style="color:#ff4500;">calculation between invocations
</span><span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">N</span>, <span style="color:#eedd82;">Acc</span>) <span style="color:#00ffff;">when</span> <span style="color:#eedd82;">N</span> &gt; 0 -&gt;
    factorial(<span style="color:#eedd82;">N</span>-1, <span style="color:#eedd82;">N</span>*<span style="color:#eedd82;">Acc</span>);
<span style="color:#87cefa;">factorial</span>(0, <span style="color:#eedd82;">Acc</span>) -&gt;
    <span style="color:#eedd82;">Acc</span>.</pre>
<p>In foregoing example you can see guards, tail-recursion, usage of accumulators, functions with different arities. Experiment with it.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=14&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
		<item>
		<title>comment-dwim and comment-style</title>
		<link>http://ruslanspivak.com/2007/09/20/comment-dwim-and-comment-style/</link>
		<comments>http://ruslanspivak.com/2007/09/20/comment-dwim-and-comment-style/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 21:24:11 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/09/20/comment-dwim-and-comment-style/</guid>
		<description><![CDATA[In my previous post I mentioned that if you use Emacs and you&#8217;re editing Erlang source code you can easily comment whole marked region by invoking M-; to which by default bound interactive Lisp function comment-dwim. This works in different modes and it&#8217;s very handy especially if your language of choice has no multiline comments. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=13&subd=ruslanspivak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/">previous post</a> I mentioned that if you use Emacs and you&#8217;re editing Erlang source code you can easily comment whole marked region by invoking <strong>M-;</strong> to which by default bound interactive Lisp function  <strong>comment-dwim</strong>. This works in different modes and it&#8217;s very handy especially if your language of choice has no multiline comments.</p>
<p>So far so good, but I must admit sometimes I get the blues when I comment some nested code blocks with <strong>M-;</strong> keystrokes. Well, maybe I&#8217;m blowing things out of proportion about my sadness, but in any case recently I came across really nice small blog post called <a href="http://edward.oconnor.cx/2007/09/comment-style">comment-style</a> and since then I&#8217;m happy with comments produced in nested blocks. Let&#8217;s see why.</p>
<p>When you comment marked regions in Emacs buffer with <strong>M-;</strong> the <strong>comment-style</strong> variable defines the way regions are commented.  Default value is <strong>plain</strong>.</p>
<p>Buffer with Python code before commenting:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">def</span> <span style="color:#87cefa;">binsearch</span>(seq, key, start, end):
    <span style="color:#00ffff;">if</span> end &lt; start:
        <span style="color:#00ffff;">return</span> -1
    mid = (start + end) / 2
    <span style="color:#00ffff;">if</span> key == seq[mid]:
        <span style="color:#00ffff;">return</span> mid
    <span style="color:#00ffff;">if</span> key &lt; seq[mid]:
        <span style="color:#00ffff;">return</span> binsearch(seq, key, start, mid-1)
    <span style="color:#00ffff;">if</span> key &gt; seq[mid]:
        <span style="color:#00ffff;">return</span> binsearch(seq, key, start+1, end)</pre>
<p>Buffer with Python code after commenting:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">def</span> <span style="color:#87cefa;">binsearch</span>(seq, key, start, end):
    <span style="color:#00ffff;">if</span> end &lt; start:
        <span style="color:#00ffff;">return</span> -1
    mid = (start + end) / 2
    <span style="color:#ff4500;"># if key == seq[mid]:
</span><span style="color:#ff4500;">#         </span><span style="color:#ff4500;">return mid
</span>    <span style="color:#00ffff;">if</span> key &lt; seq[mid]:
        <span style="color:#00ffff;">return</span> binsearch(seq, key, start, mid-1)
    <span style="color:#00ffff;">if</span> key &gt; seq[mid]:
        <span style="color:#00ffff;">return</span> binsearch(seq, key, start+1, end)</pre>
<p>As you see visually elements of commented block are not on the same line.</p>
<p>Example with Erlang code before commenting (somewhat far-fetched example though our main concern are comments, not code here):</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">all</span>(<span style="color:#eedd82;">Pred</span>, [<span style="color:#eedd82;">Hd</span>|<span style="color:#eedd82;">Tail</span>]) -&gt;
    <span style="color:#00ffff;">case</span> <span style="color:#eedd82;">Pred</span>(<span style="color:#eedd82;">Hd</span>) <span style="color:#00ffff;">of</span>
        true -&gt;<span style="color:#87cefa;"> </span>all(<span style="color:#eedd82;">Pred</span>, <span style="color:#eedd82;">Tail</span>);
        false -&gt;
            io:format(<span style="color:#ffa07a;">"Hd = ~p~n"</span>, [<span style="color:#eedd82;">Hd</span>]),
            true,
            false
    <span style="color:#00ffff;">end</span>;
<span style="color:#87cefa;">all</span>(<span style="color:#eedd82;">Pred</span>, []) <span style="color:#00ffff;">when</span> <span style="color:#7fffd4;">is_function</span>(<span style="color:#eedd82;">Pred</span>, 1) -&gt;<span style="color:#87cefa;"> </span>true.</pre>
<p>After commenting:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">all</span>(<span style="color:#eedd82;">Pred</span>, [<span style="color:#eedd82;">Hd</span>|<span style="color:#eedd82;">Tail</span>]) -&gt;
    <span style="color:#00ffff;">case</span> <span style="color:#eedd82;">Pred</span>(<span style="color:#eedd82;">Hd</span>) <span style="color:#00ffff;">of</span>
        true -&gt;<span style="color:#87cefa;"> </span>all(<span style="color:#eedd82;">Pred</span>, <span style="color:#eedd82;">Tail</span>);
        false -&gt;
            <span style="color:#ff4500;">%% </span><span style="color:#ff4500;">io:format("Hd = ~p~n", [Hd]),
</span><span style="color:#ff4500;">%%             </span><span style="color:#ff4500;">true,
</span>            false
    <span style="color:#00ffff;">end</span>;
<span style="color:#87cefa;">all</span>(<span style="color:#eedd82;">Pred</span>, []) <span style="color:#00ffff;">when</span> <span style="color:#7fffd4;">is_function</span>(<span style="color:#eedd82;">Pred</span>, 1) -&gt;<span style="color:#87cefa;"> </span>true.</pre>
<p>So the visual issue with comments is the same.</p>
<p>After reading foregoing blog post about comment-style I&#8217;ve added this line to my <em>.emacs</em>:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
(setq comment-style 'indent)</pre>
<p>Easy as pie and which means (taken from Emacs help): &#8221; &#8230;`<em>comment-start&#8217; markers should not be put at the left margin but at the current indentation of the region to comment</em>.&#8221;</p>
<p>After that addition foregoing commented block in Python mode will look like:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">def</span> <span style="color:#87cefa;">binsearch</span>(seq, key, start, end):
    <span style="color:#00ffff;">if</span> end &lt; start:
        <span style="color:#00ffff;">return</span> -1
    mid = (start + end) / 2
    <span style="color:#ff4500;"># if key == seq[mid]:
</span>    <span style="color:#ff4500;">#     return mid
</span>    <span style="color:#00ffff;">if</span> key &lt; seq[mid]:
        <span style="color:#00ffff;">return</span> binsearch(seq, key, start, mid-1)
    <span style="color:#00ffff;">if</span> key &gt; seq[mid]:
        <span style="color:#00ffff;">return</span> binsearch(seq, key, start+1, end)</pre>
<p>in Erlang mode:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">all</span>(<span style="color:#eedd82;">Pred</span>, [<span style="color:#eedd82;">Hd</span>|<span style="color:#eedd82;">Tail</span>]) -&gt;
    <span style="color:#00ffff;">case</span> <span style="color:#eedd82;">Pred</span>(<span style="color:#eedd82;">Hd</span>) <span style="color:#00ffff;">of</span>
        true -&gt;<span style="color:#87cefa;"> </span>all(<span style="color:#eedd82;">Pred</span>, <span style="color:#eedd82;">Tail</span>);
        false -&gt;
            <span style="color:#ff4500;">%% </span><span style="color:#ff4500;">io:format("Hd = ~p~n", [Hd]),
</span>            <span style="color:#ff4500;">%% </span><span style="color:#ff4500;">true,
</span>            false
    <span style="color:#00ffff;">end</span>;
<span style="color:#87cefa;">all</span>(<span style="color:#eedd82;">Pred</span>, []) <span style="color:#00ffff;">when</span> <span style="color:#7fffd4;">is_function</span>(<span style="color:#eedd82;">Pred</span>, 1) -&gt;<span style="color:#87cefa;"> </span>true.</pre>
<p>Now I am happy. Thank you, <a href="http://edward.oconnor.cx/2007/09/comment-style">Edward</a> for the tip.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ruslanspivak.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ruslanspivak.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruslanspivak.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruslanspivak.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ruslanspivak.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ruslanspivak.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ruslanspivak.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ruslanspivak.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ruslanspivak.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ruslanspivak.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ruslanspivak.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ruslanspivak.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruslanspivak.com&blog=1522719&post=13&subd=ruslanspivak&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/09/20/comment-dwim-and-comment-style/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4d73d92d1e35b86c5c45667b7a036da3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alienoid</media:title>
		</media:content>
	</item>
	</channel>
</rss>