Tapatalk

Can I use an UE macro as preprocessor replacement?

Can I use an UE macro as preprocessor replacement?

4
NewbieNewbie
4

    Jul 08, 2006#1

    Working in a language called Progress. I'm trying to create a macro to replace a preprocessor constant with it's defined value.

    /* example preprocessor definitions */
    &scop constant_friendly_name constant_value
    &scop num_days_in_week 7
    &scop days_of_week Mon Tue Wed Thu Fri Sat Sun

    /* the constant name is 1 word, followed by >= 1 space, then the constant value; the constant value *could* span lines if line-terminated with ~ but I am not concerned with that scenario as it's rarely used in the code I'm dealing with */

    /* in code the constant is wrapped inside {& } like so: */
    print "{&days_of_week}".

    /* would like the macro to change this to */
    print "Mon Tue Wed Thu Fri Sat Sun".

    Is this possible with UltraEdit and if so, can someone give me some pointers? I'm using the 12.10a build.

    Thanks!

    6,685587
    Grand MasterGrand Master
    6,685587

      Jul 08, 2006#2

      That was no problem for me because I already have several macros which are for string replacements. But none of my macros is so easy as the following one which should work for you. It is easy because the line with the 2 strings has an identifier string (&scop) and the first string is always a single word and not a term with spaces like in my macros.

      The macro needs the property Continue if a Find with Replace not found enabled.

      It also works for multi-line defines because it selects always from start of the replacement string till the end of the line which has no ~ at the end. This is normally the end of the current line. You must make sure only that you never have 1 or more trailing spaces if they should not be also part of the replacement string.

      If your defines are case-sensitive, add the find parameter MatchCase at the find command Find "{&^c}".

      Here is the macro for UltraEdit style:

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Top
      Clipboard 9
      Loop
      Find "&scop"
      IfNotFound
      ExitLoop
      EndIf
      EndSelect
      Key Ctrl+RIGHT ARROW
      SelectWord
      StartSelect
      Copy
      EndSelect
      Key Ctrl+RIGHT ARROW
      StartSelect
      Find RegExp Select "[~^~]$"
      Find "{&^c}"
      Replace All "^s"
      EndSelect
      EndLoop
      ClearClipboard
      Clipboard 0

      If you prefer legacy Unix engine, modify the 4th command to UnixReOn and use following regular expression find to select the (multi-line) replacement string:

      Find RegExp Select "[^~]$"

      I have had no success with this macro with the Perl engine with UE v12.10a. The are still some bugs with it. In this case it finds first "&scop" (a normal find!) but the macro exits the loop - strange!

      Note: If instead of Find "{&^c}" and Replace All "^s" the command ReplInFiles is used, the macro can be used also for projects with multiple files where all defines are stored in a separate file.
      Best regards from an UC/UE/UES for Windows user from Austria

      4
      NewbieNewbie
      4

        Jul 08, 2006#3

        Great. Thanks so much!

        I modified it a bit to get it to work just as I needed.

        InsertMode
        ColumnModeOff
        HexOff
        Clipboard 9
        Find "&"
        IfNotFound
        ExitLoop
        EndIf
        TrimTrailingSpaces
        Find " "
        Key RIGHT ARROW
        Key LEFT ARROW
        UnixReOn
        Find RegExp Select "[\w-]*"
        UnixReOff
        Copy
        Find " "
        Key RIGHT ARROW
        Key LEFT ARROW
        StartSelect
        Find RegExp Select "[~^~]$"
        Find "{&^c}"
        Replace All "^s"
        EndSelect
        EndLoop
        ClearClipboard
        Clipboard 0

        I can't replace ALL preprocessors in any given file, so I removed the Top command and I assume I'm at the start of the line containing the target preprocessor when I run the macro.

        I also added a TrimTrailingSpaces as I believe the preprocessor does that itself.

        I neglected to mention originally that &scop, &scoped-define, &glob, and &global-define are all valid preprocessor directives, so I changed '&scop' to '&'. I'm not sure if this correctly accomplishes the sanity check that should probably be here, but that's OK for now.

        Also '-' is a legal character in a preprocessor name so I kludged around SelectWord. I suspect there is a better way to do this.

        Thanks again

        6,685587
        Grand MasterGrand Master
        6,685587

          Jul 09, 2006#4

          Does your macro really work?

          It contains the commands ExitLoop and EndLoop but no Loop. And Find " " with the following cursor movements moves the cursor only to the position after first space after the definition string. You have written in your first post " followed by >= 1 space" and so your code will fail if there are more than 1 space.

          Here is an improved version of the macro in Unix regex style according to the new infos from your second post. If you don't want the loop, remove the red lines.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOn
          TrimTrailingSpaces
          Clipboard 9
          Loop
          Find RegExp "(&scop[-defin]*|&glob[-adefiln]*)"
          IfFound
          EndSelect
          Key Ctrl+RIGHT ARROW
          Find RegExp "[\w-]*"
          StartSelect
          Copy
          EndSelect
          Key Ctrl+RIGHT ARROW
          StartSelect
          Find RegExp Select "[^~]$"
          Find "{&^c}"
          Replace All "^s"
          EndSelect
          Else
          ExitLoop

          ClearClipboard
          EndIf
          EndLoop
          Clipboard 0

          If someone else is interested in, here is the macro with UltraEdit regex style:

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          TrimTrailingSpaces
          Clipboard 9
          Loop
          Find RegExp "^{&scop[^-defin]++^}^{&glob[^-adefiln]++^}"
          IfFound
          EndSelect
          Key Ctrl+RIGHT ARROW
          Find RegExp "[0-9A-Z_^-]+"
          StartSelect
          Copy
          EndSelect
          Key Ctrl+RIGHT ARROW
          StartSelect
          Find RegExp Select "[~^~]$"
          Find "{&^c}"
          Replace All "^s"
          EndSelect
          Else
          ExitLoop

          ClearClipboard
          EndIf
          EndLoop
          Clipboard 0


          With the loop the macro needs the macro property Continue if a Find with Replace not found enabled.
          Best regards from an UC/UE/UES for Windows user from Austria

          4
          NewbieNewbie
          4

            Jul 09, 2006#5

            Mofi wrote:Does your macro really work?
            Yes I wouldn't have reposted it having just written it and not trying it at least once. :) Now I see however that it worked mostly by coincidence given it had some syntactical and logic errors.
            Mofi wrote:It contains the commands ExitLoop and EndLoop but no Loop. And Find " " with the following cursor movements moves the cursor only to the position after first space after the definition string. You have written in your first post " followed by >= 1 space" and so your code will fail if there are more than 1 space.
            Absolutely right. My mistakes.
            Mofi wrote:Here is an improved version of the macro in Unix regex style according to the new infos from your second post. If you don't want the loop, remove the red lines.
            Great. Thanks again. I really need to carve out some time to get familiar with regular expressions.