home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / pine / pico / spell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  7.2 KB  |  294 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: spell.c,v 4.5 1993/11/09 00:41:53 mikes Exp $";
  3. #endif
  4. /*
  5.  * Program:    spell.c
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  * Copyright 1991-1993  University of Washington
  19.  *
  20.  *  Permission to use, copy, modify, and distribute this software and its
  21.  * documentation for any purpose and without fee to the University of
  22.  * Washington is hereby granted, provided that the above copyright notice
  23.  * appears in all copies and that both the above copyright notice and this
  24.  * permission notice appear in supporting documentation, and that the name
  25.  * of the University of Washington not be used in advertising or publicity
  26.  * pertaining to distribution of the software without specific, written
  27.  * prior permission.  This software is made available "as is", and
  28.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  29.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  30.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  31.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  32.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  33.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  34.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  35.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  36.  *
  37.  * Pine and Pico are trademarks of the University of Washington.
  38.  * No commercial use of these trademarks may be made without prior
  39.  * written permission of the University of Washington.
  40.  *
  41.  */
  42.  
  43. #include <stdio.h>
  44. #include "osdep.h"
  45. #include "estruct.h"
  46. #ifdef    SPELLER
  47. #include "edef.h"
  48. #include "pico.h"
  49.  
  50. #ifdef    ANSI
  51.     void chword(char *, char *);
  52.     int  movetoword(char *);
  53. #else
  54.     void chword();
  55.     int  movetoword();
  56. #endif
  57.  
  58.  
  59. #define  NSHLINES  12
  60.  
  61. static char *spellhelp[] = {
  62.   "Spell Check Help",
  63.   " ",
  64.   "\tThe spell checker examines all words in the text.  It then",
  65.   "\toffers each misspelled word for correction while simultaneously",
  66.   "\thighlighting it in the text.  To leave a word unchanged simply",
  67.   "~\thit ~R~e~t~u~r~n at the edit prompt.  If a word has been corrected,",
  68.   "\teach occurrence of the incorrect word is offered for replacement.",
  69.   " ",
  70.   "~\tSpell checking can be cancelled at any time by typing ~^~C (~F~3)",
  71.   "\tafter exiting help.",
  72.   " ",
  73.   "End of Spell Check Help",
  74.   " ",
  75.   NULL
  76. };
  77.  
  78.  
  79. static char *pinespellhelp[] = {
  80.   "Spell Check Help",
  81.   " ",
  82.   "\tThe spell checker examines all words in the text.  It then",
  83.   "\toffers each misspelled word for correction while simultaneously",
  84.   "\thighlighting it in the text.  To leave a word unchanged simply",
  85.   "\thit Return at the edit prompt.  If a word has been corrected,",
  86.   "\teach occurrence of the incorrect word is offered for replacement.",
  87.   " ",
  88.   "\tSpell checking can be cancelled at any time by typing ^C (F3)",
  89.   "\tafter exiting help.",
  90.   " ",
  91.   "End of Spell Check Help",
  92.   " ",
  93.   NULL
  94. };
  95.  
  96.  
  97.  
  98. /*
  99.  * spell() - check for potentially missspelled words and offer them for
  100.  *           correction
  101.  */
  102. spell(f, n)
  103. {
  104.     int    status, next, khelp = 0, ret;
  105.     FILE   *p;
  106.     char   *b, *sp, *fn;
  107.     char   wb[NLINE], cb[NLINE];
  108.     char   *writetmp();
  109.     FILE   *P_open();
  110.  
  111.     setimark(0, 1);
  112.     emlwrite("Checking spelling...");         /* greetings */
  113.  
  114.     if((fn = writetmp(0, 0)) == NULL){
  115.     emlwrite("Can't write temp file for spell checker");
  116.     return(-1);
  117.     }
  118.     if((sp = (char *)getenv("SPELL")) == NULL)
  119.       sp = SPELLER;
  120.     sprintf(s, "( %s ) < %s", sp, fn);
  121.  
  122.     if((p=P_open(s)) == NULL){            /* read output from command */
  123.     unlink(fn);
  124.     emlwrite("Can't fork spell checker");
  125.     return(-1);
  126.     }
  127.  
  128.     khelp = 0;
  129.     ret = 1;
  130.     while(fgets(wb, NLINE, p) != NULL && ret){
  131.     if((b = (char *)strchr(wb,'\n')) != NULL)
  132.       *b = '\0';
  133.     strcpy(cb, wb);
  134.  
  135.     gotobob(0, 1);
  136.  
  137.     if(Pmaster == NULL)
  138.       sgarbk = FALSE;
  139.  
  140.     status = TRUE;
  141.     next = 1;
  142.  
  143.     while(status){
  144.         if(!khelp++)            /* not too early */
  145.           wkeyhelp("GC0000000000", "Get Help,Cancel");
  146.  
  147.         if(next++)
  148.           if(movetoword(wb) != TRUE)
  149.         break;
  150.  
  151.         update();
  152.         (*term.t_rev)(1);
  153.         pputs(wb, 1);            /* highlight word */
  154.         (*term.t_rev)(0);
  155.  
  156.         if(strcmp(cb, wb)){
  157.         sprintf(s, "Replace %s with %s", wb, cb);
  158.         status=mlyesno(s, TRUE);
  159.         }
  160.         else
  161.           status=mlreplyd("Edit a replacement: ", cb, NLINE, QDEFLT);
  162.  
  163.  
  164.         curwp->w_flag |= WFMOVE;        /* put cursor back */
  165.         update();
  166.         pputs(wb, 0);            /* un-highlight */
  167.  
  168.         switch(status){
  169.           case TRUE:
  170.         chword(wb, cb);            /* correct word    */
  171.           case FALSE:
  172.         update();            /* place cursor */
  173.         break;
  174.           case ABORT:
  175.         emlwrite("Spell Checking Cancelled", NULL);
  176.         ret = FALSE;
  177.         status = FALSE;
  178.         break;
  179.           case HELPCH:
  180.         if(Pmaster)
  181.           (*Pmaster->helper)(pinespellhelp, 
  182.                      "Help with Spelling Checker", 1);
  183.         else
  184.           pico_help(spellhelp, "Help with Spelling Checker", 1);
  185.           case (CTRL|'L'):
  186.         next = 0;            /* don't get next word */
  187.         khelp = 0;            /* key help has changed */
  188.         sgarbf = TRUE;            /* repaint full screen */
  189.         update();
  190.         status = TRUE;
  191.         continue;
  192.           default:
  193.         emlwrite("Huh?");        /* shouldn't get here, but.. */
  194.         status = TRUE;
  195.         sleep(1);
  196.         break;
  197.         }
  198.         forwword(0, 1);            /* goto next word */
  199.     }
  200.     }
  201.     P_close(p);                    /* clean up */
  202.     unlink(fn);
  203.     swapimark(0, 1);
  204.     curwp->w_flag |= WFHARD|WFMODE;
  205.  
  206.     if(Pmaster == NULL)
  207.       sgarbk = TRUE;
  208.  
  209.     if(ret)
  210.       emlwrite("Done checking spelling");
  211.     return(ret);
  212. }
  213.  
  214.  
  215.  
  216.  
  217. /* 
  218.  * chword() - change the given word, wp, pointed to by the curwp->w_dot 
  219.  *            pointers to the word in cb
  220.  */
  221. void
  222. chword(wb, cb)
  223.   char *wb;                    /* word buffer */
  224.   char *cb;                    /* changed buffer */
  225. {
  226.     ldelete(strlen(wb), 0);            /* not saved in kill buffer */
  227.     while(*cb != '\0')
  228.       linsert(1, *cb++);
  229.  
  230.     curwp->w_flag |= WFEDIT;
  231. }
  232.  
  233.  
  234.  
  235.  
  236. /* 
  237.  * movetoword() - move to the first occurance of the word w
  238.  *
  239.  *    returns:
  240.  *        TRUE upon success
  241.  *        FALSE otherwise
  242.  */
  243. movetoword(w)
  244.   char *w;
  245. {
  246.     int      i;
  247.     int      ret  = FALSE;
  248.     int         olddoto;
  249.     LINE     *olddotp;
  250.     register int   off;                /* curwp offset */
  251.     register LINE *lp;                /* curwp line   */
  252.  
  253.     olddoto = curwp->w_doto;            /* save where we are */
  254.     olddotp = curwp->w_dotp;
  255.  
  256.     curwp->w_bufp->b_mode |= MDEXACT;        /* case sensitive */
  257.     while(forscan(&i, w, 1) == TRUE){
  258.     if(i)
  259.       break;                /* wrap NOT allowed! */
  260.  
  261.     lp  = curwp->w_dotp;            /* for convenience */
  262.     off = curwp->w_doto;
  263.  
  264.     /*
  265.      * We want to minimize the number of substrings that we report
  266.      * as matching a misspelled word...
  267.      */
  268.     if(off == 0 || !isalpha(lgetc(lp, off - 1).c)){
  269.         off += strlen(w);
  270.         if((!isalpha(lgetc(lp, off).c) || off == llength(lp)) 
  271.            && lgetc(lp, 0).c != '>'){
  272.         ret = TRUE;
  273.         break;
  274.         }
  275.     }
  276.  
  277.     forwchar(0, 1);                /* move on... */
  278.  
  279.     }
  280.     curwp->w_bufp->b_mode ^= MDEXACT;        /* case insensitive */
  281.  
  282.     if(ret == FALSE){
  283.     curwp->w_dotp = olddotp;
  284.     curwp->w_doto = olddoto;
  285.     }
  286.     else
  287.       curwp->w_flag |= WFHARD;
  288.  
  289.     return(ret);
  290. }
  291. #else
  292. spell(){}
  293. #endif    /* SPELLER */
  294.