Need help in enabling batch language outlining
-
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