Archive for May, 2009

Conkeror: An Emacs Web-Browser

Sunday, May 24th, 2009

I tried out Conkeror, a new web browser, today. It is based on the emacs philosophy of extensibility, with Javascript as the extension language. While this is a great idea - the main issue I have with emacs is having to switch to a browser(Yes, I know about W3M, but I’ve tried it and found it too slow) whenever I need to access web sites. So I heard about Conkeror, and decided to try it out to see if I could possibly start migrating to using it for some of my editing.

Unfortunately, Conkeror is not ready for what I wanted to use it for. The first is that by default, the open-url does not have history. In Firefox, I use history to be able to tab-complete to sites I’ve been to before; not being able to do this is very annoying. It seems like a pretty basic thing; every other browser has history enabled, so there is no excuse for Conkeror not having this feature.

Another issue is a slight stall when opening hints. In Firefox, I use the Vimperator plugin, which has a similar hints system to Conkeror. If you aren’t familiar with hints, you press the ‘f’ key and every link on the page is given a number which you can type to open that link. In Vimperator, the link opens as soon as you uniquely identify the link. However, in Conkeror, there is a noticeable delay between identifying the ling and the browser starting to load the page. This seems illogical; I’ve already said what I want to do, why make me wait?

Another, smaller problem is that it doesn’t implement all of emacs’ keybindings. Several times, I have tried to split the browser with C-x 3, but since this isn’t implemented nothing happens(this also seems like one of the things that probably is not solvable via javascript customizations). While this isn’t a major problem - Conkeror is in beta, after all - It is jarring to run across situations like these.

The last issue I had with Conkeror is being unable to actually edit files. Without this, I can’t really use it to edit, can I? While you can obviously edit text fields, and use some emacs keybindings in them (and define more with javascript), you can’t open an empty buffer and start typing. I really, really want an editor/browser combination that isn’t terrible.

While Conkeror isn’t usable for me at this point, it is a very interesting project. I plan on hacking on it a bit; maybe I’ll get it to the point where I can do all my web browsing and some of my editing in it, switching back to emacs for heavier-duty coding. Until then, I’ll stick with Vimperator.

Google Adsense

Saturday, May 23rd, 2009

As you may have noticed, I added Adsense to my blog earlier today. The process for doing this is fairly simple: Essentially, you just add a new ‘Text’ widget on your sidebar and paste in the code Google gives you to display ads. If you need a more detailed guide, the instructions I followed are located here.

Webjump

Monday, May 18th, 2009

One emacs function I have been using more frequently recently is webjump. It allows you to easily perform web searches from within emacs - While I use firefox instead of an emacs-based browser, it still saves valuable time instead of first switching to the browser. You can customize the sites you can search by customizing the variable webjump-sites. However, while Webjump is useful for less-frequently searched sites, for ones that I perform searches for on a regular basis I have functions such as the following:

(defun google (query)
  "googles a query"
  (interactive "sQuery:")
  (browse-url (concat "http://www.google.com/search?q=" query)))

This allows me to bind keys to search specific sites, such as C-M-g for the above-defined google query.

JSR 305

Thursday, May 14th, 2009

JSR-305

This Google Tech Talk was about JSR-305, a JSR that was adopted and will be in Java 7 (I believe). This JSR involves added annotations for software defect detection - essentially, it aims to provide standard annotations that tools can understand and use to help detect errors in your code. Some of these have been implemented nonstandardly in tools such as IntelliJ, such as the @Nonnull annotation.

This talk went over what would be provided and examples of why this would be useful, instead of going into implementation which would be tool-dependent. He did mention one possibility for enforcing these annotations at runtime, but I think it would have been interesting to go more in-depth on this. Oh well, there’s only so much you can cover in an hour.

I’ve known about some of the annotations before as ones that were coming to Java7, eg @Nonnull, but not some of the others. While (hopefully) the ones built-in to Java7 will have compiler support, since the JSR includes a way of defining your own classes it would be nice if there was a way to be able to tell javac how to verify those, reducing the need for external tools. On second thought, this seems like informing the compiler how to check for violations of these annotations seem rather difficult; maybe it should be left to tools developers.

One thing mentioned in this presentation that I was unaware of was class- and package-level default annotations. When I heard about @Nonnull, I liked the idea, but it seemed like it would end up making Java even more verbose, since you’d usually want to add it to essentially every variable. Default annotations allow you to annotate a class or package and essentially have variables ‘inherit’ from this default; this allows you to make every variable in one class @Nonnull with only one annotation, much better than if each variable declaration needed an annotation.

Google Tech Talk: git

Tuesday, May 12th, 2009

Git

This Google Tech Talk, by Randall Schwartz, covers Git and how it works. I started using Git to host my emacs customizations on GitHub several months ago, but had no real idea of how to use it in depth. I could use it to commit changes and push to my repos, but did not know how to do any more advanced things, and almost deleted my work a few times while trying to revert commits.

This tech talk helped in my understanding of Git. Most of it was describing how Git worked instead of just what the commands do, which was very interesting and gave a better understanding of the system than just a rundown of the commands would have been. The downside of this is that by the time he got to how to do the commands, he was running out of time, so he ran through those pretty quickly. However, it was a great introduction to understanding Git and how to use it.

The staging area and index were explained quite well, which helps me understand a few times what I thought was committed actually wasn’t - I didn’t really understand why you had to add modified files before committing, and occasionally forgot to do this. Later, I was surprised when those files weren’t changed when I cloned my repos. Knowing how Git works will certainly help me use it correctly in the future.

Unload-Feature

Saturday, May 9th, 2009

When reading my RSS feed a few days ago, I ran across an interesting command: unload-feature. This will take any feature - eg, a function or symbol - and ‘unload’ the library that provided it. In effect, this undoes a require or load-library command. Before I knew about this feature, I had to restart emacs whenever I wanted to revert loading a library - now, i can just call this function.

Collaborative Editing

Thursday, May 7th, 2009

Eclipse Day at the Googleplex: Wiring Hacker Synapses

This Google Tech Talk goes over the algorithm Eclipse Ganymede uses for collaborative text editing. This feature allows two people to connect to the same file and make changes to it, with each person’s changes being reflected in the other person’s document. This is not difficult until changes start conflicting with each other: How do you preserve the meaning of edits when, due to latency issues, editing instructions may occur out-of-order? This presentation addresses that issue.

This presentation made me think of how to use Emacs for collaborative editing. A few thoughts and searches led me to several options:

The first option to use Emacs for collaborative editor is to use GNU Screen. Screen is a terminal multiplexor, allowing features such as multiple shells, multiple connections to a screen instance, and many other features. I’ve been meaning to get around to learning screen for a while; even though I mostly use run shells inside Emacs, solving many issues, Screen still seems like it would be useful to know. To perform collaborative editing in Emacs with Screen, just have one person start a screen instance, run ‘emacs -nw’, and have the other person connect to the same screen instance, probably via SSH. This will allow both users to view and edit the document at the same time.

Emacs can also spawn a new frame on any X display using the make-frame-on-display function. Just pass the function which display to use, and a new frame will pop up in that display. Since both frames will be running the same Emacs instance, each person connected will be able to see changes and edit in real time. Users can still affect each other, however; changes to the emacs session will be effected over all connected clients, and closing emacs will kill it for everyone.

There are certainly other ways to do collaborative editing within emacs: Just look at The EmacsWiki page on Collaborative Editing for more.

How to get the highest score in Word Challenge

Monday, May 4th, 2009

Some of you may know of the Facebook application ‘Word Challenge’. After one f my friends wrote an application that does most of the work of playing the game, I decided to one-up him and write a bot that plays the game entirely automatically. Writing this bot required three major parts: Determining which characters were on the screen, which words to send, and actually sending said words.

To determine which letters are displayed, I just used a few command-line programs, executing them using the c function ’system’. I use ’scrot’ to take a screenshot of the entire screen, ‘convert’ to cut out the images of the letters, and then ‘tesseract’ to OCR each letter individually. This is unfortunately not very portable; the Firefox window must be in the same place each time you run the bot, and if you are running the bot on a different computer you will have to change which parts of the screenshot you cut out with ‘convert’. I found some letters did not scan well (specifically p, m, i, r, and s), so a few hacks had to be done to ensure that words could be matched even if these letters were incorrectly scanned.

Once you have found which letters are available, you need to find which combinations of words to send. For this, you first need a list of valid words. I just downloaded the corncab dictionary and stripped out all words with more than six characters; you could also probably reverse-engineer the Flash client and get the dictionary from it. Once you have this dictionary, there are several ways of determining which words to be able to send. The first, and easiest, is to scan through the words in the dictionary one at a time and check if they can be created using the given letters. The more efficient way is to find all permutations of the letters and check if those were in the dictionary. As it turns out, either way works well enough for this application.

Once which words to send are determined, only the matter of actually sending them is left. To send keys to the application, I used XTestFakeKeyEvent. This function adds a key press to X’s normal event queue, to be processed as any key would be. You need to use it twice per key press, once to send a key down and once for key up. For example, to send a ‘A key press, you would need to do the following:

  XTestFakeKeyEvent( display, XKeysymToKeycode( display, XK_A ), True, 0 );
  XTestFakeKeyEvent( display, XKeysymToKeycode( display, XK_A ), False, 0 )

To send a word, you just need to send all the characters in it, followed by and ‘ENTER’ press. Once all the words have been transmitted, the bot sends a ‘CONTROL’ press in order to cycle to the next set of letters and waits for several seconds in order to allow the application to finish processing. The program then loops, generating an arbitrarily high score until you stop it.

There were a few other technical issues that had to be dealt with, such as what to do when the application did not finish processing all the key presses before a new cycle began, but this is the entire structure of the bot. All in all, the bot ended up being 191 lines of c.