Archive Page 2

What did I do the past 3 months?

Well, I’ve been doing an internship at a known internet company and it has been an awesome experience. I’ve learned quite a lot, I had an amazing host and a very supportive team. Now I’m on my way back home, and there’s nothing like a couple of hours in an airport to raise the blogging enthusiasm.

I worked with java, that language that I didn’t like too much because, as on of my friends at work said, there is too much plumbing needed to do anything. But, now I feel more respectful to the java camp, and definitely I’m going to keep writing some code in java, after all, if you consider that skills are based on real production code written, java would be my “main” language. (Although is hard to admit)

I did practice some elisp, and I realized tahat even with cool IDE’s with pretty nice features, nothing beats emacs at manipulating text files. Changing editors is a very hard topic, you have to deal with muscle memory and such, and then you found thos little things you take for granted as… how to move.

Well now is time to go back, to my girlfriend and my family!

Do you want to show off emacs?

Take a look at this screencast. Just amazing, I found it while reading the emacs wiki page of cedet.

Elisp func to create a dummy admin.py

While writing a django app, I faced the tedious task of updating the corresponding admin.py file for the models I was writing. Because I didn’t want to customize any of the admin options just yet all I had to do is insert new register entries on the file (one for model). I grew tired of this pretty soon so I wrote this elisp function to update an admin.py file easily. Hope this helps somebody

(defun rl/django-admin-all-models()
(interactive)
(let ((content " "))
  (with-temp-buffer
    (insert "from models import *\n")
    (insert "from django.contrib import admin\n\n")
    (let ((text-start (point)))
  (insert-file-contents "models.py")
  (keep-lines "^class.*$" text-start (point-max))
  (while (re-search-forward "^class \\(\\w+\\).*" nil t)
    (replace-match "admin.site.register(\\1)"))
  (write-file "admin.py" nil)))))

A word of warning: this will override your current admin.py

python and emacs: the rope way

If you follow Planet Emacsen you have probably read a couple of great posts about emacs and python.

Ryan McGuire’s EnigmaCurry blog has a post about using Emacs as a powerful Python IDE, and later about using Autocomplete.el: code completion in emacs. The heart of both posts is rope and ropemacs. Rope is a great!, go to rope’s website for a full feature listing (e.g. autocompletion, refactoring, pydoc, etc.)

A simple post about what you need to install and how you have this post from Edward O’Connor.

Also, if you use yasnippet for templates you may also find interesting Ian Eure’s post about Disabling Python-mode’s skeletons would be also very useful.

Querying for an ethernet address in the arp cache

Just a quick note for myself, and for anybody looking to do the
same. If you have an IP address and you want the corresponding MAC,
using the ARP cache, here is the function:

#include <stdio.h>
#include <sys/types.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>

void print_eth_addr(const char* ip)
{
     int s;
     struct sockaddr_in *sin;
     struct arpreq a;
     struct in_addr addr;

     s = socket(AF_INET, SOCK_DGRAM,0);

     memset(&a, 0, sizeof(a));
     strcpy(a.arp_dev, "wlan0");
     sin = (struct sockaddr_in*)&(a.arp_pa);
     sin->sin_family = AF_INET;
     a.arp_flags = ATF_PUBL;

     inet_aton(ip, &addr);

     memcpy(&sin->sin_addr, &addr, sizeof(struct in_addr));

     if (ioctl(s,SIOCGARP,&a)) {
          perror("ioctl");
     }
     unsigned char* hw_addr = (unsigned char *) a.arp_ha.sa_data;
     printf("HWAddr found : %x:%x:%x:%x:%x:%x\n", hw_addr[0],
        hw_addr[1], hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]);

}

Just change “wlan0″ for “eth0″ or your favorite network interface.

Why did I stop using tabs on emacs?

A long (long!) time ago I used to be trapped on the Windows world,
surfing the Web using Microsoft Internet Explorer… and I hated
it. It was an absolute mess having more than 5 windows open, so I
didn’t open more than 3. And then I found Firefox with it’s tabs, I
just loved it.

Fast forward to the present, on almost anything I do using a GUI I
want to be able to use tabs, even on Emacs. I was a huge fan of
elscreen,
it’s a nice package that creates and manages tabs on emacs, nicely
integrated with emacs-w3m (although
emacs-w3m have their own tabs), dired and a few more. I wrote some
elisp code to integrate it with
erc and
jabber.el. So what happen?

ido-mode

Tabs are a great way of organizing and making accessible content, don’t
get me wrong, but on emacs you have the power of the minibuffer, and matching it with
ido-mode feel more emacsy for me than tabs. It does fuzzy
auto-completion on the minibuffer when opening a file or switching
buffers. And when you begin to feeling comfortable using it, tabs (at
least for me) become almost useless.

If what I said earlier doesn’t convince you, I recommend you to watch
Stuart Halloway’s great What You
Can Learn From ido.el
. It greatly
motivated me to take a deep look at ido-mode.

Yasnippet for C style comments

Are you using yasnippet right?
No? Well, in a nutshell is an awesome template system for Emacs, I
really recommend it. The template language is very easy to learn and
there are templates for a lot of programming languages.

Yasnippet stores your snippets in the filesystem, a single snippet per
file and inside a directory structure that mirrors the major mode
hierarchy. For example, the snippet for the using namespace std C++
idiom is stored in a file named using, and it’s located in the
directory snippets/text-mode/cc-mode/c++-mode. The file name (in
this example using) is used as the key for the expansion. You know,
convention over configuration .

Because yasnippets uses filenames as keys, you face filesystem’s
naming restrictions, and this is a problem if you want to define a
snippet for C comments, you can’t name a file /* for obvious
reasons. What do you do then? Well, yasnippet allows you to define
your snippets in elisp code directly, so if you want to create your C
sytle comments template, you can do something like this

  ;; $0 defines where the cursor will be left after the expansion
  (yas/define 'cc-mode "/*" "/* $0 */")
  (yas/define 'cc-mode "/**" "/**\n * $0\n **/")

The first argument is the major mode where this snippet will be available, the second is the key you will use to expand the template, and the last argument is the template itself.

For more about writting snippets for this system, go here

Emacs ps (v.23)

Another post about a new feature in emacs 23. This new feature is proced, and from the NEWS entry:

** proced.el provides a Dired-like interface for operating on processes.
Proced makes an Emacs buffer containing a listing of the current
processes (using ps(1)).  You can use the normal Emacs commands to
move around in this buffer, and special Proced commands to operate on
the processes listed.

I found this great!, another reason to stay inside your emacs :)

Here is a screenshot from my machine:

screenshot

Documenting a Django project with Sphinx

Sphinx is a documentation tool written for python (but could be used
on other circumstances). I’ve learning how to used and I wanted to
document a django project I’ve been working on, but when you try to
import your modules you get an exception

 autodoc can't import/find module '<yourmodule>', it reported error:
 "Settings cannot be imported, because environment variable
 DJANGO_SETTINGS_MODULE is undefined.",please check your spelling
 and sys.path

This happens because django do some enviroment settings before using
your app, so I wanted to do the same with. It was easier than I
tought, just put the following snippet in your sphinx conf.py

from MYPROJECT import settings
from django.core.management import setup_environ

setup_environ(settings)

of course, for it to work you need to modify your path, in my case was:

sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../../'))

Awesome wm: my first steps

A tiling WM maximizes the use of your screen real state by handling
the space as a grid where each window (or frame) gets a piece of it in
a non-overlapping way. An additional, but not exclusive, feature found
on most of this wm is that you can do almost everything using keyboard
only.

I have a widescreen monitor and with the extra horizontal space I can
arrange two windows side-to-side without losing too much info, and if
you have some of those massive 20+ inches lcd monitors, you have a lot
of space. I still consider a 17 inches monitor to be big… I’ve
been using 14′ monitor for almost my entire life :)

They are a lot of tiling WM, check the Wikipedia article for a
list of the most common. I decided to try awesome 3 a couple of
months ago and I don’t regret a bit.

First step: don’t forget about that Windows(r) key

When you open awesome for the first time, all you get is a dark theme,
with a gray wallpaper. You will have a statusbar on the top with a
clock on the right, a weird icon next to it and some numbers on the
left (1..9).

First thing first, the magic key here is the “Windows Key” that
almost all the PC keyboards have, and the behavior is based on Vi, the text editor. From now on the “Windows key” will be just
modkey.

Open your apps

To create a new terminal window all you need to do is press
modkey+Enter. To open a different app you could create your own
shortcut (but we are not there yet) or press modkey+F1. That will
create a prompt on the statusbar where you can insert your command
(e.g. “firefox”).

Moving

Now, if you have several windows open you can circle them using
modkey+j and modkey+k. If you want to move between “tags” use
modkey+left or modkey+right (arrow keys).

Changing window sizes

Using the default layout, there will be a “main window” on the left
that will take half of the screen and all the other windows will be
confined to the right. With modkey+h and modkey+l you can change
how much space is dedicated for each of this “groups”.

What if you want a single window to take the whole screen temporarily?
Use modkey+m :)

That’s it!

I have some trouble getting starting with awesome (specially because
googling for awesome doesn’t help much), so I hope this would help
somebody else trying to use awesome for the first time.

Here is a screenshot of my desktop, click for a full size version.

Awesome wm screenshot