home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 67 / IOPROG_67A.ISO / soft / Tools / mwsppv4.exe / ONREPLACE.SCRIPT < prev    next >
Encoding:
Text File  |  2002-11-24  |  3.5 KB  |  117 lines

  1. !!Script
  2. // Copyright ⌐ 2002 - Modelworks Software
  3.  
  4. /**
  5. @Event: onReplace
  6. @Summary: onReplace~called to implement smart replace.
  7. */
  8.  
  9. var gFindParameters = getGlobal("FindParameters", null);
  10. var gOutput = getOutput();
  11.  
  12. function OnEvent(editor, found, replace)
  13. {
  14.     //gOutput.writeLine("----------------------");
  15.     //gOutput.writeLine("found " + found);
  16.     //gOutput.writeLine("replace " + replace);
  17.     if (found.length > 0 && replace.length > 0 && !gFindParameters.caseSensitive && !gFindParameters.regularExpression)
  18.     {
  19.         var firstReplaceWord = GetFirstSubword(replace);
  20.         var firstFoundWord = GetFirstSubword(found);
  21.         
  22.         //gOutput.writeLine("firstReplaceWord " + firstReplaceWord);
  23.         //gOutput.writeLine("firstFoundWord " + firstFoundWord);
  24.         
  25.         if (firstReplaceWord.length > 0 && firstFoundWord.length > 0)
  26.         {
  27.             var replaceWordIsLower = IsLower(firstReplaceWord.charAt(0));
  28.             var foundWordIsLower = IsLower(firstFoundWord.charAt(0));
  29.             
  30.             if (replaceWordIsLower != foundWordIsLower)
  31.             {
  32.                 if (foundWordIsLower)
  33.                 {
  34.                     // camel case
  35.                     var newReplace = firstReplaceWord.toLowerCase() +
  36.                         replace.substring(firstReplaceWord.length, replace.length);
  37.                     //gOutput.writeLine("camel newReplace " + newReplace);
  38.                     return newReplace;
  39.                 }
  40.                 else
  41.                 {
  42.                     if (IsLower(firstFoundWord.charAt(1)))
  43.                     {
  44.                         // Pascal case
  45.                         var newReplace = firstReplaceWord.charAt(0);
  46.                         newReplace = newReplace.toUpperCase();
  47.                         newReplace +=replace.substring(1, replace.length);
  48.                         //gOutput.writeLine("Pascal newReplace " + newReplace);
  49.                         return newReplace;
  50.                     }
  51.                     else
  52.                     {
  53.                         // ACKRONIMN case
  54.                         var newReplace = firstReplaceWord.toUpperCase() +
  55.                             replace.substring(firstReplaceWord.length, replace.length);
  56.                         //gOutput.writeLine("ACKRONIMN newReplace " + newReplace);
  57.                         return newReplace;
  58.                     }
  59.                 }
  60.             }
  61.             //gOutput.writeLine("No change  " + replace);
  62.         }
  63.     }
  64.     return null; // if no change
  65. }
  66.  
  67. function GetFirstSubword(word)
  68. {
  69.     var code = word.charAt(0);
  70.     if (IsLower(code))
  71.     {
  72.         // camel casing
  73.         var i = 0;
  74.         do
  75.         {
  76.             code = word.charAt(++i);
  77.         } while (IsLower(code) && i < word.length);
  78.         
  79.         return word.substring(0, i);
  80.     }
  81.     else
  82.     {
  83.         var i = 0;
  84.         do
  85.         {
  86.             code = word.charAt(++i);
  87.         } while (IsUpper(code)&& i < word.length);
  88.         
  89.         if (i == 1)
  90.         {
  91.             // Pascal casing
  92.             do
  93.             {
  94.                 code = word.charAt(++i);
  95.             } while (IsLower(code) && i < word.length);
  96.             
  97.             return word.substring(0, i);
  98.         }
  99.         // else word is in upper case assume that it is an ACKRONIMN
  100.         
  101.         return word.substring(0, i-1);
  102.     }
  103.     return "";
  104. }
  105.  
  106. function IsLower(code)
  107. {
  108.     return code >= 'a' && code <= 'z';
  109. }
  110.  
  111. function IsUpper(code)
  112. {
  113.     return !IsLower(code);
  114. }
  115.  
  116. !!/Script
  117.