#!../src/tops -s ../sys -u ../usr

# File cat2doc, running from admin/

"cat2doc" is this_script \ must match the name of this file being run

# Reads category definitions and modifies the .doc file named on the 
# command line.

# The top of this file does the work, and the bottom of this file 
# contains the categories and their words.

\-----------------------------------------------------------------------

keys?

IF "newman.doc" (qFile) \ interactive testing: 'cat2doc' source

ELSE \ running in batch with .doc file name given on the command line:

   "Usage: cat2doc <filename>" is usage

   # Must have at least 3 argvs (like: tops cat2doc myfile.doc):
   ARGV rows 3 < IF usage . nl halt THEN

   # Next-to-last argv is this_script:
   ARGV its rows one less ndx quote -path this_script alike not
   IF usage . nl halt THEN

   # Last argv is name of file to read, insert categories, and save:
   ARGV one endmost strchop (qFile)

THEN

(qFile) "docfile" book     \ file to insert categories within
this_script "catfile" book \ categories data are on this script

\-----------------------------------------------------------------------

\  Files to process:
   docfile filefound not IF " not found: " . docfile . nl halt THEN
   (qS) into docfile

   catfile filefound not IF " not found: " . catfile . nl halt THEN
   (qS) into catfile

   "category:" is keyword \ keying on this in both files

\  Handle to categories file:
   catfile old binary "CAT" again closeif open

\  Mapping the categories file:
   CAT purged keyword fmapleft (hMap)
   this 1st catch into C0 \ offset to each category
   2nd catch into Cn      \ length of each category line

   C0 Cn plus into W0     \ offset to each category's word list
   C0 -1 lag CAT fsize  
   that these rows ndx
   poke W0 less into Wn   \ length of each category's word list

\  Making a hash table with words as keys, and categories as values:
   "" "" 4000 "%CAT" hash_make
   W0 rows 1st
   DO CAT W0 I pry fseek
      CAT Wn I pry fget (hT) words chop (hKeys)
      CAT C0 I pry fseek
      CAT Cn I pry fget (hT) asciify chop
      keyword strlen erase chop
      those rows repeat (hVals)
      %CAT hash_add
   LOOP
   CAT close

\  Overwriting docfile categories with the ones from hash table:
   this_script "Utilities" msource

   docfile asciiload into DOC
   DOC keyword grepr into DOCrows
   DOC DOCrows reach 1st word drop into DOCwords

   DOCwords rows 1st
   DO %CAT DOCwords I quote hash_lookup drop any?
      IF (hT) keyword spaced swap commas "_" chblank cat
         DOCwords I quote chop spaced swap cat
         DOC chars +trailing (qS)
         DOC (qS hT) DOCrows I pry (row) said
      ELSE " no category for " . DOCwords I quote . nl
      THEN
   LOOP

\  File bak1 to bak2, old docfile to bak1:
   docfile "." cat 1 suffix is bak1
   docfile "." cat 2 suffix is bak2

   bak1 file?  IF "mv " bak1 spaced, bak2 cat cat shell THEN
   "mv " docfile cat spaced, bak1 cat shell 

\  Saving revised docfile:
   DOC docfile save

   nl " revised " . docfile -path . " saved" . nl

   private halt

\-----------------------------------------------------------------------

   Utilities

   "commas" missing IF tex.v source THEN
   private halt


{  WORDS will extract all the words in a category from a .doc file.

      To source: "cat2doc" "Category extract" msource

      Then use WORDS; see example below.  

      Note that WORDS is set to load newman.doc every time it runs, 
      in case the file has been edited.
}
   Category extract

   public
   define: WORDS (qCategory --- ) \ lists words for Category

      docpath "newman.doc" catpath into docfile
      "category:" is keyword 

      docfile asciiload into DOC
      DOC keyword grepr into DOCrows
      DOC DOCrows reach 1st word drop into DOCwords
   
      "_" chblank push sp sp sp
      DOCwords rows 1st
      DO DOC DOCrows I pry quote (hS) dup peek grepr rows any
         IF (hS) \ DOCwords I quote "file.handle" alike IF halt THEN
            keyword tug              \ move right to keyword
            peek tug                 \ move right to Category
            "," chblank              \ commas into blanks
            peek chars tic +trailing \ at least one extra blank 
            1st peek chars tic items \ to catch chars+1       
            catch peek (qS1 qS2) alike
            IF DOCwords I quote strchop . sp out 60 >
               IF nl sp sp sp THEN
            THEN
         ELSE (hS) drop
         THEN
      LOOP pull drop
   end
   private halt

   Example:

[tops@clacker] ready > "math::matrix::operator" WORDS
   *diagonal >vector @ bang chain chainr chars clone cobs cols 
   compare compareAbove compareRef cows columnMaxMin cram diag 
   diagpost diagpost_complex diagpre diagpre_complex dims down 
   dup_sum dupes EIVrows endcols endmost filter find flatten 
   fold foldr 
[tops@clacker] ready > 

\-----------------------------------------------------------------------

Categories and their words.

To keep commas straight, link multiple words of a category with under-
scores.  The underscores are removed during processing.

   Example.  Categories given in this file as:
      math program_control string signal_processing multitasker

   will become after processing:
      math, program control, string, signal processing, multitasker

Begin categories and words.

category: bit_operator
   2real 4ints 4real == bit bitoff biton bswap4 byte 

category: boolean_operator
   < <= <> = > >= any any? exe? execute exists? file? filefound 
   filetrue flip greaterthan lessthan local? not 

category: catalog
   ! .d .e _catitems are bank book called catbins catitems catnames 
   catseq cprops CONS ctype extract fence fence_at implant implant1 

category: communication
   _pppconnect 

category: constant
   -INF -Inf 2pi BL cm/in cyc/rad eight false five four GIJILLION 
   in/cm in/mm INF left MILLION mm/in nine one pi rad/cyc rad/deg 
   right seven six ten three tiny true two UDEF zero 

category: file
   -path append append4 ascii asciiload asciiread bcreate BIG_ENDIAN 
   binary bsource catf catpath cd chdir close closeif copy4 delete 
   deleteif dir_watch dir? dirnames dirsize edit eload endian 
   esave export2 export4 export8 fclose fend fendget fendlen 
   fendtag fget fgetr fhead file? file file_watch file.handle 
   file.pos file.rechead file.recsize file.size file.sizeof file.status 
   file.type fileatime filectime filefound filetrue filesize 
   filetime finitr fload fmapl fmapleft fmapNL forn fortbackspace 
   fortclose fortendian_set fortfile fortmap fortput fortrec 
   fortrecs fortrewind fortseek fortskip fortstep fpos fprops 
   fput fputr frec fseek fsize fstep fstr fstr1 fwrite get get_sorted 
   get2 get2c get4 get4c get4sp import2 import4 import4f import8 
   include open uimport2 

category: formatting
   32nds 8ths _int$ 

category: graphics
   .fontTable blend Blue colorpix colorvalue colorset Cpix dheight 
   dheightMM dprops dwidth dwidthMM ecb fcb fontadd fontdef fontfcb 
   fontid fontTable gcb GCcreate GCfree GCfore GCline getCLIP 
   graphset Green 

category: hash
   _hash

category: keyboard
   clear

category: math
   * + - -cmagphase / /by /cby /element /factor /f /mod 10^ 2^ 
   5^ across atan2 bins bins1 bsearch caccel ceig cgm changes 
   closest cdisp cmag cmagphase colsort cos cosh cosine columnMaxMin 
   cov crate cruddy db10 db20 dbcheatsheet dgemm digits e^ expspace 
   flip less 

category: math::complex
   *c *cby *conj complex diagpost_complex diagpre_complex

category: math::fft
   _lamp FFT fft1 wfft

category: math::matrix

category: math::vector
   *cross *dot

category: math::rotation
   *dircos9 cylvec dircos dircos' dircos9 sphvec xformvec

category: math::matrix::creation
   asciiread fget fgetr fill flips fload get list: matread matrix 

category: math::matrix::partitioning
   catch claw dice fetch pile pilen rake rip ram reach repeat 
   stuff submat trilp triup 

category: math::matrix::termwise
   *by *cby *element *factor *f + +d +offset -d goose 

category: matlab
   _engEval _engGet _engOpen _engPut _ep _matlab engGet engOff 
   engOn engOn? engPut engRun matlab 

category: math::matrix::operator
   *diagonal >vector @ bang chain chainr chars clone cobs cols 
   compare compareAbove compareRef cows columnMaxMin cram diag 
   diagpost diagpost_complex diagpre diagpre_complex dims down 
   dup_sum dupes EIVrows endcols endmost filter find flatten 
   fold foldr 

category: memory_management
   2drop drop dump freed lop mallinfo memfree mempeek mempull 
   memptr memptrshow memput memprobe memspy memarena memfreed 
   meminuse purged 

category: multitasker
   -ALARM ALARM ctime everyhour frate ftic nexthour PLAY TASK 

category: nastran
   append4 copy4 get_sorted get2 get2c get4 get4c get4sp

category: partitioning
   blanklines catch claw comb cull dice fetch pile pilen rake 
   rip ram reach repeat stuff submat trilp triup 

category: perl
   _perl

category: printing
   . .bin .d .e .fontTable .hex .i .m .me .out .totals .s .u 
   .uinthex cr dot echo emit ersys flush_sysout fontadd fontdef 
   fontfcb fontid fontTable format luhrs mformat mformatset mtext 

category: program_control
   "} # ' ( ) +LOOP -hide -inlinex -stkbal -strict -trace 0based 
   1based 1st 2nd 3rd 4th ; ?DO @* [ \ ] _bin _binh _syspath 
   ARGV argv BIG_ENDIAN bmake brun bye CATMSG catmsg clean clear 
   console cprompt define: DEFN defname DO docpath done done_off 
   dprops ecb ELSE end env ercnt erp ersys ertrip exe exe? execute 
   exists? exit expecting expectout expectkill extract fcb filefound 
   filetrue flush_sysout gcb getlogin getpid getuid halt IF inline: 
   inlinex LOOP notrace quit return THEN TRACE trace wtrace {" 

category: program_control::keyboard
   accept getch

category: program_control::time
   ctime date elapsed everyhour fileatime filectime filetime 
   greg 

category: shell
   _vi _view 

category: signal_processing
   band_table bands cheby1 cheby2 detrend ellip Frms Frms_log 
   
category: sparse

category: stack_operator
   .s 2drop 2dup cop depth depthSTK drop dump dup lop luhrs other 
   over pick rev revn roll rot swap uh 

category: strings
   " "} ' -blanklines -path -trailing _cite _int$ alike alphabetize 
   asciify backward between BL blanklines blockofblanks brandom 
   bswap4 cat cats catpath character chars chblank cheep chkeep 
   choose chop cite columnofints crop crowd field fieldwidth 
   grab grabs grep grep1 grep2 grepe grepr notrailing nullbyte 
   pilen pileof quote quotes remtab remtabf suffix {" 
