Similar to the macro above is the following script.
Code: Select all
if (UltraEdit.document.length > 0) {
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
var nColumn = UltraEdit.activeDocument.currentColumnNum;
if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColumn++;
var nLine = UltraEdit.activeDocument.currentLineNum;
var sIndent = "";
if (nColumn > 1) {
UltraEdit.activeDocument.gotoLineSelect(0,1);
if (UltraEdit.activeDocument.isSel()) {
sIndent = UltraEdit.activeDocument.selection.replace(/^([ \t]+).*$/,"$1");
if (sIndent == UltraEdit.activeDocument.selection) sIndent = "";
}
}
UltraEdit.activeDocument.gotoLine(nLine,nColumn);
UltraEdit.activeDocument.write("if()\r\n"+sIndent+"{\r\n"+sIndent+"\t\r\n"+sIndent+"}");
UltraEdit.activeDocument.gotoLine(nLine,nColumn+3);
}
But there are some disadvantages with the script as is.
The script does not recognize the type of line terminator of the active file. Well, depending on version of UE/UES the property
UltraEdit.activeDocument.lineTerminator could be used to make the script independent of line terminator type of active file.
Indent and unindent must be done within the script itself. But the script cannot really find out which settings are currently used for the active file. Are tabs or spaces used and if spaces, how many spaces? Well, if you want a script just for you and all your files are using always the same indent, then this is no real problem because you can use fixed spaces/tabs in the string.
A problem with script execution is that the status of the script is written to active output window after clearing current content. This behavior can be turned off with a configuration setting. But when you use this setting, don't forget to enable it temporarily when you next time develop a script because then also no errors detected by the Javascript interpreter are written to the output window.
The main disadvantage is that every script execution results in displaying the small Cancel dialog. While for macros there is a macro property to turn this off and break a macro execution with pressing ESC, it is not possible for scripts to turn off displaying the Cancel dialog because script execution can't be breaked with ESC.