indent macro to align columns by = char

Help with writing and playing macros

indent macro to align columns by = char

Postby Bego » Mon Dec 20, 2004 9:35 am

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:

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;

after macro run, we will see ALL = signs in the same column:
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;

:cry: we should use non prop font to see the result :?

Don't know how to get this.

The approach is clear: go through selection and determin max. col. of "=" sign.
go through selection once more and add the needed number of blanks before =

Any help apperciated.
Thanks, Alex
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: indent macro to align columns by = char

Postby Mofi » Mon Dec 20, 2004 10:44 am

You have to use [code][/code] to write preformated text in this forum.

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;


I guess, nobody can solve this with UltraEdit's macro language without doing the space inserting with an external tool because UltraEdit's macro language does not support variables and complex if statements. But maybe someone find a solution!?
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: indent macro to align columns by = char

Postby oracledba » Wed Jan 19, 2005 4:36 pm

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 user has a paragraph of text highlighted.

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 thats 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 searchs
* the expression shearch replaces the "=" sign and all text that follows with nothing.
Here is what that macro would look like.
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 do a 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...

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 net result will be a fixed length file.

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
expresion search replace of %*^= and replace with =
this will make a stracth file containg JUST the TRAILING text because what we are doing is searching from start of line to an "=" sign and replaceing 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.
Now use the step 3 technqiue 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 its my preferance to make it an empty file because I as I said, this window is useless to us now 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...

SaveAs "c:\x.x"
CloseFile

step 6 join the leading and trailing text
========
When the above step finished, UE has no choice but 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.

Top
ColumnModeOn
Key END
Paste


CONGRATS you now have that paragraph formated 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 no choice but to bring you back to
the last window you were in, which happens to be the ORIGNAL window.
Our cursor is already exactly where the original paragraph was located.
Put editor back to "NORMAL" mode and paste selection


Enjoy.
User avatar
oracledba
Basic User
Basic User
 
Posts: 24
Joined: Wed Jan 19, 2005 12:00 am

Re: indent macro to align columns by = char

Postby palou » Thu Jan 20, 2005 2:28 am

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" (at least it works
with Ue 10.20, I don't have Ue 9.x any more to try)
User avatar
palou
Basic User
Basic User
 
Posts: 46
Joined: Fri Dec 17, 2004 12:00 am
Location: Geneva / Switzerland

Re: indent macro to align columns by = char

Postby oracledba » Thu Jan 20, 2005 10:59 am

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 step1 PRIOR to the CUT add in a expression search replace to suffix on an equalsign to every row. That would make step 1 look like this...

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...
Find RegExp "^=$"
Replace All ""
User avatar
oracledba
Basic User
Basic User
 
Posts: 24
Joined: Wed Jan 19, 2005 12:00 am


Return to Macros