Align current line to cursor position (with spaces)

Align current line to cursor position (with spaces)

344
MasterMaster
344

    Feb 08, 2006#1

    Hi guys,

    I'm trying to write a macro that aligns (indents) the current line to the given cursor-position (and then moves the cursor 1 line down
    to do the same stuff eventually again).
    I only work with spaces in the files (autocorrect tabs to spaces), so this is a given fact.
    The problem is to memorize the current cursor position and the unablity of UE to store variables or "find in selected text only".
    - "no variable" perhaps means to work with many

    Code: Select all

    ifcol 1 "" ifcol 2 " " ifcol 3 "  " 
    
    -constructs etc. ...
    - "find select" does not search in selected area as supposed, it finds and THEN selects the area.
    Also, I might have to stuff or remove spaces at the beginning of the line, depending on how the line looks like
    and the cursor is positionend:

    Code: Select all

         THIS LINE HAS 5 SPACES and CURSOR IS POSITIONED IN COLUMN 10 BEFORE MACRO RUNS
              THIS LINE HAS 5 SPACES and CURSOR IS POSITIONED IN COLUMN 10 BEFORE MACRO RUNS  <AFTER MACRO run>
    
         THIS LINE HAS 5 SPACES and CURSOR IS POSITIONED IN COLUMN 3 BEFORE MACRO RUNS
       THIS LINE HAS 5 SPACES and CURSOR IS POSITIONED IN COLUMN 3 BEFORE MACRO RUNS  <AFTER MACRO run>
    
    I already have a macro that aligns (indents) the current line as THE LINE ABOVE to solve a similar problem, but this is more easy.
    I use a find 1st NONSPACE REGEXP of line above, copy the spaces and paste them one line below.

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    UnixReOn
    Clipboard 9
    Key UP ARROW
    Key END
    IfColNumGt 1
    PlayMacro 1 "KEY_HOME"
    StartSelect
    Find RegExp "\S"
    IfFound
    Key LEFT ARROW
    EndIf
    Copy 
    EndSelect
    EndIf
    Key DOWN ARROW
    Key END
    IfColNumGt 1
    PlayMacro 1 "KEY_HOME"
    StartSelect
    Find RegExp Select "\S"
    IfFound
    Key LEFT ARROW
    EndIf
    Paste 
    EndIf
    Key DOWN ARROW
    PlayMacro 1 "setDefaults"
    
    Any ideas how to solve the current cursor-position problem ? Moooooofi !?!? :-)

    rds Bego
    Normally using all newest english version incl. each hotfix. Win 10 64 bit

    6,675585
    Grand MasterGrand Master
    6,675585

      Feb 09, 2006#2

      In God and Mofi you can trust. Here is your align macro with explanation:

      InsertMode
      ColumnModeOff
      HexOff
      IfColNumGt 1
      StartSelect
      Key HOME
      Clipboard 9
      Cut
      Paste
      ColumnModeOn
      Paste
      ColumnModeOff
      ClearClipboard
      Clipboard 0
      IfCharIs 32
      StartSelect
      Key Ctrl+RIGHT ARROW
      EndSelect

      Key DEL
      EndIf
      OverStrikeMode
      Loop
      IfColNum 1
      ExitLoop
      Else
      Key LEFT ARROW
      " "
      Key LEFT ARROW
      EndIf
      EndLoop
      InsertMode
      Key Ctrl+RIGHT ARROW
      Else
      IfCharIs 32
      StartSelect
      Key Ctrl+RIGHT ARROW
      EndSelect

      Key DEL
      EndIf
      EndIf

      Edited: This macro only works correct with option Home Key Always Goto Column 1 is activated!

      If current cursor position is greater than 1 the macro first selects everything from current cursor position to start of the line, cut it to user clipboard 9 and paste it back. The cursor is again were it was at the beginning.

      Now the important trick: switch to column mode and paste it again. The cursor will not change on paste in column mode! I often use this trick if I want to paste something without changing the cursor position (key sequence: Alt+C, Ctrl+V, Alt+C).

      Next check if the character at the cursor position after second paste is a space. If it is a space select all spaces til first non-white character. This could be also done with following regular expression search instead of the green marked commands:

      For UltraEdit style: Find RegExp " +"
      For Unix style: Find RegExp " *"

      If you want to use the find, don't forget to add UnixReOff or UnixReOn at beginning of the macro which is missing in my macro because no search is needed.

      The selected spaces are now deleted. So the current line is aligned to the cursor position. The following simple loop overwrites all characters from the column before current cursor position to the start of the line.

      The last command sets the cursor to first non-white character which sets the cursor to the position as it was before starting the macro.

      If the cursor is at start of the line before macro execution, the alignment is much easier. In this case only all spaces at start of the line must be deleted to align the line to column 1.

      Note for other users: This macro will not work for tabs as indent character, only for lines with space as indent character!
      Best regards from an UC/UE/UES for Windows user from Austria

      344
      MasterMaster
      344

        Feb 09, 2006#3

        Hi god,

        1st, THANX very much! I KNEW somehow it should work, but wasn't able to do it myself.
        The macro works exact the way I want it to :-)

        Just one MINOR issue: if the cursor is just before the first non-space character of the line, it doesn't work. Nothing should be done here, but it smashes the lines.
        Reason:

        IfColNumGt 1
        StartSelect
        Key HOME
        IfColNumGt 1

        StartSelect

        Key HOME
        EndIf

        In this block, no leading spaces are selected here.
        With the red line, it works but shifting to the right is no more possible....
        I decided to leave it as is, because it makes no sense "shifting the line to the same position" :-)

        ---> Many greetz Mofi !

        Bego
        Normally using all newest english version incl. each hotfix. Win 10 64 bit

        6,675585
        Grand MasterGrand Master
        6,675585

          Feb 09, 2006#4

          I'm not God! You can only trust in me like in God. The bug of my macro is the proof that I'm only a fallible human.

          I now have looked how the HOME key works with option Home Key Always Goto Column 1 is not checked. I didn't know that HOME pressed if already in column 1 moves the cursor right to first non-space character. This has the bad effect you described here.

          To solve this problem, I have modified the macro so it now selects to start of the line with a regular expression search or with select to top of the file, if you run it on the first line of the file. But now the macro is not indepedent anymore for the UltraEdit/Unix regular expression setting.

          Users which always use HOME to column 1 setting should use the modified macro of my first post.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          IfColNumGt 1
          Find RegExp Up Select "%"
          IfNotFound
          SelectToTop
          EndIf

          Clipboard 9
          Cut
          Paste
          ColumnModeOn
          Paste
          ColumnModeOff
          ClearClipboard
          Clipboard 0
          IfCharIs 32
          StartSelect
          Key Ctrl+RIGHT ARROW
          EndSelect
          Key DEL
          EndIf
          OverStrikeMode
          Loop
          IfColNum 1
          ExitLoop
          Else
          Key LEFT ARROW
          " "
          Key LEFT ARROW
          EndIf
          EndLoop
          InsertMode
          Key Ctrl+RIGHT ARROW
          Else
          IfCharIs 32
          StartSelect
          Key Ctrl+RIGHT ARROW
          EndSelect
          Key DEL
          EndIf
          EndIf
          UnixReOn

          Remove the last red command, if you use regular expression in UltraEdit style by default instead of Unix style.
          For UltraEdit v11.10c and lower see Advanced - Configuration - Find - Unix style Regular Expressions.
          For UltraEdit v11.20 and higher see Advanced - Configuration - Searching - Unix style Regular Expressions.
          Macro commands UnixReOn/UnixReOff modifies this setting.
          Best regards from an UC/UE/UES for Windows user from Austria

          344
          MasterMaster
          344

            Feb 09, 2006#5

            ok, the ultimate solution now :-)

            Me, I like the "HOME is not always POS1"-setting for really jumping to the 1st word.
            Pressing it twice jumps to Pos1 anyway.

            Hollaraiduljioeh ... Bego :D

              Feb 09, 2006#6

              Hey Mofi !

              Look at this !!!! Seems to be much quicker ;-)
              I found another solution than the " "-loop :-)
              Wut do u think ?

              UnixReOn is needed for the \S (non Whitespace) characters.

              rds Bego

              Code: Select all

              InsertMode
              ColumnModeOff
              HexOff
              UnixReOff
              IfColNumGt 1
              Find RegExp Up Select "%"
              IfNotFound
              SelectToTop
              EndIf
              Clipboard 9
              Cut 
              Paste 
              ColumnModeOn
              Paste 
              ColumnModeOff
              ClearClipboard
              Clipboard 0
              IfCharIs 32
              StartSelect
              Key Ctrl+RIGHT ARROW
              EndSelect
              Key DEL
              EndIf
              UnixReOn
              StartSelect
              Key HOME
              Find RegExp "([\S])"
              Replace All SelectText " "
              Key Ctrl+RIGHT ARROW
              
              Normally using all newest english version incl. each hotfix. Win 10 64 bit

              6,675585
              Grand MasterGrand Master
              6,675585

                Feb 09, 2006#7

                Bego, you are right. The loop can be replaced by a regular expression which is faster. I thought about it yesterday as I developed the macro, but decided me for the loop version because I wanted to avoid a regular expression search. After you have reported your problem caused by the HOME key function which can be solved only with a regular expression search, this second regular expression is really better than the loop. Thanks for remembering me on the replace approach.

                Here is the complete macro with UltraEdit style regular expression:

                InsertMode
                ColumnModeOff
                HexOff
                UnixReOff
                IfColNumGt 1
                Find RegExp Up Select "%"
                IfNotFound
                SelectToTop
                EndIf
                Clipboard 9
                Cut
                Paste
                ColumnModeOn
                Paste
                ColumnModeOff
                ClearClipboard
                Clipboard 0
                IfCharIs 32
                StartSelect
                Key Ctrl+RIGHT ARROW
                EndSelect
                Key DEL
                EndIf
                StartSelect
                Key HOME
                Find RegExp "[~ ]"
                Replace All SelectText " "
                EndSelect
                Key Ctrl+RIGHT ARROW
                Else
                IfCharIs 32
                StartSelect
                Key Ctrl+RIGHT ARROW
                EndSelect
                Key DEL
                EndIf
                EndIf

                Users which prefer Unix style for regular expressions should use

                On instead of Off
                ^ instead of %
                \S instead of [~ ]
                Best regards from an UC/UE/UES for Windows user from Austria

                11
                Basic UserBasic User
                11

                  Feb 09, 2006#8

                  Here is yet another version of a Align Line at Cursor Macro. The main advantages over Mofi's macro.
                  - The cursor is moved down automatically.
                  - The changes can be undone. The other macros use a Paste after switching to and from ColumnMode - Ultraedit loses it's undo-buffer in that case.
                  - The macro works even after moving down to a line which is shorter than the alignment column

                  Here is the code. I added some coments for better understanding. These must of course be removed when you actually paste the code into the Macro Editor.

                  Simple assertions reflecting the current state are enclosed in curly brackets; block-comments are prefixed with //

                  Code: Select all

                  InsertMode
                  ColumnModeOff
                  HexOff
                  // I use UE-style regex
                  // see Mofi's post above for the change for UNIX-style regex
                  UnixReOff
                  // short-cut: If we are already at column 1, we simply need to cut the leading
                  // spaces and go down - simple
                  IfColNum 1
                    // trim leading spaces
                    Loop 
                      IfCharIs 32
                        Key DEL
                      Else
                        ExitLoop
                      EndIf
                    EndLoop
                    Key DOWN ARROW
                    ExitMacro
                  EndIf
                  {  Colum > 1  }
                  StartSelect
                  Key HOME
                  Key HOME
                  // This if-condition is required to overcome a bug in UE: 
                  // When "Home Key Always Goto Column 1" is unchecked, two subsequent HOME normally
                  // bring you to column 1. However, if the cursor is ALREADY at the first non-blank
                  // character, the first HOME brings you to Column 1 and the second back to the
                  // previous position.
                  // You should be able to detect that using Key HOME / IfColNumGt 1 / Key HOME / EndIf
                  // Unfortunately this doesn't work either, because UE does not report the correct
                  // cursor-position in selection-mode (i.e. after a Start Select); instead it 
                  // always uses the position the cursor was at when the StartSelect was issued.
                  IfSel
                    Clipboard 9
                    Copy 
                    Find RegExp "?"
                    Replace All SelectText " "
                    Clipboard 8
                    Copy 
                    Paste 
                    EndSelect
                    // enter a new line
                    "
                    "
                    Clipboard 9
                    Paste 
                    Clipboard 0
                    Key HOME
                    Key HOME
                    // trim leading spaces
                    Loop 
                      IfCharIs 32
                        Key DEL
                      Else
                        ExitLoop
                      EndIf
                    EndLoop
                    Key BACKSPACE
                  Else
                    // The cursor is at the first non-blank position and this is our alignment-column
                    EndSelect
                  EndIf
                  //
                  // we are done aligning the current line. Now go to the line below ...
                  Key DOWN ARROW
                  //
                  // Again a short-cut: If we are at Column 1 we do not need the stuff below
                  IfColNum 1
                    ExitMacro
                  EndIf
                  // This is the tricky part to correctly align lines which are SHORTER than our
                  // alignment-column.
                  // NOTE: This WILL add trailing spaces to the line
                  IfCharIs 13
                  Else
                  // we are not at the end-of-the line, so this line is NOT shorter than our
                  // alignment position; again we can leave right away
                  ExitMacro
                  EndIf
                  {  current line is shorter than alignment-column  }
                  Key UP ARROW
                  StartSelect
                  Loop 
                    {  in previous line  }
                    Key LEFT ARROW
                    Key DOWN ARROW
                    {  in next line to be aligned  }
                    IfCharIs 13
                      // current line is still shorter than alignment-column; repeat steps above
                      Key UP ARROW
                    Else
                      // we reached the last character in our current too-short line. Now we go
                      // back to the line above ...
                      Key RIGHT ARROW
                      Key UP ARROW
                      // and find our selection to be just as long as we need to pad the line below
                      // so we save this stuff ...
                      Clipboard 9
                      Copy 
                      // ... blank it 
                      Find RegExp "?"
                      Replace All SelectText " "
                      Clipboard 8
                      Copy 
                      // and restore it
                      Clipboard 9
                      Paste 
                      Key DOWN ARROW
                      // back at the too-short line below, we simply paste our padding blanks
                      Clipboard 8
                      Paste 
                      Clipboard 0
                      // ... and leave
                      ExitLoop
                    EndIf
                  EndLoop
                  

                  344
                  MasterMaster
                  344

                    Feb 10, 2006#9

                    Hi DarkTurok,

                    thanx 4 ur post and taking part in our macro-competition ;-)
                    The undo-issue really is nice...
                    Time will tell which macro I will prefer. Still my favorite is the fast one ...

                    rds Bego :D
                    Normally using all newest english version incl. each hotfix. Win 10 64 bit

                    6,675585
                    Grand MasterGrand Master
                    6,675585

                      Feb 11, 2006#10

                      Hi DarkTurok and Bego!

                      This macro gets better and better!

                      I now have put the macro below and the macro at Macro that indents lines as the line before with hotkeys to my Automac.mac macro file because these align macros are very useful for programmers working with UltraEdit or UEStudio which uses only spaces for indentation.

                      Thanks DarkTurok for showing us how to align the line without switching to column mode and so don't break the undo chain. And also for the IfSel trick for the special handling of HOME key when the cursor is already on first non-white character of the line and the HOME key option is not set to go always to column 1.

                      This macro forum topic is well for macro newbies for learning. They can see here how macro experts are developing their macros.

                      I have taken the macro of DarkTurok and added some improvements. The cursor down now also works if the last line is aligned by the macro indepedent of how the last line is terminated (with or without CRLF). The IfEof code sequences (except first one) are responsible for this special handling on last line. I also replaced all loops with the faster Key Ctrl+RIGHT ARROW code sequences which also creates less undo steps.

                      I have tested the macro with both settings of option Home Key Always Goto Column 1 at Editor - Miscellaneous and also with both settings of option Allow Positioning Beyond Line End at Editor Display - Cursor/Caret.

                      This macro now has 1 regular expression replace in UltraEdit style. To use the macro with Unix style, use On instead of Off and a point . instead of the question mark ?.

                      This macro is indepedent of macro property Continue if a Find with Replace not found because it always finds a character which is replaced by a space.

                      Last I have eliminated all ExitMacro commands except first one so it's possible to pack the whole macro into a loop which aligns all lines till end of file according to the cursor position in the current line. This could be useful if someone wants to align a block of lines by copying the block to a temporary new file, sets the cursor in first line to the align position and runs the macro with a loop without loop number. The result of this action would be a completely aligned block which now must be only copied back from the temporary file buffer to the source file.

                      Here is the ready to use macro code. An indented and commented version for better understanding is below.

                      IfEof
                      ExitMacro
                      EndIf
                      InsertMode
                      ColumnModeOff
                      HexOff
                      IfColNum 1
                      IfCharIs 32
                      StartSelect
                      Key Ctrl+RIGHT ARROW
                      EndSelect
                      Delete
                      EndIf
                      Key END
                      IfEof
                      "
                      "
                      Else
                      Key HOME
                      Key DOWN ARROW
                      EndIf
                      Else
                      StartSelect
                      Key HOME
                      Key HOME
                      IfSel
                      Clipboard 9
                      Copy
                      UnixReOff
                      Find RegExp "?"
                      Replace All SelectText " "
                      Clipboard 8
                      Copy
                      Paste
                      EndSelect
                      "
                      "
                      Clipboard 9
                      Paste
                      ClearClipboard
                      Key HOME
                      Key HOME
                      IfCharIs 32
                      StartSelect
                      Key Ctrl+RIGHT ARROW
                      EndSelect
                      Delete
                      EndIf
                      Key BACKSPACE
                      Else
                      EndSelect
                      EndIf
                      Clipboard 8
                      Key END
                      IfEof
                      "
                      "
                      Paste
                      Else
                      Key HOME
                      IfCharIs 32
                      Key Ctrl+RIGHT ARROW
                      EndIf
                      Key DOWN ARROW
                      IfCharIs 13
                      Key LEFT ARROW
                      Key RIGHT ARROW
                      Key UP ARROW
                      IfCharIs 32
                      StartSelect
                      Key Ctrl+RIGHT ARROW
                      Copy
                      EndSelect
                      Key DOWN ARROW
                      Paste
                      Else
                      Key DOWN ARROW
                      EndIf
                      Else
                      IfEof
                      Paste
                      EndIf
                      EndIf
                      EndIf
                      ClearClipboard
                      Clipboard 0
                      EndIf


                      Macro above again but now with indents and // comments for better reading and understanding.

                      Code: Select all

                      IfEof               // If the macro is executed on end of file do nothing and
                       ExitMacro          // exit. Without this condition it would create a new line
                      EndIf               // and so the macro could not be used in an "endless" loop.
                      InsertMode
                      ColumnModeOff
                      HexOff
                      
                      IfColNum 1          // If current cursor position is at start of the line,
                                          // only leading spaces must be deleted from the current
                       IfCharIs 32        // line which can be done fast with selecting all spaces
                        StartSelect       // till first word and delete the selection.
                        Key Ctrl+RIGHT ARROW
                        EndSelect
                        Delete
                       EndIf
                       Key END            // If this line is the last line of the file and the last
                       IfEof              // line is not terminated with CRLF then add a line break.
                        "
                        "
                       Else               // If this line is not the last line, go back to
                        Key HOME          // column 1 and simply move the cursor down once.
                        Key DOWN ARROW
                       EndIf
                      
                      Else                // The cursor is not at start of the line.
                                          // So a real line alignment must be done.
                       StartSelect        // Select everything from current cursor position to start
                       Key HOME           // of the line. If option Home Key Always Goto Column 1 is
                       Key HOME           // not set and the cursor was already set at first non-white
                                          // character of the line nothing would be selected.
                       IfSel
                        Clipboard 9       // Copy text before current cursor position to user clipboard
                        Copy              // because it could contain non-white characters which must
                        UnixReOff         // be kept and replace all characters of the selection by
                        Find RegExp "?"   // the space character.
                        Replace All SelectText " "
                        Clipboard 8       // Copy the "cleaned" selection to user clipboard 8 and
                        Copy              // paste it over the current selection to bring the cursor
                        Paste             // back to it's initial position before macro execution.
                        EndSelect
                        "                 // Insert a line break which temporarily moves rest
                        "                 // of the line on right cursor side to a new line.
                        Clipboard 9       // Paste original content before the cursor
                        Paste             // position at start of this temporary line.
                        ClearClipboard    // Clipboard 9 will not be used anymore so clear it's content.
                        Key HOME          // Set cursor to column 1. In this situation 2 HOME will
                        Key HOME          // always work even if HOME key is not set to go always
                        IfCharIs 32       // to column 1 and delete all leading spaces.
                         StartSelect
                         Key Ctrl+RIGHT ARROW
                         EndSelect
                         Delete
                        EndIf             // The line is now correct restored without leading spaces
                        Key BACKSPACE     // and so only a backspace is needed to bring the line
                       Else               // up to the line with correct amount of indent spaces.
                        EndSelect
                       EndIf
                      
                       Clipboard 8        // Clipboard 8 which still contains the spaces needed
                       Key END            // for correct alignment from start of the line till
                       IfEof              // the cursor position is selected and cursor is moved
                        "                 // to end of the line. If this is also the end of the
                        "                 // file, insert a line break and the indent spaces.
                        Paste
                       Else               // This is not the last line of the file without a
                        Key HOME          // CRLF. So set cursor back to it's original position.
                        IfCharIs 32       // If HOME always moves the cursor to column 1!
                         Key Ctrl+RIGHT ARROW
                        EndIf
                        Key DOWN ARROW    // Back at correct cursor position now move cursor to
                        IfCharIs 13       // next line. If the cursor is at the end of the line
                         Key LEFT ARROW   // (character is CR) move cursor left/right so UltraEdit
                         Key RIGHT ARROW  // forgets previous cursor position in the line above
                         Key UP ARROW     // and move the cursor up to the aligned line again.
                         IfCharIs 32      // Is the cursor on a new position left to the original
                          StartSelect     // cursor position before macro execution then copy all
                          Key Ctrl+RIGHT ARROW  // spaces from current to first word of the line and
                          Copy            // add it at end of the next line to create the trailing
                          EndSelect       // spaces needed to hold the cursor in the correct column.
                          Key DOWN ARROW  // This is always done if next line was too short.
                          Paste
                         Else
                          Key DOWN ARROW  // Next line is short but has exactly the correct length.
                         EndIf
                        Else              // The cursor in the next line is not at a CR character.
                         IfEof            // Maybe the aligned line was the last line which was
                          Paste           // terminated with a CRLF so only the correct amount
                         EndIf            // of indent spaces still saved in clipboard 8 must
                        EndIf             // be inserted. If the cursor is not at end of the file,
                       EndIf              // the cursor is at the correct column in the next line
                                          // which is fortunately already long enough.
                       ClearClipboard     // Clear now content of clipboard 8 and choose
                       Clipboard 0        // windows clipboard before macro exits.
                      
                      EndIf
                      Best regards from an UC/UE/UES for Windows user from Austria

                      11
                      Basic UserBasic User
                      11

                        Feb 12, 2006#11

                        Hi Mofi!

                        Great job improving this one. Oddly enough, I never thought of using the CTRL+RIGHT ARROW-sequence for selecting and deleting consecutive whitespace - this will speed up a lot of my ohter macros a lot.

                        BTW, I notified IDM about the IfColNum/IfColNumGt x issue when selection-mode is active. They said to look into it.

                        344
                        MasterMaster
                        344

                          Feb 13, 2006#12

                          Hi u 2 dudes.

                          No more improvements from me. Mission accomplished :-)

                          was fun, rds Bego
                          Normally using all newest english version incl. each hotfix. Win 10 64 bit