Skip to content

Commit bc0e9c6

Browse files
committed
Run Plugin Commands
- Run plugin commands - Run menu commands (eg. TextFX) - /W4 warning cleanup - pysearch and pymlsearch helper functions
1 parent 1d4bfa8 commit bc0e9c6

20 files changed

+348
-84
lines changed

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
5757
<ClCompile>
5858
<PrecompiledHeader>Use</PrecompiledHeader>
59-
<WarningLevel>Level3</WarningLevel>
59+
<WarningLevel>Level4</WarningLevel>
6060
<Optimization>Disabled</Optimization>
6161
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;PYTHONSCRIPT2010_EXPORTS;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
6262
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -130,7 +130,9 @@
130130
<ClCompile Include="..\src\ScintillaWrapper.cpp" />
131131
<ClCompile Include="..\src\ScintillaWrapperGenerated.cpp" />
132132
<ClCompile Include="..\src\ShortcutDlg.cpp" />
133-
<ClCompile Include="..\src\stdafx.cpp" />
133+
<ClCompile Include="..\src\stdafx.cpp">
134+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
135+
</ClCompile>
134136
<ClCompile Include="..\src\WcharMbcsConverter.cpp" />
135137
</ItemGroup>
136138
<ItemGroup>

PythonScript/src/AboutDialog2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void AboutDialog::doDialog()
2323
}
2424

2525

26-
BOOL CALLBACK AboutDialog::run_dlgProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
26+
BOOL CALLBACK AboutDialog::run_dlgProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM /* lParam */)
2727
{
2828
switch (Message)
2929
{

PythonScript/src/ConsoleDialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ BOOL ConsoleDialog::run_dlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
8484

8585
}
8686

87-
return FALSE;
8887
}
8988

9089

@@ -129,7 +128,7 @@ void ConsoleDialog::historyPrevious()
129128

130129
void ConsoleDialog::historyNext()
131130
{
132-
if (m_currentHistory < m_history.size())
131+
if (static_cast<size_t>(m_currentHistory) < m_history.size())
133132
{
134133
char buffer[1000];
135134
GetWindowTextA(m_hInput, buffer, 1000);

PythonScript/src/CreateWrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
}
5252

5353
castsRet = {
54-
'bool' : lambda val: 'return static_cast<bool>(' + val + ')',
54+
'bool' : lambda val: 'return 0 != (' + val + ')',
5555
'boost::python::tuple': lambda val: 'int retVal = callScintilla(' + val + ');\n\treturn make_tuple(COLOUR_RED(retVal), COLOUR_GREEN(retVal), COLOUR_BLUE(retVal))'
5656

5757
}

PythonScript/src/MenuManager.cpp

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ HMENU MenuManager::getOurMenu()
227227
if (NULL == m_pythonPluginMenu)
228228
{
229229
HMENU hPluginMenu = (HMENU)::SendMessage(m_hNotepad, NPPM_GETMENUHANDLE, 0, 0);
230-
HMENU hPythonMenu = NULL;
230+
231231
int iMenuItems = GetMenuItemCount(hPluginMenu);
232232
for ( int i = 0; i < iMenuItems; i++ )
233233
{
@@ -711,4 +711,100 @@ void MenuManager::deleteInstance()
711711
delete s_menuManager;
712712
s_menuManager = NULL;
713713
}
714+
}
715+
716+
717+
int MenuManager::findPluginCommand(const TCHAR *pluginName, const TCHAR *menuOption)
718+
{
719+
720+
HMENU hPluginMenu = (HMENU)::SendMessage(m_hNotepad, NPPM_GETMENUHANDLE, 0, 0);
721+
722+
int iMenuItems = GetMenuItemCount(hPluginMenu);
723+
TCHAR strBuffer[500];
724+
725+
for ( int i = 0; i < iMenuItems; ++i )
726+
{
727+
728+
::GetMenuString(hPluginMenu, i, strBuffer, 500, MF_BYPOSITION);
729+
if (0 == _tcsicmp(pluginName, strBuffer))
730+
{
731+
HMENU hSubMenu = ::GetSubMenu(hPluginMenu, i);
732+
733+
int subMenuItems = ::GetMenuItemCount(hSubMenu);
734+
for (int subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos)
735+
{
736+
737+
TCHAR *context = NULL;;
738+
::GetMenuString(hSubMenu, subMenuPos, strBuffer, 500, MF_BYPOSITION);
739+
TCHAR *name = _tcstok_s(strBuffer, _T("\t"), &context);
740+
741+
if (name && 0 == _tcsicmp(menuOption, name))
742+
{
743+
return ::GetMenuItemID(hSubMenu, subMenuPos);
744+
}
745+
}
746+
// We've found the plugin, but not the option, so no point continuing
747+
break;
748+
}
749+
750+
}
751+
752+
return 0;
753+
}
754+
755+
756+
757+
int MenuManager::findMenuCommand(const TCHAR *menuName, const TCHAR *menuOption)
758+
{
759+
HMENU hMenuBar = ::GetMenu(m_hNotepad);
760+
761+
return findMenuCommand(hMenuBar, menuName, menuOption);
762+
}
763+
764+
765+
int MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *menuName, const TCHAR *menuOption)
766+
{
767+
int iMenuItems = GetMenuItemCount(hParentMenu);
768+
int retVal = 0;
769+
770+
TCHAR strBuffer[500];
771+
772+
for ( int i = 0; i < iMenuItems; ++i )
773+
{
774+
MENUITEMINFO mii;
775+
mii.cbSize = sizeof(MENUITEMINFO);
776+
mii.fMask = MIIM_ID | MIIM_STRING | MIIM_SUBMENU;
777+
mii.cch = 500;
778+
mii.dwTypeData = strBuffer;
779+
780+
::GetMenuItemInfo(hParentMenu, i, TRUE, &mii);
781+
782+
if (NULL != mii.hSubMenu && 0 == _tcsicmp(menuName, strBuffer))
783+
{
784+
int subMenuItems = ::GetMenuItemCount(mii.hSubMenu);
785+
for (int subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos)
786+
{
787+
TCHAR *context = NULL;;
788+
::GetMenuString(mii.hSubMenu, subMenuPos, strBuffer, 500, MF_BYPOSITION);
789+
TCHAR *name = _tcstok_s(strBuffer, _T("\t"), &context);
790+
791+
if (name && 0 == _tcsicmp(menuOption, name))
792+
{
793+
return ::GetMenuItemID(mii.hSubMenu, subMenuPos);
794+
}
795+
}
796+
}
797+
798+
if (NULL != mii.hSubMenu)
799+
{
800+
retVal = findMenuCommand(mii.hSubMenu, menuName, menuOption);
801+
// If we've found it in the sub menu (or within the sub menu)
802+
if (0 != retVal)
803+
break;
804+
}
805+
806+
}
807+
808+
return retVal;
809+
714810
}

PythonScript/src/MenuManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class MenuManager
4848

4949
void configureToolbarIcons();
5050

51+
int findPluginCommand(const TCHAR *pluginName, const TCHAR *menuOption);
52+
int findMenuCommand(const TCHAR *menuName, const TCHAR *menuOption);
53+
int findMenuCommand(HMENU parentMenu, const TCHAR *menuName, const TCHAR *menuOption);
5154
static int s_startCommandID;
5255
static int s_endCommandID;
5356
static int s_startFixedID;

PythonScript/src/NotepadPlusWrapper.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "NotepadPlusWrapper.h"
55
#include "menuCmdID.h"
66
#include "PromptDialog.h"
7+
#include "MenuManager.h"
78

89
using namespace std;
910
using namespace boost::python;
@@ -166,7 +167,7 @@ void NotepadPlusWrapper::open(const char *filename)
166167

167168
bool NotepadPlusWrapper::activateFile(const char *filename)
168169
{
169-
return (bool)callNotepad(NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2tchar(filename).get()));
170+
return 0 != callNotepad(NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2tchar(filename).get()));
170171
}
171172

172173
int NotepadPlusWrapper::getCurrentView()
@@ -663,3 +664,38 @@ boost::python::str NotepadPlusWrapper::getCurrentFilename()
663664
}
664665

665666

667+
bool NotepadPlusWrapper::runPluginCommand(boost::python::str pluginName, boost::python::str menuOption)
668+
{
669+
MenuManager *menuManager = MenuManager::getInstance();
670+
if (!pluginName.is_none() && !menuOption.is_none())
671+
{
672+
shared_ptr<TCHAR> tpluginName = WcharMbcsConverter::char2tchar(extract<const char *>(pluginName));
673+
shared_ptr<TCHAR> tmenuOption = WcharMbcsConverter::char2tchar(extract<const char *>(menuOption));
674+
int commandID = menuManager->findPluginCommand(tpluginName.get(), tmenuOption.get());
675+
if (commandID)
676+
{
677+
::SendMessage(m_nppHandle, WM_COMMAND, commandID, 0);
678+
return true;
679+
}
680+
}
681+
return false;
682+
683+
}
684+
685+
bool NotepadPlusWrapper::runMenuCommand(boost::python::str menuName, boost::python::str menuOption)
686+
{
687+
MenuManager *menuManager = MenuManager::getInstance();
688+
if (!menuName.is_none() && !menuOption.is_none())
689+
{
690+
shared_ptr<TCHAR> tmenuName = WcharMbcsConverter::char2tchar(extract<const char *>(menuName));
691+
shared_ptr<TCHAR> tmenuOption = WcharMbcsConverter::char2tchar(extract<const char *>(menuOption));
692+
int commandID = menuManager->findMenuCommand(tmenuName.get(), tmenuOption.get());
693+
if (commandID)
694+
{
695+
::SendMessage(m_nppHandle, WM_COMMAND, commandID, 0);
696+
return true;
697+
}
698+
}
699+
return false;
700+
701+
}

PythonScript/src/NotepadPlusWrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ class NotepadPlusWrapper
518518

519519
boost::python::str NotepadPlusWrapper::getCurrentFilename();
520520

521+
bool runPluginCommand(boost::python::str pluginName, boost::python::str menuOption);
522+
bool runMenuCommand(boost::python::str menuName, boost::python::str menuOption);
523+
521524
bool callback(PyObject* callback, boost::python::list events);
522525

523526
void clearAllCallbacks();

PythonScript/src/NotepadPython.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ void export_notepad()
5555
.def("messageBox", &NotepadPlusWrapper::messageBox, "Displays a message box. messageBox(message, title, flags).\nFlags can be 0 for a standard 'OK' message box, or a combination of MessageBoxFlags")
5656
.def("prompt", &NotepadPlusWrapper::promptDefault, "Prompts the user for some text. enteredText = prompt(prompt, title[, defaultText])")
5757
.def("prompt", &NotepadPlusWrapper::prompt, "Prompts the user for some text. enteredText = prompt(prompt, title[, defaultText])")
58+
.def("runPluginCommand", &NotepadPlusWrapper::runPluginCommand, "Runs a command from the plugin menu.\nUse to run direct commands from the Plugins menu.\nTo call TextFX or other menu functions, either use notepad.menuCommand() (for Notepad++ menu commands), or notepad.runMenuCommand(menuName, menuOption) for TextFX or non built-in menus.\nrunPluginCommand(pluginName, menuOptionTitle)")
59+
.def("runMenuCommand", &NotepadPlusWrapper::runMenuCommand, "Runs a command from the menus. For built-in menus use notepad.menuCommand(), for non built-in menus (e.g. TextFX), use notepad.runMenuCommand(menuName, menuOption).\n runMenuCommand('TextFX Edit', 'Delete Blank Lines')")
5860
.def("clearCallbacks", &NotepadPlusWrapper::clearAllCallbacks, "Clears all callbacks")
5961
.def("clearCallbacks", &NotepadPlusWrapper::clearCallbackFunction, "Clears all callbacks for a given function")
6062
.def("clearCallbacks", &NotepadPlusWrapper::clearCallbackEvents, "Clears all callbacks for the given list of events")

PythonScript/src/PromptDialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ BOOL CALLBACK PromptDialog::dlgProc(HWND hWnd, UINT message, WPARAM wParam, LPAR
5454
}
5555

5656

57-
BOOL CALLBACK PromptDialog::runDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
57+
BOOL CALLBACK PromptDialog::runDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM /* lParam */)
5858
{
5959
switch(message)
6060
{

0 commit comments

Comments
 (0)