Sharing dotfiles between Windows and Ubuntu

Link. May 9, 2008. Comments [0]. Posted in: Tools

I have several configuration files I keep on my home folder for several applications, most of which is what in Unix-speak is usually known as "dotfiles". This includes things like my VIM configuration and runtime files, my Unix and PowerShell profile scripts and so on.

Naturally, I want these to be available on all of my machines, and synchronizing them manually has always been a drag, but so far, I had not really looked too hard at how to avoid it.

The most natural way to do this was, of course, source control, which I wanted to have anyway. It was easy enough to use SVN or GIT for this, but one thing had prevented me from going this way: File name conventions.

A few weeks ago I ranted about how some cross-platform applications would use a different set of file/folder names when running on windows instead of Unix, and how this was a nasty legacy coming from the old FAT/FAT32 days, but was not so much of a problem with NTFS.

There was a point to that rant, very related to today's post: What prevented me from using simple source control to share my dotfiles between my Windows machines and my Ubuntu machines was exactly this issue with the renamed dotfiles.

In particular, one of the things that constantly nagged me was having to rename my _vimrc and vimfiles/ directories from Windows to .vimrc and .vim/ when synchronizing them to the Ubuntu machines. It was a royal pain, and one that source control wouldn't solve at all.

Fortunately, turns out there's an easy way to avoid this with GVim on Windows, so that I could use .vimrc and .vim/ there as well:

  1. It appears that .vimrc is natively supported in modern VIM versions. I don't know when this came to happen, but I just renamed my _vimrc to .vimrc and it just worked. Pure goodness.
  2. Renaming vimfiles/ to .vim didn't work right out. But, fortunately, you can change the set of folders that VIM will look into when loading runtime files by modifying the runtimepath option. So I just added this right at the top of my .vimrc file:
set runtimepath=~/.vim,$VIMRUNTIME,~/.vim/after

Restarted VIM, and it just worked. Fantastic! I'm now simply using git to keep my dotfiles in source control and synchronizing them between machines is simply a matter of doing git pull/push every once in a while. Very nice.

Don't forget the BOM!

Link. May 9, 2008. Comments [0]. Posted in: .NET | BizTalk

If you're writing text files in an encoding that supports a Byte-Order Mark, you should always try to make sure that you include it, unless you have a protocol in place that precludes you from doing so (such as a legacy application that doesn't know how to deal with them).

One of the reasons you should always remember the BOM is that many applications can use it to try to guess what encoding they should use when trying to read the text you're feeding them.

Encoding detection based on the BOM is not foolproof, but it's better than having nothing at all, particularly in cases cases where simply assuming blindly a predefined encoding such as UTF-8 or UTF-16 might not be an option at all.

One particular case where remembering the BOM is very important is with UTF-8. Let me tell you a story to illustrate why:

We've been working the past few weeks on testing and improving a BizTalk-based solution for a client that some other consulting company had created. One particular piece had been working fine until, suddenly, we started getting an error when BizTalk tried to parse an incoming message with an error stating that "The character is not valid on the specified encoding".

Looking at the message, it was supposed to be UTF-8 encoded, and for the most part looked OK. The character causing trouble was, in fact, a 0xA0 char (non-breaking space) inside an element value. While this was not good, it wasn't clear why it was causing trouble.

Since it was an XML message, we opened it up in Internet Explorer: Yep, that too parsed it incorrectly and got stuck when it reached the problematic character.

Looking a bit further , we found that in this particular case, the original developer had written a piece of code that created a Stream object with the message contents and then fed that to BizTalk. The code looked a bit like this:

public static Stream CreateStream(String msg) {
   MemoryStream stream = new MemoryStream();
   byte[] bytes = Encoding.UTF8.GetBytes(msg);
   stream.Write(bytes, 0, bytes.Length);
   stream.Position = 0;
   return stream;
}

The message text itself was a piece of XML that included an <?xml?> declaration with the encoding attribute specifying UTF-8. This seemed OK, even if the code above just seemed like a pretty uncomfortable way of creating the stream.

However, this gave us a clue: UTF8Encoding.GetBytes() won't give you a BOM. Looking at the message in a binary editor, we validated indeed the message did not have a BOM at all. So we tried replacing the code above to one that simply used a StreamReader object (which uses UTF-8 with a BOM by default), and that fixed the issue right away!

This highlights why the BOM is so important for UTF-8: The basic characters in the set share the same values as the ASCII code. This is generally an advantage, but it can also mean that stuff that's incorrectly coded (such as our example above) might seem to work fine for a while until an unexpected character comes along and everything crumbles down. This is unlike other encoding, such as UTF-16, where things usually blow up right away.

In this particular case, the culprit was really a combination of factors: The lack of a BOM together with the presence of the encoding specification in the XML declaration [1]. I'm not sure why the XML stacks get stuck on a BOM-less UTF-8 file with an encoding declaration, but there you have it. So don't forget the BOM!

[1] I personally thing the encoding specification in the XML declaration is probably the single most stupid idea included in the XML spec. It's just downright evil.

Bitten by IEnumerable<T>

Link. May 7, 2008. Comments [2]. Posted in: .NET

Yesterday I was working on some fairly complex code involving some LINQ to SQL, lots of generics (with some reflection generously sprinkled on top) and some extension methods to complete the cake.

Most was working as expected, until I ran into a little snag. I had one line of code looking a bit like this:

DoSomething<MyType>(someCollection);

When I ran my NUnit tests, it appeared as if DoSomething() had never been executed. I knew that someCollection had some items in it, so I was pretty surprised. I fired up a debugger, put a breakpoint in the first line in of DoSomething and execute the code. No dice, the breakpoint wasn't even hit.

So I went one step further and put a breakpoint just at the call to DoSomething. The breakpoint got hit, pressed F11 (step into) and.... it went right to the next line without stepping into the method. What was up with that?

After scratching my head a lot, I realized it had been me being clever that caused the problem. See, DoSomething was basically a map of sorts, defined somewhat like this:

IEnumerable<T> DoSomething<T>(IEnumerable list) {
   foreach ( var obj in list )
      yield return obj.Map<T>();
}

Can you spot the problem?

I had created the method so that I could reuse the transformed object stream after the mapping, just in case I needed it. However, on this particular use I was making of the method I was disregarding the return and was simply interested in getting the internal function called for each item in the collection.

This turned out to be a really bad idea, because the way the C# compiler generates the code for this is pretty smart: It's completely pipelined/streamed, such that you need to consume the returned IEnumerable<T> object. If you don't, then the collection provider never gets iterated on, and thus, nothing happened.

In hindsight, this should've been pretty obvious, and it's a very natural behavior, if you think about it. I should've been able to pick it up sooner, but I admit the debugger behavior surprised me a bit.

Vim Scripts

Link. May 1, 2008. Comments [1]. Posted in: Tools

Someone recently asked me what Vim scripts and plug-ins I use on my Vim setup. There are literally hundreds of plug-ins on the Vim site, some of them quite complex and interesting.

Though I've customized my own Vim setup quite a bit, I don't use all that many plug-ins. Here's what I normally use:

Plug-Ins

  • Calendar: Displays a calendar in a Vim buffer. Though not as complete as the functionality in Emac's calendar, it is still pretty useful :-).
  • Camelcasemotion: This one adds word-movement navigation motions that are aware of the casing of characters inside words. So, for example, if you're at the beginning of "newIdentifier" and press ",w", you'll navigate to the 'I' instead of to the end of the complete word like with "w".
    This is one of the features I missed the most from Eclipse. Unlike Eclipse, though, I rather like how with Camelcasemotion I can keep both behaviors at the same time.
  • NumberMarksNumber Marks:  Allows you to define numbered marks that behave a lot more like bookmarks in Visual Studio (you can even navigate them using F2!). The nice part about this one is that it will show the mark in a column to the left of the text, unlike regular Vim marks which are hidden.

    While on the topic of marks, there's also ShowMarks, which solves the issue of the hidden Vim marks: It also adds a new column at the left of the editing window showing which marks have been defined and where in the current document. It does its job, but I've found it to be more intrusive than Number Marks and it also seems to cause some issues with a few other plug-ins at times, which is why I don't like it as much.

  • NERD_Tree: Provides a "side pane" for browsing the file system and opening files within Vim, which is sometimes better than simply opening up a directory in the main buffer. I've hooked it up so that Ctrl+E,Ctrl+E pops up the tree.

    NerdTree
  • Obvious Mode: Great little plug-in recently released that highlights the status bar in a different color when you're in insert mode. This is particularly useful for me when working on the console version of Vim (which I do every so often) because I don't normally get the block-vs-line cursor difference that's common in GVim.
  • VimExplorer: For more complex file browsing and file manipulation options, I also use VimExplorer occasionally. This is more like Windows Explorer embedded inside Vim [screenshot], so it is a lot more complex than NERD_Tree, and has a lot more options. I use it a lot less, but it's still pretty useful.
  • Xmledit: Makes editing XML files in Vim a bit nicer, adding things like automatic insertion of closing tags.
  • Xmlpretty: Helps in doing a basic pretty print formatting of XML files. It's not very sophisticated, but it's occasionally useful. The downside of Xmlpretty is that it requires a bit more work to setup than other scripts since it doesn't configure itself automatically. I bound it to Ctrl+K, Ctrl+F.
  • Cream: Cream is sort of an alternate, highly customized distribution of Vim that makes it behave more like "normal" text editors (particularly for Windows users). I don't use Cream per-se, but I've occasionally plucked some functions from the cream source for my own Vim setup. In particular, I use Cream's functions for tab-ifying lines of text in select mode.

Syntax and Others

Besides the built-in syntax, indent and file type plug-ins that come with the Vim runtime, I use a few other ones:

  • CssColorCSS Color:  Cool little plug-in that highlights color specifications in CSS with the color they represent. I've found this particularly useful when modifying or inspecting an existing CSS file.
  • PS1: I use slightly modified versions of Peter Provost's indent, syntax and file type plug-ins to edit Windows PowerShell scripts.
  • CSharp: A slightly modified version of the Built-in cs.vim syntax file. It's ok, but could use a bunch of improvements, I mostly just browse C# code on Vim, though, so it's not that big of a deal.

Color Schemes

Like with Visual Studio, I change the color schemes I use in Vim every so often. Here are some of the ones I use the most:

  • Moria: A nice color scheme for Vim featuring versions with both dark and black background colors. Lately I've been using the version with the black background more (:Colo Black).
  • Twilight: This is another dark color scheme, with some some khaki/gold highlights. It works particularly well for source code from scripting languages like Ruby, PowerShell or Python, though not as well for C# and Java code. For some reason, however, this looks a lot better on my Ubuntu machine than on my Windows one (something to do with gamma correction I'm sure).
  • Wombat: I've talked about wombat before. Lately I haven't been in love with it so much and have switched to some of the other ones.
  • Zenburn: This is an old time favorite of many Vim users, and it's a really nice color scheme. I've been using this one a lot more lately, as it has fairly soothing colors, without being so low-contrast that it becomes harder to use with the LCD brightness turned down.
    I use Zenburn with the "zenburn_high_Contrast" option enabled, though, which makes the background slightly darker than what this screenshot shows.
    The CSS I'm using to highlight my code samples in this blog lately is Zenburn.
  • DarkSpectrum: This one I've been using lately as well. It features a darkish gray background with blue/gold/orange/green highlights and some bold white for keywords and such [screenshot]. It doesn't work as well for some languages, most notably Vim scripts themselves, though.
  • Impact: This is the one I use for console vim, as it works better with the limited color palette available there (particularly on windows).

Creating Multi-Part Messages in BizTalk

Link. May 1, 2008. Comments [0]. Posted in: BizTalk

Last year I wrote an entry about adding arbitrary binary attachments to a Multi-Part message in BizTalk 2006 using a helper C# component. That entry has been fairly popular (as far as BizTalk posts go, which is not usually much!), and I occasionally get questions about it.

The sample uses a bit of clever code to insert arbitrary attachments into an existing XLANGMessage instance during construction. However, it's not very clear that this is actually a supported scenario and so there's always a chance it might get you into trouble.

I recently thought of another way, possibly much better supported to accomplish the same in BizTalk 2006, though with a little performance hit: Use the Invoke Pipelines feature in BizTalk 2006 Orchestrations.

Basically, the idea would be to create a custom pipeline component (depending on what you need it could be a regular encoding/pre-assemble or an assembler component), generate a new multi-part message in it, and return that to the orchestration.

I haven't tried it yet, but I think it should work.

Vimperator and Firefox 3.0

Link. April 30, 2008. Comments [0]. Posted in: Tools

Just so that I know where to find them (as I always forget):

You can find Vimperator 0.6 builds that work with the Firefox 3.0 betas here: http://www.calmar.ws/firefox/vimperator/

They are updated every so often, so it's useful to keep checking them. I've been using them a few weeks now and it's been working mostly fine!

BizTalk 2006, MSMQ and BizTalk 2002

Link. April 28, 2008. Comments [0]. Posted in: BizTalk

A little over a year ago I posted an entry about a new feature included in the MSMQ Send Adapter in BizTalk 2006 which provided a way to send messages through MSMQ from BizTalk to legacy applications that required the message contents to be formatted using the ActiveXFormatter, or that simply used the old COM-based MSMQ API.

However, I didn't mentioned anything about how to enable the opposite scenario: How to receive and parse a text message in BizTalk 2006 coming from one of those legacy applications. Someone recently asked about this on the BizTalk Newsgroup and, as It turns out, BizTalk Server 2002 happens to fall in this category as well.

Fortunately, this scenario is a lot simpler, if we assume that the contents of the message will be plain text (say, some string content or XML). The ActiveX message serialization rules dictate that a message with a BodyType of String will simply be serialized on the wire as a set of bytes encoded using UCS-2 (UTF-16LE), without a Byte Order Mark (BOM).

BizTalk can parse UTF-16 contents without problems, but without a BOM, the standard disassemblers can't figure out the encoding of the message on it's own.

Fortunately, most of the time you can work around this by setting the message Charset before parsing (for example using my own FixEncoding pipeline component).

Xubuntu 8.04 Installed

Link. April 26, 2008. Comments [2]. Posted in: Linux

Ubuntu 8.04 (Hardy Heron) came out this week, and I downloaded it right away via torrent. I decided to go this time with Xubuntu, which uses XFCE as the desktop environment instead of the regular Gnome or KDE based editions.

Xubuntu804

I had already fooled around with XFCE before and found it to be an excellent desktop environment overall, and much more lightweight than both Gnome and KDE. I was also using Xubuntu 7.10 already on a virtual machine on my Vista box using Virtual Box [1] and it worked very well for my needs (which I admit are fairly basic).

As expected, installation went very well. The only problem I had during installation was that my data drive (a partition I mount my /home in) ended up getting formatted accidentally. I should've paid more attention during partitioning! Fortunately, there was nothing there that was irreplaceable.

After installation, I proceeded to do some tweaking and customization, and enabled the ATI restricted X11 driver using Driver Manager. This time, it worked flawlessly right out of the box, and suspend seems to be working correctly without having to fool around with it at all.

I then went ahead and installed some of the tools I use more often on Linux, like Vim, gnome-terminal, tofromdos and all the samba tools. I now have to install ruby, mono, Open Office (probably), Virtual Box and a few others, but those can wait a bit, as the Canonical servers were still getting hammered pretty hard yesterday. They seem a lot better today, so maybe I'll get some of that installed later on.

Overall, I am very pleased with the experience so far!

[1] I turned to Virtual Box for a couple of reasons: It runs Linux very nicely, unlike Virtual PC, which just sucks at that. I had also been using Virtual Box on a Linux host, and worked very nicely there as well (and seamless mode for Windows-based guests is very nice!).

Registering Null Adapter on 64-Bit Machines

Link. April 24, 2008. Comments [0]. Posted in: BizTalk

Shashikant Raina was kind enough to share with me an issue he ran into while trying my /dev/null Adapter for BizTalk Server on a 64-bit machine:

After creating the adapter registry keys using the included .reg file, he noticed that the adapter would not appear listed in the BizTalk Administration Console when trying to add the adapter to BizTalk.

Here's how Sashikant fixed it:

On 64 bit machine, when you simply click on a registry key file (i.e NullAdapter.reg) it creates entries in default registry editor on that machine. When you then go to Biztalk admin and try to add Null adapter, you can't see it is an option from list of available adapters to add.
Reason, bring that the entries were created in the wrong registry editor (64 bit). To get the correct registry entries, run this registry file
using regedit.exe located in \Windows\SysWOW64 on the 64 bit machine.
This will create Null adapter entries in 32 bit editor. You can then go to Biztalk admin and add the null adapter.

So if this happens to you, just make sure the registry entries are imported into the right place!

Thanks Shashikant!

MVP Summit 2008

Link. April 11, 2008. Comments [0]. Posted in: Personal

Provided the American Airlines problems from this week get sorted out, I'll be attending the MVP Summit again this year. I'll be arriving Sunday pretty late but will be staying until Friday.

If anyone wants to meet, chat, or just say Hi, send me an email (tomas at winterdom.com) or ping me via twitter.

About

Tomas Restrepo is co-founder of devdeo ltda. His interests include .NET, Connected Systems, PowerShell and, lately, dynamic programming languages. More...

email: tomas@winterdom.com
msn: tomasr@passport.com
twitter: tomas_restrepo

Technorati Profile

devdeo logo

View my profile on LinkedIn

MVP logo

Syndicate

Ads

Links

Tag Cloud

.NET (231) Architecture (47) ASP.NET (6) BizTalk (170) Blogging (64) C++ (3) Castle (2) Commerce Server (3) Development (118) DLR (7) Enterprise Services (25) Fonts (4) Host Integration Server (1) LINQ (3) Linux (5) NHibernate (1) Personal (143) PowerShell (22) QuickCounters (4) Tools (74) Vista (38) VS Color Scheme (10) VSTO (2) WCF (64) Web Services (87) WinFX (80) Workflow (47) WPF (5) XML (21)

Statistics

Total Posts: 986
This Year: 56
This Month: 6
This Week: 4
Comments: 752

Blogroll

Post Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 1.9.7174.0

Sign In