Trim Leading spaces

Help with writing and playing macros

Trim Leading spaces

Postby skp » Fri May 08, 2009 12:56 pm

Hello forum,
I am learning macros and I wrote a simple macro to delete leading spaces (I took the help of code posted on this forum).

InsertMode
ColumnModeOff
HexOff
Key HOME
IfCharIs 32
StartSelect
Key Ctrl+RIGHT ARROW
EndSelect
Delete
EndIf
Key DOWN ARROW

The macro works for the current line. But the way I want it to work is to delete the leading spaces of the current line and go to the next line (I used the Key DOWN ARROW). But it doesn't seem to go to the next line. Can someone guide me please? Thanks a lot...
skp
Newbie
 
Posts: 3
Joined: Fri Apr 03, 2009 11:14 am

Re: Trim Leading spaces

Postby pietzcker » Sat May 09, 2009 12:26 am

I wouldn't use a macro for that. The Perl regex ^[ \t]+ (replace all with nothing) will do the same thing much faster.

But I guess this is just a macro programming exercise - however, I've always found the way StartSelect and EndSelect word very confusing, and I can't help you here. Mofi will know what's wrong.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Trim Leading spaces

Postby Mofi » Sat May 09, 2009 11:16 am

Which version of UltraEdit do you use because the macro worked on my computer?

Do you know that when you have selected 1 or more lines and you press Shift+Tab UltraEdit shifts the selected lines one indent to left. How many spaces/tabs an indent is can be configured for all files at once or individually for different groups of files according to their file extensions in the Word Wrap / Tab Settings configuration dialog. See for example extension based tab settings problem.

Trim all leading spaces in whole file or just a selected block is definitely done best with a regular expression replace either using the Perl / Unix expression pietzcker posted or using %[ ^t]+ as search string with the UltraEdit regular expression engine.

Hint:

The position of the cursor after Key HOME depends on configuration setting Home key always goto column 1. Your version works always fine if this setting is enabled. Otherwise you would need additional code like:

Key HOME
IfColNum 1
Else
Key HOME
EndIf


or

Key HOME
IfColNumGt 1
Key HOME
EndIf


or

GotoLine 0 1

Which version you can use depends on your version of UltraEdit.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Trim Leading spaces

Postby skp » Tue May 12, 2009 2:51 pm

Thank you so much pietzcker and Mofi for taking the time to answer my questions and sharing your knowledge.

Perl regular expression seems to be a very good option and I will further dig and learn. Seems a lot can be achieved using this.

Mofi, Shift+Tab is very cool. Thanks a lot for letting me know about this and this is just what I need.

I am using UE version 15.00.0.1046 on XP sp3. I have the Home key always goto column 1 "CHECKED".

Although I was able to achieve what I want with Shift+Tab, I was not able to get the macro to work the way I wanted. It works fine for the current line. But the way I want it to work is to trim the leading spaces for the current line and move to the next line and stop. Moving to the next line is what I am not able to achieve (I have the Key DOWN ARROW for that but that command does not seem to fire).
skp
Newbie
 
Posts: 3
Joined: Fri Apr 03, 2009 11:14 am

Re: Trim Leading spaces

Postby Mofi » Wed May 13, 2009 2:36 am

I tested 2 macro versions to trim leading spaces/tabs in current line and move the cursor down to next line. Both versions worked with UE v15.00.0.1046. The second macro requires macro property Continue if search string not found checked in the macro properties.

Version 1 without using a regular expression replace:

GotoLine 0 1
IfCharIs 32
Else
IfCharIs 9
Else
ExitMacro
EndIf
EndIf
SelectWord
Delete
Key DOWN ARROW


SelectWord selects since UE v10.10 also all contiguous space if the caret is between 2 white-space characters which is also true when the cursor is at start of a line because on the byte stream of the file on left side is also a white-space charater (usually the LF). That works also at top of the file. That makes the selection of the spaces/tabs at start of the line faster and easier.

Version 2 with using a regular expression replace.

GotoLine 0 1
SelectWord
PerlReOn
Find RegExp SelectText "[ \t]+"
Replace ""
IfFound
Key DOWN ARROW
Else
GotoLine 0 1
EndIf
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Trim Leading spaces

Postby skp » Wed May 13, 2009 3:17 pm

Mofi,
Thank You so much.... Your macro worked perfect...
I have to learn more and get used to writing them...Thanks again for all your help :D
skp
Newbie
 
Posts: 3
Joined: Fri Apr 03, 2009 11:14 am


Return to Macros