home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / mwjpp516.exe / %MAINDIR% / Tools / Misc / wrap.script < prev    next >
Encoding:
Text File  |  2001-06-19  |  2.0 KB  |  93 lines

  1. !!Script
  2. // Copyright ⌐ 1997-1998 - Modelworks Software
  3.  
  4. /**
  5. @Tool: wrap~breaks long lines into multiple 80 column or less lines. To 
  6. use select one or more lines and then run the script. 
  7. @See: Unwarp@Tools Reference/Tools/Misc.html#Unwrap
  8. @EndTool: 
  9. @Summary: wrap~breaks long lines into multiple 80 column or less lines
  10. */
  11.  
  12. var gOutput = getOutput();
  13. var gMaxLineLength = 80;
  14. var gWordBreak = " \t";
  15.  
  16. function DoCommand()
  17. {
  18.   var editor = getActiveEditor();
  19.   if (editor)
  20.   {
  21.     var selection = editor.getSelection();
  22.     if (selection.endCharIndex == 0)
  23.     {
  24.       selection.endLineIndex -= 1;
  25.     }
  26.     
  27.     for (var lineIndex = selection.endLineIndex; 
  28.       lineIndex >= selection.startLineIndex; lineIndex--)
  29.     {     
  30.       var line = editor.copy(lineIndex);
  31.             
  32.             var indentLevel = editor.getIndentLevel(lineIndex);
  33.             var newLine = "\n";
  34.             while(indentLevel > 0)
  35.             {
  36.                 newLine += "\t";
  37.                 indentLevel -= 1;
  38.             }
  39.             
  40.       if (line.length > gMaxLineLength)
  41.       {
  42.         var array = new Array();
  43.   
  44.         array[0] = 0;
  45.         array[1] = gMaxLineLength;
  46.         var i = 1;
  47.         while (line.length > array[i])
  48.         {
  49.           var index = LastSpace(line, array[i]);
  50.           if (index > array[i-1])
  51.           {
  52.                         var value = line.charAt(index);
  53.                         if (value == ' ' || value == '\t')
  54.                         {
  55.                    array[i] = index + 1;
  56.                         }
  57.                         else
  58.                         {
  59.                    array[i] = index;
  60.                         }
  61.           }
  62.           array[i+1] = array[i] + gMaxLineLength;
  63.           i += 1;
  64.         }
  65.   
  66.         for (j = i - 1; j > 0; j--)
  67.         {
  68.           line = replaceInString(line, array[j], 0, newLine);
  69.         }
  70.         editor.replace(line, lineIndex);
  71.       }
  72.     }
  73.     
  74.     editor.setActive("Wrap Lines");
  75.   }
  76. }
  77.  
  78. function LastSpace(line, fromIndex)
  79. {
  80.     var toIndex = Math.max(0, fromIndex - gMaxLineLength - 1);
  81.     for (var i = fromIndex; i > toIndex; i--)
  82.     {
  83.         var value = line.charAt(i);
  84.         if (gWordBreak.indexOf(value) > -1)
  85.         {
  86.             return i;
  87.         }
  88.     }
  89.     return -1;
  90. }
  91.  
  92. !!/Script
  93.