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
UnixRe
Off
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