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

  1. !!Script
  2. // Copyright ⌐ 1999 - Modelworks Software
  3. // @Created build 285 cm19990331
  4. // @Modified build 290 cm19990427
  5.  
  6. /**
  7. @Tool: columnPaste~replaces the data in the
  8. current selection from the starting column of the
  9. selection to the ending column of the selection 
  10. for each line with the contents of the clipboard.
  11. @EndTool: 
  12. @Summary: columnPaste~paste data to a column
  13. */
  14.  
  15. var gOutput = getOutput();
  16.  
  17. function DoCommand()
  18. {
  19.     var editor = getActiveEditor();
  20.     if (editor)
  21.     {   
  22.         var range = editor.getSelection();
  23.         
  24.            var startLine = editor.copy(range.startLineIndex);
  25.         var endLine = editor.copy(range.endLineIndex);
  26.         
  27.         var tabWidth = editor.tabWidth;
  28.         var startColumn = CharacterToColumn(startLine, range.startCharIndex, tabWidth);
  29.         var endColumn = CharacterToColumn(endLine, range.endCharIndex, tabWidth);
  30.         
  31.         if (startColumn <= endColumn) //cm19990427
  32.         { 
  33.             var dataArray = GetDataArray(range.startLineIndex);
  34.     
  35.             for (var i = range.startLineIndex; i < range.endLineIndex; i++)
  36.             {
  37.                 var line = editor.copy(i);
  38.                 var start = ColumnToCharacter(line, startColumn, tabWidth);
  39.                 var end = ColumnToCharacter(line, endColumn, tabWidth);
  40.                 
  41.                 if (dataArray[i] != null)
  42.                 {
  43.                     editor.replace(dataArray[i], i, start, i, end);
  44.                 }
  45.                 else
  46.                 {
  47.                     editor.remove(i, start, i, end);
  48.                 }
  49.             }
  50.             var start = ColumnToCharacter(endLine, startColumn, tabWidth);
  51.             var end = ColumnToCharacter(endLine, endColumn, tabWidth);
  52.     
  53.             if (dataArray[i] != null)
  54.             {
  55.                 editor.replace(dataArray[i], i, start, i, end);
  56.                 editor.select(range.startLineIndex, range.startCharIndex, range.endLineIndex, start + dataArray[i].length);
  57.             }
  58.             else
  59.             {
  60.                 editor.remove(i, start, i, end);
  61.                 editor.select(range.startLineIndex, range.startCharIndex, range.endLineIndex, start);
  62.             }
  63.             
  64.             editor.setActive("Paste Column");
  65.         }
  66.         else
  67.         {
  68.             alert("The starting column of the selection must be \nless than or equal to the ending column of \nthe selection"); 
  69.         }
  70.     }
  71. }
  72.  
  73. function GetDataArray(lineIndex)
  74. {
  75.     var data = Application.getClipboardText();
  76.     var dataArray = new Array();
  77.     
  78.     var startIndex = 0;
  79.     var endIndex = data.indexOf('\r\n');
  80.     
  81.     while (endIndex > -1)
  82.     {
  83.         dataArray[lineIndex] = data.substring(startIndex,endIndex);
  84.         
  85.         startIndex = endIndex + 2;
  86.         endIndex = data.indexOf('\r\n', startIndex);
  87.         lineIndex += 1;
  88.     }
  89.         
  90.     dataArray[lineIndex] = data.substring(startIndex,data.length);
  91.     
  92.     return dataArray;
  93. }
  94.  
  95. function CharacterToColumn(line, charIndex, tabWidth)
  96. {
  97.     var column = 0;
  98.     for (var i = 0; i <= charIndex; i++)
  99.     {
  100.         if (line.charAt(i) == '\t')
  101.         {
  102.             column += tabWidth - (column % tabWidth);
  103.         }
  104.         else
  105.         {
  106.             column += 1;
  107.         }
  108.     }
  109.     return column;
  110. }
  111.  
  112. function ColumnToCharacter(line, column, tabWidth)
  113. {
  114.     var columnCount = 0;
  115.     var length = line.length;
  116.     
  117.     for (var i = 0; i < length; i++)
  118.     {
  119.         if (line.charAt(i) == '\t')
  120.         {
  121.             columnCount += tabWidth - (columnCount % tabWidth);
  122.         }
  123.         else
  124.         {
  125.             columnCount += 1;
  126.         }
  127.         
  128.         if (columnCount >= column)
  129.         {
  130.             return i;
  131.         }
  132.     }
  133.     return length;
  134. }
  135.  
  136. !!/Script
  137.  
  138.