Author Topic: Would like to use the search and replace dialog from recorded macros.  (Read 2720 times)

Tim Kemp

  • Senior Community Member
  • Posts: 546
  • Hero Points: 92
I'd like a way to use the search and replace dialog from recorded macros. I often have to do repetitive tasks where I select a chunk of text, search for it, make some modifications to every instance and then repeat. Unfortunately, as soon as the dialog opens nothing gets recorded. I have ended up using macros to load up the clipboard, select the text to search and then stop so I can complete the rest of it by hand. Over and over.

I expect the same thing applies to other dialogs, but I haven't felt the need to use any but search or search and replace.

I do know about, and use, regular expressions. I have also used "Next occurrence" in recorded macros, but that's not always a possibility. Neither of these provides nearly the flexibility that the search dialog would.

I've resorted to external keyboard/mouse macro recorders at times, but they aren't a good solution.

Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Would like to use the search and replace dialog from recorded macros.
« Reply #1 on: January 17, 2015, 02:24:25 AM »
I'd like a way to use the search and replace dialog from recorded macros. I often have to do repetitive tasks where I select a chunk of text, search for it, make some modifications to every instance and then repeat. Unfortunately, as soon as the dialog opens nothing gets recorded. I have ended up using macros to load up the clipboard, select the text to search and then stop so I can complete the rest of it by hand. Over and over.

I expect the same thing applies to other dialogs, but I haven't felt the need to use any but search or search and replace.

I do know about, and use, regular expressions. I have also used "Next occurrence" in recorded macros, but that's not always a possibility. Neither of these provides nearly the flexibility that the search dialog would.

I've resorted to external keyboard/mouse macro recorders at times, but they aren't a good solution.

Hi Tim
I'm having difficulty understanding what you're trying to do.  Can you give a real example?
You say "I select a chunk of text, search for it, make some modifications to every instance and then repeat."
What do you mean by "repeat" here?

If I record a macro and search in all files I get something like this.  The arguments in the call to _mffind2 reflect all the things I choose in the search dialog.
Code: [Select]
#include "slick.sh"
_command last_recorded_macro() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   _mffind2('hello','','<Workspace>','*.*','','32','1');
}

If you want the search dialog to appear when your macro runs you can call e.g. find_in_files from your macro.


Tim Kemp

  • Senior Community Member
  • Posts: 546
  • Hero Points: 92
Re: Would like to use the search and replace dialog from recorded macros.
« Reply #2 on: January 17, 2015, 02:21:47 PM »
@Graeme

Here is a recent real example. I inherited an embedded project that implemented bit access using hard coded macros like this:
Code: [Select]
$define bFlagName_MASK   0x0001
#define SET_bFlagName    (Flag1 |= bFlagName_MASK)
#define CLR_bFlagName    (Flag1 &= !bFlagName_MASK)
#define bFlagName        (Flag1 & bFlagName_MASK)

There are a lot of them and I found maintaining the code harder than it should have been. I was adding a feature to the project where I needed to add a number of bits and move around several others, so I decided to refactor. I added common bit access macros like this:
Code: [Select]
#define SET(bit) (flags[bit>>3] |= (1<<(bit&7)))
#define CLR(bit) (flags[bit>>3] &= (~(1<<(bit&7))))
#define GET(bit) (flags[bit>>3] & (1<<(bit&7)))

And moved the bit names to an enumeration like this:
Code: [Select]
enum flagBits
{
    B_FLAG_NAME,
    NUMBER_OF_FLAGS
};

Next came the fun part, changing all original macro instances in the code to the new macros. It wasn't a one to one mapping, so I couldn't use SlickEdit's Quick Rename. What I ended up doing was to create a table with two columns one with the old macro name and the other with the replacement string like this:
Code: [Select]
SET_bFlagName   SET(B_FLAG_NAME)
CLR_bFlagName   CLR(B_FLAG_NAME)
bFlagName       GET(B_FLAG_NAME)

I wanted to write a macro that would replace all instances of the text in the first column with the text in the second column. Not being a very experienced Slick-C programmer and not really having the time to figure it out, I tried to do it with a recorded macro. It could have been pretty easy. There aren't many steps to doing it manually.
  • Move down to the next line in the table
  • Copy the text from the second column into the clipboard
  • Select the text from the first column
  • Invoke Replace in Files
  • Hit tab to get to the Replace with field
  • Paste from the clipboard
  • Select Replace All
I couldn't get it to work directly. I ended up automating it using a recorded SlickEdit macro to do the first three steps and an external program to play back keystrokes to SlickEdit to call the macro and handle the dialog.

Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Would like to use the search and replace dialog from recorded macros.
« Reply #3 on: January 18, 2015, 01:27:15 AM »
ok.  What I often do is record a macro, then stop recording and click the edit button in the dialog to see what got recorded and "steal" some of the code.  For replace in files you get something like this

Code: [Select]
_command last_recorded_macro() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   _mfreplace2('old_text','new_text','WE*','<Workspace>','*.*','','0','1','0');
}

So you could then just edit the macro (lastmac.e gets opened) and add more calls to _mfreplace2

   _mfreplace2('old_text2','new_text2','WE*','<Workspace>','*.*','','0','1','0');
Click load module on the macro menu to load lastmac.e, then click "execute last macro" in the macro menu to run it.
As you can see, slick macro recording records commands rather than keystrokes.  If you know a little bit about how to write macros you don't need slick to record keystrokes in the search dialog.  If you don't (99% of people), I guess if slick macro recording wanted to be really smart, it could take notice of you "pasting" text into the search or replace string  - then maybe adjust the recorded macro after asking if you want the literal text or the "clipboard" contents. 

Graeme

Tim Kemp

  • Senior Community Member
  • Posts: 546
  • Hero Points: 92
Re: Would like to use the search and replace dialog from recorded macros.
« Reply #4 on: January 18, 2015, 03:45:39 AM »
@Graeme That's a good suggestion. I'll try to remember it for next time I have to do something like this.

Thanks,
Tim