Author Topic: Ending Macros When String Not Found  (Read 54 times)

DRauch

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Ending Macros When String Not Found
« on: October 17, 2018, 03:16:13 pm »
Hello,

I am writing a macro to search a file for a string using the "find" function.:

find('string', 'I>')

After finding the string in the file, the macro does X. If the string being searched for is not found, how do I get the macro to stop running/display "string not found"/do nothing?

Thanks
DPR

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2167
  • Hero Points: 113
Re: Ending Macros When String Not Found
« Reply #1 on: October 17, 2018, 03:18:24 pm »
I'm not sure I understand.  Is this in a loop and you want to find it once and break/

DRauch

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Ending Macros When String Not Found
« Reply #2 on: October 17, 2018, 03:23:38 pm »
Here is what I currently have in the macro. When the macro finds 'PAGE  ', I want it to execute (this will remove the line in my file that has 'PAGE  '). When the macro does not find 'PAGE  ', I do not want it to execute the functions below and instead display "String not found" or something to indicate that 'PAGE  ' no longer exists in my file.

_command RemoveHeaders() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   _macro('R',1);
   find('PAGE  ','I>');
   _deselect();
   cursor_up();
   begin_line_text_toggle();
   deselect();
   _select_char('','E');
   cursor_down(5);
   select_it('CHAR','','E');
   delete_selection();
}

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2167
  • Hero Points: 113
Re: Ending Macros When String Not Found
« Reply #3 on: October 17, 2018, 03:32:38 pm »
Try this. 

I changed it to use search, which is a lower level function.  There are also lower level selection functions, but if you don't care if you lose your current selection it doesn't matter.  The key here is checking the return code from search.  It will return 0 if successful and some negative return code if it doesn't.  In this case we don't care what it is - although if you were debugging a more complicated case you might (because it could return something for an invalid regular expression (if you were using regular expressions), and then it would never work.

Code: [Select]
_command void RemoveHeaders() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   status := search('PAGE  ','I>');
   if (status) return;

   _deselect();
   cursor_up();
   begin_line_text_toggle();
   deselect();
   _select_char('','E');
   cursor_down(5);
   select_it('CHAR','','E');
   delete_selection();
}

DRauch

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Ending Macros When String Not Found
« Reply #4 on: October 17, 2018, 04:41:25 pm »
Thank you for getting back to me. I tried editing the macro and inserting the changes to the code you provided.

Unfortunately, that did not work. The intent of my macro is to search for 'PAGE  '. Once 'PAGE  ' is found, I want to move to the line above it, select the following 5 lines, and then delete those 5 lines (this will delete the line with 'PAGE  ').

Once the macro gets to the last instance of 'PAGE  ' and deletes the 5 corresponding lines, the macro should search for the next occurrence of 'PAGE  ' and, like you said, see that there is no more occurrences of 'PAGE  '. The current macro code still deletes 5 lines despite not finding 'PAGE  '. This is what I do not want to happen. I want the macro to notify the user that 'PAGE  ' was not found (or something to tell the user that all lines associated with 'PAGE  ' have been deleted).

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2167
  • Hero Points: 113
Re: Ending Macros When String Not Found
« Reply #5 on: October 17, 2018, 04:45:13 pm »
Hm, are you sure you reloaded it?  When I test here after it finds the last instance, it returns and does not delete lines.

DRauch

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Ending Macros When String Not Found
« Reply #6 on: October 17, 2018, 04:53:16 pm »
Pretty sure... I saved the macro, closed it, and then attempted to edit the macro again to see that the changes had been saved... here is what I see when going back to edit:
Code: [Select]
#include "slick.sh"

_command void RemoveHeaders() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   status := search('PAGE  ','I>');
   if (status) return;

   _deselect();
   cursor_up();
   begin_line_text_toggle();
   deselect();
   _select_char('','E');
   cursor_down(5);
   select_it('CHAR','','E');
   delete_selection();
}

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2167
  • Hero Points: 113
Re: Ending Macros When String Not Found
« Reply #7 on: October 17, 2018, 04:54:51 pm »
While you have it open, go to Macro>Load Module... and then select the file this is in.

DRauch

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Ending Macros When String Not Found
« Reply #8 on: October 17, 2018, 04:57:36 pm »
That'll do it! Perhaps my attempts before posting would have worked too if I knew to properly load the macro.

Thank you very much.