home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcoral16.zip / HANDLE_K.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  11KB  |  522 lines

  1. /*
  2. ** Copyright 1989, 1992 by Lionel Fournigault
  3. **
  4. ** Permission to use, copy, and distribute for non-commercial purposes,
  5. ** is hereby granted without fee, providing that the above copyright
  6. ** notice appear in all copies and that both the copyright notice and this
  7. ** permission notice appear in supporting documentation.
  8. ** The software may be modified for your own purposes, but modified versions
  9. ** may not be distributed.
  10. ** This software is provided "as is" without any expressed or implied warranty.
  11. **
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Xlib.h>
  17. #include <X11/cursorfont.h>
  18. #include <X11/Xutil.h>
  19. #include <X11/keysym.h>
  20. #include <ctype.h>
  21. #include <strings.h>
  22.  
  23. #include "xcoral.h"
  24. #include "flist.h"
  25.  
  26. extern EdWin *edwin;
  27. extern Display *dpy;
  28.  
  29. extern Trans st_initial[], st_control_x[], st_escape[];
  30. static int    f_ascii (), f_nothing (), f_controle (), f_escape (), f_ctr (),
  31.     f_special (), f_ctrx ();
  32.  
  33. Trans st_initial [8] = {
  34.     { KEY, f_ascii, (int) st_initial },
  35.     { CONTROL, f_nothing, (int) st_initial },
  36.     { CONTROL_AND_KEY, f_ctr, (int) st_initial },
  37.     { CONTROL_AND_X, f_nothing, (int) st_control_x },
  38.     { ESCAPE, f_escape, (int) st_escape },
  39.     { ARROW, f_ascii, (int) st_initial },
  40.     { SPECIAL, f_special, (int) st_initial },
  41.     { NULL, NULL, NULL }
  42. };
  43.  
  44. Trans st_control_x [8] = {
  45.     { KEY, f_ctrx, (int) st_initial },
  46.     { CONTROL, f_nothing, (int) st_control_x },
  47.     { CONTROL_AND_KEY, f_ctrx, (int) st_initial },
  48.     { CONTROL_AND_X, f_ctrx, (int) st_initial },
  49.     { ESCAPE, f_escape, (int) st_escape },
  50.     { ARROW, f_ascii, (int) st_initial },
  51.     { SPECIAL, f_special, (int) st_control_x },
  52.     { NULL, NULL, NULL }
  53. };
  54.  
  55. Trans st_escape [8] = {
  56.     { KEY, f_escape, (int) st_initial },
  57.     { CONTROL, f_nothing, (int) st_initial },
  58.     { ESCAPE, f_escape, (int) st_initial },
  59.     { ARROW, f_ascii, (int) st_escape },
  60.     { SPECIAL, f_special, (int) st_escape },
  61.     { NULL, NULL, NULL },
  62.     { NULL, NULL, NULL },
  63.     { NULL, NULL, NULL }
  64. };
  65.  
  66. static int     num;        /* Pour les repetitions        */
  67. static char     ns [32];        
  68. static int     last_stat;    /* Pour les sequences escapes    */
  69. static deletewindow = False;
  70.  
  71. static KeySym    ksym;
  72. XComposeStatus    compose;
  73. static int     n_bytes;
  74.  
  75. static void GetInfosKey (), GetInfosAsciiKey (), GetDigit ();
  76.  
  77. /*
  78. **    Function name : automate
  79. **
  80. **    Description : Un petit automate, pour s'amuser...
  81. **        Il a trois etats possibles :
  82. **        - initial ( etat par default )
  83. **        - controle_x ( sequence Ctr X ... )
  84. **        - escape ( Avec en plus le "repeat" )
  85. **    Input : 
  86. **    Ouput :
  87. */
  88. ST *automate ( event, current_st )
  89.     XKeyEvent *event;
  90.     ST *current_st;
  91. {
  92.     char buf [32];
  93.     register int i;
  94.     InfosKey infos;
  95.        
  96.        bzero (buf, 32);
  97.     n_bytes = XLookupString ( event, buf, 32, &ksym, &compose );
  98. #ifdef DEBUG
  99.     printf ( "n_bytes = %d %X\n", n_bytes, ksym );
  100. #endif DEBUG
  101.     (void) GetInfosKey ( ksym, buf [0], &infos );
  102.  
  103.     deletewindow = False;
  104.  
  105.     for ( i=0; current_st -> trans [i].type != NULL; i++ ){
  106.         if ( current_st -> trans [i].type == infos.type ) {
  107.             if ( current_st -> trans [i].fnt == 0 || current_st -> trans [i].dest_stat == 0 ) {
  108.                 (void) fprintf ( stderr, "Error trans = %d\n", i );
  109.                          if ( deletewindow == False )
  110.                     return ( (ST *) current_st );
  111.                          else
  112.                              return ( (ST *) - 1 );    
  113.             }
  114.             current_st -> trans [i].fnt ( &infos );
  115.                   if ( deletewindow == False )
  116.                 return ( ( ST * ) current_st -> trans [i].dest_stat );
  117.                   else
  118.                       return ( (ST *) - 1 );    
  119.         }
  120.     }
  121.     (void) fprintf ( stderr, "Automate error\n" );
  122.     return ( (ST *) st_initial );
  123. }
  124.  
  125.  
  126. /*
  127. **    Function name : GetInfosKey
  128. **
  129. **    Description : GetInfosKey remplie la structure InfosKey
  130. **        en fonction du symbol relatif a l'evenement
  131. **        touche enfoncee.
  132. **        On cherche a savoir dans un premier temps si la
  133. **        touche est de type controle, escape, ou fleche.
  134. **    Input : Le symbole, le caractere, les infos.
  135. **    Ouput :
  136. */
  137. static void GetInfosKey ( ksym, c, infos )
  138.     KeySym        ksym;
  139.     char         c;
  140.     InfosKey     *infos;
  141. {
  142.     switch ( ksym ) {
  143.         case XK_Control_L :
  144.         case XK_Control_R :
  145.             infos -> type = CONTROL;
  146.             infos -> ch = NULL;
  147.             ClearMessageWindow ( edwin -> mwin );
  148.             break;
  149.         case XK_Escape     :
  150.         case XK_Meta_L    :
  151.         case XK_Meta_R    :
  152.             infos -> type = ESCAPE;
  153.             infos -> ch = c;
  154.             ClearMessageWindow ( edwin -> mwin );
  155.             break;
  156.         case XK_Right :
  157.                 infos -> ch = 'f';
  158.             infos -> type = ARROW;
  159.             break;
  160.         case XK_Left     :
  161.             infos -> ch = 'b';
  162.             infos -> type = ARROW;
  163.             break;
  164.         case XK_Up    :
  165.                 infos -> ch = 'p';
  166.             infos -> type = ARROW;
  167.                 break;
  168.         case XK_Down    :
  169.             infos -> ch = 'n';
  170.             infos -> type = ARROW;
  171.             break;
  172.         default:
  173.             (void) GetInfosAsciiKey ( c, infos );
  174.             break;
  175.     }
  176. }
  177.  
  178. /*
  179. **    Function name : GetInfosAsciiKey
  180. **
  181. **    Description : GetInfosAsciiKey remplie la structure InfosKey
  182. **        dans les cas suivants:
  183. **        - Sequence Controle X
  184. **        - Sequence Controle + Key
  185. **        - Return, tab, delete, backspace space
  186. **        - Caractere imprimable.
  187. **    Input : Le symbole, le caractere et les infos.
  188. **    Ouput :
  189. */
  190. static void GetInfosAsciiKey ( c, infos )
  191.     char         c;
  192.     InfosKey    *infos;
  193. {
  194.  
  195.     if ( c >= NULL && c <= CtrZ && n_bytes != NULL ) {
  196.         switch ( c ) {
  197.             case CtrX    :
  198.                 infos -> type = CONTROL_AND_X;
  199.                 break;
  200.             case RETURN     :
  201.             case LINEFEED    :
  202.             case TAB     :
  203.             case BACKSPACE    :
  204.                     infos -> type = KEY;
  205.                 break;
  206.             default:
  207.                 infos -> type = CONTROL_AND_KEY;
  208.                 break;
  209.         }
  210.         ClearMessageWindow ( edwin -> mwin );
  211.         infos -> ch = c;
  212.         return;
  213.     }
  214.     else if ( last_stat == REPEAT ) {
  215.         if ( isdigit ( c ))
  216.             infos -> type = ESCAPE;
  217.         else {
  218.             last_stat = NULL;
  219.             infos -> type = KEY;
  220.             ClearMessageWindow ( edwin -> mwin );
  221.         }
  222.         infos -> ch = c;
  223.     }
  224.     else if ( n_bytes == 1 ) {
  225.             infos -> ch = c;
  226.         infos -> type = KEY;
  227.     }
  228.     else {
  229.         infos -> type = SPECIAL; infos -> ch = NULL;
  230.     }
  231. }
  232.  
  233.  
  234. /*
  235. **    Function name : f_ascii
  236. **
  237. **    Description : Traitement des caracteres ascii
  238. **    Input : Le infos.
  239. **    Ouput :
  240. */
  241. static f_ascii ( infos )
  242.     InfosKey *infos;    
  243. {
  244.     if ( infos -> type == ARROW ) {
  245.            TextCursorOff ( edwin -> text );
  246.            switch ( infos -> ch ) {
  247.            case 'n':
  248.          DownCursor ( edwin -> text );
  249.          break;
  250.            case 'p':
  251.          UpCursor ( edwin -> text );
  252.          break;
  253.            case 'f':
  254.          (void) ForwardChar ( edwin -> text );
  255.          break;
  256.            case 'b':
  257.          BackwardChar ( edwin -> text );
  258.          break;
  259.            }
  260.            TextCursorOn ( edwin -> text );
  261.            return;
  262.     }
  263.     
  264.     switch ( infos -> ch ) {
  265.         case TAB :
  266.                f_tab ( edwin -> text );
  267.                break;
  268.         case DELETE:
  269.         case BACKSPACE:
  270.                f_delete ( edwin -> text );
  271.                break;
  272.         case LINEFEED:
  273.         case RETURN:
  274.             f_return ( edwin -> text );
  275.                break;
  276.                 default :
  277.                if ( isprint ( infos -> ch ) )
  278.                       f_impc ( edwin -> text, infos -> ch );
  279.     }
  280. }
  281.  
  282.  
  283. /*
  284. **    Function name : f_ctrx
  285. **
  286. **    Description : Traitement des sequences ^X ^key
  287. **    Input : Les infos. 
  288. **    Ouput :
  289. */
  290. static int f_ctrx ( infos )
  291.     InfosKey *infos;
  292. {
  293.     register int n;
  294.  
  295.     switch ( infos -> ch ) {
  296.         case CtrC :
  297.                   if ( DeleteWindow ( edwin -> text ) == 0 )
  298.                 deletewindow = True;    
  299.             break;
  300.         case CtrS :
  301.             KbdSaveFile ( edwin -> text );
  302.             break;
  303.         case CtrF :
  304.             KbdReadFile ( edwin -> text );
  305.             break;
  306.         case CtrW :
  307.             KbdWriteFile ( edwin -> text );
  308.             break;
  309.         case CtrX:
  310.             ExchangePointMark ( edwin -> text );
  311.             break;
  312.         case 'b':
  313.             DisplayOpenFiles ( edwin -> text );
  314.             break;
  315.         case 'k':
  316.             TextCursorOff ( edwin -> text );
  317.             KillCurrentBuffer ( edwin -> text );
  318.             TextCursorOn ( edwin -> text );
  319.             break;
  320.         case 'l':
  321.             TextCursorOff ( edwin -> text );
  322.             if ( (n = atoi ( (char *) GetString ( edwin -> text, "Goto Line : ", (char *) 0 ))) != 0 )  {
  323.                 GotoLineNumber ( edwin -> text, n );
  324.                 CurrentLineToMiddle ( edwin -> text ); 
  325.             }
  326.             else
  327.                 DisplayMessage ( edwin -> text -> mwin, "Abort" );
  328.             TextCursorOn ( edwin -> text );
  329.             num = 0;
  330.             break;
  331.         case 'i':
  332.             KbdInsertFile ( edwin -> text );
  333.             break;
  334.  
  335.         case '2':
  336.             NewWindow ( edwin -> text );
  337.             break;
  338.         default :
  339.             break;
  340.     }
  341. }
  342.  
  343.  
  344. /*
  345. **    Function name : f_ctr
  346. **
  347. **    Description : Traitement des sequences ^key
  348. **    Input : Les infos.
  349. **    Ouput :
  350. */
  351. static int f_ctr ( infos )
  352.     InfosKey *infos;
  353. {
  354.     TextCursorOff ( edwin -> text );    
  355.  
  356.     switch ( infos -> ch ) {
  357.         case CtrA:
  358.             MoveToBline ( edwin ->text );
  359.             break;
  360.         case CtrB:
  361.             BackwardChar ( edwin -> text );
  362.             break;
  363.         case CtrD :
  364.             Control_D ( edwin -> text );
  365.             break;
  366.         case CtrE:
  367.             MoveToEline ( edwin -> text );
  368.             break;
  369.         case CtrF:
  370.             (void) ForwardChar ( edwin -> text );
  371.             break;
  372.         case CtrG:
  373.             DisplayMessage ( edwin -> mwin, "Abort" );
  374.             num = 0;
  375.             if ( last_stat == REPEAT )
  376.                 last_stat = NULL;
  377.             ResetSearchString ();
  378.             break;
  379.         case CtrK:
  380.             if ( num == 0 ) num++;
  381.             Control_K ( edwin -> text, num );
  382.             num = 0;
  383.             break;
  384.         case CtrL:
  385.             ClipOn ( edwin -> text, NULL );
  386.             RefreshPage ( edwin -> text );
  387.             ClipOff ( edwin -> text );
  388.             break;
  389.         case CtrN:
  390.             DownCursor ( edwin -> text );
  391.             break;
  392.         case CtrO:
  393.             f_return ( edwin -> text );
  394.             TextCursorOff ( edwin -> text );            
  395.             BackwardChar ( edwin -> text );
  396.             break;
  397.         case CtrP:
  398.             UpCursor (  edwin -> text );
  399.             break;
  400.         case CtrR:
  401.             BackwardSearch ( edwin -> text );
  402.             break;
  403.         case CtrS:
  404.             ForwardSearch ( edwin -> text );
  405.             break;
  406.         case CtrV:
  407.             NextPage ( edwin -> text );
  408.             break;
  409.         case CtrW:
  410.             KillRegion ( edwin -> text );
  411.             break;
  412.                     case CtrY:
  413.             if ( num == 0 )
  414.                    Control_Y ( edwin -> text, 0 );
  415.             else 
  416.                    Control_Y ( edwin -> text, num-1 );
  417.             num = 0;
  418.             break;
  419.         case Ctr_sp:
  420.             SetMark ( edwin -> text );
  421.             break;
  422.         default :
  423.             ;
  424.     }
  425.     TextCursorOn ( edwin -> text );
  426. }
  427.  
  428.  
  429. /*
  430. **    Function name : f_escape
  431. **
  432. **    Description : Traitement des sequences escape.
  433. **    Input : 
  434. **    Ouput :
  435. */
  436. static int f_escape ( infos )
  437.     InfosKey    *infos;
  438. {
  439.     TextCursorOff ( edwin -> text );    
  440.  
  441.     switch ( infos -> ch ) {
  442.         case '<':
  443.             FirstPage ( edwin -> text );
  444.             break;
  445.         case '>' :
  446.             GotoEnd ( edwin -> text );
  447.             CurrentLineToMiddle ( edwin -> text );
  448.             break;
  449.         case 'v' :
  450.             PreviousPage ( edwin -> text );
  451.             break;
  452.         case 'r':
  453.             GlobalReplace ( edwin -> text );
  454.             break;
  455.         case 'q':
  456.             QueryReplace ( edwin -> text );
  457.             break;
  458.         case 'w':
  459.             CopyRegion ( edwin -> text );
  460.             break;
  461.         default  :
  462.             GetDigit ( infos );
  463.             break;
  464.     }
  465.  
  466.     TextCursorOn ( edwin -> text );
  467. }
  468.  
  469. /*
  470. **    Function name : GetDigit
  471. **
  472. **    Description : Traitement la sequence escape num
  473. **    Input : Les infos.
  474. **    Ouput :
  475. */
  476. static void GetDigit ( infos )
  477.     InfosKey *infos;
  478. {
  479.     char str [32];
  480.     char tmp[2];
  481.  
  482.     if ( isdigit ( infos -> ch )) {
  483.         tmp [0] = infos -> ch; tmp [1] = NULL;
  484.         (void) strcat  ( ns, tmp );
  485.  
  486.         if ( atoi(ns) > MAXREPEAT ) {
  487.             DisplayMessage ( edwin -> mwin, "Too much...bye" );
  488.             last_stat = NULL;
  489.             ns [0] = num = NULL;
  490.             return;
  491.         }
  492.  
  493.         if ( last_stat == REPEAT )
  494.             (void) sprintf ( str,"Repeat : %d%c", num, infos -> ch );
  495.         else {
  496.             (void) sprintf ( str,"Repeat : %c", infos -> ch );
  497.             last_stat = REPEAT;
  498.         }
  499.         DisplayMessage ( edwin -> mwin,  str );
  500.         num = atoi (ns);    
  501.     }
  502.     else {
  503.         last_stat = NULL;
  504.         ns [0] = num = NULL;
  505.         if ( isprint ( infos -> ch ) && infos -> ch != ESCAPE ) {
  506.             f_impc ( edwin -> text, infos -> ch );
  507.             ClearMessageWindow ( edwin -> mwin);
  508.         }
  509.     }
  510. }
  511.  
  512. static int f_nothing (){}
  513. static int f_special ( infos, edwin )
  514.     InfosKey *infos;
  515.     EdWin    *edwin;
  516. {
  517. #ifdef lint
  518.     (void) fprintf ( stderr, "dpy = %d infos = %d edwin = %d\n",
  519.         (int) dpy, (int) infos, (int) edwin );
  520. #endif lint
  521. }
  522.