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

  1. !!Script
  2. // Copyright ⌐ 1999 - Modelworks Software
  3. // @Created build 285 cm19990330
  4. // @Modified build 290 cm19990427
  5.  
  6. /**
  7. @Tool: columnCut~cuts the data from the 
  8. current selection from the starting column of the
  9. selection to the ending column of the selection 
  10. for each line to the clipboard.
  11. To use this script first make a selection in the 
  12. starting line at the starting column and select 
  13. to the ending line ending column. Then run the 
  14. script.
  15. @EndTool: 
  16. @Summary: columnCut~cuts a column of data
  17. */
  18.  
  19. var gOutput = getOutput();
  20.  
  21. function DoCommand()
  22. {
  23.     var editor = getActiveEditor();
  24.     if (editor)
  25.     {   
  26.         var range = editor.getSelection();
  27.         
  28.         var startLine = editor.copy(range.startLineIndex);
  29.         var endLine = editor.copy(range.endLineIndex);
  30.         
  31.         var tabWidth = editor.tabWidth;
  32.         var startColumn = CharacterToColumn(startLine, range.startCharIndex, tabWidth);
  33.         var endColumn = CharacterToColumn(endLine, range.endCharIndex, tabWidth);
  34.         if (startColumn <= endColumn) //cm19990427
  35.         {
  36.             var data = "";
  37.             for (var i = range.startLineIndex; i < range.endLineIndex; i++)
  38.             {
  39.                 var line = editor.copy(i);
  40.                 var start = ColumnToCharacter(line, startColumn, tabWidth);
  41.                 var end = ColumnToCharacter(line, endColumn, tabWidth);
  42.                 data += line.substring(start, end) + '\r\n';
  43.                 
  44.                 editor.remove(i, start, i, end);
  45.             }
  46.             var start = ColumnToCharacter(endLine, startColumn, tabWidth);
  47.             var end = ColumnToCharacter(endLine, endColumn, tabWidth);
  48.             data += endLine.substring(start, end);
  49.             
  50.             editor.remove(i, start, i, end);
  51.     
  52.             Application.setClipboardText(data);
  53.             
  54.             editor.setActive("Cut Column");
  55.         }
  56.         else
  57.         {
  58.             alert("The starting column of the selection must be \nless than or equal to the ending column of \nthe selection"); 
  59.         }
  60.     }
  61. }
  62.  
  63. function CharacterToColumn(line, charIndex, tabWidth)
  64. {
  65.     var column = 0;
  66.     for (var i = 0; i <= charIndex; i++)
  67.     {
  68.         if (line.charAt(i) == '\t')
  69.         {
  70.             column += tabWidth - (column % tabWidth);
  71.         }
  72.         else
  73.         {
  74.             column += 1;
  75.         }
  76.     }
  77.     return column;
  78. }
  79.  
  80. function ColumnToCharacter(line, column, tabWidth)
  81. {
  82.     var columnCount = 0;
  83.     var length = line.length;
  84.     
  85.     for (var i = 0; i < length; i++)
  86.     {
  87.         if (line.charAt(i) == '\t')
  88.         {
  89.             columnCount += tabWidth - (columnCount % tabWidth);
  90.         }
  91.         else
  92.         {
  93.             columnCount += 1;
  94.         }
  95.         
  96.         if (columnCount >= column)
  97.         {
  98.             return i;
  99.         }
  100.     }
  101.     return length;
  102. }
  103.  
  104. !!/Script
  105.  
  106.