home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 67 / IOPROG_67A.ISO / soft / Tools / mwsppv4.exe / COLUMNINSERT.SCRIPT < prev    next >
Encoding:
Text File  |  1999-04-27  |  2.8 KB  |  105 lines

  1. !!Script
  2. // Copyright ⌐ 1999 - Modelworks Software
  3. // @Created build 290 cm19990420
  4. // @Modified build 290 cm19990427
  5.  
  6. /**
  7. @Tool: columnInsert~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 specified string.
  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: columnInsert~inserts data in a column
  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.         
  35.         if (startColumn <= endColumn) // cm19990427
  36.         {
  37.             var insertData = prompt("Enter data to insert in this coloumn", "");
  38.     
  39.             for (var i = range.startLineIndex; i < range.endLineIndex; i++)
  40.             {
  41.                 var line = editor.copy(i);
  42.                 var start = ColumnToCharacter(line, startColumn, tabWidth);
  43.                 var end = ColumnToCharacter(line, endColumn, tabWidth);
  44.                 
  45.                 editor.replace(insertData, i, start, i, end);
  46.             }
  47.             var start = ColumnToCharacter(endLine, startColumn, tabWidth);
  48.             var end = ColumnToCharacter(endLine, endColumn, tabWidth);
  49.     
  50.             editor.replace(insertData, i, start, i, end);
  51.             editor.select(range.startLineIndex, range.startCharIndex, range.endLineIndex, start + insertData.length);
  52.             
  53.             editor.setActive("Insert Column");
  54.         }
  55.         else
  56.         {
  57.             alert("The starting column of the selection must be \nless than or equal to the ending column of \nthe selection"); 
  58.         }
  59.     }
  60. }
  61.  
  62. function CharacterToColumn(line, charIndex, tabWidth)
  63. {
  64.     var column = 0;
  65.     for (var i = 0; i <= charIndex; i++)
  66.     {
  67.         if (line.charAt(i) == '\t')
  68.         {
  69.             column += tabWidth - (column % tabWidth);
  70.         }
  71.         else
  72.         {
  73.             column += 1;
  74.         }
  75.     }
  76.     return column;
  77. }
  78.  
  79. function ColumnToCharacter(line, column, tabWidth)
  80. {
  81.     var columnCount = 0;
  82.     var length = line.length;
  83.     
  84.     for (var i = 0; i < length; i++)
  85.     {
  86.         if (line.charAt(i) == '\t')
  87.         {
  88.             columnCount += tabWidth - (columnCount % tabWidth);
  89.         }
  90.         else
  91.         {
  92.             columnCount += 1;
  93.         }
  94.         
  95.         if (columnCount >= column)
  96.         {
  97.             return i;
  98.         }
  99.     }
  100.     return length;
  101. }
  102.  
  103. !!/Script
  104.  
  105.