Saturday, June 20, 2009

Vim-style Line Numbers in Emacs

Being a heavy Vim user throughout my university degree and now throughout work, I get very used to having the line numbers down the side (with "set numbers" in a .vimrc) as I hack code. To get something like this in Emacs, there is linum-mode.

You can toggle linum-mode for any Emacs buffer with the command M-x linum-mode. Or if you want to set it globally for every file you edit, add this to your .emacs file:
(require 'linum)
(global-linum-mode 1)
linum-mode should come pre-installed with Emacs 22 and beyond. It is also bundled with Aquamacs 1.7 (and maybe earlier...)

Sunday, June 14, 2009

Getting a Perl REPL in Emacs

Although Perl doesn't really have a REPL, you can use Perl's debug mode to obtain something as close to a REPL as you can get without installing extra software with the command perl -de1

So when in Emacs (or XEmacs or Aquamacs or whatever), using the following Emacs commands in sequence will open Perl's debug mode inside a horizontally split window:
C-x 2
C-x o
M-x shell
perl -de1
And away you go.

Thursday, June 11, 2009

When to use defparameter and defvar

Because I always forget:
< gigamonkey> Say you were running a genetic algorithm and you
              said (defparameter *populations* nil)
< gigamonkey> Then you run it for a few days and *populations*
              is full of thousands of populations you've
              generated. Then you change some code in the file
              and reload it. Ooops. You've just wiped out your
              *populations*.
< gigamonkey> (defvar *populations* nil) saves you from that.
< gigamonkey> Conversely if you have
              (defvar *mutation-rate* .01) and then you decide
              to tweak things and change the .01 to .001 and
              reload the file, you'll be confused when nothing
              changes.
< gigamonkey> In that case you should have used defparamater.
< gigamonkey> spelled right, of course.

Saturday, June 6, 2009

Tweeting from the Command Line with Perl and Net::Twitter

The Net::Twitter module implements the entire Twitter API. This gives us the ability to create Perl scripts, small and large, to interact with Twitter.

Although I use Twitter a bit, I'm not that heavy a user that I need a massive application to tweet from. Instead I usually just use the web interface. But sometimes even that's too much if the tweet is short. Here's some code to tweet from the command line:

#!/usr/bin/env perl

use warnings;
use strict;
use Config::ApacheFormat;
use Net::Twitter;
use Term::ANSIColor;

use constant TWEETRC => "$ENV{HOME}/.tweetrc";

die "usage: tweet <message>\n" unless @ARGV == 1;

die "tweet: Unable to locate ~/.tweetrc file\n"
    unless -e TWEETRC;

my $tweet = shift;

if ( length $tweet > 140 ) {
    print "error: tweet is longer than 140 characters\n";
    print color 'green';
    print substr( $tweet, 0, 140 );
    print color 'bold red';
    print substr( $tweet, 140 );
    print color 'reset';
    print "\n";
    exit 1;
}

my $config = new Config::ApacheFormat;
$config->read(TWEETRC);
$config->autoload_support(1);

my $username = $config->username;
my $password = $config->password;

my $twitter = Net::Twitter->new(
    {   username => $username,
        password => $password
    }
);

my $result = $twitter->update( { status => $tweet } );

unless ( defined $result ) {
    printf "error updating status: %s\n",
        $twitter->get_error->{error};
    exit 1;
}
To use it, create a .tweetrc file in your home directory. Add something like this:
username foo
password bar

Replace 'foo' and 'bar' with your Twitter username and password, respectively. Make sure you set permissions correctly so other users can't read your .tweetrc.

And that's it. I place the script in my ~/bin directory, which is in my $PATH, under the name tweet with executable privileges and then run it like this:

tweet "I am a BANANA!"
Originally I didn't want to have to have to put quotes around the message, but shell meta-characters (like single quotes as an apostrophe) were getting in the way, so this was the safest alternative short of reading from standard input.

UPDATE: The code in this post has a problem which I have posted about here.

Wednesday, June 3, 2009

Checking Perl Syntax Within Vim

Sick of switching back and forth between your shell and Vim buffer fixing syntax errors and adding forgotten "use" statements? Running perl -c is a handy command to run on your Perl source files to check for syntax errors. Here's a little key mapping to add to your .vimrc file:
map ,pc <Esc>:! perl -c %<CR>
Now type ,pc (mnemonic: perl -c) in command mode to check the syntax of your Perl source file without having to switch windows. It is a little annoying to have to do it multiple times (if there are many errors) since Vim swaps between the file you're editing and the shell to show the output, but it's definitely a step above manually swapping windows :)

Yet Another Blog *sigh*

Ok. I've had many blogs before. On those blogs I've promised to blog regularly and failed miserably. This time I make no such promise to anyone (not even myself), that way I won't feel bad when I inevitably run out of things to write about. But what am I going to write about? Well, I named the blog "Luke's Thought Dump" for that exact reason: a place to dump thoughts. Any little tricks I pick up (for Vim, Emacs, Perl, Python, Lisp, Linux, OS X or whatever), that I want to remember for some time in the future, will end up here, and I've already got a handful in mind that I've picked up since starting my new job back in February. So that's it. I'll make a small post this weekend when I have a bit of time :)