• Login
Community
  • Login

How do you color certain text after a keyword in notepad++?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
udl
31 Posts 5 Posters 3.8k 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.
  • L
    Leahnn Rey
    last edited by Aug 9, 2022, 6:12 AM

    Basically i wanted my user defined language to have certain highlighting after a certain keyword.
    For example, in Python, when i define a function:
    florida.PNG
    You can clearly see the text after the ‘def’ keyword in python is colored orange. When i do that in a user defined language, such as creating a function:
    minnesota.PNG
    You can clearly see that the text after ‘function’ is not highlighted. (Keep in mind i have my global foreground set to yellow). I want the text after ‘function’ to be highlighted a bluish-like color. How may i do this?

    E 1 Reply Last reply Aug 9, 2022, 8:58 AM Reply Quote 0
    • E
      Ekopalypse @Leahnn Rey
      last edited by Aug 9, 2022, 8:58 AM

      @Leahnn-Rey

      In its current state, UDL does not support this.
      One way to achieve your goals would be to use the EnhanceAnyLexer plugin.

      L 1 Reply Last reply Aug 9, 2022, 10:41 AM Reply Quote 1
      • L
        Leahnn Rey @Ekopalypse
        last edited by Aug 9, 2022, 10:41 AM

        @Ekopalypse Alright, can i do that by modifying the xml files then? i have some experience in XML, i even modified the autoCompletion.xml just for my UDL, how may i do this then? Yes, i know about the plugin, but i don’t know how to use it.

        E 1 Reply Last reply Aug 9, 2022, 1:02 PM Reply Quote 1
        • E
          Ekopalypse @Leahnn Rey
          last edited by Aug 9, 2022, 1:02 PM

          @Leahnn-Rey

          If you have installed the latest version, use the plugin menu
          and select “Enhance current language”.
          The configuration file will open and you should see something like

          [NAME_OF_YOUR_UDL]
          ; color each word
          0x66ad1 = \w+
          ; check in the respective styler xml if the following IDs are valid
          excluded_styles = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20,21,22,23
          

          Now you can change the regex \w+ and see the result immediately in the other view where your udl code is. Does this make sense to you?

          L 1 Reply Last reply Aug 9, 2022, 1:25 PM Reply Quote 2
          • L
            Leahnn Rey @Ekopalypse
            last edited by Aug 9, 2022, 1:25 PM

            @Ekopalypse Doesn’t work for some reason, Maybe because i don’t have my UDL in the stylers.xml.

            E 1 Reply Last reply Aug 9, 2022, 1:27 PM Reply Quote 1
            • E
              Ekopalypse @Leahnn Rey
              last edited by Aug 9, 2022, 1:27 PM

              @Leahnn-Rey

              does the configuration file open as described?

              E 1 Reply Last reply Aug 9, 2022, 1:43 PM Reply Quote 0
              • E
                Ekopalypse @Ekopalypse
                last edited by Aug 9, 2022, 1:43 PM

                Some demo

                L 3 Replies Last reply Aug 10, 2022, 7:32 AM Reply Quote 5
                • L
                  Leahnn Rey @Ekopalypse
                  last edited by Aug 10, 2022, 7:32 AM

                  @Ekopalypse Thanks, I’ll try later.

                  1 Reply Last reply Reply Quote 0
                  • L
                    Leahnn Rey @Ekopalypse
                    last edited by Aug 10, 2022, 7:34 AM

                    @Ekopalypse Also, what are the codes you are entering? Looks like hexadecimals.

                    E 1 Reply Last reply Aug 10, 2022, 10:47 AM Reply Quote 0
                    • E
                      Ekopalypse @Leahnn Rey
                      last edited by Aug 10, 2022, 10:47 AM

                      @Leahnn-Rey

                      Yes, this is a hexadecimal value that specifies the color used to highlight the matches.
                      To quote the documentation from the configuration file:

                      ; Each configured lexer must have a section with its name,
                      ; (NOTE: use the menu function "Enhance current language" as it takes care of the correct naming)
                      ; followed by one or more lines with the syntax
                      ; color = regular expression.
                      ; A color is a number in the range 0 - 16777215.
                      ; The notation is either pure digits or a hex notation starting with 0x or #, 
                      ; such as 0xff00ff or #ff00ff.
                      ; Please note: 
                      ; * red goes in the lowest byte (0x0000FF)
                      ; * green goes in the center byte (0x00FF00)
                      ; * blue goes in the biggest byte (0xFF0000) 
                      ; * this BGR order might conflict with your expectation of RGB order.
                      ; * see Microsoft COLORREF documentation https://docs.microsoft.com/en-us/windows/win32/gdi/colorref
                      
                      1 Reply Last reply Reply Quote 1
                      • L
                        Leahnn Rey @Ekopalypse
                        last edited by Aug 10, 2022, 1:39 PM

                        @Ekopalypse Works like a charm, now, how do i make it so that for example, i want my text to be colored blue after the keyword ‘function’. I have absolutely no idea what an .ini file is.

                        P 1 Reply Last reply Aug 10, 2022, 1:55 PM Reply Quote 0
                        • P
                          PeterJones @Leahnn Rey
                          last edited by PeterJones Aug 10, 2022, 1:57 PM Aug 10, 2022, 1:55 PM

                          @Leahnn-Rey ,

                          I have absolutely no idea what an .ini file is.

                          The .ini file is the configuration file that you opened when you clicked Plugins > Enhance Any Lexer > Enhance current language – ie, the file that has the color codes in it.

                          i want my text to be colored blue after the keyword ‘function’.

                          Then you need to come up with a regular expression (regex) that matches the word after the keyword function. In the .ini file, the color goes on the left of the equal sign, and the regex goes on the right. So in the original example that Enhance current language created for you, 0x66ad1 = \w+:

                          • the color is 0x66ad1, which means 6 units of blue, 6a=106 units of green, and d1=209 units of red;
                          • the regex is \w+ which means “one or more word characters” (where a word character is defined as letters, numbers, and underscore).

                          So you would need to create a regex that does what you want. Those regex follow the same rules for Notepad++'s regular-expression search, as defined here in the user manual .

                          To give you a freebie, you want it to require the prefix function , then match one or more word characters: that would look like function \w+ . But that changes the color of the function keyword as well as the function’s name, which is probably not what you want.

                          Instead, you will want to either “reset ” the regex between with a \K (everything before the \K is “thrown out” after it matches, so using function \K\w+ will match function-space, throw it out, and then match-and-color one or more word charaters) or use a positive lookbehind with (?<=...) (so in your case, lookbehind for function-space, then normal match on one or more words would be (?<=function )\w+ ).

                          Hope this helps.

                          L 2 Replies Last reply Aug 10, 2022, 1:59 PM Reply Quote 4
                          • L
                            Leahnn Rey @PeterJones
                            last edited by Aug 10, 2022, 1:59 PM

                            @PeterJones Thank you so much Peter. You have no idea how much this is gonna help me.

                            1 Reply Last reply Reply Quote 1
                            • L
                              Leahnn Rey @PeterJones
                              last edited by Aug 10, 2022, 2:00 PM

                              @PeterJones If i wanted to, can i add multiple ones? such as void or function?

                              P 1 Reply Last reply Aug 10, 2022, 3:03 PM Reply Quote 0
                              • A
                                Alan Kilborn
                                last edited by Alan Kilborn Aug 10, 2022, 2:01 PM Aug 10, 2022, 2:01 PM

                                You have no idea how much this is gonna help me.

                                I think the discussion in this thread has also helped others understand the EnhanceAnyLexer plugin a little better, too. I know it has helped me.

                                E 1 Reply Last reply Aug 11, 2022, 8:19 AM Reply Quote 1
                                • P
                                  PeterJones @Leahnn Rey
                                  last edited by Aug 10, 2022, 3:03 PM

                                  @Leahnn-Rey said in How do you color certain text after a keyword in notepad++?:

                                  If i wanted to, can i add multiple ones? such as void or function?

                                  Yes.

                                  There are two ways: you could either do a separate color = regex pair, if you want them each different colors (or even the same color but simple maintenance), or you could build a more complicated regex

                                  0x66ad1 = function \K\w+
                                  0x22CD7 = void \K\w+
                                  

                                  vs

                                  0x66ad1 = (?<=function |void )\w+
                                  
                                  L 1 Reply Last reply Aug 10, 2022, 3:17 PM Reply Quote 0
                                  • L
                                    Leahnn Rey @PeterJones
                                    last edited by Aug 10, 2022, 3:17 PM

                                    @PeterJones If i do that, it just says “Invalid lookbehind assertion encountered in the regular expression.”

                                    P 1 Reply Last reply Aug 10, 2022, 3:22 PM Reply Quote 0
                                    • P
                                      PeterJones @Leahnn Rey
                                      last edited by PeterJones Aug 10, 2022, 3:25 PM Aug 10, 2022, 3:22 PM

                                      @Leahnn-Rey ,

                                      0x66ad1 = (function|void) \K\w+
                                      

                                      Sorry, I had forgotten that alternation is not a “fixed length lookbehind”: I remembered you couldn’t have + or * or ? modifiers in a lookbehind, but you also cannot have the alternation if the lengths of the alternates are different. Switch to alternation with the \K, as I show in this reply, gets rid of that problem (because \K doesn’t have the fixed-width requirement)

                                      L Neil SchipperN 2 Replies Last reply Aug 11, 2022, 3:59 AM Reply Quote 0
                                      • L
                                        Leahnn Rey @PeterJones
                                        last edited by Aug 11, 2022, 3:59 AM

                                        @PeterJones Didn’t work. It works when i do:

                                        0xb0c94e = void \K\w+
                                        0xb0c94e = function \K\w+
                                        

                                        So i will keep it that way. I’m having an issue with the positive lookahead. Since i like monokai (Which is the theme i’m using) i want my text to be colore green when there is a () at the end. But, when i do:

                                        0x600e6a3 = (?=\(\))\w+
                                        

                                        It doesn’t color the text before the parenthesis. Am i doing something wrong here?
                                        NOTE: i read the documentation

                                        Neil SchipperN 1 Reply Last reply Aug 11, 2022, 8:13 AM Reply Quote 0
                                        • Neil SchipperN
                                          Neil Schipper @Leahnn Rey
                                          last edited by Aug 11, 2022, 8:13 AM

                                          @Leahnn-Rey

                                          This should work: \w+(?=\(\))

                                          Think of the look-ahead as a kind of modifier of the active (ie, already specified, and so, to the left of the look-ahead) match specification.

                                          L 2 Replies Last reply Aug 11, 2022, 1:05 PM Reply Quote 2
                                          3 out of 31
                                          • First post
                                            3/31
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors