by Mofi » Sun Sep 07, 2008 3:56 am
I have posted 2 suggestions and written that you should use the one which is better for your batch files. Both are not perfect. Using both at the same time is a very bad idea.
My first suggestion is to use marker characters. From help of UltraEdit about syntax highlighting:
Marker Characters
There are times when it is desirable to highlight all characters between two characters. UltraEdit provides for "marker characters" that mark the first and last part of a string that UltraEdit highlights between. All characters between the two characters are highlighted.
So with this definition any environment variable name between 2 percentage signs are automatically highlighted. The remaining problem is the highlighting of the parameters which unfortunately start also with a %, but do not end with a %. Therefore %1, %2, ... are additionally added to the wordfile to highlight the command line parameter references, too. But if a parameter reference is used in the same line as an environment variable and the parameter reference is left the env, this solution highlights the parameter reference and the env wrong as in:
if %2 equ %alreadyfound% goto justecho
Marker characters have a higher priority for syntax highlighting than simple words. With the suggested highlighting using marker characters there is no solution for that problem. I use personally this method because most of the time I use double quotes around parameter references because simply required (to handle paths/file names with spaces correct) and I have enabled highlighting double quoted strings as strings.
My second suggestion uses a substring definition instead of marker characters. From help of UltraEdit:
Keywords beginning with a Substring
There are instances in some languages where it is desirable to highlight keywords that begin with a particular substring, however the complete word is not known. UltraEdit provides the ability to define substrings that are used to determine if a word should be highlighted. If such substrings are defined for a particular language under a color group UltraEdit will determine if a word begins with one of the substrings. If it does, it will be highlighted accordingly.
So with this definition all words starting with % are highlighted. What is a word? A sequence of characters limited by the delimiter characters specified by the /Delimiters = definition in the wordfile and the line ending characters.
So the substring definition does not highlight the example above wrong. But if % is not immediately after a delimiter character or other non delimiter character follows the parameter reference or the env, this results in a not or wrong highlighting like for example for:
if exist Test_%1 goto !TestOk
if exist %1_bak.* del %1_bak.* >nul
If there would be a perfect solution to highlight parameter references and environment variables in batch files in all situations correct with UltraEdit, I would use it for myself, would have posted it and would have sent already a better wordfile for batch files to IDM for making it public and replacing all the different wordfiles existing for highlight command line scripts. How to highlight environment variables and parameter references correct in batch files with UltraEdit has been asked several times in the past, but nobody has ever found a better solution than the 2 methods I have posted here. But unfortunately with the current (14.10) syntax highlighting capabilities the "bad" syntax of batch files cannot be better handled by UltraEdit. Complex regular expressions would be necessary to highlight parameter references and environment variables correct in all situations which I guess notepad++ and pspad support.
Another problem is the variable in a FOR loop requiring 2 percentage signs before the letter. So for %%V there is also no perfect solution available.
But for myself I don't really require regular expressions for syntax highlighting. Whenever the syntax of a language cannot be 100% correct highlighted by UltraEdit, I simply changed the kind of how I write the code in that language to get always a correct highlighting with UltraEdit. From my personal observations this results always in better scripts/programs. For example to avoid that highlighting problem with parameter references I'm using the first solution and use in larger batch files following at top of the batch file:
set ProjectName=%1
set BuildMode=%2
set OutputName=%3
You can imagine how the batch file continues. It does not contain %1, %2, %3 anywhere below these lines. It uses %ProjectName%, %BuildMode% and %OutputName%. And now you can also understand why I have written above "From my personal observations this results always in better scripts/programs". The batch file is much easier to understand when using names for the command line parameters instead of %1, %2, %3 and everybody can see at top of the batch file what each parameter means.