Tapatalk

Expand selection to a rectangular selection to end of file

Expand selection to a rectangular selection to end of file

24
Basic UserBasic User
24

    Jan 12, 2013#1

    A script I need is "Set Selection" with no "begin" and "end" range input as in Edit > Select Range.

    "Begin" will be where the cursor is on at present position. And "end" would be always the end of file (EOF).

    So I would just need to select Column (start) and Column (end).

    Thanks in advance!

    6,685587
    Grand MasterGrand Master
    6,685587

      Jan 12, 2013#2

      I'm not sure if I have understood completely what you want next.

      Why not using Select Range to make a rectangular selection from current caret position to column X on last line of the file?

      Let's say the caret is at column 20 on line 15 and the Select Range dialog is opened (by hotkey). As the beginning position is already right pressing twice TAB selects the number for the end line. Well, you most likely don't know it. But that does not matter as you can simply enter for example 9999999 to create a rectangular selection to column X on last line if the file has not more than 9999999 lines. Next you press once more TAB and enter the end column. Now hitting RETURN and the selection is made.

      However, here is a script which assumes that there is a selection in current line marking begin and end column which should be expanded in column editing mode as rectangular selection to last line of the file.

      Code: Select all

      // Run this script only when at least 1 file is opened.
      if (UltraEdit.document.length > 0)
      {
         // There must be a selection in current line only with caret blinking
         // on end of the selection. It is important for correct working of the
         // script that the selection does not end beyond end of the current line.
         if (UltraEdit.activeDocument.isSel()) {
            // Define environment for script.
            UltraEdit.insertMode();
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            // Get caret position at end of the selection.
            var nBeginLine = UltraEdit.activeDocument.currentLineNum;
            var nEndCol = UltraEdit.activeDocument.currentColumnNum;
            // The next line is needed only for UE prior v16.00 and UES prior v10.00.
            if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nEndCol++;
            // Calculate the column at beginning of the selection by simply
            // subtracting the number of characters of the selection.
            var nBeginCol = nEndCol - UltraEdit.activeDocument.selection.length;
            // This results definitely in a wrong column number if a tab is included.
            if (UltraEdit.activeDocument.selection.indexOf('\t') >= 0) {
               // In case of a tab being included in the selection, cut the selection
               // to clipboard 9, get the column number at beginning of the selection
               // and paste the selection back.
               UltraEdit.selectClipboard(9);
               UltraEdit.activeDocument.cut();
               nBeginCol = UltraEdit.activeDocument.currentColumnNum;
               if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nBeginCol++;
               UltraEdit.activeDocument.paste();
               UltraEdit.clearClipboard();
               UltraEdit.selectClipboard(0);
            }
            // Go to end of the file to get its line number.
            UltraEdit.activeDocument.bottom();
            var nEndLine = UltraEdit.activeDocument.currentLineNum;
            // If last line of file ends with a line termination, subtract 1
            // from the line number to get real line number of last line.
            if (UltraEdit.activeDocument.isColNum(1)) nEndLine--;
            // Go back (hopefully right) beginning of the initial selection.
            UltraEdit.activeDocument.gotoLine(nBeginLine,nBeginCol);
            // Turn on column editing mode and make now a rectangular
            // selection to initial end column in active line in last line.
            if (typeof(UltraEdit.columnModeOn) == "function") UltraEdit.columnModeOn();
            else if (typeof(UltraEdit.activeDocument.columnModeOn) == "function") UltraEdit.activeDocument.columnModeOn();
            UltraEdit.activeDocument.gotoLineSelect(nEndLine,nEndCol);
         }
      }
      Please note that this script works only as designed if the current selection does not spread over multiple lines. And the selection must also end before end of active line. So the result is wrong if a selection is made (in already enabled column editing mode) beyond end of active line.

      Also tabs within the selection makes it more difficult to determine starting column. A workaround is needed in this case which results in creating 2 undo steps.

      And last if the selection in current line ends at a column greater the number of columns on last line, the selection is also not correct if the configuration setting Allow positioning beyond line end is not enabled.

      Making a rectangular selection without command Select Range depends on many variables: the configuration setting Allow positioning beyond line end, the existence of the termination of the last line, the number of columns on last line, the number of columns in active line, and perhaps more. So it is very hard to write general scripts for making a rectangular selection working in all cases. For this reason I avoid making rectangular selections in macros and scripts as much as possible and use the column editing commands or regular expression replaces to reformat a rectangular block.

      Just one more note:

      Command Select Range with a too high end line number makes always a rectangular selection to end column on the line the caret is blinking when moving caret to end of file with Ctrl+End. This can be beyond last line of file if the last line ends with a line termination. The script above expands the selection to last line of file before last line termination if the last line ends with a line termination (and has enough characters as on active line).

      24
      Basic UserBasic User
      24

        Jan 14, 2013#3

        I didn't know you could just put a bigger number (e.g. 999999999) and then it would work.

        Anyway thanks once again!