home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / reactivekbd / part01 / rk_file.c < prev   
Encoding:
C/C++ Source or Header  |  1989-10-16  |  3.2 KB  |  125 lines

  1. /* "FILE's" link to rk_button, written by John Darragh, Calgary, revised 3-89
  2.  *
  3.  * these are the main function used by file+rk.c to generate predictions
  4.  */
  5. #include "rk_button.h"
  6. #include "file+rk.h"
  7.  
  8. extern int     pred_number;              /* Defined in file+rk.c   */
  9. extern char     nl_truncate_mode,
  10.          eol_only_mode,
  11.          eol_longer_mode;
  12.          show_eol_mode;
  13. extern ED_STRUCT editor_data;
  14.  
  15. extern char   first[MAX_SET],              /* Defined in rk_button.c */
  16.               context[MAX_CMD_LINE_LENGTH],
  17.               old_context[MAX_CMD_LINE_LENGTH];
  18. extern Buffer Buf, CBuf;
  19. extern char   *prime_file;
  20. extern char   pred_on_display;
  21. int max_len=8;
  22. int max_eol=30;
  23. int maxk=8;
  24. int max_freq=127;
  25. int max_nodes=256*1024;              /* 256K */
  26.  
  27.  
  28. static char   temp[1024];              /* scratch string buf */
  29.  
  30.  
  31. make_a_prediction (s) char *s; {    /* sets s to the current prediction */
  32.  
  33.     char *a = temp; int length, i;
  34.     
  35.     if (eol_only_mode && *(editor_data.dot)) { *s = '\0'; return; }
  36.         strcpy (temp, context);
  37.     strcat (temp, editor_data.current_buffer);
  38.     length = strlen (temp) - strlen (editor_data.dot);
  39.     temp[length] = '\0';
  40.     if (length > maxk) a = &temp[length - maxk];
  41.  
  42.     if (strcmp (old_context, a)) pred_number = 0;
  43.     strcpy (old_context, a);
  44.     
  45.     for (i=1; i<=maxk; i++) CBuf[i] = nil;
  46.     while (*a) {
  47.         for (i=maxk; i>0; i--) CBuf[i] = scan_up(CBuf[i-1],*a);
  48.         a++;
  49.     }
  50.     find_first(CBuf);
  51.         build_menu(CBuf, s);
  52.     }
  53.  
  54. update_the_model (s) char *s; { /* adds s into the model & updates log */
  55.  
  56.     char *a = s, *c = s; int length, i; FILE *to;
  57.  
  58.     if (strlen(s) < maxk) {
  59.             strcpy (temp, context);
  60.             strcat (temp, s);
  61.         length = strlen (temp);
  62.         if (length > maxk) a = &temp[length - maxk];
  63.         else a = temp;
  64.     }
  65.     strcpy (context, a);
  66.     while (*c) {
  67.         for (i=maxk; i>0; i--) Buf[i] = move_up(Buf[i-1],*c);
  68.         c++;
  69.     }            /* do not save "empty" lines in the log file */
  70.                     /* reopen to append log, otherwise create it */
  71.         if (s[0] != '\n') {    /* now log can be manipulated and still used */
  72.         if ((to = fopen (prime_file, "a")) == NULL) {
  73.         sprintf (temp, "cannot reopen or create: %s\n", prime_file);
  74.         abortit (temp, -1);
  75.         }
  76.         fputs (s, to); fflush (to);
  77.         fclose(to);
  78.     }
  79. }
  80.  
  81. build_menu(buf,s) Buffer buf; char *s; { /* fill out prediction in s */
  82.  
  83.     int i,j,length; Buffer tbuf; char *bptr = s, c;
  84.  
  85.     if (eol_longer_mode && !(*(editor_data.dot))) {
  86.         length = max_eol
  87.                  - get_display_length(editor_data.current_buffer);
  88.         if (length < max_len) length = max_len;
  89.     } else length = max_len;
  90.  
  91.     c = first[pred_number];
  92.     for (i=0; i<=maxk; i++) tbuf[i] = buf[i];
  93.     for (i=1; i<=length; i++) {
  94.         if (show_eol_mode)
  95.             *bptr++ = c;
  96.         else if((c != '\n') || (i==1))
  97.             *bptr++ = c;
  98.         if (nl_truncate_mode && (c == '\n')) goto bp;
  99.         for (j=maxk; j>=1; j--) tbuf[j] = scan_up (tbuf[j-1],c);
  100.         c = first_pred(tbuf);
  101.         }
  102. bp:    *bptr = '\0';
  103. }
  104.  
  105.  
  106. shutdown_() {
  107.     FILE *to;
  108.     if ((to = fopen (prime_file, "a")) != NULL) {
  109.         fputs ("\07", to);  fflush (to);  fclose(to);
  110.     }
  111. }
  112.  
  113. abortit (message, status)
  114. char *message;
  115. int status;
  116. {
  117.     if(pred_on_display)
  118.     erase_pred_buffer(&editor_data);
  119.     shutdown_pty_and_tty();
  120.     shutdown_();
  121.     fprintf (stderr, "%s", message);
  122.     fflush (stdout); fflush (stderr);
  123.     exit (status);
  124. }
  125.