BEGIN   { code = 0
          codeno = 0
          codechunkno = 1

          definitions = ":def:|:edef:|:xdef:|:gdef:"\
             ":let:|"\
             ":chardef:|:mathchardef:|"\
             ":newcount:|:countdef:|"\
             ":newdimen:|:dimendef:|"\
             ":newskip:|:skipdef:|"\
             ":newtoks:|:toksdef:|"\
             ":muskipdef:|"\
             ":newif:|"\
             ":newbox:|"\
             ":read:"\
             ":font:"

          print "\\input tweb.sty\n\n"\
           "{\\catcode`\\| = 0 |catcode`|\\ = 12 %\n"\
           "|obeylines |gdef|ttfinish#1\\EDOC{|gobblespace#1|endgroup}}\n\n"\
           "\\makeindexes\n"
}

END   { print "\n"\
              "\\printuserleveldefs\n"\
              "\\printdefs\n"\
              "\\printchunks\n"\
              "\\bye"
}


/^@text %%% *version *= *"/ \
{
      gsub(/^@text %%% *version *= *"/, "")
      gsub(/",/, "")
      printf "\\gdef \\RCSversion {%s}\n", $0
      next
}

/^@file /       { printf "\\def\\filename{%s}\n", substr($0,7) ; next }

/^@quote$/      { printf "\|"  ; next }
/^@endquote$/   { printf "\|"  ; next }

/^@begin code/  {
                  print "\\BEGINCODE"
                  code = 1
                  codeno = $3
                  next
                }
/^@end code/    {
                  print "\\ENDCODE"
                  code = 0
                  next
                }

/^@begin docs/  {
                  printf "\\BEGINDOC\n"
                  next
                }
/^@end docs/    { printf "\\ENDDOC\n" ; next }

/^@defn /       {
                  gsub(/\[\[/, "\\CODE "); gsub(/]]/, "\\EDOC")
                  name = substr($0,7)
                  defs[name] += 1
                  printf "\\noindent\\LA %s\\RA", name

                  if (defs[name] > 1)
                     printf "$+\\!\\!\\equiv$"
                  else
                     printf "$\\equiv$"

                  gsub("@", "\"@", name)
                  printf "\%\n\\chk{\\LA %s\\RA}"\
                         "\n\\nobreak\\medskip", name

                  next
                }
/^@use /        {
                  gsub(/\[\[/, "\\CODE "); gsub(/]]/, "\\EDOC")
                  name = substr($0,6)
                  used[name] = codeno
                  printf "\\LA %s\\RA", name
                  gsub("@", "\"@", name)
                  printf "\%\n\\chk{\\LA %s\\RA}", name, name

                  getline
                  next
                }

/^@literal /    { printf "%s", substr($0, 10) ; next }

/^@nl$/         { printf "\n" ; next }

/^@text /       { line = junk = substr($0,7)
                  indexed = ""
                  found = ""

                  if (code)
                  {
                     while (match(junk, /\\[a-zA-Z@]+/)>0\
                         && substr(junk, 1, RSTART) !~ /%/)
                     {
                        prefix = "\\ddx{"

                        found = substr(junk, RSTART+1, RLENGTH-1)
                        junk = substr(junk, RSTART+RLENGTH)

                        if ( ":" found ":" ~ definitions )
                        {
                           prefix = "\\udx{"
                           match(junk, /\\[a-zA-Z@]+|\\[^a-zA-Z@]/)
                           found = substr(junk, RSTART+1, RLENGTH-1)
                           junk = substr(junk, RSTART+RLENGTH)
                        }

                        if (found != "" && found !~ /[^a-zA-Z@]/)
                        {
                           gsub("@", "\"@", found)
                           indexed = "\%\n"\
                              prefix found\
                              "@\\\\" found "\|N{"\
                              codechunkno "}}" indexed
                        }
                     }
                     printf "\\NO{%d}\\CODE %s\\EDOC%s",\
                         codechunkno++, line, indexed
                  }
                  else
                     printf "%s", line
               }