<?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>George MacKerron: code blog &#187; Stata</title>
	<atom:link href="http://blog.mackerron.com/category/stata/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mackerron.com</link>
	<description>GIS, software development, and other snippets</description>
	<lastBuildDate>Fri, 03 Feb 2012 18:35:21 +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>Tab completion for Stata variables in TextMate</title>
		<link>http://blog.mackerron.com/2010/02/06/stata-textmate-tab-completion/</link>
		<comments>http://blog.mackerron.com/2010/02/06/stata-textmate-tab-completion/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 16:59:33 +0000</pubDate>
		<dc:creator>George</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Stata]]></category>

		<guid isPermaLink="false">http://blog.mackerron.com/?p=264</guid>
		<description><![CDATA[I recently switched to TextMate for editing Stata .do files: unlike Stata&#8217;s built-in editor on the Mac, it has syntax highlighting and other goodies via Timothy Beatty&#8217;s bundle (now hosted by Dan Byler). One thing it doesn&#8217;t have, though, is tab completion. Or rather, it didn&#8217;t, until now. What does it do? It&#8217;s the same [...]]]></description>
			<content:encoded><![CDATA[<p>I recently switched to <a href="http://macromates.com/">TextMate</a> for editing Stata .do files: unlike Stata&#8217;s built-in editor on the Mac, it has syntax highlighting and other goodies via <a href="http://bylr.net/3/2010/10/stata-bundle-for-textmate/">Timothy Beatty&#8217;s bundle</a> (now hosted by Dan Byler). </p>

<p>One thing it doesn&#8217;t have, though, is <a href="http://en.wikipedia.org/wiki/Command_line_completion">tab completion</a>. Or rather, it didn&#8217;t, until now.</p>

<p><span id="more-264"></span></p>

<h2>What does it do?</h2>

<p>It&#8217;s the same thing you get in Stata&#8217;s Command window: you type part of a variable name, press tab, and the name is completed as far as it can be without ambiguity. </p>

<p>For example, I type &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.13.42.png" alt="tab fr" height="29" width="80" /></p>

<p>&#8212; I press tab &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.13.51.png" alt="tab fruit_and_veg__q" height="29" width="177" /></p>

<p>&#8212; and the variable name is completed, leaving the cursor one space to the right and ready to keep typing. (Note that my use of Stata&#8217;s <code>tab[le]</code> command in these examples is an unfortunate coincidence and has nothing to do with tab completion! I could just as well have written <code>regress fr</code>. Sorry.)</p>

<p>In fact, <em>proper</em> tab completion (as found in Unixy shells, for file names, but not in Stata&#8217;s Command window) goes a little further than this: if you press tab at the end of an ambiguous name stem, it prompts you with all the possible endings.</p>

<p>And so it is with this TextMate bundle addition. I type &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.14.11.png" alt="tab pol" height="29" width="88" /></p>

<p>&#8212; I press tab &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.14.21.png" alt="tab politic" height="28" width="123" /></p>

<p>&#8212; and the name is completed up to the next ambiguity. So I press tab again &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.14.33.png" alt="List of suggestions" height="104" width="310" /></p>

<p>&#8212; and I get a tooltip list of options. I add an &#8216;s&#8217; (just enough to pick one option out) &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.15.57.png" alt="tab politics" height="30" width="134" /></p>

<p>&#8212; press tab again &#8212;</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-5-Feb-18.14.49.png" alt="tab politics_left_right__q" height="28" width="259" /></p>

<p>&#8212; and bingo!</p>

<h2>How does it work?</h2>

<p>Each time you press tab, the bundle asks Stata to run a simple .do file; this exports your current data set with an <code>if 0</code> condition, meaning that all the actual data is excluded, and we just get the variable names. (For detail, check the Ruby script).</p>

<p>A slightly unfortunate effect of this is that your Stata Results window (and log, if you&#8217;re keeping one) fills up with these <code>outsheet</code> commands. I hope that&#8217;s not a deal-breaker. If only Stata would provide some basic AppleScript support (please?) I might be able to do something about this.</p>

<h2>How do I get it?</h2>

<p><em><strong>Update!</strong>  As per comments below, you can now download the <a href="http://bylr.net/3/2010/10/stata-bundle-for-textmate/">bundle with tab completion rolled in</a>, so you can probably stop reading here.</em></p>

<p>At some point I may roll this into a new bundle file to make installation easier (there are also a few fixes I&#8217;ve made to the original bundle). But in the meantime, you can add it to the bundle yourself.</p>

<p>Open the bundle editor (Bundles &gt; Bundle Editor &gt; Show Bundle Editor) and select the Stata bundle. Click the &#8216;+&#8217; button and select &#8216;New Command&#8217;. Give the command a name. Then, on the right, select Save: Nothing, Input: Selected Text or Word and Output: Replace Selected Text. Choose Activation: Key Equivalent: and a hotkey (I use Ctrl-Tab).</p>

<p>Then paste in the following Ruby script:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'TM_SUPPORT_PATH'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/lib/exit_codes.rb'</span>
dofile = <span style="color:#996600;">'/tmp/textmatestatavars.do'</span>
varfile = <span style="color:#996600;">'/tmp/textmatestatavars.tsv'</span>
&nbsp;
stem = ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'TM_CURRENT_WORD'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
stem = <span style="color:#996600;">''</span> <span style="color:#9966CC; font-weight:bold;">if</span> stem.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">||</span> stem.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\s$<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
stem = stem.<span style="color:#9900CC;">lstrip</span>
&nbsp;
TextMate.<span style="color:#9900CC;">exit_discard</span> <span style="color:#9966CC; font-weight:bold;">if</span> stem.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>^a<span style="color:#006600; font-weight:bold;">-</span>z0<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span>_<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>im<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>dofile, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> 
  f.<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>set notifyuser off\noutsheet <span style="color:#008000; font-style:italic;">#{stem}* using &quot;#{varfile}&quot; if 0, noquote\n}</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">unlink</span><span style="color:#006600; font-weight:bold;">&#40;</span>varfile<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>varfile<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#996600;">`osascript -e 'tell application &quot;Stata&quot; to open POSIX file &quot;#{dofile}&quot;'`</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>varfile<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">'No variables found'</span>	
  TextMate.<span style="color:#9900CC;">exit_show_tool_tip</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
vars = <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>varfile<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> f.<span style="color:#9900CC;">read</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> vars.<span style="color:#9900CC;">length</span> == <span style="color:#006666;">1</span>
  <span style="color:#CC0066; font-weight:bold;">print</span> vars.<span style="color:#9900CC;">first</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">' '</span>
  <span style="color:#CC0066; font-weight:bold;">exit</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
v1, v2 = vars.<span style="color:#9900CC;">sort</span>.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>, <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">sort_by</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> v.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
common_prefix = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>...<span style="color:#006600; font-weight:bold;">&#40;</span>v1.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">break</span> v1<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>, i<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> v1<span style="color:#006600; font-weight:bold;">&#91;</span>i, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> == v2<span style="color:#006600; font-weight:bold;">&#91;</span>i, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
common_prefix = v1 <span style="color:#9966CC; font-weight:bold;">unless</span> common_prefix.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> stem == common_prefix
  <span style="color:#CC0066; font-weight:bold;">print</span> vars.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  TextMate.<span style="color:#9900CC;">exit_show_tool_tip</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
  <span style="color:#CC0066; font-weight:bold;">print</span> common_prefix
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>It should look like this:</p>

<p><img src="/wp-content/uploads/2010/02/Screen-shot-2010-02-06-at-16.52.53.png" alt="Screenshot" height="557" width="755" /></p>]]></content:encoded>
			<wfw:commentRss>http://blog.mackerron.com/2010/02/06/stata-textmate-tab-completion/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.mackerron.com/category/stata/feed/ ) in 0.33075 seconds, on Feb 5th, 2012 at 2:54 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 5th, 2012 at 3:54 am UTC -->
