Completing a script run automatically deselects pre-launch selected text?

Completing a script run automatically deselects pre-launch selected text?

21
Basic UserBasic User
21

    Mar 11, 2022#1

    Trying to further improve a java script (for UE 28.20) I had written a few days ago. It occurred to me that the first script could not only merge text from another file into the main file (that worked fine), but also at the end pre-select the text lines I would then run my 2nd script on.

    Trouble is, even when I select text in the active view, then run a dummy script:

    Code: Select all

    /*
     *   ae_test.js
     */
    var ue  = UltraEdit;
    that does pretty much nothing, the previous text selection is deselected when the script is done.

    It took me quite some time to figure out that this was not an issue with the UE selection code, like

    Code: Select all

    var doc = UltraEdit.activeDocument;
        doc.gotoLine(13,1);        // Preselect all lines in the current block
        doc.startSelect();
        doc.gotoLine(17,1);
        doc.endSelect();
    
    // or better:
        doc.gotoLine(13,1);
        doc.gotoLineSelect(17,1);
    but the default behaviour of UE.

    Is there a way to run a script that will select text, and then preserve that selection of text after the script has completed execution?

    Thanks.

    6,603548
    Grand MasterGrand Master
    6,603548

      Mar 12, 2022#2

      I cannot reproduce with UltraEdit for Windows v28.20.0.92 the behavior of current selection of text being lost on execution of the dummy script independent on user interface mode and independent on how the execution of the script is triggered by me.

      Here is script code to get the either the current position of the caret in active file on nothing selected on starting the script or the beginning and end of selected text if there is text selected on starting the script. The file cannot be read-only and the selection cannot be too large for this code. The active file is also always modified temporarily by this code.

      Code: Select all

      if (UltraEdit.document.length > 0)   // Is at least one file opened?
      {
         // Is the active file not opened in hex edit mode?
         if (!UltraEdit.activeDocument.isHexModeOn())
         {
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            UltraEdit.insertMode();
      
            var nBeginColumn;
            var nBeginLine;
            var nEndColumn;
            var nEndLine;
      
            // Is a text selected in active file?
            if (UltraEdit.activeDocument.isSel())
            {
               // Cut the selected text to get caret positioned at the beginning
               // of the selection independent on selection made from top left
               // to bottom right or reverse from bottom right to top left.
               var nClipboard = UltraEdit.clipboardIdx;
               UltraEdit.selectClipboard((nClipboard != 9) ? 9 : 8);
               UltraEdit.activeDocument.cut();
               // Get the line number at beginning of selected text.
               nBeginLine = UltraEdit.activeDocument.currentLineNum;
               // Get the column number at beginning of selected text.
               nBeginColumn = UltraEdit.activeDocument.currentColumnNum;
               // Paste the initially selected text which results in caret
               // being after this action at the end of the initial selection.
               UltraEdit.activeDocument.paste();
               // Get the line number at end of selected text.
               nEndLine = UltraEdit.activeDocument.currentLineNum;
               // Get the column number at end of selected text.
               nEndColumn = UltraEdit.activeDocument.currentColumnNum;
               // Clear used clipboard and restore initial clipboard.
               UltraEdit.clearClipboard();
               UltraEdit.selectClipboard(nClipboard);
            }
            else
            {
               nBeginLine = UltraEdit.activeDocument.currentLineNum;
               nBeginColumn = UltraEdit.activeDocument.currentColumnNum;
               nEndLine = 0;
            }
      
            // Other scripting commands which move caret in active file.
      
            if (nEndLine)  // Was there a selection on starting execution of script?
            {              // Yes, restore the initial selection if possible at all.
               UltraEdit.activeDocument.gotoLine(nBeginLine,nBeginColumn);
               UltraEdit.activeDocument.gotoLineSelect(nEndLine,nEndColumn);
            }
            else           // No, restore initial caret position if possible at all.
            {
               UltraEdit.activeDocument.gotoLine(nBeginLine,nBeginColumn);
            }
         }
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      21
      Basic UserBasic User
      21

        Mar 12, 2022#3

        Thanks for putting in so much effort and the code. Will look through the code in detail.

        But you are right, after rebooting my Win10Pro system this morning and relaunching UE, I just tested selecting text and running the "dummy script" above. Indeed the selected text stays selected.

        I am guessing that I must have messed up something with my java scripts, like selection mode not being turned off internally,  or something weird like that. Apparently it got reset by rebooting Windows or possibly just relaunching UE. I had guessed that I might have set some strange preference setting or activated some other temporary mode.

        Will continue to work on my script and test it all out, to make sure it now works properly.

        Update: Just finished editing my script, and it works as it should have yesterday. So I was using the methods properly. *sigh*

          Mar 12, 2022#4

          Mofi wrote:
          Mar 12, 2022

          Code: Select all

                // Cut the selected text to get caret positioned at the beginning
                // of the selection independent on selection made from top left
                // to bottom right or reverse from bottom right to top left.
          Interesting piece of code, especially how you are able to get the top/left and bottom/right coordinates of the selection block. Thanks for that.

            Mar 14, 2022#5

            Strange it happened again.  The code should select an area, and it did, but once the script ended, that text is automatically deselected. Well, again I simply relaunched UE, and that fixed the issue. Let's hope the UE 2022.0 fixes that by some miracle.