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

  1. !!Script
  2. // Script by Will Hains
  3.  
  4. // Wrap selection with { ... } - also works for an insertion caret
  5.  
  6. var editor = getActiveEditor();
  7. if (editor)
  8. {
  9.   //Get current selection
  10.   var selection = editor.getSelection();
  11.   
  12.   var startLine = selection.startLineIndex;
  13.   var startChar = selection.startCharIndex;
  14.   var endLine = selection.endLineIndex;
  15.   var endChar = selection.endCharIndex;
  16.   
  17.   //Create string of tabs to selection indent level
  18.   var indentLevel = editor.getIndentLevel(selection.startLineIndex);
  19.   var tabString = "";
  20.   for(var x = 0; x < indentLevel; x++) {
  21.     tabString += "\t";
  22.   }
  23.   
  24.   //Adjust indexes for fully selected lines
  25.   if(startChar == 0 & endChar == 0) {
  26.     startChar = indentLevel;
  27.     endLine--;
  28.     endChar = 32000;
  29.   editor.select(
  30.     startLine, startChar,
  31.     endLine, endChar
  32.   );
  33.   selection = editor.getSelection();
  34.   var startLine = selection.startLineIndex;
  35.   var startChar = selection.startCharIndex;
  36.   var endLine = selection.endLineIndex;
  37.   var endChar = selection.endCharIndex;
  38.   }
  39.  
  40.   //Insert after selection
  41.   var afterSelection = "\n" + tabString + "}";
  42.   editor.insert(
  43.     endLine, endChar,
  44.     afterSelection
  45.   );
  46.   
  47.   //Insert before selection
  48.   var beforeSelection = "{\n" + tabString;
  49.   editor.insert(
  50.     startLine, startChar,
  51.     beforeSelection
  52.   );
  53.   
  54.   //Adjust startLine & endLine to reflect change
  55.   startLine++;
  56.   endLine++;
  57.   
  58.   //Indent selection between braces
  59.   for(var x = startLine; x <= endLine; x++) {
  60.     editor.insert(x, 0, "\t");
  61.   }
  62.  
  63.   //Adjust startChar & endChar to reflect change
  64.   startChar = indentLevel + 1;
  65.   endChar++;
  66.   
  67.   //Re-select the original selection
  68.   editor.select(
  69.     startLine, startChar,
  70.     endLine, endChar
  71.   );
  72.   
  73.   editor.setActive();
  74.   
  75. }
  76.  
  77. !!/Script
  78.