Need help in enabling batch language outlining

  • I would like to enable outlining of batch file functions. Newer versions of the Windows command shell allow returning from a ‘call’ via ‘exit /b’ (or ‘goto:eof’). I would like to enable outlining in batch files as in other languages with functions. But how to do it? Modify a source file and recompile? Modify or add a config file? (I have not looked at the source, as I thought it would be easier to just ask here first, but I am a developer so could do that if not too time consuming). Check out the detail below and please let me know if you can direct me to the code or config file to change/add or if you guys want to do it, power to ya. Thanks.

    A function in batch language looks like this:

    : <label>
    :: A double colon can be a comment instead of using ‘REM’. (I use the double colon and have this enabled via Notepad++ config file settings).
    :: Body of function.
    exit /b <exit code>

    where “label” is the function name, and where “exit code” is a signed integer in the range –2,147,483,647 to 2,147,483,647 or an environment variable in the form: %var_name% set to the value of a signed integer in that range.

    For example:

    echo.Hello world!
    set /a exit_code = 0
    exit /b %exit_code%

    The function is called thusly (within the same batch file):

    call :DoSomething

    or, if DoSomething is in another batch file:

    call otherbatchfile.bat :DoSomething

    (Note: batch language is not case sensitive, though I use camel case for function names).

    Arguments can be passed to functions also:

    set var1=%~1
    set var2=%~2
    echo.%var1% %var2%!
    set /a exit_code = 0
    exit /b %exit_code%

    call :DoSomethingElse “Hello” “World”

  • Not really sure what your request is. There is already support for syntax highlighting of batch files. Does your file name not have a “.bat” extension?

    You can use “Language”->“Define your language…” to create syntax highlighting for any type of file you’d like. I assume (but haven’t tried) that you can write your own language definition even for an existing one that NPP supports.

  • Functions in batch language are not really a feature of the language–all that is provided are the mechanisms to make functions hackishly work. Those mechanisms are: batch file labels, and the ‘exit /b’ statement. Since batch does not have “real” functions, the outlining functionality of Notepad++ is not available for .bat files. I.e., Notepad++ does not recognize the patterns of use of the label and ‘exit /b’ mechanisms that define functions.

    .bat files do not have a separate .xml file as some languages do in Notepad++. Apparently batch is one of those tricky languages to highlight so the code to do that is in the Notepad++ source code (?).

    I don’t think that defining a new language will work for .bat files since it doesn’t have any .xml config file to begin with.

  • I believe the built-in languages are a Scintilla component (Scintilla is the actual editor within NPP).
    Support for syntax highlighting of batch files is there. Go to “Settings”->“Style Configurator…” and select “Batch” in the “Language:” box. The “Style:” box contains the various elements of the language that you can configure; LABEL is one of those elements.

    I suspect that you mean something other than syntax highlighting when you use the term “outlining”. Perhaps you want to enable folding of functions and if-else statements. I’m pretty sure to do that you’ll have to define your own language.using “Language”->“Define your language…”.

    Batch labels show up in “View”->“Function List” tool as functions. That might give you part of whatever it is you desire.

  • Yes, when I said “outlining”, I meant that I want to be able to fold/expand source code based upon the functions in the batch file (as is the capability in other languages). The syntax highlighting for batch is adequate.

    “View->Function List” lists the labels in a the batch file. So, that’s at least a start toward changing the source code to recognize that ‘exit /b’ denotes the end of a function. Since defining functions that way is completely optional (and there are other ways, like using ‘goto:eof’), I have a feeling the Scintilla and Notepad++ developers may not be interested in changing the code to support that (?), and I may be on my own to modify and recompile the source code.

    Defining a whole new language in Notepad++ (and that has its own limitations) when batch is already supported, seems like the wrong approach (?) and I have a feeling that the capability does not exist in the user-defined language facility (?) to define the end of a function as ‘exit /b’ so that source code can be folded on functions.

    More suggestions from anyone appreciated.

  • If you define your own language, you can do some pretty interesting things. In my GAWK language definition, for example, I have 3 different settings for comments: “#” for normal ones that are somewhat subdued in appearance, “##” for ones describing a function that stand out more, and “#!” for things I really need to see (something temporarily commented out or maybe something I plan to delete later).

    I made a small test language. On the Folder & Default tab, in the Folding in code 1 style: Open: box I put:

    :Function (

    and in the Close: box I put

        EndFunction )

    That allows me to fold on “(”…")" and “:Function<whatever>”…“EndFunction”

    So this is how my batch file would look:

    if !%1 == ! (
        echo Supply at least one argument!
    else (
        gawk -f %1
    exit /b 0
    rem EndFunction

    I can fold the if, else, and the entire function. I could have used “exit” instead of EndFunction, but I imagine you might want multiple exit points. I suppose you could always goto a single exit point, though. And if you did that, you wouldn’t need the “REM EndFunction” line after every function.

  • Jim, your post prompted me to play-around with Notepad++'s user-defined language facilities. As my needs for syntax highlighting are modest, and my need for code-folding is much more important, I have found, that the user-defined-language capability of Notepad++ can *almost * satisfy my needs for working with batch files. The problem is that there is no option on the “Folder & Default” tab to “force at beginning of line” (as there is on the “Comment & Number” tab). Without that, colons beyond the first character of a line (standard batch labels and calling of functions) get a fold glyph in the fold margin. I don’t want to have to aberrate function names to facilitate folding, I want a colon in the first column to be the open fold trigger. ‘exit’ works fine as a folding close indicator.

    I’ve copied the keywords for batch language from langs.model.xml and used those in the “Keywords Lists” tab, to good success. If all I got were keywords and folding, I’d be a happy camper. Alas, the folding definition facilities in Notepad++ are not as comprehensive as comment definition facilities, so it looks like the Notepad++'s user-defined language capability is not going to work. (Read: Feature Request!).

    Noteworthy: Defining line comments as text preceded with a double colon, nicely overrides the single colon so the folding behaves correctly. I.e., comments are correctly distinguished from labels.

  • I have found that using the “Comment & Number” tab to define functions almost solves my problem. I specify “Comment line style” with just the open indicator as ‘::’, and specify the “Comment style” open/close indicators as ‘:’/‘exit /b’. The problem introduced, is that a function defined immediately after a line comment, is not recognized. E.g.:

    :: Line comment
    exit /b

    'func_name does not get a folding glyph in the fold margin.

    I know, I’m now trying to trick Notepad++ into doing what I want it to do.

  • That last attempt (described in my last post) won’t work: It turns the whole function into a comment, meaning no syntax highlighting. Duh! My bad.). I could use that when I need code folding, and then switch to syntax highlighting mode (by selecting a different user-defined language)–sounds like a temporary solution for now.

  • Best solution so far using Notepad++ user-defined language capability (hardly perfect or complete, but useable):

    Ignore case: checked
    Ext: bat

    Folder & Default
    Folder & Default.Folding in code 1 style.Open: :
    Folder & Default.Folding in code 1 style.Close: exit /b

    Comment & Number
    Allow preceeding whitespace: checked
    Allow folding of comments: checked
    Comment & Number.Comment line style.Open: :: echo call

    Operators & Delimiters
    Operators 1
    = % ! & * :~ :% :! :break :loop :end

    Keywords Lists
    rem set if else exist errorlevel for in do break call copy chcp cd chdir choice cls country ctty date del erase dir echo exit goto loadfix loadhigh mkdir md move path pause prompt rename ren rmdir rd shift time type ver verify vol com con lpt nul defined not errorlevel cmdextversion setlocal endlocal

  • Glad you were able to get something usable working.

  • It didn’t work out so well (what I posted last time), so I started instrumenting my batch code more so that it does, the concept being the same though. (E.g., using %{% and %}% to establish the function body).

  • Thank you for great ideas

    I could make some my language but got a Problem.

    I set

    1. comment line: #
    2. folder: #{ and #}

    I expect both comment line and folder work, but its not.
    Just a comment line works and folder do nothing.
    Does anybody has a experience?

  • @lee-jeongwoo

    => I did it wrong way.
    Comment Line Folding works, Not just folding

  • Hello @lee-jeongwoo,

    you need to make it unique. At the moment # is a comment and should be also identified
    as a folding char. What about using ## as comment?


Log in to reply