Snippets with macro

Help with writing and playing macros

Snippets with macro

Postby mathmax » Fri May 25, 2007 11:36 am

Hello,

I've made a macro that enclose the instructions if, for, while,...
For each of them, I've created a template.
Then, I created a macro :

Code: Select all
HexOff
Key LEFT ARROW
SelectWord
Find "if"
Replace All SelectText "if"
IfFound
Template 0
Else
Find "case"
Replace All SelectText "case"
IfFound
Template 1
Else
Find "for"
Replace All SelectText "for"
IfFound
Template 2
Else
Find "while"
Replace All SelectText "while"
IfFound
Template 3
Else
Find "function"
Replace All SelectText "function"
IfFound
Template 4
Else
Find "procedure"
Replace All SelectText "procedure"
IfFound
Template 5
Else
Template 6
EndIf
EndIf
EndIf
EndIf
EndIf


I have to write the instruction and execute the macro just after.
It works when there is no tab character before the instruction. But if there is a TAB (or a whitespace), it doesn't indent the second line of the template. So it look like something like this :

Code: Select all
      if ()
endif


Is there a way to align the second line with the first one ?
I would rather prefer to keep the template (i.e not to write all the snippets in the macro). Is it possible ?

Thank you in advance for your help.

mathmax
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: Snippets with macro

Postby Mofi » Sun May 27, 2007 12:26 pm

Yes, it is possible, if you change the macro code to this one:

HexOff
Key LEFT ARROW
SelectWord
Find "if"
Replace All SelectText "if"
IfFound
Delete
StartSelect
Key HOME
Template 0
ExitMacro
EndIf
Find "case"
Replace All SelectText "case"
IfFound
Delete
StartSelect
Key HOME
Template 1
ExitMacro
EndIf
Find "for"
Replace All SelectText "for"
IfFound
Delete
StartSelect
Key HOME
Template 2
ExitMacro
EndIf
Find "while"
Replace All SelectText "while"
IfFound
Delete
StartSelect
Key HOME
Template 3
ExitMacro
EndIf
Find "function"
Replace All SelectText "function"
IfFound
Delete
StartSelect
Key HOME
Template 4
ExitMacro
EndIf
Find "procedure"
Replace All SelectText "procedure"
IfFound
Delete
StartSelect
Key HOME
Template 5
Else
Delete
StartSelect
Key HOME
Template 6
EndIf


And if you have defined your templates as shown below for example for the if condition.

[$replace$]if (^)
[$replace$]endif



How it works: The macro deletes the keyword after identifying it. So the current line should contain only the indenting spaces/tabs which are selected next by the macro. It is also possible that nothing is selected. Last the template is inserted with the selected spaces/tabs inserted at start of every line in the template because of special code [$replace$]. ^ marks the cursor position after inserting the template.

With this method you have created templates with auto indent because the macro selects the indenting spaces/tabs before inserting the template.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Snippets with macro

Postby mathmax » Sun May 27, 2007 8:16 pm

Hi, thank you very much for the macro. It works fine. However, I still have a little problem. The macro doesn't work with instructions such as "do while" which are composed of several words. Is it possible to modify the macro so that it works with such instructions too ?

Thank you for you help
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: Snippets with macro

Postby Mofi » Tue May 29, 2007 6:15 am

Replace at top of the macro

Key LEFT ARROW
SelectWord


by

Key HOME
IfCharIs 32
Key CTRL+RIGHT ARROW
EndIf
IfCharIs 9
Key CTRL+RIGHT ARROW
EndIf
StartSelect
Key END
EndSelect


Then you can also use

Find "do while"
Replace All SelectText "do while"
IfFound
Delete
StartSelect
Key HOME
Template x
ExitMacro
EndIf


I have not tested if it really works what I suggest here.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Snippets with macro

Postby mathmax » Wed May 30, 2007 6:12 pm

Thank you, it works almost but the word "do" is deleted when I execute the script. Do you know how to correct this ?
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: Snippets with macro

Postby Mofi » Thu May 31, 2007 6:05 am

You must have a template for do while loops which contains do while. You can't use the while template for do while loops.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Snippets with macro

Postby mathmax » Sat Jun 02, 2007 8:46 am

sorry, that was my template that contained "while" instead of "do while". So it works perfectly. Thank you very much for your help.
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm


Return to Macros