Recent

Author Topic: Writing plugins for Notepad++  (Read 7583 times)

Irvine

  • New member
  • *
  • Posts: 8
Writing plugins for Notepad++
« on: October 15, 2014, 05:01:46 pm »
I have a “Howdy world” demo and a set of packages for writing Notepad++ plugins with Lazarus. From an internet search, I know that many people on this site worked on the original package and I hope they can help clear up an irritating technical issue.

I have got the demo working and have started on my own project to write a “LangaugeTool” plugin. As things stand, things are going reasonably well: I can compile a dll which doesn't crash Notepad++: use it to open and close forms, and generally test out ideas. The problem I am having is with the procedure ThelloWorldPlugin.SetToolbarIcon(); It is declared in “HelloWorldPlugin” and is somehow associated with the procedure ThelloWorldPlugin.HelloWorldDocking;

My problem is, I cannot see how it is done. I have been studying both the “HelloWorldPlugin” and “Helloworldockingform” units with no success, I even tried searching all the files in the demo, but the procedure is only mentioned twice: Once in the Type declaration for “ThelloWorldPlugin” and once to actually define its code.

In case you are wondering, I would like to use the toolbar icon to send user selected text to a local host server on port 8081. For initial testing and debugging during the design phase, I am just writing a greeting:

procedure TLT_plugin.LangTool;
var
  s: string;
begin
  s := 'Welcome to LanguageTool';
  GetNPPPluginInstance.SendToNppScintilla(SCI_REPLACESEL, 0, LPARAM(PChar(s)));
end;

This works well from the plugin menu, but I would like to get it to work from the toolbar before I start expanding it out.

Thankyou for your time

Irvine

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Writing plugins for Notepad++
« Reply #1 on: October 16, 2014, 08:24:12 am »
Haven't looked at that code for a long time.... Have you looked in the NotepadPPPluginPackage directory? The HowdyWorldDemo depends on the .lpk package there where a lot of functionality is present.

A grep search through that directory shows the SetToolbarIcon string is present in nppplugin.pas
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Irvine

  • New member
  • *
  • Posts: 8
Re: Writing plugins for Notepad++
« Reply #2 on: October 16, 2014, 04:24:12 pm »
Thanks for the reply, (and also the original demo.)

I noticed what you said about npppplugin.pas late last night, it's a virtual procedure which is called by  DoNppnToolbarModification, (which is marked in the readme as an area for possible expansion.) Anyway, this procedure is in turn called by BeNotified etc...

On another track: for reasons I do not understand, my LanguageTool icon has miraculously appeared on the tool-bar. The only change I made was to add an HTML file to the RC script. The problem is, it is still associated with the docking form....  I was intending to use this form as a secondary window to display and hold the results of a grammar check, so, I really need to work out where  the docking form is attached to the toolbar icon.

Also, without criticism, and noting I think the package is great, there is a problem with the AboutForm in the Howdy World Demo. Basically it is possible to open multiple instances of the form, and, if Notepad++ is then closed, the memory is never freed.

Again, I am not sure what is happening with the demo AboutForm: In the demo, button1, (the "okay" button which closes the form,) is declared as an AboutForm object. Yet there is no procedure, nor click event, associated with the button in either the aboutForm unit nor the object inspector. Testing in my own project, when such a button is added to a blank TNppform, it does nothing unless I add a click event?

All in all, it looks as if I am going to have to dig really deep into the NppPlugin package. This is something I really hate doing, and, as an original author, any advice you care to give will be much appreciated.

Irvine

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Writing plugins for Notepad++
« Reply #3 on: October 16, 2014, 04:32:00 pm »
Hi Irvine,

No offence taken. TBH, I've completely forgotten everything about that plugin except that the Delphi application handle or something can be easily replaced/simulated by the Lazarus window handle... or something. Goes to show.

Yes, to understand what's going on you'd need to dive in. That said, IIRC the code is about the bare minimum to get things done.
(Re the about form: could be that the plugin registers a callback procedure with notepad++ which notepad++ then calls to display the about box...)

Any improvements gratefully received (e.g. patches, updated versions - I can run a diff tool over it to get the changes).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Irvine

  • New member
  • *
  • Posts: 8
Re: Writing plugins for Notepad++
« Reply #4 on: October 17, 2014, 11:32:23 am »
Okay, just an update in case anyone has similar problems.

After studying the Npppackage for over 24 hours, I had a brainstorm, and realised there is a subtle trap with any demo. Either consciously or unconsciously, one tends to try and model ones own code after the demo; however, by its nature, the demo tries to keep things simple, neat and separate.

As a result, I was trying to be too literal: ie writing a module to check the text, which in turn calls a module to present the results. In fact, what I really needed to do was call "text checking" from my equivalent of "HelloWorldDocking". To put this another way, rather than have the "docking window" a sub module of "text checking", "text checking" should be a sub module of the "docking window", (it's so obvious...)

I have done a few tests and this seems to work, all I need to do is write code to make the actual call to the "docking window" conditional on having valid results from "text checking".

Now that that problem is solved, before I go any further I need to figure out what is happening with the demo AboutForm and how to prevent the potential memory leak. I have a few ideas about this and will keep you posted.

Irvine

PS
With regard to posting any changes to either the demo or the package, I will certainly do so.

Edit
PPs
By the way, if anyone is trying to figure out where the link between the toolbar icon and the docking window is made, it is done when the docking window is registered. It is not easy to change this, since a pointer to the ToolbarIcon record is used as a handle for the docking window, (or something like that.)
« Last Edit: October 17, 2014, 11:42:29 am by Irvine »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Writing plugins for Notepad++
« Reply #5 on: October 17, 2014, 11:54:17 am »
Thanks for posting! Good luck with your investigations.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Writing plugins for Notepad++
« Reply #6 on: October 17, 2014, 12:04:41 pm »
I'm still using it from the time it was ported from Delpi to Lazarus.
Getting new functionality to work is indeed quite fiddly.
Mostly because of lacking N++ API documentation.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Irvine

  • New member
  • *
  • Posts: 8
Re: Writing plugins for Notepad++
« Reply #7 on: October 17, 2014, 12:22:04 pm »
...Getting new functionality to work is indeed quite fiddly.
Mostly because of lacking N++ API documentation.

Tell me about it. It is not just the lack of documentation for the NP++ API, it is the lack of easily searchable scintilla documentation. I have been using the documentation for Pyscripts, (see below,) which contains a lot of relevant material and useful links.

http://npppythonscript.sourceforge.net/docs/latest/index.html

Irvine

  • New member
  • *
  • Posts: 8
Re: Writing plugins for Notepad++
« Reply #8 on: October 18, 2014, 01:14:29 pm »
Firstly, I figured out that the reason the demo does not use a click event for Button1 is because it was set to modal, with the code to close the form in the calling procedure  in HelloworldPlugin. This not only achieves nothing, it is the root of the whole problem.

Basically, launching a process is the responsibility of Np++, which is not affected by any modal settings, BUT, closing a process is the responsibility of the plugin, which is affected by modal results. In other words there can be multiple objects launched even though they are all modal. The really nasty stuff happens if you try to close Np++ before you close a modal object. Np++ passes the control back to the plugin so that it can destroy any existing objects, but because the objects are modal, they still have control and the close procedures are locked out.

Some basic tests to illustrate:

1) Click on the original plugin menu About. Click anywhere on Np++, Np++ has the focus and you can launch a second About form or another plugin process.

2) Leaving the About form open, try closing Np++. With the TaskManager process window, you can see that things have now gone crazy, and Np++ is eating system resources. Switch to the Task manager applications window and you will see this is because the HelloWorld dll is still alive.

There is more, but I think this is enough to give a taste of the potential problems.

I have attached an updated demo, which I believe fixes the problem. Alongside the fixes, there are some explanatory notes outlining why it is not a good idea to use modal results for NpppForms. I have put my name, (Irvine), to these notes so that you can see at a glance what I have changed.

Irvine

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Writing plugins for Notepad++
« Reply #9 on: October 19, 2014, 11:16:30 am »
Thanks a lot - putting your post on my to do list ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Writing plugins for Notepad++
« Reply #10 on: October 22, 2014, 02:09:03 pm »
Thanks, committed fixes in commit b9b0cc415a94
https://bitbucket.org/reiniero/notepad-pluginlazarus
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018