home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part04 / user.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  6.9 KB  |  299 lines

  1. /*
  2.  * User status and command window.
  3.  *
  4.  * Robert W. Baldwin, December 1984.
  5.  *
  6.  * Bob Baldwin 10/86.
  7.  *  Formatting changes.
  8.  *  Change usrdown to fix screen fall off bug.
  9.  *  Change usrfirst to fix cursor pos so jumpcmd() works.
  10.  */
  11.  
  12.  
  13. #include    <stdio.h>
  14. #include    "window.h"
  15. #include    "terminal.h"
  16. #include    "layout.h"
  17. #include    "specs.h"
  18. #include    "parser.h"
  19.  
  20.  
  21. #define    DONEMSG     "Command completed."
  22. #define    USRHLP    1        /* Offset for help line. */
  23. #define    USRSTAT    2        /* Offset for status line. */
  24. #define    USRCMD    3        /* Offset for command line. */
  25.  
  26.  
  27. /* Global space to build status messages. */
  28. char    statmsg[MAXWIDTH+1];
  29.  
  30.  
  31. extern    int    usrfirst();    /* Defined below. */
  32. extern    int    usrdokey();    /* Defined below. */
  33. extern    int    usrdraw();    /* Defined below. */
  34. extern    int    usrup();    /* Defined below. */
  35. extern    int    usrdown();    /* Defined below. */
  36.  
  37.  
  38. /* Command table. */
  39. #define USRHTEXT \
  40. "bigram, knit, prop, load, save, lookup, clear, equiv, auto-tri, pword"
  41.  
  42. cmdent    usrcmdtab[] = {
  43.         {"quit-program permanently", quitcmd},
  44. {"auto-trigram max_dev: % min_total_chars: % min_wire_chars: %", atrguess},
  45.         {"knitting using blocks from: % to: %  Min show count: %", kntguess},
  46.         {"lookup-pattern: % in dictionary", webmatch},
  47.         {"equivalence-class guess, use accept level: % (try 2.0)", ecbguess},
  48.         {"pwords-from file: %  Max dev: % (try 1.0)", pwdguess},
  49.         {"load-permutations", permload},
  50.         {"save-permutations", permsave},
  51.         {"clear-zee permutation", clearzee},
  52.         {"propagate-info from: % to: % using Zee", pgate},
  53.         {"bigram-guess level: % (2.0), min_prob: % (0.15)", lpbguess},
  54.         {0, NULL},
  55.         };
  56.  
  57.  
  58. /* Keystroke table for the user area. */
  59. extern    usrdocmd();
  60.  
  61. /* Keystroke table for the whole user window.
  62.  * The editing on the command line is handled by a sub-window.
  63.  */
  64. keyer    usrktab[] = {
  65.         {CGO_UP, usrup},    /* Keep off help and status lines. */
  66.         {CGO_DOWN, usrdown},    /* Normal behavior ok. */
  67.         {CEXECUTE, usrdocmd},    /* Interprete a command. */
  68.         {0, NULL},        /* Handle other chars elsewhere. */
  69.         };
  70.  
  71. /* Keystroke table for the command line sub-window.
  72.  */
  73. keyer    cmdktab[] = {
  74.                 {CNEXTARG, wl_nxtarg},
  75.         {CGO_LEFT, wl_dlleft},    /* Stay withing variable area. */
  76.         {CGO_RIGHT, wl_dlright},/* Stay withing variable area. */
  77.         {CDELF, wl_dlfdel},    /* Delete forward within var area. */
  78.         {CDELB, wl_dlbdel},    /* Delete backward within var area. */
  79.         {CCLRLINE, wl_dlclr},    /* Clear variable area. */
  80.         {CINSERT, wl_dlinsert},    /* All other chars self-insert. */
  81.         };
  82.  
  83.  
  84. /* Window for the user commands and program status. */
  85.  
  86. displine usraline[USRHEIGHT];        /* Display lines for the user. */
  87. displine *usrlines[USRHEIGHT+1];    /* Pointers to them plus NULL. */
  88.  
  89. twindow    user = {
  90.         USRROW,1,        /* Origin. */
  91.         USRHEIGHT, USRWIDTH,    /* Height and width. */
  92.         USRCMD+1, USRSCOL,    /* Initial (relative) cursor pos. */
  93.         NULL,            /* No private data. */
  94.         usrfirst,        /* Firstime = restore cursor pos. */
  95.         wl_noop,        /* Lasttime = do nothing. */
  96.         usrdraw,        /* Default draw routine. */
  97.         usrdokey,        /* Custom keystroke handler. */
  98.         usrktab,        /* Keystroke table. */
  99.         usrlines,
  100. };
  101.  
  102.  
  103.  
  104. /* Display a string in the status area.
  105.  * If the string is empty, this will clear the status area.
  106.  * Put the cursor back where it was.
  107.  *
  108.  * The empty string is special case.  If the status line is
  109.  * empty, then wcur_col = 1.  A second request to clear
  110.  * the line is ignored.
  111.  * If the line is not empty, wcur_col = dl_min_col.
  112.  */
  113. usrstatus(w, str)
  114. twindow    *w;
  115. char    *str;
  116. {
  117.     displine *line;
  118.     int     row, col;
  119.     
  120.     row = rowcursor();
  121.     col = colcursor();
  122.  
  123.     line = w->dlines[USRSTAT];
  124.     if (*str == 0  &&  line->wcur_col == 1)
  125.           return;
  126.  
  127.     dlsetvar(line, str);
  128.     (*(line->wredraw))(line);
  129.     setcursor(row, col);
  130.  
  131.     line->wcur_col = (*str == 0) ? 1 : 2;
  132. }
  133.  
  134.  
  135.  
  136. /* Display a string in the help area.
  137.  * If the string is empty, this will clear the help area.
  138.  * Put the cursor back where it was.
  139.  */
  140. usrhelp(w, str)
  141. twindow    *w;
  142. char    *str;
  143. {
  144.     displine *line;
  145.     int     row, col;
  146.     
  147.     row = rowcursor();
  148.     col = colcursor();
  149.  
  150.     line = w->dlines[USRHLP];
  151.     dlsetvar(line, str);
  152.     (*(line->wredraw))(line);
  153.  
  154.     setcursor(row, col);
  155. }
  156.  
  157.  
  158.  
  159. /* Draw the user area.
  160.  * Leaves the cursor on the command line.
  161.  */
  162. usrdraw(w)
  163. twindow    *w;
  164. {
  165.     wl_twdraw(w);
  166. }
  167.  
  168.  
  169.  
  170. /* Make a window with a title and partial outline.
  171.  */
  172. gwindow *(iuser())
  173. {
  174.     int     i;
  175.     displine *line, *lines;
  176.     twindow     *w;
  177.  
  178.     w = &user;
  179.     lines = usraline;
  180.     for (i = 0 ; i < w->wheight ; i++)  {
  181.         line = &lines[i];
  182.         line->worg_row = w->worg_row + i;
  183.         line->worg_col = w->worg_col;
  184.         line->wheight = 1;
  185. /* Fix bug on terminals that autowrap. */
  186.         line->wwidth = (i == USRCMD) ? w->wwidth - 1 : w->wwidth;
  187.         line->wcur_row = 1;
  188.         line->wcur_col = USRSCOL;
  189.         line->wfirst = wl_rcursor;
  190.         line->wlast = wl_noop;
  191.         line->wredraw = wl_dldraw;
  192.         line->wkey = dokey;
  193.         line->wkeyprocs = arwktab;
  194.         line->dl_min_col = USRSCOL;
  195.         line->dl_max_col = line->wwidth;
  196.         clrdline(line);
  197.         w->dlines[i] = line;
  198.         }
  199.     w->dlines[i] = NULL;
  200.  
  201.     line = &lines[USRHLP];
  202.     setadline(line, "Help   : This feature not implemented");
  203.     line = &lines[USRSTAT];
  204.     setadline(line, "Status : ");
  205.     dlsetvar(line, "Just barely working");
  206.     line = &lines[USRCMD];
  207.     setadline(line, "Command: ");
  208.     line->wkeyprocs = cmdktab;
  209.  
  210.     return((gwindow *) w);
  211. }
  212.  
  213.  
  214. /* Behavior of the up arrow key.
  215.  * Move up to the window above us staying in the same column.
  216.  */
  217. usrup(w, k)
  218. twindow    *w;        /* The user window. */
  219. key    k;
  220. {
  221.     wl_setcur(w, 1, w->dlines[USRCMD]->wcur_col);
  222.     jogup(w, k);
  223. }
  224.  
  225.  
  226. /* Behavior of the down arrow key.
  227.  * Move to command line.
  228.  */
  229. usrdown(w, k)
  230. twindow    *w;        /* The user window. */
  231. key    k;
  232. {
  233.     wl_setcur(w, USRHEIGHT, w->dlines[USRCMD]->wcur_col);
  234. }
  235.  
  236.  
  237. /* Behavior when cursor first enters the user window.
  238.  * Move to the command line.
  239.  */
  240. usrfirst(w, row, col)
  241. twindow    *w;
  242. int    row, col;    /* Place in window when cursor currently is. */
  243. {
  244.     displine *cmdline;
  245.  
  246.     wl_setcur(w, USRHEIGHT, w->dlines[USRCMD]->wcur_col);
  247.     usrhelp(w, USRHTEXT);
  248.     cmdline = w->dlines[USRCMD];
  249.     (*(cmdline->wfirst))(cmdline, 1, col);
  250. }
  251.  
  252.  
  253. /* Keystroke handler for user window.
  254.  * If it is not an up-arrow or down-arrow, pass it to the command line.
  255.  */
  256. usrdokey(w, k)
  257. twindow    *w;        /* The user window. */
  258. key    k;
  259. {
  260.     char     *expanded;    /* Expanded command line. */
  261.     char     cmdbuf[MAXWIDTH+1];
  262.     displine *cmdline;
  263.  
  264.     cmdline = w->dlines[USRCMD];
  265.     usrstatus(&user, "");
  266.     if (ddokey(w, k, w->wkeyprocs))  {  /* If handled by top window. */
  267.         return;                /* Includes doit key. */
  268.     }
  269.  
  270.     if (k == ((CINSERT << CMDSHIFT) | SPACE))  {
  271.         dlgetvar(cmdline, cmdbuf);
  272.         expanded = cmdcomplete(usrcmdtab, cmdbuf);
  273.         if (expanded != NULL)  {
  274.             dlsetvar(cmdline, expanded);
  275.             wl_dldraw(cmdline);
  276.             return;
  277.             }
  278.         }
  279.     (*(cmdline->wkey))(cmdline, k);    /* Else pass to sub-window. */
  280. }
  281.  
  282.  
  283. /* Interprete a command.
  284.  */
  285. usrdocmd(usr)
  286. twindow    *usr;
  287. {
  288.     char     cmdbuf[MAXWIDTH+1];
  289.     char     *errmsg;
  290.     displine *cmdline;
  291.  
  292.     cmdline = usr->dlines[USRCMD];
  293.     wl_setcur(usr, usr->wcur_row, cmdline->wcur_col);
  294.     dlgetvar(cmdline, cmdbuf);
  295.     if ((errmsg = cmddo(usrcmdtab, cmdbuf)) == NULL)
  296.         errmsg = DONEMSG;
  297.     usrstatus(&user, errmsg);
  298. }
  299.