-- Copyright 1996-2001 Robelle Solutions Technology Inc. -- Version 1.00 November 14, 2001 name QSLMatchCharacter group "Ro&belle" command "M&atch Character"; property QSLScriptVersion = "MatchCharacter 1.00 - Copyright Robelle Solutions Technology 2001"; property leftMatch = {"(", "[", "{"}; property rightMatch = {")", "]", "}"}; -- This script searches for matching left and right characters like -- parentheses, square brackets or curly braces. -- Uses the current selection to determine what to search for: -- If the selection is a left character, "(", "[" or "{", the script -- searches forward for the appropriate matching right character, ")", "]" or "}". -- If found, the script selects the text between the two characters. sub StartSearch(theFile, searchInfo); returnValue = {}; occurrenceCount = 1; findResult = true; regexpString = "[\" + searchInfo.fromString + "\" + searchInfo.toString + "]"; repeat while occurrenceCount > 0 and findResult findResult = theFile.find(regexp: regexpString, backwards: searchInfo.direction); if findResult then if theFile.GetSelectedText()[1] = searchInfo.fromString then occurrenceCount = occurrenceCount + 1; else occurrenceCount = occurrenceCount - 1; endif endif endrepeat if occurrenceCount = 0 then returnValue = theFile.selection; writelog("Return=" + string(returnValue)); endif return returnValue; endsub -- -- See if this is a valid character and, if so, determine -- search direction. -- sub IdentifyInitialChar(initialChar); searchInfo = {}; searchInfo.direction = false; searchInfo.fromString = ""; searchInfo.toString = ""; if pos(leftMatch, initialChar) = 0 and pos(rightMatch, initialChar) = 0 then result = dialog("The initial string is invalid. Can be " + string(leftMatch) + "or " + string(rightMatch)); else if pos(leftMatch, initialChar) > 0 then searchInfo.direction = false; searchInfo.fromString = leftMatch[pos(leftMatch, initialChar)]; searchInfo.toString = rightMatch[pos(leftMatch, initialChar)]; else searchInfo.direction = true; searchInfo.fromString = rightMatch[pos(rightMatch, initialChar)]; searchInfo.toString = leftMatch[pos(rightMatch, initialChar)]; endif endif return searchInfo; endsub -- -- Retrieve the selected initial character -- sub GetSelection(theFile); returnValue = ""; theSelection = theFile.selection; if length(theSelection) = 1 then -- No selection result = dialog("You must select some text."); else if theSelection.start.line <> theSelection.end.line then -- Selection spans multiple lines result = dialog("The selection must be on a single line."); else returnValue = theFile.getselectedtext()[1]; -- Subscript needed to convert to string endif endif return returnValue; endsub sub DoIt(); theFile = qedit.activefile; initialChar = GetSelection(theFile); if initialChar <> "" then searchInfo = IdentifyInitialChar(initialChar); if searchInfo.fromString <> "" then searchInfo.startLocation = theFile.selection; finalSelection = StartSearch(theFile, searchInfo); if length(finalSelection) > 0 then if not searchInfo.direction then -- Forward search finalSelection.start = searchInfo.startLocation.start; else finalSelection.end = searchInfo.startLocation.end; endif theFile.select(range: finalSelection); else theFile.select(range: searchInfo.startLocation); result = dialog("Could not find a matching left/right character"); endif endif endif endsub -- Mainline DoIt();