Convert all language keywords in a syntax highlighted file to upper case

Convert all language keywords in a syntax highlighted file to upper case

19
Basic UserBasic User
19

    Oct 13, 2010#1

    Dear all,

    is it possible to make upper case (or back to lower case) all the language keywords in a file? I have a VHDL file with some keywords in all upper case and some not and I want them all in upper case.

    30
    Basic UserBasic User
    30

      Oct 13, 2010#2

      It's easily done, but requires scripting.

      I wrote a script for you that will search for a list of keywords, and turn all of them into UPPERCASE.

      Code: Select all

      var WorkingFile = UltraEdit.activeDocument;
      
      function KeywordCaseChange(SearchString)
      {
      	WorkingFile.top();
      
      	// Do initial search for string
      	UltraEdit.perlReOn();
      	WorkingFile.findReplace.regExp = true;
      	WorkingFile.findReplace.matchCase = false;
      
      	var CompletionFlag = 0;
      
      	while (CompletionFlag == 0)
      	{
      		WorkingFile.findReplace.find(SearchString);
      
      		// If the initial search fails, END and return false.
      		if (WorkingFile.isFound() == false)
      		{
      			CompletionFlag = 1;
      			return false;
      		}
      		else
      		{
      			WorkingFile.toUpper();
      		}
      	}
      	WorkingFile.top();
      }
      
      KeywordCaseChange("\\bkeyword1\\b|\\bkeyword2\\b|\\bkeyword3\\b");
      The last line of the script that executes the function is where you can edit the list of keywords. The entire search pattern goes in between the set of double quotes, and is expected to be in RegEx form.

      All you need to know about this is that if you don't want words to be made upper case in the middle of a larger word (e.g. if you want to make upper case "keyword", but don't want to accidentally end up with "KEYWORDs"), you need to surround each keyword with "\\b". Also, put a pipe character ("|") in between each search term. In RegEx, it acts as an "OR" operator.

      If you want to do any other case changes, it should be pretty easy to figure out from here.

      6,686585
      Grand MasterGrand Master
      6,686585

        Oct 14, 2010#3

        • If you use a syntax highlighting wordfile for your VHDL files, and
        • if the first line of the syntax highlighting language definition for VHDL does not contain keyword Nocase to make the syntax highlighting case sensitive (if not remove the keyword), and
        • all the keywords in the color groups are in upper case (if not select them and use Format - To Upper Case), and
        • you have enabled configuration setting Auto-correct keywords at Advanced - Configuration - Editor Display - Miscellaneous,
        you can use following script to upper case the syntax highlighted keywords of VHDL using the auto correct feature of UltraEdit.

        Code: Select all

        UltraEdit.insertMode();
        if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
        else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
        UltraEdit.activeDocument.top();
        while (!UltraEdit.activeDocument.isEof()) {
           UltraEdit.activeDocument.write(" ");
           UltraEdit.activeDocument.key("BACKSPACE");
           UltraEdit.activeDocument.key("CTRL+RIGHT ARROW");
        }
        UltraEdit.activeDocument.top();
        Best regards from an UC/UE/UES for Windows user from Austria

        19
        Basic UserBasic User
        19

          Oct 14, 2010#4

          Thank you very much for your answers. This was very helpful.

            Nov 17, 2010#5

            I'm using the method of Bracket for a while but the disadvantages is that when a line has a -- then the remaining of the line should be skipped because it's a comment. Now all the comments are also converted to upper case. How can I solve this? I don't understand the Javascript enough.

            The Mofi method has the advantage that it uses all the words of the uew file and it skips the comment, but I cannot write the keywords any more in lower case and I don't want to write in upper case all the time.

            I hope someone can solve this problem.

            30
            Basic UserBasic User
            30

              Nov 17, 2010#6

              Not difficult. Unfortunately, UltraEdit doesn't have very good RegEx support, and Perl RegEx has some limitations that keep us from dealing with this issue gracefully.

              As a result, we need to hack a solution via a different route.

              The following script will do as you ask. It will look for "--" as what marks a comment line. It also takes the keyword list in only one place - the last line that calls the function, and it requires just the keywords, seperated by a pipe character if you're addressing more than one.

              Code: Select all

              var WorkingFile = UltraEdit.activeDocument;
              
              function KeywordCaseChange(KeyWordList)
              {
              	WorkingFile.top();
              
              	// Do initial search for string
              	UltraEdit.perlReOn();
              	WorkingFile.findReplace.regExp = true;
              	WorkingFile.findReplace.matchCase = false;
              	WorkingFile.findReplace.replaceAll = true;
              	
              	
              	//  Assemble the search string we need to do the initial tagging.
              	var SearchString = "^(((?!--).)*?)(?<!@@@)\\b(" + KeyWordList + ")\\b";
              	
              	
              	var CompletionFlag = 0;
              
              	//  Tag all the correct keywords by inserting "@@@" in front of them.
              	while (CompletionFlag == 0)
              	{
              	
              	
              		WorkingFile.findReplace.replace(SearchString, "\\1@@@\\3");
              	
              	
              		// If the initial search fails, END and return false.
              		if (WorkingFile.isFound() == false)
              		{
              				CompletionFlag = 1;
              				
              		}
              
              	}
              	
              	
              	WorkingFile.top();
              	
              	
              	
              	//  Set the search string for the next stage, where we convert them to uppercase.
              	SearchString = "(?<=@@@)\\w+\\b";
              	
              
              	CompletionFlag = 0;
              
              	while (CompletionFlag == 0)
              	{
              		WorkingFile.findReplace.find(SearchString);
              
              		// If the initial search fails, END and return false.
              		if (WorkingFile.isFound() == false)
              		{
              				CompletionFlag = 1;
              				
              		}
              		else
              		{
              				WorkingFile.toUpper();
              		}
              	}
              	WorkingFile.top();
              	
              	
              	//  Remove all the tagging.
              	WorkingFile.findReplace.replace("@@@", "");
              	
              	
              }
              
              KeywordCaseChange("keyword1|keyword2|keyword3");

              19
              Basic UserBasic User
              19

                Nov 18, 2010#7

                Thank you very much for your fast and detailed answer. It helped a lot and I'm very happy with that.

                One thing is unclear that is what you mean with
                It also takes the keyword list in only one place -
                Do you mean that it will take the keywords from the vhdl93.uew file (that is the keyword file). I think that it should take these keywords but in that case that part is not working. Only keywords (the KeywordCaseChange) at the end of the script file are made upper case and not the one in the .uew file.

                But maybe I'm reading the script wrong. (I don't understand the SearchString).

                30
                Basic UserBasic User
                30

                  Nov 18, 2010#8

                  In other words, the very last line of the script, "KeywordCaseChange("keyword1|keyword2|keyword3");" is the only place you need to enter the list of keywords you wish to act upon (seperated by a pipe character, if you are using more than one). You are not supposed to put them anywhere else in the script.

                  6,686585
                  Grand MasterGrand Master
                  6,686585

                    Nov 19, 2010#9

                    acinfo64 wrote:The Mofi method has the advantage that it uses all the words of the uew file and it skips the comment, but I cannot write the keywords any more in lower case and I don't want to write in upper case all the time.
                    A solution here would be to create a copy of vhdl93.uew which contains all the keywords you want completely in upper case defined in upper case in the wordfile and first line changed to

                    /L20"VHDL Uppercase" Line Comment = -- String Chars = " File Extensions = XXX

                    So this wordfile is by default not applied to VHDL files, but is case sensitive. Now before running my script you select from menu View - View As (Highlighting File Type) the language VHDL Uppercase, next run my script, and finally switch back to not case sensitive language VHDL via the menu.
                    Best regards from an UC/UE/UES for Windows user from Austria