Surround / wrap selected text with quotes, brackets and other characters

Surround / wrap selected text with quotes, brackets and other characters

326
Basic UserBasic User
326

    Jul 20, 2016#1

    This is a variation on a "close brackets" or "close quotes" script / functionality.
    It can wrap selected text with an opening- and end-characters which you can define yourself.
    An example below is given for [] but it should work with other characters as well such as " ' < { (

    It follows these rules:
    • Normal mode: if caret is at an empty space (including start/end of line) -> write the closing character
    • Normal mode: if caret is at a non-empty space only write opening-character
    • Normal mode: if something is selected -> wrap selection in opening- and closing-characters
    • Column mode: if nothing is selected -> insert opening- and closing-characters
    • Column mode: if something is selected on ONE line -> wrap selection in opening- and closing-characters
    v1.1:
    • Column mode: if something is selected on Multiple lines -> wrap selection in in opening- and closing-characters, place caret just before close character so you can continue typing in column mode :-)
    v1.0
    • Column mode: if something is selected on Multiple lines -> remove selection and write opening- and closing-characters
    See post below.

    Create a script like so, say OpenClose-SquareBrackets.js assign [ as hotkey

    Code: Select all

    var openchar = "[";
    var closechar = "]";
    // include OpenClose.js
    
    Updated March 2018 now UltraEdit.activeDocument.currentLineNum is working again

    Version 1.1

    Save this script as OpenClose.js:

    Code: Select all

    // -----------------------------------------------------------------------------------
    // Script:
    // - OpenClose.js (script meant to be included)
    //
    // Purpose:
    // - Script taking into account selected text and works in column mode
    //   to insert closing "<'{( e.g. type [ and ] is automatically inserted as well.
    //   See Rules below.
    //
    // Author / UE forum thread:
    // - hugov
    // - https://forums.ultraedit.com/viewtopic.php?f=52&t=16787
    //
    // History:
    // - v1.1 UE v25.00.0.53 now supports UltraEdit.activeDocument.currentLineNum again (March 2018)
    // - v1.0 first version
    //
    // Follows these rules:
    // - Normal mode: if caret is at an empty space (including start/end of line) -> write the closing character
    // - Normal mode: if caret is at a non-empty space only write opening-character
    // - Normal mode: if something is selected -> wrap selection in opening- and closing-characters
    // - Column mode: if nothing is selected -> insert opening- and closing-characters
    // - Column mode: if something is selected on ONE line -> wrap selection in opening- and closing-characters
    // as of v1.1:
    // - Column mode: if something is selected on Multiple lines -> wrap selection in in opening- and closing-characters,
    //   place caret just before close character so you can continue typing in column mode :-)
    //
    // - v1.0 behaviour (code in forum thread):
    // - Column mode: if something is selected on Multiple lines -> remove selection and write opening- and closing-characters
    //                this is where the problems begin, it should be possible to wrap selection and stay in column mode (see TODO)
    //
    // Usage:
    // - Create one or more scripts as shown below, one for each of the open/closing combinations
    //   you would like to use so for " ' [ < { ( or other characters
    // - Assign a hotkey to the script, for [ assigning [ as a hotkey will work,
    //   for " use shift+' etc
    //
    // -[start of script]------------------------
    // var openchar = "[";
    // var closechar = "]";
    //
    // // include OpenClose.js
    // -[end of script  ]------------------------
    //
    // -----------------------------------------------------------------------------------
    
    if (UltraEdit.document.length > 0) // returns number of active documents
    {
       // The column mode property is a property of the UltraEdit object in
       // UltraEdit for Windows and UEStudio, but a property of the document
       // object in UltraEdit for Linux and for Mac.
       var bColumnMode = false;
       if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
       else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
    
       if (!bColumnMode) // check if we're if column mode, if not proceed
       {
          UltraEdit.insertMode();
          if (UltraEdit.activeDocument.isSel()) // if something is selected just wrap it and we're done
          {
             UltraEdit.activeDocument.write(openchar + UltraEdit.activeDocument.selection + closechar);
          }
          else
          {
             CheckChar();
          }
       }
       else // column mode == true
       {
          if (UltraEdit.activeDocument.isSel()) // multiple lines - not always correct
          {
             UltraEdit.selectClipboard(1);
             UltraEdit.activeDocument.copy(); // inefficient method of trying to determine number of selected lines
             var ctext = UltraEdit.clipboardContent;
             var c = ctext.split("\n");
             var columnlength = c[1].length-1;
             if (c.length > 1)
             {
                var CopyWrappedText = "";
                var arrayLength = c.length-1;
                for (var i = 0; i < arrayLength; i++) {
                    CopyWrappedText += openchar + c[i].trim() + closechar + "\n";
                }
                UltraEdit.clipboardContent = CopyWrappedText;
                UltraEdit.activeDocument.paste();
                // currentLineNum will always be the TOP line of the selection after paste so we can use that
                // combined with the height of the selection (arrayLength) to figure out how many lines to move down
                var LineNumTop = UltraEdit.activeDocument.currentLineNum; // UltraEdit v25.00.0.53 works again - see forum thread
                var LineNumBottom = LineNumTop + arrayLength - 1;
                var EndColumn = UltraEdit.activeDocument.currentColumnNum + columnlength + 1;
                UltraEdit.activeDocument.gotoLine(LineNumTop,EndColumn);
                UltraEdit.activeDocument.startSelect();
                var x=0;
                while (x < arrayLength-1) {
                     UltraEdit.activeDocument.key("DOWN ARROW");
                     x++;
                  }
                UltraEdit.activeDocument.endSelect();
             }
             else
             {
                UltraEdit.activeDocument.write(openchar + ctext + closechar);
             }
              UltraEdit.selectClipboard(0);
           }
           else // for single line use write otherwise open + close will be inserted in all lines below current line as well
           {
             CheckChar();
           }
       }
      UltraEdit.selectClipboard(0);
    }
    
    function CheckChar() {
       var char = UltraEdit.activeDocument.currentChar; // if current character isn't empty (space or end of line) just write with closing char
       if (!char.trim().length)
       {
          UltraEdit.activeDocument.write(openchar + closechar);
          UltraEdit.activeDocument.key("LEFT ARROW");
          UltraEdit.activeDocument.cancelSelect();
       }
       else // current char wasn't empty so just write opening char
       {
          UltraEdit.activeDocument.write(openchar);
       }
    }
    
    Version 1.0

    Save this script as OpenClose.js:

    Code: Select all

    // -----------------------------------------------------------------------------------
    // Script:
    // - OpenClose.js (script meant to be included)
    //
    // Purpose:
    // - Script taking into account selected text and works in column mode
    //   to insert closing "<'{( e.g. type [ and ] is automatically inserted as well.
    //   See Rules below.
    //
    // Author / UE forum thread:
    // - hugov
    // - https://forums.ultraedit.com/viewtopic.php?f=52&t=16787
    // 
    // History:
    // - v1.0 first version
    //
    // Follows these rules:
    // - Normal mode: if caret is at an empty space (including start/end of line) -> write the closing character
    // - Normal mode: if caret is at a non-empty space only write opening-character
    // - Normal mode: if something is selected -> wrap selection in opening- and closing-characters
    // - Column mode: if nothing is selected -> insert opening- and closing-characters
    // - Column mode: if something is selected on ONE line -> wrap selection in opening- and closing-characters
    // - Column mode: if something is selected on Multiple lines -> remove selection and write opening- and closing-characters
    //                this is where the problems begin, it should be possible to wrap selection and stay in column mode (see TODO)
    //
    // Usage:
    // - Create one or more scripts as shown below, one for each of the open/closing combinations
    //   you would like to use so for " ' [ < { ( or other characters
    // - Assign a hotkey to the script, for [ assigning [ as a hotkey will work,
    //   for " use shift+' etc
    //
    // -[start of script]------------------------
    // var openchar = "[";
    // var closechar = "]";
    //
    // // include OpenClose.js
    // -[end of script  ]------------------------
    //
    // TODO: (not possible)?
    // - Allow for selection in column mode (wrap selected text) and stay in column mode*.
    //   Currently not possible - note that UltraEdit.activeDocument.currentLineNum fails
    //   to work in column mode (always returns line 1) - tested with UE v23.20.0.28
    //   (*: if you don't mind loosing your "column" it is possible) - see forum thread
    //   for answer
    // -----------------------------------------------------------------------------------
    
    if (UltraEdit.document.length > 0) // returns number of active documents
    {
       // The column mode property is a property of the UltraEdit object in
       // UltraEdit for Windows and UEStudio, but a property of the document
       // object in UltraEdit for Linux and for Mac.
       var bColumnMode = false;
       if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
       else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
    
       if (!bColumnMode) // check if we're if column mode, if not proceed
       {
          UltraEdit.insertMode();
          if (UltraEdit.activeDocument.isSel()) // if something is selected just wrap it and we're done
          {
             UltraEdit.activeDocument.write(openchar + UltraEdit.activeDocument.selection + closechar);
          }
          else
          {
             CheckChar();
          }
       }
       else // column mode == true
       {
          if (UltraEdit.activeDocument.isSel()) // multiple lines - not always correct
          {
             UltraEdit.selectClipboard(1);
             UltraEdit.activeDocument.copy(); // inefficient method of trying to determine number of selected lines
             var ctext = UltraEdit.clipboardContent;
             var c = ctext.split("\n");
             if (c.length > 1)
             {
                UltraEdit.activeDocument.columnInsert(openchar + closechar);
                UltraEdit.activeDocument.key("LEFT ARROW");
             }
             else
             {
                // UltraEdit.activeDocument.write(openchar + closechar);
                UltraEdit.activeDocument.write(openchar + ctext + closechar);
             }
             UltraEdit.selectClipboard(0);
          }
          else // for single line use write otherwise open + close will be inserted in all lines below current line as well
          {
             CheckChar();
          }
       }
    }
    
    function CheckChar() {
       var char = UltraEdit.activeDocument.currentChar; // if current character isn't empty (space or end of line) just write with closing char
       if (!char.trim().length)
       {
          UltraEdit.activeDocument.write(openchar + closechar);
          UltraEdit.activeDocument.key("LEFT ARROW");
          UltraEdit.activeDocument.cancelSelect();
       }
       else // current char wasn't empty so just write opening char
       {
          UltraEdit.activeDocument.write(openchar);
       }
    }
    I hope this is useful for someone.

      Jul 20, 2016#2

      This is a bit of experimental code which would allow you to wrap a selected rectangle of text to be wrapped in the opening and closing characters - [ and ] are hard coded here, if you want to incorporate this in the script above use openchar and closechar for '[' and ']' respectively

      The problem is that after pasting the modified text where each line of the selection is wrapped in the opening and closing characters you are still in column mode but there is only one line active, so if you where editing four lines you have to "select" the four lines again manually if you would like to continue. Perhaps when UltraEdit.activeDocument.currentLineNum works again in column mode (see note in script above) there may be a work around.

      Or perhaps I'm sorely mistaken and it is possible but I don't know how to do it, feel free to post a solution of course

      Code: Select all

      UltraEdit.selectClipboard(1);
      UltraEdit.activeDocument.copy(); // inefficient method of getting the first line
      var txt = UltraEdit.clipboardContent;
      var n = txt.indexOf("\n")-1;
      var newtxt = txt.split("\n");
      var clip = newtxt.join("]\n");
      UltraEdit.activeDocument.columnCut(0);
      UltraEdit.activeDocument.columnInsert('[');
      UltraEdit.clearClipboard;
      UltraEdit.clipboardContent = clip;
      UltraEdit.activeDocument.paste()
      for (i = 0; i < n; i++)
      {
         UltraEdit.activeDocument.key("RIGHT ARROW");
      }
      UltraEdit.clearClipboard;
      UltraEdit.selectClipboard(0);
      

        Mar 15, 2018#3

        Just a quick update in UltraEdit v25.00.0.53, 64 bit, UltraEdit.activeDocument.currentLineNum is now working correctly.

        Edit: Update script above, newer (v1.1) and original (v1.0) code above - if you have a version of UltraEdit where UltraEdit.activeDocument.currentLineNum is not working correctly use v1.0 - both versions should work with versions of UltraEdit which supports UltraEdit.activeDocument.currentLineNum so you can try both and use the one you prefer in that case.

        32
        NewbieNewbie
        32

          Feb 16, 2019#4

          Thanks for sharing your script hugov, it was just what I needed. <3