Windows command line/DOS batch variable highlighting using %

Windows command line/DOS batch variable highlighting using %

2
NewbieNewbie
2

    Aug 29, 2008#1

    If there is a single % the color setting works fine. What I want is to have
    %%a
    or
    %variable%

    completely colored.

    I want everything from the first % to a space colored or everything between and including the % colored.

    6,686585
    Grand MasterGrand Master
    6,686585

      Aug 31, 2008#2

      I have 2 suggestions, both are not perfect. Use the one which works better for your batch files:

      tab is a placeholder for a real horizontal tab character.

      /L1"Batch Files" Nocase Line Comment Num = 4rem  Line Comment Valid Columns = [1] Line Comment Alt = :: Line Comment Valid Columns Alt = [1] Block Comment On = echo Block Comment On Alt = :! String Chars = " DisableMLS File Extensions = BAT CMD
      /Delimiters = # $tab()*+,./:;<=>\
      /Function String = "%:!^([~ ^t^p]+^)[ ^t]++$"
      /Marker Characters = "%%"
      /C7"Environment Var."
      %% %1 %2 %3 %4 %5 %6 %7 %8 %9

      /L1"Batch Files" Nocase Line Comment Num = 4rem  Line Comment Valid Columns = [1] Line Comment Alt = :: Line Comment Valid Columns Alt = [1] Block Comment On = echo Block Comment On Alt = :! String Chars = " DisableMLS File Extensions = BAT CMD
      /Delimiters = # $tab()*+,./:;<=>\
      /Function String = "%:!^([~ ^t^p]+^)[ ^t]++$"
      /C7"Environment Var."
      ** %

      I start all labels with a ! which explains the alternate block comment and the function string. Example:

      if errorlevel 1 goto !ErrMes
      echo Program returned no error!
      goto !EndBatch

      :!ErrMes
      echo Program returned an error!
      Pause

      :!EndBatch

      2
      NewbieNewbie
      2

        Sep 05, 2008#3

        I modified the DOS/BATCH section like this:

        /L12"Batch Files" Nocase Noquote Line Comment = rem Line Comment Alt = REM String Chars = "; File Extensions = BAT CMD SYS
        /Delimiters = #$ ()+,./:;< =tab>\
        /Function String = "%:!^([~ ^t^p]+^)[ ^t]++$"
        /Marker Characters = "%%"
        /Open Brace Strings = "{" "(" "["
        /Close Brace Strings = "}" ")" "]"
        /C1"Batch-commands"
        color-1
        do
        else end errorlevel exist exit echo
        for find findstr
        goto
        if
        not
        pause
        return
        say select
        then
        when
        /C2"DOS-Commands"
        ansi append assign attrib autofail
        backup basedev boot break buffers
        cache call cd chcp chdir chkdsk choice cls cmd codepage color-2 command comp copy country color
        date ddinstal debug del detach device devicehigh devinfo dir diskcoache diskcomp diskcopy doskey dpath dumpprocess do
        eautil endlocal erase exit_vdm extproc echo equ nequ
        fcbs fdisk fdiskpm files find format fsaccess fsfilter
        graftabl
        iopl
        join
        keyb keys
        label lastdrive libpath lh loadhigh
        makeini maxwait md mem memman mkdir mode move
        net
        patch path pauseonerror picview pmrexx print printmonbufsize priority priority_disk_io prompt protectonly
        protshell pstat
        rd recover reipl ren rename replace restore rmdir rmsize run
        set setboot setlocal shell shift sort spool start subst suppresspopups swappath syslevel syslog
        threads time timeslice trace tracebuf tracefmt trapdump tree type
        undelete unpack use
        ver verify view vmdisk vol
        xcopy xcopy32 xdfcopy
        /C3"Variables"
        color-3
        ** %
        %% %1 %2 %3 %4 %5 %6 %7 %8 %9
        /C4"Semikolon"
        color-4
        ;
        /C5"Delimiters"
        color-5
        #
        (
        )
        ,
        .
        //
        /
        :
        <
        >
        \
        /C6"Operators" STYLE_OPERATOR
        color-6
        &
        >
        /
        \
        =
        |
        <
        /C7"Labels"
        color-7
        :

        This works nearly as I wanted it to, but now ANYTHING between 2 % is colored even if a space separates the character or character string from the %.

        Example: if %2 equ %alreadyfound% goto justecho

        in the preceding line %2 equ %alreadyfound% is all colored orange, even though equ is listed as an operator of a different color and it is separated from the % by a space. Is there a way to not color as a variable any character or combination of characters if they are separated from % by at least one space?

        6,686585
        Grand MasterGrand Master
        6,686585

          Sep 07, 2008#4

          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.