Hi Jussi
I wrote a Python macro, which works perfectly when you call iy from the macro menu.
Eventually I worked out how to get rid of all the default macros so I could try calling it using the Shift+Alt+F1 key combination. But when I do this the macro misbehaves
Can you suggest anything that is a common gotchya for newbies when doing this.
In case it iis useful in diagnosing this the macro script is
import zeus
def key_macro():
zeus.screen_update_disable();
# mark the function name
zeus.MoveLineHome();
zeus.MarkBlockSet();
zeus.MoveWordNext();
zeus.MarkBlockReset();
# create the header
zeus.MarkCopyEx();
#zeus.MoveLineUp();
zeus.MoveDocumentStartEx();
zeus.write("//\n");
zeus.write("// ");
zeus.MarkPasteEx();
zeus.write("\n");
zeus.write("//\n");
descLine = zeus.get_line_pos();
zeus.write("// description\n");
zeus.write("//\n");
zeus.write("// Parameters\n");
zeus.write("//");
zeus.TabSmart();
zeus.write("None\n");
zeus.write("//\n");
zeus.write("// Returns\n");
zeus.write("//");
zeus.TabSmart();
zeus.write("Nothing\n");
zeus.write("//\n");
zeus.write("// Changes\n");
zeus.write("//\n");
zeus.write("//\n");
# select the description word so editing can begin
zeus.set_line_pos(descLine, 3);
zeus.MoveWordNextEx();
zeus.MarkBlockSet();
zeus.MoveWordNext();
zeus.MarkBlockReset();
zeus.screen_update_enable();
zeus.screen_update_enable();
zeus.screen_update();
key_macro() # run the macro
THe way to use it is to place it on a line that has a single word on it, the word should start at the first character position. Essentially it generates a function header comment for the name.
Thanks
john
Python macro misbehaves when called via the keyboard
Hi John,
Just to better understand what is happening, if a macro is bound to a particular document type then that macro will only appear in the macros menu if you are currently editing a file of that document type. But if the macro is defined for the default document type it will appear for all files edited.
While this makes the macro menu highly configurable, it also makes the macro menu very dynamic and a bit confusing.
I am assuming you a binding Shift+Alt+F1 keys to one of the MacroExecute## functions and this will be creating the problem?
While these functions are in fact needed by the Zeus menu, from a user prespective they are next to useless. Because of the dynamic nature of the macros menus the macro keyboard function will change depending on what is currently being edited, so binding a key to the function will not always produce the same result.
So the better option is to add the macro to the menu as you already have done, but to also make sure the macro is saved to the Zeus zScript directory. This will allow you to bind the macro directly to the keyboard. I did just this using your macro and for me when I hit Shift+Alt+F1 I got this result:
Cheers Jussi
Eventually I worked out how to get rid of all the default macros
Just to better understand what is happening, if a macro is bound to a particular document type then that macro will only appear in the macros menu if you are currently editing a file of that document type. But if the macro is defined for the default document type it will appear for all files edited.
While this makes the macro menu highly configurable, it also makes the macro menu very dynamic and a bit confusing.
so I could try calling it using the Shift+Alt+F1 key combination. But when I do this the macro misbehaves. Can you suggest anything that is a common gotchya for newbies when doing this.
I am assuming you a binding Shift+Alt+F1 keys to one of the MacroExecute## functions and this will be creating the problem?
While these functions are in fact needed by the Zeus menu, from a user prespective they are next to useless. Because of the dynamic nature of the macros menus the macro keyboard function will change depending on what is currently being edited, so binding a key to the function will not always produce the same result.
So the better option is to add the macro to the menu as you already have done, but to also make sure the macro is saved to the Zeus zScript directory. This will allow you to bind the macro directly to the keyboard. I did just this using your macro and for me when I hit Shift+Alt+F1 I got this result:
Code: Select all
//
// description
//
// Parameters
// None
//
// Returns
// Nothing
//
// Changes
//
//
Hi Jussi
Now the macro does get called but doesn't operate properly. You have called the macro on an empty line when testing it, and it will work correctly then. However if you call it on a line that has a single word on it you should find that it behaves differently when you call it from the menu or use the keyboard shortcut. Well it does for me
Thanks for your trouble.
john
I guessed this was the case. I had looked in the documentation but it didn't seem to mention it; could just mean I missed the right topic of course. If it isn't there already I think the above statement would be very helpful if added to the Introduction to Macro Scripting.Just to better understand what is happening, if a macro is bound to a particular document type then that macro will only appear in the macros menu if you are currently editing a file of that document type. But if the macro is defined for the default document type it will appear for all files edited.
I am using the defaullt bindings and having gotten rid of the macros in the default template I expected my macro to be called when I edited files of the appropriate type. THis macro isn't applicable for other types.I am assuming you a binding Shift+Alt+F1 keys to one of the MacroExecute## functions and this will be creating the problem?
While these functions are in fact needed by the Zeus menu, from a user prespective they are next to useless. Because of the dynamic nature of the macros menus the macro keyboard function will change depending on what is currently being edited, so binding a key to the function will not always produce the same result.
Now the macro does get called but doesn't operate properly. You have called the macro on an empty line when testing it, and it will work correctly then. However if you call it on a line that has a single word on it you should find that it behaves differently when you call it from the menu or use the keyboard shortcut. Well it does for me

Thanks for your trouble.
john
Hi John,

At first I suspected there might be a bug with the macro, so I wrote the following Python macro as an alternative to your original macro:
but sure enough this new macro had exactly the same problems as your original macro 
After a bit more testing the penny finally dropped
Because the macro is bound to Shift+Alt+F1 the Shift part of this binding is used by CUA marking and the CUA marking was getting in the way of the macro. Basically CUA marking was happening as the macro was running.
To test this theory I used the Options, Editor Options menu edited the currently active keyboard mapping and truned off the CUA marking option. Sure enough the macro now worked fine for all cases
But turning off CUA marking is probably not the best solution. Peraonally I would find it very hard to work without this option. So what I would suggest is binding the macro to somehting like Ctrl+Alt+F1 instead.
I tested this binding and at least for me it worked fine. While doing the binding you will get presented with the Zeus online dialog box, but after closing the Zeus online help and completing the binding the key macro binding works fine
Cheers Jussi
I had another look at this bug and sure enough the macro works fine for a new new file but does not work if you run the macro in an exisiting fileNow the macro does get called but doesn't operate properly. You have called the macro on an empty line when testing it, and it will work correctly then. However if you call it on a line that has a single word on it you should find that it behaves differently when you call it from the menu or use the keyboard shortcut.

At first I suspected there might be a bug with the macro, so I wrote the following Python macro as an alternative to your original macro:
Code: Select all
import zeus
def key_macro():
zeus.screen_update_disable();
# this is the header string
header = "// \n" \
"// %s \n" \
"// \n" \
"// description \n" \
"// \n" \
"// Parameters \n" \
"// None \n" \
"// \n" \
"// Returns \n" \
"// Nothing \n" \
"// \n" \
"// Changes \n" \
"// \n" \
"//\n";
# NOTE: The next line assumes the cursor is over the function name
# or some text has been higlighted
function_name = zeus.macro_tag("$WEX")
# build up the header string with the Python sprintf
result = header % (function_name);
# this used for debugging purposes
#zeus.message_box(1, result);
# move the the start of the function line
zeus.MoveLineHome();
# save the cursor position
zeus.cursor_save();
# write out the function header
zeus.write(result);
# restore the cursor position
zeus.cursor_restore();
# search for the word "description"
zeus.set_find_text("description");
# run the search to highlight the word
zeus.SearchNext();
zeus.screen_update_enable();
zeus.screen_update();
key_macro() # run the macro

After a bit more testing the penny finally dropped

To test this theory I used the Options, Editor Options menu edited the currently active keyboard mapping and truned off the CUA marking option. Sure enough the macro now worked fine for all cases

But turning off CUA marking is probably not the best solution. Peraonally I would find it very hard to work without this option. So what I would suggest is binding the macro to somehting like Ctrl+Alt+F1 instead.
I tested this binding and at least for me it worked fine. While doing the binding you will get presented with the Zeus online dialog box, but after closing the Zeus online help and completing the binding the key macro binding works fine

Cheers Jussi