I hope someone will find it useful - based on Columnizer.js

I hope someone will find it useful - based on Columnizer.js

51
NewbieNewbie
51

    May 19, 2019#1

    Code: Select all

    // Columnizer script version 2
    //
    // uses UE clipboard 9 as read only; resets to Clipboard 0
    //
    ////////////////////////////////////////////////////////////////////////////////////////////////
    // To safe a lot of space tab usage ;)
    //
    // Takes a multi-row selection and inserts spaces to align selected string to the predefined column
    // Columns is defined in clipboard no 9 (default is set to 80
    // String / character to search for is asked by script at the beginning.
    //
    // Example:
    // as= bass
    //   gsd   = zzz
    //
    // After entering as string "=" and putting into UE clipboard_9 = 20, will produce. (It can be any string)
    //
    // as               = bass
    //   gsd            = zzz
    //
    
    // Columnizer script
    // Takes a multi-row selection and inserts spaces between the words, as necessary, to form columns.
    //
    function getNewColFromCclippord(defaultColumn)
    {
        UltraEdit.selectClipboard(9);
    
        var nColNumber = Number(UltraEdit.clipboardContent);
    
        if (isNaN(nColNumber))
        {
             nColNumber = defaultColumn;
        }
        else if ((nColNumber < 8) || (nColNumber > 3009))
        {
            nColNumber = defaultColumn;
        }
    
        UltraEdit.selectClipboard(0);
    
        return nColNumber;
    }
    
    function columnizeOneLine (oneLine, oldcol, newcol)
    {
        // UltraEdit.outputWindow.write('Move to : "' + kkColNo + '"');
        // UltraEdit.messageBox('Move to : "' + kkColNo + '"');
    
        var len = newcol - oldcol + 1;
    
        if (len > 0)
        {
            var str = new Array(parseInt(len)).join( ' ' );
            // insert required number of spaces to move the part of the string right
            output = [oneLine.slice(0, oldcol), str, oneLine.slice(oldcol)].join('');
            // UltraEdit.outputWindow.write('1): "' + oneLine + '" 2): "' + oldcol + '" 3): "' + newcol + '" >>"' + output + '"' );
        }
    
        return output;
    }
    
    function columnizeLines ()
    {
        var newlineDOS = "\r\n"; // line end boundary
        var newlineUNIX = "\n"; // line end boundary
        var newlineMAC = "\r"; // line end boundary
        var newline = "\r\n"; // default line end boundary
    
        // put the selected txt block into the variable
        var selectionString;
        selectionString = UltraEdit.activeDocument.selection;
    
        // determine the type of newline character that currently exists in this selection
        var docType = UltraEdit.activeDocument.lineTerminator;
    
        if (docType == 0 || docType == -1 || docType == -2)
        {
            newline = newlineDOS;
        }
        if (docType == 1)
        {
            newline = newlineUNIX;
        }
        if (docType == 2)
        {
            newline = newlineMAC;
        }
    
        var newcol = getNewColFromCclippord(80);
    
        var SearchStr = UltraEdit.getString("String/char to start to move to new column:",1);
    
        // break up the selected text at the line breaks and pass this into a new array dimensioned off the splits
        var lineArray = new Array();
        lineArray = selectionString.split(newline);
        var lines = lineArray.length;
    
        var outString = "";
    
        for (var lineIndex = 0; lineIndex < lines; ++lineIndex)
        {
            var oldcol = lineArray[lineIndex].search(SearchStr);
    
            if ( oldcol >= 0 )
            {
                var Line = columnizeOneLine (lineArray[lineIndex], oldcol,newcol);
                outString = outString + Line;
            }
            else
            {
                outString = outString + lineArray[lineIndex];
            }
    
            if (lineIndex < lines - 1)
            {
                outString = outString + newline;
            }
        }
    
        // only write an output back to the file if a selection was made on at least one line of actual text
        if (SearchStr != "" &&  lines >= 1)
        {
            UltraEdit.activeDocument.write(outString);
        }
    }
    
    // Assign to Alt+F4
    columnizeLines();
    
    

    2

      indent block

      Apr 19, 2022#2

          Without selection:
            Executes indentation in a block beginning at the current line until the
            indentation-text found in consecutive lines.

          With selection:
            Executes indentation in the selected lines.

          Parameters:
            - the indentation-char/text must be selected in the current clipboard (clipboards 0-9 can be used)
            - the cursor must be positioned at the indentation-position (it must not be beyond the end of the line!)

          KeyMapping:
            - Alt + Ctrl + I


      Code: Select all

       /*****************************************************************************************************
        --*+FILE:       IndentBlock.js        :FILE+*--
        --*+Beschreibung:
      
          Without selection:
            Executes indentation in a block beginning at the current line until the
            indentation-text found in consecutive lines.
      
          With selection:
            Executes indentation in the selected lines.
      
          Parameters:
            - the indentation-char/text must be selected in the current clipboard (clipboards 0-9 can be used)
            - the cursor must be positioned at the indentation-position (it must not be beyond the end of the line!)
      
          KeyMapping:
            - Alt + Ctrl + I
      
        :Beschreibung+*--
        --*+History:
        +-----------+-----------------+------------+---------------+------------------------------------------
         VERSION     AUTOR             DATUM        SUCHMARKE       BESCHREIBUNG
        +-----------+-----------------+------------+---------------+------------------------------------------
         1.0.0.0     Árpád Szilágyi    13.04.2022   22:56           Erstanlage
        +-----------+-----------------+------------+---------------+------------------------------------------
        :History+*--
        *****************************************************************************************************/
      
      
      //>> Konfig >>------------------------------------------------------------------
      
          var gsDebug = null;             //debug is switched off ("Abbruch!..." will be written...)
      //  var gsDebug = true;             //debug is activated
      //  var gsDebug = false;            //debug is deactivated ("Info: Debug ist deactivated!" and "Abbruch!..." will be written...)
      
      //<< Konfig <<------------------------------------------------------------------
      
      
      function wrDebug (message)
      /*------------------------------------------------------------------------------
      ------------------------------------------------------------------------------*/
      {
      
        if (gsDebug == false) {
          var messageTyp = message.split(' ');
          if (messageTyp [0] == "Abbruch!") {
            UltraEdit.outputWindow.write( message );
          } else {
            UltraEdit.outputWindow.write( "Info: Debug ist deactivated!" );
          }
          gsDebug = null;
        } else if (gsDebug == true) {
      
          UltraEdit.outputWindow.write( "Debug:\t\t".concat(String(message)) );
      
        }
        return;
      }
      
      
      function wrAbbruch (message)
      /*------------------------------------------------------------------------------
      ------------------------------------------------------------------------------*/
      {
        gsDebug = false;
        wrDebug(message)
        return;
      }
      
      
      function setOutputWindow()
      /*------------------------------------------------------------------------------
      OutputWindow options
      ------------------------------------------------------------------------------*/
      {
        if (gsDebug == true || gsDebug == false)
        {
          UltraEdit.outputWindow.showStatus = true; //shows the status (failed/succeded)
          if (!UltraEdit.outputWindow.visible)
          {
            UltraEdit.outputWindow.showWindow(true);
          }
          UltraEdit.outputWindow.showOutput = true; //Determines visibility of output from active script.
          UltraEdit.outputWindow.clear(); //Clears contents of output window.
          //var_dump();
        }
      }
      
      
      
      function nthIndex(str, pat, n)
      /*------------------------------------------------------------------------------
      Ermittelt die Position des Nth Vorkommen von PAT in STR.
      ------------------------------------------------------------------------------*/
      {
        var L= str.length, i= -1;
        while(n-- && i++<L){
          i= str.indexOf(pat, i);
          if (i < 0) break;
        }
        return i;
      }
      
      
      
      function main ()
      /*------------------------------------------------------------------------------
      ------------------------------------------------------------------------------*/
      {
        setOutputWindow();
      
        // define environment for this script
        UltraEdit.activeDocument.hexOff();
        UltraEdit.insertMode();
        if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
        else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
        //
        //gets the first and last line of the selection;
        //if there is a selection at starting this script, only the selected lines will be processed
        //
        var nLineFirst = -1;
        var nLineLast = -1;
        if (UltraEdit.activeDocument.isSel()) {
      
          // Get number of the line on which the selection begins.
          var nLineFirst = UltraEdit.activeDocument.currentLineNum;
          wrDebug("nLineFirst =[" + nLineFirst + "]");
      
          // Get the selected block into a string variable.
          var sLines = UltraEdit.activeDocument.selection;
      
          var sLineTerm = "\r\n";   // Default line terminator type is DOS.
          //array of lines in the selection...
          var asLines = sLines.split(sLineTerm);
          nLineLast = nLineFirst + asLines.length -1;
      
          //Clears any selection in active file.
          //UltraEdit.activeDocument.cancelSelect();
        }
        //wrDebug("nLineFirst/nLineLast =[" + nLineFirst + "/" + nLineLast + "]");
      
      
        //actual cursor line and column
        var lineNumSaved = UltraEdit.activeDocument.currentLineNum;
        var colNumSaved = UltraEdit.activeDocument.currentColumnNum;
        //wrDebug("Cursor-line/col:[" + lineNumSaved + "/" + colNumSaved + "]");
      
        //actual clipboard index and content
        var clipIdxSaved = UltraEdit.clipboardIdx;
        var clipSaved =  UltraEdit.clipboardContent;
        //wrDebug("Clipboard(" + clipIdxSaved + "):[" + clipSaved + "]");
        //----------------------------------------------------------------------------
      
        var sIndentTxt = clipSaved;       //indentation-text
        var nIndentPos = colNumSaved;     //indentation-position - colNumSaved must not be beyond the end of the line!!!
      
      
      
        { //------------------------------
          //check of sIndentTxt: it must not consist of only spaces
      
          //wrDebug("sIndentTxt=[" + sIndentTxt  + "] --> " + sIndentTxt.length );
      
          var sTmp = "                                                                  ";
        //sTmp = sTmp.padEnd(sIndentTxt.length, ' ');
          sTmp = sTmp.slice(-sIndentTxt.length);
          //wrDebug("sTmp=[" + sTmp + "]");
      
          if (sIndentTxt == sTmp) {
            gsDebug = false;
            wrAbbruch("Abbruch! IndentBlock: Der in Clipboard eingefügte Text darf nicht nur aus Leerzeichen bestehen!");
            return;
          }
        } //------------------------------
      
      
      
        var lineNum = UltraEdit.activeDocument.currentLineNum;
        var colNum = UltraEdit.activeDocument.currentColumnNum;
        var tmpLineNum; //help variable to do break in while if gotoLine cannot be executed (if the end of file reached)
      
        //>>while---------------------------------------------------------------------
        //lines will be processed
        //  - until end of file or
        //  - sIndentTxt (content of the actual clipboard) found in line
        //----------------------------------------------------------------------------
      
        var selectedLine;
        var sSearchTxt = sIndentTxt;
        var nSearchTxtPos;
        var nPos;
        var nPosBefore;
        var nPosAfter;
      
        //wrDebug("nIndentPos =[" + nIndentPos + "]");
        //wrDebug("sIndentTxt =[" + sIndentTxt + "]");
      
        while (1 == 1) {
          //searching for sSearchTxt in all consecutive lines; the processing ends if sSearchTxt cannot be found in a line
      
          if (nLineFirst !== -1 && nLineLast !== -1 ) {
            //only the lines in the selection will be processed
            if (lineNum < nLineFirst || lineNum > nLineLast) {
              break;
            }
          }
      
          //wrDebug("-----------------------");
          //wrDebug("lineNum    =[" + lineNum + "]");
          UltraEdit.activeDocument.gotoLine(lineNum, 0); //goto line/column
      
          if (tmpLineNum === undefined) {
           //the first line
           tmpLineNum = UltraEdit.activeDocument.currentLineNum;
      
          } else if (tmpLineNum == UltraEdit.activeDocument.currentLineNum) {
            //at the end of the file gotoLine is not succeded
            break; // --> end of processing
      
          } else {
           //the next line
           tmpLineNum = UltraEdit.activeDocument.currentLineNum;
          }
      
          UltraEdit.activeDocument.selectLine(); //select the whole line
          selectedLine = UltraEdit.activeDocument.selection; //save the selection
      
      
          nPosBefore = -1;
          nPosAfter = -1;
          nSearchTxtPos = -1;
          for (var ix = 1; ix <= 100; ++ix ) {
            //looking for the sSearchTxt in the actual line
            //wrDebug("  ");
      
            nPos = nthIndex(selectedLine, sSearchTxt, ix);
            if (nPos !== -1) {
              nPos = nPos +1; //correction because of the difference between the determination of nIndentPos and nPos
            }
            //wrDebug("Nth nPos =[" + ix + "th " + nPos + "]");
      
      
            if ( nPos == -1 ) {
              //not found at all or not found anymore
              break;
      
            } else if ( nPos <= nIndentPos ) {
              nPosBefore = nPos;
      
            } else if ( nPos > nIndentPos ) {
              nPosAfter = nPos;
              break;
      
            }
          }
          //wrDebug("nPosBefore/nPosAfter=[" + nPosBefore + "]/[" + nPosAfter + "]");
      
          if ( nPosBefore == -1 && nPosAfter == -1 ) {
            if (nLineFirst == -1 && nLineLast == -1) {
              //if sSearchTxt is not found in the line and no selection will be processed, than breaks the loop
              break;
            }
          }
      
          if ( nPosBefore !== -1 && nPosAfter == -1 ) {
            nSearchTxtPos = nPosBefore;
      
          } else if (nPosBefore == -1 && nPosAfter !== -1) {
            nSearchTxtPos = nPosAfter;
      
          } else if (nPosBefore !== -1 && nPosAfter !== -1) {
      
            if (nIndentPos - nPosBefore < nPosAfter - nIndentPos) {
              nSearchTxtPos = nPosBefore;
      
            } else {
              nSearchTxtPos = nPosAfter;
            }
      
          }
          if (nSearchTxtPos !== -1) {
      
            //wrDebug("nPosBefore/nPosAfter --> nSearchTxtPos=[" + nPosBefore + "]/[" + nPosAfter + "] --> " + nSearchTxtPos);
            //wrDebug("lineNum=[" + lineNum + "] --> nSearchTxtPos/nIndentPos =[" + nSearchTxtPos + "]/[" + nIndentPos + "]");
      
      
            //preparation for deleteText()
            UltraEdit.activeDocument.cancelSelect(); //Clears any selection in active file.
      
            //------------------------------
            if (nSearchTxtPos < nIndentPos) {
              //spaces will be inserted from the position (nSearchTxtPos)
              UltraEdit.activeDocument.gotoLine(lineNum, nSearchTxtPos);
      
              for (var i = 0; i < nIndentPos - nSearchTxtPos; ++i) {
                UltraEdit.activeDocument.write(" ");
              }
      
            //------------------------------
            } else if (nSearchTxtPos > nIndentPos) {
              //spaces will be deleted backwards from (nSearchTxtPos -1)
      
      
              for (var ix = nSearchTxtPos - nIndentPos; ix > 0 ; --ix) {
      
                var nCol = nIndentPos +ix -1;
                //wrDebug("nCol =[" + nCol + "]");
      
                UltraEdit.activeDocument.gotoLine(lineNum, nCol);
                //in this case spaces will be deleted to reach the nIndentPos for sIndentTxt
      
                //var char = UltraEdit.activeDocument.currentChar; //Returns value of character at cursor.
                var bSpace = UltraEdit.activeDocument.isChar(' ');
                //wrDebug("bSpace =[" + bSpace + "]");
      
                if (bSpace) {
                  UltraEdit.activeDocument.deleteText();  //Delete current character or selected text (selection is deleted!)
      
                } else {
                  break; //only spaces will be deleted (if there is no other char inbetween)!
                }
              }
            }
          }
          //------------------------------
      
          //go to the next line
          lineNum = lineNum +1;
        }
        //<<while---------------------------------------------------------------------
      
        //----------------------------------------------------------------------------
        //restore cursor position
        UltraEdit.activeDocument.gotoLine(lineNumSaved, colNumSaved);
      
        //restore clipboard  index and content
        UltraEdit.selectClipboard(clipIdxSaved);
        UltraEdit.clipboardContent = clipSaved;
      }
      
      
      main(); //Alt + Ctrl + I
      
      
      IndentBlock.7z (3.01 KiB)   2