Modify the C++ xml language file to include math functions
-
I just want to be able to modify the C++ language file to include math functions. I don’t want to create a new language. I tried to import the cpp.xml and it errors off with filed to import. Surely someone has already added various functions to the existing cpp file. Any help out there?
Peter
-
I know just the person that will come along and help you with this. :-)
-
Hello to another Peter Jones. :-) (Sorry for the delay: I saw your post last night and again this morning, but I was on my phone browser at the time, and this reply needed to wait for being on my PC where I can actually access NPP and its config files.)
I have two theories as to what you want. I will answer both.
First, maybe you are trying to add more functions to the C++ auto-completion XML file in
autoCompletion\cpp.xml
. That’s a little surprising to me, however, because most of the standard math functions (fabs, sin, exp, atan2, etc) are already in the autocompletion keyword list. But there are other math functions that exist (maybe you need the riemann_zeta() function from<cmath>
).If there really are ones missing in there, the way to edit them is to follow the user-manual’s procedure for editing config files (that is, exit Notepad++ completely to make sure config changes are saved, then open
autoCompletion\cpp.xml
, edit, save, exit NPP, and reload to make the change take effect). For auto-completion config files, you need to make sure the keywords are in alphabetical order (for the cpp.xml file, the alphabetization is case-sensitive, soZZZ
comes beforeaaa
).But since you mentioned “filed to import” on the XML, I get the impression maybe that’s not what you mean. I think maybe you tried to take the autoCompletion XML file, and import it into the User Defined Language dialog to add new keywords to the syntax highlighting. That’s not the right way of doing things. There are two problems with that procedure: the first being that the
autoCompletion\cpp.xml
has nothing to do with the syntax highlighting – it just defines words for auto-completion and function-argument hints as you are typing, not defining which words get highlighted as colors – so it’s a completely separate XML from the UDL XML file(s), so you cannot import it in the UDL dialog; the second problem is thinking that User Defined Languages are handled in the same way as the built-in language lexers like C++, and that you can somehow start a UDL based on a built-in lexer’s definition. You can see a couple paragraphs on that difference in this recent discussion.But if all you want is for it to colorize functions like
fabs
andatan2
andexp
, you don’t need to define a UDL; instead, you can use the Settings > Style Configurator to add keywords to the C++ > INSTRUCTION WORDSuser-defined keywords
box. If you start with the default installation, you will see that those keywords aren’t highlighted, as in this screenshot:
So then add in the math keywords in that user input box, and suddenly, those keywords are also highlighted:
Use Save & Close button to make it permanent.Some built-in languages have many of their STYLE keyword lists with user-defined input box; others, like C++, only have one or two (in C++, INSTRUCTION WORD for additional functions, and TYPE WORD if you want to add common typedef’s that you use); other languages, like HTML, don’t have any of the user-defined keyword boxes available, unfortunately.
If you want something a bit more, like using a different color for the math keywords than for the normal C++ INSTRUCTION WORD list, you can add extra highlighting to a builtin lexer (like the C++lexer) using regexes via the PythonScript plugin in conjunction with the script
EnhanceAnyLexer.py
that @Ekopalypse shares in his github repoHopefully, one of those procedures will give you the “to include math functions” that you desire. If not, you will have to be a bit more explicit about what you want.
—
PS: on the name collision: I knew I should have left my accidental @Not-Peter-Jones account (accidentally created from a second Google account) as @PETER-JONES (like it originally was), to avoid other people claiming my name as their user name. ;-) But I changed it to make sure that people wouldn’t get confused when @-mentioning my main account. Now, I guess, it’s unavoidable. ;-)
One of the “benefits” of having a common name like “Peter Jones” is the plethora of others who share our name. I still remember when I was college (lo those many years ago), I had the hassle of proving to the tuition department that they had sent the other Peter Jones’s tuition bill to me (the wrong Peter Jones). Fortunately, the student ID and middle initials on our paperwork was different, and I straightened it up without too much difficulty.
Anyway, good luck with your Notepad++ usage. Maybe someday, every Peter Jones in the world will use Notepad++, and we’ll rule! ;-)
-
Great. I wasn’t aware that I could do the mods that easily. Maybe someone should add a link in Google search to access your solution. It works great!
Peter
-
Peter, I have modified the “CPP” entries in stylers.xml and langs.xml to add a new keyword–stylers.xml I added:
<WordsStyle name=“INSTRUCTION WORD2” styleID=“12” fgColor=“800000” bgColor=“FFFFFF” fontName=“” fontStyle=“1” fontSize=“” keywordClass=“instre2” />I also added a new keyword to the langs.xml:
<Keywords name=“instre2”>cos sin tan sec csc sqrt exp abs fabs pow powf log log10 acos asin atan atan2 min max</Keywords>They show up in the Style Configurator for C++ OK, but the StyleID seems to be an issue: I have changed it from 5 to 8 and 12 ( 8 and 12 weren’t used in CPP). The RED color I chose does not show up–It uses the default black color. If I use 5 then it changes the orginal INSTRUCTION WORD fgcolor to RED. I read in the documenation about StyleID–that you can’t just add a row–Is there a way to do what I am trying to do?
Below is the comment about adding a row and changing the StyleID. I don’t understand the synergy between StyleID and a new keyword; it seems redundant. If you can’t define additional Keywords in the language xml’s, then it seems pretty limiting.
The name attribute contains the same text that shows up in the Style Configurator list. The styleID attribute is used inside the lexer to map the settings to the various arrays accessed in the lexer – do not change that number. The fgColor through fontSize are the user-configurable colors and styles. The keywordClass settings should also not be changed.
“Note that you cannot add a new style to a lexer’s list of styles by just adding a new row, so you cannot just add CUSTOMKEYWORD to the styler xml and expect it to affect any setting; the lexer code has to know what to do with that entry, and can only use its prebuilt styleID.”
-
I have modified the “CPP” entries in stylers.xml and langs.xml to add a new keyword–stylers.xml I added:
<WordsStyle name=“INSTRUCTION WORD2”
…As you quoted later, that’s not going to work. The cpp lexer is coded and compiled with a particular list of styles that it can handle, and adding new rows to the XML will do nothing to change the fact that the lexer doesn’t do anything with those extra rows/styles.
I don’t understand the synergy between StyleID and a new keyword; it seems redundant. If you can’t define additional Keywords in the language xml’s, then it seems pretty limiting.
The langs.xml entry for CPP lists all the default keywords for each of the keyword-based styles. You could add words to that list there, so that they’ll show up in the left/uneditable box instead of the right/user-defined box. The stylers.xml maps the name of the style to a particular styleID: for example, the
<Keyword name='instre1'>
in langs.xml maps to thekeywordClass='instre1'
in stylers.xml, which then maps to thename="TYPE WORD"
for Style Configurator andstyleID="16"
for mapping to the lexer software itself). The lexer software compiled into Notepad++ is hardcoded with what to do with any word that’s within that particular StyleID – and note that some StyleID’s aren’t associated with keywords; they are associated with other syntax (like however a language defines “strings” or similar conecepts). If the lexer software doesn’t have. Also, if stylers.xml has<WordsStyle...>value value value</WordsStyle>
instead of the default<WordsStyle.../>
, and if the lexer was coded to accept the user-defined keywords for a given styleID, then that list of values is the list of extra words added to the user-defined keywords box in the style configurator.Is there a way to do what I am trying to do?
-
Use the user-defined keywords, as I already showed. This uses the same color as the other keywords, but it works, and is the way that Notepad++ was designed to be used. (The user-defined keywords end up being put in stylers.xml, like
<WordsStyle name="INSTRUCTION WORD" styleID="5" fgColor="0000FF" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" keywordClass="instre1">cos</WordsStyle>
if I addcos
to the INSTRUCTION WORD in C++.) -
Use the PythonScript plugin and the script I linked above, which will allow you to add extra highlighting to an existing lexer – which sounds exactly like what you’re asking for; that’s why I presented you that option by saying “If you want something a bit more, like using a different color for the math keywords than for the normal C++ INSTRUCTION WORD list”. For example, you could create a regex that was an or’d list of the words you want(
r'cos|sin|tan|sec|csc|sqrt|exp|abs|fabs|pow|powf|log|log10|acos|asin|atan|atan2|min|max'
), and choose a separate color so that it uses a different color -
Go into the source code repository, copy the C++ lexer, and edit the code to make it a “lexer plugin” with the additional features you want – though I don’t know if it’s 100% compatible to do that: I haven’t heard reports yet of anyone who has successfully done that conversion… so if you do, please let us know.
-
-
Thanks, I thought as much. I think at this time I’ll live with just adding to the users list and use the same color as the INSTRUCTION WORD. I didn’t want to make a project out this, I’d hoped for a simpler solution. I did take a look at the python lexer, but didn’t want to go there, at least for now. If I do get some free time, maybe I’ll try what you said. Seems like othe poeple would have done this by now. In any case thanks for your help.
Thanks again.
-
To any future readers:
There’s actually an option #4 and #5 that didn’t exist, or that I didn’t know about, in 2021:
-
v8.6.6 added the ability to have the GLOBALCLASS style, which gives you a separate keyword list. see this post for how to use it
-
In this post, I show how to use a script to enable the “substyle” feature of Scintilla/Lexilla, which allows adding new keyword lists to certain lexers. Fortunately, the C++ lexer is one. The instructions in that post are for PHP, but if you do the edits in the
CPP_SubstyleLexer
and append the color/keyword-list info to theSCE_C_IDENTIFIER
element, you can have your own custom lists of keywords for C++.
-