One of my favorite things about Windows 7 was the Aero Snap feature, which let you easily maximize / restore windows and resize them to take up half of the screen. There is plenty of room to have two windows side by side – especially since widescreen monitors have become commonplace – and one often has the need to compare the contents of two windows.

If you are running Gnome and Metacity, then you too can have an Aero Snap like feature using these commands. Metacity comes with support for assigning commands to keyboard shortcuts, and I have formulated a command that will read the current resolution of your screen and resize the current window to be either on the left half or the right. You will need the following programs installed: grep, awk, xwininfo, and wmctrl; all except wmctrl are probably already installed if you are running a mainstream distro.

There are a few drawbacks to this implementation. One is that the windows are resized, and cannot be reverted to their previous size by repeating the keyboard shortcut. Although I have not tested this on a multiple screen setup, I doubt it would work correctly. And finally, these commands will not resize a maximized window – you must restore it first.

The following commands will set this up for you if you are running Gnome / Metacity; just paste them into a terminal. The commands themselves are not Metacity specific and can be used with just about any window manager, but if you are not using Metacity you won’t be able to use gconftool-2 to apply them.

gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_1 "<Super>Left"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_1 "wmctrl -r :ACTIVE: -e 0,0,0,`xwininfo -root | grep Width | awk '{ print (($2/2)-6)}'`,`xwininfo -root | grep Height | awk '{ print $2 }'`"

gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_2 "<Super>Right"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_2 "wmctrl -r :ACTIVE: -e 0,`xwininfo -root | grep Width | awk '{ print (($2/2)+5) ",0," (($2/2)-6) }'`,`xwininfo -root | grep Height | awk '{ print $2 }'`"

Notice that I subtract 6 pixels from half the width of the screen – this is probably due to the border around the window. Six works for my theme, but you might have to adjust this value if you get a gap or overlap with your windows. The default keybinding I chose was the same as on Windows 7: Win+← and Win+→ – you can change this to whatever you want in the Keyboard Shortcuts management app.

If you want to add a couple more Windows 7 style keyboard shortcuts, feel free to pick from this list:

gconftool-2 --type string --set /apps/metacity/window_keybindings/minimize "<Super>Down"
gconftool-2 --type string --set /apps/metacity/window_keybindings/toggle_maximized "<Super>Up"
gconftool-2 --type string --set /apps/metacity/global_keybindings/show_desktop "<Super>D"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_terminal "<Super>T"
gconftool-2 --type string --set /apps/gnome_settings_daemon/keybindings/home "<Mod4>e"

Note that for some reason, Win+E to open a Nautilus window is odd – you have to use Mod4 instead of Super, and e has to be lowercase.

Flex and Bison are used together to create parsers, usually for programming-related tasks like parsing source files or SQL statements. While not as easy on the programmer as newer libraries like Spirit OR ANTLR, they are more efficient since they generate raw C code that can be compiled into your application. They are also capable of outputting C++ code, but there is a lack of clear documentation / examples available demonstrating this.

What I present here are my efforts over the last couple of days towards creating a program that uses the two together in C++ mode. The result is trivial – numbers are recognized and divided by five – since the focus of this program is to demonstrate using Flex and Bison. If you do not know Flex and Bison, I recommend Lex & Yacc by Tom Niemann (Flex is a clone of Lex, Bison is a clone of Yacc).

Download the example source code here.

This is an example, not a tutorial – I’m not going to take you through the code line by line. I do have some notes for you, though:

  • waffleshop.y
    • The require line tells Bison that this is meant for Bison 2.4.1. While it might work on other versions of Bison, the C++ code generated is ‘experimental’ and subject to change between versions.
    • The skeleton line specifies an alternate template to use – lalr1.cc is the C++ version.
    • The parse-param options tell Bison that we want the class to have an additional member variable – the scanner. Since Bison calls the scanner to get a token, the Bison class needs to have a reference to the scanner. The lex-param option tells Bison that when it calls yylex, it should pass the scanner as an additional argument. Our implementation of yylex invokes the passed scanner and returns the result.
    • There are two sections of code: the first is ‘%code requires’. The code inside of this block is put in the generated header file as well as the c file. The other %code block contains code that only goes in the c file, and since we don’t want anything else calling the yylex global function we make it static to the file.
  • WaffleshopParser.h
    • This is a convenience class that bundles the parser and the scanner together; it is good Object-Oriented design to do this, and is recommended in the Bison documentation.
  • WaffleshopScanner.h
    • FlexLexer.h is provided by Flex, and defines the base class that generated Flex scanner classes inherit from. The preprocessor directive surrounding it is necessary because FlexLexer.h is a mess (it says so right in the Flex generated code on line 16).
    • The yylex function has to be overloaded because Bison passes a pointer to the yylval variable. It would be nice if we could just specify this in YY_DECL, but the base Flex class has yylval with no arguments defined as pure virtual, forcing us to implement it anyway; I just went ahead and used it.

I used the following resources to construct this example:

Please post any suggestions or questions – I am no expert on either Flex or Bison and am always interested in improving my code!

Update: After you have gleaned all you can from this example, check out this followup about creating an INI file parser using Flex and Bison in C++.

If you have not read Yahoo!’s Best Practices for Speeding Up Your Website, you should definitely go through that now. It cover’s a lot of information that every web developer / web server admin should know. Once you have read through that document, you can use the Firefox YSlow extension to examine your website and let you know how well it follows Yahoo!’s rules.

One issue that I ran into while implementing these rules was an efficient and effective way to add far-future expiration headers to my static content. My first attempt had entries in the web server configuration file for every directory that I wanted these headers on. While this did work, any change I wanted to make required editing the server configuration and restarting the server daemon. Also, not everybody has access to the server configuration files.

A popular option is to use .htaccess files in folders you want to add headers for. This wasn’t an option for me since I’m using Lighttpd and not Apache. Also, if your goal is performance then you should not be using them anyway.

The solution that I came up with was to create a subdomain (static.thewaffleshop.net). This subdomain has the same document root as the regular domain. The only thing it does differently is add far-future expiration tags to any content accessed through the subdomain. Using lighttpd conditionals, this was extremely easy to configure:

$HTTP["host"] =~ "^static\." {
        expire.url = ( "" => "access 1 years" )
}
$HTTP["host"] =~ "^(www\.|static\.)?thewaffleshop\.net$" { ... }
$HTTP["host"] =~ "^(static\.)?thewaffleshop\.net$" { ... }

There is one conditional to see if the host starts with static., and the other vhost configurations only need slightly modified to pass through the static subdomain. I’m not sure how you would do this in Apache, perhaps using ServerAlias? Apache guru’s please leave a comment if you know how to implement this.

This solution is incredibly flexible – adding and removing expiration headers is as simple as changing the URL for the files. Just make sure that you never link to content via the static subdomain – different URL’s to the same content is a big SEO (Search Engine Optimization) no-no.