Need help in enabling batch language outlining
-
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:
:FunctionStart if !%1 == ! ( echo. echo Supply at least one argument! echo. ) 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
:func_name
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: batFolder & Default
Folder & Default.Folding in code 1 style.Open: :
Folder & Default.Folding in code 1 style.Close: exit /bComment & Number
Allow preceeding whitespace: checked
Allow folding of comments: checked
Comment & Number.Comment line style.Open: :: echo callOperators & Delimiters
Operators 1
= % ! & * :~ :% :! :break :loop :endKeywords 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
- comment line: #
- 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? -
=> 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?Cheers
Claudia