Saturday, January 26, 2013

Update to android-ndk-profiler



Android Robot
Image from Wikipedia
I've just updated my profiling library for native code on Android to version 3.2. This version is the first "final" release since I moved the code from the subversion repository on Google Code over to github. I have kept the main project hosting on Googlecode still as Github recently removed the ability to host pre-compiled downloads. Not sure what to do with issues yet, but pull requests are welcome :-)

There are no huge changes, but I have made a switch from the old ad-hoc include mechanism to use the NDK module system. This should make it a lot easier to set up the profiling code in an Android.mk file. I've updated the usage page with details of how to do this. For some reason the HTML documentation for the NDK is not online anywhere, the best you can do to view the module documentation without downloading the NDK itself is probably this page.

The switch over to github meant I could also start using Travis CI, which is a great continuous integration service for public repositories. I haven't read much about this anywhere, but it seems like a well thought out service, and it was really simple to set up.



Thursday, January 24, 2013

Command line and Gnome Desktop integration tips for Vim

Here are a couple of ideas that I find handy when working with Vim.The first tip is how to check what the last search term was in Vim from the command line. A silly idea that I've found more useful than I thought it would be.

For this to work, you need set nocompatible so that Vim writes out its state information to a viminfo file on exit. The line in the viminfo file that stores the last search looks like this:

 ~Msle0~/search_term

The m, s, l and e characters describe which magic, smartcase, line/char offset, or from the end or start flags Vim had set when you searched. For example, the third character is an 's' or an 'S' depending on whether smart case is on (S) or off (s).

Here is a script, which I call last_vim_search, that shows you what the last search term stored in the viminfo file was:

#!/bin/bash
lvs=$(awk -F/ '/^~[Mm][Ss][Ll][eE]0~\// { print $2; }' ~/.viminfo)
if test $# -eq 0 ; then
    echo $lvs
    exit
fi
grep "$lvs" "$@"

The snippet of awk searches for the matching pattern, splits on the "/" character, and spits out the last used pattern.
$ last_vim_search
\<pattern\>

As an added bonus, if I pass arguments to the script then it searches whatever arguments I passed in using grep.
$ last_vim_search *c
file.c:123: ....
file.c:456: ....

The second tip is how to integrate Vim with the "Recent Documents" feature from the Gnome Desktop. Frequently I find myself editing a text file in Vim, and then having to attach it to an email or upload it to a web form. Having the file you want to upload in the recent documents list helps a lot in these situations, as it is quicker to get to than having to navigate the file chooser to the correct location and selecting the file there. Unfortunately only "Gnome-ish" applications update the recent documents list, and terminal Vim is not one of these. This tip fixes that situation, and lets Vim update the recent list too.

For this to work, you will need the python-gtk bindings. Debian and Ubuntu at least installs python-gtk by default with Gnome. Also your version of Vim needs to have Python support compiled in. If not, you could adapt the python snippet to run in its own script.

"
" add text files that I edit to Gnome's recent file list
"

functions:RecentAddFile()
    python <<EOF
import vim
import gtk
n = vim.current.buffer.name
f = 'file://' + n
gtk.recent_manager_get_default().add_full(f,
    {'mime_type''text/plain''app_name''vim''app_exec':'vim'})
EOF
endfunc

autocmd BufReadPost *.txt call <SID>RecentAddFile()

This makes use of the "recent documents" GTK API. It's only short and doesn't really need much explanation. The python <<EOF marks the start of the embedded snippet of python code, the rest is all vimscript. The autocmd BufReadPost bit means that when Vim reads in a txt file it will call the function to add the file to the recent list.



The python code uses Vim's own API to get the current file name, which it turns into a URL by prepending "file://". The gtk call adds the URL to the recent document list. There's a simpler "add_item" method too that doesn't need the options passed in via the dictionary argument, but it doesn't seem to work in this context.

You could tweak the code to only add files in your home directory - as is it will add help pages as recent docs too. With different autocmd lines you could also add other file types to the recent history.

And now for something completely different... check out this great Super Meat Boy plushy that my better half made for eldest son. That's love :-)



PS. This weekend I'm hoping to have time to put the final touches on a new android-ndk-profiler release. The current beta-ish version seems to be working OK if you want to try it out.