<?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>Einar Egilsson &#187; Extensions</title>
	<atom:link href="http://einaregilsson.com/category/extensions/feed/" rel="self" type="application/rss+xml" />
	<link>http://einaregilsson.com</link>
	<description>A site for my programming pet projects</description>
	<lastBuildDate>Fri, 10 Feb 2012 09:54:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>ClipboardDiff Visual Studio Extension</title>
		<link>http://einaregilsson.com/clipboarddiff-visual-studio-extension/</link>
		<comments>http://einaregilsson.com/clipboarddiff-visual-studio-extension/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 11:36:09 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=430</guid>
		<description><![CDATA[tl;dr: I made a Visual Studio 2010 extension, ClipboardDiff, which lets you diff selected text against the contents of the clipboard. Get it in the Visual Studio Gallery. Follow me on twitter @einaregilsson to get notified of updates and other cool extensions and stuff. At work I sometimes run into &#8220;evil twin&#8221; methods in our codebase. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>tl;dr:</strong> I made a Visual Studio 2010 extension, ClipboardDiff, which lets you diff selected text against the contents of the clipboard. <a title="Download the extension" href="http://visualstudiogallery.msdn.microsoft.com/a7519ab0-6029-49f3-9243-a74d1718a5bb">Get it in the Visual Studio Gallery</a>.</p>
<p><em>Follow me on twitter <a href="http://twitter.com/einaregilsson">@einaregilsson</a> to get notified of updates and other cool extensions and stuff.</em></p>
<p><a href="http://einaregilsson.com/wp-content/uploads/2011/04/cdlogo.png"><img class="alignright size-full wp-image-432" title="ClipboardDiff Logo" src="http://einaregilsson.com/wp-content/uploads/2011/04/cdlogo.png" alt="" width="140" height="140" /></a>At work I sometimes run into &#8220;evil twin&#8221; methods in our codebase. Those are methods that at a glance look almost exactly the same and where one has obviously been copy pasted from the other but then they have gotten out of sync over time. In these cases I try to refactor the two methods into one and add parameters for any behaviour that was different in the two original methods. But it can be hard to tell exactly what has changed between the two methods, especially when they are dozens of lines each. When dealing with this I used to copy each of the methods into their own temp file and then run a diff tool on them. After doing this a few times I got annoyed at the process, and decided to automate it somehow.<br />
<span id="more-430"></span></p>
<p>I&#8217;ve been playing around with Visual Studio extensions a lot lately and so I decided to make one for this problem. The extension is called ClipboardDiff and is now available in the Visual Studio Gallery. The idea is that you copy the first part of text you want to compare, then you select the next part you want to compare and then run the command &#8216;Diff selection against clipboard&#8217;. The command is available by right clicking on the selected text, it&#8217;s at the bottom of the context menu, or you can invoke it with they keyboard combination Alt+Shift+J. If you want to bind it to another keyboard combination just go into Tools-&gt;Customize-&gt;Keyboard and search for &#8216;ClipboardDiff&#8217;, it&#8217;ll find the command.</p>
<p><a href="http://einaregilsson.com/wp-content/uploads/2011/04/cd.png"><img class="alignnone size-full wp-image-431" title="Running the diff command" src="http://einaregilsson.com/wp-content/uploads/2011/04/cd.png" alt="" width="522" height="381" /></a></p>
<p>You can self choose which diff tool to use, to configure it go to Tools-&gt;ClipboardDiff settings and there you can enter the path to your diff tool of choice, and the arguments you want to pass to it. The placeholder values $FILE1$ and $FILE2$ will be replaced by the temp file names once you invoke the diff command. ClipboardDiff knows the default locations of a few diff programs, so if you have Perforce Merge, Tortoise Diff or WinMerge installed you won&#8217;t have to configure anything (Those are just the tools I happened to have installed on my machine). If you haven&#8217;t configured which tool to use the Settings window will be opened the first time you try to run a diff.</p>
<p><a href="http://einaregilsson.com/wp-content/uploads/2011/04/settings.png"><img class="alignnone size-full wp-image-433" title="Configure which diff tool to use" src="http://einaregilsson.com/wp-content/uploads/2011/04/settings.png" alt="" width="560" height="145" /></a></p>
<p>You can <a title="Download the extension" href="http://visualstudiogallery.msdn.microsoft.com/a7519ab0-6029-49f3-9243-a74d1718a5bb">download the extension in the Visual Studio Gallery</a> and the source, as always, is GPL&#8217;d and available on Github at <a href="https://github.com/einaregilsson/ClipboardDiff">https://github.com/einaregilsson/ClipboardDiff</a>. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/clipboarddiff-visual-studio-extension/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Stop Build on first error in Visual Studio 2010</title>
		<link>http://einaregilsson.com/stop-build-on-first-error-in-visual-studio-2010/</link>
		<comments>http://einaregilsson.com/stop-build-on-first-error-in-visual-studio-2010/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 08:08:48 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=426</guid>
		<description><![CDATA[tl;dr: StopOnFirstBuildError is Visual Studio 2010 extension that cancels the rest of a solution build if a single project fails to compile, thus saving you time. Download it in the Visual Studio Gallery. Follow me on twitter @einaregilsson to get notified of updates and other cool extensions and stuff. At work I often work on a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>tl;dr:</strong> StopOnFirstBuildError is Visual Studio 2010 extension that cancels the rest of a solution build if a single project fails to compile, thus saving you time. <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/91aaa139-5d3c-43a7-b39f-369196a84fa5">Download it in the Visual Studio Gallery</a>.</p>
<p><em>Follow me on twitter <a href="http://twitter.com/einaregilsson">@einaregilsson</a> to get notified of updates and other cool extensions and stuff.</em></p>
<p>At work I often work on a solution that has around 25 projects. When one of the projects fails to build, Visual Studio insists on trying to build the rest of the projects, even though at that point I don&#8217;t want it to, since I&#8217;m never going to run the program when some of the projects have failed. Often the other projects depend on the project that failed, and the error list gets filled with errors from those projects that just obscure the root cause of the problem. So, I figured I could probably do something about it.</p>
<p><span id="more-426"></span>I googled around for a solution and found a <a href="http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.msbuild.stoponfirstfailure.aspx">StopOnFirstFailure</a> property in MSBuild that sounded promising, but I couldn&#8217;t find any good way to hook into the VS-&gt;MSBuild relationship. I&#8217;m sure there is some way to do this, so if anyone knows, please let me know. The next thing I found was a <a href="http://stevedunns.blogspot.com/2006/10/visual-studio-build-tip.html">tip from Steve Dunn</a>. It&#8217;s a nice little macro that listens for an event that is fired after each project is built, and calls the Cancel Build command if a project has failed to build. You just open the Macro IDE, open the EnvironmentEvents.vb file and paste the following code in at the bottom:</p>
<pre class="brush: vb; title: ; notranslate">
Private Sub BuildEvents_OnBuildProjConfigDone(
                   _ ByVal Project As String,
                   _ ByVal ProjectConfig As String,
                   _ ByVal Platform As String,
                   _ ByVal SolutionConfig As String,
                   _ ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
    If Success = False Then 'The build failed...cancel any further builds.
        DTE.ExecuteCommand(&quot;Build.Cancel&quot;)
    End If
End Sub
</pre>
<p>This does 95% of what I wanted to do. But still, I&#8217;m not a big fan of Visual Studio Macros, I prefer extensions where possible so I can easily see in one place what things I have installed instead of digging through the Macro projects. I also wanted to activate the error window after cancelling the build and be able to turn this on and off easily. So, I made an extension out of it. The main thing in it is still the macro code from Steve Dunn above, but in addition the extension:</p>
<ul>
<li>Activates the error list after cancelling the build.</li>
<li>Prints a message to the Build output window, saying why the build was cancelled.</li>
<li>Adds a menu item to the Build menu, &#8220;Stop Build on first error&#8221;, which you can use to turn the functionality on and off easily.</li>
<li>Is context aware, the menu item and functionality are only available in multi-project solutions, since it would be pretty useless in a single project solution.
</ul>
<p>And that&#8217;s it. You can <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/91aaa139-5d3c-43a7-b39f-369196a84fa5">download the extension</a> in the Visual Studio Gallery, get the GPL&#8217;d source code at <a href="https://github.com/einaregilsson/StopOnFirstBuildError">https://github.com/einaregilsson/StopOnFirstBuildError</a> and follow me on Twitter <a href="http://twitter.com/einaregilsson">@einaregilsson</a> to get notified of updates and other new extensions. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/stop-build-on-first-error-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Better EventHandler names in Visual Studio 2010</title>
		<link>http://einaregilsson.com/better-eventhandler-names-in-visual-studio-2010/</link>
		<comments>http://einaregilsson.com/better-eventhandler-names-in-visual-studio-2010/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 20:36:14 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=422</guid>
		<description><![CDATA[tl;dr: I made a Visual Studio extension, EventHandler Naming, that lets you customize generated eventhandler names and use delegate inference. Not perfect, but works. Download it in the Visual Studio Gallery Follow me on twitter @einaregilsson to get notified of updates and other cool extensions and stuff. The problem I&#8217;ve been annoyed by the eventhandler [...]]]></description>
			<content:encoded><![CDATA[<p><strong>tl;dr:</strong> I made a Visual Studio extension, EventHandler Naming, that lets you customize generated eventhandler names and use delegate inference. Not perfect, but works. <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/14f62cb9-b559-4bad-9388-37da2150e238?SRC=Home">Download it in the Visual Studio Gallery</a></p>
<p><em>Follow me on twitter <a href="http://twitter.com/einaregilsson">@einaregilsson</a> to get notified of updates and other cool extensions and stuff.</em></p>
<h3>The problem</h3>
<p>I&#8217;ve been annoyed by the eventhandler names generated by Visual Studio for years. No, I don&#8217;t really want to name my methods <strong>txtName_TextChanged</strong>, which does not match any reasonable naming convention and looks ugly. I usually prefer something like <strong>OnNameTextChanged</strong> or something similar. Another thing that has annoyed me is</p>
<pre class="brush: csharp; title: ; notranslate">
this.Load +=new EventHandler(OnLoad);
</pre>
<p>instead of just</p>
<pre class="brush: csharp; title: ; notranslate">
this.Load += OnLoad;
</pre>
<p>So, I figured I could probably hack something together to fix these problems fairly easily. As it turns out, some of it was easy, some of it was hard and some of it was impossible.</p>
<p><span id="more-422"></span></p>
<h3>How do you use it?</h3>
<p><a href="http://einaregilsson.com/wp-content/uploads/2010/12/options.png"><img class="alignright size-full wp-image-424" title="options" src="http://einaregilsson.com/wp-content/uploads/2010/12/options.png" alt="" width="309" height="259" /></a></p>
<p>In the Tools menu there is a new menu item, EventHandler Naming. That will give you the options window shown here. The <em>&#8220;Pattern&#8221;</em> is what is used to create the eventhandler names, you can use two macros there, $(EventName) and $(SiteName). EventName is pretty self explanatory, but the SiteName can be the name of the variable/component that owns the event being subscribed to, the typename of the type that owns the event (in the case of static events) or again the typename if you are subscribing to events within the class that owns the event. For both $(SiteName) and $(EventName) you can specify simple transforms like changing the case to camelCase, PascalCase, UPPERCASE or lowercase.</p>
<p>The<em> &#8220;Omit $(SiteName) for own events&#8221;</em> is for the case where you are subscribing to your own, or a base class event. At least for me, if I&#8217;m in a form and am subscribing to its Load event I don&#8217;t want the handler to be named <strong>OnForm1Load</strong>, I just want it to be named <strong>OnLoad</strong>. Whereas if I&#8217;m subscribing to an event from some variable then I want the variable name (sitename) to be a part of the event handler name.</p>
<p>The <em>&#8220;Use delegate inference&#8221;</em> does what is says. Instead of getting <strong>+= new EventHandler(someName)</strong> you will get <strong>+= someName</strong>.</p>
<h3>Implementation: WinForms and WebForms</h3>
<p>Luckily the Win and WebForms designers have a fairly nice API that you can hook into. I just created my own implementation of <strong>IEventBindingService</strong> and implemented the <strong>CreateUniqueMethodName</strong> method. For all other methods I just delegated to the real event binding service. The code to hook that up is as follows:</p>
<pre class="brush: csharp; title: ; notranslate">
    internal class DesignerCreationListener
    {
        internal DesignerCreationListener(IDesignerEventService designerEvents) {
            designerEvents.DesignerCreated += (s, e) =&gt; e.Designer.LoadComplete += OnDesignerLoaded;
        }

        private void OnDesignerLoaded(object sender, EventArgs e)
        {
            IDesignerHost host = sender as IDesignerHost;
            if (host == null)
            {
                return;
            }
            host.LoadComplete -= OnDesignerLoaded;

            Type type = typeof(IEventBindingService);
            IEventBindingService originalService = (IEventBindingService) host.GetService(type);
            if (originalService == null)
            {
                return;
            }

            host.RemoveService(type);
            host.AddService(type, new DesignerEventBindingService(originalService, new PatternNameProvider()));
        }
    }
</pre>
<p><strong>DesignerEventBindingService</strong> is my class, which takes the <strong>originalService</strong> in its constructor and delegates to it as necessary.</p>
<h3>Implementation: Code Window</h3>
<p>The code window implementation is basically a big hack. It&#8217;s the C# language service that takes care of inserting event handlers there and it basically has no hooks or extension points at all. (I would love it if someone proves me wrong on this). So, here I&#8217;m basically listening to two events, <strong>BeforeKeyPress </strong>and <strong>AfterKeyPress</strong>. I&#8217;m just checking whether the keypress is a tab key, doing some heuristics to make sure that we&#8217;re actually creating eventhandlers and not just pressing tab in some arbitrary place.</p>
<p>This is the reason why you get the old eventhandler name in the tooltip first after you press +=, the new name only comes after the delegate constructor is created. I tried everything I could think of to fix that, but the language service itself is responsible for showing that tooltip and drawing text on it and I can&#8217;t hook into that.</p>
<p>The language service is also the reason I have to show the delegate constructor while we are creating the name and only remove the explicit constructor afterwards for delegate inference. If I mess with the delegate constructor before the method is created then the language service just aborts the operation and the next tab key stroke won&#8217;t do anything.</p>
<p>So, overall it&#8217;s missing some things, but for the most part it does what you&#8217;d want.</p>
<h3>Implementation: WPF / XAML</h3>
<p>And this is the impossible part. The XAML designer doesn&#8217;t use the IDesignerHost interface at all, and as far as I could tell from looking at the VSX forum there is just no way to really extend the XAML editor in any meaningful way. It&#8217;s disappointing that they didn&#8217;t use the excellent IEventBindingService there, if someone knows a way that I could hook in here then please let me know.</p>
<h3>Conclusion</h3>
<p>I hope you find the extension useful, the source is GPL and available at<a href="https://github.com/einaregilsson/EventHandlerNaming"> https://github.com/einaregilsson/EventHandlerNaming</a> . If you have any tips, suggestions, or bug requests send me an email or a tweet at <a href="http://twitter.com/einaregilsson">@einaregilsson</a>.  And please tweet and share this if you like it <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 415px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">﻿</div>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/better-eventhandler-names-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Easy way to have custom icons in Visual Studio AddIn</title>
		<link>http://einaregilsson.com/easy-way-to-have-custom-icons-in-visual-studio-addin/</link>
		<comments>http://einaregilsson.com/easy-way-to-have-custom-icons-in-visual-studio-addin/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 09:39:34 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=305</guid>
		<description><![CDATA[I&#8217;ve recently been developing a Visual Studio AddIn and I wanted to use custom icons for a command I had. Looking for a solution I found the offical MSDN article on the subject, that might possibly be the most misleading and useless article ever. Add the resource file in Visual Studio, then exclude from project, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been developing a Visual Studio AddIn and I wanted to use custom icons for a command I had. Looking for a solution I found the <a href="http://msdn.microsoft.com/en-us/library/ms228771.aspx">offical MSDN article on the subject</a>, that might possibly be the most misleading and useless article ever. Add the resource file in Visual Studio, then exclude from project, rename your images to numbers, edit with Notepad and then build satellite assemblies on the commandline? Really? That is a horrible way to do it and not at all necessary. I&#8217;ve found a very simple and easy way to add these icons without all that hassle.</p>
<p><span id="more-305"></span></p>
<p>There are a couple of other articles out there that approach the problem in a different way. Roy Osherove has an <a href="http://weblogs.asp.net/rosherove/archive/2008/08/02/howto-set-custom-visual-studio-addin-menu-icons-without-a-satellite-dll.aspx">article</a> where he shows how you can add icons without having satellite assemblies for them, but that has &gt; 350 lines of code for loading the images yourself including P/Invokes and all sorts of stuff, and I didn&#8217;t really need such a heavyweight solution. To be fair, his solution does give you the ability to use .ico files as well, and offers transparency, but transparency can be achieved in an easier way.</p>
<p>I also found <a href="http://www.mztools.com/articles/2005/MZ2005007.aspx">another article</a> that shows how you can get transparency by using a special color that, as far as I know, is not documented anywhere officially. It also shows you how you can build a satellite assembly from Visual Studio without resorting to Notepad and the command line tools, but this approach uses a special project for the satellite assembly, which is completely unneccessary.</p>
<p>So, finally, my approach, which is really a refinement of the last article I mentioned. If your add-in is meant for only VS2008 then you can get away with having no satellite assembly, VS2008 falls back to looking in your main assembly for the resources if it doesn&#8217;t find a satellite assembly. If you want to support VS2005 you must have a satellite assembly for it to work.</p>
<p>So, assume we have a project named MyProject which will compile to an assembly called MyProject.dll. Here&#8217;s what you do:</p>
<ol>
<li>Add a new &#8216;resources&#8217; item to the project and give it the name &#8220;MyProject.en.resx&#8221;.</li>
<li>Go the properties for the item and set the &#8220;Custom Tool&#8221; field to nothing. This will remove the auto-generated code for the .resx file</li>
<li>Double click on the .resx file, this opens up the resource editor. There you can add an existing image (Add Resource-&gt;Add existing file). The image should be a 16&#215;16 pixel bitmap image. It must be in True Color format. True Color is the same as a 24-bit bitmap.</li>
<li>If you want transparency in your image then use the special color: Red:0 , Green:254, Blue:0 . Note that it really is 254, NOT 255.</li>
<li>In the resource editor, give your image a name that is a number, e.g. 1. Note that you did not have to change the filename of your image file, just its name in the resource file.</li>
<li>Now add your command in your add-in file like so:
<pre class="brush: csharp; title: ; notranslate">
commands.AddNamedCommand2(_addInInstance, &quot;MyProject&quot;,
      &quot;MyProject&quot;, &quot;Executes the command for MyProject&quot;,
      false,
      1,
      ref contextGUIDS,
      (int)vsCommandStatus.vsCommandStatusSupported +
      (int)vsCommandStatus.vsCommandStatusEnabled,
      (int)vsCommandStyle.vsCommandStylePictAndText,
      vsCommandControlType.vsCommandControlTypeButton);
</pre>
<p>There you must send in <strong>false</strong> and then the number you gave the icon in your .resx file, e.g. 1.</li>
</ol>
<p>Now when you compile your solution, a satellite assembly with the name MyProject.resources.dll will automatically be created and put in a &#8220;en&#8221; subfolder of your output folder. If you only want to support VS2008 and don&#8217;t want satellite assemblies then you can name the .resx file just MyProject.resx instead, then no satellite assemblies will be created and the resources will be embedded in the main assembly.</p>
<p>And that&#8217;s it. Add more resources in Visual Studio, have things compile automatically and don&#8217;t touch command line tools, notepad, hundreds of lines of custom code or anything like that. I *think* it is enough to just have the &#8220;en&#8221; resources dll even if you use another locale, I&#8217;m pretty sure Visual Studio falls back to that if it doesn&#8217;t find a satellite assembly in your current culture. At least I have my Windows set up for Danish, but my &#8220;en&#8221; satellite assembly works just fine for me. Although, there doesn&#8217;t exist a Danish version of Visual Studio, maybe this works differently for localized Visual Studio, e.g. the German version. If someone has one of those and can test I&#8217;d love to know whether it falls back to &#8220;en&#8221; if nothing else is available. If it doesn&#8217;t then just create a few more MyProject.de.resx, MyProject.es.resx etc. to get more satellite assemblies. But like I said before, VS2008 and later doesn&#8217;t need this, so for that case just have the one MyProject.resx file.</p>
<p>I&#8217;ve made the small MyProject example and it can be <a href="/download/MyProjectAddIn.zip">downloaded here</a>. Let me know if this works (or doesn&#8217;t) for you. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/easy-way-to-have-custom-icons-in-visual-studio-addin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ZenCoding Visual Studio AddIn</title>
		<link>http://einaregilsson.com/zen-coding-visual-studio-addin/</link>
		<comments>http://einaregilsson.com/zen-coding-visual-studio-addin/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 23:05:08 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[ZenCoding]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=289</guid>
		<description><![CDATA[4 Feb 2010: I&#8217;m no longer working on this addin and cannot provide support for failed installations. I gave some information about possible causes for failed installations in this comment, you can see if that helps, or discuss with other users in the comments. There are unlikely to ever be new versions of this addin [...]]]></description>
			<content:encoded><![CDATA[<p><strong>4 Feb 2010:</strong> I&#8217;m no longer working on this addin and cannot provide support for failed installations. I gave some information about possible causes for failed installations in <a href="http://einaregilsson.com/2009/11/12/zen-coding-visual-studio-addin/#comment-70702">this comment</a>, you can see if that helps, or discuss with other users in the comments. There are unlikely to ever be new versions of this addin published by me, but Boris Sevo has forked the project and has a version with a newer zencoding library at <a href="http://zencoding.codeplex.com/">http://zencoding.codeplex.com/</a>. So go there for updates <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The original blog post about the architecture of the addin is now completely out of date as 90% of the addin is now written in IronPython with just a tiny shim layer of C# to instantiate the IronPython classes. I&#8217;ll probably write a blog post about that architecture seperately (or generally about how to write addins for VS in IronPython). Those interested in the architecture can download the source and look at it.</p>
<p>A nice tutorial with screenshots on how to setup the keyboard mappings and use the addin has just been posted at <a href="http://www.netsi.dk/wordpress/index.php/2009/12/02/zen-coding-a-very-fast-way-of-generating-html-elements-in-your-editor/">http://www.netsi.dk/wordpress/index.php/2009/12/02/zen-coding-a-very-fast-way-of-generating-html-elements-in-your-editor/</a> so go there for your setup instructions. <del>And there is now a dedicated rss feed for updates at <a href="http://einaregilsson.com/zcupdates.aspx ">http://einaregilsson.com/zcupdates.aspx </a>so subscribe to that to be notified of new versions. Eventually I&#8217;ll put an update check in the addin itself.</del></p>
<p><strong>ZenCoding.VisualStudio v1.1.0.333</strong></p>
<p><a title="MSI installer for the add-in" href="http://einaregilsson.com/download/ZenCoding.VisualStudio.msi">DOWNLOAD ADD-IN</a> || <a title="Download zip file with source code" href="http://einaregilsson.com/download/ZenCoding.VisualStudio.zip">DOWNLOAD SOURCE</a></p>
<p><span id="more-289"></span><strong>RELEASE NOTES:</strong></p>
<p><strong>Version 1.1.0.333, 02.12.2009:</strong></p>
<p>This release adds the following features</p>
<ul>
<li>Uses latest version of zencoding python library</li>
<li>Wrap with abbreviation command</li>
<li>Use, create, edit, delete custom snippets (Tools-&gt;Options-&gt;ZenCoding)</li>
<li>Icons for commands</li>
<li>Some bug fixes</li>
</ul>
<p>Also a total rewrite with everything now written in IronPython</p>
<p><strong>Version 1.0, 12.11.2009:</strong></p>
<p>Initial release, features:</p>
<ul>
<li>Expand abbreviation</li>
</ul>
<p><strong>Original blog post follows:</strong></p>
<p>Earlier this week, while reading <a href="http://secretgeek.net">Leon Bambrick&#8217;s blog</a> I learned about a set of plugins, named &#8220;zen-coding&#8221;, which expand snippets of css-selector like code into full blown html and/or css elements. <a href="http://secretgeek.net/zen_coding.asp">Leon&#8217;s article</a> explains it better and there is also some documentation on the <a href="http://code.google.com/p/zen-coding/">official zen-coding page</a>. Anyway, this plugin is available for a number of text editors and IDE&#8217;s but not for Visual Studio. I use Visual Studio a lot and wanted zen-coding in there, so I decided to try and whip up a small add-in for it.</p>
<p>The original library is available both as JavaScript and Python. I really didn&#8217;t want to port or alter anything in the original code, since for future versions I want to be able to just drop in a couple of files from the original library and rebuild the add-in. The obvious choice then was to see if the library would work with IronPython. Fortunately the zen-coding library is just basic string manipulation and worked perfectly with IronPython right out of the box.</p>
<p>The next challenge was figuring out how to call into that from the add-in, which I wanted to write in C#. The library contained two functions I needed to be able to call. That turned out to be surprisingly easy. <del>I made a wrapper class, ZenCodingEngine, which has two delegates with the correct method signatures, and then in its constructor I ran some custom python import code, and got references to the functions I needed and assigned them to my delegates.</del> I changed that approach in the latest version and moved almost all of the logic into Python instead. Now the C# AddIn is very thin, it basically just receives events from VS and invokes a single exec_command() method that is defined in a python script. The interesting parts of that code are below:</p>
<pre class="brush: csharp; title: ; notranslate">
private void ReloadPython()
{
  ScriptEngine engine = Python.CreateEngine();
  ScriptScope scope = engine.CreateScope();
  string folder = Path.GetDirectoryName(new
    Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
  List searchPaths = new List&lt;string&gt;(engine.GetSearchPaths());
  searchPaths.Add(folder);
  engine.SetSearchPaths(searchPaths);
  scope = engine.ExecuteFile(Path.Combine(folder, &quot;vs_zen_coding.py&quot;));
  engine.SetVariable(scope, &quot;App&quot;, _applicationObject);
  execCommand = engine.GetVariable&lt;Func&lt;bool&gt;&gt;(scope, &quot;exec_command&quot;);
}

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
  handled = false;
  if (!CanExecuteCommand(commandName, executeOption))
  {
    return;
  }

  try
  {
    #if RELOAD_EVERY_TIME
    ReloadPython();
    #else
    if (execCommand == null)
    {
      //Load first time, to avoid making the VS startup slower
      ReloadPython();
    }
    #endif

    execCommand();
  }
  catch (Exception ex)
  {
    DisplayError(ex);
  }
  handled = true;
}</pre>
<p>The ReloadPython() method is not called on startup, instead it is called the first time the command is called. IronPython performs well, but there is still some startup cost and I don&#8217;t want to add that to the startup time of Visual Studio. Instead you pay the price the first time you actually need it. Then we have the Exec method which is from a VS interface and is called when the command is invoked. There I use a compilation constant, RELOAD_EVERY_TIME, to check if I should reload my python script every time I invoke the command. This has proved to be INCREDIBLY useful, instead of constantly starting VS up again and again I just edit my python script, save it and the next time I execute the command the new code is used. And of course I turn this constant off for release builds.</p>
<p>Another nice trick to play around with the VS API, start up an IronPython shell and type:</p>
<div style="background-color:black; color:white;">
&gt;&gt;&gt; from System.Runtime.InteropServices import Marshal<br />
&gt;&gt;&gt; app = Marshal.GetActiveObject(&#8220;VisualStudio.DTE.9.0&#8243;)<br />
&gt;&gt;&gt; app.ActiveDocument.Name<br />
&#8216;ZenCodingAddIn.cs&#8217;
</div>
<p>This gets you a reference to the running instance of VS2009 which you can then experiment with.</p>
<p>You can download <a href="/download/ZenCoding.VisualStudio.msi">an installer</a> for the add-in or get <a href="/download/ZenCoding.VisualStudio.zip">the source</a>. The add-in is licensed under the GPL v3, the same license the zen-coding library uses. The add-in works for Visual Studio 2005, 2008 and 2010 beta 2.</p>
<p>Now, once you&#8217;ve installed the add-in, nothing happens. Well, actually what happens is there&#8217;s a new command available in Visual Studio, named <strong>ZenCoding.Expand</strong>, or <strong>ZenCoding.VisualStudio.ZenCodingAddIn.Expand</strong>, depending on where you are looking for it. (The keyboard mapping uses the full command id, but when adding it to a toolbar you get a shorter friendly name for it). By default it is not mapped to any keyboard combination. This is because <del>it&#8217;s late and I can&#8217;t be bothered to figure it out</del> I strongly believe that add-ins should not force a keyboard shortcut on you, possibly overriding something you have set yourself. So, map the command to your preferred keyboard shortcut (mine is Ctrl+E,Ctrl+K). Or you can drag the command  onto a toolbar, which is fairly useless, but possible.</p>
<p>Enjoy!</p>
<p><strong>UPDATE 13.11.2009: </strong> The main zen-coding developer also told me that I&#8217;m using an older version, the latest version has a lot more features, including user defined snippets. I&#8217;ll start looking at that when I have time, which won&#8217;t be just yet. </p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/zen-coding-visual-studio-addin/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>URL-2-Titlebar Firefox Add-on</title>
		<link>http://einaregilsson.com/url-2-titlebar-firefox-add-on/</link>
		<comments>http://einaregilsson.com/url-2-titlebar-firefox-add-on/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 11:48:04 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mozilla]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=275</guid>
		<description><![CDATA[Recently I got a request in the mail from a user of one of my Firefox add-ons. He asked me if I could make an add-on that displayed the url of the current tab in the titlebar instead of the actual title of the page being shown. I&#8217;m not interested in creating more add-ons for [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I got a request in the mail from a user of one of my Firefox add-ons. He asked me if I could make an add-on that displayed the url of the current tab in the titlebar instead of the actual title of the page being shown. I&#8217;m not interested in creating more add-ons for Firefox or Thunderbird, they take up quite a lot of time with user requests, getting approved by <a href="http://addons.mozilla.org">the Mozilla addons site</a>, etc. That is why I&#8217;ve <a href="/projects/abandoned/">discontinued</a> 6 out of the 9 add-ons I&#8217;ve created. But anyway, I knew that it would be trivial to make this add-on so I decided to help this person out.<span id="more-275"></span></p>
<p>You can <a href="/download/url2titlebar.xpi">download the add-on</a> here. This add-on will never be updated or published on AMO, it is offered strictly as-is. The one thing I found interesting about this was figuring out how to do the absolute minimum to get this add-on working. There is no localization, no folder structure, just three files, install.rdf, chrome.manifest and url2titlebar.xul. Put those into a zip file, rename it to .xpi and you have your extension. These of course are not best practices in extension development, just an exercise in making the simplest extension possible. Below is the full content of the three files:
</p>
<h5>url2titlebar.xul</h5>
<pre class="brush: xml; title: ; notranslate">&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot;?&amp;amp;gt;
&amp;amp;lt;overlay xmlns=&amp;amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;amp;quot;&amp;amp;gt;
  &amp;amp;lt;script&amp;amp;gt;
    window.addEventListener('load', function() {
      function setTitle() {
        document.title = gBrowser.selectedBrowser.contentDocument.location;
      }
      gBrowser.tabContainer.addEventListener('TabSelect', setTitle, false);
      document.addEventListener('pageshow', setTitle, false);
      setTitle();
    },false);
  &amp;amp;lt;/script&amp;amp;gt;
&amp;amp;lt;/overlay&amp;amp;gt;
</pre>
<h5>chrome.manifest</h5>
<pre class="brush: plain; title: ; notranslate">
content url2titlebar file:./
overlay chrome://browser/content/browser.xul chrome://url2titlebar/content/url2titlebar.xul
</pre>
<p><strong>install.rdf</strong></p>
<pre class="brush: xml; title: ; notranslate">&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot;?&amp;amp;gt;
&amp;amp;lt;RDF xmlns=&amp;amp;quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;amp;quot;
 xmlns:em=&amp;amp;quot;http://www.mozilla.org/2004/em-rdf#&amp;amp;quot;&amp;amp;gt;
  &amp;amp;lt;Description about=&amp;amp;quot;urn:mozilla:install-manifest&amp;amp;quot;&amp;amp;gt;
    &amp;amp;lt;em:id&amp;amp;gt;url2titlebar@einaregilsson.com&amp;amp;lt;/em:id&amp;amp;gt;
    &amp;amp;lt;em:name&amp;amp;gt;URL-2-Titlebar&amp;amp;lt;/em:name&amp;amp;gt;
    &amp;amp;lt;em:version&amp;amp;gt;0.0.1&amp;amp;lt;/em:version&amp;amp;gt;
    &amp;amp;lt;em:creator&amp;amp;gt;Einar Egilsson&amp;amp;lt;/em:creator&amp;amp;gt;
    &amp;amp;lt;em:description&amp;amp;gt;Set the title of the window to the current url&amp;amp;lt;/em:description&amp;amp;gt;
    &amp;amp;lt;em:targetApplication&amp;amp;gt;
      &amp;amp;lt;Description&amp;amp;gt;
        &amp;amp;lt;em:id&amp;amp;gt;{ec8030f7-c20a-464f-9b0e-13a3a9e97384}&amp;amp;lt;/em:id&amp;amp;gt; &amp;amp;lt;!-- firefox --&amp;amp;gt;
        &amp;amp;lt;em:minVersion&amp;amp;gt;3.0&amp;amp;lt;/em:minVersion&amp;amp;gt;
        &amp;amp;lt;em:maxVersion&amp;amp;gt;3.5.*&amp;amp;lt;/em:maxVersion&amp;amp;gt;
      &amp;amp;lt;/Description&amp;amp;gt;
    &amp;amp;lt;/em:targetApplication&amp;amp;gt;
  &amp;amp;lt;/Description&amp;amp;gt;
&amp;amp;lt;/RDF&amp;amp;gt;
</pre>
<p>So, there you have it, the simplest possible Firefox add-on. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/url-2-titlebar-firefox-add-on/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mozilla Extension Generator</title>
		<link>http://einaregilsson.com/mozilla-extension-generator/</link>
		<comments>http://einaregilsson.com/mozilla-extension-generator/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 01:00:17 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/01/mozilla-extension-generator/</guid>
		<description><![CDATA[Since I started creating Mozilla extensions I spent a lot of time writing boilerplate code, and copying from one extension from the next. Then I found <a href="http://ted.mielczarek.org/code/mozilla/extensionwiz/">Ted Mielczarek's Extension Generator</a> which is a great page that takes some parameters and creates an extension ready for you to use. I used that for some time but in the end I still kept modifying each extension to fit my own style and include my library functions. So I decided to make my own Mozilla Extension Generator in Python]]></description>
			<content:encoded><![CDATA[<p>Since I started creating Mozilla extensions I spent a lot of time writing boilerplate code, and copying from one extension from the next. Then I found <a href="http://ted.mielczarek.org/code/mozilla/extensionwiz/">Ted Mielczarek&#8217;s Extension Generator</a> which is a great page that takes some parameters and creates an extension ready for you to use. I used that for some time but in the end I still kept modifying each extension to fit my own style and include my library functions. So I decided to make my own Mozilla Extension Generator in Python<span id="more-44"></span>, that would spit out extensions exactly the way I want them. I mostly liked the output of Ted&#8217;s generator so I took an extension from it, modified to fit my style and then created the generator to create it. So if you see similarities between extension my by generator and Ted&#8217;s, that&#8217;s the reason, mine is partially based on his output. I&#8217;ve decided to put the generator on my page for others to download, and they can then tweak it to fit their ideas of what should be in all extensions. </p>
<p>When the script is run the output will look something like this:</p>
<pre style="background-color:black; color:white; font-family:monospace; font-size:11pt;">
Mozilla Extension Generator
Copyright (c) 2007 Einar Egilsson (http://einaregilsson.com)

Extension name: My New Extension
Extension code name: MyNewExtension
Extension description: Does this and that
Firefox or Thunderbird (f/t) : f
Include Menu item ? (y/n): y
Include Context menu ? (y/n): y
Author name: Einar Egilsson
Guid: mynewextension@einaregilsson.com
Menu item label: My New MenuItem
Menu item access key: m
Contextmenu item label: My New ContextItem
Contextmenu item access key: c

Creating files...

mynewextension/install.rdf
mynewextension/chrome.manifest
mynewextension/defaults/preferences/mynewextension.js
mynewextension/chrome/content/mynewextension.js
mynewextension/chrome/content/overlay.xul
mynewextension/chrome/skin/overlay.css
mynewextension/chrome/content/mynewextension.png
mynewextension/chrome/content/mynewextensionlib.js
mynewextension/chrome/locale/en-US/mynewextension.dtd
mynewextension/chrome/locale/en-US/mynewextension.properties

Extension complete
</pre>
<p>This will generate all the necessary files for the extension, you can simply go into the folder created, zip it up, name as .xpi and install the extension. The questions are pretty straight forward, just about the only thing you need to know is that Extension code name must be a single word, because it will become a javascript object that all your other code will be wrapped in, to avoid polluting the global namespace. </p>
<p>Now, some features of the generated extension:</p>
<ul>
<li>Localization ready, strings are in .dtd and .properties files</li>
<li>Includes a library file with common function like accessing your prefs, writing to a file and more.</li>
<li>Includes debug functions that output to the javascript console and can be turned off in about:config</li>
<li>Can include menu item and context menu commands</li>
<li>&#8230;and more, just look at the generated files</li>
</ul>
<p>The generator is a single python file that includes all the other files. It should be easy to modify for anyone with a working knowledge of python. There are a few constants at the top that you can tweak, they are:</p>
<pre class="brush: python; title: ; notranslate">
#Constants
#Fill these out so you won't get prompted for them everytime:
YOUR_NAME       = ''
YOUR_DOMAIN     = '' # if filled out the extension guid will be of
                     #the form extensionname@yourdomain.com
FIREFOX_MIN     = '2.0'
FIREFOX_MAX     = '2.0.0.*'
THUNDERBIRD_MIN = '2.0'
THUNDERBIRD_MAX = '2.0.0.*'
INITIAL_VERSION = '0.9'
</pre>
<p>The icon file is also embedded in the file as a base64 encoded string. To replace it with your own file, create a .png image file (34&#215;34 pixels) and run the script below (<a href="/download/base64.py">download</a>) to create the base64 string, then copy it over the existing base64 string in the extension.py file.</p>
<pre class="brush: python; title: ; notranslate">
import sys, base64

if len(sys.argv) == 1:
    print 'Usage: base64 &amp;lt;filename&amp;gt;'
    sys.exit(0)

b64 = base64.b64encode(open(sys.argv[1], 'rb').read())
i = 0

while i &lt; len(b64):
    print b64[i:i+80]
    i += 80
</pre>
<p>And finally, you can <a href="/download/extension.py">download the python script here</a>. You can also <a href="/download/extension.py.html">view it syntax highlighted here</a> but don&#8217;t copy it from there as the syntax highlighting might screw something up. Enjoy <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/mozilla-extension-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

