• Login
Community
  • Login

EDIT ALREADY DEFINED LANGUAGE (C)

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 4 Posters 3.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J
    janlei0524
    last edited by Feb 22, 2017, 2:13 AM

    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

    1 Reply Last reply Reply Quote 0
    • G
      gstavi
      last edited by Feb 22, 2017, 7:06 AM

      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.

      1 Reply Last reply Reply Quote 0
      • D
        dail
        last edited by dail Feb 22, 2017, 2:24 PM Feb 22, 2017, 2:15 PM

        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.

        G 1 Reply Last reply Feb 24, 2017, 3:23 AM Reply Quote 2
        • J
          janlei0524
          last edited by Feb 24, 2017, 2:13 AM

          @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

          1 Reply Last reply Reply Quote 0
          • J
            janlei0524
            last edited by Feb 24, 2017, 3:02 AM

            @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!

            1 Reply Last reply Reply Quote 0
            • G
              glennfromiowa @dail
              last edited by Feb 24, 2017, 3:23 AM

              @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

              1 Reply Last reply Reply Quote 3
              5 out of 6
              • First post
                5/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors