home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / Evatac Software / Preditor 3.0 / Tools / Extension Module Builder / Examples / Smart Quotes / SmartQuotes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-28  |  2.1 KB  |  123 lines  |  [TEXT/TCEd]

  1. /************************************************************
  2.  
  3.     SmartQuotes.c
  4.     
  5.     Preditor 3.0 extension.
  6.     
  7.     Replace quotes with smart quotes appropriately.
  8.  
  9.     © Copyright Evatac Software  1988-1995
  10.     All rights reserved
  11.  
  12. ************************************************************/
  13.  
  14. #include "PreditorExtension.h"
  15.  
  16. #include <SetupA4.h>
  17. #ifndef THINKC
  18. #include <A4Stuff.h>
  19. #else
  20. #define SetCurrentA4()    0; RememberA4()
  21. #define SetA4(x)        SetUpA4()
  22. #endif
  23.  
  24. #ifdef powerc
  25. ProcInfoType __procinfo = ExtensionUPPInfo;
  26. #endif
  27.  
  28. static Boolean startNewQuote[256];
  29.  
  30. /*
  31.  * quoteInitStarts
  32.  */
  33. static void quoteInitStarts(void)
  34. {
  35.     short i;
  36.     
  37.     for (i = 0; i < 256; i++)
  38.         startNewQuote[i] = false;
  39.         
  40.     startNewQuote[' ']         = true;
  41.     startNewQuote['(']         = true;
  42.     startNewQuote['{']         = true;
  43.     startNewQuote['[']        = true;
  44.     startNewQuote['\t']     = true;
  45.     startNewQuote['\r']     = true;
  46.     startNewQuote[0xca]     = true;
  47.     startNewQuote[0xd2]     = true;
  48.     startNewQuote[0xd4]     = true;
  49. }
  50.  
  51. void main(
  52.     ExternalCallbackBlock        *callbacks,
  53.     WindowRef                    window
  54.     )
  55. {
  56.     long                     saved_a4;
  57.     Handle                     h;
  58.     long                     i, len;
  59.     Boolean                 changed = FALSE;
  60.     unsigned char             wasc, c, prevc;
  61.  
  62.     saved_a4 = SetCurrentA4();
  63.     
  64.     quoteInitStarts();
  65.     
  66.     extScanContents(callbacks, 0);
  67.     
  68.     len = extCharacterCount(callbacks);
  69.  
  70. #ifdef DO_PROGRESS
  71.     extStartProgress(callbacks, (char *) "\pConverting Quotes...", len, true);
  72. #endif
  73.  
  74.     for (i=0; i<len; i++) {
  75.         
  76. #ifdef DO_PROGRESS
  77.         if (extDoProgress(callbacks, i+1))
  78.             break;
  79. #endif
  80.         
  81.         if (!extNextScanCharacter(callbacks, &c))
  82.             break;
  83.         
  84.         wasc = c;
  85.  
  86.         if ((i == 0) || startNewQuote[prevc])
  87.         {
  88.             if (c == '"')
  89.                 c = '“';
  90.             else if (c == '\'')
  91.                 c = '‘';
  92.         }
  93.         else
  94.         {
  95.             if (c == '"')
  96.                 c = '”';
  97.             else if (c == '\'')
  98.                 c = '’';
  99.         }
  100.         
  101.         if (c != wasc)
  102.         {
  103.             extSetSelection(callbacks, i, i+1);
  104.             extInsert(callbacks, &c, 1);
  105.         }
  106.         
  107.         prevc = c;
  108.     }
  109.     
  110.     extDoneScan(callbacks);
  111.  
  112.     /*
  113.       * Note: undo is handled automatically, since we want to 
  114.       * consider all quote replacements as "one" operation.
  115.      */
  116.  
  117. #ifdef DO_PROGRESS
  118.     extDoneProgress(callbacks);
  119. #endif
  120.  
  121.     SetA4(saved_a4);
  122. }
  123.