Looking for tips/directions on how to best approach creating a custom plugin for Notepad++
-
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
the next step is to try and make the script work in PythonScript Notepad Plugin
@Alan-Kilborn provided a nice simple template to start from.
I’m not a Python expert, prefer to mock my Scintilla testing in NppExec, but I’m becoming more comfortable with Python and thus PythonScript in N++. I also used Alan’s class-based approach when creating PythonScripts for N++ so that I can start, stop and get status from the PythonScript console.
Cheers.
-
Hey Guys,
sorry for late response. I continue to learn python and I tried to use PythonScript to write some code of my own to do the desired searches. However, no luck. One of the problem that I encounter is that PythonScript requires somewhat different commands than regular Python code (please correct me if I’m wrong, still a newb here!). For example, I never saw this function “editor” in Python, but it seems like it’s related to Notepad/Scintilla. Many of my code lines does not work, because I don’t understand how python language and PythonScript plugin are related to each other.
Here is an example:
I found in PythonScript documentation some guidelines to help me write some elementary code to just test how things work. And while the second line works fine and does what it’s supposed to, I could not get the first one to work at all, was getting errors constantly (that either it’s a syntax error, something is not defined, and a whole bunch of others).
I then started reading about plugin templates for NP++, and (please, again correct me if I’m wrong) that they have to be written at least in part in C++ or with the use of cython. I like learning python and will continue to, but I do not feel that I can handle learning C++ at this point, seeing how python gives me trouble.
I would also not want to lose on the great functionality of the search result window from Notepad++, that it provides on it’s own.
Here is what I mean. This is a pic of some of the regex searches I wrote and store in shortcuts.xml:
and here is how the regexes look as stored in shortcuts.xml:
I got a whole bunch of these saved regex searches, but when the search results in NPP look like in the screen above, where next to the number of hits the regex formula itself is displayed, it does not tell anything to colleagues who don’t know the regex syntax. And this in turn makes it very hard for them to understand what the searches are actually searching for.
So the idea behind the plugin was the be able to “name” those searches with custom names, like “These numbers appear to be abnormal, please check them with documents”, and so on. I thought that this would the first functionality of the plugin, and using python would allow for greater control over searching and replacing. But maybe I’m mistaken and trying to develop a plugin is not the best way to go about making this tool (regex compilation) more accessible to others. Is it even possible to name the searches (like in the last screen) with some custom names, so that instead of “[0-9]{4} > [0-9]{4} .*{5}” the search name is displayed like “number of items above required stock” (to just give an ad hoc example) ? -
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
For example, I never saw this function “editor” in Python, but it seems like it’s related to Notepad/Scintilla.
It is. At the top of your Npp PythonScript scripts, add the line:
from Npp import *
and that should be good enough to get past that “error” you’re now seeing.
Cheers.
-
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
I got a whole bunch of these saved regex searches, but when the search results in NPP look like in the screen above, where next to the number of hits the regex formula itself is displayed, it does not tell anything to colleagues who don’t know the regex syntax. And this in turn makes it very hard for them to understand what the searches are actually searching for.
Workaround. Not pretty, but may be useful. Create your RegEx with the
(?x)
modifier to allow inline comments and free space. So here’s an example in my ‘shortcuts.xml’:<Macro name="Mike Test" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?x) # Find Header (?m)(?'PACKAGE_HEADER'^(?-i:package\b))(?s:.*?)(?=\s*(?:(?&PACKAGE_HEADER)|\Z))" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="3" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1636" sParam="" /> </Macro>
Notice I use the
(?x)
modifier followed by a#
to introduce the comment describing what the RegEx does. Then a line break and the actual RegEx.Running that macro in N++ produces:
which of course still shows the complex RegEx in the search results, but notice the line “before” it is the nice comment.
Does that help a little?
Cheers.
-
@michael-vincent said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
Does that help a little?
This helps a ton!! Thank you! I will add these comments/descriptions to all current regexes and this will give me plenty of time to work on the actual plugin in python and taking my time to understand it all properly, without worrying that my colleagues are losing out on some of the functionalities of our tools!
This will be a game changer and a first step in a new direction. Thank you again! -
One more question, while I still remember it. Is it possible, in any way, even a crude one at the moment (due to my lack of skills), that if one of the search regex finds a string, NPP would prompt a user with a pop up window/message in any other form, that “hey, this has been found in your file, you might want to take a look at it”?
-
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
One more question, while I still remember it. Is it possible, in any way, even a crude one at the moment (due to my lack of skills), that if one of the search regex finds a string, NPP would prompt a user with a pop up window/message in any other form, that “hey, this has been found in your file, you might want to take a look at it”?
I don’t think so with just the Macro approach. If you implement the searches in PythonScript, then yes absolutely possible as with any other scripting solution (NppExec for example) and certainly with a proper plugin.
Cheers.
-
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
Hey Guys,
sorry for late responseThere’s a lot going on with this thread starting with “Hey Guys”.
I’ll try to address some things further, in this and subsequent postings in this thread…@michael-vincent said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
Create your RegEx with the (?x) modifier to allow inline comments and free space
But using
(?x)
at the start can affect the meaning of what follows, unless you use(?-x)
at some point after.Why not avoid that possibility and just use the comment specifier, example:
(?#hello there I'm a comment)
at the start of the regex? -
@michael-vincent said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
if one of the search regex finds a string, NPP would prompt a user with a pop up window/message in any other form, that “hey, this has been found in your file, you might want to take a look at it”?
I don’t think so with just the Macro approach. If you implement the searches in PythonScript, then yes absolutely possible as with any other scripting solution (NppExec for example) and certainly with a proper plugin.
No way to do this in a macro, as stated. PythonScript has two functions for prompting the user:
notepad.prompt()
notepad.messageBox()
Each returns data which can impact script logic flow after the prompting box window is closed.
-
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
I would also not want to lose on the great functionality of the search result window from Notepad++, that it provides on it’s own.
So that window and PythonScript don’t typically play well together. If you script searches, you have to find your own way of presenting the output to the user.
-
@alan-kilborn said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
Why not avoid that possibility and just use the comment specifier, example: (?#hello there I’m a comment) at the start of the regex?
Much nicer solution!
-
Plugins are usually shared libraries (dll) and creating them with Python is not as easy as with a language that is supposed to create them natively, like C++, C, D, Rust, C#, V or others. Yes, one way to create them is with Cython or also with cffi.
But I would recommend using another language instead, because as far as I know, including Python 3.10, there is still a problem if you want to embed more than one Python interpreter into a running process, which means that as long as you are the only one writing plugins for your own use, it’s fine, but as soon as another plugin developer decides to do so as well or someone wants to use the existing PythonScript plugin, then the problems start.
-
Thank you. As a temporary band-aid fix for this (until I learn how to do the thing properly), is it possible in NPP to display only the search results that have > 0 hits, and hide all searches that returned 0 results?
-
@slusher59
No, as far as I know there is neither a built-in option nor a plugin message that would allow you to do this. And hacking it might be a bad idea because it could then conflict with subsequent searches. -
@slusher59 said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
is it possible in NPP to display only the search results that have > 0 hits, and hide all searches that returned 0 results?
As I said before:
@alan-kilborn said in Looking for tips/directions on how to best approach creating a custom plugin for Notepad++:
So Search results window and PythonScript don’t typically play well together.
If you script searches, you have to find your own way of presenting the output to the user.
One way of doing this is having your script write output to a new (file) tab that it creates. That way you have total control of what goes into the output. You can even make it appear a lot like the usual Search results. If you want to get really fancy, you could even set it up so that double-clicking in this output would open the file where the hit was found.
It just depends if the time you want to spend on this is better spent scripting or developing a true plugin. But even a true plugin isn’t going to have an easy time of doing custom things with Notepad++'s Search results area.