Win32::Mechanize::NotepadPlusPlus::Notepad - The main application object for Notepad++ automation
use Win32::Mechanize::NotepadPlusPlus ':main'; my $npp = notepad(); # main application
The editor object for Notepad++ automation using Win32::Mechanize::NotepadPlusPlus
The Constructors and similar object methods in this section are purely for class access, and will be called by the NotepadPlusPlus object. They should never need to be referenced directly. (Instead, you will get the notepad, editor1, editor2, and editor instances from the app instance)
notepad
editor1
editor2
editor
use Win32::Mechanize::NotepadPlusPlus; # creates the singleton ::Notepad object my $npp = Win32::Mechanize::NotepadPlusPlus::notepad(); # calls ...Notepad::notepad() my $ed1 = Win32::Mechanize::NotepadPlusPlus::editor1(); # calls ...Notepad::editor1() my $ed2 = Win32::Mechanize::NotepadPlusPlus::editor2(); # calls ...Notepad::editor2() my $ed = Win32::Mechanize::NotepadPlusPlus::editor(); # calls ...Notepad::editor()
This API was based on the Notepad++ plugin PythonScript's API for the Notepad object.
These methods open, close, and save files (standard File menu operations).
Closes the currently active document
Closes all open documents
Closes all but the currently active document
Create a new document.
Opens the given file.
Save the current file
Saves all currently unsaved files
Save the current file as the specified $filename
Save the current file as the specified $filename, but don’t change the filename for the buffer in Notepad++
Sessions allow you to make a group of files that you can easily reload by loading the session.
Save the current session (list of open files) to a file.
Saves a session (list of filenames in @filesList) to a file.
Opens the session by loading all the files listed in the $sessionFilename.
Reads the session stored in $sessionFilename, and returns a list of the file paths that it references.
This does not open the files in the session; to do that, use notepad()->loadSession($sessionFilename)
notepad()->loadSession($sessionFilename)
These methods influence which views are available and which file buffers are available in which views; they also read or manipulate the information about the files in these buffers.
Views relate to the one or two editor windows inside Notepad++. Buffers are the individual file-editing buffers in each view. Because each view has a group of buffers, each buffer has an index within that view.
These methods allow you to change which file buffer is active in a given view, and get information about which view and buffer are active.
Activates the given $bufferID
Activates the buffer with the given $filename, regardless of view.
Activates the document with the given $view and $index. view is 0 or 1.
Gets the bufferID of the currently active buffer
Gets the current active index for the given $view (0 or 1)
Get the currently active view (0 or 1)
Moves the active file from one view to another
Clones the active file from one view to the other, so it's now available in both views (which makes it easy to look at different sections of the same file)
These methods allow you to get the filename for a selected or active buffer, or get the list of all currently-open files.
Gets the filename of the selected buffer.
If $bufferid is omitted, it will get the filename of the active document
Gets the filename of the active document
Gets a list of the open filenames.
Returns: A reference to an array-of-arrays, one row for each file, with filename, bufferID, index, and view as the elements of each row:
[ [$filename1, $bufferID1, $index1, $view1], ... [$filenameN, $bufferIDN, $indexN, $viewN] ]
Returns the number of open files in $view, which should be 0 or 1. If undef or $view not given, return total number of files open in either view.
undef
These methods allow you to determine or change the active language parser for the buffers.
Get the current language type
Returns: LANGTYPE
Gets the language type of the given $bufferID. If no $bufferID is given, then the language of the currently active buffer is returned.
Returns: An integer that corresponds to
Set the language type for the currently-active buffer.
Sets the language type of the given bufferID. If not bufferID is given, sets the language for the currently active buffer.
Get the name and or longer description for the given language $langType.
Determines the encoding for a given file, and determines or changes the EOL-style for the file buffer.
Gets the encoding of the given bufferID. If no bufferID is given, then the encoding of the currently active buffer is returned.
Returns: An integer corresponding to how the buffer is encoded
Gets the EOL format type (i.e. Windows [0], Unix [1] or old Mac EOL [2]) of the given bufferID. If no bufferID is given, then the format of the currently active buffer is returned.
Returns: The integers 0,1,or 2, corresponding to Windows EOL (CRLF), Unix/Linux (LF), or the old Mac EOL (CR).
Sets the EOL format type (i.e. Windows [0], Unix [1] or old Mac EOL [2]) of the specified buffer ID. If not bufferID is passed, then the format type of the currently active buffer is set.
These methods allow you to reload the contents of the appropriate buffer from what is on disk.
Reloads the given $bufferID
Reloads the buffer of the current document
Reloads the buffer for the given $filename
When automating Notepad++, there are times when you may want an extra Scintilla Editor instance, even though it never needs to be seen inside the Notepad++ window. You can create and destroy hidden instances using these methods
Create a new Scintilla handle. Returns an Editor object. This Scintilla editor instance is not available to be displayed in either view, but in all other ways behaves like the main Scintilla Editor instances.
If $parentHwnd is passed (non-zero), that HWND will be used as the parent window for the new Scintilla; otherwise, Notepad++ itself will be used as the parent.
$parentHwnd
Please note: as of v7.8, there is no way to properly destroy the created Scintilla handle. There are a limited number of Scintilla handles that can be allocated before.
This method always returns a true, and warns that the method is deprecated.
In Notepad++ v7.7.1 and earlier, the NPPM_DESTROYSCINTILLAHANDLE tried to destroy the scintilla instance. However, this could crash Notepad++, so as of v7.8, Notepad++ ignores this message. To prevent Win32::Mechanize::NotepadPlusPlus from crashing Notepad++, the destroyScintilla() does not bother to send the message (in case it's Notepad++ v7.7.1 or earlier).
destroyScintilla()
Hides the menu bar.
RETURN: The previous state: it will return 1 if it was hidden before, or 0 if it was shown before
Shows the menu bar
Returns 1 if the menu bar is currently hidden, 0 if it is shown.
Hides the Tab bar.
Shows the Tab bar
Returns 1 if the tab bar is currently hidden, 0 if it is shown.
Hides the toolbar.
Shows the toolbar
Returns 1 if the toolbar is currently hidden, 0 if it is shown.
Hides the status bar.
Shows the status bar
Returns 1 if the status bar is currently hidden, 0 if it is shown.
Sets the status bar text. For statusBarSection, use one of the STATUSBARSECTION constants.
Gets the status bar text. For statusBarSection, use one of the STATUSBARSECTION constants.
NOT IMPLEMENTED (there is no Notepad++ Message "NPPM_GETSTATUSBAR").
Gets the handle for the main Notepad++ application menu (which contains File, Edit, Search, ...).
Gets the handle for the Plugins menu.
Runs a Notepad++ menu command. Use the MENUCOMMAND enum (%nppidm below), or integers directly from the nativeLang.xml file, or the string name from the MENUCOMMAND enum.
%nppidm
Runs a command from the menus. For built-in menus use notepad.menuCommand(), for non built-in menus (e.g. TextFX and macros you’ve defined), use notepad.runMenuCommand(menuName, menuOption). For other plugin commands (in the plugin menu), use Notepad.runPluginCommand(pluginName, menuOption).
notepad.menuCommand()
notepad.runMenuCommand(menuName, menuOption)
Notepad.runPluginCommand(pluginName, menuOption)
Menus are searched for the text, and when found, the internal ID of the menu command is cached. When runMenuCommand is called, the cache is first checked if it holds the internal ID for the given menuName and menuOption. If it does, it simply uses the value from the cache. If the ID could have been updated (for example, you’re calling the name of macro that has been removed and added again), set refreshCache to any Perl expression that evaluates as True.
@menuNames is a one-or-more element list of strings; each string can either be a name from the menu hierarchy (either a menu name or a command name) or a pipe-separated string with multiple names. See the example below.
@menuNames
Returns: True if the menu command was found, otherwise False
e.g.:
notepad()->runMenuCommand('Tools', 'SHA-256', 'Generate from selection into clipboard'); notepad()->runMenuCommand('Tools', 'SHA-256 | Generate from selection into clipboard'); notepad()->runMenuCommand('Tools | SHA-256', 'Generate from selection into clipboard'); notepad()->runMenuCommand('Tools | SHA-256 | Generate from selection into clipboard'); notepad()->runMenuCommand('Macro', 'Trim Trailing Space and Save', { refreshCache => 1 });
Runs a command from the plugin menu. Use to run direct commands from the Plugins menu. To call TextFX or other menu functions, either use notepad.menuCommand(menuCommand) (for Notepad++ menu commands), or notepad.runMenuCommand(menuName, menuOption) for TextFX or non standard menus (such as macro names).
notepad.menuCommand(menuCommand)
Note that menuOption can be a submenu in a plugin’s menu. So:
notepad.runPluginCommand('Python Script', 'demo script')
Could run a script called “demo script” from the Scripts submenu of Python Script.
Menus are searched for the text, and when found, the internal ID of the menu command is cached. When runPluginCommand is called, the cache is first checked if it holds the internal ID for the given menuName and menuOption. If it does, it simply uses the value from the cache. If the ID could have been updated (for example, you’re calling the name of macro that has been removed and added again), set refreshCache to True. This is False by default.
notepad.runPluginCommand(‘XML Tools’, ‘Pretty Print (XML only)’)
Displays a message box with the given message and title.
Flags can be 0 for a standard ‘OK’ message box, or a combination of MESSAGEBOXFLAGS. title is “Python Script for Notepad++” by default, and flags is 0 by default.
Returns: A RESULTxxxx member of MESSAGEBOXFLAGS as to which button was pressed.
Prompts the user for some text. Optionally provide the default text to initialise the entry field.
Returns: The string entered.
None if cancel was pressed (note that is different to an empty string, which means that no input was given)
For any messages not implemented in the API, if you know the appropriate $msgid, and what are needed as $wparam and $lparam, you can send the message to the Notepad GUI directly.
If you have developed a wrapper for a missing message, feel free to send in a Pull Request, or open an issue, including your wrapper code.
use Win32::Mechanize::NotepadPlusPlus ':vars';
This hash contains maps all known message names from Notepad_plus_msgs.h, which are useful for passing to the SendMessage method.
SendMessage
You can find out the names and values of all the messages using:
use Win32::Mechanize::NotepadPlusPlus ':vars'; printf "%-40s => %s\n", $_, $nppm{$_} for sort keys %nppm;
This hash contains maps all known message names from menuCmdID.h, which are useful for passing to the SendMessage method with the NPPM_MENUCOMMAND message.
All of these should be accessible through the notepad()->runMenuCommand() method as well.
use Win32::Mechanize::NotepadPlusPlus ':vars'; printf "%-40s => %s\n", $_, $nppidm{$_} for sort keys %nppidm;
These give details about the current instance of Notepad++, or the Perl Library, or Perl itself.
Gets the Notepad++ version as a string.
(This was called getVersion in the PythonScript API.)
Gets the PythonScript plugin version as a string.
Gets the Perl interpreter version as a string.
Gets the Perl interpreter bits-information (32-bit vs 64-bit) as an integer.
Gets the command line used to start Notepad++
NOT IMPLEMENTED. RETURNS undef. (May be implemented in the future.)
Gets the directory Notepad++ is running in (i.e. the location of notepad++.exe)
Gets the plugin config directory.
Callbacks are functions that are registered to various events.
FUTURE: they were in the PythonScript plugin, but I don't know if they'll be able to work in the remote perl module. If I ever integrated more tightly with a Notepad++ plugin, it may be that they can be implemented then.
Installed as part of Win32::Mechanize::NotepadPlusPlus
Peter C. Jones <petercj AT cpan DOT org>
<petercj AT cpan DOT org>
Please report any bugs or feature requests emailing <bug-Win32-Mechanize-NotepadPlusPlus AT rt.cpan.org> or thru the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Win32-Mechanize-NotepadPlusPlus, or thru the repository's interface at https://github.com/pryrt/Win32-Mechanize-NotepadPlusPlus/issues.
<bug-Win32-Mechanize-NotepadPlusPlus AT rt.cpan.org>
Copyright (C) 2019,2020 Peter C. Jones
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information.
To install Win32::Mechanize::NotepadPlusPlus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::Mechanize::NotepadPlusPlus
CPAN shell
perl -MCPAN -e shell install Win32::Mechanize::NotepadPlusPlus
For more information on module installation, please visit the detailed CPAN module installation guide.