expand selection to include beginning of first line and end of last line selected

expand selection to include beginning of first line and end of last line selected

9

    Sep 15, 2015#1

    I want to allow users to select a selection of lines haphazardly, dragging their cursor from the middle of line [34] to the middle of line [37], for example. Eventually, my script will process all lines of the selection, and the first line and last line need to be fully selected. (If you use VIM, think of Shift V selection, i.e., only full lines are selected.)

    Given this:

    Code: Select all

    var myText = UltraEdit.activeDocument.selection
    ...is there a transformation or querying I can use to ensure the first and last lines of the selection are reaching to the beginning and end?

    Example:
    User selects like this (X is selected text):

    Code: Select all

    xxxXXXXX
    XXXXXXXX
    XXXXXxxx
    ...and I want to convert that selection in the script to:

    Code: Select all

    XXXXXXXX
    XXXXXXXX
    XXXXXXXX

    6,681583
    Grand MasterGrand Master
    6,681583

      Sep 16, 2015#2

      There are no scripting properties to get line and column number of top left and bottom right edge of an active selection.

      It is not clear from your question if the selection should end before line termination of last line of selection or should include the line termination of last selected line.

      I suppose the line termination of last selected line should not be included which makes expanding the selection in both directions easier to code.

      I have two possible solutions both tested with UE v22.20. The first one is faster, but does not produce the right result on selection was made by the user from bottom to top.

      Code: Select all

      // This script selects from beginning of first selected line to end of
      // last selected line without selecting the line termination on last
      // line which works also on a selection at end of file even if there
      // is no line termination on last line of file.
      
      // But this works only when selection was made from top to bottom. When
      // the selection was made from bottom to top, this script does not work
      // as nFirstLine and nLastLine will have identical values on selection
      // made from bottom to top.
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         if (UltraEdit.activeDocument.isSel())
         {
            // Define environment for this script.
            UltraEdit.insertMode();
            UltraEdit.columnModeOff();
      
            // Get number of line at top left edge of selection.
            var nFirstLine = UltraEdit.activeDocument.currentLineNum;
            UltraEdit.activeDocument.cancelSelect();
            // Get number of line at end of selection (= caret position)
            // after selection was canceled by above command. This returns
            // same line number if selection was made from bottom to top.
            var nLastLine = UltraEdit.activeDocument.currentLineNum;
      
            // Go to column 1 of first line of initial selection.
            UltraEdit.activeDocument.gotoLine(nFirstLine,1);
      
            // Go to column 1 of last line of initial selection and
            // select all text between previous position in file.
            UltraEdit.activeDocument.gotoLineSelect(nLastLine,1);
      
            // Extend the selection to end of this line.
            UltraEdit.activeDocument.startSelect();
            UltraEdit.activeDocument.key("END");
            UltraEdit.activeDocument.endSelect();
         }
      }
      
      The second script is a bit slower, but works independent on selection direction.

      Code: Select all

      // This script selects from beginning of first selected line to end of
      // last selected line without selecting the line termination on last
      // line which works also on a selection at end of file even if there
      // is no line termination on last line of file. It works independent
      // on how selection was made by the user: from top to bottom or
      // reverse from bottom to top (blinking caret position is different).
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         if (UltraEdit.activeDocument.isSel())
         {
            // Define environment for this script.
            UltraEdit.insertMode();
            UltraEdit.columnModeOff();
      
            // Get number of line at top left edge of selection.
            var nFirstLine = UltraEdit.activeDocument.currentLineNum;
            // By default the last line is also the first line of the selection.
            var nLastLine = nFirstLine;
      
            // Find all DOS/UNIX/MAC line terminations in selected text copied
            // into an array of strings.
            var asLineTerms = UltraEdit.activeDocument.selection.match(/\r?\n|\r/g);
      
            // Does the selection contain any line termination at all?
            if (asLineTerms != null)
            {
               // Increment the number of last line according to
               // number of line terminations found in selected text.
               nLastLine += asLineTerms.length;
            }
      
            // Cancel selection which would not be really necessary.
            UltraEdit.activeDocument.cancelSelect();
      
            // Go to column 1 of first line of initial selection.
            UltraEdit.activeDocument.gotoLine(nFirstLine,1);
      
            // Go to column 1 of last line of initial selection and
            // select all text between previous position in file.
            UltraEdit.activeDocument.gotoLineSelect(nLastLine,1);
      
            // Extend the selection to end of this line.
            UltraEdit.activeDocument.startSelect();
            UltraEdit.activeDocument.key("END");
            UltraEdit.activeDocument.endSelect();
         }
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      9

        Sep 28, 2015#3

        This was a great reply. Thanks, Mofi. I didn't see that you'd answered until today, so apologies for my delay in thanking you :)