<?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>mnemonikk.org &#187; emacs</title>
	<atom:link href="http://mnemonikk.org/category/emacs/feed/" rel="self" type="application/rss+xml" />
	<link>http://mnemonikk.org</link>
	<description>My life with the machine</description>
	<lastBuildDate>Tue, 06 Jul 2010 13:27:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Setting default-directory for Mercurial MQ patches in Emacs</title>
		<link>http://mnemonikk.org/2010/07/06/setting-default-directory-for-mercurial-mq-patches-in-emacs/</link>
		<comments>http://mnemonikk.org/2010/07/06/setting-default-directory-for-mercurial-mq-patches-in-emacs/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 13:27:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[mercurial]]></category>

		<guid isPermaLink="false">http://mnemonikk.org/?p=106</guid>
		<description><![CDATA[Emacs&#8217; diff-mode is a great tool to work with patches. You can move inside a patch by files or by hunks, it highlights the changes in each line and you can apply and revert individual hunks. However, diff-mode doesn&#8217;t work out-of-the-box with Mercurial&#8217;s MQ extension. To make it work, we first have to make Emacs [...]]]></description>
			<content:encoded><![CDATA[<p>Emacs&#8217; diff-mode is a great tool to work with patches. You can move inside a patch by files or by hunks, it highlights the changes in each line and you can apply and revert individual hunks. However, diff-mode doesn&#8217;t work out-of-the-box with Mercurial&#8217;s MQ extension. To make it work, we first have to make Emacs recognize an MQ patch automatically like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'auto-mode-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.hg/patches/&quot;</span> <span style="color: #66cc66;">.</span> diff-mode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>However, applying and reverting hunks will not work, because Emacs can not find the files mentioned in the patch, as it assumes that the paths are more or less relative to where the patch file lies. To fix this we add a function to diff-mode-hook:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mq-patch-set-default-directory <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;.hg&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reverse</span> <span style="color: #66cc66;">&#40;</span>split-string default-directory <span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> default-directory <span style="color: #66cc66;">&#40;</span>expand-file-<span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>concat default-directory <span style="color: #ff0000;">&quot;../../&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'diff-mode-hook 'mq-patch-set-default-directory<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>A possible usage scenario for diff-mode with MQ could be that you want to apply parts of an (unapplied) MQ patch to your working copy, maybe because the patch as a whole doesn&#8217;t work anymore or you want to throw it away and only intend to keep a few bits of it. </p>
<p>Whatever you&#8217;re doing with this, I hope you&#8217;ll find it useful. </p>
]]></content:encoded>
			<wfw:commentRss>http://mnemonikk.org/2010/07/06/setting-default-directory-for-mercurial-mq-patches-in-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Showing the current directory in Emacs&#8217; mode line</title>
		<link>http://mnemonikk.org/2008/05/23/showing-the-current-directory-in-emacs-mode-line/</link>
		<comments>http://mnemonikk.org/2008/05/23/showing-the-current-directory-in-emacs-mode-line/#comments</comments>
		<pubDate>Fri, 23 May 2008 13:28:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://mnemonikk.org/?p=7</guid>
		<description><![CDATA[Today I got tired of always looking up, where all these little files named &#8220;_show.rhtml&#8221;, &#8220;_list.rhtml&#8221; and their ilk are living, and patched the emacs mode line to include the last element of the current buffer&#8217;s directory. http://www.emacswiki.org/cgi-bin/wiki/ModeLineDirtrack describes something very similar, but it repeats the whole mode line definition of mode-line-format, which might break [...]]]></description>
			<content:encoded><![CDATA[<p>Today I got tired of always looking up, where all these little files named &#8220;_show.rhtml&#8221;, &#8220;_list.rhtml&#8221; and their ilk are living, and patched the emacs mode line to include the last element of the current buffer&#8217;s directory. <a title="This page on EmacsWiki" href="http://www.emacswiki.org/cgi-bin/wiki/ModeLineDirtrack">http://www.emacswiki.org/cgi-bin/wiki/ModeLineDirtrack</a> describes something very similar, but it repeats the whole mode line definition of mode-line-format, which might break with the next version of Emacs. It&#8217;s much cleaner to add to a variable that is used in the mode line:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>setq-default mode-line-buffer-identification
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span>
               '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span>replace-regexp-in-string <span style="color: #ff0000;">&quot;^.*/<span style="color: #000099; font-weight: bold;">\\</span>(.*<span style="color: #000099; font-weight: bold;">\\</span>)/&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>1/&quot;</span> default-directory<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
               mode-line-buffer-identification<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mnemonikk.org/2008/05/23/showing-the-current-directory-in-emacs-mode-line/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Live syntax-checking JavaScript with Emacs</title>
		<link>http://mnemonikk.org/2008/05/06/live-syntax-checking-javascript-with-emacs/</link>
		<comments>http://mnemonikk.org/2008/05/06/live-syntax-checking-javascript-with-emacs/#comments</comments>
		<pubDate>Tue, 06 May 2008 08:00:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[emacs]]></category>
		<category><![CDATA[flymake]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://mnemonikk.org/?p=6</guid>
		<description><![CDATA[There are quite some options for doing live syntax checks from within Emacs. A good one is using Steve Yegge&#8217;s relatively new js2-mode for javascript editing which has a javascript parser built in. But that is not what this blog post will be about. The other option is to use flymake with some command line [...]]]></description>
			<content:encoded><![CDATA[<p>There are quite some options for doing live syntax checks from within Emacs. A good one is using Steve Yegge&#8217;s relatively new <a href="http://code.google.com/p/js2-mode/">js2-mode</a> for javascript editing which has a javascript parser built in. But that is not what this blog post will be about.</p>
<p>The other option is to use flymake with some command line javascript syntax checker. Two possible syntax checkers are Mozilla&#8217;s stand alone SpiderMonkey interpreter &#8220;smjs&#8221; with the &#8220;-s&#8221; (strict) option and Douglas Crockford&#8217;s JsLint running under Rhino. There are reasons to use each of them:</p>
<ul>
<li>SpiderMonkey is the javascript engine currently used in Firefox. So, if it can not parse your javascript, neither can Firefox.</li>
<li>JsLint does more than just syntax checking, it also checks for bad style and possible programming errors, like missing semicolons and braces, assignment in an if conditions. This can be good, but it&#8217;s also a hinderance when you&#8217;re working on code that was not written to make JsLint happy and you don&#8217;t want to refactor it. Furthermore, the style enforced by JsLint might differ from your programming style, as it says on the web page: &#8220;Warning! JSLint will hurt your feelings.&#8221;<br />
I, for instance, like to leave out the braces after an if statement if the executed block is itself just one statement &#8211; it takes up less space, and if I ever want to add to this block Emacs will indent the code correctly, so I won&#8217;t forget to add the braces afterwards. However, maybe JsLint is right, because you sometimes have to collaborate with people that don&#8217;t use Emacs &#8211; maybe I should do them the favor.</li>
</ul>
<h3>Setting up SpiderMonkey</h3>
<p>On an Ubuntu system the stand alone SpiderMonkey interpreter is provided by the package &#8220;spidermonkey-bin&#8221;. SpiderMonkey has the problem that it is just the javascript engine, but does not include the DOM, and there is no global window object. So if your javascript code does more than just defining functions you need to do some mocking. This is fortunately very easy to do.</p>
<p>The code I wanted to check also installs event handlers with prototype&#8217;s Event.observe method, so I had to mock that, too:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// fake the global window object</span>
<span style="color: #003366; font-weight: bold;">var</span> window <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> Event <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> observe<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You will most probably need to extend this for your code. Now you can do a syntax check from the command line like this:</p>
<pre>smjs -s -e "(load '/path/to/mock.js')" file-to-check.js</pre>
<h3>Setting up JsLint</h3>
<p>JsLint is itself written in javascript. To use it from the command line you need the javascript interpreter Rhino. It is, not surprisingly, provided on Ubuntu by the package &#8220;rhino&#8221;. JsLint for rhino is avaiable <a href="http://www.jslint.com/rhino/index.html">here</a>. To check a javascript file with rhino, just call it like this:</p>
<pre>rhino /path/to/jslint.js file-to-check.js</pre>
<h3>Integrating both into Emacs</h3>
<p>The following code allows you to do live syntax checking from within Emacs with either SpiderMonkey or JsLint. You can switch between them using &#8220;C-c t&#8221;. The code uses a circular list structure to keep the list of available methods, just for the heck of it.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-js-method 'spidermonkey<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> flymake-js-toggle-method <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>methods '#<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#40;</span>spidermonkey jslint <span style="color: #66cc66;">.</span> #<span style="color: #cc66cc;">1</span>#<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-js-method <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cadr</span> <span style="color: #66cc66;">&#40;</span>memq flymake-js-method methods<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>flymake-start-syntax-check<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> flymake-js-init <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>temp-file <span style="color: #66cc66;">&#40;</span>flymake-init-create-temp-buffer-copy
		     'flymake-create-temp-inplace<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>local-file <span style="color: #66cc66;">&#40;</span>file-relative-<span style="color: #b1b100;">name</span>
		      temp-file
		      <span style="color: #66cc66;">&#40;</span>file-name-directory buffer-file-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq</span> flymake-js-method 'spidermonkey<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;smjs&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;-s&quot;</span> <span style="color: #ff0000;">&quot;-e&quot;</span> <span style="color: #66cc66;">&#40;</span>format <span style="color: #ff0000;">&quot;load('%s')&quot;</span> <span style="color: #66cc66;">&#40;</span>expand-file-<span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;/path/to/mock.js&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> local-file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;rhino&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>expand-file-<span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;/path/to/jslint.js&quot;</span><span style="color: #66cc66;">&#41;</span> local-file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>eval-after-load <span style="color: #ff0000;">&quot;flymake&quot;</span>
  '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
     <span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'flymake-allowed-file-name-masks
                  '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.js<span style="color: #000099; font-weight: bold;">\\</span>(on<span style="color: #000099; font-weight: bold;">\\</span>)?$&quot;</span> flymake-js-init flymake-simple-cleanup flymake-get-real-file-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'flymake-err-line-patterns
                  '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;^<span style="color: #000099; font-weight: bold;">\\</span>(.+<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\\</span>([0-9]+<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\:</span> <span style="color: #000099; font-weight: bold;">\\</span>(SyntaxError<span style="color: #000099; font-weight: bold;">\:</span>.+<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\:</span>$&quot;</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'flymake-err-line-patterns
                  '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;^<span style="color: #000099; font-weight: bold;">\\</span>(.+<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\\</span>([0-9]+<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\:</span> <span style="color: #000099; font-weight: bold;">\\</span>(strict warning: trailing comma.+<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\:</span>$&quot;</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'flymake-err-line-patterns
                   '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;^Lint at line <span style="color: #000099; font-weight: bold;">\\</span>([[:digit:]]+<span style="color: #000099; font-weight: bold;">\\</span>) character <span style="color: #000099; font-weight: bold;">\\</span>([[:digit:]]+<span style="color: #000099; font-weight: bold;">\\</span>): <span style="color: #000099; font-weight: bold;">\\</span>(.+<span style="color: #000099; font-weight: bold;">\\</span>)$&quot;</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> my-js-setup-flymake <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flymake-mode <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>local-set-key <span style="color: #66cc66;">&#40;</span>kbd <span style="color: #ff0000;">&quot;C-c n&quot;</span><span style="color: #66cc66;">&#41;</span> 'flymake-goto-next-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>local-set-key <span style="color: #66cc66;">&#40;</span>kbd <span style="color: #ff0000;">&quot;C-c p&quot;</span><span style="color: #66cc66;">&#41;</span> 'flymake-goto-prev-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>local-set-key <span style="color: #66cc66;">&#40;</span>kbd <span style="color: #ff0000;">&quot;C-c t&quot;</span><span style="color: #66cc66;">&#41;</span> 'flymake-js-toggle-method<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>add-hook 'javascript-mode-hook 'my-js-setup-flymake<span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>The result</h3>
<p>And here is what it looks like with SpiderMonkey:</p>
<p><img src="http://mnemonikk.org/images/emacs-flymake-spidermonkey.png" alt="Emacs with flymake running spidermonkey" width="598" height="135" /></p>
<p>And here an example of some code and what opinion JsLint has about it:</p>
<p><img src="http://mnemonikk.org/images/emacs-flymake-jslint.png" alt="Emacs with flymake running jslint" width="474" height="182" /></p>
]]></content:encoded>
			<wfw:commentRss>http://mnemonikk.org/2008/05/06/live-syntax-checking-javascript-with-emacs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
