Community
    • Login

    How to set a different background color for the text before a certain line?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 4 Posters 217 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.
    • SuperCPUS
      SuperCPU
      last edited by

      Hello everyone,
      Sorry if this was asked before, I posted the question on GitHub first, and I was redirected here - https://github.com/notepad-plus-plus/notepad-plus-plus/issues/17099
      I am editing a text file where the most important part starts after a line containing only the word STARTCODE
      Can Notepad++ change the background color for the text before that? For example, to make it light gray, so I know those lines are not important. Or is it possible to do it with a plugin?

      PeterJonesP mpheathM 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @SuperCPU
        last edited by

        @SuperCPU ,

        The Notepad++ UDL system isn’t quite up to the task by itself, because there’s no way to say “highlight from beginning of line to here if this matches”.

        But there are two ways I can think of, using plugins.

        First, I know the AnalysePlugin has the ability to apply multiple regular expressions to the same file; I don’t use it, but there might be a way to have it automatically apply colors based on the regular expression (but I’m not 100% sure, so you might need to experiment) – but a regex like ^.*STARTCODE should mark from the beginning of the line until the STARTCODE

        Second (and this one I’m more confident of): you could use the UDL feature of Notepad++ in combination with the EnhanceAnyLexer plugin. That plugin defines its rules based on the lexer language (whether it’s a builtin language like Python or a UDL for your custom needs), and doesn’t actually care whether the UDL has any keywords (though you might want to define STARTCODE as a keyword). But with that plugin, you can define a foreground color

        [normal text]
        0x777777 = ^.*STARTCODE
        
        [exact name of UDL]
        0x777777 = ^.*STARTCODE
        

        baef46b0-0347-4bde-b633-8f0e426bfeb0-image.png

        (My screenshot shows it in the [normal text] section, since I didn’t want to create a new UDL for this example, but if you’ve got a UDL called exact name of UDL, then the second section in my post would do it.)

        I had seen your Issue earlier, so already had a mental plan of what I’d say if you ever posted here. But while I was typing up this reply, I realized that maybe my first interpretation was wrong. If you had the text,

        before
        before
        xyz STARTCODE
        blah
        blah
        

        were you intending the gray text to just be the xyz STARTCODE, or did you want to also gray out the before lines?

        If the latter, then your EnhanceAnyLexer line would be more like,

        0xcccccc = (?s)\A.*\bSTARTCODE\b
        

        207ac063-abe9-4a95-8101-eb72f9779811-image.png

        (Either way, make sure to not set the excluded_styles = #, #, .... unless you want to exclude your regex from working when inside one of the other UDL styles. It’s more powerful to be able to restrict it, but for new users, it usually makes more sense to just leave that option out.)

        I hope one of these is what you want.

        SuperCPUS 1 Reply Last reply Reply Quote 3
        • SuperCPUS
          SuperCPU @PeterJones
          last edited by

          @PeterJones
          Thank you for your reply and for all the effort to answer my question!

          Sorry I wasn’t clear enough: I wanted to say that I want to change the background color for all the lines before that line containing the word STARTCODE, so your second answer is what I need.
          However, it works but once I close the EnhanceAnyLexerConfig.ini or I simply start editing the “startcode.txt” file, the graying is gone for those lines before STARTCODE

          And another question is: how can I change the background color instead of the ink color?

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @SuperCPU
            last edited by

            However, it works but once I close the EnhanceAnyLexerConfig.ini or I simply start editing the “startcode.txt” file, the graying is gone for those lines before STARTCODE

            Once you save the config, assuming you used [normal text], then the next time you open a text file, it will use that rule. So if you close startcode.txt and reopen it, it should work right.

            And another question is: how can I change the background color instead of the ink color?

            EnhanceAnyLexer only affects foreground colors, not background colors. Sorry.

            SuperCPUS 1 Reply Last reply Reply Quote 1
            • mpheathM
              mpheath @SuperCPU
              last edited by

              @SuperCPU

              LuaScript plugin code added to startup.lua can add an indicator from the start of document up to the start of the line with STARTCODE.

              function Event_StartCodeTag()
                  -- Add indicator from position 0 to start position of STARTCODE.
              
                  if editor.LexerLanguage == 'null' then
                      local indic = 3
              
                      if editor:IndicatorEnd(indic, 0) == 0 then
                          local start_pos = editor:findtext('^STARTCODE$', SCFIND_REGEXP |
                                                                           SCFIND_MATCHCASE)
                          if start_pos then
                              editor.IndicatorCurrent = indic
                              editor:IndicatorClearRange(0, start_pos)
                              editor.IndicStyle[indic] = INDIC_ROUNDBOX
                              editor.IndicFore[indic] = 0xC0C0C0
                              editor.IndicAlpha[indic] = 100
                              editor:IndicatorFillRange(0, start_pos)
                          end
                      end
                  end
              end
              
              function Event_Ready()
                  -- Called when the editor is ready.
              
                  Event_StartCodeTag()
                  npp.AddEventHandler('OnSwitchFile', Event_StartCodeTag)
              end
              
              npp.AddEventHandler('OnReady', Event_Ready)
              

              This will try to handle any document using the null lexer. That is the None (Normal Text) item in the Language menu.

              It is set to use indicator 3 with a INDIC_ROUNDBOX style with a colour of 0xC0C0C0 which is light grey. The alpha is 100 and can be set between 0 and 255. The OnReady event is to ensure the indicators are not being applied until Notepad++ is ready.

              Viewed with the default theme:

              startcode.png

              Other scripting plugins should be able to apply indicators with similar code.

              SuperCPUS 1 Reply Last reply Reply Quote 3
              • SuperCPUS
                SuperCPU @PeterJones
                last edited by

                @PeterJones
                Thank you
                I am doing something wrong, I’ve added that line under the section [normal text] and it works but once I start editing the startcode.txt file, the change is gone. Also reopening it doesn’t help.

                EkopalypseE 1 Reply Last reply Reply Quote 0
                • SuperCPUS
                  SuperCPU @mpheath
                  last edited by

                  @mpheath
                  Thank you, it works well
                  Do you have a solution for changing the background color for the lines after keyword “ENDCODE” too?

                  Another question is if it’s possible to do this but also for empty lines and for the entire width of the window, not only for the length of each line? For example, in your sample text, the line “Oh, so boring.” makes the gray background shorter than for the previous line (because the line is shorter). However, I would like to set the gray background till the end of the window width. Because at the moment, the text before “STARTCODE” has gray background only where there are characters, the empty lines and the rest of the space is still white.

                  mpheathM 1 Reply Last reply Reply Quote 0
                  • EkopalypseE
                    Ekopalypse @SuperCPU
                    last edited by

                    @SuperCPU said in How to set a different background color for the text before a certain line?:

                    but once I start editing the startcode.txt

                    The EnhanceAnyLexer plugin only processes visible lines.
                    Therefore, if a regex is used that affects a match outside this range,
                    it will be ignored unless the offset parameter is set in the configuration.
                    The reason for this is that the plugin reacts to changes in the text or position and repeatedly executes the respective regular expressions.
                    The more lines that need to be taken into account, the longer this takes.

                    1 Reply Last reply Reply Quote 2
                    • mpheathM
                      mpheath @SuperCPU
                      last edited by

                      @SuperCPU

                      Do you have a solution for changing the background color for the lines after keyword “ENDCODE” too?

                      A solution requires planning and development though only STARTCODE was previously mentioned. Now may need to revise the solution to what satisfies the new requirement. Indicators cannot do the entire width of the window though styles can do EOL filled styling.

                      A UDL might be adequate with a little styling enhancement with LuaScript.

                      1. Define a new UDL from menu Language -> User Defined Language -> Define your language...
                      2. Set Default style backgroud to grey. Need to click styler button to access the styler dialog.
                      3. Go to delimiters tab and for style 1 set Open as STARTCODE and Close as ENDCODE.
                      4. Optionally. Click styler button and set background to a preferred color or set as transparent.
                      5. Save UDL with a name.

                      The lua script:

                      local function EnhanceDoc()
                          -- Enhance UDL with STARTCODE.
                      
                          if editor.LexerLanguage == 'user' then
                              local start_pos = editor:findtext('^STARTCODE$', SCFIND_REGEXP |
                                                                               SCFIND_MATCHCASE)
                              if start_pos then
                      
                                  -- Style 0.
                                  local bg0 = editor.StyleBack[0]
                                  editor.StyleEOLFilled[0] = true
                      
                                  -- Style 16: Delimiter 1.
                                  editor.StyleEOLFilled[16] = true
                      
                                  -- Style 24: Terminator.
                                  editor.StyleBack[24] = bg0
                                  editor.StyleEOLFilled[24] = true
                              end
                          end
                      end
                      
                      
                      npp.AddEventHandler('OnReady', function()
                          EnhanceDoc()
                          npp.AddEventHandler('OnSwitchFile', EnhanceDoc)
                      end)
                      

                      May need to restart Notepad++ after setup of the UDL and the Lua code. Once the editor is ready and the document is active, then set the language to the UDL.

                      Viewed with the default theme:

                      startcode2.png

                      The Python console shows the Style IDs in red so you know where the 0, 16 and 24 originate from.

                      Other scripting plugins should be able to apply styles with similar code.

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors