User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
2 posts Page 1 of 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
  • 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)
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 post below.

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

Code: Select all
var openchar = "[";
var closechar = "]";
// include OpenClose.js

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 columnmode
//   to insert closing "<'{( e.g. type [ and ] is automatically inserted as well.
//   See Rules below.
//
// Author / UE forum thread:
// - hugov
// - https://www.ultraedit.com/forums/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:
// - Creat 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
  {
   if (UltraEdit.columnMode == false) // check if we're if columnmode, 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.
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);
2 posts Page 1 of 1