Select content inside braces?

Select content inside braces?

12
Basic UserBasic User
12

    Oct 26, 2011#1

    Hi,

    the Ctrl + B (Search - Selecting to Matching Brace) selects everything between two braces _but_ includes both of them in the selection.

    In 90% of the cases I need a function that selects everything between two braces (multiline must be supported, too) but without including the braces.

    Is this possible with a macro?

    Regards,
    highend

    6,675585
    Grand MasterGrand Master
    6,675585

      Oct 26, 2011#2

      I think, with a macro it is not possible taking into account that Search - Select to Matching Brace works in both directions and therefore the macro would need to find out if the command selected a block downwards or upwards. Variables are not supported in macros.

      But selecting everything between 2 braces is possible with a script. The advantage of a script solution is that it can be made really smart. See the script SelectWithinBraces.js below which I just coded for you and which I will use in future maybe too.

      Code: Select all

      /* This script selects smart everything between two matching braces.
         The limitation is that only single character braces are supported.
         So it works for { and }, ( and ), [ and ], and < and >. But it
         can't be used for other "brace" strings like "#ifdef" and "#endif".
         But match brace feature is usually used only for real braces. */
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Remember current position of caret in file.
         var nColumn = UltraEdit.activeDocument.currentColumnNum;
         var nLine = UltraEdit.activeDocument.currentLineNum;
      
         // Run match brace command which either selects a block downwards
         // to matching closing brace or upwards to matching opening brace.
         UltraEdit.activeDocument.matchBrace();
      
         // Is really a string selected now?
         if (UltraEdit.activeDocument.isSel())
         {
            // Get selected block into a string variable.
            var sBlock = UltraEdit.activeDocument.selection;
            // Remove the first and last character of the
            // block which are the opening and closing braces.
            var sBlock = sBlock.substr(1,sBlock.length-2);
      
            /* In C/C++/C# and JavaScript and perhaps other languages the match brace
               command could be used also for selecting a block between { and } and
               in this case it would be good to select just the lines between { and }.
               Therefore remove from beginning of string white-spaces and first line
               termination and also all white-spaces at end of the block. It is also
               for strings between ( and ) good to remove spaces at beginning and
               end of the selected inner string. */
            sBlock = sBlock.replace(/^[ \t]*\r{0,1}\n{0,1}/,"");
            sBlock = sBlock.replace(/[ \t]+$/,"");
      
            /* If match brace was downwards, the caret is now positioned after
               matching closing brace. In this case move caret 1 character to
               left and use match brace command again to select updwards. */
            if ((UltraEdit.activeDocument.currentLineNum > nLine) ||
                ((UltraEdit.activeDocument.currentLineNum == nLine) &&
                (UltraEdit.activeDocument.currentColumnNum > nColumn)))
            {
               UltraEdit.activeDocument.key("LEFT ARROW");
               UltraEdit.activeDocument.matchBrace();
            }
            // Set caret 1 character to right after opening brace.
            UltraEdit.activeDocument.key("RIGHT ARROW");
      
            // Use a find for the already determined string
            // to select everything between the matching braces.
            UltraEdit.activeDocument.findReplace.mode=0;
            UltraEdit.activeDocument.findReplace.matchCase=true;
            UltraEdit.activeDocument.findReplace.matchWord=false;
            UltraEdit.activeDocument.findReplace.regExp=false;
            UltraEdit.activeDocument.findReplace.searchDown=true;
            if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
               UltraEdit.activeDocument.findReplace.searchInColumn=false;
            UltraEdit.activeDocument.findReplace.find(sBlock);
         }
      }

      12
      Basic UserBasic User
      12

        Oct 26, 2011#3

        Thanks a lot Mofi, your script works perfectly.

        I really like UE more and more, beeing able to assign hotkeys to scripts and macros makes the whole thing really usable :D