Archive for the ‘code’ Category

Howto remove ^M characters using vim

Monday, July 5th, 2010

Thanks to this post on Tech recipes I can easily replace DOS/Windows line endings in files:

To remove the ^M characters at the end of all lines in vi, use:

:%s/^V^M//g

The ^v is a CONTROL-V character and ^m is a CONTROL-M. When you type this, it will look like this:

:%s/^M//g

In UNIX, you can escape a control character by preceeding it with a CONTROL-V. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).

Rushkoff on programming literacy

Wednesday, March 31st, 2010

Just saw this video by Douglas Rushkoff on what I would call ‘programming literacy’ and it made me curious about his new book Life Inc. I really like this quote from the last part of this video:

“If we don’t create a society, that at least knows there is a thing called programming, then we will end being not the programmers, but the users and worse: the used”

(more…)

The future is here: RepRap – a 3D replication printer for personal use -

Thursday, March 25th, 2010

“If RepRap is succesfull a number of changes may well happen in society. The principal one of them will be that we have the distribution of the
manufacture of goods. At the moment economics scale it means that it is sensible for goods to be manufactured in factories and then to be shipped to the individual people who wish to have these goods using a complicated transport system. If RepRap takes off and increases its abilities by evolution to manufacture more and more products, then people having these machines in their homes will be no longer a need, or no longer such a big need for factories to make the goods they want. When they want something it will simply be a question of downloading it from the web, in the way they currently do with music, a film or anything else. That downloaded file would then allow them to manufacture whatever object is was they wanted in their own home.[...]”

Check the embedded movie below by RepRap.org from which I took this quote (starts around 5:14) by Adrian Bowyer from. The RepRap raises a lot of interesting questions, such as is personal fabrication another nail in the coffin of intellectual property? Looking forward to this publication: “The Intellectual Property Implications Of Low-Cost 3D Printing by Simon Bradshaw, Adrian Bowyer and Patrick Haufe.” which as far as I can see will be available online here, the 15th of April.

(more…)

First commit of a theme for all Creative Commons chapters around the globe?

Wednesday, November 11th, 2009

Today, I finally added my first commit of the Creative Commons Netherlands(CC NL) wordpress theme (see also ‘New Creative Commons Netherlands site launched’) to the Creative Commons.org git repository. This means that other local CC chapters only need to install Wordpress and install this theme to have their own Creative Commons website. For example, Creative Commons France could now easily update their website and have a look and feel similar to Creative Commons Netherlands (and thus Creative Commons.org), without having to develop a theme on their own. By using this template, local Creative Commons organizations show more cohesion between the different CC chapters and allow them to be identifiable as part of the global Creative Commons movement.

I’m very interested in the response on this theme, although it’s still kinda rough around the edges. I still need to find some time to replace the Dutch names for English, write something down on how to use the themeĀ  and there’s probably tons of stuff that might need some tweaking, but at least it’s in the repository.

If you’re interested in using it yet something is keeping you from doing this feel free to contact me and I’ll see what I can do to help you.

Last but certainly not least, I would like to thank:

- Paul Keller (Creative Commons Netherlands) for recognizing the potential use of this theme for other local CC chapters around the world.
- Mike Linksvayer and Nathan Yergler from Creative Commons.org for their enthusiasm, help and infrastructure.

Bram Cohen on Object Oriented Programming

Sunday, September 27th, 2009

“Taken less literally, it’s just gibberish, a completely nonsensical way of thinking about the problem, like teaching a drawing class where you cover pentagrams.”

Bram Cohen (inventor of the p2p Bittorrent protocol) has an interesting view on subclassing in object oriented programming.

Wordpress simple redirect

Tuesday, September 1st, 2009

Recently I needed an easy way for a client to create a link in a Wordpress powered website that would automagically redirect a visitor to a certain location upon visiting. Part of the requirement was that this link could be easily remembered and used in printed material such as leaflets and magazines. As an extra option it should be easy to change the location towards the redirection would point. I couldn’t find anything like this so I created a very simple little page template.

How it works:
Copy the template page Page Redirect code below to a file and save it as page-redirect.php in your own template’s directory. Create a Page in Wordpress and set the Page’s template to Page Redirect. Add a custom field to this page with the name redirect_url and the full url of the url you want to redirect to.
For instance: http://www.redirected-location.com. Now save the page and you should now test it by visiting the page you created.

For example (I assume you are using nice urls):

http://www.your-website.com/the-page-title-of-the-page-with-the-redirect-template

If all went well you should be instantaneously be redirected (using a standard php redirect with a 302 HTTP redirect) ) to the url you entered in the custom field. You can easily change the redirect location by changing the value of the custom field.

Code:


< ?php
/*
 * Template Name: Page Redirect
 *
 * Makes it easy to redirect a page to another url, using the parameter redirect_url
 */
$redirect_url = get_post_meta($post->ID, "redirect_url", true); 

// Defaults if no options we're given
if( ! empty($redirect_url) ) {  header("Location: $redirect_url"); } else { echo ""; }
/* Make sure that code below does not get executed when we redirect. */

exit;
?>

As I said it uses the standard PHP redirect, but you could easily change or extend it with more specific http headers if needs arise. Feel free to use the above snippet as you see fit, as far as I’m concerned it’s in the public domain. I hope this might be useful to you. If you have any questions, feel free to leave a comment.

Mapping firefox keyboard shortcut with vimperator to select a tab

Friday, May 1st, 2009

I’ve started to use the Firefox add-on Vimperator. It enables Firefox to behave more like Vim. I installed this add-on, not because I know Vim so well, but because I really like the ability to use my browser with a keyboard.

One of the things I really missed after installing Vimperator was the ability to select an open tab directly by a keyboard shortcut. I’ve used the following mapping (a well known method to vim users) to fix this and now I can select an open tab directly.

:map <C-1> :tabn1<CR>

In the snippet above I created the shortcut CONTROL-1 which selects tab number 1 following with ENTER. I would like to use a variable in the mapping but I have not been able to find out how to achieve this, so for now just add more mappings with following numbers, e.g. replace 1 for 2 etc. If you know how to add variables in vimperator let me know!

If you don’t see the numbers in your tabs or don’t see tabs at all you’ll need to enable the gui, you can use the following snippet:

:set go+=mTBn<CR>

This shows the following gui parts:

  • m Menubar
  • T Toolbar
  • B Bookmark bar
  • n Tab number

Now you should see the gui including the numbers in the opened tabs.

NB: Don’t forget to save the settings with the following:

:mkv<CR>

if you’re already have a saved configuration then you need to add a exclamation mark to the above.

:mkv!<CR>

Quick howto using Virtualbox with webdevelopment

Friday, March 27th, 2009

I use Virtualbox (version 1.5.6_OSE) on Ubuntu Hardy with Windows XP as one of the (guest) virtual machines. In order to access my webserver installed on the host (real physical system (Ubuntu Hardy) I need to use the ip-address 10.0.2.2 (I use NAT in Virtualbox’s network settings) from Windows XP (or any other guest operating system) and edit the hosts (c:\windows\system32\drivers\etc\hosts) file with the Virtual Host names I use on the host. This allows me to access the Virtual Host based websites to be accessed from the Windows XP operating system.

An example hosts file on Windows XP:

# make sure there's at least one space between the ip-address and the virtual host name
127.0.0.1 localhost
10.0.2.2 SOME_VIRTUAL_HOST_NAME

Replace SOME_VIRTUAL_HOST_NAME with a real Virtual Host name. In my case I use the extension .dev to indicate a local project in development, so it could be something like burobjorn.dev

Internet Explorer and “Operation Aborted”

Friday, March 20th, 2009

I was working on a website for a client when we discovered that sometimes the website was not accessible for Internet Explorer. Notice the word sometimes.

A word dreaded by every programmer including myself. In order to solve problems there needs to be a pattern. Something like: “press the red button and the screen goes blank”. Without the ability to reproduce a problem it is really hard to solve the problem or confirm that the actions you took to solve it were indeed the (right) solution and not some fluke.

In this case a user visited the website and got access to it, or the user visited the site and got an arcane error message:

“Internet Explorer cannot open the Internet site http://<Web site>.com. Operation aborted.”

I never saw this message before and after some testing we could confirm it only happened with Microsoft Internet Explorer. There were no problems with any other browser, but somehow Internet Explorer sometimes spat out this utterly useless error message and prevented the user to visit the client’s website.Together with some engineers from the hosting company I sat out to find and solve this issue. So we started to have a look at the web server’sĀ  log files.

At first the engineer from the hosting company suspected it might have something to do with some HTTP 500 error codes, missing images (so you would expect HTTP 404 errors) and some rewrite rules. According to his first thesis this combination might have caused a redirection loop and Internet Explorer could not cope with it. After quickly fixing the missing images (a typo in a path) we could dismiss this thesis and we continued our quest. Luckily a colleague of one of the engineers remembered a similar issue creeping up with a different client of them and pointed us in the right direction. A quick search (note to self don’t try to be smart, first search for the error message on the Net, then analyze logfiles) later gave use quite a lot of links pointing towards Microsoft.

Apparently Microsoft’s engineers think that its a good idea to confront a user of their browser with an arcane error message and prevent the user from accessing the website when the visited website in question does something less smart (maybe even stupid). While any other browser would at least try to let the user access the website and cope with the issue. I wonder where the Interaction Design people were during this decision.

No, I’m not making this up. Microsoft states in the knowledgebase article on this issue: “This behavior is by design.” What are the arguments for doing it this way? Interestingly in the same article they claim:

How do I fix this problem?
The easiest way for you to fix the problem is to upgrade to Internet Explorer 8. This problem no longer occurs in Internet Explorer 8.

So the behavior is by design, yet they advice you to solve it by upgrading your browser. Interesting, to say the least.

Anyway, after reading the article and doing some more debugging (which is a lot more painful then it ever should be using Internet Explorer) I found the culprit to be a piece of JavaScript (swfobject.js in this case) trying to access an element before the parser was ready. A bug and certainly something we needed to fix, but Microsoft did you really have to abort loading the webpage and preven the user from accessing it? Why can all the other browsers cope with this and still allow the user to visit the website? Would it have hurt to show a less obtrusive error message in the status bar?

For those having similar issues please see these urls for more info:
http://support.microsoft.com/kb/927917/
http://blogs.msdn.com/ie/archive/2008/04/23/what-happened-to-operation-aborted.aspx

Commandline bookmarker

Wednesday, March 4th, 2009

When you use the commandline a lot like I do, you sometimes wonder about the amount of typing involved. Even if you use tabs like a maniac, you’ll probably end up typing certain paths a gazillions times a day. Not anymore…

I stumbled upon a great little time saver called bm (bookmark). It allows you to bookmark (as if you’re in a web browser) certain paths and create a cdbm command which allows you to quickly change directory to the bookmarked path.

1) To create a bookmark type:

bm -a /path/you/want/to/bookmark name_of_your_bookmark

2) To use a bookmark type:

cdbm name_of_your_bookmark

I highly recommend this nifty little timesaver. You only have to compile it (it’s really easy, just follow the two instructions in the README.txt) and I can confirm it works without a hitch on Ubuntu 8.04, but my guess is that it would probably could work on other POSIX systems as well (like OS X).

Thanks to Bart from Onderstekop.nl for creating it!

UPDATE
Download the bm_1.0-1_i386 package I created for Ubuntu. Tested on Ubuntu (9.04) Jaunty on 32-bit architecture and it works for me. Your mileage may vary!