vim tricks for constructing header files

Discussion in 'C Programming' started by luser- -droog, May 16, 2011.

  1. I've just cobbled together a few tricks that save a bunch of
    keystrokes when generating the .h file from a freshly written
    ..c file.

    A freshly written .c file:
    1
    2 #include "m.h"
    3 #include "ob.h"
    4
    5 object consarr(mfile *mem, unsigned sz) {
    6 unsigned ent = mtalloc(mem, sz * sizeof(object));
    7 return (object){ .tag = arraytype, .sz = sz, .ent = ent, .off
    = 0};
    8 }
    9
    10 void arrput(mfile *mem, object a, integer i, object o) {
    11 put(mem, a.comp_.ent, a.comp_.off + i, sizeof(object), &o);
    12 }
    13
    14 object arrget(mfile *mem, object a, integer i) {
    15 object o;
    16 get(mem, a.comp_.ent, a.comp_.off + i, sizeof(object), &o);
    17 return o;
    18 }
    19
    "ar.c" 19L, 472C written

    then switch to new file
    :e ar.h
    load previous file
    :r #
    trim headers
    d}
    replace function body with semicolon
    j$c%;ESC
    reposition, repeat change
    jj$.
    again
    jj.
    et voila:

    1
    2 object consarr(mfile *mem, unsigned sz);
    3
    4 void arrput(mfile *mem, object a, integer i, object o);
    5
    6 object arrget(mfile *mem, object a, integer i);
    7
    "ar.h" 7L, 149C written
     
    luser- -droog, May 16, 2011
    #1
    1. Advertisements

  2. luser- -droog

    Rui Maciel Guest

    You can go a step further and record a macro with that list of commands or
    map a specific keyboard shortcut to that script.


    Rui Maciel
     
    Rui Maciel, May 17, 2011
    #2
    1. Advertisements

  3. luser- -droog

    Old Wolf Guest

    Have to say that I always write the .h file first.

    Define and document the interface between units first,
    and implement it later -- good design IMHO.

    Then all you have to do is copy the .h file to the .c file
    and change semicolons to curly braces.
     
    Old Wolf, May 18, 2011
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.