Looking for Scintilla examples for a Notepad++ plugin
-
Hi everyone,
I want to write a Notepad++ plugin that can change the font color and background color of certain specific text. However, using a method like this:
SendMessage(hSci, SCI_STYLESETFORE, SCE_USER1, RGB(255, 0, 0));
is not very efficient, especially when dealing with very large files.
I asked ChatGPT about this, and it suggested that using Lexilla could be much more efficient. I found the Lexilla source code here:
https://github.com/notepad-plus-plus/notepad-plus-plus/tree/master/scintillaHowever, I still failed and was not able to achieve my goal.
Does anyone know of any Scintilla examples, similar to this Notepad++ plugin template?
https://github.com/npp-plugins/plugintemplateThanks in advance!
-
@John-HUANG-0 said in Looking for Scintilla examples for a Notepad++ plugin:
I want to write a Notepad++ plugin that can change the font color and background color of certain specific text. However, using a method like this:
SendMessage(hSci, SCI_STYLESETFORE, SCE_USER1, RGB(255, 0, 0));
Yes, that is the way that Scintilla requires you to set the colors.
is not very efficient, especially when dealing with very large files.
How is that not efficient? You set the color once for a given style index (in your case, whatever integer style number SCE_USER1 is defined as), and then every piece of text in your file that you want styled that way, you use the SCI_SETSTYLING and related messages to tell Scintilla which pieces of text get the styles defined.
I asked ChatGPT about this, and it suggested that using Lexilla could be much more efficient.
That’s not a good representation of reality, as often happens when you expect a LLM to “understand” the concepts involved.
Lexilla is the Scintilla-based library that includes a lexer for each of about a hundred different programming languages. Each uses the various Scintilla commands to apply style numbers to the text, based on each language’s specific requirements, and then Scintilla applies whatever colors that Notepad++ (or whatever other host application uses Scintilla+Lexilla) tells Scintilla to use for that index.
So, it telling you to “use Lexilla” could only be accomplished if the text that you want styled is already in the same format as one of the predefined languages in the Lexilla library (like C++ or Python or whatever). So I don’t think that advice from ChatGPT will be in any way helpful or beneficial.
Does anyone know of any Scintilla examples, similar to this Notepad++ plugin template?
https://github.com/npp-plugins/plugintemplateExamples? Probably. A template like the plugintemplate? No. The expectation is that if you want to build a lexer plugin, you will start with one of the templates for a normal plugin, and then add in the methods described in the Lexer Plugin section of the User Manual plugin description. I believe that CsvLint plugin at least has aspects of being a lexer plugin, and the Papyrus Script Lexer plugin might also be an example; (I would suggest the GEDCOM lexer, but it’s old enough that it might still use the old ILexer4 interface instead of ILexer5; though I don’t know for sure). When the Developer first announced the switch to ILexer5 for lexer plugins, he recommended looking at the PR at https://github.com/poiru/rainlexer/pull/3/files to see what changes were made to an old ILexer4 plugin to make it ILexer5 compatible, so presumably looking at the source code for that lexer plugin would be a good idea, too.
Hopefully that at least gives you a starting point or possible examples.
But if you’re just looking to apply styling briefly, rather than being a full lexer plugin, then the algorithm I would suggest:
- At plugin startup, set the forground/background colors (and any other style attributes) once for each of the styles you are using in your plugin
- When function X of your plugin is called to briefly apply the styles, go through the entire document, or at least the visible text, applying the appropriate SCI_SETSTYLING and similar to map the style numbers to the right piece(s) of text.
I don’t know if I’ve really given you what you want, but hopefully that’s at least a starting point for you.
(Please note: I am not a lexer-plugin author, so I may not have expressed subtleties very well… If someone else comes in with more concrete information, trust their explanation over mine.)
-
@John-HUANG-0 said in Looking for Scintilla examples for a Notepad++ plugin:
I want to write a Notepad++ plugin that can change the font color and background color of certain specific text.
Depending on your needs, the first thing you should consider is whether you really need a plugin, or whether a User Defined Language could do what you want.
However, using a method like this:
SendMessage(hSci, SCI_STYLESETFORE, SCE_USER1, RGB(255, 0, 0));
is not very efficient, especially when dealing with very large files.
When making many calls to Scintilla, it is best to use Direct Access. Even better, if you are writing in C++, is to use the ScintillaCall interface. Annoyingly, that interface does not seem to be documented anywhere. You can read the relevant section in the help for my Visual Studio template and, if it makes sense, either use that template or look at the code (start here) to see how it’s done.
I asked ChatGPT about this, and it suggested that using Lexilla could be much more efficient. I found the Lexilla source code here:
https://github.com/notepad-plus-plus/notepad-plus-plus/tree/master/scintillaI have never attempted to write a lexer, so I can’t give advice there, except to say that unless you are trying to implement full syntax highlighting for a computer language that doesn’t already have a lexer, it’s probably not the way to go. The efficiency problem can be solved by using the direct access interface or ScintillaCall.