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++:
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.