% Copyright 1991 by Norman Ramsey.  All rights reserved.
% See file COPYRIGHT for more information.
\subsection{Managing indentation and columns}
These functions are used to help accumulate indentations across module
boundaries by measuring the widths of various strings
in columns.
The size of things depends on [[tabsize]] as defined in columns.h.
If [[tabsize == 0]], tabs shouldn't be touched on input and won't be
generated on output.
<<header>>=
extern int tabsize;
extern int columnwidth (char *s);      /* number of columns string occupies */
extern int limitcolumn (char *s, int startcolumn);
                                /* width of startcolumn blanks plus s */
extern void indent_for (int width, FILE *fp);
                                /* indent to width; next char -> width+1 */
@ %def tabsize
<<*>>=
#include <stdio.h>
#include "columns.h"

int tabsize = 8;
<<*>>=
int columnwidth (char *s) {             /* width of a string in columns */
  return limitcolumn(s, 0);
}
@ %def columnwidth
<<*>>=
int limitcolumn (char *s, int col) {
    while (*s) {
        col++;
        if (*s=='\t' && tabsize > 0) while (col % tabsize != 0) col++;
        s++;
    }
    return col;
}
@ %def limitcolumn
<<*>>=
void indent_for (int width, FILE *fp) {
                                /* write whitespace [[width]] columns wide */
/*fprintf(fp,"<%2d>",width); if (width>4) {fprintf(fp,"    "); width -= 4;}*/
    if (tabsize > 1)
        while (width >= tabsize) {
            putc('\t', fp);
            width -= tabsize;
        }
    while (width > 0) {
        putc(' ', fp);
        width--;
    }
}
@ %def indent_for
