2013-07-25 :: Aligning parameters of a C function declaration, with lineup-parameters
Renaming a GObject class can involve a lot of manual work. Once the type is renamed, the parameters of the function declarations are no longer aligned. In the following example, GtkSourceSearch has been renamed to GtkSourceSearchContext:
guint
_gtk_source_search_replace_all (GtkSourceSearchContext *search,
const gchar *replace,
gint replace_length)
{
/* ... */
}
It is a GNOME convention to align the parameters, so the code must be fixed:
guint
_gtk_source_search_replace_all (GtkSourceSearchContext *search,
const gchar *replace,
gint replace_length)
{
/* ... */
}
As programmers, we don't like doing such a manual work, and even less when we
are renaming a big class. Unfortunately, the
Indent
program doesn't have an option for that. If you use Vim, there is the
Align plugin,
that have the \afnc alignment mapping. But it doesn't align the
parameters on the parenthesis.
So I wrote a script! The result is in my gnome-c-utils repository (containing only the lineup-parameters script for the moment).
To detect the parameters, a few regular expressions are used. There are quite a lot of restrictions, but it should work for most GNOME software. To have a perfect script, libclang could be used, but I've never used it, so regular expressions was a quicker solution.
The script can be used to convert an entire file, or can be used from a text editor (stdin is read, and the output is written to stdout). I've documented how to add a key binding in Vim. So by pressing a single key, the parameters are correctly aligned!
This is just a small step to improve the developer experience in GNOME. But I hope this will be useful for other developers. I dream of a GNOME C IDE that handles the boilerplate (for renaming classes too), that aligns the parameters and the function prototypes in the headers, with auto-completion for signals and properties names, and so on.
Edit: update the links (in 2026).