Menu

[r2]: / NppWLangLexer_Plugin / src / NppPluginInterface.cpp  Maximize  Restore  History

Download this file

346 lines (245 with data), 10.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// NppPluginInterface.cpp
// This file is part of the Notepad++ External Lexers Plugin.
// Copyright 2008 - 2009 Thell Fowler (thell@almostautomated.com)
//
// This program is free software; you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation; either version
// 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program;
// if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
// Public and Private Plugin Interface Functions
// for use with Notepad++ Plugins
///////////////////////////////////////////////////////////////////////////////////////////////
/***
*
* This file implements an alternate interface to the standard Notepad++ plugin interface,
* using a namespace with public functions defined in the header and private ones in an
* unnamed namespace in this file.
*
* The commenting in this code may seem excessive, yet it is there for people who would
* like to better understand how the plugin system works and how it can be used.
*
***/
///////////////////////////////////////////////////////////////////////////////////////////////
// Include Directives
/***
*
* The includes provided here are for the base plugin interface.
* To extend the plugin interface include a header that extends the namespace.
*
* An example of this is the NppPluginInterface_ExternalLexer.h which adds features to
* the namespace for use with Scintilla's external lexer api.
*
***/
# include "NppPluginInterface.h" // Provides definitions for the public plugin interface.
//---------------------------------------------------------------------------------------------
// Additional library includes.
#include <algorithm> // Utility functions for working with the vectors.
#include <iterator> // Iterator for merging the vectors, and crtdgb.h for _ASSERT.
///////////////////////////////////////////////////////////////////////////////////////////////
// Main interface implementation.
namespace Npp_Plugin_Interface { // <--- Namespace --->
//*********************************************************************************************
// Private interface implementation.
namespace { // <--- Unnamed namespace for private variables --->
//*********************************************************************************************
// Private variables
// <--- Npp Data --->
tstring _Name; // Name passed to NPP for use in the 'Plugins' menu.
HANDLE _hModule; // Notepad++'s handle for this plugin module.
HWND _hNpp; // Handle to Notepad++ for messaging.
HWND _hScintillaMain; // Handle to Notepad++'s main view for messaging.
HWND _hScintillaSecond; // Handle to Notepad++'s second view for messaging.
HWND _hCurrView; // Handle to the currently active Notepad++ view for messaging.
bool _bRefreshCurrViewH; // Indicates if hCurrView handle needs updating.
bool _isReady; // Flag if Notepad++ has finished initalizing.
// <--- Function item vectors --->
std::vector<FuncItem> _PluginFuncVector; // Container for this plugin's function commands.
//*********************************************************************************************
// Private functions
#ifdef UNICODE
extern "C" __declspec(dllexport) BOOL isUnicode()
{
// Notepad++ comes in two flavors, ansi and unicode, this plugin is made for the unicode version.
return TRUE;
}
#endif // End: UNICODE
extern "C" __declspec(dllexport) void setInfo(NppData NppData)
{
/*
*
* Since these values should not be changed during runtime they are stored in
* this 'private' area with public functions providing access to them.
*
*/
//PluginsManager forwards Notepad++'s handle information for use in messaging.
_ASSERT(!_hNpp); // Assert that these handles haven't already been set.
_hNpp = NppData._nppHandle;
_hScintillaMain = NppData._scintillaMainHandle;
_hScintillaSecond = NppData._scintillaSecondHandle;
}
HWND updateCurrViewHandle() // Updates handle pointer to the currently active view.
{
int currentEdit;
::SendMessage(_hNpp, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&currentEdit);
_bRefreshCurrViewH = false;
return ( currentEdit == 0 ) ? ( _hScintillaMain ): ( _hScintillaSecond );
}
} // End: Unnamed namespace for private variables
//*********************************************************************************************
// Publicly defined functions.
void init(tstring name, HANDLE hModule) // Initialize Plugin.
{
/*
* A plugin is really a singleton from the point of view of the plugin so control over
* initialization and variable updates is required, this should happen in your sources'
* DllMain function.
*
*/
_ASSERT( _Name.empty() ); // Assert that these values haven't already been set.
_ASSERT( _Name.size() <= nbChar ); // Ensure this plugin stays within name size limits.
_Name.assign(name);
_hModule = hModule;
}
void setPluginFuncItem(tstring Name, PFUNCPLUGINCMD pFunction,
int cmdID, bool init2Check, ShortcutKey* pShKey)
{
/*
* This function stores Function Items to the FuncItem vector.
*
* If three basic entries 'separator', 'help', and 'about' are stored as PluginFuncItems
* and an extension to this namespace provides another FuncItems vector, the two can
* be merged while at the same time ensuring the main plugins menu functions are at
* the bottom (or top) of the plugin's menu in Npp.
*
*/
// Notify if length is too long.
if ( !( Name.length() < nbChar ) )
::MessageBox(Npp_Plugin_Interface::hNpp(),
TEXT("Function name is too long and will be truncated."),
TEXT("Function Item Name Alert"),
MB_ICONINFORMATION);
FuncItem thisFunction;
Name.copy(thisFunction._itemName, Name.length());
thisFunction._itemName[ Name.length() ] = '\0';
thisFunction._cmdID = cmdID;
thisFunction._pFunc = pFunction;
thisFunction._init2Check = init2Check;
thisFunction._pShKey = pShKey;
_PluginFuncVector.push_back(thisFunction);
}
HANDLE hModule() // Plugin module's handle.
{
return _hModule;
}
HWND hNpp() // Returns the main Notepad++ handle.
{
return _hNpp;
}
/*
*
* The hMainView and hSecondView sometimes need to be used instead of the hCurrView when
* a message must reach both. For instance a lexer plugin that works with highlights
* (indicators) and makes a change to the highlighter's value will only have it updated in
* the currently focused view unless the message is sent to both.
*
*/
// Returns handle used in messaging Notepad++'s main view.
HWND hMainView()
{
return _hScintillaMain;
}
// Returns handle used in messaging Notepad++'s secondary view.
HWND hSecondView()
{
return _hScintillaSecond;
}
// Returns handle for the currently focused Notepad++ view.
HWND hCurrView()
{
/*
*
* When messages need to be sent a handle must be provided, this handle points
* to the View that is currently in focus.
* The bRefreshCurrViewH will force it to be updated during run-time. This can have lots
* of uses; for example, a lexer plugin may need to verify the language of a buffer after
* activation hCurrView() will ensure the message is sent to the correct handle.
*
*/
if ( _bRefreshCurrViewH || !_isReady ) {
_hCurrView = updateCurrViewHandle();
}
return _hCurrView;
}
// Returns the current target of hCurrView(). 0 for main view and 1 for second view.
int intCurrView()
{
return ( ( hCurrView() == hMainView() ) ? ( 0 ) : ( 1 ) );
}
void hCurrViewNeedsUpdate()
{
_bRefreshCurrViewH = true;
}
// Returns Notepad++'s initialization state.
bool isNppReady()
{
/*
*
* The NppReady flag can be used to control a plugins activites during Npp startup.
* The hCurrView() forces on update of the handle on each message until this is set.
*
*/
return _isReady;
}
// The NppReady flag can be used to control a plugins activites during Npp startup.
// The hCurrView() function forces an update of the handle on each message processed
// until this is set.
void setNppReady()
{
_isReady = true;
}
// Provides a response to PluginsManager's getFuncArray call and allows for extensions
// to work with generating an array to send back that includes their own FuncItems.
FuncItem * getPluginFuncArray()
{
return ( &_PluginFuncVector[0] );
}
// Provides a response for a namespace's extension to retrieve and work with the plugin's
// function item vector. This is useful in merging and ordering function items together
// before responding to the Npp plugin manager's getFuncArray() call.
//
// <--- See the NppExtLexerInterface for an example --->
//
std::vector<FuncItem> getPluginFuncVector()
{
return ( _PluginFuncVector );
}
// Provides a response to PluginsManager's getFuncArray call and allows for extensions
// to work with generating an array to send back that includes their own FuncItems.
int getPluginFuncCount()
{
return _PluginFuncVector.size();
}
//*********************************************************************************************
// Exported Functions
/*
* These exported functions facilitate registering this plugin
* into the Notepad++ Plugins Manager and the Scintilla
* Lexer Library.
*
*/
//---------------------------------------------------------------------------------------------
// Plugin Module Details - for Notepad++'s PluginsManager
// PluginsManager retrieves the 'module name' for it's internal use in messaging
// and function pointer control.
extern "C" __declspec(dllexport) const TCHAR * getName()
{
return &_Name[0];
}
} // End namespace: Npp_Plugin_Interface
MongoDB Logo MongoDB