home *** CD-ROM | disk | FTP | other *** search
- !!Script
- // Copyright ⌐ 2002 - Modelworks Software
-
- /**
- @Event: onReplace
- @Summary: onReplace~called to implement smart replace.
- */
-
- var gFindParameters = getGlobal("FindParameters", null);
- var gOutput = getOutput();
-
- function OnEvent(editor, found, replace)
- {
- //gOutput.writeLine("----------------------");
- //gOutput.writeLine("found " + found);
- //gOutput.writeLine("replace " + replace);
- if (found.length > 0 && replace.length > 0 && !gFindParameters.caseSensitive && !gFindParameters.regularExpression)
- {
- var firstReplaceWord = GetFirstSubword(replace);
- var firstFoundWord = GetFirstSubword(found);
-
- //gOutput.writeLine("firstReplaceWord " + firstReplaceWord);
- //gOutput.writeLine("firstFoundWord " + firstFoundWord);
-
- if (firstReplaceWord.length > 0 && firstFoundWord.length > 0)
- {
- var replaceWordIsLower = IsLower(firstReplaceWord.charAt(0));
- var foundWordIsLower = IsLower(firstFoundWord.charAt(0));
-
- if (replaceWordIsLower != foundWordIsLower)
- {
- if (foundWordIsLower)
- {
- // camel case
- var newReplace = firstReplaceWord.toLowerCase() +
- replace.substring(firstReplaceWord.length, replace.length);
- //gOutput.writeLine("camel newReplace " + newReplace);
- return newReplace;
- }
- else
- {
- if (IsLower(firstFoundWord.charAt(1)))
- {
- // Pascal case
- var newReplace = firstReplaceWord.charAt(0);
- newReplace = newReplace.toUpperCase();
- newReplace +=replace.substring(1, replace.length);
- //gOutput.writeLine("Pascal newReplace " + newReplace);
- return newReplace;
- }
- else
- {
- // ACKRONIMN case
- var newReplace = firstReplaceWord.toUpperCase() +
- replace.substring(firstReplaceWord.length, replace.length);
- //gOutput.writeLine("ACKRONIMN newReplace " + newReplace);
- return newReplace;
- }
- }
- }
- //gOutput.writeLine("No change " + replace);
- }
- }
- return null; // if no change
- }
-
- function GetFirstSubword(word)
- {
- var code = word.charAt(0);
- if (IsLower(code))
- {
- // camel casing
- var i = 0;
- do
- {
- code = word.charAt(++i);
- } while (IsLower(code) && i < word.length);
-
- return word.substring(0, i);
- }
- else
- {
- var i = 0;
- do
- {
- code = word.charAt(++i);
- } while (IsUpper(code)&& i < word.length);
-
- if (i == 1)
- {
- // Pascal casing
- do
- {
- code = word.charAt(++i);
- } while (IsLower(code) && i < word.length);
-
- return word.substring(0, i);
- }
- // else word is in upper case assume that it is an ACKRONIMN
-
- return word.substring(0, i-1);
- }
- return "";
- }
-
- function IsLower(code)
- {
- return code >= 'a' && code <= 'z';
- }
-
- function IsUpper(code)
- {
- return !IsLower(code);
- }
-
- !!/Script
-