<?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>Ruslan&#039;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>The harder you work the luckier you get</description>
	<lastBuildDate>Fri, 29 Jun 2012 04:41:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>httpcode.el – explain HTTP status code in minibuffer</title>
		<link>http://ruslanspivak.com/2011/12/24/httpcode-el-explain-http-status-code-in-minibuffer/</link>
		<comments>http://ruslanspivak.com/2011/12/24/httpcode-el-explain-http-status-code-in-minibuffer/#comments</comments>
		<pubDate>Sun, 25 Dec 2011 04:49:56 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=1308</guid>
		<description><![CDATA[In addition to command line version here comes Emacs package to explain HTTP status codes in minibuffer httpcode.el]]></description>
			<content:encoded><![CDATA[<p></p><p>In addition to <a href="http://ruslanspivak.com/2011/12/21/httpcode-explain-http-status-code-on-the-command-line/">command line version</a> here comes Emacs package to explain HTTP status codes in minibuffer <a href="http://github.com/rspivak/httpcode.el">httpcode.el</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2011/12/24/httpcode-el-explain-http-status-code-in-minibuffer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to indent a block of text in Emacs</title>
		<link>http://ruslanspivak.com/2011/04/28/how-to-indent-a-block-of-text-in-emacs/</link>
		<comments>http://ruslanspivak.com/2011/04/28/how-to-indent-a-block-of-text-in-emacs/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 22:05:18 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=911</guid>
		<description><![CDATA[Sometimes I need to indent a block of text in Emacs regardless of current major mode. A key binding that fits the bill is C-x TAB which runs the command indent-rigidly. If used with prefix argument C-u it will indent by specified number of spaces, so pressing C-u 8 C-x TAB after marking a block [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Sometimes I need to indent a block of text in Emacs regardless of current major mode. A key binding that fits the bill is <em><strong>C-x TAB</strong></em> which runs the command <em><strong>indent-rigidly</strong></em>.</p>
<p>If used with prefix argument <em><strong>C-u</strong></em> it will indent by specified number of spaces, so pressing <em><strong>C-u 8 C-x TAB</strong></em> after marking a block of text will indent that block by <em>8</em> spaces and <em><strong>C-u -8 C-x TAB</strong></em> will remove <em>8</em> spaces of indentation.<em><strong></strong></em><strong><em> </em></strong>There is a nice shortcut: <em><strong>C-u C-x TAB</strong></em> will indent a block of text by <em>4</em> spaces.</p>
<p>When in <a href="https://code.launchpad.net/python-mode">Python mode</a> marking a region and pressing <em><strong>C-c &gt;</strong></em> to shift lines in region to the right and <em><strong>C-c &lt;</strong></em> to shift lines in region to the left works perfectly well too.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2011/04/28/how-to-indent-a-block-of-text-in-emacs/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Multiline regexp</title>
		<link>http://ruslanspivak.com/2010/12/18/multiline-regexp/</link>
		<comments>http://ruslanspivak.com/2010/12/18/multiline-regexp/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 01:01:40 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=679</guid>
		<description><![CDATA[One late evening my colleague and I were sitting in front of my laptop working on some Python code in Emacs. We needed to quickly make some replacements all over the file. The original code looked something like this and we wanted to remove json.dumps call around dictionaries: def foo(): return json.dumps({ 'key': 6, 'q': [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>One late evening my colleague and I were sitting in front of my laptop working on some Python code in Emacs. We needed to quickly make some replacements all over the file. The original code looked something like this and we wanted to remove <em><strong>json.dumps</strong></em> call around dictionaries:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;"><span style="color:#fbde2d;">def</span> <span style="color:#ff6400;">foo</span>():
    <span style="color:#fbde2d;">return</span> json.dumps({
        <span style="color:#61ce3c;">'key'</span>: 6,
        <span style="color:#61ce3c;">'q'</span>: 7,
        })

<span style="color:#fbde2d;">def</span> <span style="color:#ff6400;">bar</span>():
    <span style="color:#fbde2d;">return</span> json.dumps({
        <span style="color:#61ce3c;">'key'</span>: 8,
        <span style="color:#61ce3c;">'q'</span>: 9,
        })</pre>
<p>We wanted to make it look like this:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;"><span style="color:#fbde2d;">def</span> <span style="color:#ff6400;">foo</span>():
    <span style="color:#fbde2d;">return</span> {
        <span style="color:#61ce3c;">'key'</span>: 6,
        <span style="color:#61ce3c;">'q'</span>: 7,
        }

<span style="color:#fbde2d;">def</span> <span style="color:#ff6400;">bar</span>():
    <span style="color:#fbde2d;">return</span> {
        <span style="color:#61ce3c;">'key'</span>: 8,
        <span style="color:#61ce3c;">'q'</span>: 9,
        }</pre>
<p>As you can see a replace regular expression has to deal with code that spans multiple lines and writing Emacs multiline regexp might not be straightforward if you hadn&#8217;t written one before.<br />
I&#8217;ll tell you right away that we couldn&#8217;t figure out quickly how to write that regular expression and had to use &#8220;brute force&#8221;, but next day I found a solution that works and which I can use in the future when I need to deal with multiline regexp.</p>
<p>Here is what I typed in Emacs:<br />
<em>M-x query-replace-regexp</em><br />
<em> Query replace regexp: json.dumps((.*(?:^J.*?)*))</em><br />
<em> Query replace regexp json.dumps((.*(?:^J.*?)*)) with: 1</em></p>
<p>Let&#8217;s take a closer look at the regexp:</p>
<ol>
<li>First <strong><em>.*</em></strong> is reponsible for matching  all characters up to a first newline</li>
<li><em><strong>(?:^J.*?)*</strong></em> &#8211; the most important part. It&#8217;s a shy group that takes care of matching a newline and everything after the newline up to a next newline.  The shy group is faster and doesn&#8217;t introduce new group number so we can use<strong><em> 1</em></strong> to reference an outer group later. The search inside the group is non greedy due to <strong><em>?</em></strong> character and allows us to properly handle contents inside multiple<em> json.dumps</em> expressions in the same file. Also note that<em><strong> ^J</strong></em> is not a real character that you need to type &#8211; in Emacs to match a newline character when carrying out interactive search/replace you have to enter <strong><em>C-q C-j</em></strong> which will be displayed as <em><strong>^J</strong></em>.</li>
</ol>
<p>Here is an EmacsWiki page that helped me figure out some details: <a href="http://www.emacswiki.org/emacs/MultilineRegexp">MultilineRegexp</a></p>
<p>Another approach would be to call shell python command on a region. For the regexp I use <em><strong>(?s)</strong></em> to set a flag <em><strong>re.S</strong></em> (dot matches all) to match all characters including newline. Select a region in Emacs buffer or press <em>C-x h</em> to mark the whole buffer and type:</p>
<p><em>C-u M-|</em><br />
<em> Shell command on region: <strong>python -c &#8220;import sys, re; print re.sub(r&#8217;(?s)json.dumps((.*?))&#8217;, &#8216;g&lt;1&gt;&#8217;, sys.stdin.read())&#8221;</strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/12/18/multiline-regexp/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>BASH history &#8211; incremental search forward</title>
		<link>http://ruslanspivak.com/2010/11/25/bash-history-incremental-search-forward/</link>
		<comments>http://ruslanspivak.com/2010/11/25/bash-history-incremental-search-forward/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 01:37:00 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=605</guid>
		<description><![CDATA[In my previous post I wrote about using Ctrl-R for incremental search backward. It&#8217;s about time to move on to incremental search forward. Default BASH key binding for incremental history search forward is Ctrl-S - no surprises here (if you&#8217;re an Emacs user you should feel at home right away without any customizations). The only [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my <a title="BASH history - reverse intelligent search" href="http://ruslanspivak.com/2010/11/20/bash-history-reverse-intelligent-search/">previous post</a> I wrote about using <em><strong>Ctrl-R</strong></em> for incremental search backward.</p>
<p>It&#8217;s about time to move on to incremental search forward.</p>
<p>Default BASH key binding for incremental history search forward is <em><strong>Ctrl-S </strong></em>- no surprises here (if you&#8217;re an Emacs user you should feel at home right away without any customizations).</p>
<p>The only issue is that that key binding is shadowed by so called terminal flow control key binding. And you probably experienced that at least once when you accidentally pressed <em><strong>Ctrl-S</strong></em> and your terminal &#8220;hang&#8221;. To revive terminal after that you have to press <em><strong>Ctrl-Q</strong></em>.</p>
<p>If you run <em><strong>$ stty -a</strong></em> you&#8217;ll see those key bindings in a form like <em><strong> start = ^Q; stop = ^S</strong></em> ﻿</p>
<p>There are several workarounds to have your search back:</p>
<ol>
<li>Disable terminal flow control altogether: <em><strong>$ stty -ixon</strong></em><br />
Now your <em><strong>Ctrl-S</strong></em> will work like a charm</li>
<li>If you want to continue to use terminal flow control then you can just re-bind its <em><strong>Ctrl-S</strong></em> to, let&#8217;s say, <em><strong>Ctrl-X</strong></em> allowing BASH <em><strong>Ctrl-S</strong></em> to work as incremental history search forward:<br />
<em><strong>$ stty stop ^X</strong></em></li>
<li>Bind readline&#8217;s  <em><strong>forward-search-history</strong></em> function to another key sequence, for example, <em><strong>Alt-S</strong></em>:<br />
- <em><strong>$ bind &#8216;&#8221;es&#8221;: forward-search-history&#8217;</strong></em><br />
- or put this line into <em>~/.inputrc</em> : <em><strong>&#8220;es&#8221;: forward-search-history</strong></em></li>
</ol>
<p>Personally I use <strong><em>(1)</em> / <em>(2)</em></strong> because being an Emacs user it&#8217;s natural for me to use <em><strong>Ctrl-S</strong></em> for incremental search forward and it also plays nicely with <em><strong>Ctrl-R</strong></em> &#8211; after I cycled backwards through matched commands with <em><strong>Ctrl-R</strong></em> I can cycle forward trough them by simply pressing <em><strong>Ctrl-S</strong></em>, works perfectly without a hitch.</p>
<p>Workaround <em><strong>(3)</strong></em> for some reason doesn&#8217;t work as nicely with <em><strong>Ctrl-R</strong></em>, once I press <em><strong>Alt-S</strong></em> after <em><strong>Ctrl-R</strong></em> it clears up typed characters. I put it here just as an alternative for non-Emacs users <img src='http://ruslanspivak.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/11/25/bash-history-incremental-search-forward/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Auto insert module header</title>
		<link>http://ruslanspivak.com/2010/11/01/auto-insert-module-header/</link>
		<comments>http://ruslanspivak.com/2010/11/01/auto-insert-module-header/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 10:18:23 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=511</guid>
		<description><![CDATA[It&#8217;s a good development practice to insert at the beginning of a file copyright, license, and author information. Being lazy I automated the process of insertion using Emacs&#8217; skeleton mode and auto insert mode. This is an example skeleton configuration: When I open a new Python file with C-x C-f the header is inserted automatically: [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>It&#8217;s a good development practice to insert at the beginning of a file copyright, license, and author information. Being lazy I automated the process of insertion using Emacs&#8217; <a href="http://www.emacswiki.org/emacs/SkeletonMode">skeleton mode</a> and <a href="http://www.emacswiki.org/emacs/AutoInsertMode">auto insert mode</a>.</p>
<p>This is an example skeleton configuration:<br />
<a href="http://ruslanspivak.files.wordpress.com/2010/10/emacsskeletoninit3.png"><img class="alignleft size-full wp-image-532" title="emacsskeletoninit" src="http://ruslanspivak.files.wordpress.com/2010/10/emacsskeletoninit3.png" alt="" width="450" height="312" /></a></p>
<p>When I open a new Python file with <em><strong>C-x C-f</strong></em> the header is inserted automatically:<br />
<a href="http://ruslanspivak.files.wordpress.com/2010/10/emacsskeletonresult1.png"><img class="alignleft size-full wp-image-529" title="emacsskeletonresult" src="http://ruslanspivak.files.wordpress.com/2010/10/emacsskeletonresult1.png" alt="" width="450" height="236" /></a></p>
<p>Skeletons support Lisp expressions, so I embedded automatic year generation as well as retrieval of organization&#8217;s name, user&#8217;s full name, and user&#8217;s email address. (You may need to add ORGANIZATION and EMAIL environment variables to your <em>.bash_profile</em>)<br />
<em><strong>M-x</strong></em> <em>skeleton-name</em> will call the skeleton interactively and insert it at the cursor&#8217;s position</p>
<p>Alternatively the header insertion automation can be accomplished with <a href="http://code.google.com/p/yasnippet/">yasnippet</a> package, which I use with different programming modes.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/11/01/auto-insert-module-header/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Swapping lines</title>
		<link>http://ruslanspivak.com/2010/10/21/swapping-lines/</link>
		<comments>http://ruslanspivak.com/2010/10/21/swapping-lines/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 02:05:53 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=486</guid>
		<description><![CDATA[Being able to swap lines in Emacs with C-x C-t comes in handy when I edit Python code and want to prettify some imports: import sys import os I move cursor to a line that I want to swap, in this case import os, and press C-x C-t which exchanges the current line and a [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Being able to swap lines in Emacs with <strong><em>C-x C-t</em></strong> comes in handy when I edit Python code and want to prettify some imports:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;"><span style="color:#fbde2d;">import</span> sys
<span style="color:#fbde2d;">import</span> os</pre>
<p>I move cursor to a line that I want to swap, in this case <strong><em>import os</em></strong>, and press <strong><em>C-x C-t</em></strong> which exchanges the current line and a previous line:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;"><span style="color:#fbde2d;">import</span> os
<span style="color:#fbde2d;">import</span> sys</pre>
<p>A very basic and simple operation. And while the same can be accomplished with cutting and pasting &#8211; swapping is faster in this case.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/10/21/swapping-lines/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Nuke whitespaces on file saving</title>
		<link>http://ruslanspivak.com/2010/10/11/nuke-whitespaces-on-file-saving/</link>
		<comments>http://ruslanspivak.com/2010/10/11/nuke-whitespaces-on-file-saving/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 04:17:19 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=375</guid>
		<description><![CDATA[In one of my previous posts I wrote how I kept track of whitespaces and a column 80 overflow. And in my config you could see that I used delete-trailing-whitespace with write-file-hooks ;; nuke trailing whitespaces when writing to a file (add-hook 'write-file-hooks 'delete-trailing-whitespace) What I wanted to use with write-file-hooks was whitespace-cleanup, but somehow [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In one of my <a href="http://ruslanspivak.com/2010/09/27/keep-track-of-whitespaces-and-column-80-overflow/">previous posts</a> I wrote how I kept track of whitespaces and a column 80 overflow. And in my config you could see that I used <em>delete-trailing-whitespace</em> with <em>write-file-hooks</em></p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">nuke trailing whitespaces when writing to a file
</span>(add-hook 'write-file-hooks 'delete-trailing-whitespace)
</pre>
<p>What I wanted to use with <em>write-file-hooks</em> was <em><strong>whitespace-cleanup</strong></em>, but somehow it didn&#8217;t work for me with that hook (whitespaces were removed when saving a file, but buffer was marked as containing changes).</p>
<p>Thanks to Valeriy Zamarayev who, <a href="http://ruslanspivak.com/2010/09/27/keep-track-of-whitespaces-and-column-80-overflow/#comment-712">in his comment to my post</a>, mentioned save hook and whitespace-cleanup. This is what I use now to remove whitespaces when saving a file:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">nuke whitespaces when writing to a file
</span>(add-hook 'before-save-hook 'whitespace-cleanup)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/10/11/nuke-whitespaces-on-file-saving/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Streamline file editing with EmacsClient</title>
		<link>http://ruslanspivak.com/2010/10/03/streamline-file-editing-with-emacsclient/</link>
		<comments>http://ruslanspivak.com/2010/10/03/streamline-file-editing-with-emacsclient/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 03:13:16 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=344</guid>
		<description><![CDATA[In the past when I had to edit a file from the command line in my terminal I did one of the following: Launched Emacs with the file as a command line parameter. That was usually a slow and annoying process. Switched to a running Emacs instance and used C-x C-f to open the file. [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In the past when I had to edit a file from the command line in my terminal I did one of the following:</p>
<ul>
<li>Launched Emacs with the file as a command line parameter. That was usually a slow and annoying process.</li>
<li>Switched to a running Emacs instance and used <em><strong>C-x C-f</strong></em> to open the file. If the file path was too long it was taking some time to enter it and was annoying too.</li>
<li>Resorted to Vi in some simple editing cases because it was faster and less annoying (yes, guilty as charged <img src='http://ruslanspivak.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>After being annoyed couple times I set out to find  a solution to my problem and it didn&#8217;t take long to find out that in Emacs world existed a perfect one: <a href="http://www.emacswiki.org/emacs/EmacsClient">EmacsClient</a></p>
<p>My problem was solved.</p>
<p>If you&#8217;ve never tried <a href="http://www.emacswiki.org/emacs/EmacsClient">EmacsClient</a> before I urge you to do so &#8211; it&#8217;ll do you some good.</p>
<p>To use it you need to run Emacs in <em>server mode</em> and use a command line utility <strong><em>emacsclient</em></strong> to open a file for editing in an already running Emacs instance. Opening a file for editing in this way is blazingly fast.</p>
<p>I have this line in <em><strong>.emacs</strong></em> to start Emacs in server mode:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;">(server-start)</pre>
<p>You can also start server manually from a running Emacs instance: <strong><em>M-x server-start</em></strong></p>
<p>Then from the command line send a file to the running instance with <em>emacsclient</em>:<br />
<strong>$ emacsclient -n </strong><em>your_file_name</em></p>
<p>I simplified <em>emacsclient</em> invocation by creating a function and putting it into my <strong><em>.bashrc</em></strong>:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;">ec() {
    emacsclient -n $<span style="color:#ff6400;">1</span>
}</pre>
<p>So now whenever I need to edit a file from the command line I just invoke the <em><strong>ec</strong></em> function with a file as a parameter:</p>
<p><strong>$ ec</strong> <em>file.txt</em></p>
<p>P.S.: You can also set environment variable <strong>EDITOR</strong> to <em>emacsclient</em> to make it a default editor for different utilities relying on the <strong>EDITOR</strong> variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/10/03/streamline-file-editing-with-emacsclient/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Keep track of whitespaces and column 80 overflow</title>
		<link>http://ruslanspivak.com/2010/09/27/keep-track-of-whitespaces-and-column-80-overflow/</link>
		<comments>http://ruslanspivak.com/2010/09/27/keep-track-of-whitespaces-and-column-80-overflow/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 02:28:32 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=307</guid>
		<description><![CDATA[When working in Emacs python mode I keep track of whitespaces and column 80 overflow using a whitespace-mode that is part of Emacs 22+ Here is a snippet from my .emacs: ;; nuke trailing whitespaces when writing to a file (add-hook 'write-file-hooks 'delete-trailing-whitespace) ;; display only tails of lines longer than 80 columns, tabs and [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>When working in Emacs python mode I keep track of whitespaces and column 80 overflow using a <a href="http://www.emacswiki.org/emacs/WhiteSpace">whitespace-mode</a> that is part of Emacs 22+</p>
<p>Here is a snippet from my <em>.emacs</em>:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">nuke trailing whitespaces when writing to a file
</span>(add-hook 'write-file-hooks 'delete-trailing-whitespace)

<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">display only tails of lines longer than 80 columns, tabs and
</span><span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">trailing whitespaces
</span>(setq whitespace-line-column 80
      whitespace-style '(tabs trailing lines-tail))

<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">face for long lines' tails
</span>(set-face-attribute 'whitespace-line nil
                    <span style="color:#F8F8F8;">:background</span> <span style="color:#61CE3C;">"red1"</span>
                    <span style="color:#F8F8F8;">:foreground</span> <span style="color:#61CE3C;">"yellow"</span>
                    <span style="color:#F8F8F8;">:weight</span> 'bold)

<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">face for Tabs
</span>(set-face-attribute 'whitespace-tab nil
                    <span style="color:#F8F8F8;">:background</span> <span style="color:#61CE3C;">"red1"</span>
                    <span style="color:#F8F8F8;">:foreground</span> <span style="color:#61CE3C;">"yellow"</span>
                    <span style="color:#F8F8F8;">:weight</span> 'bold)

<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">activate minor whitespace mode when in python mode
</span>(add-hook 'python-mode-hook 'whitespace-mode)
</pre>
<p>Because my Emacs is setup to save/restore desktop I&#8217;ve added the following lines to play nicely with the <em>whitespace-mode</em> when restoring a previous session:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
<span style="color:#AEAEAE;font-style:italic;">;; </span><span style="color:#AEAEAE;font-style:italic;">save whitespace-mode variables
</span>(add-to-list 'desktop-globals-to-save 'whitespace-line-column)
(add-to-list 'desktop-globals-to-save 'whitespace-style)
</pre>
<p>This is how trailing whitespaces, tabs and long lines tails look in my Emacs buffer:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
<span style="color:#FBDE2D;">import</span> sys

<span style="color:#FBDE2D;">def</span> <span style="color:#FF6400;">main</span>():
    <span style="color:#61CE3C;">"""This line is longer than 80 cloumns ...........................</span><span style="color:#ffff00;background-color:#ff0000;font-weight:bold;">.."""</span>
    values = [
        <span style="color:#AEAEAE;font-style:italic;"># next line contains leading tab
</span><span style="color:#ffff00;background-color:#ff0000;font-weight:bold;">        </span><span style="color:#61CE3C;">'qwerty'</span>,
        <span style="color:#61CE3C;">'test'</span>,<span style="color:#ffff00;background-color:#ff0000;font-weight:bold;">    </span>
        ]

<span style="color:#ffff00;background-color:#ff0000;font-weight:bold;">   </span>
<span style="color:#FBDE2D;">if</span> <span style="color:#F8F8F8;">__name__</span> == <span style="color:#61CE3C;">'__main__'</span>:
    main() <span style="color:#AEAEAE;font-style:italic;"># this line contains trailing whitespaces</span><span style="color:#ffff00;background-color:#ff0000;font-weight:bold;">    </span><span style="color:#AEAEAE;font-style:italic;">
</span>
</pre>
<p><strong>UPDATE</strong> November 15th, 2011<br />
In <strong>Emacs 24</strong> you have to add a <strong>face</strong> specifier to the <strong>whitespace-style</strong> to make trailing whitespaces highlighted:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
(setq whitespace-line-column 80
      whitespace-style '(face tabs trailing lines-tail))
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/09/27/keep-track-of-whitespaces-and-column-80-overflow/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>C-w to delete word backward in Conkeror</title>
		<link>http://ruslanspivak.com/2010/09/22/c-w-to-delete-word-backward-in-conkeror/</link>
		<comments>http://ruslanspivak.com/2010/09/22/c-w-to-delete-word-backward-in-conkeror/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 04:03:42 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=293</guid>
		<description><![CDATA[Long time ago after reading Steve Yegge&#8217;s post on Effective Emacs I rebound C-w to &#8216;backward-kill-word and since then I&#8217;ve had the same key binding for both Emacs and BASH. Now that Conkeror is my default browser it makes sense to have C-w to work the same way as in Emacs and BASH. This is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Long time ago after reading Steve Yegge&#8217;s post on <a href="http://sites.google.com/site/steveyegge2/effective-emacs">Effective Emacs</a> I rebound <em><strong>C-w</strong></em> to <em><strong>&#8216;backward-kill-word</strong></em> and since then I&#8217;ve had the same key binding for both Emacs and BASH.</p>
<p>Now that <a href="http://conkeror.org/">Conkeror</a> is my default browser it makes sense to have <em><strong>C-w</strong></em> to work the same way as in Emacs and BASH.</p>
<p>This is a snippet that I&#8217;ve added to my  <em><strong>~/.conkerorrc</strong></em></p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;"><span style="background-color:#330;">define_key(text_keymap, 'C-w', 'cmd_deleteWordBackward');</span></pre>
<p>Now I have a uniform key binding across my tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/09/22/c-w-to-delete-word-backward-in-conkeror/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>BASH key bindings</title>
		<link>http://ruslanspivak.com/2010/09/20/bash-key-bindings/</link>
		<comments>http://ruslanspivak.com/2010/09/20/bash-key-bindings/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 05:34:09 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=279</guid>
		<description><![CDATA[Here is a list of BASH key bindings that I use on a daily basis: Key binding Action Ctrl-r History reverse search Ctrl-a Jump to BOL Ctrl-e Jump to EOL Ctrl-l Clear terminal Ctrl-k Delete from cursor to EOL Ctrl- Undo last operation Ctrl-m Return Ctrl-w Delete word left from cursor Ctrl-u Delete from BOL [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Here is a list of BASH key bindings that I use on a daily basis:</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups">
<col align="left"></col>
<col align="left"></col>
<thead>
<tr>
<th>Key binding</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ctrl-r</td>
<td>History reverse search</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-a</td>
<td>Jump to BOL</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-e</td>
<td>Jump to EOL</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-l</td>
<td>Clear terminal</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-k</td>
<td>Delete from cursor to EOL</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-</td>
<td>Undo last operation</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-m</td>
<td>Return</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-w</td>
<td>Delete word left from cursor</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-u</td>
<td>Delete from BOL to cursor</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-x Ctrl-e</td>
<td>Open the default editor</td>
</tr>
<tr>
<td></td>
<td>$EDITOR and run edited command</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-p</td>
<td>Previous command in history</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-n</td>
<td>Next command in history</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-f</td>
<td>Move forward a char</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-b</td>
<td>Move backward a char</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Alt-f</td>
<td>Move forward a word</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Alt-b</td>
<td>Move backward a word</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-d</td>
<td>Delete char under cursor</td>
</tr>
<tr>
<td></td>
<td>Exit shell if empty</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Alt-d</td>
<td>Delete forward word</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-y</td>
<td>Paste content of the kill ring</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-t</td>
<td>Swap current char with</td>
</tr>
<tr>
<td></td>
<td>previous char</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Alt-t</td>
<td>Swap current word with</td>
</tr>
<tr>
<td></td>
<td>previous word</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Alt-u</td>
<td>Uppercase word at cursor</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Alt-l</td>
<td>Lowercase word at cursor</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-s</td>
<td>Freeze terminal</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ctrl-q</td>
<td>Restore frozen terminal</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Shift-PgUp</td>
<td>Scroll screen up</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Shift-PgDn</td>
<td>Scroll screen down</td>
</tr>
</tbody>
</table>
<p>Most of these key bindings work in Emacs too.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/09/20/bash-key-bindings/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>C-m as Return in Conkeror</title>
		<link>http://ruslanspivak.com/2010/09/15/c-m-as-return-in-conkeror/</link>
		<comments>http://ruslanspivak.com/2010/09/15/c-m-as-return-in-conkeror/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 01:56:00 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/?p=272</guid>
		<description><![CDATA[By default C-m is not bound to Return in Conkeror and I became quite addictive to that binding in Emacs. It took me a bit to figure it out, but here is a snippet that should go into .conkerorrc to make C-m to work: require("global-overlay-keymap.js"); define_key_alias("C-m", "return");]]></description>
			<content:encoded><![CDATA[<p></p><p>By default <strong><em>C-m</em></strong> is not bound to <strong><em>Return</em></strong> in <a href="http://conkeror.org">Conkeror</a> and I became quite addictive to that binding in Emacs.<br />
It took me a bit to figure it out, but here is a snippet that should go into <em>.conkerorrc</em> to make <em><strong>C-m</strong></em> to work:</p>
<pre style="color:#f8f8f8;background-color:#0c1021;font-size:8pt;">require(<span style="color:#61ce3c;">"global-overlay-keymap.js"</span>);
define_key_alias(<span style="color:#61ce3c;">"C-m"</span>, <span style="color:#61ce3c;">"return"</span>);</pre>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2010/09/15/c-m-as-return-in-conkeror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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) [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2009/04/06/downcase-with-replace-regexp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</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>

		<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 [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2009/03/16/refresh-firefox-page-from-emacs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</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 [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2009/03/09/emacs-dark-theme-ipython-colors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</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 [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/22/delete-blank-lines-with-flush-lines/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</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 [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/05/incremental-search-forward/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</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; [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/03/align-regexp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</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; [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/02/erlang-for-python-programmers-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</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 [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/10/01/creating-tables-in-emacs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</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 [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/09/24/erlang-for-python-programmers-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</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. [...]]]></description>
			<content:encoded><![CDATA[<p></p><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>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/09/20/comment-dwim-and-comment-style/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Erlang for Python programmers: Part II</title>
		<link>http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/</link>
		<comments>http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/#comments</comments>
		<pubDate>Sun, 16 Sep 2007 21:54:23 +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/16/erlang-for-python-programmers-part-ii/</guid>
		<description><![CDATA[In this short tutorial we will take a look at comparison operations, some arithmetic operations and modules. You may also want to skim over Inro and Part I. Comparison operations Python Erlang Description Erlang Example --------+--------+-----------------------+---------------- &#60; &#60; strictly less than --------+--------+-----------------------+---------------- &#60;= =&#60; less than or equal --------+--------+-----------------------+---------------- &#62; &#62; strictly greater than --------+--------+-----------------------+---------------- [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In this short tutorial we will take a look at comparison operations, some arithmetic operations and modules. You may also want to skim over <a href="http://ruslanspivak.com/2007/08/27/erlang-for-python-programmers-intro/">Inro</a> and <a href="http://ruslanspivak.com/2007/09/09/erlang-for-python-programmers-part-i/">Part I</a>.</p>
<h3><u><strong>Comparison operations</strong></u></h3>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
 Python   Erlang   Description             Erlang Example
 --------+--------+-----------------------+----------------
  &lt;        &lt;        strictly less than
 --------+--------+-----------------------+----------------
  &lt;=       =&lt;       less than or equal
 --------+--------+-----------------------+----------------
  &gt;        &gt;        strictly greater than
 --------+--------+-----------------------+----------------
  &gt;=       &gt;=       greater than or equal
 --------+--------+-----------------------+----------------
  !=       /=       not equal
 --------+--------+-----------------------+----------------
  ==       ==       equal                   1&gt; 1 == 1.
                                            true
                                            2&gt; 1 == 1.0.
                                            true
 --------+--------+-----------------------+----------------
           =:=      exactly equal to        1&gt; 1 =:= 1.
                                            true
                                            2&gt; 1 =:= 1.0.
                                            false
 --------+--------+-----------------------+----------------
           =/=      exactly not equal to
 --------+--------+-----------------------+----------------
  is                object identity
 --------+--------+-----------------------+----------------
  is not            negated obj identity</pre>
<h4><u>Python notes</u></h4>
<p>Above comparison operations are supported by all objects. In Python you can chain comparisons:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x, y, z = 1, 3, 7</span>
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x &lt; y &lt;= z</span>
True
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x &lt; y and y &lt;= z</span>
True</pre>
<p>Foregoing examples are identical, except that in second case <em><strong>y</strong></em> is evaluated twice.</p>
<h4><u>Erlang notes</u></h4>
<p>Following order is defined:</p>
<p><em>number &lt; atom &lt; reference &lt; fun &lt; port &lt; pid &lt; tuple &lt; list &lt; binary</em></p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">5 &lt; erlang.</span>
true
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">erlang &lt; make_ref().</span>
true</pre>
<p>Both Erlang comparison operators (<strong>&lt;, =&lt;, &gt;, &gt;=, /=, ==</strong>) and Python comparison operators(<strong>&lt;, &lt;=, &gt;, &gt;=, !=, ==</strong>) make type coerce, &#8220;narrower&#8221; type is widened to that of another, ie when comparing integer and float first integer is converted to float, etc.<br />
Erlang has special operators without type coerce though: <strong>=:= </strong>and <strong>=/=</strong></p>
<h3><strong><u>Arithmetic operations</u></strong></h3>
<p>I&#8217;ll provide only several operations, for more take a look at corresponding language references.</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
 Python   Python Desc.         Erlang    Erlang Desc.   Example
 --------+--------------------+---------+--------------+--------------------
  x % y    remainder            X rem Y   integer        &gt;&gt;&gt; 7 % 3
           of x / y                       remainder      1
                                          of X / Y       &gt;&gt;&gt; 7.0 % 3
                                                         1.0

                                                         1&gt; 7 rem 3.
                                                         1
                                                         1&gt; 7.0 rem 3.
                                                         =ERROR REP..
 --------+--------------------+---------+--------------+--------------------
  x / y    quotient             X / Y     floating       &gt;&gt;&gt; 7 / 3
           of x and y                     point          2
                                          division       &gt;&gt;&gt; 7.0 / 3
                                                         2.3333333333333335

                                                         1&gt; 7 / 3.
                                                         2.33333
 --------+--------------------+---------+--------------+--------------------
  x // y   (floored) quotient   X div Y   integer        &gt;&gt;&gt; 7 // 3
           of x and y,                    division       2
           integer division                              &gt;&gt;&gt; 7.5 // 3
           (result type is                               2.0
           not forced to be
           int)                                          1&gt; 7 div 3.
                                                         2</pre>
<h3><u><strong>Modules</strong></u></h3>
<p>Code in Erlang is organized into units called modules, which is familiar word for Python programmer.</p>
<p>Let&#8217;s define sample module with function declaration stored in file <em><strong>mymath.erl</strong></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>([fact/1]).

<span style="color:#87cefa;">fact</span>(0) -&gt;<span style="color:#87cefa;"> </span>1;
<span style="color:#87cefa;">fact</span>(<span style="color:#eedd82;">N</span>) -&gt;<span style="color:#87cefa;"> </span><span style="color:#eedd82;">N</span> * fact(<span style="color:#eedd82;">N</span>-1).</pre>
<p><u>What we see here:</u></p>
<ol>
<li> module&#8217;s source code is stored in file with <strong>.erl</strong> extension</li>
<li>module consists of <strong>attributes</strong> and <strong>function declarations</strong> which are terminated by <strong>period</strong> (<strong>.</strong>)</li>
<li>we should provide module declaration defining name of the module</li>
</ol>
<ul>
<li>module name should be atom</li>
<li>module name is the same as file name minus <strong>.erl</strong> extension</li>
<li>module declaration is mandatory</li>
<li>module declaration attribute should be defined first.</li>
</ul>
<p>To make functions defined in module accessible outside the module we need to export them, for this <strong>-export</strong> module attribute exists. We write exported functions inside square brackets in form of <strong>func/N</strong> where <strong>N</strong> is the number of arguments of function, called <strong>arity.</strong> I&#8217;ll repeat that functions not pointed in <strong>-export</strong> will not be accessible outside module.</p>
<p>Before code can be run, module must be compiled. Resulting compiled file will contain extension <strong>.beam</strong></p>
<p>If you use Emacs you can compile module with <strong>C-c C-k</strong> and see results in erlang shell:</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}</pre>
<p>Or you can compile it directly in shell. Make sure your shell&#8217;s current directory is where your <strong>mymath.erl</strong> lives, if it&#8217;s not the case use <em><strong>cd</strong></em> command in erlang shell, <em>cd(&#8220;/path/to/dir/with/mymath.erl&#8221;)</em> :</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">c(mymath).</span>
{ok,mymath}</pre>
<p>To invoke our function <strong>fact</strong> we use syntax <strong>mod:func</strong> :</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">mymath:fact(4).</span>
24</pre>
<p>Erlang has also <strong>-import</strong> attribute which allows to import functions into modules, so that you don&#8217;t need to use fully-qualified name <strong>mod:func</strong> to invoke function, again familiar behaviour and naming to Python programmer.</p>
<p>Erlang allows to insert code from file as-is with <strong>-include</strong> attribute at point where <strong>-import </strong>is defined, this is used to include records and macro definitions, for example.</p>
<p>Comments in Erlang module begin with character &#8220;<strong>%</strong>&#8220;, continue up to end-of-line and may be placed anywhere except inside string and quoted atoms.<br />
Like in Python Erlang has no multiline comments.<br />
If you use Emacs you can easily comment whole region with <strong>M-;</strong> command after you marked it.</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(mymath).
<span style="color:#87cefa;">-export</span>([fact/1, print_double/1]).
<span style="color:#87cefa;">-import</span>(lists, [foreach/2]).
<span style="color:#87cefa;">-include</span>(<span style="color:#ffa07a;">"my_records.hrl"</span>).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">sum(L) -&gt;
</span><span style="color:#ff4500;">%%     </span><span style="color:#ff4500;">sum(L, 0).
</span>
<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">sum([H|T], Acc) -&gt;
</span><span style="color:#ff4500;">%%     </span><span style="color:#ff4500;">sum(T, H+Acc);
</span><span style="color:#ff4500;">%% </span><span style="color:#ff4500;">sum([], Acc) -&gt; Acc.
</span>
<span style="color:#87cefa;">fact</span>(0) -&gt;<span style="color:#87cefa;"> </span>1;
<span style="color:#87cefa;">fact</span>(<span style="color:#eedd82;">N</span>) -&gt;<span style="color:#87cefa;"> </span><span style="color:#eedd82;">N</span> * fact(<span style="color:#eedd82;">N</span>-1).
<span style="color:#87cefa;">print_double</span>(<span style="color:#eedd82;">L</span>) -&gt;
    foreach(<span style="color:#00ffff;">fun</span>(<span style="color:#eedd82;">X</span>) -&gt;<span style="color:#87cefa;"> </span>io:format(<span style="color:#ffa07a;">"Double of ~p = ~p~n"</span>, [<span style="color:#eedd82;">X</span>, <span style="color:#eedd82;">X</span>*2]) <span style="color:#00ffff;">end</span>, <span style="color:#eedd82;">L</span>).</pre>
<p>Fin.</p>
<p>Next tutorial will be devoted to thorough exploration of functions in Erlang.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/09/16/erlang-for-python-programmers-part-ii/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How to say big numbers in English: Common Lisp</title>
		<link>http://ruslanspivak.com/2007/09/12/how-to-say-big-numbers-in-english-common-lisp/</link>
		<comments>http://ruslanspivak.com/2007/09/12/how-to-say-big-numbers-in-english-common-lisp/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 09:08:46 +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/12/how-to-say-big-numbers-in-english-common-lisp/</guid>
		<description><![CDATA[When i was describing notations and representation of numbers in Erlang(and Python) i forgot to mention very cool feature of format function in Common Lisp, with which i got acquainted exploring highly-recommended Practical Common Lisp book. This feature is ~R directive which knows how to say really big numbers in English words. Here is the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>When i was describing <a href="http://ruslanspivak.com/2007/09/09/erlang-for-python-programmers-part-i/">notations and representation of numbers in Erlang(and Python)</a> i forgot to mention very cool feature of <strong>format </strong>function in Common Lisp, with which i got acquainted exploring highly-recommended <a href="http://www.gigamonkeys.com/book/">Practical Common Lisp</a> book.</p>
<p>This feature is  <strong>~R</strong> directive which knows how to say really big numbers in English words.</p>
<p>Here is the example (I use <a href="http://sbcl.sourceforge.net/">SBCL</a> + <a href="http://common-lisp.net/project/slime/">SLIME</a>):</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">CL-USER&gt; </span>(format nil "~R" 1604872898756737459538)
<span style="color:#ff0000;">"one sextillion six hundred four quintillion eight hundred seventy-two
quadrillion eight hundred ninety-eight trillion seven hundred fifty-six
billion seven hundred thirty-seven million four hundred fifty-nine
thousand five hundred thirty-eight"</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/09/12/how-to-say-big-numbers-in-english-common-lisp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Erlang for Python programmers: Intro</title>
		<link>http://ruslanspivak.com/2007/08/27/erlang-for-python-programmers-intro/</link>
		<comments>http://ruslanspivak.com/2007/08/27/erlang-for-python-programmers-intro/#comments</comments>
		<pubDate>Sun, 26 Aug 2007 22:51:32 +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/08/27/erlang-for-python-programmers-intro/</guid>
		<description><![CDATA[I assume you have already installed Erlang on your system and are able to run erlang shell either from command line or from within Emacs (C-c C-z). This is a view of Eshell from my Emacs: Erlang (BEAM) emulator version 5.5.2 [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.5.2 (abort with ^G) 1&#62; Let&#8217;s do a simple thing [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I assume you have already installed <a href="http://www.erlang.org/doc/installation_guide/part_frame.html">Erlang</a> on your system and are able to run erlang shell either from command line or from within <a href="http://www.erlang.org/doc/apps/tools/erlang_mode_chapter.html">Emacs</a> (<em>C-c C-z</em>).</p>
<p>This is a view of Eshell from my Emacs:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
Erlang (BEAM) emulator version 5.5.2  [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.5.2  (abort with ^G)
<span style="color:#00ffff;">1&gt; </span></pre>
<p>Let&#8217;s do a simple thing and try to assign a value to a variable in the Eshell. First you should know that variables in Erlang start with uppercase letter or underscore (_), for example:</p>
<ul>
<li>X</li>
<li>Name</li>
<li>Address</li>
<li>_</li>
<li>_NoWarn</li>
</ul>
<p>Ok, let&#8217;s finally assign some value to a variable X:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">X = 4.</span>
4
<span style="color:#00ffff;">2&gt;</span></pre>
<p>So far, so good. Make sure to type final &#8220;.&#8221; at the end of expression when you are done entering code.</p>
<p>Let&#8217;s assign new value to X:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">X = 5.</span>

=ERROR REPORT==== 27-Aug-2007::00:23:02 ===
Error in process &lt;0.29.0&gt; with exit value: {{badmatch,5},[{erl_eval,expr,3}]}

** exited: {{badmatch,5},[{erl_eval,expr,3}]} **
<span style="color:#00ffff;">3&gt; </span></pre>
<p>Here you need to understand and remember couple of things:</p>
<ol>
<li>Erlang uses <strong>single assignment</strong>, a variable can only be bound once, bar none.</li>
<li>In  expression <strong>X =  5</strong> operator <strong>= </strong>is not an assignment, but <strong>match operator </strong>and variables are bound to values using <strong>pattern matching </strong>mechanism.</li>
</ol>
<p>I think it won&#8217;t be an exaggeration if I say that <em>Pattern Matching</em> permeates Erlang. To understand why we got ERROR REPORT in above example with <strong>{badmatch, 5}</strong> let&#8217;s dive into this pattern matching a bit.</p>
<p>When you use pattern matching&#8217;s = operator left side called <strong>pattern</strong> is matched against right side which is a <strong>term</strong>(piece of data of any Erlang&#8217;s data type: integer, float, list, tuple, etc). If your matching fails you get run-time error, if it succeeds then any unbound variable in lefthand pattern becomes bound.</p>
<p>Returning to our example &#8211; first we had an unbound variable:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">1&gt; </span><span style="font-weight:bold;">X.</span>
** 1: variable 'X' is unbound **
<span style="color:#00ffff;">2&gt; </span></pre>
<p>Then we bound value to variable X with pattern matching:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">2&gt; </span><span style="font-weight:bold;">X = 4.</span>
4
<span style="color:#00ffff;">3&gt; </span><span style="font-weight:bold;">X.</span>
4
<span style="color:#00ffff;">4&gt; </span></pre>
<p>Now as the variable X is bound we can&#8217;t change its value any more (remember <strong>single assignment</strong>?) otherwise we&#8217;ll get <strong>badmatch</strong> error like in foregoing X = 5 (4 does not match 5), but we may use = to <strong>match </strong>our variable:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">4&gt; </span><span style="font-weight:bold;">X = 4.</span>
4
<span style="color:#00ffff;">5&gt; </span></pre>
<p>Let&#8217;s add a bit of tuples to pattern matching. In Erlang tuple is defined inside {&#8230;}. ie:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">5&gt; </span><span style="font-weight:bold;">{2, 3}.</span>
{2,3}
<span style="color:#00ffff;">6&gt; </span></pre>
<p>and now pattern matching:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">6&gt; </span><span style="font-weight:bold;">{X, Y} = {2, 3}.</span>

=ERROR REPORT==== 27-Aug-2007::01:10:45 ===
Error in process &lt;0.29.0&gt; with exit value: {{badmatch,{2,3}},[{erl_eval,expr,3}]}

** exited: {{badmatch,{2,3}},[{erl_eval,expr,3}]} **
<span style="color:#00ffff;">7&gt; </span><span style="font-weight:bold;">{X, Y} = {4, 3}.</span>
{4,3}
<span style="color:#00ffff;">8&gt; </span><span style="font-weight:bold;">X.</span>
4
<span style="color:#00ffff;">9&gt; </span><span style="font-weight:bold;">Y.</span>
3
<span style="color:#00ffff;">10&gt; </span></pre>
<p>In first attempt we tried to match X with 2 and the matching failed as the value of X is 4, in a second attempt the matching succeeded and unbound variable Y also got its value (became bound).</p>
<p>This may look to you like <strong>tuple unpacking</strong> in Python:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x, y = (4, 3)</span>
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x, y</span>
(4, 3)
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x, y = (5, 3)</span>
<span style="color:#00ffff;">&gt;&gt;&gt; </span><span style="font-weight:bold;">x, y</span>
<span style="background-color:#40e0d0;">(</span>5, 3<span style="background-color:#40e0d0;">)</span></pre>
<p>but it&#8217;s only visual similarity, though i must admit, useful one.</p>
<p>That&#8217;s it for today, later you&#8217;ll see how pattern matching is uber-useful and how it is used in different parts of Erlang&#8217;s constructs.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/08/27/erlang-for-python-programmers-intro/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Erlang man pages in Emacs</title>
		<link>http://ruslanspivak.com/2007/08/19/erlang-man-pages-in-emacs/</link>
		<comments>http://ruslanspivak.com/2007/08/19/erlang-man-pages-in-emacs/#comments</comments>
		<pubDate>Sun, 19 Aug 2007 11:56:47 +0000</pubDate>
		<dc:creator>Ruslan Spivak</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://ruslanspivak.com/2007/08/19/erlang-man-pages-in-emacs/</guid>
		<description><![CDATA[Well it&#8217;s actually as simple as running M-x man RET module_name RET in minibuffer. I&#8217;m using Erlang setup from rpm on FC7 and by default man pages are located under /usr/lib/erlang/man/ so to use above command in minibuffer you must make sure you have your erlang&#8217;s man path either in /etc/man.config or you&#8217;ve added it [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Well it&#8217;s actually as simple as running <em>M-x man RET module_name RET</em> in minibuffer.</p>
<p>I&#8217;m using Erlang setup from rpm on FC7 and by default man pages are located under <em>/usr/lib/erlang/man/ </em>so to use above command in minibuffer you must make sure you have your erlang&#8217;s man path either in <em>/etc/man.config</em> or you&#8217;ve added it into <em>MANPATH </em>with something like<em> export MANPATH=$MANPATH:/usr/lib/erlang/man </em>in your <em>.bash_profile </em>in case your Erlang man pages are not in location accessible by default.</p>
<p>You may also pass man path in minibuffer manually without changing /etc/man.config or MANPATH. In my case to view man page for <em>&#8216;lists&#8217; </em>module it will look like:</p>
<p><em>M-x man RET -M /</em><em>usr/lib/erlang/man lists </em><em>RET</em></p>
<p>I&#8217;ve customized some stuff for myself in emacs and made binding to F6 key so that whenever i set cursor on module name in my erlang buffer and press F6 i get man page in another window poped up describing module at point. So if you have in your code or in any buffer, for example:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">(em@localhost)2&gt; </span>lists:reverse([1, 2, 3]).</pre>
<p>you need to move cursor to <strong>lists</strong> and press F6. Quite handy if you consult man pages often, which is the case for me now as i learn Erlang.</p>
<p>Here is the corresponding elisp helper function and key binding from my dot emacs:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
(<span style="color:#00ffff;">defun</span> <span style="color:#87cefa;">get-erl-man</span> ()
  (interactive)
  (<span style="color:#00ffff;">let*</span> ((man-path <span style="color:#ffa07a;">"/usr/lib/erlang/man"</span>)
         (man-args (format <span style="color:#ffa07a;">"-M %s %s"</span> man-path (current-word))))
    (man man-args)))

(global-set-key [(f6)] (<span style="color:#00ffff;">lambda</span> () (interactive) (get-erl-man)))</pre>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/08/19/erlang-man-pages-in-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Htmlize your Erlang code buffer</title>
		<link>http://ruslanspivak.com/2007/08/18/htmlize-your-erlang-code-buffer/</link>
		<comments>http://ruslanspivak.com/2007/08/18/htmlize-your-erlang-code-buffer/#comments</comments>
		<pubDate>Sat, 18 Aug 2007 21:37:06 +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/08/18/htmlize-your-erlang-code-buffer/</guid>
		<description><![CDATA[Recently I&#8217;ve begun to make blog posts and now want to have highlighting of Erlang&#8217;s code(actually any code , be it Python, Lisp, etc) in my posts. I&#8217;m using The One True Editor and it goes without saying that i want to convert my erlang code into html from within emacs to have corresponding syntax [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Recently I&#8217;ve begun to make blog posts and now want to have highlighting of Erlang&#8217;s code(actually any code , be it Python, Lisp, etc) in my posts. I&#8217;m using <a href="http://www.gnu.org/software/emacs/">The One True Editor</a> and it goes without saying that i want to convert my erlang code into html from within emacs to have corresponding syntax coloring. <a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el">Htmlize package</a> to the rescue!</p>
<p>After you downloaded package and put</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
(add-to-list 'load-path <span style="color:#ffa07a;">"path_to_your_htmlize.el"</span>)
(<span style="color:#00ffff;">require</span> '<span style="color:#7fffd4;">htmlize</span>)</pre>
<p>into your dot emacs &#8211; you are ready to use it.</p>
<p><em>M-x htmlize-buffer<strong> </strong></em>or <em>M-x htmlize-region</em> will convert whole buffer or selected region with associated decorations into HTML( See <em>C-h a htmlize RET</em> for more functions). By default <a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el">htmlize</a> will make HTML output in <em>css</em> mode which is not very suitable for inserting into blog post as output includes also style sheets. Excerpt from htmlize documentation:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#ff4500;">;; </span><span style="color:#ff4500;">htmlize supports three types of HTML output, selected by setting
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">`</span><span style="color:#7fffd4;">htmlize-output-type</span><span style="color:#ff4500;">': `</span><span style="color:#7fffd4;">css</span><span style="color:#ff4500;">', `</span><span style="color:#7fffd4;">inline-css</span><span style="color:#ff4500;">', and `</span><span style="color:#7fffd4;">font</span><span style="color:#ff4500;">'.  In `</span><span style="color:#7fffd4;">css</span><span style="color:#ff4500;">'
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">mode, htmlize uses cascading style sheets to specify colors; it
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">generates classes that correspond to Emacs faces and uses &lt;span
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">class=FACE&gt;...&lt;/span&gt; to color parts of text.  In this mode, the
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">produced HTML is valid under the 4.01 strict DTD, as confirmed by
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">the W3C validator.  `</span><span style="color:#7fffd4;">inline-css</span><span style="color:#ff4500;">' is like `</span><span style="color:#7fffd4;">css</span><span style="color:#ff4500;">', except the CSS is
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">put directly in the STYLE attribute of the SPAN element, making it
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">possible to paste the generated HTML to other documents.  In `</span><span style="color:#7fffd4;">font</span><span style="color:#ff4500;">'
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">mode, htmlize uses &lt;font color="..."&gt;...&lt;/font&gt; to colorize HTML,
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">which is not standard-compliant, but works better in older
</span><span style="color:#ff4500;">;; </span><span style="color:#ff4500;">browsers.  `</span><span style="color:#7fffd4;">css</span><span style="color:#ff4500;">' mode is the default.
</span></pre>
<p><em>inline-css</em> is the type of output i want. This can be achieved by customizing <em>htmlize-output-type</em> variable with <em>M-x customize htmlize RET </em>or directly modifying dot emacs. Converting:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
(add-to-list 'load-path <span style="color:#ffa07a;">"path_to_your_htmlize.el"</span>)
(<span style="color:#00ffff;">require</span> '<span style="color:#7fffd4;">htmlize</span>)</pre>
<p>with htmlize-region and then applying <em>nxml-mode</em> to produced result gives:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ad00;">&lt;!</span><span style="color:#acacfc;">DOCTYPE</span> html <span style="color:#acacfc;">PUBLIC</span> <span style="color:#00ad00;">"</span><span style="color:#70f170;">-//W3C//DTD HTML 4.01//EN</span><span style="color:#00ad00;">"</span><span style="text-decoration:underline;">&gt;</span>
<span style="color:#00ad00;">&lt;!--</span><span style="font-style:italic;"> Created by htmlize-1.34 in inline-css mode. </span><span style="color:#00ad00;">--&gt;</span>
<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">html</span><span style="color:#00ad00;text-decoration:underline;">&gt;</span>
  <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">head</span><span style="color:#00ad00;">&gt;</span>
    <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">title</span><span style="color:#00ad00;">&gt;</span>.emacs<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">title</span><span style="color:#00ad00;">&gt;</span>
  <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">head</span><span style="color:#00ad00;">&gt;</span>
  <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">body</span> <span style="color:#acacfc;">style</span>=<span style="color:#00ad00;">"</span><span style="color:#70f170;">color: #ffffff; background-color: #000000;</span><span style="color:#00ad00;">"</span><span style="color:#00ad00;">&gt;</span>
    <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">pre</span><span style="color:#00ad00;">&gt;</span>
(add-to-list 'load-path <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">span</span> <span style="color:#acacfc;">style</span>=<span style="color:#00ad00;">"</span><span style="color:#70f170;">color: #ffa07a;</span><span style="color:#00ad00;">"</span><span style="color:#00ad00;">&gt;</span>"path_to_your_htmlize.el"<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">span</span><span style="color:#00ad00;">&gt;</span>)
(<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">span</span> <span style="color:#acacfc;">style</span>=<span style="color:#00ad00;">"</span><span style="color:#70f170;">color: #00ffff;</span><span style="color:#00ad00;">"</span><span style="color:#00ad00;">&gt;</span>require<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">span</span><span style="color:#00ad00;">&gt;</span> '<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">span</span> <span style="color:#acacfc;">style</span>=<span style="color:#00ad00;">"</span><span style="color:#70f170;">color: #7fffd4;</span><span style="color:#00ad00;">"</span><span style="color:#00ad00;">&gt;</span>htmlize<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">span</span><span style="color:#00ad00;">&gt;</span>)
<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">pre</span><span style="color:#00ad00;">&gt;</span>
  <span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">body</span><span style="color:#00ad00;">&gt;</span>
<span style="color:#00ad00;">&lt;</span><span style="color:#acacfc;">/</span><span style="color:#acacfc;">html</span><span style="color:#00ad00;">&gt;</span></pre>
<p>As you see I need to cut contents between <em>&lt;pre&gt;</em> <em>&lt;/pre&gt;</em> including<br />
<em>&lt;pre&gt;</em> tags itself to insert that then directly into blog post. After that i need manually copy/paste styles from <em>&lt;body&gt;</em>  tag into <em>&lt;pre&gt;</em> tag and also add font-size: 8pt to meet my taste. Being lazy i&#8217;ve implemented function  <em>my-htmlize-region</em> in elisp to make that job for me every time i press F5 key:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
(<span style="color:#00ffff;">defun</span> <span style="color:#87cefa;">my-htmlize-region</span> (beg end)
  <span style="color:#ffa07a;">"Htmlize region and put into &lt;pre&gt; tag style that is left in &lt;body&gt; tag
plus add font-size: 8pt"</span>
  (interactive <span style="color:#ffa07a;">"r"</span>)
  (<span style="color:#00ffff;">let*</span> ((buffer-faces (htmlize-faces-in-buffer))
         (face-map (htmlize-make-face-map (adjoin 'default buffer-faces)))
         (pre-tag (format
                   <span style="color:#ffa07a;">"&lt;pre style="%s font-size: 8pt"&gt;"</span>
                   (mapconcat #'identity (htmlize-css-specs
                                          (gethash 'default face-map)) <span style="color:#ffa07a;">" "</span>)))
         (htmlized-reg (htmlize-region-for-paste beg end)))
    (switch-to-buffer-other-window <span style="color:#ffa07a;">"*htmlized output*"</span>)
    <span style="color:#ff4500;">; </span><span style="color:#ff4500;">clear buffer
</span>    (kill-region (point-min) (point-max))
    <span style="color:#ff4500;">; </span><span style="color:#ff4500;">set mode to have syntax highlighting
</span>    (nxml-mode)
    (<span style="color:#00ffff;">save-excursion</span>
      (insert htmlized-reg))
    (<span style="color:#00ffff;">while</span> (re-search-forward <span style="color:#ffa07a;">"&lt;pre&gt;"</span> nil t)
      (replace-match pre-tag nil nil))
    (goto-char (point-min))))

(global-set-key [(f5)] (<span style="color:#00ffff;">lambda</span> (beg end)
                         (interactive <span style="color:#ffa07a;">"r"</span>) (my-htmlize-region beg end)))
</pre>
<p>So now selecting:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
(add-to-list 'load-path <span style="color:#ffa07a;">"path_to_your_htmlize.el"</span>)
(<span style="color:#00ffff;">require</span> '<span style="color:#7fffd4;">htmlize</span>)</pre>
<p>and pressing F5 i get this in another buffer:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">pre</span> <span style="color:#ACACFC;">style</span>=<span style="color:#00AD00;">"</span><span style="color:#70F170;">color: #ffffff; background-color: #000000; font-size: 8pt</span><span style="color:#00AD00;">"</span><span style="color:#00AD00;">&gt;</span>
(add-to-list 'load-path <span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">span</span> <span style="color:#ACACFC;">style</span>=<span style="color:#00AD00;">"</span><span style="color:#70F170;">color: #ffa07a;</span><span style="color:#00AD00;">"</span><span style="color:#00AD00;">&gt;</span>"path_to_your_htmlize.el"<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">/</span><span style="color:#ACACFC;">span</span><span style="color:#00AD00;">&gt;</span>)
(<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">span</span> <span style="color:#ACACFC;">style</span>=<span style="color:#00AD00;">"</span><span style="color:#70F170;">color: #00ffff;</span><span style="color:#00AD00;">"</span><span style="color:#00AD00;">&gt;</span>require<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">/</span><span style="color:#ACACFC;">span</span><span style="color:#00AD00;">&gt;</span> '<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">span</span> <span style="color:#ACACFC;">style</span>=<span style="color:#00AD00;">"</span><span style="color:#70F170;">color: #7fffd4;</span><span style="color:#00AD00;">"</span><span style="color:#00AD00;">&gt;</span>htmlize<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">/</span><span style="color:#ACACFC;">span</span><span style="color:#00AD00;">&gt;</span>)
<span style="color:#00AD00;">&lt;</span><span style="color:#ACACFC;">/</span><span style="color:#ACACFC;">pre</span><span style="color:#00AD00;">&gt;</span></pre>
<p>As you see it&#8217;s just what i want:<br />
<em>&lt;pre&gt;</em> tags with content and <em>&lt;pre&gt;</em> tag contains style from above mentioned  <em>&lt;body&gt;</em> tag plus <em>font-size</em> is added too.</p>
<p>Factorial in Erlang with syntax coloring as i have it in my Emacs:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#87cefa;">-module</span>(factorial).
<span style="color:#87cefa;">-export</span>([factorial/1, factorial_rec/1]).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">tail recursion
</span><span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">Num</span>) -&gt;
    factorial(<span style="color:#eedd82;">Num</span>, 1).

<span style="color:#87cefa;">factorial</span>(1, <span style="color:#eedd82;">Acc</span>) -&gt;<span style="color:#87cefa;"> </span><span style="color:#eedd82;">Acc</span>;
<span style="color:#87cefa;">factorial</span>(<span style="color:#eedd82;">Num</span>, <span style="color:#eedd82;">Acc</span>) -&gt;
    factorial(<span style="color:#eedd82;">Num</span>-1, <span style="color:#eedd82;">Num</span> * <span style="color:#eedd82;">Acc</span>).

<span style="color:#ff4500;">%% </span><span style="color:#ff4500;">recursion
</span><span style="color:#87cefa;">factorial_rec</span>(1) -&gt;<span style="color:#87cefa;"> </span>1;
<span style="color:#87cefa;">factorial_rec</span>(<span style="color:#eedd82;">Num</span>) -&gt;
    <span style="color:#eedd82;">Num</span> * factorial_rec(<span style="color:#eedd82;">Num</span>-1).
</pre>
<p>This is a joy of using Emacs.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/08/18/htmlize-your-erlang-code-buffer/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>More about binary search in Erlang</title>
		<link>http://ruslanspivak.com/2007/08/17/more-about-binary-search-in-erlang/</link>
		<comments>http://ruslanspivak.com/2007/08/17/more-about-binary-search-in-erlang/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 22:20:51 +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/08/17/more-about-binary-search-in-erlang/</guid>
		<description><![CDATA[In my previous post My Erlang binary search I&#8217;ve implemented binary search in Python and Erlang as small exercise for myself after reading Half-baked Ideas blog post. Good article pointing to specifics of implementation of that algorithm in Erlang was posted on Erlane&#8217;s blog which is in a nutshell: binary search in Erlang implemented on [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my previous post <a href="http://ruslanspivak.com/2007/08/15/my-erlang-binary-search/">My Erlang binary search</a> I&#8217;ve implemented binary search in Python and Erlang as small exercise for myself after reading <a href="http://erl.nfshost.com/wordpress/2007/08/11/binary-search/">Half-baked Ideas blog post</a>. Good article pointing to specifics of implementation of that algorithm in Erlang was posted on <a href="http://erlane-project.blogspot.com/2007/08/binary-search-ultimate-test.html">Erlane&#8217;s blog</a> which is in a nutshell: binary search in Erlang implemented on list data structure is less efficient than linear search.</p>
<p>So to grasp why linear search which is O(N) is faster in Erlang then binary search which is in theory O(logN) go and read that <a href="http://erlane-project.blogspot.com/2007/08/binary-search-ultimate-test.html">article.</a><br />
In Python list data structure (I&#8217;m talking here about CPython) is implemented as mutable and extensible vector of references to objects(we might say array of pointers), here is the excerpt from listobject.h:</p>
<pre style="color:#ffffff;background-color:#000000;font-size:8pt;">
<span style="color:#00ffff;">typedef</span> <span style="color:#00ffff;">struct</span> {
    PyObject_VAR_HEAD
    <span style="color:#ff4500;">/* </span><span style="color:#ff4500;">Vector of pointers to list elements.  list[0] is ob_item[0], etc. </span><span style="color:#ff4500;">*/</span>
    <span style="color:#98fb98;">PyObject</span> **<span style="color:#eedd82;">ob_item</span>;

    <span style="color:#ff4500;">/* </span><span style="color:#ff4500;">ob_item contains space for 'allocated' elements.  The number
     * currently in use is ob_size.
     * Invariants:
     *     0 &lt;= ob_size &lt;= allocated
     *     len(list) == ob_size
     *     ob_item == NULL implies ob_size == allocated == 0
     * list.sort() temporarily sets allocated to -1 to detect mutations.
     *
     * Items must normally not be NULL, except during construction when
     * the list is not yet visible outside the function that builds it.
     </span><span style="color:#ff4500;">*/</span>
    <span style="color:#98fb98;">Py_ssize_t</span> <span style="color:#eedd82;">allocated</span>;
} <span style="color:#eedd82;">PyListObject</span>;</pre>
<p>So <a href="http://ruslanspivak.com/2007/08/15/my-erlang-binary-search/">Python version</a> works as expected regarding efficiency.  While my goal  of implementing  binary search algorithm in Erlang was learning Erlang and not particulary developing optimized version of that algorithm or using another one it&#8217;s always good to pick <a href="http://erlane-project.blogspot.com/2007/08/binary-search-ultimate-test.html">new tricks of the trade</a> along the way and for me the issue with my implementation of <a href="http://ruslanspivak.com/2007/08/15/my-erlang-binary-search/">binary search in Erlang</a> just boils down to: your experience and know your tools (language, os, editor(emacs <img src='http://ruslanspivak.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , etc )</p>
<p>As <em>Aldous Huxly</em> said: <em><span class="body">&#8220;Experience is not what happens to you; it&#8217;s what you do with what happens to you.&#8221;. </span></em><span class="body">So make errors (not many <img src='http://ruslanspivak.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  in your programs, fix them and gain new experience with your new language. </span></p>
<p><span class="body"><a href="http://erlane-project.blogspot.com/2007/08/from-java-to-erlang-or-how-to-structure.html">Another post by Erlane team</a></span>  is good reading describing paradigm shifts when you are newbie and are on your own way to Erlang/OTP wizardry.</p>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>http://ruslanspivak.com/2007/08/17/more-about-binary-search-in-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: ruslanspivak.com @ 2013-05-25 00:30:50 -->