home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 484 / NPED.ZIP / NPSPELL.PRG < prev    next >
Text File  |  1992-09-13  |  13KB  |  373 lines

  1. /*
  2.  
  3. Program Name  npSpell.prg
  4.  
  5. Purpose       Spell checker for Funck(y) II's notepad()
  6.  
  7. Author        Stephen L. Woolstenhulme    CIS 73060,1302
  8.               3805 Indigo Drive
  9.               Plano, Texas 75075
  10.  
  11. Notice        Copyright (c) 1992
  12.               Stephen L. Woolstenhulme
  13.               All Rights Reserved
  14.  
  15. Function      npSpell( np, cDelim )
  16.  
  17. Parameters    "np"      The NotePad object to check.
  18.  
  19.               "cDelim"  An optional character string of delimiters for
  20.                         tokens.  I recommend you let npSpell use its
  21.                         defaults!
  22.  
  23. */
  24.  
  25. #include "inkey.ch"
  26. #include "FUNCky.ch"
  27. #define K_ALT_BS 270
  28.  
  29. #xcommand ifNil <xVar> is <xVal> => ;
  30.                 <xVar> := iif( <xVar> == Nil, <xVal>, <xVar> )
  31.  
  32. function npSpell( np, cDelim )
  33.    local aCurWin := {}, aNewWin := {}, aSkipAll := {}
  34.    local cWord, cScrn := savescreen( 0, 0, maxrow(), maxcol() )
  35.    local i, nPos, nWordCount := 0
  36.    local lFound
  37.    local cTitleBuffer := '', nTitlePos := 0, nTitleAttr := 0
  38.  
  39.    gettitle( @cTitleBuffer, @nTitlePos, @nTitleAttr )
  40.    npSetBookMark( np, 10 )
  41.    
  42.    if cDelim == Nil
  43.        cDelim := chr(9) + chr(10) + chr(12) + chr(13) + chr(32) + chr(38) + ;
  44.                  '~`$%^@.,-_!?/\|;:(){}[]<>#+=*"0123456789'
  45.    endif
  46.    
  47.    if select( 'dict' ) == 0
  48.        use np_dict alias dict shared new
  49.    
  50.        if ! file( 'npalpha' + indexext() )
  51.            @ maxrow(), 0 say padc( 'Indexing Main Dictionary Alphabetically ...', maxcol() + 1 ) color 'w+/rb'
  52.            index on dict->WORD to npalpha
  53.            set index to
  54.        endif
  55.    
  56.        if ! file( 'npsound' + indexext() )
  57.            @ maxrow(), 0 say padc( 'Indexing Main Dictionary By Soundex ...', maxcol() + 1 ) color 'w+/rb'
  58.            index on soundex( dict->WORD ) to npsound
  59.            set index to
  60.        endif
  61.    
  62.        set index to npalpha, npsound
  63.    endif
  64.  
  65.    if select( 'dictu' ) == 0
  66.         
  67.        if ! file( 'np_dictu.dbf' )
  68.            dbcreate( 'np_dictu.dbf', dict->( dbstruct() ) )
  69.        endif
  70.    
  71.        use np_dictu alias dictu shared new
  72.    
  73.        if ! file( 'npalphau' + indexext() )
  74.            @ maxrow(), 0 say padc( 'Indexing Auxiliary Dictionary ...', maxcol() + 1 ) color 'w+/rb'
  75.            index on dictu->WORD to npalphau
  76.            set index to
  77.        endif
  78.    
  79.        set index to npalphau
  80.    endif
  81.  
  82.    npHome( np )
  83.    
  84.    do while ! npIsToken( np, cDelim )
  85.        npTokenRight( np, cDelim )
  86.    enddo
  87.    
  88.    nPos := npCsrIndex( np, 0 )
  89.    cls( 23, chr( 177 ) )
  90.    aCurWin := { npWinTop( np ), npWinLeft( np ), npWinBottom( np ), npWinRight( np ) }
  91.    aNewWin := { 2, 5, round( maxrow()*.3, 0 ), maxcol() - 5 }
  92.    npWinResize( np, aNewWin[1], aNewWin[2], aNewWin[3], aNewWin[4] )
  93.    
  94.    box( npWinTop( np )-1,               ;
  95.         npWinLeft( np )-2,              ;
  96.         npWinBottom( np )+1,            ;
  97.         npWinRight( np )+2, '', 23 )     
  98.    npDisplay( np )
  99.    settitle( ' Suggested Words ', 1, 31 )
  100.    
  101.    box( npWinBottom( np ) + 4,        ;
  102.         npWinLeft( np ) - 2,          ;
  103.         npWinBottom( np ) + 14,       ;
  104.         npWinRight( np ) + 2, '', 23 )     
  105.    @ maxrow(), 0 say padc( 'Checking spelling ...', maxcol() + 1 ) ;
  106.                             color 'w+/rb'
  107.    
  108.    do while npCsrIndex( np ) < npBuffSize( np )
  109.        cWord := padr( upper( npGetToken( np, cDelim ) ), len( dict->WORD ) )
  110.        
  111.        if ! empty( cWord )
  112.            nWordCount++
  113.            lFound := dict->( dbseek( cWord, .t. ) ) // look in main dictionary
  114.            
  115.            if ! lFound
  116.                if left( cWord, 1 ) == "'"
  117.                    cWord := padr( substr( cWord, 2 ), len( dict->WORD ) )
  118.                endif
  119.                
  120.                if right( trim( cWord), 1 ) == "'"
  121.                    cWord := padr( substr( cWord, 1, len( trim( cWord ) ) - 1 ), len( dict->WORD ) )
  122.                endif
  123.                
  124.                if right( trim( cWord ), 2 ) == "'S" // check root of 'S words
  125.                    cWord := strtran( cWord, "'S", "  " )
  126.                endif
  127.                
  128.                lFound := dict->( dbseek( cWord, .t. ) )
  129.                
  130.                if ! lFound      // check user dictionary
  131.                    lFound := dictu->( dbseek( cWord, .t. ) )
  132.                endif
  133.                
  134.                // check 'skip all' array
  135.                if ! lFound .and. ascan( aSkipAll, cWord ) == 0
  136.                    if spellFix( np, cDelim, cWord, aSkipAll )
  137.                        loop
  138.                    else
  139.                        exit
  140.                    endif
  141.                endif
  142.            endif
  143.        endif
  144.  
  145.        npTokenRight( np, cDelim )
  146.    enddo
  147.    
  148.    @ maxrow(), 0 say padc( ' Dictionary checked ' + ;
  149.                              ltrim( str( nWordCount, 5 ) ) + ;
  150.                            ' times.  Press any key to continue ... ', ;
  151.                              maxcol() + 1 ) color 'w+/rb'
  152.    inkey(0)
  153.    restscreen( 0, 0, maxrow(), maxcol(), cScrn )
  154.    npWinResize( np, aCurWin[1], aCurWin[2], aCurWin[3], aCurWin[4] )
  155.    settitle( cTitleBuffer, nTitlePos, nTitleAttr )
  156.    
  157.    box( npWinTop( np ) - 1,          ;
  158.         npWinLeft( np )-2,             ;
  159.         npWinBottom( np )+1,           ;
  160.         npWinRight( np )+2, '', 23 )
  161.         npUnMark( np )
  162.         npgobookmark( np, 10 )
  163.         npDisplay( np )
  164.    @ npWinBottom( np ) + 1, npWinLeft( np ) say 'F1:Help' 
  165.    @ npWinBottom( np ) + 1, npWinLeft( np ) + 10 say 'F2:Spell' 
  166.    @ npWinBottom( np ) + 1, npWinRight( np ) - 19 say  'F10:Exit' 
  167.    @ npWinBottom( np ) + 1, npWinRight( np ) - 8 say 'Esc:Abort' 
  168.         
  169. return NIL
  170.  
  171. static function spellFix( np, cDelim, cWord, aSkipAll )
  172.     local getlist := {}
  173.     local aAlpha := array(7), aSound := array(7)
  174.     local i, nRecno := dict->( recno() ), cChoice
  175.     local cOldWord := npGetToken( np, cDelim ), cLookUp := space(16)
  176.     local cRoot := strtran( cOldWord, "'" )
  177.     local nAlpha, nSound
  178.     local lContinue := .t., lRetVal := .t.
  179.     
  180.     npMarkToken( np, cDelim )
  181.     npDisplay( np )
  182.     csrput( npScrRow( np ), npScrCol( np ) )
  183.     lContinue := .t.
  184.     dict->( dbSeek( cWord, .t. ) )
  185.     nAlpha := dict->( recno() )
  186.     dict->( dbsetorder(2) )
  187.     dict->( dbgotop() )
  188.     dict->( dbseek( cWord, .t. ) )
  189.     nSound := dict->( recno() )
  190.     dict->( dbsetorder(1) )
  191.     dict->( dbgoto( nAlpha ) )
  192.     
  193.     do while lContinue
  194.         nSound := dict->( recno() )
  195.         // load array of options from the alpha index
  196.         dict->( dbskip( -4 ) )
  197.         
  198.         for i := 1 to 7
  199.                 aAlpha[i] := dict->WORD
  200.                 dict->( dbskip(1) )
  201.         next
  202.         
  203.         // load array of options from the soundex index
  204.         dict->( dbsetorder(2) )
  205.         dict->( dbgoto( nSound ) )
  206.         dict->( dbskip( -4 ) )
  207.         
  208.         for i := 1 to 7
  209.                 aSound[i] := dict->WORD
  210.                 dict->( dbskip(1) )
  211.         next
  212.         
  213.         dict->( dbsetorder(1) )
  214.         dict->( dbgoto( nAlpha ) )
  215.         
  216.         // call skip/add/edit/look up/scroll code here.
  217.         
  218.         @ npWinBottom( np )+5, npWinLeft( np ) +  6 say ' Alphabetical Order '
  219.         @ npWinBottom( np )+6, npWinLeft( np ) +  6 say ' ──────────────────── '
  220.         @ npWinBottom( np )+5, npWinLeft( np ) + 46 say ' Soundex Order '
  221.         @ npWinBottom( np )+6, npWinLeft( np ) + 46 say ' ─────────────── '
  222.         
  223.         for i := 1 to 7
  224.                 @ npWinBottom( np )+6+i, npWinLeft( np ) + 8  say chr(i+64) + '. ' + ;
  225.                 capFirst( aAlpha[i] )
  226.                 @ npWinBottom( np )+6+i, npWinLeft( np ) + 48 say chr(i+71) + '. ' + ;
  227.                 capFirst( aSound[i] )
  228.         next
  229.         
  230.         @ maxrow(), 0 clear
  231.         @ maxrow(), 0        say ' 1:Skip '
  232.         @ maxrow(), col()+ 1 say ' 2:Skip All '
  233.         @ maxrow(), col()+ 1 say ' 3:Add '
  234.         @ maxrow(), col()+ 1 say ' 4:Edit '
  235.         @ maxrow(), col()+ 1 say ' 5:Look Up '
  236.         @ maxrow(), col()+ 1 say ' -:Up '
  237.         @ maxrow(), col()+ 1 say ' +:Down '
  238.         @ maxrow(), col()+ 1 say ' Esc:Abort '
  239.         inkey(0)
  240.         cChoice := upper( chr( lastkey() ) )
  241.         
  242.         do case
  243.                 
  244.         case cChoice $ 'ABCDEFGHIJKLMN'  // picked from alpha/soundex lists
  245.             
  246.             if at( cChoice, 'ABCDEFG' ) > 0
  247.                     cWord := aAlpha[ at( cChoice, 'ABCDEFG' ) ]
  248.             elseif at( cChoice, 'HIJKLMN' ) > 0
  249.                     cWord := aSound[ at( cChoice, 'HIJKLMN' ) ]
  250.             endif
  251.             
  252.             
  253.             do case
  254.                     
  255.             case cRoot == capfirst( cRoot ) // match proper case
  256.                     cWord := capfirst( trim( cWord ) )
  257.                     
  258.             case cRoot == upper( cRoot ) // match upper case
  259.                     cWord := upper( trim( cWord ) )
  260.                     
  261.             otherwise
  262.                     cWord := lower( trim( cWord ) )
  263.                     
  264.             endcase
  265.             
  266.             if left( cOldWord, 1 ) == "'"
  267.                     cWord := "'" + cWord
  268.             endif
  269.             
  270.             if right( cOldWord, 1 ) == "'"
  271.                     cWord := trim( cWord ) + "'"
  272.             endif
  273.             
  274.             npDeleteBlock( np )
  275.             npInsertText( np, cWord )
  276.             npDisplay( np )
  277.             lContinue := .f.
  278.             
  279.         case cChoice == '1'              // picked skip
  280.             npWordRight( np )
  281.             lContinue := .f.
  282.                 
  283.         case cChoice == '2'              // picked skip all
  284.             cWord := padr( upper( cWord ), len( DICT->WORD ) )
  285.             aadd( aSkipAll, padr( upper( npGetToken( np, cDelim ) ), len( dict->WORD ) ) )
  286.             lContinue := .f.
  287.             
  288.         case cChoice == '3'              // add to dictionary
  289.             dictu->( dbappend() )
  290.             dictu->( rlock() )
  291.             dictu->WORD := upper( cWord )
  292.             dictu->( dbcommit() )
  293.             dictu->( dbunlock() )
  294.             lContinue := .f.
  295.             
  296.         case cChoice == '4'              // edit
  297.             @ maxrow(), 0 say padc( 'Make changes, then press F10 ... ', maxcol() + 1 ) 
  298.             npSetBookMark( np, 10 )
  299.             notepad( np )
  300.             npGoBookMark( np, 10 )
  301.             lContinue := .f.
  302.                 
  303.         case cChoice == '5'              // look up
  304.             cLookUp := space(16)
  305.             @ maxrow(), 0 clear
  306.             @ maxrow(), 0 say 'Find words beginning with' get cLookUp
  307.             read
  308.             
  309.             if ! lastkey() == K_ESC .and. ! empty( cLookUp )
  310.                  dict->( dbseek( upper( trim( cLookUp ) ), .t. ) )
  311.             endif
  312.             
  313.         case cChoice == '-'              // skip up
  314.             dict->( dbskip(-5) )
  315.             nAlpha := dict->( recno() )
  316.             
  317.             dict->( dbsetorder(2) )
  318.             dict->( dbgoto(nSound) )
  319.             dict->( dbskip(-5) )
  320.             nSound := dict->( recno() )
  321.             
  322.             dict->( dbsetorder(1) )
  323.             dict->( dbgoto(nAlpha) )
  324.             
  325.         case cChoice == '+'               // skip down
  326.             dict->( dbskip(5) )
  327.             nAlpha := dict->( recno() )
  328.             
  329.             dict->( dbsetorder(2) )
  330.             dict->( dbgoto(nSound) )
  331.             dict->( dbskip(5) )
  332.             nSound := dict->( recno() )
  333.             
  334.             dict->( dbsetorder(1) )
  335.             dict->( dbgoto(nAlpha) )
  336.         
  337.         case lastkey() == K_ESC
  338.             lRetVal := .f.
  339.             lContinue := .f.
  340.  
  341.         endcase
  342.     enddo
  343.     
  344.     @ maxrow(), 0 clear
  345.     
  346. return lRetVal
  347.  
  348.  
  349. static function npTokenRight( np, cDelim )    // Not in Funck(y), but needed.
  350.     do while npIsToken( np, cDelim ) .and. ;
  351.         npCsrIndex( np ) < npBuffSize( np )
  352.         npCsrRight( np, 1 )
  353.     enddo
  354.     
  355.     do while ! npIsToken( np, cDelim ) .and. ;
  356.         npCsrIndex( np ) < npBuffSize( np )
  357.         npCsrRight( np, 1 )
  358.     enddo
  359. return Nil
  360.  
  361. static function npTokenLeft( np, cDelim )     // Not in Funck(y), but needed.
  362.     do while npIsToken( np, cDelim ) .and. ;
  363.         npCsrIndex( np ) > 1
  364.         npCsrLeft( np, 1 )
  365.     enddo
  366.         
  367.     do while ! npIsToken( np, cDelim ) .and. ;
  368.         npCsrIndex( np ) > 1
  369.         npCsrLeft( np, 1 )
  370.     enddo
  371. return Nil
  372.  
  373.