Creating a plug-in for Padre, the Perl IDE
A 12 minutes long video and screencast on how to create a plugin for Padre, the Perl IDE. Introducing My plugin as it works in verion 0.84 of Padre.
From the very beginning Padre had a plugin system to encourage users to add new features. The editor comes with a skeleton of a plugin called My to make it easy to get started.
The menu option Tools -> Plug-in Tools -> Edit My Plug-in will copy read-only the skeleton file to the configuration area of Padre and will open it in the editor. In order to actually use what is in the plugin you have to enable it via the Tools -> Plug-in Manager by selecting the name of the plugin and clicking on the Enable button.
Once enabled, you can access the menu items provided by the plugin via Tool -> My Plugin.
Looking at the source code of the plugin you can see the plugin_name method that returns the name of the plugin. Putting an & sign in-front of a character will make that character to be a hot-key. Of course in order for you change to take effect you will need to reload the plug-in. You can do that by clicking on the Tools -> Plug-in Tools -> Reload My Plug-in.
The menu_plugins_simple method returns the menu system of the plugin. The first value is the actual name of plugin the second value is an ARRAY reference in which every odd value is the name of a menu item and every even value is a subroutine reference that will be called when the menu item is selected. You need to uncomment it and reload My plug-in in order to see how it works.
The actual implementation is further down in the file called other_method.
It shows several tools to make it easier to write a simple plugin.
The message method of the $main object of Padre creates a pop-up window with a title and some text.
$main->message( 'Hi from My Plugin', 'Other method' );
The prompt method provide a place to enter a value and it will even remember to previously typed value in a unique variable which is the 3rd parameter to the method.
my $name = $main->prompt('What is your name?', 'Title', 'UNIQUE_KEY_TO_REMEMBER');
The Padre::Current class has a method called document that returns, surprisingly, the object of the currently edited document (file).
my $doc = Padre::Current->document;
One has to be careful and check it for definedness but if there is a document currently open then this will contain the relevant object. The text_get method on the document object will return the content of the editor:
my $text = $doc->text_get;
The filename method returns the name of the file:
my $filename = $doc->filename
and the text_set method will replace the current text with the one that was passed to it:
$doc->text_set( $text );
These allow us to easily manipulate the content of the current editor.
Plugin to CPAN
Once you accumulated enough interesting tools in your My plug-in, you might consider renaming it and packaging it into a CPAN module for others to enjoy as well.
Questions?
If you have any questions, the best place to ask them are the mailing list and IRC channel of Padre.
Liked it?
If you liked the screencast, please subscribe to my channel and also let others people know about it.