Author Topic: Need to create macro to enumerate  (Read 3978 times)

flethuseo

  • Senior Community Member
  • Posts: 176
  • Hero Points: 2
Need to create macro to enumerate
« on: January 07, 2013, 10:32:15 pm »
I have a bunch of test cases in an XML file named : blah_blah_blah_blah_number

The test cases have the numbers all messed up like:

blah_3
blah_1
blah_7
....

I need to re-number them. So that the first one gets renumbered 1, the second 2.. and so on. I want to build a macro for this but I don't know how to start. I need some sort of search function that can go to the pattern I give it, and then it substitutes the number with a count that I keep in some variable. I'm not proficient at all with Slick-C, and would like to get this done quickly :\

Any help appreciated,
Ted

hs2

  • Senior Community Member
  • Posts: 2732
  • Hero Points: 284
Re: Need to create macro to enumerate
« Reply #1 on: January 08, 2013, 07:32:48 am »
It's already there: enumerate
It's very versatile (see the docs) and in conjunction with col. selections and you can easily 'renumber' the lines in your example.
Note that there are also a number of sort commands which are also very useful.
HS2
« Last Edit: January 08, 2013, 07:36:11 am by hs2 »

flethuseo

  • Senior Community Member
  • Posts: 176
  • Hero Points: 2
Re: Need to create macro to enumerate
« Reply #2 on: January 08, 2013, 03:12:57 pm »
I don't think it is as simple as using enumerate, because all of the test cases are not right next to one another, so I just can't simply select a block and run enumerate..

it's more like:
<TestCase name="blah_1">
 ....
 ...
</TestCase>

<TestCase name="blah_2">
 ....
 ...
</TestCase>

It has to be a sort of find a certain string XYZ.. and then number that string followed by XYZ_(number), unless there is a more sophisticated way to use enumerate.

Ted
« Last Edit: January 08, 2013, 05:37:40 pm by flethuseo »

hs2

  • Senior Community Member
  • Posts: 2732
  • Hero Points: 284
Re: Need to create macro to enumerate
« Reply #3 on: January 09, 2013, 09:39:38 am »
Right, if you want to sort the TestCase elements it's a bit more complex scenario and you need a hand-crafted macro.
I'd do it this way:
- create a macro with starting number as argument (to specify on cmdline) perhaps with a default value (1 ?)
- place cursor at the beginning of the section to sort

in a loop:
- setup search pattern ("Testcase name=blah_" :+ number)
- save cursor pos. and search for pattern with the appropriate options
- start line selection if found or break loop/return from macro if not
- search for closing 'Testcase' tag while selection is auto-extended (break loop/return from macro if not)
- cut selected text to clipboard
- restore cursor pos. and insert clipboard

If you want to run it single stepped you need a global or module static var. for 'number'.
Just create a macro to set the number variable and a 2nd macro to do the sorting (maybe using a loop with counter as argument or not).

If you want to patch just the TestCase name it's similar but more simple.
Just setup a search expression and the replace pattern accordingly and run 'replace'.

Good luck,
HS2
« Last Edit: January 09, 2013, 09:45:33 am by hs2 »

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Need to create macro to enumerate
« Reply #4 on: January 09, 2013, 06:26:51 pm »
If you want to run it single stepped you need a global or module static var. for 'number'.
Just create a macro to set the number variable and a 2nd macro to do the sorting (maybe using a loop with counter as argument or not).
Or you could make it search backwards from the current position to find the previous number, and add 1 to it.  And if there's no previous number use 1.

flethuseo

  • Senior Community Member
  • Posts: 176
  • Hero Points: 2
Re: Need to create macro to enumerate
« Reply #5 on: January 21, 2013, 02:01:43 am »
I really am new to this whole programming macros thing. I don't know most of the commands and I don't know how to quickly find them, besides running the record macro functionality. I have been trying to follow through your pseudocode as a slick-c programming exercise, but I don't know the equivalent functions for some of the instructions:

What are the slickedit functions to do these things:
- save cursor pos
- restore cursor pos
- start line selection (do you mean the select_line() function?)
- I am not sure if I understand you correctly on this:  "search for closing 'Testcase' tag while selection is auto-extended".
Does starting a line selection automatically allow the selection to be auto extended when finding the Testcase closing tag?

Ted

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Need to create macro to enumerate
« Reply #6 on: January 22, 2013, 03:50:35 am »
I was surprised, but searching in the Help for "cursor" or "pos" or "position" are useless to find the functions because it turns out the description text uses the term "location" instead.  Doh.

Anyway:
- save_pos (or _save_pos2, see help text for differences)
- restore_pos (or _restore_pos2, see help)
- select_line
- Yes, starting a selection puts it into a mode where moving the cursor pos extends the selection.  The help for select_line describes the usage.