<?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>ColonelPanic</title>
	<atom:link href="http://colonelpanic.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://colonelpanic.net</link>
	<description>Dumping our corps so you don&#039;t have to</description>
	<lastBuildDate>Tue, 07 Sep 2010 21:58:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Get help for browser plugins and firebreath</title>
		<link>http://colonelpanic.net/2010/09/get-help-for-browser-plugins-and-firebreath/</link>
		<comments>http://colonelpanic.net/2010/09/get-help-for-browser-plugins-and-firebreath/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 21:58:10 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=338</guid>
		<description><![CDATA[Having problems? The comments on this blog are full of questions particularly about browser plugins, and even more specifically about NPAPI plugins.  Now, I don&#8217;t mind helping people out at all; that&#8217;s how we all learn.  However, in an effort to help everyone help each other, I wanted to bring the IRC chat room for [...]]]></description>
			<content:encoded><![CDATA[<h2>Having problems?</h2>
<p>The comments on this blog are full of questions particularly about browser plugins, and even more specifically about NPAPI plugins.  Now, I don&#8217;t mind helping people out at all; that&#8217;s how we all learn.  However, in an effort to help everyone help each other, I wanted to bring the IRC chat room for FireBreath to everyone&#8217;s attention.</p>
<h2>IRC Chatroom</h2>
<p>To date, there are about 4 of us who are usually in the chatroom, and we discuss more than just FireBreath.  FireBreath is a project that combines experience with NPAPI, ActiveX/COM, and cross platform development in a fairly unique way.  We benefit from interaction with anyone working on any of these technologies.  What I&#8217;m trying to say is, feel free to bring your plugin questions of *any* type to that room.  We won&#8217;t do your work for you and we don&#8217;t promise to have an answer to every question, but we have a pretty decent understanding of the subject between us.</p>
<h2>Where it is</h2>
<p>The server is irc.freenode.net.  The channel is #firebreath.</p>
<p>See you there.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/09/get-help-for-browser-plugins-and-firebreath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic HTML content in iTunes LP / iTunes Extras</title>
		<link>http://colonelpanic.net/2010/08/dynamic-html-content-in-itunes-lp-itunes-extras/</link>
		<comments>http://colonelpanic.net/2010/08/dynamic-html-content-in-itunes-lp-itunes-extras/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 03:58:41 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=332</guid>
		<description><![CDATA[Recently, I have been doing some iTunes LP projects for one of my clients. One of the things that immediately struck me as a huge potential time saver was automating the creation of HTML elements for lists of items, ie lists of song and video tracks. Often I can splice images such that their position [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I have been doing some iTunes LP projects for one of my clients. One of the things that immediately struck me as a huge potential time saver was automating the creation of HTML elements for lists of items, ie lists of song and video tracks. Often I can splice images such that their position can be controlled with a single CSS rule, and explicitly creating all the <em>image-fader</em> DIVs for the individual songs or videos in the HTML views is terribly redundant.</p>
<h2>The Goal</h2>
<p>Create HTML elements dynamically which transition appropriately when the view controller appears.</p>
<h2>The Problem</h2>
<p>The shipping version of TuneKit.js doesn&#8217;t give you a chance to append elements to the DOM before viewDidAppear is invoked (which is after transitionToController).</p>
<p><span id="more-332"></span></p>
<h2>Initial Attempts</h2>
<p>I first tried appending the child elements (using TKUtils.buildElement in a loop over appData.songs) inside the viewDidAppear callback. Obviously, the problem here is that the elements don&#8217;t exist the first time the view controller appears, so can&#8217;t nicely fade in via your defined CSS animations.</p>
<p>Next, I tried moving the logic that create the HTML elements into viewWillAppear. The problem here was that this method was called before the desired view controller is added to the DOM &#8212; so the element I need to append the children to doesn&#8217;t exist.</p>
<h2>The Solution</h2>
<p>The solution was actually pretty simple, and works like a charm; you just need to be willing to edit TuneKit.js.<br />
<!--more--><br />
Before:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// pre-transition methods</span>
controller._viewWillAppear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
controller.<span style="color: #660066;">viewWillAppear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// add it to the tree</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">view</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>controller.<span style="color: #660066;">view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// transition</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>previous_controller <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">transitionToController</span><span style="color: #009900;">&#40;</span>previous_controller<span style="color: #339933;">,</span> controller<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>After:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// add it to the tree</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">view</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>controller.<span style="color: #660066;">view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// pre-transition methods</span>
controller._viewWillAppear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
controller.<span style="color: #660066;">viewWillAppear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// transition</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>previous_controller <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">transitionToController</span><span style="color: #009900;">&#40;</span>previous_controller<span style="color: #339933;">,</span> controller<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Conclusion</h2>
<p>Now that TuneKit appends the view controller to the DOM before calling the viewWillAppear callback, your dynamic elements can be created before the view appears and transitionToController is invoked.</p>
<p>Done.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/08/dynamic-html-content-in-itunes-lp-itunes-extras/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Browser Plugins vs Extensions &#8211; the difference</title>
		<link>http://colonelpanic.net/2010/08/browser-plugins-vs-extensions-the-difference/</link>
		<comments>http://colonelpanic.net/2010/08/browser-plugins-vs-extensions-the-difference/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 13:42:01 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[ActiveX]]></category>
		<category><![CDATA[Browser Plugin Development]]></category>
		<category><![CDATA[NPAPI]]></category>
		<category><![CDATA[bho]]></category>
		<category><![CDATA[browser plugin]]></category>
		<category><![CDATA[extension]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=312</guid>
		<description><![CDATA[Overloaded terms One of the least understood concepts in the Browser Plugin world is &#8212; browser plugins.  What they are, and even more: what they are not.  Probably at least once a week I answer a question somewhere on a forum or on the comments on this blog and say &#8220;You can&#8217;t do that with [...]]]></description>
			<content:encoded><![CDATA[<h2>Overloaded terms</h2>
<p>One of the least understood concepts in the Browser Plugin world is &#8212; browser plugins.  What they are, and even more: what they are not.  Probably at least once a week I answer a question somewhere on a forum or on the comments on this blog and say &#8220;You can&#8217;t do that with a plugin; you need an extension&#8221;.</p>
<p>Many people confuse plugins and extensions in the browser world. They sound like they should be the same thing, but in fact they are very different.</p>
<h2>What you can do with a plugin</h2>
<p>A plugin is, quite simply, a third party library that &#8220;plugs in&#8221; to the browser that can be embedded inside a web page using an &lt;embed&gt; tag or a &lt;object&gt; tag.  Many of them then draw pretty pictures or animations, though that&#8217;s not required.  Many allow you to talk to them through javascript, though that&#8217;s not required.  In short, a plugin affects only a specific page in which it is placed.</p>
<p>Examples of common plugins include:</p>
<ul>
<li><span id="more-312"></span>Macromedia Flash</li>
<li>Microsoft Silverlight</li>
<li>Apple Quicktime</li>
<li>Adobe Reader (at least this includes a plugin in addition to the rest of the application)</li>
</ul>
<p>Some plugins respond to a mime type and can run in place of the page, such as Adobe Reader, which allows you to view PDF files in your web browser.  Again, however, it affects only that page, and no others.  In Firefox, Chrome, Opera, and Safari, these plugins are usually called &#8220;NPAPI plugins&#8221;, since they are written using the <a title="NPAPI on wikipedia" href="http://en.wikipedia.org/wiki/NPAPI">NPAPI</a>.  In Safari, you could also use a <a href="http://developer.apple.com/mac/library/documentation/InternetWeb/Conceptual/WebKit_PluginProgTopic/WebKitPluginTopics.html">Webkit Plugin</a>.  In Internet Explorer you would do this with an ActiveX Control.</p>
<h2>What a plugin can not do</h2>
<p>Plugins have to either handle a mime type or be embedded into a web page.  They don&#8217;t get put there automatically. They don&#8217;t create toolbars.  They don&#8217;t affect browser menus.  They don&#8217;t know about tabs, or even about other pages in the same browser process.  They don&#8217;t make coffee, tea, or hot chocolate, though they could be written to control an external appliance to do so if you really wanted.  They don&#8217;t automatically process the content of every web page loaded.</p>
<p>&#8220;But wait!&#8221; I hear you all cry.  &#8220;We have seen these magical things done (well, except the hot chocolate machine)&#8221;.</p>
<p>&#8220;Yes,&#8221; I reply.  &#8220;Those are extensions.&#8221;</p>
<h2>Extensions</h2>
<p>Extensions, or &#8220;add-ons&#8221;, can often do these magical things.  They can add onto the browser UI, process pages that the browser loads.  They do all sorts of things.  However, they are not the same thing as plugins; they affect the web browser itself, not really the page.  They may affect a page as well, of course.  In fact, you can even put a plugin inside an extension, at least in Firefox.  However, if you write an extension for Firefox, you&#8217;ll probably have to write it again for Chrome, Safari, and IE&#8230; one for each.  Perhaps there is a framework out there for writing portable extensions, though.  It&#8217;s not really my area of expertise, so I don&#8217;t know.</p>
<p>Examples of extensions / add-ons:</p>
<ul>
<li><a href="http://www.downthemall.net/">DownThemAll</a> for Firefox</li>
<li><a href="http://getfirebug.com/" target="_blank">Firebug</a> for Firefox</li>
<li><a href="https://chrome.google.com/extensions/detail/kcnhkahnjcbndmmehfkdnkjomaanaooo" target="_blank">Google Voice extension</a> for Chrome</li>
<li><a href="http://kanab.hys.cz/let-there-be-comic-sans/">Let there be Comic Sans</a> for Safari</li>
</ul>
<p>And many, many, many more.  Extensions can be written in different languages depending on the browser.  In Firefox you can write them in C++ or javascript, and on Internet Explorer you write them as special ActiveX controls called <a href="http://en.wikipedia.org/wiki/Browser_Helper_Object">Browser Helper Objects</a>, or BHOs.</p>
<h2>An extension could contain a plugin, but a plugin can&#8217;t contain an extension</h2>
<p>One very common way to install a plugin in Firefox is to use a .xpi file.  <strong><em>This is an extension</em></strong>.  It may have a NPAPI plugin inside of it, but it is still an extension.  It will not work on other web browsers, even though your NPAPI plugin may work perfectly fine on Chrome and Safari if installed in another way.</p>
<p>Conversely, a plugin by nature does not include an extension.</p>
<h2>FireBreath</h2>
<p>Nearly a year ago now I began a project called FireBreath, which is a cross-platform browser plugin architecture.  FireBreath is, frankly, awesome. You can literally get a plugin up and going in under 10 minutes if you already have everything installed.  That means you can spend your time doing other more important things, like actually developing your functionality, rather than spending all your time figuring out how to deal with the plugin APIs.</p>
<p>That said, FireBreath is a cross-platform browser <span style="text-decoration: underline;"><em><strong>PLUGIN</strong></em></span> framework.  It does not create extensions.  It will never be able to create extensions.  If you need an extension, you need something else.</p>
<h2>When not to use a plugin</h2>
<p>Finally, and this is a topic that people seem to think is a little strange coming from me, there are many cases when it is not a good idea to use a browser plugin.  In fact, a good rule of thumb is that if you can do what you need to do without one, don&#8217;t use one.</p>
<p>Why?</p>
<ul>
<li>There are no really good ways to install browser plugins that will work 100% of the time
<ul>
<li>XPI install only works on one profile in firefox</li>
<li>CAB install only works in Internet Explorer and is sometimes a little unreliable</li>
<li>ClickOnce works only in Windows and only if .Net is installed</li>
<li>Java installers only work reliably and consistently on Mac OS</li>
<li>MSI or EXE installers while being the best method IMHO require a little more work on the part of the user, and the average user is&#8230; less savvy than you are, shall we say.</li>
</ul>
</li>
<li>Plugins have to take into account a large range of hardware types
<ul>
<li>If you use graphics libraries such as OpenGL or DirectX keep in mind that every graphics card works a little differently with these technologies.  You will run into weird cases where your plugin doesn&#8217;t work on someone&#8217;s computer.  If your luck is like mine, that computer will belong to an executive at a fortune 500 company (yes, that happened to me.  no, it wasn&#8217;t fun)</li>
</ul>
</li>
<li>Plugins don&#8217;t work until they have been installed
<ul>
<li>This is a major limiting factor to uptake of your site.  Many users don&#8217;t want to install yet another plugin on their machine, and many get lost in the incredibly complicated task of downloading and running the installer.</li>
</ul>
</li>
</ul>
<h2>Moving on</h2>
<p>If all of those things haven&#8217;t convinced you that what you want isn&#8217;t actually a browser plugin &#8212; great!  That&#8217;s why I&#8217;m still posting about this stuff.  Despite all the reasons not to use a plugin, there are also a lot of cases where it&#8217;s a good idea. Browesr plugin development can be a lot of fun!  Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/08/browser-plugins-vs-extensions-the-difference/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging PHP in Vim using VimDebugger</title>
		<link>http://colonelpanic.net/2010/08/debugging-php-in-vim-using-vimdebugger/</link>
		<comments>http://colonelpanic.net/2010/08/debugging-php-in-vim-using-vimdebugger/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 15:41:13 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[xdebug]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=298</guid>
		<description><![CDATA[Remote Debugging PHP from vim I&#8217;ve recently been using PHP a lot.  I&#8217;ve also been using VIM a lot.  So, it makes sense that I would start using the PHP remote debugger, right?  Well, the problem is (and don&#8217;t take this the wrong way), the php remote debugger plugin for vim was written in 2004, [...]]]></description>
			<content:encoded><![CDATA[<h2>Remote Debugging PHP from vim</h2>
<p>I&#8217;ve recently been using PHP a lot.  I&#8217;ve also been using VIM a lot.   So, it makes sense that I would start using the PHP remote debugger,  right?  Well, the problem is (and don&#8217;t take this the wrong way), the  php remote debugger plugin for vim was written in 2004, and there are a  few things missing from it.  So, being the overly-easily-amused  developer I am, I embarked on a quest to fix it.</p>
<p><span id="more-298"></span></p>
<h2>DBGp python library</h2>
<p>It didn&#8217;t take me long to realize that the python classes (vimscript  doesn&#8217;t do sockets, so you have to use something like python) provided  with the old plugin was not extensible enough for what I needed.  I  started re-factoring it, but fortunately before I got too far I found a  DBGp python library written by ActiveState.  I had to do some hacking to  get it all working together &#8212; vim doesn&#8217;t deal with import statements  in python well &#8212; but it turned out to be perfect for my needs.</p>
<h2>VimDebugger README</h2>
<p>Here is the README from the <a title="VimDebugger" href="http://github.com/taxilian/VimDebugger">github repo</a>:</p>
<div>
<h3>Vim Debugger</h3>
<p>VimDebugger is a dbgp client plugin for vim for debugging php   applications with xdebug.  It is currently in early stages, but is   already much more advanced than many other options.</p>
<h3>Special Thanks</h3>
<p>VimDebugger utilizes the ActiveState DBGp library. VimDebugger borrows code liberally from <a href="http://www.vim.org/scripts/script.php?script_id=1152">the remote PHP debugger</a> vim plugin by Seung Woo Shin.</p>
<h3>Setup</h3>
<p>At present, VimDebugger.py must be saved as   $VIMRUNTIME.&#8221;/plugin/VimDebugger.py&#8221; in order to work correctly.  I hope   to allow more flexible placement of this file in a future release.    VimDebugger.vim should generally be placed in the same directory.</p>
<h3>Remote Debugging</h3>
<p>VimDebugger will listen on localhost:9000.  This could be changed in   VimDebugger.vim, but currently is not easily configurable through your   .vimrc.  You need to have xdebug already configured to connect to that   port.</p>
<h3>Debugger functions</h3>
<p>The following commands are added by the debugger for all your remote debugging needs:</p>
<ul>
<li>:DbgRun  &#8211; Starts the listener (listens for 10 seconds) if your  script is not  already attached.  If it is, continues (will run to the  end or until  the next breakpoint)</li>
<li>:DbgDetach &#8211; Detaches the remote debugger and shuts down the listener</li>
<li>:DbgToggleBreakpoint &#8211; Toggles a breakpoint on the current line of the current file</li>
<li>:DbgStepInto &#8211; Steps into the next function or include</li>
<li>:DbgStepOver &#8211; Steps over the next function or include</li>
<li>:DbgStepOut &#8211; Steps out to the next step up in the stack</li>
</ul>
<h3>Key Bindings</h3>
<p>The debugger does not automatically bind any hotkeys, but leaves  that  to you to do in your own .vimrc.  I often use Visual Studio, so I  set  my key bindings up in a similar way:</p>
<pre>map &lt;F11&gt; :DbgStepInto&lt;CR&gt;
map &lt;F10&gt; :DbgStepOver&lt;CR&gt;
map &lt;S-F11&gt; :DbgStepOut&lt;CR&gt;
map &lt;F5&gt; :DbgRun&lt;CR&gt;
map &lt;S-F5&gt; :DbgDetach&lt;CR&gt;
map &lt;F8&gt; :DbgToggleBreakpoint&lt;CR&gt;</pre>
<h3>Watch</h3>
<p>Currently, there are no functions for adding your own watch items,   but that is planned for one of the next releases.  For now, the watch   window will automatically refresh every step with the current context.    The Watch window utilizes vim code folding on multi-line entries, so if   you have an object, be sure to expand it by double clicking, hitting   enter, or using the vim code folding keyboard commands to see the   introspection at work.  Only three levels are returned, to keep the   response from overwhelming the debugger.</p>
<p>For objects, a list of  the methods available on that object will be  returned with their  visibility.  Also, a list of properties will be  returned with their  visibility and their value.</p>
<p>For arrays, a list of values (up to three nested levels) will be returned.</p>
<h3>Stack</h3>
<p>The stack window updates every frame. To go to another part of the   stack, simply go to the line in question and double click or press   enter.</p>
<h2>Screenshots</h2>
<p>How well does it work?  Well, let me show you some screenshots, and then you decide.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-8.39.21-AM2.png"><img class="alignnone size-full wp-image-301" title="Screen shot 2010-08-05 at 8.39.21 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-8.39.21-AM2.png" alt="" width="586" height="209" /></a></p>
<p>On the left, you see the code window; that looks almost identical to the old vim plugin.  That&#8217;s one thing it did very well, and I couldn&#8217;t improve on it; so I shamelessly stole a lot of the code, instead.  On the bottom right you see the first of the improvements I added.  Instead of just displaying a list of stack entries and requiring someone to type &#8220;:Up&#8221; and &#8220;:Dn&#8221; to traverse the stack, the list is formatted and set up so that you can double click or press enter on the line you want, and it will take you there in the left pane.</p>
<p>The real improvements can be seen in the watch window, on the top right.  You may notice that these are folded; there are three variables in the local scope, and as you can see they are all instances of objects.  Well, often when I&#8217;m debugging I need to know what kind of object that is!  Here it tells you.  That&#8217;s not even the best part, though; expand the fold, and you&#8217;ll see the following:</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.01.06-AM.png"><img class="alignnone size-full wp-image-302" title="Screen shot 2010-08-05 at 9.01.06 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.01.06-AM.png" alt="" width="473" height="71" /></a></p>
<p>The number of methods and properties!  Expand methods:</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.06.29-AM.png"><img class="alignnone size-full wp-image-303" title="Screen shot 2010-08-05 at 9.06.29 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.06.29-AM.png" alt="" width="441" height="152" /></a></p>
<p>I&#8217;ve truncated it for brevity, but trust me: the method names are all there.  Let&#8217;s look at properties.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.07.34-AM.png"><img class="alignnone size-full wp-image-304" title="Screen shot 2010-08-05 at 9.07.34 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.07.34-AM.png" alt="" width="444" height="182" /></a></p>
<p>You&#8217;ll notice again; all of the properties, with their values, and&#8230; more folds?  Open one of them.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.11.00-AM.png"><img class="alignnone size-full wp-image-305" title="Screen shot 2010-08-05 at 9.11.00 AM" src="http://colonelpanic.net/wp-content/uploads/2010/08/Screen-shot-2010-08-05-at-9.11.00-AM.png" alt="" width="610" height="187" /></a></p>
<p>As you can see, array values are also sent.  For practical reasons, there is a depth limit of 3 when fetching watch variables, but that is configurable.  The main challenge is getting the data back from PHP; I have the max data size set at 64KB, so that is the limit to how much can be sent. We could probably raise that limit, but that has other consequences.</p>
<h2>Installing it</h2>
<p>First, you may want to read <a title="How to debug PHP with Vim and XDebug" href="http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/" target="_blank">this blog post</a> to see how to set up xdebug.  Instead of using the vim plugin they link to, though, get mine from the <a title="VimDebugger on github" href="http://github.com/taxilian/VimDebugger">github repo</a>.  Put VimDebugger.py and VimDebugger.vim in your ~/.vim/plugins dir, and add the key bindings to your .vimrc.  Let me know how it goes!</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/08/debugging-php-in-vim-using-vimdebugger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Terminal Nirvana on Snow Leopard with SIMBL + Visor</title>
		<link>http://colonelpanic.net/2010/07/terminal-nirvana-on-snow-leopard-with-simbl-visor/</link>
		<comments>http://colonelpanic.net/2010/07/terminal-nirvana-on-snow-leopard-with-simbl-visor/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 15:09:24 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[end]]></category>
		<category><![CDATA[home]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[simbl]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[visor]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=255</guid>
		<description><![CDATA[Note 1: The method described here should also work in Mac OS X Leopard or Tiger, just get the appropriate versions of SIMBL / Visor. Note 2: If you don&#8217;t want everything on my bullet list, just follow the steps appropriate for what you want to accomplish. Note 3: I have provided .patch files, etc. [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em><strong>Note 1</strong>: The method described here should also work in Mac OS X Leopard or Tiger, just get the appropriate versions of SIMBL / Visor.</em><br />
<em><strong>Note 2</strong>: If you don&#8217;t want everything on my bullet list, just follow the steps appropriate for what you want to accomplish.</em><br />
<em><strong>Note 3</strong>: I have provided .patch files, etc. at the bottom of the post for those who, like me, are a bit lazy.</em></p></blockquote>
<h2>What is Terminal Nirvana?</h2>
<p>As a developer, Terminal is one of my best friends. Maybe you can relate. I have constantly been in search of the ultimate Terminal configuration, and it has ever eluded me&#8230; until now.</p>
<p>But what is Terminal Nirvana? Here are my criteria:</p>
<ol>
<li><strong>I want the Home / End keys to move the cursor to the front / end of the line. </strong>I grew up on Windows, and I like the keys to function this way. To me, it just makes sense. Unfortunately, <a title="KeyFixer" href="http://www.starryhope.com/tech/apple/2006/keyfixer/">KeyFixer</a> doesn&#8217;t work in Terminal (but I highly recommend it for changing the behavior in most other apps!).</li>
<li><strong>I don&#8217;t want Home / End to behave differently when I am in a <a title="GNU Screen" href="http://en.wikipedia.org/wiki/Gnu_screen">screen</a> session.</strong> I use screen all the time, and Home / End need to behave consistently.</li>
<li><strong>I don&#8217;t want Home / End to do unexpected things in </strong><strong><a href="http://en.wikipedia.org/wiki/Vim_(text_editor)">VIM</a> &#8211; ever</strong><strong>.</strong> Pressing Home / End in VIM should, at the very least, not do weird things like delete lines, whether I am in a screen session or not.</li>
<li><strong>I want my terminal window available at all times.</strong> With as much as I use the terminal, opening Terminal.app all the time feels wasteful. There should be a better way. This is where <a title="SIMBL" href="http://www.culater.net/software/SIMBL/SIMBL.php">SIMBL</a> and <a title="Visor" href="http://visor.binaryage.com/">Visor</a> come in.</li>
<li><strong>I don&#8217;t want it to show up in the Dock, or in my Command + Tab list.</strong> As useful as Terminal is, there are a lot of other useful applications around, and I use a lot of them. Since I always want Terminal open anyway, it is annoying to constantly avoid / ignore / tab over it when I am using Command + Tab to switch to another running application. Oh, and it should stay away from my Dock, too.</li>
<li><strong>I want good, configurable colors in my shell.</strong> Fortunately, Visor takes care of this one by including <a title="Terminal Colors in Leopard / Snow Leopard" href="http://ciaranwal.sh/2007/11/01/customising-colours-in-leopard-terminal">TerminalColours</a> as part of the package, so you get this one &#8220;for free.&#8221;</li>
</ol>
<p>Sound like a tall order? It took me a few hours crawling around the web and experimenting to come up with a good solution, but I managed it in the end. Hopefully my experience can help save a few people some time in the future.</p>
<p>This is how I did it.<br />
<span id="more-255"></span></p>
<h2>First things first</h2>
<p>Before you go any further, make sure you have installed <a title="SIMBL" href="http://www.culater.net/software/SIMBL/SIMBL.php">SIMBL</a> and <a title="Visor" href="http://visor.binaryage.com/">Visor</a>. The latest versions work great in Snow Leopard with a 64-bit Terminal.app, so knock yourself out.</p>
<h2>Home / End keys</h2>
<p>To change the Home / End keys in Terminal.app, you are going to need to go to Terminal -&gt; Preferences -&gt; [Settings] -&gt; [Keyboard]. Remember, settings are profile-specific, so you will need to make the changes to any profiles you care about. (<em>Note: Visor uses the &#8220;Visor&#8221; profile, regardless of what you set as your default profile. What I do is rename the automatically-created &#8220;Visor&#8221; profile, configure my default profile, and then duplicate it with the name &#8220;Visor&#8221;.</em>)</p>
<p>The first thing I tried was changing the keybindings to &#8220;\001&#8243; and &#8220;\005&#8243; per the instructions I found on <a href="http://www.rickycampbell.com/fix-osx-terminal-pgup-pgdn-home-and-end/">Ricky Campbell&#8217;s blog</a>. This seemed to make sense; CTRL+A and CTRL+E move the cursor to the front / end of the line in Terminal. And I thought it was working perfectly, for a few glorious moments&#8230;</p>
<p>Unfortunately, CTRL+A is also the key combination to start a command in screen. So when I launched screen, suddenly my home key didn&#8217;t work anymore, and I randomly started sending screen commands when I used it.</p>
<p>I also tried using a ~/.inputrc file and the keybindings &#8220;\033[1~&#8221; and &#8220;\033[4~&#8221; as discussed on <a href="http://fplanque.com/dev/mac/mac-osx-terminal-page-up-down-home-end-of-line">two</a> <a href="http://linuxart.com/log/archives/2005/10/13/super-useful-inputrc/">blogs</a> I stumbled across while looking for an alternate solution. Now, I really thought I had the problem licked. Except, Home / End did weird things in VIM running within a screen session, and the necessity of using an additional .inputrc file was just a bit of a bummer.</p>
<p>Turns out Shift + Home and Shift + End  already did what I wanted, so the simplest solution of all is to just copy those keybindings already present in the Terminal settings. For good measure, I copied the &#8220;shift page up&#8221; and &#8220;shift page down&#8221; bindings to &#8220;page up&#8221; and &#8220;page down.&#8221; (<em>Note: you can either copy-paste the existing values, or hit ESC to get the \033 value and then type the other characters manually.</em>)</p>
<h3>Terminal key bindings:</h3>
<ul>
<li>home
<ul>
<li>Value: \033[H</li>
</ul>
</li>
<li>end
<ul>
<li>Value: \033[F</li>
</ul>
</li>
<li>page up
<ul>
<li>Value: \033[5~</li>
</ul>
</li>
<li>page down
<ul>
<li>Value: \033[6~</li>
</ul>
</li>
</ul>
<h2>Fixing VIM in screen</h2>
<p>Okay, almost there with the keybindings. If you run VIM inside a screen session, you will notice that Home deletes the current line, and using the arrow keys while in insert mode inserts A / B / C / D rather than moving the cursor. Not good.</p>
<p>A little tweak to your existing ~/.vimrc file will fix it right up (or, if you don&#8217;t have an existing ~/.vimrc, create it). (<em>Note: the easiest way to get the proper escape characters is to enter insert mode, and hit CTRL+V followed by the Home or End key. This will add the ^[ escape character, as well as the [F or [H characters. Do the same thing for CTRL + O to get the ^O escape character.</em>) By using CTRL+O on the imap lines, we make the Home / End keys work the same way in insert mode or command mode.</p>
<p>As an added bonus, this fixes the arrow keys while in insert mode. Though, to be honest, I have no idea why&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">&quot;&quot;&quot; ~/.vimrc</span>
&nbsp;
<span style="color: #adadad; font-style: italic;">&quot;&quot;&quot; Fix home and end keybindings for screen</span>
<span style="color: #25BB4D;">map</span> <span style="color: #000000;">^</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>F <span style="color: #000000;">$</span>
imap <span style="color: #000000;">^</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>F <span style="color: #000000;">^</span>O<span style="color: #000000;">$</span>
<span style="color: #25BB4D;">map</span> <span style="color: #000000;">^</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>H g0
imap <span style="color: #000000;">^</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>H <span style="color: #000000;">^</span>Og0</pre></td></tr></table></div>

<h2>Making Terminal always available</h2>
<p>This one was actually pretty simple. Using <a title="SIMBL" href="http://www.culater.net/software/SIMBL/SIMBL.php">SIMBL</a> and <a title="Visor" href="http://visor.binaryage.com/">Visor</a> (both compatible with 64-bit Terminal at the time of this writing) you can get Terminal to dock to the top / side / bottom of your screen, and configure it to open with a keystroke you can define.</p>
<p>My keystroke is Command + Esc (turn off Front Row first, if you hate it as much as I do). Once Terminal has been launched for the first time (thus creating the Visor window), this keystroke gets you in from anywhere, or hides it when you are done.</p>
<h2>Hiding the Dock icon, and removing Terminal from the Command + Tab list</h2>
<p>This is the most involved step, but it also provides one of the biggest benefits in my mind. Most of the information here comes from a <a href="http://www.metaskills.net/2009/8/18/visor-terminal-on-snow-leopard">MetaSkills blog article</a>.</p>
<p>In order to accomplish this, the LSUIElement key in Terminal&#8217;s Info.plist must be set to &#8220;1&#8243;. This disables the Dock icon and removes the application from the Command + Tab list. Don&#8217;t want to completely prevent Terminal from ever functioning like a normal application? The quick solution is to create new application &#8220;VisorTerminal.app&#8221; which is used for Visor, while leaving Terminal.app alone to function as normal. (This is actually a good thing, because another side-effect of setting LSUIElement : 1 is you no longer have anything change in the menubar when you are in your application&#8230; ie, you can&#8217;t click on VisorTerminal next to the Apple icon, and go into Preferences.)</p>
<h3>Create a new VisorTerminal.app</h3>
<p>Duplicate the existing Terminal.app.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>Utilities<span style="color: #000000; font-weight: bold;">/</span>Terminal.app <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>Utilities<span style="color: #000000; font-weight: bold;">/</span>VisorTerminal.app</pre></td></tr></table></div>

<p>Once you have duplicated Terminal.app, you need to change a few bundle identifiers in your new application so the two appear as distinct entities to Mac OS X. In other words, change &#8220;Terminal&#8221; to &#8220;VisorTerminal&#8221; in a few places. This is also when you add LSUIElements : 1.</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
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- /Applications/Utilities/VisorTerminal.app/Contents/Info.plist --&gt;</span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleDisplayName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>VisorTerminal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleIdentifier<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.apple.VisorTerminal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>VisorTerminal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #808080; font-style: italic;">&lt;!-- Note: Put this right before the closing &lt;/dict&gt;&lt;/plist&gt;</span>
<span style="color: #808080; font-style: italic;">     at the end of the file --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>LSUIElement<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...</pre></td></tr></table></div>

<p>At this point, rather than entering my preferences all over again, I elected to copy my Terminal.app preferences. Repeat this step any time you make preference changes that you want to copy over. (<em>Note: using a symlink does not seem to work. If you make changes while in VisorTerminal, such as changing the Visor hotkey, the modified file is saved over your symlink.</em>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Preferences<span style="color: #000000; font-weight: bold;">/</span>com.apple.Terminal.plist ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Preferences<span style="color: #000000; font-weight: bold;">/</span>com.apple.VisorTerminal.plist</pre></td></tr></table></div>

<h3>Modify Visor to use VisorTerminal.app</h3>
<p>Once your new application is created and ready to go, you need to tell Visor to activate for that application rather than Terminal.app. Apply the following very familiar tweaks to Visor&#8217;s Info.plist:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- ~/Library/Application\ Support/SIMBL/Plugins/Visor.bundle/Contents/Info.plist --&gt;</span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>BundleIdentifier<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.apple.VisorTerminal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ExecPattern<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*/VisorTerminal.app/Contents/MacOS/Terminal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>BundleIdentifier<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.apple.VisorTerminal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...</pre></td></tr></table></div>

<p>Once this last step is done, you are almost there. Make sure Terminal.app and VisorTerminal.app are both closed. Now, launch VisorTerminal.app and behold its wonder!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">open <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>Utilities<span style="color: #000000; font-weight: bold;">/</span>VisorTerminal.app</pre></td></tr></table></div>

<h2>What? It didn&#8217;t work?</h2>
<p>Let me guess. A Terminal window opened, but Visor didn&#8217;t take effect. What gives? (You may have also noticed that you don&#8217;t have an entry in your menubar&#8230; see above.)</p>
<p>Turns out newer versions of SIMBL <a href="http://code.google.com/p/simbl/issues/detail?id=9">don&#8217;t activate in applications with LSUIElement : 1</a>. Apparently this is expected behavior due to the Cocoa event mechanism. But all is not lost!</p>
<p>You can manually inject SIMBL into an application using Apple Script.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;VisorTerminal&quot;</span>
	inject SIMBL <span style="color: #0066ff;">into</span> Snow Leopard
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span></pre></td></tr></table></div>

<p>Great, but I don&#8217;t want to do something manually every time I want a Visor window. I just want it to always be there, remember?</p>
<p>Use the <a href="http://www.tuaw.com/2007/12/27/applescript-the-script-editor/">AppleScript Editor</a> to save this snippet as an application, and then include that application in your user startup items. Voila! When you log in, the AppleScript helper executes, which launches VisorTerminal with SIMBL injected. Visor activates, your terminal automatically hides itself, yet is always there when you want it with the push of a button. It doesn&#8217;t clutter up your Dock, your application switching, and you can still use Terminal.app as usual whenever it makes sense to do so.</p>
<h2>Final thoughts</h2>
<p>First off, note that this tutorial was written using <em>SIMBL 0.9.7a</em>, <em>Visor 2.2</em>, and <em>Terminal 2.1.1 (273)</em> on Mac OS X <em>Snow Leopard 10.6.4</em>. YMMV.</p>
<p>Secondly, my thanks to all the people who are smarter than me and figured all this out. All I did was put things together.</p>
<p>Third, see below for patch files and my AppleScript application.</p>
<p>Finally, does anyone have the following issue, and know a way around it? After setting my key bindings, when I launch Terminal for the first time, they don&#8217;t take effect. Home does nothing special. If I open a new Terminal window, the home key starts working as expected, both in the new window as well as the original window. This happens to me whether I use \001 for the control character (and this without a ~/.inputrc file) or if I use the solution described above. Slightly bothersome, but since I almost always use my Visor terminal (which doesn&#8217;t exhibit the same problem&#8230; nor did it using Visor sans any modifications discussed here) I don&#8217;t notice it too often.</p>
<p><strong>Patch files:</strong> <a href="http://gist.github.com/484082">http://gist.github.com/484082</a><br />
<strong>AppleScript app:</strong> <a href="http://colonelpanic.net/wp-content/uploads/2010/07/HiddenVisorTerminal.zip">HiddenVisorTerminal.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/07/terminal-nirvana-on-snow-leopard-with-simbl-visor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing CouchDB on Mac OS 10.6 with MacPorts</title>
		<link>http://colonelpanic.net/2010/07/installing-couchdb-on-mac-os-10-6-with-macports/</link>
		<comments>http://colonelpanic.net/2010/07/installing-couchdb-on-mac-os-10-6-with-macports/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 04:56:46 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Systems Administration]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[macports]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=248</guid>
		<description><![CDATA[CouchDB If you don&#8217;t know what CouchDB is, well, there are a lot of other places where you can find that out better than I could explain it.  Google it, my friends, google it. Harder than it should be Installing CouchDB with MacPorts should really be as easy as typing &#8220;sudo port install couchdb&#8221;.  Unfortunately, [...]]]></description>
			<content:encoded><![CDATA[<h2>CouchDB</h2>
<p>If you don&#8217;t know what CouchDB is, well, there are a lot of other places where you can find that out better than I could explain it.  Google it, my friends, google it.</p>
<h2>Harder than it should be</h2>
<p>Installing CouchDB with MacPorts should really be as easy as typing &#8220;sudo port install couchdb&#8221;.  Unfortunately, at least as of the date this was posted, it is not that easy.  The problem is that CouchDB is written in erlang, and erlang has been updated in macports to be a newer version than CouchDB 0.11 (the current stable version) supports.</p>
<p>I have written a post previous to this one about how to downgrade that.  <a title="Downgrading erlang to R13B04" href="http://colonelpanic.net/2010/07/downgrading-erlang-macports-to-r13b04/" target="_blank">Please read that and do it before proceeding with these instructions</a>.  If you don&#8217;t &#8212; it just won&#8217;t work.</p>
<h2><span id="more-248"></span>Installing CouchDB</h2>
<pre>$ sudo port install couchdb
---&gt;  Computing dependencies for couchdb
---&gt;  Fetching couchdb
---&gt;  Verifying checksum(s) for couchdb
---&gt;  Extracting couchdb
---&gt;  Configuring couchdb
---&gt;  Building couchdb
---&gt;  Staging couchdb into destroot
---&gt;  Installing couchdb @0.11.0_0
---&gt;  Activating couchdb @0.11.0_0
########################################################################
# Changes in CouchDB 0.10.0 make 0.9.1 database files incompatible.
# See http://wiki.apache.org/couchdb/BreakingChanges for details
#
# Run the following command to install the CouchDB launchd
# startup item in order to start and re-start service automatically:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.apache.couchdb.plist
########################################################################
---&gt;  Cleaning couchdb
</pre>
<p>Don&#8217;t be alarmed; even though it says 0.10.0 as the version in the notice, that is a warning about an upgrade that (if this is a new install) doesn&#8217;t apply to us.</p>
<h2>Add missing files</h2>
<p>There are a couple of directories that CouchDB scans for configuration files that aren&#8217;t created by default.  We&#8217;ll create those now (this shouldn&#8217;t be critical, but it does prevent warnings.)</p>
<pre>$ sudo mkdir /opt/local/etc/couchdb/default.d
$ sudo mkdir /opt/local/etc/couchdb/local.d
</pre>
<h2>Test the configuration</h2>
<pre>$ sudo -u couchdb couchdbPassword:
Apache CouchDB 0.11.0 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [&lt;0.31.0&gt;] Apache CouchDB has started on http://127.0.0.1:5984/
</pre>
<p>If you don&#8217;t see this, try to read the error message and figure out what went wrong.  The most common problems I&#8217;ve seen deal with permissions issues, usually on /opt/local/var/log/couchdb and /opt/local/var/lib/couchdb</p>
<h2>Start automagically</h2>
<p>The notice above gives you a launchctl command to load the LaunchDaemon plist file; you don&#8217;t need that.  There is a much easier way:</p>
<pre>$ sudo port load couchdb
$ curl http://127.0.0.1:5984
{"couchdb":"Welcome","version":"0.11.0"}
</pre>
<p>And we&#8217;re done!  As the couch people say, <em><strong>Time to relax.</strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/07/installing-couchdb-on-mac-os-10-6-with-macports/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Downgrading Erlang macports to R13B04</title>
		<link>http://colonelpanic.net/2010/07/downgrading-erlang-macports-to-r13b04/</link>
		<comments>http://colonelpanic.net/2010/07/downgrading-erlang-macports-to-r13b04/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 04:45:31 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Systems Administration]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=242</guid>
		<description><![CDATA[The problem I&#8217;ve been using couchdb a lot lately, and I really like it; unfortunately, someone isn&#8217;t really on the ball in the MacPorts world.  They upgraded erlang in macports to default to R14A, which breaks backwards compatibility for a *lot* of other ports, including couchdb.  We&#8217;re going to change that so that we get [...]]]></description>
			<content:encoded><![CDATA[<h1>The problem</h1>
<p>I&#8217;ve been using couchdb a lot lately, and I really like it; unfortunately, someone isn&#8217;t really on the ball in the MacPorts world.  They upgraded erlang in macports to default to R14A, which breaks backwards compatibility for a *lot* of other ports, including couchdb.  We&#8217;re going to change that so that we get a version compatible with couchdb.</p>
<h2><span id="more-242"></span>Downgrading erlang</h2>
<p>The last revision that had the version of erlang that we want was <a title="Browse at revision 64916" href="http://trac.macports.org/browser/trunk/dports/lang/erlang?rev=64916">64916</a>.  First thing we need to do is check out erlang for that revision.  I&#8217;m going to store my &#8220;local&#8221; macports repo in /Users/Shared/dports/:</p>
<pre>$ mkdir /Users/Shared/dports &amp;&amp; cd /Users/Shared/dports
$ svn co --revision 64916 http://svn.macports.org/repository/macports/trunk/dports/lang/erlang lang/erlang
</pre>
<p>You should now have the local repository files that you need.  Add the path to your local repository to /opt/local/etc/macports/sources.conf before the rsync line:</p>
<pre>file:///Users/Shared/dports
rsync://rsync.macports.org/release/ports/ [default]
</pre>
<p>now run portindex to get MacPorts to see the &#8220;new&#8221; (old?) version.</p>
<pre>$ sudo portindex /Users/Shared/dports/
</pre>
<p>now if you do &#8220;port list&#8221; you should see:</p>
<pre>erlang                         @R13B04         lang/erlang
</pre>
<p>and you can install it simply with &#8220;sudo port install erlang&#8221;</p>
<h2>Port install log</h2>
<pre>$ sudo port install erlang
---&gt;  Computing dependencies for erlang
---&gt;  Fetching erlang
---&gt;  Attempting to fetch otp_src_R13B04.tar.gz from http://distfiles.macports.org/erlang
---&gt;  Attempting to fetch otp_doc_man_R13B04.tar.gz from http://distfiles.macports.org/erlang
---&gt;  Attempting to fetch otp_doc_html_R13B04.tar.gz from http://distfiles.macports.org/erlang
---&gt;  Verifying checksum(s) for erlang
---&gt;  Extracting erlang
---&gt;  Applying patches to erlang
---&gt;  Configuring erlang
---&gt;  Building erlang
---&gt;  Staging erlang into destroot
---&gt;  Installing erlang @R13B04_0
---&gt;  Activating erlang @R13B04_0
---&gt;  Cleaning erlang</pre>
<h2>Credits</h2>
<p>Special thanks to Joe Homs for his <a href="http://journal.bitshaker.com/articles/2007/10/20/install-old-versions-of-ports-using-macports/">blog post on doing the same thing with Ruby</a> that I was able to tweak to work with erlang!</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/07/downgrading-erlang-macports-to-r13b04/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Boost.Preprocessors Sequences</title>
		<link>http://colonelpanic.net/2010/07/boost-preprocessors-sequences/</link>
		<comments>http://colonelpanic.net/2010/07/boost-preprocessors-sequences/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 16:18:13 +0000</pubDate>
		<dc:creator>Georg Fritzsche</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[preprocessor]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=205</guid>
		<description><![CDATA[After recently answering a question on Stackoverflow using Boost.Preprocessor, i wondered how the sequences, e.g. SUM_MACRO((1)(2)(3)), actually work. Implementing Macros operating on sequences Sequences give you a convenient array-like data-structure wich can be passed to macros: #define HEAD(sequence) \ BOOST_PP_SEQ_ELEM(0, sequence) std::cout]]></description>
			<content:encoded><![CDATA[<p>After recently <a href="http://stackoverflow.com/questions/3150700/need-meta-programming-magic-to-define-a-mother-load-of-bit-fields-in-an-error-fre/3151243#3151243">answering</a> a question on Stackoverflow using <a href="http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/index.html">Boost.Preprocessor</a>, i wondered how the <a href="http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/data/sequences.html">sequences</a>, e.g. <code>SUM_MACRO((1)(2)(3))</code>, actually work.</p>
<p><span id="more-205"></span></p>
<h2>Implementing Macros operating on sequences</h2>
<p>Sequences give you a convenient array-like data-structure wich can be passed to macros:</p>
<pre lang="c++">
#define HEAD(sequence) \
    BOOST_PP_SEQ_ELEM(0, sequence)

std::cout << HEAD((0)(1)(2)); // prints "0"
</pre>
<p>Now how can that be implemented? First there are two characters that have special status for the preprocessor: commas and parentheses.<br />
Parentheses can be used to </p>
<ul>
<li>wrap expressions to be left alone by macro expansion, e.g. because they contain commas</li>
<li>can be bound to macro names as parameter lists</li>
</ul>
<p>The binding of parameter lists doesn't have to happen immediately, so the following works fine:</p>
<pre lang="c++">
#define IDENTITY(x) x
#define HEAD(sequence) IDENTITY sequence
std::cout << HEAD((42)); // prints "42"
</pre>
<p>The macro-expansion here is the following:</p>
<pre lang="c++">
HEAD((42))
IDENTITY (42)
42
</pre>
<p>That already works fine for a one-element sequence, but what if it had more elements:</p>
<pre lang="c++">
std::cout << HEAD((42)(23));
// results in:
std::cout << 42(23);
</pre>
<p>... which is obviously wrong. We need to get rid of the rest of the sequence by going through another macro:</p>
<pre lang="c++">
#define FIRST(x) x, DOESNT_EXIST
#define HEAD(sequence) FIRST sequence
std::cout << HEAD((42)(23));
</pre>
<p>This now results in:</p>
<pre lang="c++">
HEAD((42)(23))
FIRST (42)(23) // FIRST binds (42)
42, DOESNT_EXIST(23)
</pre>
<p>Passing this result through another macro that takes two arguments, we can ignore the second part:</p>
<pre lang="c++">
#define HEAD(sequence) IGNORE_SECOND(FIRST sequence)
#define FIRST(x) x, FOO
#define IGNORE_SECOND(x) IGNORE_SECOND_I(x) // needed for expansion of x
#define IGNORE_SECOND_I(x, _) x
</pre>
<p>Which now expands to:</p>
<pre lang="c++">
HEAD((42)(23))
IGNORE_SECOND(FIRST (42)(23)) // FIRST binds (42)
IGNORE_SECOND_I(42, DOESNT_EXIST(23))
42
</pre>
<h2>Getting elements by index</h2>
<p>Now the macros we pass the comma-separated expansion to don't have to throw away the second part:</p>
<pre lang="c++">
#define SECOND(sequence) IGNORE_SECOND(TAIL sequence)
#define TAIL(x) FIRST // throw away first element
</pre>
<p>... with which we get something like the following expansion:</p>
<pre lang="c++">
SECOND((1)(2)(3))
IGNORE_SECOND(TAIL (1)(2)(3))
IGNORE_SECOND_I(FIRST (2)(3))
IGNORE_SECOND_I(2, DOESNT_EXIST(3))
2
</pre>
<p>Now combining that with the concatenation operator <code>##</code> we can define macros that operate index-based:</p>
<pre lang="c++">
#define GET_BY_INDEX(index, sequence) IGNORE_SECOND( GET_##index sequence )
#define GET_0(x) x, DOESNT_EXIST
#define GET_1(_) GET_0
#define GET_2(_) GET_1
// ...
</pre>
<p>... which could lead to the following expansion:</p>
<pre lang="c++">
GET_BY_INDEX(2, (1)(2)(3)(4))
IGNORE_SECOND(GET_2 (1)(2)(3)(4))
IGNORE_SECOND_I(GET_1 (2)(3)(4))
IGNORE_SECOND_I(GET_0 (3)(4))
IGNORE_SECOND_I(3, DOESNT_EXIST(4))
3
</pre>
<h2>Counting elements</h2>
<p>To get the size of a sequence the elements in it need to be counted. The trick here is that substitution for macros that take arguments stops if there are no parameter-lists left that could be bound, so the following:</p>
<pre lang="c++">
#define SIZE(sequence) SIZE_0 sequence
#define SIZE_0(_) SIZE_1
#define SIZE_1(_) SIZE_2
#define SIZE_2(_) SIZE_3
// ...
</pre>
<p>would expand <code>SIZE((1)(2))</code> to <code>SIZE_2</code>. All that is left then is to map that name to a number:</p>
<pre lang="c++">
#define SIZE(sequence) \
    CONCAT(SIZE_, SIZE_0 sequence) // concatenate SIZE_ with SIZE_N
#define CONCAT(a, b) CONCAT_I(a, b) // allow for macro expansion
#define CONCAT_I(a, b) a ## b
#define SIZE_SIZE_0 0
#define SIZE_SIZE_1 1
#define SIZE_SIZE_2 2
// ...
</pre>
<p>... which would lead to expansions like:</p>
<pre lang="c++">
SIZE((1)(2))
CONCAT(SIZE_, SIZE_0 (1)(2))
CONCAT_I(SIZE_, SIZE_0 (1)(2))
CONCAT_I(SIZE_, SIZE_1 (2))
CONCAT_I(SIZE_, SIZE_2)
SIZE_ ## SIZE_2
SIZE_SIZE_2
2
</pre>
<p>- Georg</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/07/boost-preprocessors-sequences/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TextMate &#8220;Go to file&#8230;&#8221; in VIM</title>
		<link>http://colonelpanic.net/2010/05/textmate-go-to-file-in-vim/</link>
		<comments>http://colonelpanic.net/2010/05/textmate-go-to-file-in-vim/#comments</comments>
		<pubDate>Tue, 11 May 2010 20:21:02 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[textmate]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=194</guid>
		<description><![CDATA[Ahh, the great debate; which is the best text editor of them all?  I have used many of them, but I keep falling back to what for me is the classic one: VIM. Second to VIM, however, I have always been very impressed with TextMate. There are features in TextMate that just make me happy.  [...]]]></description>
			<content:encoded><![CDATA[<p>Ahh, the great debate; which is the best text editor of them all?  I have used many of them, but I keep falling back to what for me is the classic one: VIM.</p>
<p>Second to VIM, however, I have always been very impressed with TextMate. There are features in TextMate that just make me happy.  My favorate is the &#8220;Go to file&#8230;&#8221; option (Command-T).  For those of you who aren&#8217;t familiar with it, when you press Command-T in TextMate a little popup window appears.  You type part of the filename that you are looking for and TextMate displays all files in your project that contain that string.</p>
<p><span id="more-194"></span><a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.42.30-PM.png"><img class="size-medium wp-image-195 alignright" title="Screen shot 2010-05-11 at 1.42.30 PM" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.42.30-PM-300x214.png" alt="" width="186" height="132" /></a>In my (not so) humble opinion, this has got to be about the best project management tool that has ever been conceived.  The problem, however, is that TextMate only works on Mac!  Granted, I have in recent years migrated to where I do most of my work on a Mac, but when I find myself working on Windows or Linux, I need my text editor!  That&#8217;s where VIM comes in.</p>
<p>VIM is about the most extensible editor I&#8217;ve ever used, and that is one thing I love about it.  Granted, there are disadvantages to it &#8212; the learning curve is heavy, for one.  But, once you figure out how to do things, it&#8217;s great.  However, it definitely is missing this &#8220;Go to file&#8230;&#8221; like ability.  This has bothered me for months, but only today have I finally found an adequate solution.</p>
<p>The Plugin is called <a title="Fuzzy Finder" href="http://www.vim.org/scripts/script.php?script_id=1984" target="_blank">Fuzzy Finder</a>.  At first glance, it seems like it almost does what we need, but not quite.  However, combined with the <a title="VIM project plugin" href="http://www.vim.org/scripts/script.php?script_id=69">Project</a> plugin, you can come pretty darn close to TextMate&#8217;s &#8220;Go to file&#8230;&#8221; feature.</p>
<p>My purpose here isn&#8217;t to teach you how to use the project plugin, since there already exists <a title="Projects with Vim using projects.vim" href="http://nodnod.net/2009/feb/09/projects-vim-using-projectsvim/" target="_blank">plenty</a> <a title="Vimgrep and Vim Project" href="http://weierophinney.net/matthew/archives/194-Vimgrep-and-Vim-Project.html" target="_blank">of</a> <a title="VIM project plugin" href="http://www.vim.org/scripts/script.php?script_id=69" target="_blank">documentation</a>.   I will give you a few tips, however.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.56.59-PM.png"><img class="alignright size-medium wp-image-196" title="vim project name of entry" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.56.59-PM-300x78.png" alt="" width="197" height="51" /></a>When you create an entry in your project list (\C), it asks you several questions:</p>
<ul>
<li>&#8220;Enter the name of the Entry:&#8221; &#8211; This can be any name you wish</li>
<li><a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.57.33-PM.png"><img class="alignright size-medium  wp-image-197" title="vim project dir to load" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.57.33-PM-300x75.png" alt="" width="233" height="58" /></a>&#8220;Enter the Absolute Directory to Load:&#8221; &#8211; This should be the root path for your project</li>
<li>&#8220;Enter the CD parameter:&#8221; &#8211; This is the most useful option for us, but probably the least understood.</li>
<li><a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.58.01-PM.png"><img class="alignright size-medium  wp-image-198" title="vim project CD param" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-1.58.01-PM-300x88.png" alt="" width="243" height="71" /></a>&#8220;Enter the File Filter: &#8221; &#8211; I usually leave this blank, but you can use it to select which files to find</li>
</ul>
<p>The trick that I have found is that if you specify the same path for the Absolute Directory and the CD parameter, when you open files in your project vim will use that as its CWD.</p>
<p><a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-2.05.14-PM.png"><img class="alignright size-medium wp-image-200" title="vim project screenshot" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-2.05.14-PM-300x191.png" alt="" width="300" height="191" /></a>So now we have a project and we can browse the files.  We can also do fast searches in the project pane; simply use &#8220;/&#8221; to search for filenames.  However, this still isn&#8217;t quite what we wanted.</p>
<p>The next step is to install the <a title="Fuzzy Finder" href="http://www.vim.org/scripts/script.php?script_id=1984" target="_blank">Fuzzy Finder</a> plugin. Fuzzy Finder is set up to let you quickly navigate a directory tree, but it also has some other unique features that make it ideal for what we want.  To open Fuzzy Finder to find a file, the command you want is &#8220;:FufFile&#8221;.  This will pull up the Fuzzy Finder mini-dialog.<a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-2.09.17-PM.png"><img class="alignright size-full wp-image-201" title="fuzzyfinder std" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-2.09.17-PM.png" alt="" width="139" height="88" /></a></p>
<p>However, this still isn&#8217;t quite what we want; it does let us navigate quickly with filters, but we want to quickly search through all files in the directory!  Well, as it turns out, FuzzyFinder supports wildcards; not just &#8220;*&#8221;, but also the less often seen &#8220;**&#8221;, which means &#8220;in all subdirectories&#8221;.  So, we can then do &#8220;:FufFile **/&#8221; to tell it to start searching there.</p>
<p>Depending on the size of your directory, this can be a bit slow &#8212; it&#8217;s not perfect.  However, you&#8217;ll find that this way you can very quickly search through your <a href="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-2.12.36-PM.png"><img class="alignright size-medium wp-image-202" title="fuzzyfinder recursive" src="http://colonelpanic.net/wp-content/uploads/2010/05/Screen-shot-2010-05-11-at-2.12.36-PM-300x166.png" alt="" width="300" height="166" /></a>files to open the one you want &#8212; almost the same way you would in TextMate.  Keep in mind that this relies on where our CWD is, so you can use VIM&#8217;s lcd command to move to the root of where you want to search.</p>
<p>Now, typing &#8220;:FufFile **/&#8221; all the time is a little cumbersome, so you may want to add a shortcut.  If you really want it to be just like textmate, you could probably set Meta-T to open it, but I prefer to leave that for MacVim which uses it to open tabs.  Instead, I use the following:</p>
<pre style="padding-left: 30px;">nmap &lt;leader&gt;ff :FufFile **/&lt;CR&gt;
</pre>
<p>In my configuration this means that I just have to type &#8220;\ff&#8221; to have Fuzzy Finder pop up so that I can quickly open the file I want.  I also map \fb to Fuzzy Finder&#8217;s buffer search to quickly switch to another file in the buffer (&#8220;nmap &lt;leader&gt;fb :FufBuffer&lt;CR&gt;&#8221;).</p>
<p>Anyway, I hope this helps someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/05/textmate-go-to-file-in-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-Glue</title>
		<link>http://colonelpanic.net/2010/03/objectiveglue/</link>
		<comments>http://colonelpanic.net/2010/03/objectiveglue/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 16:19:19 +0000</pubDate>
		<dc:creator>Georg Fritzsche</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[bridging]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=175</guid>
		<description><![CDATA[With template metaprogramming and the Objective-C runtime bridging between Objective-C and C++ can be simplified. This post describes a prototype for generating Objective-C delegates that bridge to C++ code.]]></description>
			<content:encoded><![CDATA[<p>A while ago, i inquired on <a href="http://stackoverflow.com/questions/2276840/easing-c-to-objective-c-cocoa-bridging-via-metaprogramming">StackOverflow</a> if anybody knew about meta-programming techniques to ease bridging between Objective-C and C++:</p>
<blockquote><p>
In a pure C++ world we can generate interfacing or glue code between different components or interfaces at compile time, using a combination of template-based compile-time and runtime-techniques (to e.g. mostly automatically marshall to/from calls using legacy types).</p>
<p>When having to interface C++ applications with Objective-C/Cocoa for GUI, system integration or IPC though, things become harder due to the less strict typing &#8211; yet often not more then a flat repetitive interface layer is needed: thin bridging delegates have to be defined or conversion code to language bridging calls has to be written.</p>
<p>If you have to deal with interfaces of non-trivial size and want to avoid script-based code generation this quickly becomes cumbersome and is just a pain every time refactorings have to take place. Using a combination of (template) metaprogramming and the Objective-C runtime library, it should be possible to reduce the amount of code considerably&#8230;
</p></blockquote>
<p>As i didn&#8217;t get any answers that were satisfactory for me, i started on a prototype.<br />
<span id="more-175"></span><br />
The following is an overview on the idea behind <a href="http://bitbucket.org/cygmatic/cygmatic/src/tip/og/">the prototype</a>.</p>
<h2>Motivation</h2>
<p>What annoyed me most is that every time i just need to get some events of an Objective-C instance to some C++ instance, i need to create a separate Objective-C class and manually forward from there to the target (C++) object.</p>
<p>Lets say we have the following informal delegate protocol:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>concatString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s1 withString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s2;</pre></td></tr></table></div>

<p>Currently i have to first create an Objective-C instance:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> Glue <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    CppClass<span style="color: #002200;">*</span> instance;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCppInstance<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CppClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ins;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>concatString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s1 withString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s2;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>&#8230; with the appropriate implementation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> Glue
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCppInstance<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CppClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ins <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        instance <span style="color: #002200;">=</span> ins;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>concatString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s1 withString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s2 <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">const</span> std<span style="color: #002200;">::</span><span style="color: #a61390;">string</span> s <span style="color: #002200;">=</span>
        instance<span style="color: #002200;">-</span>&gt;concatStrings<span style="color: #002200;">&#40;</span>toStdString<span style="color: #002200;">&#40;</span>s1<span style="color: #002200;">&#41;</span>, toStdString<span style="color: #002200;">&#40;</span>s2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> toNsString<span style="color: #002200;">&#40;</span>s<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Then i still have to take care of holding and initializing that Objective-C instance in the C++ instance, with possibly additional work for <a href="http://en.wikipedia.org/wiki/Opaque_pointer">opaque pointers</a> because i don&#8217;t want to bring Objective-C into the C++ header.</p>
<h2>Objective-Glue</h2>
<p>The idea was that there should be a way to at least cut down on the amount of code and to mostly automatically generate the bridging code.</p>
<p>I now found some time and came up with a basic prototype. Given the following class definition:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> CppClass <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> concatStrings<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> s1, <span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> s2<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> s1<span style="color: #000040;">+</span>s2<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>&#8230; a suitable delegate can be build as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">CppClass cpp<span style="color: #008080;">;</span>
og<span style="color: #008080;">::</span><span style="color: #007788;">ObjcClass</span> objc<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;MyGlueClass&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
objc.<span style="color: #007788;">add_handler</span><span style="color: #000080;">&lt;</span>NSString<span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>NSString<span style="color: #000040;">*</span>, NSString<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span>
       <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;concatString:withString:&quot;</span>, <span style="color: #000040;">&amp;</span>cpp, <span style="color: #000040;">&amp;</span>CppClass<span style="color: #008080;">::</span><span style="color: #007788;">concatStrings</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Here the explicit template parameter to <code>add_handler()</code> specifies the Objective-C signature, the first parameter specifies the method name and the last two pass the C++ instance and the method to call on it.</p>
<p>Free functions can also be used:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">objc.<span style="color: #007788;">add_handler</span><span style="color: #000080;">&lt;</span>NSString<span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>NSString<span style="color: #000040;">*</span>, NSString<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span>
       <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;concatString:withString:&quot;</span>, <span style="color: #000040;">&amp;</span>concatStrings<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><a href="http://www.boost.org/doc/html/function.html">Boost.Function</a> objects can also be passed.</p>
<p>The Objective-C instance can then be called like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">id</span> ins <span style="color: #002200;">=</span> objc.get_instance<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>ins concatString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;abcd&quot;</span> withString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;efgh&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">assert</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>result compare<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;abcdefgh&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedSame<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<h2>How?</h2>
<p>While the Objective-C signature has to be specified explicitly even for formal protocols (as in the type encodings the type information for Objective-C classes is lost), the C++ signature can be looked up at compile time. Knowing those two signatures we can use template functions to get a suitable <code>IMP</code> function, that <i>&#8220;stores&#8221;</i> the knowledge of the signatures and does the necessary conversions.</p>
<p><i>Note: Behind the scenes, Objective-C methods are simple C functions that take two additional parameters, of type <code>id</code> for the instance and <code>SEL</code> for the method being called. These functions are called <code>IMP</code> or implementation functions.</i></p>
<p>Combining that with the fact that the <a href="http://developer.apple.com/mac/library/documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html">Objective-C runtime</a> allows to add and replace method implementations via its API, we can generate classes that call suitable <code>IMP</code> functions. To allow for member functions, free functions or <a href="http://www.boost.org/doc/libs/release/libs/bind">Boost.Bind</a> generated expressions to be invoked, functors (<a href="http://www.boost.org/doc/html/function.html">Boost.Function</a>) are used to store the callees.</p>
<p>The functors are stored in a map in the indexed ivar section of the instance, look-up is done via the selector that <code>IMP</code> functions are passed.<br />
The <code>IMP</code> function can then cast the retrieved pointer to the correct signature type and invoke the functor.</p>
<p><!–-nextpage-–></p>
<h2>Generating the instance</h2>
<p>Using the Objective-C runtime, we can generate a custom class. We first need the super-class (if any):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> superClassName <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;NSObject&quot;</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> className      <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;MyObjcClass&quot;</span><span style="color: #008080;">;</span>
id superClass <span style="color: #000080;">=</span> objc_getClass<span style="color: #008000;">&#40;</span>superClassName.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Then we need to allocate a new class pair and register it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">Class myClass <span style="color: #000080;">=</span> objc_allocateClassPair
                    <span style="color: #008000;">&#40;</span>superClass, className.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
objc_registerClassPair<span style="color: #008000;">&#40;</span>myClass<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>The last parameter to <code>objc_allocateClassPair()</code> reserves additional space per instance for a pointer, so we can store the callee map later.</p>
<p>Now that the class is registered, we can create an instance:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">id myInstance <span style="color: #000080;">=</span> class_createInstance<span style="color: #008000;">&#40;</span>myClass, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>To add methods, we need to use <code>class_addMethod()</code> if the class doesn&#8217;t have such a method yet, or <code>method_setImplementation()</code> to override an existing one:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> methodName <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;concatString:withString:&quot;</span><span style="color: #008080;">;</span>
SEL mySelector <span style="color: #000080;">=</span> sel_registerName<span style="color: #008000;">&#40;</span>methodName.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
BOOL added <span style="color: #000080;">=</span> class_addMethod<span style="color: #008000;">&#40;</span>myClass, mySelector, myImp, <span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>added<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// class already has such a method, override it</span>
    Method m <span style="color: #000080;">=</span> class_getInstanceMethod<span style="color: #008000;">&#40;</span>myClass, mySelector<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    method_setImplementation<span style="color: #008000;">&#40;</span>m, myImp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now for expressions like <code>[myObj concatString:s1 withString:s2]</code>, the custom implementation <code>myImp</code> would get called. But how do we generate a suitable <code>IMP</code> function?</p>
<h2>Generating IMPs</h2>
<p>As we know the ObjcC and the C++ signatures, we can use template specialization to get a suitable function. To avoid too much repitition, partial specialization of a template class with a static member function is used:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">size_t</span> Arity, <span style="color: #0000ff;">bool</span> returnsVoid, <span style="color: #0000ff;">class</span> ObjcSig, <span style="color: #0000ff;">class</span> CppSig<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> imp<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>For e.g. <code>CppClass::concatStrings</code>, we have an arity of 2 and it doesn&#8217;t return <code>void</code>. This would require the following specialization:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ObjcSig, <span style="color: #0000ff;">class</span> CppSig<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> imp<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">2</span>, <span style="color: #0000ff;">false</span>, ObjcSig, CppSig<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> <span style="color: #008080;">???</span> f<span style="color: #008000;">&#40;</span>id self, SEL sel, <span style="color: #008080;">???</span> a1, <span style="color: #008080;">???</span> a2<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">// ... invoke Callee</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Looking up the needed forms of the ObjC and C++ arguments and return types can be done via helper meta functions using Boosts FunctionTypes and TypeTraits libraries. With those types known, <code>f()</code> becomes something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> ObjcRetType f<span style="color: #008000;">&#40;</span>id self, SEL sel, ObjcArg0 a0, ObjcArg1 a1<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    boost<span style="color: #008080;">::</span><span style="color: #007788;">function</span><span style="color: #000080;">&lt;</span>CppSig<span style="color: #000080;">&gt;</span> callee <span style="color: #000080;">=</span> lookup_callee<span style="color: #000080;">&lt;</span>CppSig<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>self, sel<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> convert<span style="color: #000080;">&lt;</span>PlainObjcRet, PlainCppRet<span style="color: #000080;">&gt;</span> <span style="color: #666666;">// convert&lt;ToType,FromType&gt;</span>
        <span style="color: #008000;">&#40;</span>callee
          <span style="color: #008000;">&#40;</span>convert<span style="color: #000080;">&lt;</span>PlainCppArg0, PlainObjcArg0<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>a0<span style="color: #008000;">&#41;</span>,
           convert<span style="color: #000080;">&lt;</span>PlainCppArg1, PlainObjcArg1<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>a1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><i>Note: The <code>Plain*</code> types denote argument or return types stripped of references and constness.</i></p>
<p>With such an approach, we can choose a suitable <code>IMP</code> function at compile-time:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> imp<span style="color: #000080;">&lt;</span>function_arity, is_same<span style="color: #000080;">&lt;</span>ReturnType, <span style="color: #0000ff;">void</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span>, 
            ObjcSig, CppSig<span style="color: #000080;">&gt;</span> ImpType<span style="color: #008080;">;</span>
IMP myImp <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>IMP<span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>ImpType<span style="color: #008080;">::</span><span style="color: #007788;">f</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h2>Conversions</h2>
<p>Conversions are done via a static member function of a templated struct <code>og::converter</code>, which allows for partial specialization in user code. It is declared as following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> To, <span style="color: #0000ff;">class</span> From<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">struct</span> converter <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> To convert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">typename</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">call_traits</span><span style="color: #008080;">::</span><span style="color: #007788;">param_type</span><span style="color: #000080;">&lt;</span>From<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">type</span> from<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><em>Note: <a href="http://www.boost.org/doc/libs/1_42_0/libs/utility/call_traits.htm"><code>param_type</code></a> is a Boost utility that maps the type to the appropriate form (pass-by-value for fundamental types, pass-by-const-reference for complex types, &#8230;).</em></p>
<p>To add additional conversions, specializations of <code>converter</code> are added anywhere before the <code>add_handler()</code> call that would use them. E.g. to enable conversions from <code>NSString</code> to <code>std::string</code> the following (simplified) specialization can be added:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;&gt;</span> <span style="color: #0000ff;">struct</span> converter<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, NSString<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> convert<span style="color: #008000;">&#40;</span>NSString<span style="color: #000040;">*</span> from<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#91;</span>from UTF8String<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h2>Conclusion</h2>
<p>While the prototype shows that the basic idea works, there are still some issues that need to be worked out:</p>
<ul>
<li>support for formal protocols</li>
<li>support for properties</li>
<li>support for type-encodings (in a non-painful way)</li>
<li>allowing full customization of class and instance (currently dealloc is needed and no class methods supported)</li>
<li>convenience for wrapping complete C++ instance as Objective-C instances?</li>
<li>&#8230; probably more</li>
</ul>
<p>For those interested, here is some background reading material:</p>
<ul>
<li><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html">Objective-C Runtime Programming Guide</a></li>
<li><a href="http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html">Understanding the Objective-C runtime</a></li>
<li><a href="http://developer.apple.com/mac/library/documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html">Objective-C Runtime Reference</a></li>
</ul>
<p>As previously mentioned, this is a first prototype&#8230; so constructive input and ideas are welcome. The current source can be <a href="http://bitbucket.org/cygmatic/cygmatic/src/tip/og/">viewed online</a> via a bitbucket repository.<br />
The prototype relies on some parts of <a href="http://www.boost.org/">Boost</a> and was tested with Boost 1.42.</p>
<p><i>&#8211; Georg</i></p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/03/objectiveglue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
