;/* Execute me to compile me with DICE
dcc ScannerSection.c -// -l0 -mD -mRR -o golded:scanner/Texinfo_Section
quit ;*/

/* -----------------------------------------------------------------------------

    scanner for LaTeX sectioning (\chapter, \section, ...)

    written by

    P!\K of /|_ __   . /\  |\__: /|____  /\__:__/\   .
           /   |  \  :/  \ |   |/      \/    |    \  :
         _/    |   \_|  \ \|  _/   |\   \___   ___/  |
         \     |     |   \ \  \    |/   /         \  |
          \ _________|____\  _/\ ______/\  __|__  /  :
           \|        :     \/   \|       \/  ·  \/   .

    email: bstegema@ix.urz.uni-heidelberg.de
    www: http://www.rzuser.uni-heidelberg.de/~bstegema

  ------------------------------------------------------------------------------
*/

#include <exec/types.h>

ULONG ScanHandler(__D0 ULONG len, __A0 char **text, __A1 ULONG *line) {
    char *version = "$VER: Texinfo_Section 1.0 (10.9.96)";
    char *keywords[] = {
        "chapter", (char *)0,
        "section", (char *)3,
        "subsection", (char *)6,
        "subsubsection", (char *)9,
        "top", (char *)0,
        "unnumbered", (char *)0,
        "unnumberedsec", (char *)3,
        "unnumberedsubsec", (char *)6,
        "unnumberedsubsubsec", (char *)9,
        "appendix", (char *)0,
        "appendixsec", (char *)3,
        "appendixsubsec", (char *)6,
        "appendixsubsubsec", (char *)9,
        "majorheading", (char *)0,
        "chapheading", (char *)0,
        "heading", (char *)3,
        "subheading", (char *)6,
        "subsubheading", (char *)9,
        NULL
      };

    char *scan = *text;
    char *end = scan + len;

    while ( scan < end ) {
        if ( *scan++ == '@' ) {
            char *eon = scan;
            while ( (eon < end) && (*eon >= 'a') && (*eon <= 'z') )
                eon++;

            if ( eon - scan > 2 ) {
                char **key;
                for ( key = keywords ; *key ; key += 2 ) {
                    LONG i = 0;

                      {
                        char *left = *key;
                        char *right = scan;
                        while ( i < eon - scan ) {
                            if ( *left++ != *right++ )
                                break;
                            i++;
                          }
                      }

                    if ( i == eon - scan ) {
                        scan = eon;
                        while ( (scan < end) && (*scan == ' ') )
                            scan++;

                        if ( scan < end ) {
                            LONG length = end - scan;
                            if ( length > 80 )
                                length = 80;

                            char buf[100];
                            char *dest = buf;
                            LONG indent = (LONG)key[1];
                            while ( indent > 0 ) {
                                *dest++ = ' ';
                                indent--;
                              }

                            while ( length > 0 ) {
                                *dest++ = *scan++;
                                length--;
                              }

                            *dest = 0;

                            *text = buf;
                            return(dest - buf);
                          }

                        return(0);
                      }
                  }
              }

            scan = eon;
          }
      }

    return(0);
  }

