;/* Execute me to compile me with DICE
dcc ScannerSection.c -// -l0 -mD -mRR -o golded:scanner/LaTeX_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: LaTeX_Section 1.0 (10.9.96)";
    char *keywords[] = {
        "chapter",
        "section",
        "subsection",
        "subsubsection",
        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 > 5 ) {
                char **key;
                LONG indent;
                for ( key = keywords, indent = 0 ; *key ; key++, indent += 3 ) {
                    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 ) {
                            scan++;
                            eon = scan;
                            LONG depth = 1;
                            while ( (eon < end) && (depth > 0) ) {
                                if ( *eon == '{' )
                                    depth++;
                                else if ( *eon == '}' )
                                    depth--;

                                eon++;
                              }

                            LONG length = eon - scan - 1;
                            if ( length > 80 )
                                length = 80;

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

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

                            *dest = 0;

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

                        return(0);
                      }
                  }
              }

            scan = eon;
          }
      }

    return(0);
  }

