EDIT ALREADY DEFINED LANGUAGE (C)



  • I would like to change the font color of text inside the
    #if 0
    /* Some codes here (change color)*/
    #endif
    statement but I can’t achieve it. I tried to edit the xml files, lang.xml and stylers.xml but still im not getting what I wanted to do.

    I tried to use the define your language but if I would put #if 0 in the comment & Numbers tab, it will detect #if and 0 individually.

    Thank you in advance for your help :D



  • I assume that you refer to the lexer of the C/C++ language.

    I am using an old version of NPP so perhaps the C lexer changed a bit since but …

    Each lexer defines for its language types and assigns style for each type.
    Some types for C are: number, string, keyword, preprocessor, etc.
    The code within #if 0 is not a type the lexer recognizes so you can’t assign a style for it.
    C lexer is not user defined language. It is implemented in C by itself and it is very complex. You can only extend it in C and recompile scilexer.dll.
    In my opinion, it is not reasonable to define user defined language for C, it is too complex.

    The C lexer, which is taken from Scintilla, does have a few internal options that may be enabled or disabled.
    See LexCPP search for OptionSetCPP. Specifically lexer.cpp.track.preprocessor,
    I don’t think that NPP enables dynamic modification of these options but updating their defaults within the source and recompiling should be doable.



  • As @gstavi pointed out there are some capabilities with the built in lexer. Keep in mind though that it does have limitations but that doesn’t mean it isn’t useful. For example I’ve been able to do stuff like this:

    Note: the gray boxes are related to something else.

    This will gray out the text that is not “defined”. As mentioned this does have limitations, it seems to work better for ifdef as opposed to just if but you can play around with it. It will not parse other header files to figure out what is “defined” but you can add a custom list of defines if there are some you know.

    You will need to install LuaScript through the plugin manager and edit its startup script and add this code:

    npp.AddEventHandler("OnSwitchFile", function(filename, bufferid)
    	local langtype = npp.BufferLangType[bufferid]
    	if langtype == L_C or langtype == L_CPP then
    		editor.Property["lexer.cpp.track.preprocessor"] = "1"
    		editor.Property["lexer.cpp.update.preprocessor"] = "1"
    		--editor.KeyWords[4] = "WIN32=1 WIN32_LEAN_AND_MEAN NOGDI=0"
    
    		local offset = editor.DistanceToSecondaryStyles
    		for i = SCE_C_DEFAULT, SCE_C_ESCAPESEQUENCE do
    			editor.StyleFore[i + offset] = 0x888888
    			editor.StyleBack[i + offset] = editor.StyleBack[i]
    
    			editor.StyleFont[i + offset] = editor.StyleFont[i]
    			editor.StyleSize[i + offset] = editor.StyleSize[i]
    			editor.StyleSizeFractional[i + offset] = editor.StyleSizeFractional[i]
    			editor.StyleBold[i + offset] = editor.StyleBold[i]
    			editor.StyleWeight[i + offset] = editor.StyleWeight[i]
    			editor.StyleItalic[i + offset] = editor.StyleItalic[i]
    			editor.StyleUnderline[i + offset] = editor.StyleUnderline[i]
    		end
    	end
    end)
    
    

    There’s not alot of code here so you can adjust it to fit your needs.

    Keep in mind this is also possible using the PythonScript plugin if you are more familiar with that.



  • @gstavi thanks for the information! Im looking for a quick fix and @dail gave it. Btw, thanks again :D

    @dail! Thank you! It worked like a charm! If I have much time I would like to study lua scripting too! :D It caught my interest :D



  • @dail!
    One more thing though, I am having a problem with other preprocessors
    example:

    FILE: sample.h
    #define SOMETEXT

    FILE: sample.c

    #ifdef SOMETEXT
    /* some code here */ - this part is grayed out
    #endif

    The scenario is some other defines are treated as false. I know that SOMETEXT is only defined at compile time. I don’t want it to be grayed out. only the text included in #if 0 should be grayed out. Thanks! I can’t figure it out because right now I still have a little knowledge about this lua script. Hoping you can lend a hand. Thank you!



  • @janlei0524 I think you may have missed where
    @dail said:

    It will not parse other header files to figure out what is “defined” but you can add a custom list of defines if there are some you know.

    If I’m interpreting that correctly, the

    #define SOMETEXT
    

    must be in the same file as the

    #ifdef SOMETEXT
    

    in order for the /* some code here */ to not be greyed out


Log in to reply