Macro to perform Replace selected text with selected text case-insensitive

Macro to perform Replace selected text with selected text case-insensitive

24
Basic UserBasic User
24

    Nov 11, 2008#1

    There is probably already a macro like this or maybe UE even has a built-in key combination for it, I don't know. What I'm looking for is the fastest way possible for UEStudio to perfom a Find & Replace where the Find text is what I have currently selected and the Replace with is also the selected text. This is useful when you perform the replace without Match Case enabled.

    I work with T-SQL, the flavor of the SQL language used by SQL Server, and I am spending a lot of time on code clean-up and I'm using the features in UEStudio, like the macro, to automate a lot of the common tasks. Unfortunately there's no fixed logic on what to look for that needs to be switched from lower case to upper case, at least not for all scenarios. So what I do now is I select (highlight) a few characters of text that are lower case and should be upper case and then I switch the case to upper case and perform a Find & Replace. Since the Match Case is not checked, the replace ends up replacing all lower case instances of the selected (highlighted) text with upper case equivalents.

    I'm looking to shrink this chain of actions to highlighting the text to be set to upper case and then with a single key combination, have UEStudio do the find & replace. It maybe that this kind of functionality is not something many would use but for those in SQL programming who are working with poorly constructed SQL code this is a big help.

    Any suggestions?

    Many Thanks !

    6,681583
    Grand MasterGrand Master
    6,681583

      Nov 11, 2008#2

      Try following macro:

      UnixReOff
      ToUpper
      Find "^s"
      Replace All AllFiles "^s"

      or for UE >= v14.20

      UnixReOff
      ToUpper
      Find AllFiles "^s"
      Replace All "^s"

      I have used parameter AllFiles here because a replace all is inside a macro always from current cursor position to bottom of the current file. The parameter Replace All is from top of file is not active for a replace all executed from within a macro or script. If you need a replace all from top of file and you don't want to execute it on all open files, the macro must be more complicated although also possible. But then a script would be even easier because the current cursor position could be stored in variables.
      Best regards from an UC/UE/UES for Windows user from Austria

      24
      Basic UserBasic User
      24

        Nov 11, 2008#3

        Mofi,

        Thanks for the code. It works great for the exact example I gave but unfortunately I was too specifc, I think. The Upper Case example was only 1 of several. I have to not only set the highlighted text to Upper case but I also have to often set only parts of the text to upper case. A Better description of this (and its my fault for not wording it this way orginally) would be to say I'm looking for a way to take the highlighted text, regardless of the case of the characters in it and replace every instance of it in the current file with whatever is currently highlighted with a Find & Replace that ignores case. This way all instances of:

        where tablename.columnname
        become

        WHERE TABLENAME.ColumnName

        Sometimes the case correction is to set all of the highlighted characters to upper case or all to lowercase and sometimes it's a mix. What I'm trying to avoid is having to correct the case on the same set of characters more then once. It's not uncommon for me to find a set of characters in which the instances of the set that are in the wrong case is more then a dozen times. In a large file this is tedious to fix even using the Find & Replace because I'm having to do this kind of case correction for dozens, even hundreds of sets of characters.

        Any other sugestions?

        BTW - If it's only possible to do it with a macro that affects all open files then I can handle that by just running the macro when only 1 txt file is open.

        Thanks Again!

        6,681583
        Grand MasterGrand Master
        6,681583

          Nov 12, 2008#4

          No problem, just remove command ToUpper and you have only the replace remaining in the macro. For security I suggest following:

          IfSel
          UnixReOff
          Find AllFiles "^s"
          Replace All "^s"
          EndIf

          ^s is explained in help of UEStudio for command Find/Replace and simply means selected text.

          The following macro replaces all occurences of the highlighted text in the current file only from top of the file to end of the file. The disadvantage is that the cursor is than always at top of the file.

          IfSel
          Clipboard 9
          Copy
          Top
          UnixReOff
          Find "^c"
          Replace All "^c"
          ClearClipboard
          Clipboard 0
          EndIf

          As already written a script would be better here because with a script you can store the current selected string in a variable and don't need to use a clipboard, you can remember in variables also the current cursor position, move to top, run the replace with the string stored in the variable and move the cursor back to the original position. That would be quite a simple script. Hm, I think I will write it because it could be of general interest.
          Best regards from an UC/UE/UES for Windows user from Austria

          24
          Basic UserBasic User
          24

            Nov 12, 2008#5

            Mofi -

            Thanks for the updated code and your script idea would be fantasticly awesome, at least for my needs. I am slightly familiar with the scripting feature in UEStudio; I read over the details about it yesterday and played with one of the examples. I'm not very familiar with Java so it's hard for me being a VB/VB Basic/VB.Net (newbie) type of developer. For example I didn't realize the thing was case sensative and it took me a good while to figure it out and in turn figure out what I did to break the example script.

            If you do put together a script as you've discussed, please let me know and I will be more then happy to give it a solid real world test.

            Thansk again!