5. Sections

Org structure is very hierarchical. Sections in an Org document have an implicit section number, determined by where they are in the document, and what their hierarchical level is.

Section system is a decimal point like system. The introductory section would be section 1. A sub section would be 1.1, a second subsection 1.2, a sub section of the sub section would be 1.2.1. etc.

While parsing the file, Sorg keeps track of the current level. Every time a section heading is discovered, the level of the header is checked. If the level matches, the counter assigned for the current level is incremented. If it is a greater depth, the next counter next in line is set to be "1". If it is a lesser depth, the counter for that position is incremented, and all depths following it are zeroed out.

Section number depth can be checked and updated with the function update_depth.

<<function_declarations>>=
static void update_section_depth(sorg_d *sorg, int depth);
<<functions>>=
static void update_section_depth(sorg_d *sorg, int depth)
{
    int i;
    if(depth < 0 && depth > SORG_MAXSEC) {
        fprintf(stderr,
            "Warning: section depth of %d exceeds maximum depth of %d\n",
            depth + 1, SORG_MAXSEC);
        return;
    }
    if(depth > sorg->depth) {
        sorg->secpos[depth] = 1;
    } else if(depth < sorg->depth) {
        sorg->secpos[depth]++;
        for(i = depth + 1; i < SORG_MAXSEC; i++) sorg->secpos[i] = 0;
    } else {
        sorg->secpos[depth]++;
    }
    sorg->pdepth = sorg->depth;
    sorg->depth = depth;
}

To print the current section number in the nice X.Y.Z format, use the function print_section and supply a filehandle to write to.

<<function_declarations>>=
static void print_section(sorg_d *sorg, FILE *out);

It should be noted that this function will print a space, since a space is needed when printing the header.

<<functions>>=
static void print_section(sorg_d *sorg, FILE *out)
{
    int i;

    if(sorg->depth < 0) return;

    fprintf(out, "%d", sorg->secpos[0]);
    for(i = 1; i <= sorg->depth; i++) {
        fprintf(out, ".%d", sorg->secpos[i]);
    }

    fputc(' ', out);
}



prev | home | next