home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / PWBEXTEN / UREPEAT.C$ / UREPEAT
Encoding:
Text File  |  1992-01-15  |  4.6 KB  |  195 lines

  1. /* UREPEAT.C -- Emacs-style universal repeat function
  2.  *
  3.  * Copyright <C> 1992, Microsoft Corporation, All rights reserved
  4.  * All rights reserved.
  5.  *
  6.  * Demonstrates the following extension functions:
  7.  *
  8.  *  DoMessage, DoStatusBox, ReadCmd, SetKey
  9.  *
  10.  * Switches:
  11.  *
  12.  *  Unit:2            - Numeric; initial count for URepeat
  13.  *
  14.  * Functions:
  15.  *
  16.  *  NRepeat:SHIFT+F12 - Accepts arg count or textarg: repeat <n> times
  17.  *  URepeat:F12       - Prompt for function or macro to repeat
  18.  *      If function is Cancel, quit
  19.  *      else if function is Meta, toggle meta state
  20.  *      else if function is URepeat, increment count
  21.  *      else repeat the function or macro the specified number of
  22.  *          times, or until it fails (returns FALSE).
  23.  *
  24.  */
  25. #pragma warning( disable:4001 )  //Allow single-line comments
  26. #pragma warning( disable:4100 )  //Allow unused arguments
  27.  
  28. #include <stdlib.h>
  29. #include <ext.h>
  30.  
  31. static int iUnit = 2;
  32. char szURepeat[] = "URepeat";
  33. char szNRepeat[] = "NRepeat";
  34.  
  35. static buffer temp; //common buffer
  36.  
  37. void _cdecl EXTERNAL WhenLoaded( void );
  38.  
  39. PWBFUNC URepeat( unsigned int argData, ARG far * pArg, flagType fMeta );
  40. PWBFUNC NRepeat( unsigned int argData, ARG far * pArg, flagType fMeta );
  41.  
  42.  
  43. // URepeat - universal repeat prefix function
  44. //
  45. // Purpose:
  46. //  Repeat the subsequent command according to the number of
  47. //  times the universal repeat prefix is executed.
  48. //
  49. // Future Enhancement:
  50. //  Re-create current argument (arg count, selection ) for each repetition.
  51. //
  52.  
  53. PWBFUNC URepeat( unsigned int argData, ARG far * pArg, flagType fMeta )
  54. {
  55.     PCMD pcmd;
  56.     int count = iUnit;
  57.     static char Prompt[] = "URepeat:";
  58.     static char szCount[15];
  59.  
  60.     do {
  61.         farstrcpy( temp, Prompt );
  62.         itoa( count, szCount, 10 );
  63.         farstradd( temp, szCount );
  64.  
  65.         DoMessage( temp );
  66.  
  67.         pcmd = ReadCmd();
  68.         if( farstricmp( pcmd->name, "cancel" ) == 0 )
  69.         {
  70.             DoMessage( NULL );
  71.             return FALSE;
  72.         }
  73.         else
  74.         if( farstricmp( pcmd->name, "meta" ) == 0 )
  75.             fMeta = (flagType)!fMeta;
  76.         else
  77.             count <<= 1;
  78.     } while( farstricmp( pcmd->name, szURepeat ) == 0 );
  79.  
  80.     while( count-- )
  81.     {
  82.         if( fMeta ) fExecute( "Meta" );
  83.         if( !fExecute( pcmd->name ) )
  84.         {
  85.             DoMessage( NULL );
  86.             return FALSE;
  87.         }
  88.     }
  89.  
  90.     DoMessage( NULL );
  91.     return TRUE;
  92. }
  93.  
  94.  
  95. //  NRepeat - Repeat next command <n> times
  96. //
  97. // Input:
  98. //
  99. //  NOARG:   Prompt for repeat count
  100. //  NULLARG: Repeat for Arg count
  101. //  TEXTARG: Repeat for specified count
  102. //
  103. PWBFUNC NRepeat( unsigned int argData, ARG far * pArg, flagType fMeta )
  104. {
  105.     unsigned count;
  106.     static char szCount[15];
  107.     PCMD pcmd;
  108.  
  109.     switch( pArg->argType )
  110.     {
  111.     case NOARG:
  112.         farstrcpy( temp, "4" );
  113.         if( GetString( temp, "Repeat Count:", TRUE ) )
  114.             return FALSE;
  115.         count = atou( temp );
  116.         break;
  117.  
  118.     case NULLARG:
  119.         count = pArg->arg.nullarg.cArg;
  120.         break;
  121.  
  122.     case TEXTARG:
  123.         count = atou( pArg->arg.textarg.pText );
  124.         if( count == 0 )
  125.             return BadArg();
  126.         break;
  127.  
  128.     default:
  129.         return BadArg();
  130.         break;
  131.     }
  132.     for(;;)
  133.     {
  134.         farstrcpy( temp, "NRepeat:" );
  135.         itoa( count, szCount, 10 );
  136.         farstradd( temp, szCount );
  137.         DoMessage( temp );
  138.  
  139.         pcmd = ReadCmd();
  140.         if( farstricmp( pcmd->name, "cancel" ) == 0 )
  141.         {
  142.             DoMessage( NULL );
  143.             return FALSE;
  144.         }
  145.         else
  146.         if( farstricmp( pcmd->name, "meta" ) == 0 )
  147.             fMeta = (flagType)!fMeta;
  148.         else
  149.             break;
  150.     }
  151.  
  152.     while( count-- )
  153.     {
  154.         if( fMeta ) fExecute( "Meta" );
  155.         if( !fExecute( pcmd->name ) )
  156.         {
  157.             DoMessage( NULL );
  158.             return FALSE;
  159.         }
  160.     }
  161.     return TRUE;
  162.  
  163. }
  164.  
  165. //-------------------< Standard Extension Information >--------------------
  166.  
  167. void _cdecl EXTERNAL WhenLoaded( void )
  168. {
  169.     DoStatusBox( "Universal Repeat Extension", NULL );
  170.  
  171.     SetKey( szURepeat, "F12" );
  172.     SetKey( szNRepeat, "SHIFT+F12" );
  173.  
  174.     DoStatusBox( NULL, NULL );
  175. }
  176.  
  177. //
  178. // Command description table.
  179. //
  180. struct cmdDesc  cmdTable[] =
  181. {
  182.     {szURepeat, URepeat, 0, CURSORFUNC|KEEPMETA|ICONFOCUS|NOWINDOWS },
  183.     {szNRepeat, NRepeat, 0, NOARG|NULLARG|TEXTARG|ICONFOCUS|NOWINDOWS },
  184.     { NULL, NULL, 0, 0 }
  185. };
  186.  
  187. //
  188. // Switch description table.
  189. //
  190. struct swiDesc swiTable[] =
  191. {
  192.     { "Unit", toPIF(iUnit), SWI_NUMERIC|RADIX10 },
  193.     { NULL, NULL, 0 }
  194. };
  195.