<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0"><channel><title>Anonymous Function</title><link>http://www.anonymous-function.net</link><description>RSS feed for Anonymous Function</description><lastBuildDate>Sat, 13 Apr 2013 01:03:54 GMT</lastBuildDate><generator>PyRSS2Gen-1.1.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Non-linear Code Editing</title><link>http://www.anonymous-function.net/articles/non-linear-code-editing</link><description>&lt;h1&gt;Background&lt;/h1&gt;
&lt;p&gt;Programmers are used to working with code as if it were a collection of short stories, because computer code when stored on disk is traditionally organized into files and projects. When being edited, it is also presented to the user that way. For most programming languages, the file structure is important, since it often corresponds to an organizational method; e.g.:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Header files in C-based languages are imported by name and path.&lt;/li&gt;
&lt;li&gt;Java uses the folder structure to mirror the package structure.&lt;/li&gt;
&lt;li&gt;Java also uses the filename to show the public class defined in that file.&lt;/li&gt;
&lt;li&gt;Python works similarly for package structure and filename.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So presenting the file-level structure to the programmer makes sense, since it is an important organizational detail. But what about the contents of these files? Again, computer code is traditionally stored in plain text files, can be read or written by any simple text editor, and is presented in a linear fashion, like English text.  Dedicated IDEs and smarter editors add features on top of this presentation, like syntax highlighting, auto-completion, compiler integration, etc., but the code itself is still presented as it appears in the text file: one line after another. There are various tricks applied on top of that to aid navigation (code folding, symbol/tag lists), but none of them fundamentally alter the linear, file-oriented presentation of the code.&lt;/p&gt;
&lt;p&gt;A few reasons why this is still the case:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For some languages, order in the file is significant. In C, for example, you have to mention a function or variable before you can use it. Mostly this is accomplished by &lt;code&gt;#include&lt;/code&gt;-ing a header file, but within a single file, if you define a function, any code after that can call that function; if you move the definition around, code that was after the original definition could fail to compile.&lt;/li&gt;
&lt;li&gt;Text editors are a lowest-common-denominator. Sometimes you have nothing else, but need to modify some code right now; a standard text editor may not be as nice as your dedicated development environment, but they both show the code the same way, so at least you won&amp;#8217;t get lost.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;A new proposal&lt;/h1&gt;
&lt;p&gt;I won&amp;#8217;t claim to speak for all programmers, but when I&amp;#8217;m coding, what I care about are logical constructs. I may be looking at 3 different functions at once, and I don&amp;#8217;t particularly care about where in the source file they live; for that particular task, I want to look at them together. With smaller files, good code organization can mostly solve this problem, but as projects get more complex, I find myself wishing for a view that is not bound by file layout. And of course, sometimes you want to examine code from different files side by side. Editors with good splitting and navigation capabilities can make this less painful, but this is still a band-aid on the problem.&lt;/p&gt;
&lt;p&gt;So why not break free from the traditional text editor view of the world, and present code in a different way? Specifically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Analyze source files to find the units of code the programmer is interested in. For most languages the logical unit is functions or methods, but an argument could also be made for classes, provided they are small enough.&lt;/li&gt;
&lt;li&gt;Extract these units, along with their original location and namespace information.&lt;/li&gt;
&lt;li&gt;Present them to the user in a fashion that allows the user to organize the units on the screen however they want; this includes allowing them to fold units up and put them out of the way.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This allows the user to easily grab related units of code, place them together, and study them, regardless of where in the file(s) they actually live. Armed with the original namespace and location information, editing is also possible. Changes as well line insertions and deletions can easily be reconciled with the source document; indeed, completely new units can be inserted, with the editor selecting an appropriate location for them.&lt;/p&gt;
&lt;h1&gt;A proof of concept&lt;/h1&gt;
&lt;p&gt;As a proof of concept, I&amp;#8217;ve implemented part of the above feature-set. The implementation uses Python to analyze Python, and presents the results with &lt;span class="caps"&gt;HTML&lt;/span&gt;. The units extracted are functions, and the presentation allows the user to drag the units around the page. The main components are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Python&amp;#8217;s built-in &lt;a href="http://docs.python.org/release/2.7.3/library/ast.html"&gt;ast module&lt;/a&gt;, which is used for analysis of the Python source.&lt;/li&gt;
&lt;li&gt;The &lt;a href="http://pygments.org/"&gt;pygments&lt;/a&gt; library, which is used to turn the source code into syntax highlighted &lt;span class="caps"&gt;HTML&lt;/span&gt; and &lt;span class="caps"&gt;CSS&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;A template library to assemble the final &lt;span class="caps"&gt;HTML&lt;/span&gt; from the snippets; &lt;a href="http://www.tornadoweb.org/"&gt;tornado&lt;/a&gt;&amp;#8216;s is used, but almost any one could be substituted.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jqueryui.com/"&gt;jQuery &lt;span class="caps"&gt;UI&lt;/span&gt;&lt;/a&gt;, which is used to provide basic interactivity (making each source snippet draggable).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Clearly several features discussed above are missing, most notably any sort of editing or round-trip support. Multiple file support and folding are also desired features that are absent at the moment. Getting to this point was surprisingly easy, mostly due to Python&amp;#8217;s self-analysis abilities. Below is a snippet showing the ease of extracting all the functions from a Python source file, complete with class and line number information:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FunctionVisitor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NodeVisitor&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;functions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;visit_ClassDef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;visit_FunctionDef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;functions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lineno&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_function&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lineno&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;pass&lt;/span&gt;

        &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FunctionVisitor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generic_visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;...&lt;/span&gt;

&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FunctionVisitor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;functions&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The full implementation can be found in a &lt;a href="https://github.com/singerb/python_nle"&gt;github repository&lt;/a&gt;. Here&amp;#8217;s a screenshot of it in action:&lt;/p&gt;
&lt;div class="image"&gt;&lt;img alt="nle-screenshot" src="/images/nle-screenshot.png" title="Non-linear code display in action" /&gt;&lt;br/&gt;&lt;p class="caption"&gt;Non-linear code display in action&lt;/p&gt;&lt;/div&gt;
&lt;h1&gt;Related work &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; future directions&lt;/h1&gt;
&lt;p&gt;I&amp;#8217;m not the only person to think of concepts along this line. In fact, much of the impetus for this came from reading about the &lt;a href="http://www.lighttable.com/"&gt;Light Table&lt;/a&gt; project, which takes a similar idea about code editing and runs with it even further, adding features like interactivity and in-editor output. I wanted to explore the idea myself, with a language that I use, which was the genesis of this experiment. I&amp;#8217;ve also seen this idea mentioned in various other places around the web and Twitter.&lt;/p&gt;
&lt;p&gt;For future work on this project, code folding should be easily accomplished with &lt;span class="caps"&gt;JS&lt;/span&gt; &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;span class="caps"&gt;CSS&lt;/span&gt;, and multi-file support is an easy extension to the parsing framework. More interesting is editing support; if the presentation continues to be &lt;span class="caps"&gt;HTML&lt;/span&gt; then some sort of backend load/save framework will be needed, but there are already code-centric editors that are &lt;span class="caps"&gt;HTML&lt;/span&gt; components. &lt;a href="http://codemirror.net/"&gt;Code Mirror&lt;/a&gt; is one such editor, which is coincidentally also used by &lt;a href="http://www.lighttable.com/"&gt;Light Table&lt;/a&gt;.&lt;/p&gt;</description><guid>/articles/non-linear-code-editing</guid><pubDate>Wed, 10 Apr 2013 21:00:00 GMT</pubDate></item><item><title>Welcome</title><link>http://www.anonymous-function.net/articles/welcome</link><description>&lt;h1&gt;A short introduction&lt;/h1&gt;
&lt;p&gt;This blog is intended to be a place for me to talk about things in longer form than I can on Twitter; topics will likely range from the technical (programming, software development) to the photographic to gaming to the more general. I expect most of the content here to be technical or photographic, however, since that&amp;#8217;s what I do for a living and as a main hobby. Example topics include: Vim plugins and configuration, Linux window managers, error handling in software, useful iOS apps, pinhole cameras, and food photography.&lt;/p&gt;
&lt;p&gt;You can read more about the (custom) software powering the blog in the &lt;a href="../../colophon"&gt;colophon&lt;/a&gt;, and you can subscribe to the &lt;a href="../../rss.xml"&gt;&lt;span class="caps"&gt;RSS&lt;/span&gt; feed&lt;/a&gt; to see new posts.&lt;/p&gt;</description><guid>/articles/welcome</guid><pubDate>Wed, 10 Apr 2013 19:00:00 GMT</pubDate></item></channel></rss>