Monday, 7 December 2009

Aligning C function parameters with vim

updated: now saves/retores the paste register

It has bothered me for a while, some coding styles, most notably in the GNOME world try to enforce good looking alignment of functions parameters such as:
static UniqueResponse
on_unique_message_received (UniqueApp         *unique_app,
gint               command,
UniqueMessageData *message_data,
guint              time_,
gpointer           user_data)
{
}

Until now, I aligned the arguments by hand, but that time is over! Please welcome my first substantial vim plugin: it defines a GNOMEAlignArguments command to help you in that task. All you have to do is to add this file in your ~/.vim/plugin directory and define a macro in your ~/.vimrc to invoke it just like this:
" Align arguments
nmap ,a :GNOMEAlignArguments<CR>

HTH.

9 comments:

  1. [...] This post was mentioned on Twitter by Vim for my grandma, Vim for my grandma. Vim for my grandma said: handy macro for aligning function parameters: http://damien.lespiau.name/blog/2009/12/07/aligning-c-function-parameters-with-vim/ [...]

    ReplyDelete
  2. Thanks for the cool plugin, it works like a charm.

    ReplyDelete
  3. @Jannis
    You should re-download it again, now it saves/restore the paste register, newbie mistake.

    ReplyDelete
  4. Can you make one for gedit too? :P

    ReplyDelete
  5. Hi,

    Is there a way to make it aligne parameters in function calling?

    eg:
    output_stream = gdata_upload_stream_new (GDATA_SERVICE (service),
    SOUP_METHOD_POST,
    upload_uri,
    NULL,
    title)
    l
    output_stream = gdata_upload_stream_new (GDATA_SERVICE (service),
    SOUP_METHOD_POST,
    upload_uri,
    NULL,
    title)

    It would be awesame :D

    Anyway thanks lot for this cool plugin

    ReplyDelete
  6. OOOOps sorry my last post wasen't that explainfull,

    Here is the explainfull code sample: http://pastebin.com/f4516bb35

    Thx

    ReplyDelete
  7. @thilahute
    yup, that's the (0 option of cinoptions. (and then = to indent, of course)

    ReplyDelete
  8. @damien, oops was so simple ;)

    ReplyDelete
  9. Haha, shouldn't you be charging for that kind of kowlndege?!

    ReplyDelete