indent macro to align columns by = char

indent macro to align columns by = char

344
MasterMaster
344

    Dec 20, 2004#1

    Hello guys,

    I'm trying to write an indent macro with UltraEdit (9.10) that stuffs a SELECTION so that all equal signs are under 1 column:

    Example: Assume that those 5 lines are selected:

    Code: Select all

       int iReturn = KO_GIG_FALSE;
       int iAvStatus = NULL_INTEGER;
       int iDoktKey   = NULL_INTEGER;
       int iZeilen = GetNumRowsBO(pboDokOut, DOKUMENT_BO_DOK_T_DOKT_KEY);
       int iLauf = NULL_INTEGER;
    We will see ALL = signs in the same column after macro run:

    Code: Select all

       int iReturn    = KO_GIG_FALSE;
       int iAvStatus  = NULL_INTEGER;
       int iDoktKey   = NULL_INTEGER;
       int iZeilen    = GetNumRowsBO(pboDokOut, DOKUMENT_BO_DOK_T_DOKT_KEY);
       int iLauf      = NULL_INTEGER;
    The approach is clear: Go through selection and determine maximum column of "=" sign.
    Go through selection once more and add the needed number of blanks before equal sign.

    Any help appreciated.
    Thanks, Alex

    21
    Basic UserBasic User
    21

      Jan 19, 2005#2

      Yes you can do this UE. I have macros that do similar things.

      The only assumptions needed are your UE is set to allow positioning beyond end of line. And that the user has a text block selected.

      One key in writing this is that you program subroutines. If you try to program a single macro to do this you'll have a monster that is a nightmare to write and a nightmare to debug. 

      Step 1: Make scratch file of just leading text.
      • I use clipboard 9 to insulate ourselves from user.
      • Add a blank line to file so we know that we can write loops and do EOL searches.
      • The expression search replaces the "=" sign and all text that follows with nothing.
      Here is what that macro would look like.

      Code: Select all

      Clipboard 9  
      Cut 
      NewFile
      Paste 
      "
      "
      Find RegExp "^=*$"
      Replace All ""
      CONGRATS you have just built a variable file that contains JUST the leading text before equal sign.
       
      Step 2: Determine max length line of file, put cursor in that column.

      What we do is write a loop such that when get to the LAST line of file our cursor is guaranteed to be a the col position of the max length line in file. This is easy.

      In a loop we do a down arrow, insert a space and then delete this space. We then press key END.

      The effect of the above is simple. If the line we are on now was shorter than the previous line it now is blank padded to the previous lines length. If the line we are on now was longer than the previous line then the cursor will now be at this lines length.

      We do this in a loop so by the time we hit end of file whatever column our cursor is in, that is guaranteed to be the max col length of the file.

      Here is what that macro would look like:

      Code: Select all

      Top
      Key END
      Loop 
      IfEof
      ExitLoop
      EndIf
      " "
      Key BACKSPACE
      Key END
      Key DOWN ARROW
      EndLoop
      " "
      Key BACKSPACE
      Key DEL
      Step 3: Make file fixed length.

      Because we have just guaranteed that the last line is the longest line of file all we need to do is cut/paste the entire file in COLUMN mode. When we do this, 
      UE will automatically blank pad EVERY line to the size defined by the rectangle being used. Which in our case, the rectangle is as wide as the longest line. The next result will be a fixed length file.

      Code: Select all

      Top
      ColumnModeOn
      SelectToBottom
      StartSelect
      Key Ctrl+END
      ColumnModeOff
      ColumnModeOn
      Cut 
      Paste
      Step 4: Make scratch file of just TRAILING text.

      Make a macro exactly like step 1, the only change is now we do an expression search & replace of %*^= and replace with =. This will make a scratch file containing JUST the TRAILING text because what we are doing is searching from start of line to an "=" sign and replacing that text with an equal sign.

      Step 5: Format this trailing text and copy into paste buffer. 

      Now use the step 2 technique and get cursor on last line of file at max col position.

      Then use the step 3 technique to get this file fixed length.

      At this point our PASTE buffer contains what we want.  

      The file we are now in is useless to us. What we want is in our paste buffer. I have not found a clean way to get rid of a scratch window. The best method I've found is to simply save file to a dummy location and then close the window. Because I'm saving and closing a file, it is my preference to make it an empty file because as I said, this window is useless for us and we simply want it to go away.

      Regardless of whether you save an empty file or file with text, the only new macro code you need is:

      Code: Select all

      SaveAs "c:\x.x"
      CloseFile
      Step 6: Join the leading and trailing text.

      When the above step finished, UE has no other choice than to bring you back to the last window you were in which happens to be the first scratch file window you made. This window contains all the LEADING text and is fixed length. Our paste buffer contains all the TRAILING text and is fixed length.
      all we need to do is paste the two together in COLUMN mode.

      Code: Select all

      Top
      ColumnModeOn
      Key END
      Paste
      CONGRATS! You now have that paragraph formatted exactly as user desired.
           
      Step 7: Copy text to paste buffer, get rid of scratch window.
       
      Put editor in normal mode select all and cut. We no longer want this window so do same technique as before save to a dummy location and close file.
       
      Step 8: Paste our formatted text back out and were DONE.
       
      When the above step finished, UE has again no other choice than to bring you back to the last window you were in, which happens to be the ORIGINAL window. Our cursor is already exactly where the original paragraph was located. Put editor back to "NORMAL" mode and paste selection.

      Enjoy.

      46
      Basic UserBasic User
      46

        Jan 20, 2005#3

        Nice job oracledba,

        Your method expect also that there is no line without "=" in the selected paragraph.

        To close a file without saving just use the macro command CloseFile NoSave. (It works at least with UE 10.20. I don't have UE 9.x any more to try.)

        21
        Basic UserBasic User
        21

          Jan 20, 2005#4

          Thanks for the tip palou. I'll give that a try in my macros as my kludge works but isn't ideal.

          You bring up a good point on what would occur to lines with no delimiter.

          Sure seems like the best solution would be back in step 1 PRIOR to the CUT add an expression search replace to suffix on an equal sign to every row.

          That would make step 1 look like this:

          Code: Select all

          Clipboard 9
          Find RegExp "$"
          Replace All "^="
          Cut
          NewFile
          Paste
          "
          "
          Find RegExp "^=*$"
          Replace All ""
          And in turn prior to putting text back into main window, step 7 would need to start off with removing the delimiter we added.

          Code: Select all

          Find RegExp "^=$"
          Replace All ""