home *** CD-ROM | disk | FTP | other *** search
- !!Script
- // Copyright ⌐ 1997-1998 - Modelworks Software
-
- /**
- @Tool: wrap~breaks long lines into multiple 80 column or less lines. To
- use select one or more lines and then run the script.
- @See: Unwarp@Tools Reference/Tools/Misc.html#Unwrap
- @EndTool:
- @Summary: wrap~breaks long lines into multiple 80 column or less lines
- */
-
- var gOutput = getOutput();
- var gMaxLineLength = 80;
- var gWordBreak = " \t";
-
- function DoCommand()
- {
- var editor = getActiveEditor();
- if (editor)
- {
- var selection = editor.getSelection();
- if (selection.endCharIndex == 0)
- {
- selection.endLineIndex -= 1;
- }
-
- for (var lineIndex = selection.endLineIndex;
- lineIndex >= selection.startLineIndex; lineIndex--)
- {
- var line = editor.copy(lineIndex);
-
- var indentLevel = editor.getIndentLevel(lineIndex);
- var newLine = "\n";
- while(indentLevel > 0)
- {
- newLine += "\t";
- indentLevel -= 1;
- }
-
- if (line.length > gMaxLineLength)
- {
- var array = new Array();
-
- array[0] = 0;
- array[1] = gMaxLineLength;
- var i = 1;
- while (line.length > array[i])
- {
- var index = LastSpace(line, array[i]);
- if (index > array[i-1])
- {
- var value = line.charAt(index);
- if (value == ' ' || value == '\t')
- {
- array[i] = index + 1;
- }
- else
- {
- array[i] = index;
- }
- }
- array[i+1] = array[i] + gMaxLineLength;
- i += 1;
- }
-
- for (j = i - 1; j > 0; j--)
- {
- line = replaceInString(line, array[j], 0, newLine);
- }
- editor.replace(line, lineIndex);
- }
- }
-
- editor.setActive("Wrap Lines");
- }
- }
-
- function LastSpace(line, fromIndex)
- {
- var toIndex = Math.max(0, fromIndex - gMaxLineLength - 1);
- for (var i = fromIndex; i > toIndex; i--)
- {
- var value = line.charAt(i);
- if (gWordBreak.indexOf(value) > -1)
- {
- return i;
- }
- }
- return -1;
- }
-
- !!/Script
-